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

    <resultMap type="EmsPointMatch" id="EmsPointMatchResult">
        <result property="id"    column="id"    />
        <result property="pointName"    column="point_name"    />
        <result property="matchTable"    column="match_table"    />
        <result property="matchField"    column="match_field"    />
        <result property="siteId"    column="site_id"    />
        <result property="deviceCategory"    column="device_category"    />
        <result property="dataPoint"    column="data_point"    />
        <result property="dataPointName"    column="data_point_name"    />
        <result property="dataDevice"    column="data_device"    />
        <result property="dataUnit"    column="data_unit"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="ipPort"    column="ip_port"    />
        <result property="dataType"    column="data_type"    />
        <result property="needDiffDeviceId"    column="need_diff_device_id"    />
        <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="isAlarm"    column="is_alarm"    />
        <result property="deviceId"    column="device_id"    />
        <result property="a"    column="a"    />
        <result property="k"    column="k"    />
        <result property="b"    column="b"    />
    </resultMap>

    <sql id="selectEmsPointMatchVo">
        select id, point_name, match_table, match_field, site_id, device_category, data_point, data_point_name, data_device, data_unit, ip_address, ip_port, data_type, need_diff_device_id, create_by, create_time, update_by, update_time, remark, is_alarm, device_id, a, k, b from ems_point_match
    </sql>

    <select id="selectEmsPointMatchList" parameterType="EmsPointMatch" resultMap="EmsPointMatchResult">
        <include refid="selectEmsPointMatchVo"/>
        <where>
            <if test="pointName != null  and pointName != ''"> and point_name like concat('%', #{pointName}, '%')</if>
            <if test="matchTable != null  and matchTable != ''"> and match_table = #{matchTable}</if>
            <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="dataPoint != null  and dataPoint != ''"> and data_point = #{dataPoint}</if>
            <if test="dataPointName != null  and dataPointName != ''"> and data_point_name like concat('%', #{dataPointName}, '%')</if>
            <if test="dataDevice != null  and dataDevice != ''"> and data_device = #{dataDevice}</if>
            <if test="dataUnit != null  and dataUnit != ''"> and data_unit = #{dataUnit}</if>
            <if test="ipAddress != null  and ipAddress != ''"> and ip_address = #{ipAddress}</if>
            <if test="ipPort != null "> and ip_port = #{ipPort}</if>
            <if test="dataType != null "> and data_type = #{dataType}</if>
            <if test="needDiffDeviceId != null "> and need_diff_device_id = #{needDiffDeviceId}</if>
        </where>
    </select>

    <select id="selectEmsPointMatchById" parameterType="Long" resultMap="EmsPointMatchResult">
        <include refid="selectEmsPointMatchVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsPointMatch" parameterType="EmsPointMatch" useGeneratedKeys="true" keyProperty="id">
        insert into ems_point_match
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pointName != null">point_name,</if>
            <if test="matchTable != null">match_table,</if>
            <if test="matchField != null">match_field,</if>
            <if test="siteId != null">site_id,</if>
            <if test="deviceCategory != null">device_category,</if>
            <if test="dataPoint != null">data_point,</if>
            <if test="dataPointName != null">data_point_name,</if>
            <if test="dataDevice != null">data_device,</if>
            <if test="dataUnit != null">data_unit,</if>
            <if test="ipAddress != null">ip_address,</if>
            <if test="ipPort != null">ip_port,</if>
            <if test="dataType != null">data_type,</if>
            <if test="needDiffDeviceId != null">need_diff_device_id,</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="isAlarm != null">is_alarm,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="a != null">a,</if>
            <if test="k != null">k,</if>
            <if test="b != null">b,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pointName != null">#{pointName},</if>
            <if test="matchTable != null">#{matchTable},</if>
            <if test="matchField != null">#{matchField},</if>
            <if test="siteId != null">#{siteId},</if>
            <if test="deviceCategory != null">#{deviceCategory},</if>
            <if test="dataPoint != null">#{dataPoint},</if>
            <if test="dataPointName != null">#{dataPointName},</if>
            <if test="dataDevice != null">#{dataDevice},</if>
            <if test="dataUnit != null">#{dataUnit},</if>
            <if test="ipAddress != null">#{ipAddress},</if>
            <if test="ipPort != null">#{ipPort},</if>
            <if test="dataType != null">#{dataType},</if>
            <if test="needDiffDeviceId != null">#{needDiffDeviceId},</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="isAlarm != null">#{isAlarm},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="a != null">#{a},</if>
            <if test="k != null">#{k},</if>
            <if test="b != null">#{b},</if>
        </trim>
    </insert>

    <update id="updateEmsPointMatch" parameterType="EmsPointMatch">
        update ems_point_match
        <trim prefix="SET" suffixOverrides=",">
            <if test="pointName != null">point_name = #{pointName},</if>
            <if test="matchTable != null">match_table = #{matchTable},</if>
            <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="dataPoint != null">data_point = #{dataPoint},</if>
            <if test="dataPointName != null">data_point_name = #{dataPointName},</if>
            <if test="dataDevice != null">data_device = #{dataDevice},</if>
            <if test="dataUnit != null">data_unit = #{dataUnit},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="ipPort != null">ip_port = #{ipPort},</if>
            <if test="dataType != null">data_type = #{dataType},</if>
            <if test="needDiffDeviceId != null">need_diff_device_id = #{needDiffDeviceId},</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="isAlarm != null">is_alarm = #{isAlarm},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="a != null">a = #{a},</if>
            <if test="k != null">k = #{k},</if>
            <if test="b != null">b = #{b},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsPointMatchById" parameterType="Long">
        delete from ems_point_match where id = #{id}
    </delete>

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

    <select id="getPointNameList" resultType="String">
        select distinct t.point_name
        from ems_point_match t
        where 1=1
        <if test="siteIds != null and siteIds.size() > 0">
             AND t.site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
        <if test="deviceCategory != null and deviceCategory != ''">
            and t.device_category = #{deviceCategory}
        </if>
        <if test="pointName != null and pointName != ''">
            and t.point_name like CONCAT('%', #{pointName}, '%')
        </if>
    </select>

    <select id="getMatchInfo" resultType="com.xzzn.ems.domain.EmsPointMatch">
        select distinct t.site_id as siteId,
            t.match_table as matchTable,
            t.match_field as matchField,
            t.device_category as deviceCategory,
            t.point_name as pointName,
            t.data_point_name as dataPointName,
            t.data_type as dataType
        from ems_point_match t
        where 1=1
        <if test="siteIds != null and siteIds.size() > 0">
            AND t.site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
        <if test="deviceCategory != null and deviceCategory != ''">
            and t.device_category = #{deviceCategory}
        </if>
        <if test="deviceId != null and deviceId != ''">
            and t.device_id = #{deviceId}
        </if>
        <if test="pointName != null and pointName != ''">
            and (t.point_name like concat('%', #{pointName}, '%') or t.data_point_name like concat('%', #{pointName}, '%'))
        </if>
    </select>

    <sql id="commonFilter">
        <if test="clusterDeviceId != null and clusterDeviceId != ''">
            and cluster_device_id = #{clusterDeviceId}
        </if>
        <choose>
            <when test="params != null and !params.isEmpty()">
                AND ( <trim prefixOverrides="OR">
                <foreach collection="params.entrySet()" item="devices" index="siteId" separator="OR">
                    ( site_id = #{siteId}
                    AND device_id IN
                    <foreach collection="devices" item="deviceId" open="(" close=")" separator=",">
                        #{deviceId}
                    </foreach>
                    )
                </foreach>
            </trim>
                )
            </when>
            <otherwise>
                AND site_id IN
                <foreach collection="siteIds" item="siteId" open="(" close=")" separator=",">
                    #{siteId}
                </foreach>
            </otherwise>
        </choose>
    </sql>

    <select id="getBatteryPointDataByMinutes" resultType="com.xzzn.ems.domain.vo.GeneralQueryDataVo">
        SELECT DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:00') AS valueDate,
                site_id as siteId,
                device_id as deviceId,
                cluster_device_id as parentDeviceId,
                ${tableField} as pointValue
        FROM  ${tableName}
        WHERE  create_time &gt;= #{startDate}
                AND create_time &lt;=  #{endDate}
                AND ${tableField} is not null
                <include refid="commonFilter"/>
        GROUP BY  valueDate, site_id, device_id,cluster_device_id,pointValue
        ORDER BY site_id,device_id,cluster_device_id, valueDate ASC
    </select>

    <select id="getBatteryPointDataByHours" resultType="com.xzzn.ems.domain.vo.GeneralQueryDataVo">
        SELECT  DATE_FORMAT(t.create_time, '%Y-%m-%d %H:00') AS valueDate,
                t.site_id as siteId,
                t.device_id as deviceId,
                t.${tableField} as pointValue,
                t.cluster_device_id as parentDeviceId,
                t.create_time AS last_update_time
        FROM  ${tableName} t
        INNER JOIN ( SELECT  site_id, device_id, cluster_device_id,
                             DATE_FORMAT(create_time, '%Y-%m-%d %H:00') AS hour_group,
                            MAX(create_time) AS max_time
                        FROM  ${tableName}
                        WHERE  create_time &gt;= #{startDate}
                            AND create_time &lt;= #{endDate}
                            AND ${tableField} is not null
                        <include refid="commonFilter"/>
                    GROUP BY  site_id,  device_id,cluster_device_id,  hour_group
        ) tmp ON   t.site_id = tmp.site_id
        AND t.device_id = tmp.device_id
        AND t.cluster_device_id = tmp.cluster_device_id
        AND DATE_FORMAT(t.create_time, '%Y-%m-%d %H:00') = tmp.hour_group
        AND t.create_time = tmp.max_time
        where  ${tableField} is not null
        ORDER BY  t.site_id, t.device_id, t.cluster_device_id,  valueDate ASC
    </select>

    <select id="getBatteryPointDataByDays" resultType="com.xzzn.ems.domain.vo.GeneralQueryDataVo">
        SELECT   DATE_FORMAT(t.create_time, '%Y-%m-%d') AS valueDate,
                t.site_id as siteId,
                t.device_id as deviceId,
                t.cluster_device_id as parentDeviceId,
                t.${tableField} as pointValue,
                t.create_time AS last_update_time
        FROM   ${tableName} t
        INNER JOIN (   SELECT   site_id,  device_id,  cluster_device_id,
                                DATE_FORMAT(create_time, '%Y-%m-%d') AS day_group,
                                MAX(create_time) AS max_time
                        FROM    ${tableName}
                        WHERE  create_time &gt;= #{startDate}
                            AND create_time &lt;= #{endDate}
                            AND ${tableField} is not null
                            <include refid="commonFilter"/>
                        GROUP BY   site_id,  device_id, cluster_device_id,  day_group
        ) tmp ON     t.site_id = tmp.site_id
        AND t.device_id = tmp.device_id
        AND t.cluster_device_id = tmp.cluster_device_id
        AND DATE_FORMAT(t.create_time, '%Y-%m-%d') = tmp.day_group
        AND t.create_time = tmp.max_time
        WHERE t.${tableField} is not null
        ORDER BY   t.site_id, t.device_id, t.cluster_device_id, valueDate ASC
    </select>

    <select id="getBatteryPointDataForBoxPlot" resultType="com.xzzn.ems.domain.vo.GeneralQueryDataVo">
        SELECT DATE_FORMAT(t.create_time, '%Y-%m-%d') AS valueDate,
        t.site_id as siteId,
        t.device_id as deviceId,
        t.cluster_device_id as parentDeviceId,
        t.${tableField} as pointValue
        FROM ${tableName} t
        INNER JOIN ( SELECT site_id, device_id, cluster_device_id,
        DATE_FORMAT(create_time, '%Y-%m-%d') AS day_group
        FROM ${tableName}
        WHERE create_time &gt;= #{startDate}
        AND create_time &lt;= #{endDate}
        AND ${tableField} is not null
        <include refid="commonFilter"/>
        GROUP BY site_id, device_id, cluster_device_id, day_group
        ) tmp ON t.site_id = tmp.site_id
        AND t.device_id = tmp.device_id
        AND t.cluster_device_id = tmp.cluster_device_id
        AND DATE_FORMAT(t.create_time, '%Y-%m-%d') = tmp.day_group
        WHERE t.${tableField} is not null
        GROUP BY t.site_id, t.device_id, t.cluster_device_id, pointValue, valueDate
        ORDER BY t.site_id, t.device_id, t.cluster_device_id, valueDate ASC
    </select>

    <select id="getCommonPointDataByMinutes" resultType="com.xzzn.ems.domain.vo.GeneralQueryDataVo">
        SELECT DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:00') AS valueDate,
                site_id as siteId,
                device_id as deviceId,
                ${tableField} as pointValue
        FROM  ${tableName}
        WHERE  create_time &gt;= #{startDate}
                AND create_time &lt;=  #{endDate}
                AND ${tableField} is not null
                AND site_id IN
                <foreach collection="siteIds" item="siteId" open="(" close=")" separator=",">
                    #{siteId}
                </foreach>
                <if test="deviceId != null and deviceId != ''">
                    and device_id = #{deviceId}
                </if>
        GROUP BY  valueDate, site_id, device_id,pointValue
        ORDER BY site_id,device_id, valueDate ASC
    </select>

    <select id="getCommonPointDataByHours" resultType="com.xzzn.ems.domain.vo.GeneralQueryDataVo">
        SELECT  DATE_FORMAT(t.create_time, '%Y-%m-%d %H:00') AS valueDate,
                t.site_id as siteId,
                t.device_id as deviceId,
                t.${tableField} as pointValue,
                t.create_time AS last_update_time
        FROM  ${tableName} t
        INNER JOIN ( SELECT  site_id, device_id, DATE_FORMAT(create_time, '%Y-%m-%d %H:00') AS hour_group,
                            MAX(create_time) AS max_time
                    FROM  ${tableName}
                    WHERE  create_time &gt;= #{startDate}
                        AND create_time &lt;= #{endDate}
                        AND ${tableField} is not null
                        AND site_id IN
                        <foreach collection="siteIds" item="siteId" open="(" close=")" separator=",">
                            #{siteId}
                        </foreach>
                        <if test="deviceId != null and deviceId != ''">
                            and device_id = #{deviceId}
                        </if>
                    GROUP BY  site_id,  device_id,  hour_group
        ) tmp ON   t.site_id = tmp.site_id
        AND t.device_id = tmp.device_id
        AND DATE_FORMAT(t.create_time, '%Y-%m-%d %H:00') = tmp.hour_group
        AND t.create_time = tmp.max_time
        where  ${tableField} is not null
        ORDER BY  t.site_id,  t.device_id,  valueDate ASC
    </select>

    <select id="getCommonPointDataByDays" resultType="com.xzzn.ems.domain.vo.GeneralQueryDataVo">
        SELECT   DATE_FORMAT(t.create_time, '%Y-%m-%d') AS valueDate,
                t.site_id as siteId,
                t.device_id as deviceId,
                t.${tableField} as pointValue,
                t.create_time AS last_update_time
        FROM   ${tableName} t
        INNER JOIN (   SELECT   site_id,  device_id,  DATE_FORMAT(create_time, '%Y-%m-%d') AS day_group,
                                MAX(create_time) AS max_time
                        FROM    ${tableName}
                        WHERE  create_time &gt;= #{startDate}
                            AND create_time &lt;= #{endDate}
                            AND ${tableField} is not null
                            AND site_id IN
                                <foreach collection="siteIds" item="siteId" open="(" close=")" separator=",">
                                    #{siteId}
                                </foreach>
                            <if test="deviceId != null and deviceId != ''">
                                and device_id = #{deviceId}
                            </if>
                        GROUP BY   site_id,  device_id,   day_group
        ) tmp ON     t.site_id = tmp.site_id
        AND t.device_id = tmp.device_id
        AND DATE_FORMAT(t.create_time, '%Y-%m-%d') = tmp.day_group
        AND t.create_time = tmp.max_time
        WHERE t.${tableField} is not null
        ORDER BY   t.site_id, t.device_id,  valueDate ASC
    </select>

    <select id="getCommonPointDataForBoxPlot" resultType="com.xzzn.ems.domain.vo.GeneralQueryDataVo">
        SELECT DATE_FORMAT(t.create_time, '%Y-%m-%d') AS valueDate,
        t.site_id as siteId,
        t.device_id as deviceId,
        t.${tableField} as pointValue
        FROM ${tableName} t
        INNER JOIN ( SELECT site_id, device_id, DATE_FORMAT(create_time, '%Y-%m-%d') AS day_group
        FROM ${tableName}
        WHERE create_time &gt;= #{startDate}
        AND create_time &lt;= #{endDate}
        AND ${tableField} is not null
        AND site_id IN
        <foreach collection="siteIds" item="siteId" open="(" close=")" separator=",">
            #{siteId}
        </foreach>
        <if test="deviceId != null and deviceId != ''">
            and device_id = #{deviceId}
        </if>
        GROUP BY site_id, device_id, day_group
        ) tmp ON t.site_id = tmp.site_id
        AND t.device_id = tmp.device_id
        AND DATE_FORMAT(t.create_time, '%Y-%m-%d') = tmp.day_group
        WHERE t.${tableField} is not null
        GROUP BY t.site_id, t.device_id, pointValue, valueDate
        ORDER BY t.site_id, t.device_id, valueDate ASC
    </select>

    <select id="getSingleSiteDevicePoints" resultType="com.xzzn.ems.domain.vo.PointQueryResponse">
        select t.point_name as pointName,
                t.data_point as dataPoint,
                t.data_point_name as dataPointName,
                t.data_device as dataDevice,
                t.match_field as matchField,
                t.need_diff_device_id as isNeedDeviceId,
                t.data_unit as dataUnit,
                t.ip_address as ipAddress,
                t.ip_port as ipPort
        from ems_point_match t
        where 1=1
        <if test="siteId != null and siteId != ''">
            and t.site_id = #{siteId}
        </if>
        <if test="deviceCategory != null and deviceCategory != ''">
            and t.device_category = #{deviceCategory}
        </if>
        <if test="pointName != null and pointName != ''">
            and t.point_name like CONCAT('%', #{pointName}, '%')
        </if>
        <if test="dataPoint != null and dataPoint != ''">
            and t.data_point like CONCAT('%', #{dataPoint}, '%')
        </if>
        <if test="ipAddress != null and ipAddress != ''">
            and t.ip_address = #{ipAddress}
        </if>
        <if test="ipPort != null">
            and t.ip_port = #{ipPort}
        </if>
        <if test="isAlarm != null">
            and t.is_alarm = #{isAlarm}
        </if>
        <if test="deviceId != null">
            and t.device_id = #{deviceId}
        </if>
    </select>

    <select id="getClusterDevicePoints" resultType="com.xzzn.ems.domain.vo.PointQueryResponse">
        SELECT tmp.pointName,
               tmp.dataPoint,
               tmp.dataDevice,
               tmp.matchField,
               tmp.dataPointName,
               tmp.dataUnit,
               tmp.ipAddress,
               tmp.ipPort
        FROM ( select t.point_name as pointName,
                case
                    when t.need_diff_device_id = 1 and t.data_device = 'PCS' then concat(#{deviceId}, t.data_point)
                    when t.need_diff_device_id = 1 and t.data_device = 'BMSD' then concat(#{parentDeviceId}, t.data_point)
                    else t.data_point end as dataPoint,
                t.data_point_name as dataPointName,
                t.match_field as matchField,
                t.data_device as dataDevice,
                t.data_unit as dataUnit,
                t.ip_address as ipAddress,
                t.ip_port as ipPort
                from ems_point_match t
                where 1=1
                <if test="siteId != null and siteId != ''">
                    and t.site_id = #{siteId}
                </if>
                <if test="deviceCategory != null and deviceCategory != ''">
                    and t.device_category = #{deviceCategory}
                </if>
                <if test="isAlarm != null">
                    and t.is_alarm = #{isAlarm}
                </if>
                <if test="deviceId != null">
                    and t.device_id = #{deviceId}
                </if>
        ) as tmp
        where 1=1
        <if test="pointName != null and pointName != ''">
            and tmp.dataPointName like CONCAT('%', #{pointName}, '%')
        </if>
        <if test="dataPoint != null and dataPoint != ''">
            and tmp.dataPoint like CONCAT('%', #{dataPoint}, '%')
        </if>
        <if test="ipAddress != null and ipAddress != ''">
            and tmp.ipAddress = #{ipAddress}
        </if>
        <if test="ipPort != null">
            and tmp.ipPort = #{ipPort}
        </if>
    </select>

    <select id="getUniquePoint" resultMap="EmsPointMatchResult">
        <include refid="selectEmsPointMatchVo"/>
        where site_id = #{siteId}
        and device_category = #{deviceCategory}
        and data_point = #{dataPoint}
    </select>
    <select id="getOnePointMatch" resultMap="EmsPointMatchResult">
        <include refid="selectEmsPointMatchVo"/>
        where site_id = #{siteId}
        and device_id = #{deviceId}
        and device_category = #{deviceCategory}
        and data_point = #{dataPoint}
        order by update_time desc
        limit 1
    </select>

    <select id="getDevicePointMatchList" resultMap="EmsPointMatchResult">
        <include refid="selectEmsPointMatchVo"/>
        where site_id = #{siteId}
        <if test="deviceId != null">
            and device_id = #{deviceId}
        </if>
        <if test="deviceId == null">
            and device_id is null
        </if>
        and device_category = #{deviceCategory}
    </select>

    <select id="selectEmsPointMatchExportList" parameterType="EmsPointMatch" resultType="com.xzzn.ems.domain.vo.DevicePointMatchExportVo">
        select
            point_name as pointName,
            match_field as matchField,
            data_point as dataPoint,
            data_point_name as dataPointName,
            data_unit as dataUnit,
            is_alarm as isAlarm,
            ip_address as ipAddress,
            a, k, b
        from
            ems_point_match
        <where>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="deviceCategory != null  and deviceCategory != ''"> and device_category = #{deviceCategory}</if>
        </where>
    </select>
    <select id="getDevicePointAlarmNum" resultType="java.lang.Integer">
        select
            count(1) as alarmNum
        from
            ems_point_match
        where is_alarm = 1
          and site_id = #{siteId}
          and device_id = #{deviceId}
          and device_category = #{deviceCategory}
    </select>
    <select id="selectDeviceStatusPoint" parameterType="com.xzzn.ems.domain.vo.DeviceUpdateRequest" resultMap="EmsPointMatchResult" >
        <include refid="selectEmsPointMatchVo"/>
        where site_id = #{request.siteId}
        and device_id = #{request.deviceId}
        and device_category = #{request.deviceCategory}
        and match_field in
        <foreach collection="list" item="matchField" open="(" separator="," close=")">
            #{matchField}
        </foreach>
    </select>

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

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

    <insert id="copyTemplateToSite">
        insert into ems_point_match (
            point_name,
            match_table,
            match_field,
            site_id,
            device_category,
            data_point,
            data_point_name,
            data_device,
            data_unit,
            ip_address,
            ip_port,
            data_type,
            need_diff_device_id,
            create_by,
            create_time,
            update_by,
            update_time,
            remark,
            is_alarm,
            device_id,
            a,
            k,
            b
        )
        select
            point_name,
            match_table,
            match_field,
            #{targetSiteId},
            device_category,
            data_point,
            data_point_name,
            data_device,
            data_unit,
            ip_address,
            ip_port,
            data_type,
            need_diff_device_id,
            #{operName},
            now(),
            #{operName},
            now(),
            remark,
            is_alarm,
            device_id,
            a,
            k,
            b
        from ems_point_match
        where site_id = #{templateSiteId}
    </insert>

    <select id="selectBySiteId" resultMap="EmsPointMatchResult">
        <include refid="selectEmsPointMatchVo"/>
        where site_id = #{siteId}
    </select>

</mapper>
