<?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.holiday.dao.HolidayDao">
    <insert id="insertHoliday" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO leave_apply(user_id,user_name,org_id,org_name,leave_type,leave_reason,
                                leave_start_date,
                                <if test="leaveStartInterval != null">
                                    leave_start_interval,
                                </if>
                                leave_end_date,
                                <if test="leaveEndInterval != null">
                                    leave_end_interval,
                                </if>
                                leave_duration,examine_status,source,create_user_id,is_agree, location, host_user_id, remark,type)
        VALUES(#{userId},#{userName},#{orgId},#{orgName},#{leaveType},#{leaveReason},
                #{leaveStartDate},
                <if test="leaveStartInterval != null">
                    #{leaveStartInterval},
                </if>
                #{leaveEndDate},
                <if test="leaveEndInterval != null">
                    #{leaveEndInterval},
                </if>
                #{leaveDuration},#{examineStatus},#{source},#{createUserId}, #{isAgree}, #{location}, #{hostUserId}, #{remark}, #{type})
    </insert>
    <update id="updateHoliday">
        UPDATE leave_apply
        <set>
            <if test="userId != null and userId != ''">
                user_id = #{userId},
            </if>
            <if test="userName != null and userName != ''">
                user_name = #{userName},
            </if>
            <if test="orgId != null and orgId != ''">
                org_id = #{orgId},
            </if>
            <if test="orgName != null and orgName != ''">
                org_name = #{orgName},
            </if>
            leave_reason = #{leaveReason},
            leave_start_date = #{leaveStartDate},
            leave_start_interval = #{leaveStartInterval},
            leave_end_date = #{leaveEndDate},
            leave_end_interval = #{leaveEndInterval},
            leave_duration = #{leaveDuration},
            examine_status = #{examineStatus},
            update_user_id = #{updateUserId},
            is_agree = #{isAgree},
            location = #{location},
            host_user_id = #{hostUserId},
            remark = #{remark}
        </set>
        where id = #{id}
    </update>
    <update id="changeHolidayStatus">
        update leave_apply
        <set>
        <if test="updateUserId != 0L">
            examine_opinion = #{examineOpinion},
            examine_user_id = #{examineUserId},
            examine_time = #{examineTime},
        </if>
        <if test="examineUserId != 0L">
             update_user_id = #{updateUserId},
        </if>
            examine_status = #{examineStatus}
        </set>
        where id = #{id}
    </update>
    <!--流程相关->轮休审批->一键审批-->
    <update id="batchCheckStatus">
        update leave_apply
        <set>
            <if test="updateUserId != 0L">
                examine_opinion = #{examineOpinion},
                examine_user_id = #{examineUserId},
                examine_time = #{examineTime},
            </if>
            <if test="examineUserId != 0L">
                update_user_id = #{updateUserId},
            </if>
            examine_status = #{examineStatus}
        </set>
        WHERE id IN
        <foreach collection="ids" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </update>

    <delete id="deleteHolidayById">
        update leave_apply SET is_active = '0' WHERE id = #{id}
    </delete>

    <select id="getHolidayList" resultType="com.bonus.system.holiday.entity.HolidayBean" parameterType="com.bonus.system.holiday.entity.HolidayBean">
        SELECT
        DISTINCT
        la.id,
        la.user_id,
        la.user_name,
        la.org_id,
        la.org_name,
        la.create_time,
        la.leave_type,
        la.leave_reason,
        la.leave_start_date,
        la.leave_start_interval,
        la.leave_end_date,
        la.leave_end_interval,
        la.leave_duration,
        la.examine_status
        FROM
        leave_apply la
        LEFT JOIN sys_user_org suo on suo.user_id=la.user_id and suo.is_active=1
        WHERE
        la.is_active = '1'
        <if test="bean.leaveDate != null and bean.leaveDate != '' ">
            and locate(#{bean.leaveDate},la.create_time)
        </if>
        <if test="bean.examineStatus != null and bean.examineStatus != '' ">
            and la.examine_status = #{bean.examineStatus}
        </if>
        <if test="bean.leaveType != null and bean.leaveType != '' ">
            and locate(#{bean.leaveType},la.leave_type)
        </if>
        <if test="bean.type != null and bean.type != '' ">
            and locate(#{bean.type},la.type)
        </if>
        <if test="bean.orgName != null and bean.orgName != '' ">
            and locate(#{bean.orgName},la.org_name)
        </if>
        <if test="bean.userName != null and bean.userName != '' ">
            and locate(#{bean.userName},la.user_name)
        </if>
        <if test="bean.userId != null and bean.userId != '' ">
            and (la.user_id = #{bean.userId} or la.create_user_id = #{bean.userId} )
        </if>
        <if test="bean.userIds != null and bean.userIds != '' ">
            and  la.user_id != #{bean.userIds}
        </if>

        <if test="bean.roleType =='2' || bean.roleType == 2 ">
            and suo.org_id in (
            <foreach collection="bean.OrgListId" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>

        <if test='bean.orgList != null and bean.orgList.size() > 0'>
            and suo.org_id in (
            <foreach collection="bean.orgList" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>
        ORDER BY la.update_time DESC
    </select>

    <select id="selectHolidayById" resultType="com.bonus.system.holiday.entity.HolidayBean">
        SELECT
        id,
        user_id,
        user_name,
        org_id,
        org_name,
        create_time,
        leave_type,
        leave_reason,
        leave_start_date,
        leave_start_interval,
        leave_end_date,
        leave_end_interval,
        leave_duration,
        examine_status,
        examine_opinion,
        is_agree, location, host_user_id, remark
        FROM
        leave_apply
        WHERE
        id = #{id}
    </select>
    <select id="getLeaveType" resultType="java.util.Map">

    </select>
    <select id="getOrgList" resultType="com.bonus.system.holiday.entity.HolidayKeyBean">
        SELECT
            `id`,
            org_name as `name`
        FROM
            sys_organization
        WHERE
            is_active = '1'
    </select>
    <select id="getPersonList" resultType="com.bonus.system.holiday.entity.HolidayKeyBean">
        SELECT
            user_id as `id`,
            user_name as `name`
        FROM
            sys_user
        WHERE
            is_active = '1'
    </select>
    <select id="getLeaveTypeList" resultType="com.bonus.system.holiday.entity.HolidayKeyBean">
        SELECT
            dict_code  as `id`,
            dict_label as `name`
        FROM
            sys_dict_data
        WHERE
            dict_type = 'att_status' and dict_code > 47
    </select>
    <select id="getUserIdById" resultType="java.lang.Long">
        SELECT
            user_id
        FROM
            leave_apply
        WHERE
            id = #{id}
    </select>

    <select id="getHolidayData" resultType="com.bonus.system.holiday.entity.HolidayImportBean">
        SELECT
            user_name,leave_start_date,leave_end_date
        FROM
            leave_apply
        WHERE leave_end_date > #{previousMonthDate}
    </select>

    <select id="getHolidayDataByType" resultType="java.lang.String">
        SELECT date
        FROM sys_holiday
        WHERE date between #{startDate} and #{endDate} and type = 1
    </select>



    <select id="judgeRepeatStrat" resultType="com.bonus.system.holiday.entity.HolidayBean">
        SELECT leave_type AS leaveType,
        `type`,
        leave_start_date,
        leave_end_date,
        leave_start_interval AS leaveStartInterval,
        leave_end_interval AS leaveEndInterval,
        user_name AS userName
        FROM leave_apply
        WHERE is_active = '1' and user_id = #{userId}  and #{leaveStartDate} BETWEEN leave_start_date AND leave_end_date
        <if test='id != null and id !=""'>
            and id !=#{id}
        </if>
    </select>

    <select id="judgeRepeatEnd" resultType="com.bonus.system.holiday.entity.HolidayBean">
        SELECT leave_type AS leaveType,
        `type`,
        leave_start_date,
        leave_end_date,
        leave_start_interval AS leaveStartInterval,
        leave_end_interval AS leaveEndInterval,
        user_name AS userName
        FROM leave_apply
        WHERE is_active = '1' and user_id = #{userId}  and #{leaveEndDate} BETWEEN leave_start_date AND leave_end_date
        <if test='id != null and id !=""'>
            and id !=#{id}
        </if>
    </select>


</mapper>