<?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.wechat.dao.WechatLeaveReportingDao">

    <select id="getLeaveReportingList" resultType="com.bonus.system.wechat.entity.WechatLeaveReportingBean">
        SELECT
        l.id,
        l.user_id,
        l.user_name,
        l.org_id,
        l.org_name,
        l.create_time,
        l.leave_type,
        l.type,
        l.leave_reason,
        l.leave_start_date,
        l.leave_start_interval,
        l.leave_end_date,
        l.leave_end_interval,
        l.leave_duration,
        GROUP_CONCAT(d.dict_label SEPARATOR ', ') AS postName,
        CASE
        WHEN l.source = '1' THEN '系统'
        WHEN l.source = '3' THEN '小程序'
        ELSE ''
        END AS source
        FROM
        leave_apply l
        LEFT JOIN sys_user_post p ON p.user_id = l.user_id
        LEFT JOIN sys_dict_data d on d.dict_code = p.post_id
        WHERE
        l.is_active = '1' AND l.type = '请假' AND (l.source = '1' OR l.source = '3')
        <if test="params.params.beginTime != null and params.params.beginTime != ''"><!-- 开始时间检索 -->
            and (
                date_format(l.leave_start_date,'%y%m%d') between date_format(#{params.params.beginTime},'%y%m%d') and date_format(#{params.params.endTime},'%y%m%d') or
                date_format(l.leave_end_date,'%y%m%d') between date_format(#{params.params.beginTime},'%y%m%d') and date_format(#{params.params.endTime},'%y%m%d')
            )
        </if>
        <if test='params.orgList != null and params.orgList.size() > 0'>
            and l.org_id in (
            <foreach collection="params.orgList" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>
        <if test="params.userName != null and params.userName != ''">
            AND l.user_name like concat('%', #{params.userName}, '%')
        </if>
        <if test="params.orgId != null and params.orgId != ''">
            AND l.org_id = #{params.orgId}
        </if>
        <if test="params.userId != null and params.userId != '' ">
            and l.user_id = #{params.userId}
        </if>
        GROUP BY
        l.id,
        l.user_id,
        l.user_name,
        l.org_id,
        l.org_name,
        l.create_time,
        l.leave_type,
        l.leave_reason,
        l.leave_start_date,
        l.leave_start_interval,
        l.leave_end_date,
        l.leave_end_interval,
        l.leave_duration,
        l.examine_status
        ORDER BY l.leave_start_date DESC
    </select>

    <select id="selectLeaveReportingById"
            resultType="com.bonus.system.wechat.entity.WechatLeaveReportingBean">
        SELECT l.id,
               l.user_id,
               l.user_name,
               l.org_id,
               l.org_name,
               l.create_time,
               l.leave_type,
               l.type,
               l.leave_reason,
               l.leave_start_date,
               l.leave_start_interval,
               l.leave_end_date,
               l.leave_end_interval,
               l.leave_duration,
               GROUP_CONCAT(d.dict_label SEPARATOR ', ') AS postName,
               l.host_user_id,
               l.host_user_name                             as hostUserName,
               l.is_agree,
               l.location,
               l.remark                                  as remark
        FROM leave_apply l
                 LEFT JOIN sys_user_post p ON p.user_id = l.user_id
                 LEFT JOIN sys_dict_data d on d.dict_code = p.post_id
        WHERE l.id = #{id}
    </select>

</mapper>