<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xinda.iot.mapper.ThingsModelStandardMapper">

    <resultMap type="com.xinda.iot.domain.ThingsModelStandard" id="ThingsModelStandardResult">
        <result property="templateId"    column="template_id"    />
        <result property="templateName"    column="template_name"    />
        <result property="standardId"    column="standard_id"    />
        <result property="standardName"    column="standard_name"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="tenantName"    column="tenant_name"    />
        <result property="identifier"    column="identifier"    />
        <result property="type"    column="type"    />
        <result property="datatype"    column="datatype"    />
        <result property="places"    column="places"    />
        <result property="specs"    column="specs"    />
        <result property="isCalc" column="is_calc"/>
        <result property="isChart" column="is_chart"/>
        <result property="isSys"    column="is_sys"    />
        <result property="isReadonly"    column="is_readonly"    />
        <result property="isSharePerm"    column="is_share_perm"    />
        <result property="isHistory"    column="is_history"    />
        <result property="isMonitor"    column="is_monitor"    />
        <result property="isApp" column="is_app"/>
        <result property="formula"    column="formula"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="modelOrder"    column="model_order"    />
    </resultMap>

    <sql id="selectThingsModelStandardVo">
        select template_id, template_name, standard_id, standard_name, tenant_id, tenant_name, identifier, type, datatype, places, specs, is_calc,
               is_chart,  is_sys, is_readonly, is_share_perm, is_history, is_monitor, formula, del_flag, create_by, create_time, update_by, update_time, remark, model_order from iot_things_model_standard
    </sql>

    <select id="selectThingsModelStandardList" parameterType="com.xinda.iot.domain.ThingsModelStandard" resultMap="ThingsModelStandardResult">
        select m.template_id,m.template_name,m.standard_id,m.standard_name,
        m.tenant_id, m.tenant_name, m.identifier, m.type, m.datatype, m.places,  m.specs, m.is_calc,  m.is_chart,  m.is_sys, m.is_readonly, m.is_chart, m.is_share_perm, m.is_history, m.is_monitor, m.formula, m.del_flag, m.create_by, m.create_time, m.update_by, m.update_time, m.remark, m.model_order
        from iot_things_model_standard m
        <where>
            <if test="templateName != null  and templateName != ''"> and m.template_name like concat('%', #{templateName}, '%')</if>
            <if test="type != null "> and m.type = #{type}</if>
            <if test="isCalc != null ">
                and is_calc = #{isCalc}
            </if>
            <if test="isChart != null ">
                and is_chart = #{isChart}
            </if>
            <if test="specs != null  and specs != ''"> and m.specs = #{specs}</if>
            <if test="tenantId != null  and tenantId != ''"> and (m.tenant_id = #{tenantId} or m.is_sys = 1)</if>
            <if test="standardId != null  and standardId != ''"> and (m.standard_id = #{standardId})</if>
        </where>
        order by m.model_order ASC
    </select>


    <select id="checkUnique" parameterType="com.xinda.iot.domain.ThingsModelStandard" resultType="java.lang.Boolean">
        SELECT COUNT(*) > 0
        FROM  iot_things_model_standard
        <where>
            standard_id = #{standardId} and identifier = #{identifier}
        <if test="templateId != null  and templateId != ''"> and template_id != #{templateId}</if>
        </where>
    </select>


    <select id="selectModelListByStandardId" parameterType="Long" resultMap="ThingsModelStandardResult">
        select m.template_id,m.template_name,m.standard_id,m.standard_name,
        m.tenant_id, m.tenant_name, m.identifier, m.type, m.datatype,  m.places,m.specs,m.is_calc, m.is_chart, m.is_share_perm, m.is_sys, m.is_readonly,  m.is_history, m.is_monitor,m.formula, m.del_flag, m.create_by, m.create_time, m.update_by, m.update_time, m.remark, m.model_order
        from iot_things_model_standard m
        <where>
            m.standard_id = #{standardId}
        </where>
        order by m.model_order ASC
    </select>

    <select id="selectThingsModelStandardByTemplateId" resultMap="ThingsModelStandardResult">
        select m.template_id,m.template_name, m.standard_id, m.standard_name,m.tenant_id, m.tenant_name, m.identifier, m.type, m.datatype,  m.places,m.specs, m.is_calc,  m.is_sys, m.is_readonly, m.is_chart, m.is_share_perm, m.is_history,m.is_monitor, m.formula, m.del_flag, m.create_by, m.create_time, m.update_by, m.update_time, m.remark, m.model_order
        from iot_things_model_standard m
        where m.template_id = #{templateId}
    </select>

    <select id="selectThingsModelStandardByTemplateIds" resultMap="ThingsModelStandardResult">
        select m.template_id, m.template_name, m.standard_id, m.standard_name,m.tenant_id, m.tenant_name, m.identifier, m.type, m.datatype, m.places, m.specs,m.is_calc, m.is_chart, m.is_sys, m.is_readonly,  m.is_share_perm, m.is_history, m.is_monitor,m.formula, m.del_flag, m.create_by, m.create_time, m.update_by, m.update_time, m.remark, m.model_order
        from iot_things_model_standard m
        where m.template_id in
        <foreach item="templateId" collection="templateIds" open="(" separator="," close=")">
            #{templateId}
        </foreach>
    </select>

    <insert id="insertThingsModelStandard" parameterType="com.xinda.iot.domain.ThingsModelStandard" useGeneratedKeys="true" keyProperty="templateId">
        insert into iot_things_model_standard
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="templateName != null and templateName != ''">template_name,</if>
            <if test="standardId != null">standard_id,</if>
            <if test="standardName != null and standardName != ''">standard_name,</if>
            <if test="tenantId != null">tenant_id,</if>
            <if test="tenantName != null and tenantName != ''">tenant_name,</if>
            <if test="identifier != null and identifier != ''">identifier,</if>
            <if test="type != null">type,</if>
            <if test="datatype != null and datatype != ''">datatype,</if>
            <if test="places != null">places,</if>
            <if test="specs != null and specs != ''">specs,</if>
            <if test="isCalc != null">is_calc,</if>
            <if test="isChart != null">is_chart,</if>
            <if test="isSys != null">is_sys,</if>
            <if test="isReadonly != null">is_readonly,</if>
            <if test="isSharePerm != null">is_share_perm,</if>
            <if test="isHistory != null">is_history,</if>
            <if test="isMonitor != null">is_monitor,</if>
            <if test="isApp != null">is_app,</if>
            <if test="formula != null">formula,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="modelOrder != null">model_order,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="templateName != null and templateName != ''">#{templateName},</if>
            <if test="standardId != null">#{standardId},</if>
            <if test="standardName != null and standardName != ''">#{standardName},</if>
            <if test="tenantId != null">#{tenantId},</if>
            <if test="tenantName != null and tenantName != ''">#{tenantName},</if>
            <if test="identifier != null and identifier != ''">#{identifier},</if>
            <if test="type != null">#{type},</if>
            <if test="datatype != null and datatype != ''">#{datatype},</if>
            <if test="places != null and places != ''">#{places},</if>
            <if test="specs != null and specs != ''">#{specs},</if>
            <if test="isCalc != null">#{isCalc},</if>
            <if test="isChart != null"> #{isChart},</if>
            <if test="isSys != null">#{isSys},</if>
            <if test="isReadonly != null">#{isReadonly},</if>
            <if test="isSharePerm != null">#{isSharePerm},</if>
            <if test="isHistory != null">#{isHistory},</if>
            <if test="isMonitor != null">#{isMonitor},</if>
            <if test="isApp != null">#{isApp},</if>
            <if test="formula != null">#{formula},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="modelOrder != null">#{modelOrder},</if>
        </trim>
    </insert>

    <update id="updateThingsModelStandard" parameterType="com.xinda.iot.domain.ThingsModelStandard">
        update iot_things_model_standard
        <trim prefix="SET" suffixOverrides=",">
            <if test="templateName != null and templateName != ''">template_name = #{templateName},</if>
            <if test="standardId != null">standard_id = #{standardId},</if>
            <if test="standardName != null and standardName != ''">standard_name = #{standardName},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="tenantName != null and tenantName != ''">tenant_name = #{tenantName},</if>
            <if test="identifier != null and identifier != ''">identifier = #{identifier},</if>
            <if test="type != null">type = #{type},</if>
            <if test="datatype != null and datatype != ''">datatype = #{datatype},</if>
            <if test="places != null">places = #{places},</if>
            <if test="specs != null and specs != ''">specs = #{specs},</if>
            <if test="isCalc != null"> is_calc = #{isCalc},</if>
            <if test="isChart != null">is_chart = #{isChart},</if>
            <if test="isSys != null">is_sys = #{isSys},</if>
            <if test="isReadonly != null">is_readonly = #{isReadonly},</if>
            <if test="isSharePerm != null">is_share_perm = #{isSharePerm},</if>
            <if test="isHistory != null">is_history = #{isHistory},</if>
            <if test="isMonitor != null">is_Monitor = #{isMonitor},</if>
            <if test="isApp != null">is_app = #{isApp},</if>
            <if test="formula != null">formula = #{formula},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="modelOrder != null">model_order = #{modelOrder},</if>
        </trim>
        where template_id = #{templateId}
    </update>

    <delete id="deleteThingsModelStandardByTemplateId" parameterType="Long">
        delete from iot_things_model_standard where template_id = #{templateId}
    </delete>

    <delete id="deleteThingsModelStandardByStandardId" parameterType="Long">
        delete from iot_things_model_standard where standard_id = #{standardId}
    </delete>

    <delete id="deleteThingsModelStandardByTemplateIds" parameterType="String">
        delete from iot_things_model_standard where template_id in
        <foreach item="templateId" collection="array" open="(" separator="," close=")">
            #{templateId}
        </foreach>
    </delete>

    <insert id="batchThingsModelStandards" parameterType="java.util.List">
        INSERT INTO iot_things_model_standard (
        template_name,
        standard_id,
        standard_name,
        tenant_id,
        tenant_name,
        identifier,
        type,
        datatype,
        places,
        specs,
        is_calc,
        is_chart,
        is_sys,
        is_readonly,
        is_share_perm,
        is_history,
        is_monitor,
        is_app,
        formula,
        del_flag,
        create_by,
        create_time,
        update_by,
        update_time,
        remark,
        model_order
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.templateName},
            #{item.standardId},
            #{item.standardName},
            #{item.tenantId},
            #{item.tenantName},
            #{item.identifier},
            #{item.type},
            #{item.datatype},
            #{item.places},
            #{item.specs},
            #{item.isCalc},
            #{item.isChart},
            #{item.isSys},
            #{item.isReadonly},
            #{item.isSharePerm},
            #{item.isHistory},
            #{item.isMonitor},
            #{item.isApp},
            #{item.formula},
            #{item.delFlag},
            #{item.createBy},
            #{item.createTime},
            #{item.updateBy},
            #{item.updateTime},
            #{item.remark},
            #{item.modelOrder}
            )
        </foreach>
    </insert>


</mapper>
