<?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.securitycontrol.screen.mapper.PersonnelControlMapper">
    <insert id="addNewFence">
        INSERT INTO tb_pro_fence
        (id, fence_name, fence_type, bid_code, create_time, update_time, alarm_type, alert_ype, color)
        VALUES
        (#{uuId}, #{fencingName}, #{fencingType}, #{bidCode}, NOW(), NOW(), #{alarmType}, #{alertType}, #{color})
    </insert>
    <insert id="insertCoordinates">
        INSERT INTO tb_fence_detail
            (id, fence_id, lat, lon, create_time)
        VALUES
            (UUID(), #{uuId}, #{latitude}, #{longitude}, NOW())
    </insert>
    <update id="subcontractorSwitchListen">
        UPDATE tb_pro_fence
        SET status = #{status}
        WHERE id = #{id}
    </update>
    <delete id="delSubcontractorById">
        DELETE tb_pro_fence, tb_fence_detail
        FROM tb_pro_fence
                 JOIN tb_fence_detail ON tb_pro_fence.id = tb_fence_detail.fence_id
        WHERE tb_pro_fence.id = #{id};
    </delete>

    <!--人员管控-穿戴装备-->
    <select id="getWearEquipmentList" resultType="com.securitycontrol.entity.screen.vo.PersonnelControlVo">
        SELECT td.device_name AS deviceName,
               ttp.user_name AS userName,
               sd.dict_name AS deviceType,
               td.on_line AS status
        FROM tb_user_bind_cap tubc
        LEFT JOIN tb_device td ON tubc.device_id = td.device_id AND td.device_type = #{typeCode}
        LEFT JOIN t_team_people ttp ON tubc.people_id = ttp.user_id AND ttp.del_falge = '0'
        LEFT JOIN sys_dict sd ON td.device_type = sd.dict_code AND sd.del_flag = 0
        INNER JOIN tb_work_team twt ON ttp.team_id = twt.team_id AND twt.bid_code = #{bidCode}
        WHERE tubc.bind_status = '1'
    </select>
    <!--人员管控-人员列表-->
    <select id="getPersonnelList" resultType="com.securitycontrol.entity.screen.vo.PersonnelControlVo">
        SELECT
        twt.team_id AS id,
        twt.bid_code AS parentId,
        twt.team_name AS title
        FROM
        tb_work_team twt
        WHERE
        twt.bid_code = #{bidCode}

        UNION ALL

        SELECT
        ttp.user_id AS id,
        ttp.team_id AS parentId,
        CONCAT(ttp.user_name, ' (', ttp.phone, ')') AS title
        FROM
        t_team_people ttp
        JOIN tb_work_team twt ON ttp.team_id = twt.team_id
        WHERE
        twt.bid_code = #{bidCode}
        <if test="keyWord != null and keyWord != ''">
            AND CONCAT(ttp.user_name, ' (', ttp.phone, ')') LIKE CONCAT('%', #{keyWord}, '%')
        </if>
    </select>
    <!--人员管控-人员运动趋预警-->
    <select id="getPersonnelMovementList" resultType="com.securitycontrol.entity.screen.vo.PersonnelControlVo">
        SELECT tw.warn_time AS warningTime,
            tw.warn_content AS warningContent
        FROM tb_warn tw
        WHERE tw.warn_type = #{typeCode} AND tw.bid_code = #{bidCode}
    </select>
    <select id="getNewFenceList" resultType="com.securitycontrol.entity.screen.vo.PersonnelControlVo">
        SELECT
            tpf.id AS id,
            tpf.fence_name AS fenceName,
            sd.dict_name AS fenceType,
            tpf.create_time AS createTime,
            sd1.dict_name AS alarmType,
            sd2.dict_name AS alertType,
            tpf.color AS color,
            tpf.status AS status
        FROM
            tb_pro_fence tpf
                LEFT JOIN sys_dict sd ON tpf.fence_type = sd.dict_id
                LEFT JOIN sys_dict sd1 ON tpf.alarm_type = sd.dict_id
                LEFT JOIN sys_dict sd2 ON tpf.alert_ype = sd.dict_id
        WHERE
            bid_code = #{bidCode}
        GROUP BY
            tpf.fence_name
    </select>
    <select id="getReplicaFence" resultType="com.securitycontrol.entity.screen.vo.PersonnelControlVo">
        SELECT
            tpf.id AS id,
            tfd.lat AS latitude,
            tfd.lon AS longitude,
            tpf.color AS color
            FROM tb_fence_detail tfd
        LEFT JOIN tb_pro_fence tpf ON tfd.fence_id = tpf.id
        WHERE tpf.bid_code = #{bidCode}
    </select>
    <select id="personnelCoordinatePoint" resultType="com.securitycontrol.entity.screen.vo.PersonnelControlVo">
        SELECT
            GROUP_CONCAT(tdd.mode_name) AS modeName,
            GROUP_CONCAT(tdv.val) AS val,
            ttp.user_name AS userName,
            ttp.phone
         FROM tb_bd_device tbd
         INNER JOIN tb_pro_bd tpb ON tbd.bd_id = tpb.id AND tpb.del_flag = 0 AND tpb.bid_code = #{bidCode}
         INNER JOIN tb_device_detail tdd ON tbd.device_id = tdd.device_id AND tdd.del_flag = 0
         INNER JOIN tb_device_value tdv ON tdd.id = tdv.attribute_id
         LEFT JOIN t_team_people ttp ON ttp.user_id = tdv.val AND ttp.del_falge = '0' AND tdd.mode_name = '绑定人'
         WHERE tbd.devic_type = '1905' AND tbd.del_flag = 0
         GROUP BY tbd.device_id
    </select>

    <select id="personnelHistoricalCoordinates"
            resultType="com.securitycontrol.entity.screen.vo.PersonnelControlVo">
        SELECT
        his.val as val,
        GROUP_CONCAT(his2.val) aslatLon,
        his.source_type as sourceType
        from tb_his_device_value his
        left join  tb_his_device_value his2 on his.source_type=his2.source_type AND  his.id!=his2.id
        left join tb_device_detail tdd ON his.attribute_id = tdd.id
        left join  tb_bd_device tbd ON tbd.device_id = tdd.device_id
        LEFT JOIN tb_pro_bd tpb ON tbd.bd_id = tpb.id
        where his.val=#{bindUser} and tpb.bid_code = #{bidCode}   and tbd.devic_type=1905
        <if test="month != null and month != ''">
            AND DATE_FORMAT(his.create_time,'%Y-%m-%d') = #{month}
        </if>
        GROUP BY his.source_type
    </select>
</mapper>