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

    <select id="getVehicleStatisticsPhoto" resultType="com.securitycontrol.entity.screen.vo.AccessMgeVo">
        SELECT tw.warn_content as warnContent, trf.file_name as fileName, trf.file_suffix as fileSuffix , trf.file_id as fileId, trf.file_type as fileType
        FROM  tb_project tp
        LEFT JOIN tb_car tc ON tp.bid_code = tc.bid_code
        LEFT JOIN tb_car_access tca on tc.id = tca.car_id
          LEFT JOIN tb_warn tw ON tw.source_id = tca.id
          LEFT JOIN tb_resource_file trf on trf.source_id = tw.warn_id
        WHERE tp.bid_code = #{bidCode} and trf.del_flag = '0' AND tca.access_type = #{accessType}
        <if test='timeType=="1"'>
            and to_days(tca.accesss_time) = to_days(NOW())
        </if>

        <if test='timeType=="2"'>
            and DATE_FORMAT(tca.accesss_time, '%Y%m') = DATE_FORMAT(NOW(), '%Y%m')
        </if>
    </select>

    <select id="getVehicleStatisticsList" resultType="com.securitycontrol.entity.screen.vo.AccessMgeVo">
        SELECT thdv.source_type AS sourceType,thdv.val AS carNum,tp.pro_name AS proName
        FROM tb_his_device_value thdv
        INNER JOIN tb_device_detail tdd ON tdd.id = thdv.attribute_id AND tdd.del_flag = 0 AND tdd.mode_name = #{modeName}
        INNER JOIN tb_bd_device tbd ON tbd.device_id = tdd.device_id AND tbd.del_flag = 0 AND tbd.devic_type = #{typeCode}
        INNER JOIN tb_project tp ON thdv.bid_code = tp.bid_code AND tp.del_flag = 0
        WHERE thdv.bid_code = #{bidCode}
        <if test="startDate!=null and startDate!='' and endDate!=null and endDate!=''">
            AND thdv.create_time BETWEEN CONCAT(#{startDate},' 00:00:00') AND CONCAT(#{endDate},' 23:59:59')
        </if>
        <if test="carNum!=null and carNum!=''">
            AND INSTR(thdv.val,#{carNum}) > 0
        </if>
        <if test="proName!=null and proName!=''">
            AND INSTR(tp.pro_name,#{proName}) > 0
        </if>
        ORDER BY thdv.create_time DESC
    </select>

    <select id="getPerStatisticsPhoto" resultType="com.securitycontrol.entity.screen.vo.AccessMgeVo">
        SELECT tw.warn_content as warnContent, trf.file_name as fileName, trf.file_suffix as fileSuffix , trf.file_id as fileId, trf.file_type as fileType
        FROM  tb_work_team  twt
                  LEFT JOIN t_team_people ttp on twt.team_id = ttp.team_id
                  LEFT JOIN tb_user_access tua ON ttp.user_id = tua.user_id
                  LEFT JOIN tb_warn tw ON tw.people_id = tua.id
                  LEFT JOIN tb_resource_file trf on trf.source_id = tw.warn_id
        WHERE twt.bid_code = #{bidCode} and trf.del_flag = '0' AND tua.access_type = #{accessType}
        <if test='timeType=="1"'>
            and to_days(tua.access_time) = to_days(NOW())
        </if>

        <if test='timeType=="2"'>
            and DATE_FORMAT(tua.access_time, '%Y%m') = DATE_FORMAT(NOW(), '%Y%m')
        </if>
    </select>

    <select id="getPerStatisticsList" resultType="com.securitycontrol.entity.screen.vo.AccessMgeVo">
        SELECT thdv.source_type AS sourceType,
               thdv.val AS userId,
               tp.pro_name AS proName
        FROM tb_his_device_value thdv
        INNER JOIN tb_device_detail tdd ON tdd.id = thdv.attribute_id AND tdd.del_flag = 0 AND tdd.mode_name = #{modeName}
        INNER JOIN tb_bd_device tbd ON tbd.device_id = tdd.device_id AND tbd.del_flag = 0 AND tbd.devic_type = #{typeCode}
        INNER JOIN tb_project tp ON thdv.bid_code = tp.bid_code AND tp.del_flag = 0
        WHERE thdv.bid_code = #{bidCode}
        <if test="startDate!=null and startDate!='' and endDate!=null and endDate!=''">
            AND thdv.create_time BETWEEN CONCAT(#{startDate},' 00:00:00') AND CONCAT(#{endDate},' 23:59:59')
        </if>
        <if test="userName!=null and userName!=''">
            AND INSTR(thdv.val,#{carNum}) > 0
        </if>
        <if test="proName!=null and proName!=''">
            AND INSTR(tp.pro_name,#{proName}) > 0
        </if>
        ORDER BY thdv.create_time DESC
    </select>
    <!--车辆基本信息-->
    <select id="getCarBasicInfo" resultType="java.lang.String">
        SELECT CONCAT(IFNULL(user_name,'/'),'@',IFNULL(phone,'/')) FROM tb_car WHERE car_num = #{carNum}
    </select>
    <!--获取车辆/人员入场 离场状态-->
    <select id="getAccessLists" resultType="java.lang.String">
        SELECT GROUP_CONCAT(CONCAT(tdd.mode_name,'@',thdv.val)) AS value
        FROM tb_his_device_value thdv
        INNER JOIN tb_device_detail tdd ON thdv.attribute_id = tdd.id AND tdd.del_flag = 0
        WHERE thdv.source_type = #{sourceType}
        GROUP BY thdv.source_type
    </select>
    <!--人员基本信息-->
    <select id="getPeopleBasicInfo" resultType="java.lang.String">
        SELECT CONCAT(IFNULL(user_name,'/'),'@',IFNULL(phone,'/')) FROM t_team_people WHERE user_id = #{userId}
    </select>
</mapper>