<?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.LeaveDetailDao">
    
	<sql id="leaveDetailColumns">
		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.leave_id AS "leaveId",
		a.days AS "days",
		a.flat AS "flat",
		a.state AS "state",
		a.start_time AS "startTime",
		a.end_time AS "endTime",
		a.real_end_time AS "realEndTime",
		a.real_end_type AS "realEndType",
		a.real_days AS "realDays",
		a.is_use AS "isUse",
		a.start_type AS "startType",
		a.end_type AS "endType",
		a.leave_type_id AS "leaveTypeId",
		b.name as "leaveType.name",
		b.model as "leaveType.model",
		b.unit as "leaveType.unit",
		b.method as "leaveType.method",
		b.zero as "leaveType.zero",
		b.is_attendance as "leaveType.isAttendance",
		b.is_next as "leaveType.isNext",
		b.no as "leaveType.no",
		b.id as "leaveType.id"
	</sql>
	
	<sql id="leaveDetailJoins">
		left join leave_type b on b.id=a.leave_type_id
	</sql>
	
    
	<select id="get" resultType="LeaveDetail" >
		SELECT 
			<include refid="leaveDetailColumns"/>
		FROM leave_detail a
		<include refid="leaveDetailJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="LeaveDetail" >
		SELECT 
			<include refid="leaveDetailColumns"/>
		FROM leave_detail a
		<include refid="leaveDetailJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.leave_id = #{leaveId}
		</where>
		ORDER BY a.create_date Asc
	</select>
	
	<select id="findAllList" resultType="LeaveDetail" >
		SELECT 
			<include refid="leaveDetailColumns"/>
		FROM leave_detail a
		<include refid="leaveDetailJoins"/>
		<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_detail(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			leave_id,
			days,
			flat,
			leave_type_id,
			start_time,
		    end_time,
		    start_type,
		    end_type,
		    state,
		    real_end_time,
		    is_use,
		    real_end_type,
		    real_days
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{leaveId},
			#{days},
			#{flat},
			#{leaveType.id},
			#{startTime},
			#{endTime},
			#{startType},
			#{endType},
			#{state},
			#{realEndTime},
			#{isUse},
			#{realEndType},
			#{realDays}
		)
	</insert>
	
	<update id="update">
		UPDATE leave_detail SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			leave_id = #{leaveId},
			days = #{days},
			del_flag=#{delFlag},
			leave_type_id = #{leaveType.id},
			start_time = #{startTime},
			end_time = #{endTime},
			start_type = #{startType},
			end_type = #{endType}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM leave_detail
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE leave_detail SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="LeaveDetail" statementType="STATEMENT">
		select * FROM leave_detail  where ${propertyName} = '${value}'
	</select>
	<select id="findListHis" resultType="LeaveDetail" parameterType="java.lang.String">
		SELECT
		b.name as "name",
		a.sums as "sumDays",
		a.counts as "counts",
		a.sums as "days"
		FROM leave_count a
		left join leave_type b on b.id=a.leave_type_id
		<where>
			a.staff_id=#{staffId}
		</where>
	</select>
	<select id="findListResetHis" resultType="LeaveDetail" parameterType="java.lang.String">
		SELECT
		b.name as "name",
		sum(a.days) as "sumDays",
		count(*) as "counts",
		sum(a.days) as "days"
		FROM leave_detail a
		left join leave_type b on b.id=a.leave_type_id
		<where>
			a.del_flag = '0'  and (a.reset_state='0' or a.reset_state is null)
			AND a.leave_id in (select id from leave_apply y where y.staff_id=#{staffId} and y.status='8' and y.del_flag='0')
			group by a.leave_type_id
		</where>
	</select>

	<update id="updateReset"  parameterType="java.lang.String">
		update leave_detail a
		set a.reset_state='1',
		reset_time=now()
		<where>
			a.del_flag = '0'  and (a.reset_state='0' or a.reset_state is null)
			AND a.leave_id in (select id from leave_apply y where y.staff_id=#{staffId} and y.status='8' and y.del_flag='0')
		</where>
	</update>

	<select id="findHisList" resultType="LeaveDetail" parameterType="java.lang.String">
		SELECT
		<include refid="leaveDetailColumns"/>
		FROM leave_detail a
		<include refid="leaveDetailJoins"/>
		<where>
			a.del_flag = '0'
            and a.is_use='0'
			AND a.leave_id in (select id from leave_apply y where y.staff_id=#{staffId} and y.status='8' and y.del_flag='0')
		</where>
	</select>
    <select id="findListValidateTime" resultType="LeaveDetail" parameterType="java.lang.String">
        SELECT
        <include refid="leaveDetailColumns"/>
        FROM leave_detail a
        <include refid="leaveDetailJoins"/>
        <where>
            a.del_flag = '0'
            and a.is_use='0'
            AND a.leave_id in (select id from leave_apply y where y.staff_id=#{staffId} and y.del_flag='0'AND y.status not in('1','3','4') )
        </where>
    </select>


	<update id="updateState" >
		update leave_detail a
		set a.state='0'
		<where>
			a.del_flag = '0'
			AND a.leave_id =#{leaveId}
		</where>
	</update>



	<select id="findMaxEndTime" resultType="java.lang.String" parameterType="java.lang.String">
		SELECT
	    max(DATE_FORMAT(end_time, '%Y-%m-%d')) AS endTime
	    FROM
	    leave_detail a
	    WHERE
	    a.del_flag = '0' and a.state = '0'
	    AND a.leave_id =#{leaveId}
	</select>

    <update id="updateRealEndDate" >
        update leave_detail a
        set a.real_end_time=#{time},
        a.real_end_type=#{type},
		a.real_days=#{realDays}
        <where>
          a.id =#{id}
        </where>
    </update>
    <update id="updateIsUse" >
        update leave_detail a
        set a.is_use='1'
        <where>
            a.id =#{id}
        </where>
    </update>

	<select id="sumday" resultType="java.lang.String" parameterType="java.lang.String">
		SELECT
	ifnull(sum(d.real_days),0) sumday
         FROM
	leave_detail d
         WHERE
	d.is_use = '0'
  AND d.del_flag = '0'
  AND d.state = '0'
  AND d.leave_type_id =#{typeId}
  AND d.leave_id !=#{leaveId}
	</select>
	<select id="onlyOne" resultType="LeaveDetail" parameterType="java.lang.String">
		SELECT
		<include refid="leaveDetailColumns"/>
		FROM leave_detail a
		<include refid="leaveDetailJoins"/>
         WHERE
	a.is_use = '0'
  AND a.del_flag = '0'
  AND a.state = '0'
  and a.leave_id in (select id from leave_apply y where y.staff_id=#{staffId} and y.status='8' and y.del_flag='0')
  and a.leave_type_id=#{typeId}
	</select>

	<select id="maxdays" resultType="java.lang.String" parameterType="java.lang.String">
		SELECT
	ifnull(sum(d.real_days),0) sumday
         FROM
	leave_detail d
         WHERE
	d.is_use = '0'
  AND d.del_flag = '0'
  AND d.state = '0'
  AND d.leave_type_id =#{typeId}
  AND d.leave_id in (select id from leave_apply y where y.staff_id=#{staffId} and y.status='8' and y.del_flag='0')
	</select>
</mapper>