<?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.mes.mapper.production.ProductionPlanMapper">
    <select id="getPage" resultType="com.fjhx.mes.entity.production.vo.ProductionPlanVo">
        SELECT
            *
        FROM
            (
                SELECT
                    pp.id,
                    pp.`code`,
                    pp.work_order_id,
                    pp.start_date,
                    pp.stop_date,
                    pp.quantity,
                    pp.`status`,
                    pp.create_user,
                    pp.create_time,
                    pp.update_user,
                    pp.update_time,
                    pp.quantity - ifnull( sum( pt.quantity ), 0 ) AS remainingQuantity,
                    wo.`code` AS workOrderCode,
                    pi.`name` AS productName,
                    pi.`spec` AS productSpec
                FROM
                    production_plan pp
                        LEFT JOIN production_task pt ON pt.production_plan_id = pp.id
                        LEFT JOIN work_order wo ON pp.work_order_id = wo.id
                        LEFT JOIN bytesailing_item.product_info pi ON wo.product_id = pi.id
                GROUP BY
                    pp.id
            ) pp
            ${ew.customSqlSegment}
    </select>

</mapper>
