<?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.background.mapper.HumanManageMapper">
    <!--新增/修改人员-->
    <insert id="addOrUpdatePersonnel">
        <if test="type == 1">
            INSERT INTO t_team_people
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="userId != null and userId != ''">user_id,</if>
                <if test="teamId != null and teamId != ''">team_id,</if>
                <if test="userName != null and userName != ''">user_name,</if>
                <if test="idNumber != null and idNumber!=''">id_number,</if>
                <if test="phone != null and phone!=''">phone,</if>
                <if test="userType != null and userType!=''">user_type,</if>
                <if test="createTime != null and createTime!=''">cratet_time,</if>
                del_falge,
                <if test="personType != null and personType!=''">person_type,</if>
                <if test="projectDepName != null and projectDepName!=''">project_dep_name,</if>
                <if test="deptName != null and deptName!=''">dept_name,</if>
                <if test="sex != null and sex!=''">sex,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="userId != null and userId != ''">#{userId},</if>
                <if test="teamId != null and teamId != ''">#{teamId},</if>
                <if test="userName != null and userName != ''">#{userName},</if>
                <if test="idNumber != null and idNumber!=''">#{idNumber},</if>
                <if test="phone != null and phone!=''">#{phone},</if>
                <if test="userType != null and userType!=''">#{userType},</if>
                <if test="createTime != null and createTime!=''">#{createTime},</if>
                '0',
                <if test="personType != null and personType!=''">#{personType},</if>
                <if test="projectDepName != null and projectDepName!=''">#{projectDepName},</if>
                <if test="deptName != null and deptName!=''">#{deptName},</if>
                <if test="sex != null and sex!=''">#{sex},</if>
            </trim>
        </if>
        <if test="type == 2">
            UPDATE t_team_people
            <set>
                <if test="teamId != null and teamId != ''">team_id = #{teamId},</if>
                <if test="userName != null and userName != ''">user_name = #{userName},</if>
                <if test="idNumber != null and idNumber != ''">id_number = #{idNumber},</if>
                <if test="phone != null and phone != ''">phone = #{phone},</if>
                <if test="userType != null and userType != ''">user_type = #{userType},</if>
                <if test="status != null and status!=''">#{status},</if>
                <if test="personType != null and personType!=''">person_type = #{personType},</if>
                <if test="projectDepName != null and projectDepName!=''">project_dep_name = #{projectDepName},</if>
                <if test="deptName != null and deptName!=''">dept_name = #{deptName},</if>
                <if test="sex != null and sex!=''">sex = #{sex},</if>
            </set>
            WHERE user_id= #{userId}
        </if>
    </insert>
    <!--保存人员照片资源-->
    <insert id="addFiles">
        INSERT INTO tb_resource_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="resourceId != null and resourceId != ''">id,</if>
            <if test="fileType != null">file_type,</if>
            <if test="fileId != null and fileId!=''">file_id,</if>
            <if test="fileSuffix != null and fileSuffix!=''">file_suffix,</if>
            <if test="fileName != null and fileName!=''">file_name,</if>
            <if test="sourceId != null and sourceId!=''">source_id,</if>
            <if test="sourceType != null and sourceType!=''">source_type,</if>
            del_flag,
            <if test="updateTime != null and updateTime!=''">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="resourceId != null and resourceId != ''">#{resourceId},</if>
            <if test="fileType != null">#{fileType},</if>
            <if test="fileId != null and fileId!=''">#{fileId},</if>
            <if test="fileSuffix != null and fileSuffix!=''">#{fileSuffix},</if>
            <if test="fileName != null and fileName!=''">#{fileName},</if>
            <if test="sourceId != null and sourceId!=''">#{sourceId},</if>
            <if test="sourceType != null and sourceType!=''">#{sourceType},</if>
            0,
            <if test="updateTime != null and updateTime!=''">#{updateTime},</if>
        </trim>
    </insert>
    <insert id="bindDev">
        insert tb_user_bind_cap(id,people_id,device_id,bind_time,bind_status)
            value ( #{id},#{userId},#{devId},now(),1 )
    </insert>
    <!--删除人员照片资源文件-->
    <delete id="delFile">
        DELETE FROM tb_resource_file WHERE file_id = #{fileId}
    </delete>
    <!--删除人员-->
    <delete id="delPersonnelById">
        UPDATE t_team_people SET del_falge = '1' WHERE user_id = #{id}
    </delete>
    <update id="delBind">
            update tb_user_bind_cap set  bind_status=0 where  people_id=#{userId}
    </update>
    <update id="updateDev">
             update tb_user_bind_cap set  device_id=#{devId} where  id=#{id}
    </update>
    <!--获取人员列表-->
    <select id="getHumanLists" resultType="com.securitycontrol.entity.background.vo.HumanManageVo">
        SELECT
        ttp.user_id AS userId,
        ttp.user_name AS userName,
        sd.dict_name AS userType,
        twt.team_leader AS teamLeader,
        twt.team_name AS teamName,
        tp.pro_name AS proName,
        ttp.sex,
        ttp.person_type AS personType,
        ttp.project_dep_name AS projectDepName,
        ttp.dept_name AS deptName
        FROM t_team_people ttp
        LEFT JOIN tb_work_team twt on ttp.team_id = twt.team_id
        LEFT JOIN sys_dict sd ON ttp.user_type = sd.dict_code AND sd.del_flag = 0
        LEFT JOIN tb_project tp ON twt.bid_code = tp.bid_code AND tp.del_flag = 0
        <where>
            <if test="bidCode!=null and bidCode !=''">
                twt.bid_code = #{bidCode}
            </if>
            <if test="userName!=null and userName !=''">
                AND INSTR(ttp.user_name,#{userName}) > 0
            </if>
            AND ttp.del_falge = '0'
        </where>
    </select>
    <!--人员是否存在-->
    <select id="userIsExist" resultType="java.util.Map">
        <if test="userId == null or userId == ''">
            SELECT user_id AS id,id_number AS value FROM t_team_people
        </if>
        <if test="userId != null and userId != ''">
            SELECT user_id AS id,id_number AS value FROM t_team_people WHERE user_id != #{userId}
        </if>
    </select>
    <!--人员详情-->
    <select id="getPersonnelById" resultType="com.securitycontrol.entity.background.vo.HumanManageVo">
        SELECT
            ttp.user_id AS userId,
            ttp.user_name AS userName,
            ttp.user_type AS userType,
            ttp.id_number AS idNumber,
            ttp.phone,
            ttp.team_id AS teamId,
            ttp.sex,
            ttp.person_type AS personType,
            ttp.project_dep_name AS projectDepName,
            ttp.dept_name AS deptName
        FROM t_team_people ttp
        WHERE ttp.user_id = #{id}
    </select>
    <!--获取人员照片-->
    <select id="getFiles" resultType="com.securitycontrol.entity.system.vo.ResourceFileVo">
        SELECT id AS resourceId,
               file_id AS fileId
        FROM tb_resource_file WHERE source_id = #{userId} AND del_flag = 0
    </select>
    <!--获取人员出入记录-->
    <select id="getPersonnelAccessLists" resultType="com.securitycontrol.entity.background.vo.UserAccessVo">
        SELECT thdv.source_type AS value
        FROM tb_bd_device tbd
        INNER JOIN tb_device_detail tdd ON tbd.device_id = tdd.device_id AND tdd.del_flag = 0
        INNER JOIN tb_his_device_value thdv ON tdd.id = thdv.attribute_id AND thdv.val = #{id}
        <if test="startTime!=null and startTime!='' and endTime!=null and endTime!=''">
            AND thdv.create_time BETWEEN CONCAT(#{startTime},' 00:00:00') AND CONCAT(#{endTime},' 23:59:59')
        </if>
        WHERE tbd.devic_type = #{typeCode} AND tdd.mode_name = #{modeName} AND tbd.del_flag = 0
    </select>
    <select id="getBindHis" resultType="java.lang.Integer">
            select count(1)
                from tb_user_bind_cap
                where  people_id=#{userId} and bind_status=1
    </select>

    <select id="getBindDetail" resultType="com.securitycontrol.entity.background.dto.ParamDto">
            select device_id devId,id
                from tb_user_bind_cap
                where  people_id=#{userId} and bind_status=1
    </select>
    <!--人员详情-->
    <select id="getUserDeatail" resultType="com.securitycontrol.entity.background.vo.UserAccessVo">
        SELECT user_name AS userName,
               id_number AS idNumber,
               phone AS userPhone
        FROM t_team_people WHERE user_id = #{id}
    </select>
    <!--获取人员出入场记录-->
    <select id="getUserAccessLists" 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 = #{value}
        GROUP BY thdv.source_type
    </select>
    <!--获取人员入场 离场状态-->
    <select id="getUserAccessStatus" resultType="java.lang.String">
        <if test="type == 1">
            SELECT tdd.device_id
            FROM tb_device_value tdv
            LEFT JOIN tb_device_detail tdd ON tdv.attribute_id = tdd.id AND tdd.del_flag = 0
            WHERE tdv.val = #{userId}
        </if>
        <if test="type == 2">
            SELECT tdv.val
            FROM tb_device_detail tdd
            LEFT JOIN tb_device_value tdv ON tdd.id = tdv.attribute_id
            WHERE tdd.device_id = #{userId} AND tdd.mode_name = #{accessType} AND tdd.del_flag = 0
        </if>
    </select>
    <!--人员绑定安全帽-->
    <select id="getUserBingCup" resultType="java.lang.String">
        SELECT td.device_name AS deviceName
        FROM tb_user_bind_cap tubc
        LEFT JOIN tb_device td ON tubc.device_id = td.device_id AND td.device_type = #{typeCode}
        WHERE tubc.people_id = #{params.userId} AND tubc.bind_status = '1'
    </select>
</mapper>