<?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.ruoyi.dataprocess.mapper.HwElectronicFenceMapper">
    
    <resultMap type="HwElectronicFence" id="HwElectronicFenceResult">
        <result property="electronicFenceId"    column="electronic_fence_id"    />
        <result property="electronicFenceName"    column="electronic_fence_name"    />
        <result property="sceneId"    column="scene_id"    />
        <result property="fenceType"    column="fence_type"    />
        <result property="effectiveTimeFlag"    column="effective_time_flag"    />
        <result property="effectiveTime"    column="effective_time"    />
        <result property="triggerStatus"    column="trigger_status"    />
        <result property="fencePushFlag"    column="fence_push_flag"    />
        <result property="fencePushContent"    column="fence_push_content"    />
        <result property="fenceRecoverContent"    column="fence_recover_content"    />
    </resultMap>

    <sql id="selectElectronicFenceVo">
        select electronic_fence_id, electronic_fence_name, scene_id, fence_type, effective_time_flag, effective_time, trigger_status,
               fence_push_flag, fence_push_content, fence_recover_content
        from hw_electronic_fence hef
    </sql>

    <select id="selectSuitableElectronicFenceList" resultMap="HwElectronicFenceResult">
        <include refid="selectElectronicFenceVo"/>
               where (exists (select 1 from hw_fence_target hft where
                            hft.electronic_fence_id = hef.electronic_fence_id
                            and ((hft.target_id=#{deviceId} and hft.target_type=1) or (hft.target_id=#{monitorUnitId} and hft.target_type=2))))
                 or (fence_type=3 and scene_id = #{sceneId}) and hef.trigger_status!=0
    </select>

    <select id="selectElectronicFencesByDeviceId" resultMap="HwElectronicFenceResult">
        <include refid="selectElectronicFenceVo"/>
        where exists (select 1 from hw_fence_target hft where
            hft.electronic_fence_id = hef.electronic_fence_id
            and hft.target_id=#{deviceId} and hft.target_type=1)
            and hef.fence_type=1 and hef.trigger_status!=0
    </select>

    <select id="selectElectronicFencesByMonitorUnitId" resultMap="HwElectronicFenceResult">
        <include refid="selectElectronicFenceVo"/>
        where exists (select 1 from hw_fence_target hft where
            hft.electronic_fence_id = hef.electronic_fence_id
            and hft.target_id=#{deviceId} and hft.target_type=2)
        and hef.fence_type=2 and hef.trigger_status!=0
    </select>

    <select id="selectElectronicFencesBySceneId" resultMap="HwElectronicFenceResult">
        <include refid="selectElectronicFenceVo"/>
        where scene_id=#{sceneId} and hef.fence_type=3 and hef.trigger_status!=0
    </select>

</mapper>