<?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.fjhx.jxst.mapper.sales.SalesContractDetailsMapper">
    <select id="getProductionSchedule" resultType="com.fjhx.jxst.entity.sales.vo.SalesContractDetailsVo">
        SELECT
            scd.id,
            scd.sales_contract_id,
            scd.product_id,
            wo.production_quantity AS productionQuantity,
            (
                SELECT
                    count( ptd.id )
                FROM
                    bytesailing_mes.production_plan pp
                        LEFT JOIN bytesailing_mes.production_task pt ON pt.production_plan_id = pp.id
                        AND pt.del_flag = 0
                        LEFT JOIN bytesailing_mes.production_task_detail ptd ON ptd.production_task_id = pt.id
                        AND ptd.del_flag = 0
                WHERE
                    pp.del_flag = 0
                  AND ptd.finish_status = 1
                  AND pp.work_order_id = wo.id
            ) AS completedQuantity
        FROM
            sales_contract_details scd
                LEFT JOIN bytesailing_mes.work_order wo ON wo.contract_details_id = scd.id
        ${ew.customSqlSegment}
    </select>
</mapper>
