<?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.ruoyi.oa.mapper.SysOaTaskMapper">

    <resultMap type="com.ruoyi.oa.domain.vo.SysOaTaskVo" id="SysOaTaskResult">
        <result property="taskId" column="task_id"/>
        <result property="projectId" column="project_id"/>
        <result property="taskTitle" column="task_title"/>
        <result property="taskType" column="task_type"/>
        <result property="taskGrade" column="task_grade"/>
        <result property="collaborator" column="collaborator"/>
        <result property="beginTime" column="begin_time"/>
        <result property="finishTime" column="finish_time"/>
        <result property="content" column="content"/>
        <result property="remark" column="remark"/>
        <result property="state" column="state"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="completedTime" column="completed_time"/>
        <result property="createUserNickName" column="createUserNickName"/>
        <result property="workerNickName" column="workerNickName"/>
        <result property="postponements" column="postponements"/>
        <result property="accessory" column="accessory"/>

        <collection property="taskItemVoList" ofType="com.ruoyi.oa.domain.vo.SysOaTaskItemVo" javaType="list">
            <result property="itemId" column="item_id"/>
            <result property="content" column="itemContent"/>
            <result property="signTime" column="sign_time"/>
            <result property="status" column="itemStatus"/>
            <result property="beginTime" column="itemBeginTime"/>
            <result property="endTime" column="itemEndTime"/>
            <result property="remark" column="itemRemark"/>
            <result property="completedTime" column="itemCompletedTime"/>
            <result property="files" column="files"/>


        </collection>
    </resultMap>



    <!--先删除数据-->
    <delete id="deleteSysOaTaskByProjectId" parameterType="Long">
        delete from sys_oa_task where project_id = #{projectId}
    </delete>


    <select id="selectListVoPage" resultType="com.ruoyi.oa.domain.vo.SysOaTaskVo">
        SELECT
            sot.task_id,
            sot.project_id,
            sot.create_user_id,
            sot.worker_id,
            sot.task_title,
            sot.task_type,
            sot.task_grade,
            sot.collaborator,
            COALESCE(sot.finish_time, soti.end_time) AS finish_time,
            COALESCE(sot.temp_time, soti.temp_time)   AS temp_time,
            sot.begin_time,
            sot.origin_finish_time,
            sot.postponements,
            sot.completed_time,
            CASE
                WHEN sot.completed_time IS NULL
                    THEN DATEDIFF(
                        NOW(),
                        COALESCE(
                                sot.finish_time,
                                (SELECT a.end_time
                                 FROM sys_oa_task_item a
                                 WHERE a.task_id = sot.task_id
                                   AND a.completed_time IS NULL
                                 LIMIT 1)
                        )
                         )
                ELSE 0
                END                                           AS overDays,
            sot.rank_number,
            sot.remark,
            sot.task_rank,
            sot.state,
            sot.time_gap,
            sot.status,
            sot.create_by,
            sot.create_time,
            sot.update_by,
            sot.update_time,
            sot.del_flag,
            sot.own_rank,
            sop.project_name,
            soti.item_id,
            sop.project_num,
            su1.nick_name AS createUserNickName,
            su2.nick_name AS workerNickName
        FROM sys_oa_task sot
                 LEFT JOIN sys_user su1 ON su1.user_id = sot.create_user_id
                 LEFT JOIN sys_user su2 ON su2.user_id = sot.worker_id
                 LEFT JOIN sys_oa_project sop ON sop.project_id = sot.project_id
                 LEFT JOIN sys_oa_task_item soti
                           ON soti.task_id = sot.task_id
                               AND soti.completed_time IS NULL

            ${ew.getCustomSqlSegment}

    </select>


    <select id="selectDetailVoById" resultMap="SysOaTaskResult">
        SELECT sot.task_id,
               sot.project_id,
               sot.create_user_id,
               sot.worker_id,
               sot.task_title,
               sot.task_type,
               sot.task_grade,
                sot.accessory,
               COALESCE(
                       sot.finish_time,
                       (SELECT a.end_time
                        FROM sys_oa_task_item a
                        WHERE a.task_id = sot.task_id
                          AND a.completed_time IS NULL
                        LIMIT 1)
               )                                                 AS finish_time,
               COALESCE(
                       sot.temp_time,
                       (SELECT a.temp_time
                        FROM sys_oa_task_item a
                        WHERE a.task_id = sot.task_id
                          AND a.completed_time IS NULL
                        LIMIT 1)
               )                                                 AS temp_time,

               sot.begin_time,
               sot.origin_finish_time,
               sot.postponements,
               sot.completed_time,
               sot.content,
               su1.nick_name                                     AS createUserNickName,
               su2.nick_name                                     AS workerNickName,


               sot.rank_number,
               sot.remark,
               sot.task_rank,
               sot.state,
               sot.time_gap,
               sot.status,
               sot.create_by,
               sot.create_time,
               sot.update_by,
               sot.update_time,
               sot.del_flag,
               sot.own_rank,
               sop.project_name,
               sop.project_num,

               soti.item_id,
               soti.content                                      AS itemContent,
               soti.sign_time,
               soti.status                                       AS itemStatus,
               soti.begin_time                                   AS itemBeginTime,
               soti.completed_time                               AS itemCompletedTime,
               soti.end_time                                     AS itemEndTime,
               soti.remark                                       AS itemRemark,
               CASE
                   WHEN sot.completed_time IS NULL
                       THEN DATEDIFF(
                           NOW(),
                           COALESCE(
                                   sot.finish_time,
                                   (SELECT a.end_time
                                    FROM sys_oa_task_item a
                                    WHERE a.task_id = sot.task_id
                                      AND a.completed_time IS NULL
                                    LIMIT 1)
                           )
                            )
                   ELSE 0
                   END                                           AS overDays,
               soti.files
        FROM sys_oa_task sot
            LEFT JOIN sys_user su1
        ON su1.user_id = sot.create_user_id
            LEFT JOIN sys_user su2 ON su2.user_id = sot.worker_id
            LEFT JOIN sys_oa_project sop ON sop.project_id = sot.project_id
            LEFT JOIN sys_oa_task_item soti ON soti.task_id = sot.task_id
            LEFT JOIN sys_oss so2 ON FIND_IN_SET(so2.oss_id, soti.files)
        WHERE
            sot.task_id = #{taskId}
          AND sot.del_flag = '0'

    </select>

    <select id="getMonthlyData"
            parameterType="java.lang.String"
            resultType="com.ruoyi.oa.domain.UserMonthlyData">

        SELECT
            -- 员工昵称
            su.nick_name AS nickName,

            -- 拼接多个项目名称
            GROUP_CONCAT(DISTINCT sop.project_name SEPARATOR ',') AS projectName,

            -- 计算 totalTasks
            (
                -- 1) status=0  属于当月 => 加1
                SUM(
                        CASE
                            WHEN sot.status = 0
                                AND DATE_FORMAT(sot.begin_time, '%Y-%m') = #{month}
                                THEN 1
                            ELSE 0
                            END
                )
                    +
                    -- 2) status=1 => 统计符合当月的子记录数量
                COUNT(
                        CASE
                            WHEN sot.status = 1
                                AND DATE_FORMAT(soti.begin_time, '%Y-%m') = #{month}
                                THEN soti.item_id
                            END
                )
                ) AS totalTasks,

            -- 计算 completedTasks
            (
                -- status=0  当月  completed_time非空 => +1
                SUM(
                        CASE
                            WHEN sot.status = 0
                                AND DATE_FORMAT(sot.begin_time, '%Y-%m') = #{month}
                                AND sot.completed_time IS NOT NULL
                                THEN 1
                            ELSE 0
                            END
                )
                    +
                    -- status=1 => 统计子记录当月 且 sign_time非空
                COUNT(
                        CASE
                            WHEN sot.status = 1
                                AND DATE_FORMAT(soti.begin_time, '%Y-%m') = #{month}
                                AND soti.sign_time IS NOT NULL
                                THEN soti.item_id
                            END
                )
                ) AS completedTasks,

            -- 计算 delayedTimes
            (
                -- status=0  当月 => 累加 sot.postponements
                SUM(
                        CASE
                            WHEN sot.status = 0
                                AND DATE_FORMAT(sot.begin_time, '%Y-%m') = #{month}
                                THEN sot.postponements
                            ELSE 0
                            END
                )
                    +
                    -- status=1 当月(子记录) => 同样加上 sot.postponements
                SUM(
                        CASE
                            WHEN sot.status = 1
                                AND DATE_FORMAT(soti.begin_time, '%Y-%m') = #{month}
                                THEN sot.postponements
                            ELSE 0
                            END
                )
                ) AS delayedTimes

        FROM sys_oa_task sot
                 LEFT JOIN sys_oa_task_item soti
                           ON soti.task_id = sot.task_id
                 LEFT JOIN sys_user su
                           ON su.user_id = sot.worker_id
                 LEFT JOIN sys_oa_project sop
                           ON sop.project_id = sot.project_id

        -- 只按用户分组 => 每个nickName合并为一行
        GROUP BY su.user_id
    </select>


    <select id="queryPageListDocument" resultType="com.ruoyi.oa.domain.vo.SysOaTaskVo">
        SELECT
            sot.task_id,
            sot.project_id,
            sot.create_user_id,
            sot.worker_id,
            sot.task_title,
            sot.task_type,
            sot.task_grade,
            sot.collaborator,
            COALESCE(sot.finish_time, soti.end_time) AS finish_time,
            COALESCE(sot.temp_time, soti.temp_time)   AS temp_time,
            sot.begin_time,
            sot.origin_finish_time,
            sot.postponements,
            sot.completed_time,
            sot.content,
            sot.accessory,
            sot.rank_number,
            sot.remark,
            sot.task_rank,
            sot.state,
            sot.time_gap,
            sot.status,
            sot.create_by,
            sot.create_time,
            sot.update_by,
            sot.update_time,
            sot.del_flag,
            sot.own_rank,
            sop.project_name,
            soti.item_id,
            sop.project_num,
            su1.nick_name AS createUserNickName,
            su2.nick_name AS workerNickName,
            CASE
                WHEN sot.completed_time IS NULL
                    THEN DATEDIFF(NOW(), COALESCE(sot.finish_time, soti.end_time))
                ELSE 0
                END AS overDays,
            soti.files

        FROM sys_oa_task_user sotu
                 left join sys_oa_task sot on sot.task_id = sotu.task_id
                 LEFT JOIN sys_user su1 ON su1.user_id = sot.create_user_id
                 LEFT JOIN sys_user su2 ON su2.user_id = sot.worker_id
                 LEFT JOIN sys_oa_project sop ON sop.project_id = sot.project_id

                 LEFT JOIN sys_oa_task_item soti
                           ON soti.task_id = sot.task_id
                               AND soti.completed_time IS NULL

            ${ew.getCustomSqlSegment}


    </select>
    <select id="listDocumentProject" resultType="com.ruoyi.oa.domain.vo.SysOaTaskVo">

        SELECT
            sot.create_user_id,
            sot.worker_id,

            sop.accessory,
            su1.nick_name AS createUserNickName,
            su2.nick_name AS workerNickName,
            soti.files
        FROM sys_oa_task sot
                 LEFT JOIN sys_user su1 ON su1.user_id = sot.create_user_id
                 LEFT JOIN sys_user su2 ON su2.user_id = sot.worker_id
                 LEFT JOIN sys_oa_project sop ON sop.project_id = sot.project_id
                 LEFT JOIN sys_oa_task_item soti ON soti.task_id = sot.task_id
        WHERE
            sot.project_id = #{projectId}
          AND sot.del_flag = '0'

    </select>


</mapper>
