<?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.EmsPointConfigMapper">

    <resultMap type="EmsPointConfig" id="EmsPointConfigResult">
        <result property="id" column="id"/>
        <result property="pointId" column="point_id"/>
        <result property="siteId" column="site_id"/>
        <result property="deviceCategory" column="device_category"/>
        <result property="deviceId" column="device_id"/>
        <result property="pointName" column="point_name"/>
        <result property="dataKey" column="data_key"/>
        <result property="pointDesc" column="point_desc"/>
        <result property="registerAddress" column="register_address"/>
        <result property="dataUnit" column="data_unit"/>
        <result property="dataA" column="data_a"/>
        <result property="dataK" column="data_k"/>
        <result property="dataB" column="data_b"/>
        <result property="dataBit" column="data_bit"/>
        <result property="isAlarm" column="is_alarm"/>
        <result property="pointType" column="point_type"/>
        <result property="calcExpression" column="calc_expression"/>
        <result property="collectEnabled" column="collect_enabled"/>
        <result property="collectSource" column="collect_source"/>
        <result property="modbusRegisterType" column="modbus_register_type"/>
        <result property="modbusDataType" column="modbus_data_type"/>
        <result property="modbusReadOrder" column="modbus_read_order"/>
        <result property="modbusGroup" column="modbus_group"/>
        <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="selectEmsPointConfigVo">
        select id, point_id, site_id, device_category, device_id, point_name, data_key, point_desc, register_address,
               data_unit, data_a, data_k, data_b, data_bit, is_alarm, point_type, calc_expression,
               collect_enabled, collect_source, modbus_register_type, modbus_data_type, modbus_read_order, modbus_group,
               create_by, create_time, update_by, update_time, remark
        from ems_point_config
    </sql>

    <select id="selectEmsPointConfigById" parameterType="Long" resultMap="EmsPointConfigResult">
        <include refid="selectEmsPointConfigVo"/>
        where id = #{id}
    </select>

    <select id="selectEmsPointConfigList" parameterType="EmsPointConfig" resultMap="EmsPointConfigResult">
        <include refid="selectEmsPointConfigVo"/>
        <where>
            <if test="siteId != null and siteId != ''">and site_id = #{siteId}</if>
            <if test="pointId != null and pointId != ''">and point_id like concat('%', #{pointId}, '%')</if>
            <if test="deviceCategory != null and deviceCategory != ''">and device_category = #{deviceCategory}</if>
            <if test="deviceId != null and deviceId != ''">and device_id = #{deviceId}</if>
            <if test="dataKey != null and dataKey != ''">and data_key like concat('%', #{dataKey}, '%')</if>
            <if test="pointDesc != null and pointDesc != ''">and point_desc like concat('%', #{pointDesc}, '%')</if>
            <if test="pointType != null and pointType != ''">and point_type = #{pointType}</if>
            <if test="collectEnabled != null">and collect_enabled = #{collectEnabled}</if>
            <if test="collectSource != null and collectSource != ''">and collect_source = #{collectSource}</if>
            <if test="modbusRegisterType != null and modbusRegisterType != ''">and modbus_register_type = #{modbusRegisterType}</if>
            <if test="modbusDataType != null and modbusDataType != ''">and modbus_data_type = #{modbusDataType}</if>
            <if test="registerAddress != null and registerAddress != ''">and register_address = #{registerAddress}</if>
        </where>
        order by update_time desc, id desc
    </select>

    <insert id="insertEmsPointConfig" parameterType="EmsPointConfig" useGeneratedKeys="true" keyProperty="id">
        insert into ems_point_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pointId != null">point_id,</if>
            <if test="siteId != null">site_id,</if>
            <if test="deviceCategory != null">device_category,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="pointName != null">point_name,</if>
            <if test="dataKey != null">data_key,</if>
            <if test="pointDesc != null">point_desc,</if>
            <if test="registerAddress != null">register_address,</if>
            <if test="dataUnit != null">data_unit,</if>
            <if test="dataA != null">data_a,</if>
            <if test="dataK != null">data_k,</if>
            <if test="dataB != null">data_b,</if>
            <if test="dataBit != null">data_bit,</if>
            <if test="isAlarm != null">is_alarm,</if>
            <if test="pointType != null">point_type,</if>
            <if test="calcExpression != null">calc_expression,</if>
            <if test="collectEnabled != null">collect_enabled,</if>
            <if test="collectSource != null">collect_source,</if>
            <if test="modbusRegisterType != null">modbus_register_type,</if>
            <if test="modbusDataType != null">modbus_data_type,</if>
            <if test="modbusReadOrder != null">modbus_read_order,</if>
            <if test="modbusGroup != null">modbus_group,</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="pointId != null">#{pointId},</if>
            <if test="siteId != null">#{siteId},</if>
            <if test="deviceCategory != null">#{deviceCategory},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="pointName != null">#{pointName},</if>
            <if test="dataKey != null">#{dataKey},</if>
            <if test="pointDesc != null">#{pointDesc},</if>
            <if test="registerAddress != null">#{registerAddress},</if>
            <if test="dataUnit != null">#{dataUnit},</if>
            <if test="dataA != null">#{dataA},</if>
            <if test="dataK != null">#{dataK},</if>
            <if test="dataB != null">#{dataB},</if>
            <if test="dataBit != null">#{dataBit},</if>
            <if test="isAlarm != null">#{isAlarm},</if>
            <if test="pointType != null">#{pointType},</if>
            <if test="calcExpression != null">#{calcExpression},</if>
            <if test="collectEnabled != null">#{collectEnabled},</if>
            <if test="collectSource != null">#{collectSource},</if>
            <if test="modbusRegisterType != null">#{modbusRegisterType},</if>
            <if test="modbusDataType != null">#{modbusDataType},</if>
            <if test="modbusReadOrder != null">#{modbusReadOrder},</if>
            <if test="modbusGroup != null">#{modbusGroup},</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>

    <insert id="insertBatchEmsPointConfig">
        insert into ems_point_config (
            point_id, site_id, device_category, device_id, point_name, data_key, point_desc, register_address,
            data_unit, data_a, data_k, data_b, data_bit, is_alarm, point_type, calc_expression,
            collect_enabled, collect_source, modbus_register_type, modbus_data_type, modbus_read_order, modbus_group,
            create_by, create_time, update_by, update_time, remark
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.pointId},
                #{item.siteId},
                #{item.deviceCategory},
                #{item.deviceId},
                #{item.pointName},
                #{item.dataKey},
                #{item.pointDesc},
                #{item.registerAddress},
                #{item.dataUnit},
                #{item.dataA},
                #{item.dataK},
                #{item.dataB},
                #{item.dataBit},
                #{item.isAlarm},
                #{item.pointType},
                #{item.calcExpression},
                #{item.collectEnabled},
                #{item.collectSource},
                #{item.modbusRegisterType},
                #{item.modbusDataType},
                #{item.modbusReadOrder},
                #{item.modbusGroup},
                #{item.createBy},
                now(),
                #{item.updateBy},
                now(),
                #{item.remark}
            )
        </foreach>
    </insert>

    <update id="updateEmsPointConfig" parameterType="EmsPointConfig">
        update ems_point_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="pointId != null">point_id = #{pointId},</if>
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="deviceCategory != null">device_category = #{deviceCategory},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="pointName != null">point_name = #{pointName},</if>
            <if test="dataKey != null">data_key = #{dataKey},</if>
            <if test="pointDesc != null">point_desc = #{pointDesc},</if>
            <if test="registerAddress != null">register_address = #{registerAddress},</if>
            <if test="dataUnit != null">data_unit = #{dataUnit},</if>
            <if test="dataA != null">data_a = #{dataA},</if>
            <if test="dataK != null">data_k = #{dataK},</if>
            <if test="dataB != null">data_b = #{dataB},</if>
            <if test="dataBit != null">data_bit = #{dataBit},</if>
            <if test="isAlarm != null">is_alarm = #{isAlarm},</if>
            <if test="pointType != null">point_type = #{pointType},</if>
            <if test="calcExpression != null">calc_expression = #{calcExpression},</if>
            <if test="collectEnabled != null">collect_enabled = #{collectEnabled},</if>
            <if test="collectSource != null">collect_source = #{collectSource},</if>
            <if test="modbusRegisterType != null">modbus_register_type = #{modbusRegisterType},</if>
            <if test="modbusDataType != null">modbus_data_type = #{modbusDataType},</if>
            <if test="modbusReadOrder != null">modbus_read_order = #{modbusReadOrder},</if>
            <if test="modbusGroup != null">modbus_group = #{modbusGroup},</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="updateEmsPointConfigForImport" parameterType="EmsPointConfig">
        update ems_point_config
        set point_id = #{pointId},
            site_id = #{siteId},
            device_category = #{deviceCategory},
            device_id = #{deviceId},
            point_name = #{pointName},
            data_key = #{dataKey},
            point_desc = #{pointDesc},
            register_address = #{registerAddress},
            data_unit = #{dataUnit},
            data_a = #{dataA},
            data_k = #{dataK},
            data_b = #{dataB},
            data_bit = #{dataBit},
            is_alarm = #{isAlarm},
            point_type = #{pointType},
            calc_expression = #{calcExpression},
            collect_enabled = #{collectEnabled},
            collect_source = #{collectSource},
            modbus_register_type = #{modbusRegisterType},
            modbus_data_type = #{modbusDataType},
            modbus_read_order = #{modbusReadOrder},
            modbus_group = #{modbusGroup},
            update_by = #{updateBy},
            update_time = now(),
            remark = #{remark}
        where id = #{id}
    </update>

    <delete id="deleteEmsPointConfigById" parameterType="Long">
        delete from ems_point_config where id = #{id}
    </delete>

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

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

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

    <insert id="copyTemplateToSite">
        insert into ems_point_config (
            point_id, site_id, device_category, device_id, point_name, data_key, point_desc, register_address,
            data_unit, data_a, data_k, data_b, data_bit, is_alarm, point_type, calc_expression,
            collect_enabled, collect_source, modbus_register_type, modbus_data_type, modbus_read_order, modbus_group,
            create_by, create_time, update_by, update_time, remark
        )
        select
            concat('PT_', replace(uuid(), '-', '')), #{targetSiteId}, device_category, device_id, point_name, data_key, point_desc, register_address,
            data_unit, data_a, data_k, data_b, data_bit, is_alarm, point_type, calc_expression,
            collect_enabled, collect_source, modbus_register_type, modbus_data_type, modbus_read_order, modbus_group,
            #{operName}, now(), #{operName}, now(), remark
        from ems_point_config
        where site_id = #{templateSiteId}
    </insert>

    <select id="getRegisterAddress" resultType="java.lang.String">
        select register_address
        from ems_point_config
        where site_id = #{siteId}
          and device_category = #{deviceCategory}
          and device_id = #{deviceId}
          and data_key = #{dataKey}
        limit 1
    </select>

    <select id="getPointNameList" resultType="com.xzzn.ems.domain.vo.GeneralQueryPointOptionVo">
        select point_id as pointId,
               point_name as pointName,
               data_key as dataKey,
               point_desc as pointDesc
        from ems_point_config
        where 1 = 1
        <if test="siteIds != null and siteIds.size() > 0">
            and site_id in
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
        <if test="deviceCategory != null and deviceCategory != ''">
            and device_category = #{deviceCategory}
        </if>
        <if test="deviceId != null and deviceId != ''">
            and device_id = #{deviceId}
        </if>
        <if test="pointName != null and pointName != ''">
            and (
                point_name like concat('%', #{pointName}, '%')
                or data_key like concat('%', #{pointName}, '%')
                or point_desc like concat('%', #{pointName}, '%')
            )
        </if>
        group by point_id, point_name, data_key, point_desc
        order by point_id asc, point_name asc, data_key asc
    </select>

    <select id="getConfigListForGeneralQuery" resultMap="EmsPointConfigResult">
        <include refid="selectEmsPointConfigVo"/>
        where 1 = 1
        <if test="siteIds != null and siteIds.size() > 0">
            and site_id in
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
        <choose>
            <when test="pointIds != null and pointIds.size() > 0">
                and point_id in
                <foreach collection="pointIds" item="pointId" open="(" separator="," close=")">
                    #{pointId}
                </foreach>
            </when>
            <otherwise>
                <if test="deviceCategory != null and deviceCategory != ''">
                    and device_category = #{deviceCategory}
                </if>
                <if test="pointNames != null and pointNames.size() > 0">
                    and point_name in
                    <foreach collection="pointNames" item="pointName" open="(" separator="," close=")">
                        #{pointName}
                    </foreach>
                </if>
                <if test="deviceIds != null and deviceIds.size() > 0">
                    and device_id in
                    <foreach collection="deviceIds" item="deviceId" open="(" separator="," close=")">
                        #{deviceId}
                    </foreach>
                </if>
            </otherwise>
        </choose>
    </select>

    <select id="selectBySiteIdAndPointIds" resultMap="EmsPointConfigResult">
        <include refid="selectEmsPointConfigVo"/>
        where site_id = #{siteId}
          and point_id in
        <foreach collection="pointIds" item="pointId" open="(" separator="," close=")">
            #{pointId}
        </foreach>
    </select>

    <select id="selectModbusCollectPointConfigs" resultMap="EmsPointConfigResult">
        <include refid="selectEmsPointConfigVo"/>
        where collect_enabled = 1
          and collect_source = 'MODBUS'
          and (point_type is null or point_type &lt;&gt; 'calc')
          and (is_alarm is null or is_alarm = 0)
          and register_address is not null and register_address &lt;&gt; ''
          and modbus_register_type is not null and modbus_register_type &lt;&gt; ''
          and modbus_data_type is not null and modbus_data_type &lt;&gt; ''
        <if test="siteId != null and siteId != ''">
            and site_id = #{siteId}
        </if>
        order by site_id asc, device_id asc, modbus_read_order asc, id asc
    </select>

</mapper>
