<?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.zcloud.safetyDutyList.persistence.mapper.tasklist.TaskDetailMapper">

    <select id="listPage" resultType="com.zcloud.safetyDutyList.persistence.dataobject.tasklist.TaskDetailDO">
        SELECT td.*,
        tl.task_list_name AS taskListName,
        tl.task_level AS taskLevel,
        tl.responsibility_post AS responsibilityPost,
        rating_dept.name AS ratingDepartmentName,
        rating_user.name AS ratingUserName,
        IFNULL(fb_agg.feedback_count, 0) AS feedbackCount,
        fb_agg.current_period_feedback AS currentPeriodFeedback
        FROM safety_accountability_task_detail td
        LEFT JOIN safety_accountability_task_list tl ON td.task_list_id = tl.task_list_id AND tl.delete_enum = 'FALSE'
        LEFT JOIN department rating_dept ON td.rating_department_id = rating_dept.id
        LEFT JOIN user rating_user ON td.rating_user_id = rating_user.id
        LEFT JOIN (
            SELECT task_detail_id,
            COUNT(*) AS feedback_count,
            MAX(CASE WHEN feedback_period_flag = DATE_FORMAT(NOW(), '%Y-%m') THEN 1 ELSE 0 END) AS current_period_feedback
            FROM safety_accountability_feedback
            WHERE delete_enum = 'FALSE'
            GROUP BY task_detail_id
        ) fb_agg ON td.task_detail_id = fb_agg.task_detail_id
        <where>
            td.delete_enum = 'FALSE'
            <if test="params.taskListId != null and params.taskListId != ''">
                AND td.task_list_id = #{params.taskListId}
            </if>
            <if test="params.taskContent != null and params.taskContent != ''">
                AND td.execute_content LIKE CONCAT('%', #{params.taskContent}, '%')
            </if>
            <if test="params.feedbackCycleType != null and params.feedbackCycleType != ''">
                AND td.feedback_cycle_type = #{params.feedbackCycleType}
            </if>
            <if test="params.feedbackStatus != null and params.feedbackStatus != ''">
                AND td.feedback_status >= #{params.feedbackStatus}
            </if>
            <if test="params.taskStatus != null and params.taskStatus != ''">
                AND td.task_status = #{params.taskStatus}
            </if>
            <if test="params.taskPeriodStart != null and params.taskPeriodStart != '' and params.taskPeriodEnd != null and params.taskPeriodEnd != ''">
                AND tl.period_start_time &lt;= #{params.taskPeriodEnd}
                AND tl.period_end_time &gt;= #{params.taskPeriodStart}
            </if>
        </where>
        ORDER BY td.id ASC
    </select>

    <select id="listByTaskListId" resultType="com.zcloud.safetyDutyList.persistence.dataobject.tasklist.TaskDetailDO">
        SELECT * FROM safety_accountability_task_detail
        WHERE task_list_id = #{taskListId} AND delete_enum = 'FALSE'
        ORDER BY id ASC
    </select>

    <select id="getByTaskDetailId" resultType="com.zcloud.safetyDutyList.persistence.dataobject.tasklist.TaskDetailDO">
        SELECT td.*,
        tl.task_list_name AS taskListName,
        tl.task_level AS taskLevel,
        tl.responsibility_post AS responsibilityPost,
        rating_dept.name AS ratingDepartmentName,
        rating_user.name AS ratingUserName
        FROM safety_accountability_task_detail td
        LEFT JOIN safety_accountability_task_list tl ON td.task_list_id = tl.task_list_id AND tl.delete_enum = 'FALSE'
        LEFT JOIN department rating_dept ON td.rating_department_id = rating_dept.id
        LEFT JOIN user rating_user ON td.rating_user_id = rating_user.id
        WHERE td.task_detail_id = #{taskDetailId} AND td.delete_enum = 'FALSE'
    </select>

    <delete id="deleteByTaskListId">
        DELETE FROM safety_accountability_task_detail WHERE task_list_id = #{taskListId}
    </delete>

    <select id="listInProgress" resultType="com.zcloud.safetyDutyList.persistence.dataobject.tasklist.TaskDetailDO">
        SELECT * FROM safety_accountability_task_detail
        WHERE task_status = 1 AND delete_enum = 'FALSE'
    </select>

</mapper>
