<?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.keyPersonRatingScheduledTaskDao" name="人员评分定时任务">

    <select id="selectRatingHistoryList" parameterType="com.scpyun.platform.jilinsscgsdp.bean.entity.KeyPersonRatingHistory" resultType="com.scpyun.platform.jilinsscgsdp.bean.entity.KeyPersonRatingHistory">
        SELECT
            history.id as id,
            history.rating_dict_type as ratingDictType,
            history.key_person_id as keyPersonId,
            history.plan_start_data as planStartData,
            history.plan_end_data as planEndData,
            history.overdue_date as overdueDate,
            history.is_history as isHistory,
            history.do_status as doStatus,
            history.is_overdue as isOverdue,
            history.batch_id as batchId,
            history.create_by as createBy,
            history.create_time as createTime,
            history.update_by as updateBy,
            history.update_time as updateTime
        FROM
        jl_key_person_rating_history history
        <where> 1=1
            <if test="batchId != null and batchId != ''">
                and (history.batch_id IS NULL OR history.batch_id != #{batchId})
            </if>
            <if test="isHistory != null and isHistory != ''">
                and is_history = #{isHistory}
            </if>
        </where>
        ORDER BY history.create_time DESC
    </select>

    <!-- 待办→超期未办，例如：当前时间2025-10-11 大于 2025-10-10 -->
    <update id="updateToOverdue" parameterType="map">
        UPDATE jl_key_person_rating_history
        SET do_status = '2'
        WHERE id = #{id}
        AND do_status = '0'
        AND DATE(NOW()) &gt; plan_end_data
    </update>

    <!-- 待办→标记为临期 -->
    <update id="updateToDue" parameterType="map">
        UPDATE jl_key_person_rating_history
        SET is_overdue = '1'
        WHERE id = #{id}
        AND do_status = '0'
        AND DATE(NOW()) &gt; overdue_date AND DATE(NOW()) &lt;= plan_end_data
    </update>

    <!-- 已办→is_history=0 -->
    <update id="updateDoneToHistory" parameterType="map">
        UPDATE jl_key_person_rating_history
        SET is_history = '0'
        WHERE id = #{id}
    </update>

    <!-- 新增任务 -->
    <insert id="insert" parameterType="map">
        INSERT INTO jl_key_person_rating_history (
            id,
            rating_dict_type,
            key_person_id,
            plan_start_data,
            plan_end_data,
            overdue_date,
            is_history,
            do_status,
            is_overdue,
            batch_id,
            create_by,
            create_time
        ) VALUES (
            #{id},
            #{rating_dict_type},
            #{key_person_id},
            #{plan_start_data},
            #{plan_end_data},
            #{overdue_date},
            #{is_history},
            #{do_status},
            #{is_overdue},
            #{batch_id},
            '1',
            NOW()
        )
    </insert>

</mapper>
