<?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.work.WorkOrderMapper">
    <select id="getPage" resultType="com.fjhx.mes.entity.work.vo.WorkOrderVo">
        SELECT *
        FROM (SELECT wo.id,
                     wo.`code`,
                     wo.source,
                     wo.product_id,
                     wo.quantity,
                     wo.remarks,
                     wo.`status`,
                     wo.create_user,
                     wo.create_time,
                     wo.update_user,
                     wo.update_time,
                     wo.is_customized,
                     sc.`code`                                 AS contractCode,
                     sc.create_time                            AS contractCreateTime,
                     wo.production_quantity - ifnull(sum(pp.quantity), 0) AS remainingQuantity,
                     wo.bom_status,
                     wo.technology_status,
                     wo.production_quantity,
                     wo.stock_wait_quantity,
                     wo.research_status,
                     wo.research_bom_status,
                     wo.electrician_bom_status,
                     sc.dev_user_id
              FROM work_order wo
                       LEFT JOIN bytesailing_jxst.sales_contract sc ON wo.source_id = sc.id
                       LEFT JOIN production_plan pp ON pp.work_order_id = wo.id
              GROUP BY wo.id
              ) wo
            ${ew.customSqlSegment}
    </select>

    <select id="getSumCountInResourceId" resultType="com.fjhx.mes.entity.work.vo.WorkOrderVo">
        SELECT
            IFNULL(SUM(quantity),0) AS sumQuantity,
            source_id
        FROM
            work_order
        <where>
            source = 0
            <if test="dataResourceIds neq null and dataResourceIds.size() > 0">
                <foreach collection="dataResourceIds" item="dataResourceId" open="AND source_id IN (" separator="," close=")">
                    #{dataResourceId}
                </foreach>
            </if>
        </where>
        GROUP BY source_id
    </select>
    <select id="detail" resultType="com.fjhx.mes.entity.work.vo.WorkOrderVo">
        SELECT
            wo.id,
            wo.`code`,
            wo.source_id,
            wo.source,
            wo.product_id,
            wo.quantity,
            wo.remarks,
            wo.`status`,
            wo.tenant_id,
            wo.create_user,
            wo.create_time,
            wo.update_user,
            wo.update_time,
            wo.del_flag,
            wo.is_customized,
            wo.bom_status,
            wo.technology_status,
            wo.production_quantity,
            wo.stock_wait_quantity,
            wo.contract_details_id,
            wo.research_status,
            scd.product_remark AS contractProductRemark,
            sc.create_user AS contractCreateUser,
            sc.create_time AS contractCreateTime,
            sc.delivery_date AS contractDeliveryDate,
            sc.customer_id
        FROM
            work_order wo
                LEFT JOIN bytesailing_jxst.sales_contract_details scd ON wo.contract_details_id = scd.id and scd.del_flag = 0
                LEFT JOIN bytesailing_jxst.sales_contract sc ON scd.sales_contract_id = sc.id and sc.del_flag = 0
            ${ew.customSqlSegment}
    </select>
</mapper>
