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

    <select id="selectVoPagePlus" resultType="com.ruoyi.oa.domain.vo.OaPaymentProgressVo">
        SELECT
            opp.*,
            sop.project_name,
            sop.project_code,
            sop.project_num,
            mf.min_end_time       AS earliestEndTime,
            -- qualified = 1 当不存在 status=0 的 sof（即要么没有任何 sof，要么所有 sof.status=1）
            CASE
                WHEN NOT EXISTS (
                    SELECT 1
                    FROM sys_oa_finance sof_zero
                    WHERE sof_zero.payment_progress_id = opp.payment_progress_id
                      AND sof_zero.status = 0
                )
                    THEN 1
                ELSE 0
                END AS qualified
        FROM oa_payment_progress opp
                 LEFT JOIN sys_oa_project sop
                           ON opp.project_id = sop.project_id
                 LEFT JOIN (
            -- 只拿 status=0 且最早的 end_time
            SELECT
                payment_progress_id,
                MIN(end_time) AS min_end_time
            FROM sys_oa_finance
            WHERE status = 0
            GROUP BY payment_progress_id
        ) mf
                           ON opp.payment_progress_id = mf.payment_progress_id
            ${ew.getCustomSqlSegment}

    </select>
</mapper>