<?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.system.att.dao.AttExceptionHandleDao">

	<select id="selectAttExceptionList" resultType="com.bonus.system.att.entity.AttExceptionBean">
		select
			#{attCurrentMonth} as attCurrentMonth,
			count(case when (leave_start_date &lt; #{attNextMonthFirst} and leave_start_date &gt;= #{attCurrentMonthFirst})  then 1 END) as errorCount,
			count(case when (examine_status != 0 and leave_start_date &lt; #{attNextMonthFirst} and leave_start_date &gt;= #{attCurrentMonthFirst})  then 1 end) as finishCount,
			count(case when (examine_status = 0 and leave_start_date &lt; #{attNextMonthFirst} and leave_start_date &gt;= #{attCurrentMonthFirst})  then 1 end) as noFinishCount
		from leave_apply as la
		where la.is_active = 1
		  and (la.leave_type  like '%外出%' or la.leave_type  like '%轮休%')
	</select>

	<select id="selectAttExceptionMonth" resultType="java.util.Map">
		select
			DATE_FORMAT(leave_start_date,'%Y-%m') as month,
		count(*) as leaveCount
		from leave_apply as la
		where la.is_active = 1
		  and (la.leave_type  like '%外出%' or la.leave_type  like '%轮休%')
		group by month
	</select>

	<select id="selectAttExceptionNoHandleMonth" resultType="java.util.Map">
		select
			DATE_FORMAT(leave_start_date,'%Y-%m') as month,
		count(*) as leaveCount
		from leave_apply as la
		where la.is_active = 1
		  and (la.leave_type  like '%外出%' or la.leave_type  like '%轮休%')
		  and la.examine_status=3
		  and (examine_time is null or examine_time = '')
		group by month
	</select>

	<select id="getDetailException" resultType="com.bonus.system.att.entity.AttExceptionBean">
		select
		user_id as userId,
		user_name as name,
		org_id as orgId,
		org_name as orgName,
		#{attCurrentMonth} as attCurrentMonth,
		count(case when la.leave_type  like '%轮休%' then 1 END ) as errorRestCount,
		count(case when la.leave_type  like '%外出%' then 1 END) as errorOutCount
		from leave_apply as la
		where la.is_active = 1
		and examine_status = 0
		and (leave_start_date &lt; #{attNextMonthFirst} and leave_start_date &gt;= #{attCurrentMonthFirst})
		<if test="orgId != null and orgId != '' ">
			and la.org_id = #{orgId}
		</if>
		<if test="name != null and name != '' ">
			and INSTR(la.user_name,#{name}) > 0
		</if>
		group by user_id
	</select>

	<select id="getDetailAllException" resultType="com.bonus.system.att.entity.AttExceptionBean">
		select
		user_id as userId,
		user_name as name,
		org_id as orgId,
		org_name as orgName,
		leave_start_date as attCurrentMonth,
		count(case when la.leave_type  like '%轮休%' then 1 END ) as errorRestCount,
		count(case when la.leave_type  like '%外出%' then 1 END) as errorOutCount
		from leave_apply as la
		where
		<foreach collection="month" item="monthOne" separator="OR">
			(
			leave_start_date &gt;= CONCAT(#{monthOne}, '-01') AND
			leave_start_date &lt; LAST_DAY(CONCAT(#{monthOne}, '-01'))
			)
		</foreach>
		and   la.is_active = 1
		and examine_status = 0
		<if test="orgId != null and orgId != '' ">
			and la.org_id in
			<foreach item="id" collection="orgIds" open="(" separator="," close=")">
				#{id}
			</foreach>
		</if>
		<if test="name != null and name != '' ">
			and INSTR(la.user_name,#{name}) > 0
		</if>
		group by user_id
	</select>

	<select id="getDetailExceptionRest" resultType="com.bonus.system.att.entity.AttDetailLeaveBean">
		select
		id as id,
		user_id as userId,
		user_name as name,
		org_id as orgId,
		org_name as orgName,
		leave_type as leaveType,
		examine_status as examineStatus,
		concat(leave_start_date,' ',
		case
		when leave_start_interval=1 then '上午'
		when  leave_start_interval then '下午'
		ELSE ''
		END) as leaveStartDate,
		concat(leave_end_date,' ',
		case
		when leave_end_interval=1 then '上午'
		when  leave_end_interval then '下午'
		ELSE ''
		END) as leaveEndDate,
		leave_duration as leaveDuration
		from leave_apply as la
		where la.is_active = 1
		and examine_status = 0
		and (leave_start_date &lt; #{attNextMonthFirst} and leave_start_date &gt;= #{attCurrentMonthFirst}) and (la.leave_type  like '%轮休%')
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			and date_format(la.leave_start_date,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			and date_format(la.leave_end_date,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<if test="userId != null and userId != '' ">
			and la.user_id=#{userId}
		</if>
	</select>

	<select id="getDetailExceptionOut" resultType="com.bonus.system.att.entity.AttDetailLeaveBean">
		select
		id as id,
		user_id as userId,
		user_name as name,
		org_id as orgId,
		org_name as orgName,
		leave_type as leaveType,
		examine_status as examineStatus,
		concat(leave_start_date,' ',
		case
		when leave_start_interval=1 then '上午'
		when  leave_start_interval then '下午'
		ELSE ''
		END) as leaveStartDate,
		concat(leave_end_date,' ',
		case
		when leave_end_interval=1 then '上午'
		when  leave_end_interval then '下午'
		ELSE ''
		END) as leaveEndDate,
		leave_duration as leaveDuration
		from leave_apply as la
		where la.is_active = 1
		and examine_status = 0
		and (leave_start_date &lt; #{attNextMonthFirst} and leave_start_date &gt;= #{attCurrentMonthFirst}) and (la.leave_type  like '%外出%')
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			and date_format(la.leave_start_date,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			and date_format(la.leave_end_date,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<if test="userId != null and userId != '' ">
			and la.user_id=#{userId}
		</if>
	</select>

	<select id="getDetailExceptionAll" resultType="com.bonus.system.att.entity.AttDetailLeaveBean">
		select
		id as id,
		user_id as userId,
		user_name as name,
		org_id as orgId,
		org_name as orgName,
		leave_type as leaveType,
		examine_status as examineStatus,
		concat(leave_start_date,' ',
		case
		when leave_start_interval=1 then '上午'
		when  leave_start_interval then '下午'
		ELSE ''
		END) as leaveStartDate,
		concat(leave_end_date,' ',
		case
		when leave_end_interval=1 then '上午'
		when  leave_end_interval then '下午'
		ELSE ''
		END) as leaveEndDate,
		leave_duration as leaveDuration
		from leave_apply as la
		where la.is_active = 1
		and #{attCurrentMonth} BETWEEN DATE_FORMAT(leave_start_date, '%Y-%m') and DATE_FORMAT(leave_end_date, '%Y-%m')
		and (la.leave_type  like '%外出%' or la.leave_type  like '%轮休%')
		<if test="name != null and name != '' ">
			and INSTR(la.user_name,#{name}) > 0
		</if>
		<if test="isExamine  != null  and isExamine == 1">
			and la.examine_status = 0
		</if>
		<if test="isExamine  != null  and isExamine == 0">
			and (la.examine_status = 1 or la.examine_status = 2 or la.examine_status = 3)
		</if>
	</select>

	<select id="getOrgUserId" resultType="com.bonus.system.att.entity.AttExRemindBean">
		select
			org_head_user_id as orgHeadId
		from sys_organization as so
		where so.id = #{orgId}
	</select>

	<insert id="examineRemind" parameterType="com.bonus.system.att.entity.AttExRemindBean" >
		insert into sys_notice(
		<if test="userUpdate != null">user_id,</if>
		<if test="title != null and title != ''">title,</if>
		<if test="content != null and content != '' ">content,</if>
		type,
		create_time,
		update_time,
		is_active
		)values(
		<if test=" userUpdate != null">#{userUpdate},</if>
		<if test="title != null and title != ''">#{title},</if>
		<if test="content != null and content != ''">#{content},</if>
		#{type},
		now(),
		now(),
		1
		)
	</insert>

	<insert id="examineRemindTemp" parameterType="com.bonus.system.att.entity.AttExRemindBean" >
		insert into sys_notice_user(
		<if test="userId != null">user_id,</if>
		create_time,
		update_time,
		is_active
		)values(
		<if test=" userId != null">#{userId},</if>
		now(),
		now(),
		1
		)
	</insert>
</mapper>
