SELECT sc.id, sc.`code` AS salesContractCode, sc.customer_id, cu.`name` AS customerName, ( SELECT IFNULL( sum( cc.contract_money ), 0 ) FROM bytesailing_sale.claim_contract cc WHERE cc.contract_id = sc.id ) AS contractClaimAmount, sc.contract_amount AS contractAmount, ( SELECT IFNULL(sum( IF ( sj.op_type = 2,( sjd.price * sjd.quantity ),-(sjd.price * sjd.quantity )) ),0) FROM bytesailing_wms.stock_journal_details sjd LEFT JOIN bytesailing_wms.stock_journal sj ON sjd.stock_journal_id = sj.id JOIN bytesailing_mes.work_order wo ON sj.work_order_id = wo.id JOIN sales_contract_details scd ON wo.contract_details_id = scd.id WHERE scd.sales_contract_id = sc.id ) AS materialFee, ( SELECT IFNULL( sum( asr.amount ), 0 ) FROM after_sales_record asr JOIN bytesailing_mes.production_task_detail ptd ON asr.product_sn = ptd.product_sn JOIN bytesailing_mes.production_task pt ON ptd.production_task_id = pt.id JOIN bytesailing_mes.production_plan pp ON pt.production_plan_id = pp.id JOIN bytesailing_mes.work_order wo ON pp.work_order_id = wo.id JOIN sales_contract_details scd ON wo.contract_details_id = scd.id WHERE scd.sales_contract_id = sc.id ) AS afterSalesFee, sc.trailer_fee AS trailerFee, sc.inspection_red_pack_fee AS inspectionRedPackFee, sc.commission_fee AS commissionFee, sc.other_fee AS otherFee, sc.is_settled FROM sales_contract sc LEFT JOIN bytesailing_customer.customer cu ON sc.customer_id = cu.id ${ew.customSqlSegment}