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

    <resultMap type="PatrolLinkageStrategy" id="PatrolLinkageStrategyResult">
        <result property="linkageStrategyId" column="linkage_strategy_id"/>
        <result property="linkagePosId" column="linkage_pos_id"/>
        <result property="triggerSignalType" column="trigger_signal_type"/>
        <result property="linkageSignalName" column="linkage_signal_name"/>
        <result property="linkageAttribute" column="linkage_attribute"/>
        <result property="linkageValue" column="linkage_value"/>
        <result property="isEnable" column="is_enable"/>
        <result property="createTime" column="create_time"/>
        <result property="deviceId" column="device_id"/>
        <result property="stationNo" column="station_no"/>
        <result property="monitorIndexNo" column="monitor_index_no"/>
        <result property="deviceName" column="device_name"/>
        <result property="linkageSignal" column="linkage_signal"/>
        <result property="phyAssetId" column="phy_asset_id"/>
    </resultMap>

    <sql id="selectPatrolLinkageStrategyVo">
        select a.linkage_strategy_id,
               a.linkage_pos_id,
               a.trigger_signal_type,
               a.linkage_signal_name,
               a.linkage_attribute,
               a.linkage_value,
               a.is_enable,
               a.create_time,
               b.device_id,
               b.station_no,
               b.monitor_index_no,
               b.device_name,
               b.linkage_signal,
               b.phy_asset_id,
               b.linkage_signal device_type
        from patrol_linkage_strategy a
                 left join patrol_linkage_pos b on a.linkage_pos_id = b.linkage_pos_id
    </sql>

    <select id="selectPatrolLinkageStrategyList" parameterType="PatrolLinkageStrategy"
            resultMap="PatrolLinkageStrategyResult">
        <include refid="selectPatrolLinkageStrategyVo"/>
        <where>
            <if test="linkagePosId != null ">and a.linkage_pos_id = #{linkagePosId}</if>
            <if test="triggerSignalType != null  and triggerSignalType != ''">and a.trigger_signal_type =
                #{triggerSignalType}
            </if>
            <if test="linkageSignalName != null  and linkageSignalName != ''">and a.linkage_signal_name like concat('%',
                #{linkageSignalName}, '%')
            </if>
            <if test="linkageAttribute != null  and linkageAttribute != ''">and a.linkage_attribute =
                #{linkageAttribute}
            </if>
            <if test="linkageValue != null  and linkageValue != ''">and a.linkage_value = #{linkageValue}</if>
            <if test="isEnable != null  and isEnable != ''">and a.is_enable = #{isEnable}</if>
            <if test="deviceName != null  and deviceName != ''">and b.device_name like concat('%', #{deviceName},'%')
            </if>
            <if test="monitorIndexNo != null  and monitorIndexNo != ''">and b.monitor_index_no = #{monitorIndexNo}</if>
            <if test="phyAssetId != null  and phyAssetId != ''">and b.phy_asset_id = #{phyAssetId}</if>
            <if test="linkageSignal != null  and linkageSignal != ''">and b.linkage_signal = #{linkageSignal}</if>
        </where>
        order by a.create_time desc
    </select>

    <select id="selectPatrolLinkageStrategyByLinkageStrategyId" parameterType="Long"
            resultMap="PatrolLinkageStrategyResult">
        <include refid="selectPatrolLinkageStrategyVo"/>
        where a.linkage_strategy_id = #{linkageStrategyId}
    </select>

    <insert id="insertPatrolLinkageStrategy" parameterType="PatrolLinkageStrategy" useGeneratedKeys="true"
            keyProperty="linkageStrategyId">
        insert into patrol_linkage_strategy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="linkagePosId != null">linkage_pos_id,</if>
            <if test="triggerSignalType != null">trigger_signal_type,</if>
            <if test="linkageSignalName != null">linkage_signal_name,</if>
            <if test="linkageAttribute != null">linkage_attribute,</if>
            <if test="linkageValue != null">linkage_value,</if>
            <if test="isEnable != null">is_enable,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="linkagePosId != null">#{linkagePosId},</if>
            <if test="triggerSignalType != null">#{triggerSignalType},</if>
            <if test="linkageSignalName != null">#{linkageSignalName},</if>
            <if test="linkageAttribute != null">#{linkageAttribute},</if>
            <if test="linkageValue != null">#{linkageValue},</if>
            <if test="isEnable != null">#{isEnable},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updatePatrolLinkageStrategy" parameterType="PatrolLinkageStrategy">
        update patrol_linkage_strategy
        <trim prefix="SET" suffixOverrides=",">
            <if test="linkagePosId != null">linkage_pos_id = #{linkagePosId},</if>
            <if test="triggerSignalType != null">trigger_signal_type = #{triggerSignalType},</if>
            <if test="linkageSignalName != null">linkage_signal_name = #{linkageSignalName},</if>
            <if test="linkageAttribute != null">linkage_attribute = #{linkageAttribute},</if>
            <if test="linkageValue != null">linkage_value = #{linkageValue},</if>
            <if test="isEnable != null">is_enable = #{isEnable},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where linkage_strategy_id = #{linkageStrategyId}
    </update>

    <delete id="deletePatrolLinkageStrategyByLinkageStrategyId" parameterType="Long">
        delete
        from patrol_linkage_strategy
        where linkage_strategy_id = #{linkageStrategyId}
    </delete>

    <delete id="deletePatrolLinkageStrategyByLinkageStrategyIds" parameterType="String">
        delete from patrol_linkage_strategy where linkage_strategy_id in
        <foreach item="linkageStrategyId" collection="array" open="(" separator="," close=")">
            #{linkageStrategyId}
        </foreach>
    </delete>
</mapper>