<?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.OaProgressMapper">

    <!-- 1. 定义 OaProgressVo 的映射：同时嵌套子阶段和子任务 -->
    <resultMap id="OaProgressResult" type="com.ruoyi.oa.domain.vo.OaProgressVo">
        <id     property="progressId"   column="progress_id"/>
        <result property="projectId"    column="project_id"/>
        <result property="type"         column="type"/>
        <result property="progressName" column="progress_name"/>
        <result property="parentId"     column="parent_id"/>
        <result property="sort"         column="sort"/>
        <result property="remark"       column="remark"/>
        <result property="status"       column="status"/>
        <result property="timeRemark"   column="time_remark"/>
        <result property="contactPhone" column="contact_phone"/>

        <!-- 嵌套查询一级或二级子阶段 -->
        <collection property="children"
                    ofType="com.ruoyi.oa.domain.vo.OaProgressVo"
                    javaType="list"
                    select="queryByParentId"
                    column="progress_id"/>

        <!-- 嵌套查询本阶段下的所有任务明细 -->
        <collection property="tasks"
                    ofType="com.ruoyi.oa.domain.vo.OaProgressDetailVo"
                    javaType="list"
                    select="queryDetailsByProgressId"
                    column="progress_id"/>
    </resultMap>

    <!-- 2. 定义 OaProgressDetailVo 的映射 -->
    <resultMap id="OaProgressDetailResult" type="com.ruoyi.oa.domain.vo.OaProgressDetailVo">
        <id     property="detailId"       column="detail_id"/>
        <result property="progressId"     column="progress_id"/>
        <result property="detailName"     column="detail_name"/>
        <result property="planStartDate"  column="plan_start_date"/>
        <result property="planEndDate"    column="plan_end_date"/>
        <result property="actualStartDate" column="actual_start_date"/>
        <result property="actualEndDate"  column="actual_end_date"/>
        <result property="completePercent" column="complete_percent"/>
        <result property="planPayDate"    column="plan_pay_date"/>
        <result property="payAmount"      column="pay_amount"/>
        <result property="paidAmount"     column="paid_amount"/>
        <result property="remark"         column="remark"/>
        <result property="detailStatus"   column="detail_status"/>
    </resultMap>

    <!-- 3. 查询所有父进度（parent_id = 0）-->
    <select id="queryByProjectId"
            parameterType="java.lang.Long"
            resultMap="OaProgressResult">
        SELECT
            progress_id,
            project_id,
            type,
            progress_name,
            parent_id,
            sort,
            remark,
            status,
            time_remark,
            contact_phone
        FROM oa_progress
        WHERE project_id = #{projectId}
          AND parent_id = 0
          AND del_flag = 0
        ORDER BY sort
    </select>

    <!-- 4. 根据 parentId 查询子进度 -->
    <select id="queryByParentId"
            parameterType="java.lang.Long"
            resultMap="OaProgressResult">
        SELECT
            progress_id,
            project_id,
            type,
            progress_name,
            parent_id,
            sort,
            remark,
            status,
            time_remark,
            contact_phone
        FROM oa_progress
        WHERE parent_id = #{parentId}
          AND del_flag = 0
        ORDER BY sort
    </select>

    <!-- 5. 根据 progressId 查询对应的所有任务明细 -->
    <select id="queryDetailsByProgressId"
            parameterType="java.lang.Long"
            resultMap="OaProgressDetailResult">
        SELECT
            detail_id,
            progress_id,
            detail_name,
            plan_start_date,
            plan_end_date,
            actual_start_date,
            actual_end_date,
            complete_percent,
            plan_pay_date,
            pay_amount,
            paid_amount,
            remark,
            detail_status
        FROM oa_progress_detail
        WHERE progress_id = #{progressId}
          AND del_flag = 0
        ORDER BY plan_start_date
    </select>


    <select id="getKeyList" resultType="com.ruoyi.oa.domain.OaProgressDetail">
        select distinct detail_name, department
        from oa_progress_detail
    </select>


    <select id="selectVoPagePlus" resultType="com.ruoyi.oa.domain.vo.OaProgressVo">
        SELECT op.progress_id,
               op.project_id,
               op.contact_phone,
               op.type,
               op.progress_name,
               op.parent_id,
               op.del_flag,
               op.sort,
               op.remark,
               op.status,
               op.amount,
               op.time_remark,
               sop.project_name,
               sop.project_num,
               sop.funds,
               -- 获取 detail_status = 0 且 plan_start_date 最小的记录的 detail_name 作为 nowLevel
               (SELECT d.detail_name
                FROM oa_progress_detail d
                WHERE d.progress_id = op.progress_id
                  AND d.detail_status = 0
                ORDER BY d.plan_start_date ASC
                LIMIT 1)                              AS nowLevel,
               (select op2.contact_phone
                from oa_progress op2
                where op2.progress_id = op.parent_id) as parentPhone,
               (SELECT d.detail_id
                FROM oa_progress_detail d
                WHERE d.progress_id = op.progress_id
                  AND d.detail_status = 0
                ORDER BY d.plan_start_date ASC
                LIMIT 1)                              AS nowLevelId,
               -- 计算该记录 plan_end_date 与当前时间的天数差值作为 remainTime
               (SELECT TIMESTAMPDIFF(DAY, NOW(), d.plan_end_date)
                FROM oa_progress_detail d
                WHERE d.progress_id = op.progress_id
                  AND d.detail_status = 0
                ORDER BY d.plan_start_date ASC
                LIMIT 1)                              AS remainTime,
               (SELECT d.department
                FROM oa_progress_detail d
                WHERE d.progress_id = op.progress_id
                  AND d.detail_status = 0
                ORDER BY d.plan_start_date ASC
                LIMIT 1)                              AS nowDepart,
               (SELECT d.plan_end_date
                FROM oa_progress_detail d
                WHERE d.progress_id = op.progress_id
                  AND d.detail_status = 0
                ORDER BY d.plan_start_date ASC
                LIMIT 1)                              AS endTime,

               (SELECT d.plan_pay_date
                FROM oa_progress_detail d
                WHERE d.progress_id = op.progress_id
                ORDER BY d.plan_pay_date ASC
                LIMIT 1)                              AS payEndTime,
               -- 计算当前 progress 对应的 detail 完成比例：满足 detail_status = 1 的条数 / 总条数（避免除0）
               (SELECT IF(COUNT(*) = 0, 0, ROUND(SUM(CASE WHEN d.detail_status = 1 THEN 1 ELSE 0 END) / COUNT(*), 2))
                FROM oa_progress_detail d
                WHERE d.progress_id = op.progress_id) AS detailCompletePercent
        FROM oa_progress op
                 LEFT JOIN sys_oa_project sop ON sop.project_id = op.project_id
            ${ew.getCustomSqlSegment}
    </select>




</mapper>
