SELECT * FROM (SELECT t1.*, IFNULL(t1.amount, 0) * IFNULL(t1.rate, 0) AS amountCNY, CASE WHEN (IFNULL(t1.amount, 0) * IFNULL(t1.rate, 0) - t1.sumClaimMoney) <= 0 THEN 20 WHEN t1.sumClaimMoney = 0 THEN 0 ELSE 10 END AS refundStatusNew, (t1.amount - t1.settledAmount) as unsettledAmount, (t1.sumClaimMoney - t1.invoiceAmount) as unInvoiceAmount, (CASE WHEN t1.sumClaimMoney < t1.outboundAmount THEN 1 ELSE 0 END) AS isSpecialOutbound FROM (SELECT t1.*, c.name customerName, (SELECT IFNULL(SUM(cc.contract_money), 0) FROM claim_contract cc JOIN claim cl ON cc.claim_id = cl.id WHERE cl.confirm_status = 1 and cc.contract_id = t1.id) AS sumClaimMoney, po.produce_status, IFNULL(t3.quantity, 0) AS waitStockQuantity, IFNULL(t3.receipt_quantity, 0) AS receiptStockQuantity, IFNULL( (SELECT sum(pod.finish_quantity * cp.price) FROM contract_product cp JOIN production_order_detail pod ON pod.contract_detail_id = cp.id WHERE cp.contract_id = t1.id) , 0) AS settledAmount, po.delivery_period as prodDeliveryPeriod, IFNULL(t4.deliverQuantity, 0) as deliverQuantity, IFNULL(t4.notDeliverQuantity, 0) as notDeliverQuantity, (SELECT IFNULL(sum(iacd.amount), 0) FROM invoice_apply_contract_detail iacd JOIN invoice_apply ia ON iacd.invoice_apply_id = ia.id WHERE ia.`status` IN (10, 30, 60) AND iacd.contract_id = c.id) AS invoiceAmount, IFNULL(cor_1.outbound_amount, 0) AS outboundAmount FROM contract t1 left join customer c on t1.buy_corporation_id = c.id LEFT JOIN production_order po ON po.contract_id = t1.id LEFT JOIN (SELECT pod.contract_id, IFNULL(sum(swd.quantity), 0) AS quantity, IFNULL(sum(swd.receipt_quantity), 0) AS receipt_quantity FROM production_order_detail pod JOIN stock_wait_details swd ON swd.prod_task_id = pod.id GROUP BY pod.contract_id) t3 ON t3.contract_id = t1.id LEFT JOIN (SELECT cp.contract_id, IFNULL(sum(cp.quantity), 0) AS quantity, IFNULL(t2.deliverQuantity, 0) AS deliverQuantity, IFNULL(sum(cp.quantity), 0) - IFNULL(t2.deliverQuantity, 0) AS notDeliverQuantity FROM contract_product cp LEFT JOIN (SELECT cor.contract_id, IFNULL(sum(cor.quantity), 0) AS deliverQuantity FROM contract_outbound_records cor JOIN contract_outbound_info coi ON cor.record_id = coi.id WHERE coi.`status` IN (30, 60) GROUP BY cor.contract_id) t2 ON cp.contract_id = t2.contract_id GROUP BY cp.contract_id) t4 ON t4.contract_id = t1.id left join (SELECT cor.contract_id, sum(cor.price * cor.truck_quantity) AS outbound_amount FROM contract_outbound_records cor GROUP BY cor.contract_id) cor_1 on cor_1.contract_id = t1.id) t1 GROUP BY t1.id) t1 update contract id = #{id}, status = #{status}, is_change = #{isChange}, is_show = #{isShow}, old_contract_id = #{oldContractId}, approved_date = DATE_FORMAT(NOW(),'%Y-%m-%d %H:%m:%s'), update_time = sysdate() where id = #{upId} --生产订单 DELETE FROM production_order WHERE contract_id = #{contractId}; --生产任务 DELETE FROM production_order_detail WHERE contract_id = #{contractId}; --备料 DELETE FROM material_preparation WHERE contract_id = #{contractId}; UPDATE production_order SET confirm_status = #{confirmStatus} WHERE id = #{id} UPDATE production_order SET produce_status = #{produceStatus} WHERE contract_id = #{contractId} UPDATE production_order_detail SET produce_status = #{produceStatus} WHERE contract_id = #{contractId}