<?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="org.linlinjava.jz.db.dao.HmTaskListMapper">

<!-- 联合查询 hm_butler_task_parent  hm_notice
    统一处理status 0表示未完成（我的任务），2表示完成 （历史任务）
    bt.id as busid,
    t.id as noticeId,
    -->
	<select id="selectTaskList"
		resultType="org.linlinjava.jz.db.dto.hm.HmTaskListData">
		select *From ( 
		
			select   bt.id as id, '1' as type,  er.servant_name as servantName,
			er.employer_name as employerName,bt.create_time as createTime,bt.employ_relation_id as employRelationId,
			(CASE bt.status
			WHEN 0 THEN
			0
			ELSE
			2 end ) status,
			(select lg.operate_content from hm_butler_task_log lg where
			lg.task_parent_id=bt.id order by id desc limit 1) content
			From hm_butler_task_parent bt
			left join hm_employ_relation er on bt.employ_relation_id=er.id
			where er.user_butler_id=#{userButlerId} 
			<if test="null !=queryStr and queryStr!=''">
				and (
				 er.servant_name  like concat('%',#{queryStr},'%') 
				 or er.employer_name like concat('%',#{queryStr},'%')
				 )
			</if>
	
			UNION
	
			select t.id as id,  '2' as type, er.servant_name as servantName,
			er.employer_name as employerName, t.create_time createTime,t.employ_relation_id as employRelationId,
			(case 
			 (select ticket_status from hm_notice_user ntu where ntu.notice_id=t.id order by id desc limit 1)
			 when '2' then '2' else '0' end
			)	status,
			(select content from hm_notice_user ntu where ntu.notice_id=t.id order by id
			desc limit 1) content
			from hm_notice t
			left join hm_employ_relation er on t.employ_relation_id=er.id
			where er.user_butler_id=#{userButlerId}
			 and  FIND_IN_SET(t.r_type, #{rType})
			<if test="null !=queryStr and queryStr!=''">
				and (
				 er.servant_name  like concat('%',#{queryStr},'%') 
				 or er.employer_name like concat('%',#{queryStr},'%')
				 )
			</if>
			
			) tb
			where tb.status=#{status}
			<if test="yaerMonth !=null  and  yaerMonth != '' ">
				and  DATE_FORMAT(tb.createTime, '%Y-%m')=#{yaerMonth}
			</if>
			order by  createTime desc
	</select>
	
	<select id="selectUserTaskList"
		resultType="org.linlinjava.jz.db.dto.hm.HmTaskListData">
		select *From ( 
		
			select   bt.id as id, '1' as type,  er.servant_name as servantName,
			er.employer_name as employerName,bt.create_time as createTime,
			(CASE bt.status
			WHEN 0 THEN
			0
			ELSE
			2 end ) status,
			(select lg.operate_content from hm_butler_task_log lg where
			lg.task_parent_id=bt.id order by id desc limit 1) content
			From hm_butler_task_parent bt
			left join hm_employ_relation er on bt.employ_relation_id=er.id 
			where bt.employ_relation_id=#{employRelationId}
	
			UNION
	
			select t.id as id,  '2' as type, er.servant_name as servantName,
			er.employer_name as employerName, t.create_time createTime,
			(case 
			 (select ticket_status from hm_notice_user ntu where ntu.notice_id=t.id order by id desc limit 1)
			 when '2' then '2' else '0' end
			)	status,
			(select content from hm_notice_user ntu where ntu.notice_id=t.id order by id
			desc limit 1) content
			from hm_notice t 
			left join hm_employ_relation er on t.employ_relation_id=er.id 
			where t.employ_relation_id=#{employRelationId}
			 and  FIND_IN_SET(t.r_type, #{rType})
			) tb
			 order by  createTime desc
			 
	</select>


</mapper>