<?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.bonus.app.mapper.StayPersonMapper">

    <select id="getStayPersonList" resultType="com.bonus.app.entity.StayPersonEntity" >
        select  tpp.id gtId ,twt.team_id teamId,twt.team_name teamName,blt.sh_code shCode,tpp.lon sgLon,tpp.lat sgLat,blt.face_status faceStatus,
        peopel_type as userType,tddone.data_val as dl,tddtwo.data_val as lon,tddthree.data_val as lat,tddfour.data_val as wd,tddfive.data_val as xl,tddsix.data_val as xy,
                case
                    WHEN  peopel_type=0 then tpe.aqm_code
                    WHEN  peopel_type=1 then null
                ELSE ''
                END AS aqmCode,
                case
                    WHEN  peopel_type=0 then tpe.mj_code
                    WHEN  peopel_type=1 then null
                ELSE ''
                END AS mjCode,
                case
                    WHEN  peopel_type=0 then count(tpc.id)
                    WHEN  peopel_type=1 then 0
                ELSE ''
                END AS certificateNum,
                case
                    WHEN  peopel_type=0 then tpe.id
                    WHEN  peopel_type=1 then tlu.id
                    ELSE ''
                    END AS userId,
                case
                    WHEN  peopel_type=0 then tpe.name
                    WHEN  peopel_type=1 then tlu.name
                    ELSE ''
                    END AS userName,
                case
                    WHEN  peopel_type=0 then tpe.phone
                    WHEN  peopel_type=1 then tlu.phone
                    ELSE ''
                    END AS phone ,
                case
                    WHEN  peopel_type=0 then tpe.id_card
                    WHEN  peopel_type=1 then tlu.id_card
                    ELSE ''
                    END AS idCard
                FROM tb_bracelet blt
                left join tb_sh_box sbx on blt.box_id=sbx.id and sbx.del_flag = 0
                left join t_work_team twt on sbx.team_id = twt.team_id and twt.del_flag= 0
                left join tb_project_power tpp on tpp.id=sbx.gt_id and tpp.del_flag = 0
                left join tb_people tpe on blt.bid_id=tpe.id and peopel_type=0 and tpe.name is not null and tpe.name!=''
                left join tb_ls_user tlu on blt.bid_id=tlu.id and peopel_type=1 and tlu.name is not null and tlu.name!=''
                left join tb_people_certificate tpc on tpe.id = tpc.people_id and tpc.certificate_type = 1
                left join tb_dev_details tddone on blt.id = tddone.dev_id and tddone.dev_type = 0 and tddone.data_code = '电量' and tddone.del_flag = 0
                left join tb_dev_details tddtwo on blt.id = tddtwo.dev_id and tddtwo.dev_type = 0 and tddtwo.data_code = '经度' and tddtwo.del_flag = 0
                left join tb_dev_details tddthree on blt.id = tddthree.dev_id and tddthree.dev_type = 0 and tddthree.data_code = '纬度'  and tddthree.del_flag = 0
                left join tb_dev_details tddfour on blt.id = tddfour.dev_id and tddfour.dev_type = 0 and tddfour.data_code = '温度' and tddfour.del_flag = 0
                left join tb_dev_details tddfive on blt.id = tddfive.dev_id and tddfive.dev_type = 0 and tddfive.data_code = '心率' and tddfive.del_flag = 0
                left join tb_dev_details tddsix on blt.id = tddsix.dev_id and tddsix.dev_type = 0 and tddsix.data_code = '血压' and tddsix.del_flag = 0
        <where>
            blt.del_flag = 0
            <if test="gtId!=null and gtId!=''">
                and  tpp.id=#{gtId}
            </if>
            <if test="userName!=null and userName!=''">
                and  (tpe.name=#{userName} or tlu.name=#{userName})
            </if>
        </where>
        group by twt.team_id,blt.sh_code,sbx.gt_id,blt.peopel_type,tddone.data_val,tddtwo.data_val,tddthree.data_val,tddfour.data_val,tddfive.data_val,tddsix.data_val,
        tpe.aqm_code,tpe.mj_code,tpe.id,tlu.id,blt.face_status
        order by twt.team_id,userId
    </select>

    <!--获取所有人脸验证的人员id和类别-->
    <select id="getStayPersonInfoList" resultType="com.bonus.app.entity.PeopleInfoEntity" >
        select peopel_type as userType,
               case
                   WHEN  peopel_type=0 then tpe.id
                   WHEN  peopel_type=1 then tlu.id
                   ELSE ''
                   END AS userId
        from tb_bracelet blt
                 left join tb_sh_box sbx on blt.box_ix=sbx.id and sbx.del_flag = 0
                 left join tb_people tpe on blt.bid_id=tpe.id and peopel_type=0
                 left join tb_ls_user tlu on blt.bid_id=tlu.id and peopel_type=1
        where sbx.gt_id = #{gtId}
    </select>

    <!--获取施工距离-->
    <select id="getDistance" resultType="java.lang.String">
        SELECT dict_value
        FROM sys_dict_data
        WHERE dict_type = 'sys_sg_distance'
        LIMIT 1
    </select>

<!--    &lt;!&ndash;切换时获取工程列表&ndash;&gt;-->
<!--    <select id="getProjectList" resultType="java.util.Map">-->
<!--        select id,pro_name as name-->
<!--        from tb_project-->
<!--        where id != #{proId} and del_flag = 0-->
<!--    </select>-->

    <!--安全帽编号是否重复-->
    <select id="isExistCodeAqm" resultType="com.bonus.common.entity.bracelet.vo.ProjectVo">
        select id as userId,name as userName
        from tb_people
        where aqm_code = #{aqmCode} and id != #{userId} and del_flag = 0
    </select>

    <!--马甲编号是否重复-->
    <select id="isExistCodeMj" resultType="com.bonus.common.entity.bracelet.vo.ProjectVo">
        select id as userId,name as userName
        from tb_people
        where mj_code = #{mjCode} and id != #{userId} and del_flag = 0
    </select>

    <!--修改安全帽、马甲编号-->
    <update id="editCodeInfoList" >
        update tb_people
        set	aqm_code = #{aqmCode},mj_code = #{mjCode}
        where id = #{userId}
    </update>

    <!--根据人员id获取手环信息-->
    <select id="getShInfoList" resultType="com.bonus.app.entity.ShInfoEntity">
        select blt.id,blt.sh_code shCode,blt.sh_status shStatus,blt.dev_code devCode,blt.peopel_type userType,blt.bid_id userId,blt.face_status faceStatus,
               case
                   WHEN  blt.peopel_type=0 then tpe.name
                   WHEN  blt.peopel_type=1 then tlu.name
                   ELSE ''
               END AS userName
        from tb_bracelet blt
        left join tb_people tpe on blt.bid_id=tpe.id and blt.peopel_type=0 and tpe.name is not null and tpe.name!=''
        left join tb_ls_user tlu on blt.bid_id=tlu.id and blt.peopel_type=1 and tlu.name is not null and tlu.name!=''
        <where>
            blt.del_flag = 0
            <if test="userType!=null">
                and  blt.peopel_type = #{userType}
            </if>
            <if test="userId!=null and userId!=''">
                and  blt.bid_id = #{userId}
            </if>
        </where>
    </select>
</mapper>