po.id,
po.production_plan_ids,
po.product_model_id,
po.nps_no,
po.create_time,
po.update_time,
po.technology_routing_id,
po.quantity,
po.complete_quantity,
po.start_time,
po.end_time,
po.create_user,
po.dept_id,
po.plan_complete_time,
po.status,
po_sales.salesContractNo,
po_sales.customerName,
p.product_name as productName,
pm.model as model,
po.is_end_order as endOrder,
tr.process_route_code as processRouteCode,
ROUND(po.complete_quantity / po.quantity * 100, 2) AS completionStatus,
tb.bom_no as bomNo,
pop_return.returned as returned
from production_order po
left join (
select po2.id as orderId,
group_concat(distinct sl2.sales_contract_no order by sl2.sales_contract_no separator ',') as salesContractNo,
group_concat(distinct sl2.customer_name order by sl2.customer_name separator ',') as customerName,
group_concat(distinct sl2.project_name order by sl2.project_name separator ',') as projectName,
min(sl2.delivery_date) as deliveryDate
from production_order po2
left join production_plan pp2
on find_in_set(pp2.id, replace(replace(replace(po2.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
left join sales_ledger sl2 on sl2.id = pp2.sales_ledger_id
group by po2.id
) po_sales on po_sales.orderId = po.id
left join product_model pm on po.product_model_id = pm.id
left join product p on pm.product_id = p.id
left join technology_routing tr on po.technology_routing_id = tr.id
left join technology_bom tb on tb.id = tr.bom_id
left join (
select production_order_id as productionOrderId,
if(max(case when ifnull(is_returned, 0) = 1 then 1 else 0 end) = 1, true, false) as returned
from production_order_pick
group by production_order_id
) pop_return on pop_return.productionOrderId = po.id
and po.id = #{c.id}
and p.product_name like concat('%', #{c.productName}, '%')
and pm.model like concat('%', #{c.model}, '%')
and po.product_model_id = #{c.productModelId}
and po.technology_routing_id = #{c.technologyRoutingId}
and po.status = #{c.status}
and po.create_user = #{c.createUser}
and po.dept_id = #{c.deptId}
and po.nps_no like concat('%', #{c.npsNo}, '%')
and po.production_plan_ids like concat('%', #{c.productionPlanIds}, '%')
and po.plan_complete_time = #{c.planCompleteTime}
and po.start_time >= #{c.startTime}
and po.end_time <= #{c.endTime}