<?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.inspect.patrol.mapper.PatrolLinkagePosMapper">
    <resultMap type="PatrolLinkagePos" id="PatrolLinkagePosResult">
        <result property="linkagePosId"    column="linkage_pos_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceName"    column="device_name"    />
        <result property="stationNo"    column="station_no"    />
        <result property="monitorIndexNo"    column="monitor_index_no"    />
        <result property="phyAssetId"    column="phy_asset_id"    />
        <result property="linkageSignal"    column="linkage_signal"    />
        <result property="isEnable"    column="is_enable"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>
    <resultMap type="PatrolPoint" id="PatrolPointResult">
        <result property="patrolPointId"    column="patrol_point_id"    />
        <result property="patrolPointName"    column="patrol_point_name"    />
    </resultMap>

    <sql id="selectPatrolLinkagePosVo">
        select linkage_pos_id, device_id,device_name,station_no,monitor_index_no,phy_asset_id,linkage_signal, is_enable, create_time from patrol_linkage_pos
    </sql>

    <select id="selectPatrolLinkagePosList" parameterType="com.inspect.patrol.model.LinkagePosListModel" resultMap="PatrolLinkagePosResult">
        <include refid="selectPatrolLinkagePosVo"/>
        <where>
            <if test="deviceName != null">and  locate( #{deviceName},b.device_name)</if>
        </where>
    </select>
    <select id="selectPatrolLinkagePos" parameterType="com.inspect.patrol.linkage.frame.LinkageSignEntity" resultMap="PatrolLinkagePosResult">
        <include refid="selectPatrolLinkagePosVo"/>
        where monitor_index_no = #{monitorIndexNo}
    </select>
    
    <select id="selectPatrolLinkagePosByLinkagePosId" parameterType="Long" resultMap="PatrolLinkagePosResult">
        <include refid="selectPatrolLinkagePosVo"/>
        where linkage_pos_id = #{linkagePosId}
    </select>
    <select id="selectPatrolPointList" parameterType="String" resultMap="PatrolPointResult">
        select a.patrol_point_id,a.patrol_point_name
        from basedata_patrolpoint a
        left join patrol_preset_pos b on a.patrol_point_id = b.patrol_point_id
        where a.device_id in
        (
            select device_id from basedata_device where parent_id in (select device_id from basedata_device where parent_id = #{deviceId})
            union select device_id from basedata_device where parent_id = #{deviceId}
            union select #{deviceId} from dual
        ) and b.preset_pos_id is not null

    </select>

    <select id="selectPatrolLinkagePosByIds" parameterType="List" resultMap="PatrolLinkagePosResult">
        <include refid="selectPatrolLinkagePosVo"/>
        where linkage_pos_id in
        <foreach item="linkagePosId" collection="list" open="(" separator="," close=")">
            #{linkagePosId}
        </foreach>
    </select>
    <insert id="insertPatrolLinkagePos" parameterType="List">
        insert into patrol_linkage_pos (device_id,station_no,monitor_index_no,device_name,linkage_signal,phy_asset_id,create_time) values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            (select device_id from basedata_device where phy_asset_id = #{item.entityId} limit 1),
            #{item.stationNo},#{item.monitorIndexNo},#{item.deviceName},#{item.linkageSignal},#{item.entityId},
            now()
            )
        </foreach>
    </insert>

    <update id="updatePatrolLinkagePos" parameterType="List">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE patrol_linkage_pos
            SET station_no = #{item.stationNo},
                device_id = (select device_id from basedata_device where phy_asset_id = #{item.entityId} limit 1),
                device_name = #{item.deviceName},
                linkage_signal = #{item.linkageSignal}
            WHERE monitor_index_no = #{item.monitorIndexNo}
        </foreach>
    </update>
    <update id="updateLinkageActionConfig" parameterType="com.inspect.patrol.model.LinkageActionConfig">
        update sys_config set config_value = #{configValue} where config_id=#{configId} and config_key like 'linkage_action%';
    </update>

    <delete id="deletePatrolLinkagePosByLinkagePosId" parameterType="Long">
        delete from patrol_linkage_pos where linkage_pos_id = #{linkagePosId}
    </delete>

    <delete id="deletePatrolLinkagePosByLinkagePosIds" parameterType="String">
        delete from patrol_linkage_pos where linkage_pos_id in 
        <foreach item="linkagePosId" collection="array" open="(" separator="," close=")">
            #{linkagePosId}
        </foreach>
    </delete>
</mapper>