<?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.jeeplus.modules.leaveapply.dao.LeaveApplyDao">
    
	<sql id="leaveApplyColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		a.branch_office AS "branchOffice",
		a.staff_id AS "staffId",
		a.start_time AS "startTime",
		a.end_time AS "endTime",
		a.start_type AS "startType",
		a.end_type AS "endType",
		a.long_time AS "longTime",
		a.status AS "status",
		a.process_instance_id AS "processInstanceId",
		a.sell_status AS "sellStatus",
		a.apply_time AS "applyTime",
		a.no AS "no",
		a.reason AS "reason",
		w.name AS "basicInfo.name",
		w.id AS "basicInfo.id",
		w.no AS "basicInfo.no",
		w.status as "basicInfo.status",
		m.top_company AS "basicInfo.office.name",
		m.id AS "basicInfo.office.id",
		j.name AS "basicInfo.jobGrade.name",
		a.applyer,
		a.back_time,
		a.contrast,
		a.back_start_type as "backStartType"

	</sql>
	
	<sql id="leaveApplyJoins">
		left join work_staff_achives w on w.id=a.staff_id
		left join sys_office m on m.id=w.office_id
		left join work_job_grade j on j.id=w.job_grade
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN sys_user u ON u.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="LeaveApply" >
		SELECT 
			<include refid="leaveApplyColumns"/>
		FROM leave_apply a
		<include refid="leaveApplyJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getByStaffIdAndTime" resultType="LeaveApply" >
		SELECT
		<include refid="leaveApplyColumns"/>
		FROM leave_apply a
		<include refid="leaveApplyJoins"/>
		LEFT JOIN leave_detail d ON d.leave_id = a.id
		WHERE a.staff_id = #{staffId}
		AND a.status IN ('5','8')
		AND #{time}
		BETWEEN d.start_time AND d.end_time
		AND #{time} &lt;= d.real_end_time;
	</select>
	
	<select id="findList" resultType="LeaveApply" >
		SELECT 
			<include refid="leaveApplyColumns"/>
		FROM leave_apply a
		<include refid="leaveApplyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="beginApplyTime != null and endApplyTime != null and beginApplyTime != '' and endApplyTime != ''">
				AND a.apply_time BETWEEN #{beginApplyTime} AND #{endApplyTime}
			</if>
			<if test="basicInfo != null and basicInfo.name != null and basicInfo.name != ''">
				AND w.name  LIKE
				<if test="dbName == 'oracle'">'%'||#{basicInfo.name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{basicInfo.name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{basicInfo.name},'%')</if>
			</if>
			${sqlMap.dsf}
			<if test="staffId != null and staffId != ''">
				AND a.staff_id = #{staffId}
			</if>
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="LeaveApply" >
		SELECT 
			<include refid="leaveApplyColumns"/>
		FROM leave_apply a
		<include refid="leaveApplyJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO leave_apply(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			branch_office,
			staff_id,
			start_time,
			end_time,
			start_type,
			end_type,
			long_time,
			status,
			process_instance_id,
			sell_status,
			apply_time,
			no,
			reason,
			applyer,
			back_time,
		    contrast,
		    back_start_type

		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeId},
			#{companyId},
			#{branchOffice},
			#{basicInfo.id},
			#{startTime},
			#{endTime},
			#{startType},
			#{endType},
			#{longTime},
			#{status},
			#{processInstanceId},
			#{sellStatus},
			#{applyTime},
			#{no},
			#{reason},
			#{applyer},
			#{backTime},
			#{contrast},
			#{backStartType}

		)
	</insert>
	
	<update id="update">
		UPDATE leave_apply SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{officeId},
			company_id = #{companyId},
			branch_office = #{branchOffice},
			staff_id = #{basicInfo.id},
			start_time = #{startTime},
			end_time = #{endTime},
			start_type = #{startType},
			end_type = #{endType},
			long_time = #{longTime},
			status = #{status},
			process_instance_id = #{processInstanceId},
			sell_status = #{sellStatus},
			apply_time = #{applyTime},
			no = #{no},
			reason = #{reason},
			applyer=#{applyer},
			back_time = #{backTime},
			contrast=#{contrast},
			back_start_type = #{backStartType}

		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM leave_apply
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE leave_apply SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="LeaveApply" statementType="STATEMENT">
		select * FROM leave_apply  where ${propertyName} = '${value}'
	</select>
	<update id="updateProcessIdAndStatus" >
        UPDATE  leave_apply SET
        process_instance_id = #{processInstanceId},
        status = #{status}
        WHERE id = #{id}
    </update>

	<select id="getCount" resultType="LeaveApply" >
		SELECT
		<include refid="leaveApplyColumns"/>
		FROM leave_apply a
		<include refid="leaveApplyJoins"/>
		WHERE a.staff_id = #{basicInfo.id} and a.status not in ('8')
	</select>
	<select id="getMaxTime" resultType="java.lang.String" parameterType="java.lang.String">
	    SELECT
	       max(DATE_FORMAT(end_time, '%Y-%m-%d')) AS endTime
         FROM `leave_apply` l
        WHERE
       del_flag='0'
      and staff_id = #{staffId}
      and id !=#{id}
	</select>

	<select id="findHisTime" resultType="java.lang.Integer">
	SELECT
	count(*) as "total"
	FROM leave_detail a
	<where>
		a.del_flag = '0'
		AND a.leave_id in
		(select y.id from leave_apply y where y.staff_id=#{staffId} and y.status='8' and y.del_flag='0'
		<if test="id != null and id != ''">
			and y.id!=#{id}
		</if>)
		and ((#{startTime}> a.start_time  and #{startTime} &lt; a.end_time)
		or (#{endTime} &gt; a.start_time and #{endTime} &lt; a.end_time))
	</where>
</select>
	<select id="findHisDate" resultType="java.lang.Integer">
		SELECT
	count(*) AS "total" FROM
	leave_detail a WHERE
	a.del_flag = '0' AND a.leave_id IN (
	SELECT y.id FROM leave_apply y WHERE y.staff_id =#{staffId}
	AND y. STATUS = '8'
	AND y.del_flag = '0'
	AND y.id !=#{id})
    AND ((a.start_time = #{startTime} and a.start_type=#{startType})
	    OR (a.start_time = #{endTime} and a.start_type=#{endType})
	    OR (a.end_time = #{startTime} and a.end_type=#{startType})
        OR (a.end_time = #{endTime} and a.end_type=#{endType}))
	</select>

	<select id="getMange" resultType="LeaveApply" >
		SELECT
		<include refid="leaveApplyColumns"/>
		FROM leave_apply a
		<include refid="leaveApplyJoins"/>
		WHERE a.company_id=#{companyId} and a.del_flag = '0' and a.status in('5','8','6','7','9')
		<if test="basicInfo != null and basicInfo.name != null and basicInfo.name != ''">
			AND w.name LIKE
			<if test="dbName == 'oracle'">'%'||#{basicInfo.name}||'%'</if>
			<if test="dbName == 'mssql'">'%'+#{basicInfo.name}+'%'</if>
			<if test="dbName == 'mysql'">concat('%',#{basicInfo.name},'%')</if>
		</if>
		<if test="basicInfo != null and basicInfo.office != null  and basicInfo.office.id != null and basicInfo.office.id != ''">
			AND a.office_id = #{basicInfo.office.id}
		</if>
<!--		有异常-->
<!--		group by a.staff_id order by w.no ASC-->
	</select>

	<select id="getSumDays" resultType="LeaveApply" parameterType="java.lang.String">
		SELECT
	ifnull(sum(a.sums),0) as "sumDays"
		FROM leave_count a
		where
			a.del_flag = '0'
			and a.staff_id =#{staffId}
	</select>
	<select id="getResetState" resultType="int" parameterType="java.lang.String">
		SELECT
		count(*)
		FROM leave_count a
		where
			a.del_flag = '0'
			and a.staff_id =#{staffId}
			and a.reset_state='0'
	</select>
	<update id="updateDetailFlat">
		UPDATE leave_detail a SET
			a.flat = '1',a.state='0'
		WHERE a.leave_id=#{id}
	</update>
	<update id="deleteDetail">
		DELETE FROM leave_detail
		WHERE leave_id = #{id} and state='1'
	</update>

	<update id="updateBack">
		UPDATE leave_apply a SET
			a.back_time=null,
		a.contrast=null
		WHERE a.id=#{id}
	</update>
	<update id="updateLongTime">
		UPDATE leave_apply a SET
			a.long_time=(select ifnull(sum(days),0) from leave_detail
		WHERE leave_id = #{id} and state!='1')
		WHERE a.id=#{id}
	</update>


	<select id="getBackTaskList" resultType="LeaveApply" >
     SELECT
		<include refid="leaveApplyColumns"/>
		FROM leave_apply a
		left join work_staff_achives w on w.id=a.staff_id
		left join sys_office m on m.id=w.office_id
		left join work_job_grade j on j.id=w.job_grade
      where a.id in (SELECT a.id FROM leave_apply a
		LEFT JOIN (SELECT d.leave_id, max(d.end_time) AS end_time FROM leave_detail d
		WHERE d.del_flag = '0' GROUP BY d.leave_id) i ON i.leave_id = a.id
		WHERE a.del_flag = '0'
		AND a.`status` = '5'
		AND date_add(DATE_FORMAT(i.end_time, '%Y-%m-%d'),INTERVAL 1 day)=DATE_FORMAT(now(), '%Y-%m-%d'))
	</select>
</mapper>