<?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.bracelet.mapper.QueryConstructionMapper">
	<!--施工记录列表-->
	<select id="selectConstructionList"  resultType="com.bonus.common.entity.bracelet.vo.QueryConstruction">
		select tp.pro_name as proName,tpd.depart_name as departName,twt.team_name as teamName,twt.team_leader as teamLeader,twt.leader_phone  as teamPhone,
		count(DISTINCT tbp.id) as teamNum,count(DISTINCT tlu.id) as tempNum,a.lytime as lyTime,count(DISTINCT tw.id) as warnNum,a.teamid as teamId
		from
		(select tdu.pro_id as proid,tdu.team_id as teamid,tdub.ly_time as lytime
		from tb_dev_use tdu
		left join tb_dev_use_bid tdub on tdub.id = tdu.id and tdub.dev_type = 'shx'
		where tdu.del_flag = 0) a
		left join tb_project tp on tp.id = a.proid and tp.del_flag = 0
		left join t_work_team  twt on twt.team_id = a.teamid and twt.pro_id = tp.id and  twt.del_flag = 0
		left join tb_project_depart tpd on tpd.id = tp.depart_id
		left join tb_people tbp on tbp.team_id = a.teamid  and tbp.del_flag = 0
		left join tb_ls_user tlu on  tlu.team_id = a.teamid
		left join tb_warn tw on tw.team_id = a.teamid and tw.del_flag = 0
		<where>
			<if test="proName != null and proName != ''">
				and INSTR(tp.pro_name,#{proName}) > 0
			</if>
			<if test="departName != null and departName != ''">
				and INSTR(tpd.depart_name,#{departName}) > 0
			</if>
			<if test="teamName != null and teamName != ''">
				and INSTR(twt.team_name,#{teamName}) > 0
			</if>
			<if test="lyTime != null ">
				and date_format(a.lytime,'%y%m%d') = date_format(#{lyTime},'%y%m%d')
			</if>
		</where>
		group by proName,departName,teamName,teamLeader,teamPhone,a.lytime,a.teamid
		order by a.lytime desc
	</select>


	<!--班组人员设备列表-->
	<select id="getTeamLists" resultType="com.bonus.common.entity.bracelet.vo.PersonVo">
		select tp.id as id,tp.name as name,tp.sex as sex,tp.id_card as idCard,tp.phone as phone,
		tp.aqm_code as aqmCode,tp.mj_code as mjCode,sfs.file_path AS filePath,sfs.id AS fileId
		from t_work_team twt
		left join tb_people tp on twt.team_id = tp.team_id and tp.del_flag = 0
		left join sys_file_source sfs on tp.id = sfs.source_id AND sfs.source_type = #{sourceType} AND sfs.del_flag = 0
		<where>
			twt.team_id = #{teamId} and twt.del_flag = 0
			<if test="name != null and name!=''">
				AND INSTR(tp.name,#{name}) > 0
			</if>
			<if test="sex != null ">
				AND tp.sex = #{sex}
			</if>
		</where>
	</select>

	<!--临时人员设备列表-->
	<select id="getTempLists" resultType="com.bonus.common.entity.bracelet.vo.PersonVo">
		select tlu.id as id,tlu.name as name,tlu.sex as sex,tlu.id_card as idCard,tlu.phone as phone,
		sfs.file_path AS filePath,sfs.id AS fileId
		from t_work_team twt
		left join tb_ls_user tlu on twt.team_id = tlu.team_id
		left join sys_file_source sfs on tlu.id = sfs.source_id AND sfs.source_type = #{sourceType} AND sfs.del_flag = 0
		<where>
			twt.team_id = #{teamId} and twt.del_flag = 0
			<if test="name != null and name!=''">
				AND INSTR(tlu.name,#{name}) > 0
			</if>
			<if test="sex != null ">
				AND tlu.sex =#{sex}
			</if>
		</where>
	</select>

	<!--施工预警信息列表-->
	<select id="getWarnLists" resultType="com.bonus.common.entity.bracelet.vo.WarnInfoVo">
		select sdd.dict_label as devTypeName,tb.dev_name as devName,tw.warn_time as warnTime,tw.warn_content as warnContent
		from tb_warn tw
		left join tb_device tb on tw.dev_id = tb.id and tb.del_flag = 0
		left join sys_dict_data  sdd on sdd.dict_value = tb.dev_type and sdd.dict_type = 'sys_device_type'
		where tw.team_id = #{teamId} and tw.dev_type = 1 and tw.del_flag = 0
		union all
		select '手环箱' as warnType,tbb.sh_code as deviceName,tw.warn_time as warnTime,tw.warn_content as warnContent
		from tb_warn tw
		left join tb_bracelet tbb on tw.dev_id = tbb.id and tbb.del_flag = 0
		where tw.team_id = #{teamId} and tw.dev_type = 0 and tw.del_flag = 0
	</select>

	<select id="getConstructionSomeLists"  resultType="com.bonus.common.entity.bracelet.vo.QueryConstruction">
		select tp.pro_name as proName,tpd.depart_name as departName,twt.team_name as teamName,twt.team_leader as teamLeader,twt.leader_phone  as teamPhone,
		count(DISTINCT tbp.id) as teamNum,count(DISTINCT tlu.id) as tempNum,a.lytime as lyTime,count(DISTINCT tw.id) as warnNum,a.teamid as teamId
		from
		(select tdu.pro_id as proid,tdu.team_id as teamid,tdub.ly_time as lytime
		from tb_dev_use tdu
		left join tb_dev_use_bid tdub on tdub.id = tdu.id and tdub.dev_type = 'shx'
		where tdu.del_flag = 0) a
		left join tb_project tp on tp.id = a.proid and tp.del_flag = 0
		left join t_work_team  twt on twt.team_id = a.teamid and twt.pro_id = tp.id and  twt.del_flag = 0
		left join tb_project_depart tpd on tpd.id = tp.depart_id
		left join tb_people tbp on tbp.team_id = a.teamid  and tbp.del_flag = 0
		left join tb_ls_user tlu on  tlu.team_id = a.teamid
		left join tb_warn tw on tw.team_id = a.teamid and tw.del_flag = 0
		where
		 twt.team_id in
		<foreach collection="array" item="id" index="index" open="(" separator="," close=")">
			#{id}
		</foreach>
		group by proName,departName,teamName,teamLeader,teamPhone,a.lytime,a.teamid
		order by a.lytime desc
	</select>
</mapper> 