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

    <resultMap type="com.ruoyi.oa.domain.OaProjectSchedule" id="OaProjectScheduleResult">
        <result property="scheduleId" column="schedule_id"/>
        <result property="projectId" column="project_id"/>
        <result property="templateId" column="template_id"/>
        <result property="currentStep" column="current_step"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="status" column="status"/>
        <result property="steward" column="steward"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="remark" column="remark"/>
    </resultMap>


    <select id="selectVoPagePlus" resultType="com.ruoyi.oa.domain.vo.OaProjectScheduleVo">
        SELECT ops.schedule_id,
               ops.project_id,
               ops.template_id,
               ops.current_step,
               opss.step_name                           AS currentStepName,
               ops.start_time,
               ops.end_time,
               ops.status,
               ops.steward,
               ops.remark,

            /* ======================== 项目信息 ==================== */
               op.project_id                            AS opProjectId,
               op.project_name,
               op.project_num,
               op.project_type,
               op.address,
               op.funds,
               op.functionary,
               op.begin_time,
               op.finish_time,
               op.introduction,
               op.project_grade,
               op.project_status,
               op.trade_type,
               op.project_code,
               op.pre_pay,
               op.is_top AS isTop,
               opss.plan_end,
               opss.header,
               DATEDIFF(DATE(opss.plan_end), CURDATE())  AS remainTime,
               ROUND(
                       (
                           (ops.current_step - CASE WHEN opss.status = 0 THEN 1 ELSE 0 END)
                               / NULLIF(maxs.max_order, 0)
                           ) * 100,
                       2
               )                                        AS schedulePercentage

        FROM oa_project_schedule AS ops
                 LEFT JOIN sys_oa_project AS op ON ops.project_id = op.project_id
                 LEFT JOIN oa_project_schedule_step AS opss
                           ON ops.schedule_id = opss.schedule_id
                               AND opss.step_order = ops.current_step
                               AND opss.use_flag = '1'
                 LEFT JOIN (SELECT schedule_id,
                                   MAX(step_order) AS max_order
                            FROM oa_project_schedule_step
                            WHERE use_flag = '1'
                            GROUP BY schedule_id) AS maxs ON maxs.schedule_id = ops.schedule_id
            ${ew.getCustomSqlSegment}
    </select>


</mapper>
