<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//oos.juyouhx.com//DTD Mapper 3.0//EN" "http://oss.juyouhx.com/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.scpyun.platform.standard.jilinsscgsdp.keyDmLeaveApprovalRecord" name="请假审批记录">

    <select id="selectApprovalRecords" parameterType="map" resultType="map">
        SELECT id,leave_id,node_index,node_type,approver_id,approver_name,approver_result,approver_time,status,comment,transfer_to_id,transfer_to_name,transfer_reason
        FROM jl_key_dm_leave_approval_record
        WHERE leave_id = #{leave_id} and
        status != 0
        ORDER BY approver_time ASC
    </select>

    <select id="selectById" parameterType="map" resultType="map">
        SELECT id,leave_id,node_index,node_type,approver_id,approver_name,approver_result,approver_time,status,comment,transfer_to_id,transfer_to_name,transfer_reason
        FROM jl_key_dm_leave_approval_record
        WHERE id = #{id}
        ORDER BY approver_time ASC
    </select>

    <insert id="insert" parameterType="map">
        INSERT INTO jl_key_dm_leave_approval_record(
            id,leave_id,node_index,node_type,approver_id,approver_name,approver_result,status,comment,transfer_to_id,transfer_to_name,transfer_reason,is_used,create_by,create_time,order_no
        ) VALUES (
            #{id},#{leave_id},#{node_index},#{node_type},#{approver_id},#{approver_name},#{approver_result},#{status},#{comment},#{transfer_to_id},#{transfer_to_name},#{transfer_reason},1,#{_user.id},NOW(),#{order_no}
        )
    </insert>

    <select id="selectApprovalHistory" parameterType="page" resultType="map">
        SELECT a.*
        FROM jl_key_dm_leave_application a
        LEFT JOIN jl_key_dm_leave_approval_record r ON a.id = r.leave_id
        LEFT JOIN jl_key_dm_user u ON r.approver_id = u.id
        LEFT JOIN sys_office so on u.office_id = so.id
        <where>
            a.is_used = 1
            AND a.status != 0
            AND so.area_id = #{params.area_id}
<!--            AND (u.office_id = #{params._user.company_id} OR so.parent_ids like CONCAT(#{params._user.parent_ids}, ',', #{params._user.company_id}, '%'))-->
            AND r.node_type = 1
            <!-- 支持按起止时间查询：可单独填写一个时间进行查询 -->
            <if test="params.params != null and params.params.start_time != null and params.params.start_time != ''">
                AND a.end_time &gt;= #{params.params.start_time}
            </if>
            <if test="params.params != null and params.params.end_time != null and params.params.end_time != ''">
                AND a.start_time &lt; #{params.params.end_time}
            </if>
            <!-- 支持按状态过滤（来自 params.params.status） -->
            <if test="params.params != null and params.params.approver_name != null and params.params.approver_name != ''">
                AND (r.transfer_to_name LIKE CONCAT('%', #{params.params.approver_name}, '%')
                OR r.approver_name LIKE CONCAT('%', #{params.params.approver_name}, '%'))
            </if>
        </where>
        ORDER BY FIELD(a.status, 1) DESC, a.submit_time DESC
    </select>

    <update id="submit" parameterType="map">
        UPDATE jl_key_dm_leave_approval_record
        SET status = 1,update_by = #{_user.id}, update_time = NOW()
        WHERE id = #{id}
    </update>

    <!-- 撤回：仅限待审批状态(status=1)撤回，设置 status=0 -->
    <update id="revoke" parameterType="map">
        UPDATE jl_key_dm_leave_approval_record
        SET status = 0, update_by = #{_user.id}, update_time = NOW()
        WHERE id = #{id} AND status = 1
    </update>

    <!-- 审批通过：设置 status=9, op_result=1, approval_complete_time -->
    <update id="approve" parameterType="map">
        UPDATE jl_key_dm_leave_approval_record
        SET status = 2,  approver_time = NOW(), update_by = #{_user.id}, update_time = NOW(),approver_result = 1
        WHERE id = #{id}
    </update>

    <!-- 驳回：设置 status=-1, op_result=-1, back_reason -->
    <update id="reject" parameterType="map">
        UPDATE jl_key_dm_leave_approval_record
        SET status = 2, approver_time = NOW(), update_by = #{_user.id}, update_time = NOW(),approver_result = -1
        WHERE id = #{id}
    </update>
    <delete id="deleteById" parameterType="map">
        UPDATE jl_key_dm_leave_approval_record set is_used=0 WHERE id = #{id}
    </delete>
    <update id="update" parameterType="map">
        UPDATE jl_key_dm_leave_approval_record
        SET
        update_by = #{_user.id},
        update_time = NOW()
        <!-- 条件判断：approver_id 非空时才更新 -->
        <if test="approver_id != null">
            ,approver_id = #{approver_id}
        </if>
        <!-- 条件判断：approver_name 非空时才更新 -->
        <if test="approver_name != null">
            ,approver_name = #{approver_name}
        </if>
        <!-- 条件判断：status 非空时才更新 -->
        <if test="status != null">
            ,status = #{status}
        </if>
        <!-- 条件判断：approver_result 非空时才更新 -->
        <if test="approver_result != null">
            ,approver_result = #{approver_result}
        </if>
        <!-- 条件判断：comment 非空时才更新 -->
        <if test="comment != null">
            ,comment = #{comment}
        </if>
        WHERE
        id = #{id}
    </update>

    <update id="transferApproval" parameterType="map">
        UPDATE jl_key_dm_leave_approval_record
        SET
        status = 2,
        approver_result = 0,
        update_by = #{_user.id},
        update_time = NOW(),
        approver_time = NOW()
        <!-- 条件判断：approver_id 非空时才更新 -->
        <if test="approver_id != null and approver_id != ''">
            ,transfer_to_id = #{approver_id}
        </if>
        <!-- 条件判断：approver_name 非空时才更新 -->
        <if test="approver_name != null and approver_name != ''">
            ,transfer_to_name = #{approver_name}
        </if>
        <!-- 条件判断：comment 非空时才更新 -->
        <if test="comment != null and comment != ''">
            ,transfer_reason = #{comment}
        </if>
        WHERE
        id = #{id}
    </update>
</mapper>


