<?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.xzzn.ems.mapper.EmsPointEnumMatchMapper">
    
    <resultMap type="EmsPointEnumMatch" id="EmsPointEnumMatchResult">
        <result property="id"    column="id"    />
        <result property="matchField"    column="match_field"    />
        <result property="siteId"    column="site_id"    />
        <result property="deviceCategory"    column="device_category"    />
        <result property="enumCode"    column="enum_code"    />
        <result property="enumName"    column="enum_name"    />
        <result property="enumDesc"    column="enum_desc"    />
        <result property="dataEnumCode"    column="data_enum_code"    />
        <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"    />
    </resultMap>

    <sql id="selectEmsPointEnumMatchVo">
        select id, match_field, site_id, device_category, enum_code, enum_name, enum_desc, data_enum_code, create_by, create_time, update_by, update_time, remark from ems_point_enum_match
    </sql>

    <select id="selectEmsPointEnumMatchList" parameterType="EmsPointEnumMatch" resultMap="EmsPointEnumMatchResult">
        <include refid="selectEmsPointEnumMatchVo"/>
        <where>  
            <if test="matchField != null  and matchField != ''"> and match_field = #{matchField}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="deviceCategory != null  and deviceCategory != ''"> and device_category = #{deviceCategory}</if>
            <if test="enumCode != null  and enumCode != ''"> and enum_code = #{enumCode}</if>
            <if test="enumName != null  and enumName != ''"> and enum_name like concat('%', #{enumName}, '%')</if>
            <if test="enumDesc != null  and enumDesc != ''"> and enum_desc = #{enumDesc}</if>
            <if test="dataEnumCode != null  and dataEnumCode != ''"> and data_enum_code = #{dataEnumCode}</if>
        </where>
    </select>
    
    <select id="selectEmsPointEnumMatchById" parameterType="Long" resultMap="EmsPointEnumMatchResult">
        <include refid="selectEmsPointEnumMatchVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsPointEnumMatch" parameterType="EmsPointEnumMatch" useGeneratedKeys="true" keyProperty="id">
        insert into ems_point_enum_match
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="matchField != null">match_field,</if>
            <if test="siteId != null">site_id,</if>
            <if test="deviceCategory != null">device_category,</if>
            <if test="enumCode != null">enum_code,</if>
            <if test="enumName != null">enum_name,</if>
            <if test="enumDesc != null">enum_desc,</if>
            <if test="dataEnumCode != null">data_enum_code,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="matchField != null">#{matchField},</if>
            <if test="siteId != null">#{siteId},</if>
            <if test="deviceCategory != null">#{deviceCategory},</if>
            <if test="enumCode != null">#{enumCode},</if>
            <if test="enumName != null">#{enumName},</if>
            <if test="enumDesc != null">#{enumDesc},</if>
            <if test="dataEnumCode != null">#{dataEnumCode},</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>
         </trim>
    </insert>

    <update id="updateEmsPointEnumMatch" parameterType="EmsPointEnumMatch">
        update ems_point_enum_match
        <trim prefix="SET" suffixOverrides=",">
            <if test="matchField != null">match_field = #{matchField},</if>
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="deviceCategory != null">device_category = #{deviceCategory},</if>
            <if test="enumCode != null">enum_code = #{enumCode},</if>
            <if test="enumName != null">enum_name = #{enumName},</if>
            <if test="enumDesc != null">enum_desc = #{enumDesc},</if>
            <if test="dataEnumCode != null">data_enum_code = #{dataEnumCode},</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>
        </trim>
        where id = #{id}
    </update>

    <update id="updateDataEnumCodeById" parameterType="EmsPointEnumMatch">
        update ems_point_enum_match
        set data_enum_code = #{dataEnumCode}, update_time = now()
        where id = #{id}
    </update>

    <delete id="deleteEmsPointEnumMatchById" parameterType="Long">
        delete from ems_point_enum_match where id = #{id}
    </delete>

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

    <select id="selectList" resultMap="EmsPointEnumMatchResult">
        <include refid="selectEmsPointEnumMatchVo"/>
        <where>
            <if test="matchField != null  and matchField != ''"> and match_field = #{matchField}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="deviceCategory != null  and deviceCategory != ''"> and device_category = #{deviceCategory}</if>
        </where>
    </select>

    <select id="countBySiteId" resultType="int">
        select count(1)
        from ems_point_enum_match
        where site_id = #{siteId}
    </select>

    <delete id="deleteBySiteId">
        delete from ems_point_enum_match
        where site_id = #{siteId}
    </delete>

    <insert id="copyTemplateToSite">
        insert into ems_point_enum_match (
            match_field,
            site_id,
            device_category,
            enum_code,
            enum_name,
            enum_desc,
            data_enum_code,
            create_by,
            create_time,
            update_by,
            update_time,
            remark
        )
        select
            match_field,
            #{targetSiteId},
            device_category,
            enum_code,
            enum_name,
            enum_desc,
            data_enum_code,
            #{operName},
            now(),
            #{operName},
            now(),
            remark
        from ems_point_enum_match
        where site_id = #{templateSiteId}
    </insert>

    <delete id="deleteByScope">
        delete from ems_point_enum_match
        where site_id = #{siteId}
          and device_category = #{deviceCategory}
          and match_field = #{matchField}
    </delete>

</mapper>
