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

    <select id="getPage" resultType="com.fjhx.mes.entity.production.vo.ProductionSchedulingVo">
        SELECT ps.id,
               ps.quantity,
               ps.scheduling_date,
               ps.processes_id,
               pod.id                                     AS taskId,
               pod.quantity                               AS taskQuantity,
               pod.product_id,
               po.id                                      AS orderId,
               po.`code`                                  AS orderCode,
               pp.`name`                                  AS processesName,
               (select SUM(quantity)
                from production_scheduling ps1
                where ps1.task_id = ps.task_id
                  and ps1.processes_id = ps.processes_id) AS sumQuantity
        FROM production_scheduling ps
                 LEFT JOIN production_order_detail pod ON ps.task_id = pod.id
                 LEFT JOIN production_order po ON pod.produce_order_id = po.id
                 LEFT JOIN production_processes pp ON ps.processes_id = pp.id
            ${ew.customSqlSegment}
    </select>

    <select id="listSumQuantity" resultType="com.fjhx.mes.entity.production.vo.ProductionSchedulingVo">
        SELECT ps.scheduling_date,
               DATE_FORMAT(ps.scheduling_date, '%Y-%m-%d') AS schedulingDateStr,
               sum(ps.quantity)                            AS quantity,
               ps.processes_id,
               pp.`name`                                   AS processesName,
               pp.capacity
        FROM production_scheduling ps
                 JOIN production_processes pp ON ps.processes_id = pp.id
            ${ew.customSqlSegment}
    </select>
    <select id="schedulingTaskList" resultType="com.fjhx.mes.entity.production.vo.ProductionOrderDetailVo">
        SELECT pod.*,
               po.`code` AS orderCode
        FROM production_order_detail pod
                 LEFT JOIN production_order po ON pod.produce_order_id = po.id
                 JOIN production_scheduling ps ON ps.task_id = pod.id
            ${ew.customSqlSegment}
    </select>

</mapper>
