<?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.yifu.cloud.plus.v1.business.mapper.vacation.TVacationInfoMapper">

    <resultMap id="TVacationInfoVO" type="com.yifu.cloud.plus.v1.business.vo.vacation.TVacationInfoVO">
            <id property="id" column="ID" jdbcType="VARCHAR"/>
            <result property="businessTelecomNumber" column="BUSINESS_TELECOM_NUMBER" jdbcType="VARCHAR"/>
            <result property="empName" column="EMP_NAME" jdbcType="VARCHAR"/>
            <result property="empIdcard" column="EMP_IDCARD" jdbcType="VARCHAR"/>
            <result property="departId" column="DEPART_ID" jdbcType="INTEGER"/>
            <result property="departName" column="DEPART_NAME" jdbcType="VARCHAR"/>
            <result property="vacationStatus" column="VACATION_STATUS" jdbcType="CHAR"/>
            <result property="businessPost" column="BUSINESS_POST" jdbcType="VARCHAR"/>
            <result property="vacationType" column="VACATION_TYPE" jdbcType="VARCHAR"/>
            <result property="vacationReason" column="VACATION_REASON" jdbcType="VARCHAR"/>
            <result property="vacationNote" column="VACATION_NOTE" jdbcType="VARCHAR"/>
            <result property="vacationStartTime" column="VACATION_START_TIME" jdbcType="TIMESTAMP"/>
            <result property="vacationEndTime" column="VACATION_END_TIME" jdbcType="TIMESTAMP"/>
            <result property="vacationDuration" column="VACATION_DURATION" jdbcType="DECIMAL"/>
            <result property="vacationNotUsedDuration" column="VACATION_NOT_USED_DURATION" jdbcType="DECIMAL"/>
            <result property="acturalVacationEndTime" column="ACTURAL_VACATION_END_TIME" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="GetListColumnList">
        a.ID,
        a.BUSINESS_TELECOM_NUMBER,
        (case
              when a.VACATION_STATUS <![CDATA[<>]]> '4' then '3'
              when now() <![CDATA[<]]> a.VACATION_START_TIME then '0'
              when now() <![CDATA[>=]]> a.VACATION_START_TIME and now() <![CDATA[<=]]> a.VACATION_END_TIME then '1'
              when now() <![CDATA[>]]> a.VACATION_END_TIME then '2'
              else ''
              end
        ) as VACATION_STATUS,
        a.EMP_NAME,
        a.EMP_IDCARD,
        a.DEPART_ID,
        a.BUSINESS_POST,
        a.VACATION_TYPE,
        a.VACATION_START_TIME,
        a.VACATION_END_TIME,
        a.VACATION_DURATION,
        a.VACATION_REASON,
        a.VACATION_NOTE,
        a.VACATION_NOT_USED_DURATION,
        a.ACTURAL_VACATION_END_TIME,
        b.NAME as DEPART_NAME
    </sql>
    <sql id="GetListWhere">
        1=1
        <if test="query.businessTelecomNumber != null and query.businessTelecomNumber.trim() != ''">
            and a.BUSINESS_TELECOM_NUMBER like concat('%',#{query.businessTelecomNumber} ,'%')
        </if>
        <if test="query.empName != null and query.empName.trim() != ''">
            and a.EMP_NAME like concat('%',#{query.empName} ,'%')
        </if>
        <if test="query.empIdcard != null and query.empIdcard.trim() != ''">
            and a.EMP_IDCARD like concat('%',#{query.empIdcard} ,'%')
        </if>
        <if test="query.businessPost != null and query.businessPost.trim() != ''">
            and a.BUSINESS_POST = #{query.businessPost}
        </if>
        <if test="query.departId != null and query.departId != 1">
            AND (b.id = #{query.departId} or b.tree_logo like concat('%-', #{query.departId},'-%'))
        </if>
        <if test="query.vacationType != null and query.vacationType.trim() != ''">
            and a.VACATION_TYPE = #{query.vacationType}
        </if>
        <if test="query.vacationStartTime != null and query.vacationEndTime != null">
            and
            (a.VACATION_START_TIME between #{query.vacationStartTime} and #{query.vacationEndTime}
            or
             a.VACATION_END_TIME between #{query.vacationStartTime} and #{query.vacationEndTime}
             )
        </if>
		<if test="query.vacationStatus != null and query.vacationStatus.trim() != ''">
			<!-- 待休 -->
			<if test="query.vacationStatus == '0'.toString()">
				and now() <![CDATA[<]]> a.VACATION_START_TIME and a.VACATION_STATUS = '4'
			</if>
			<!-- 休假中 -->
			<if test="query.vacationStatus == '1'.toString()">
				and now() <![CDATA[>=]]> a.VACATION_START_TIME and now() <![CDATA[<=]]> a.VACATION_END_TIME and a.VACATION_STATUS = '4'
			</if>
			<!-- 到期待销假 -->
			<if test="query.vacationStatus == '2'.toString()">
				and now() <![CDATA[>]]> a.VACATION_END_TIME and a.VACATION_STATUS = '4'
			</if>
			<!-- 已销假 -->
			<if test="query.vacationStatus == '3'.toString()">
				and a.VACATION_STATUS <![CDATA[<>]]> '4'
			</if>
		</if>
    </sql>

    <select id="getListByPage" resultMap="TVacationInfoVO">
        select
        <include refid="GetListColumnList"/>
        from hr_business.t_vacation_info a
        left join hr_business.t_bus_dept b
        on a.DEPART_ID = b.id
        <where>
            <include refid="GetListWhere"/>
        </where>
        order by a.CREATE_TIME desc
    </select>

    <select id="getDataById" resultMap="TVacationInfoVO">
        select
        <include refid="GetListColumnList"/>
        from hr_business.t_vacation_info a
        left join hr_business.t_bus_dept b
        on a.DEPART_ID = b.id
        where a.id = #{id}
    </select>
</mapper>
