SELECT
o.`store_id` AS `storeId`,
o.`customer_id` AS `customerId`,
c.`name` AS `customerName`,
o.`id` AS `orderId`,
o.`order_no` AS `orderNo`,
o.`status` AS `orderStatus`,
o.`payment_status` AS `paymentStatus`,
o.`sale_amount` AS `saleAmount`,
o.`pending_paid_amount` AS `pendingPaidAmount`,
o.`paid_amount` AS `paidAmount`,
GREATEST(o.`sale_amount` - o.`paid_amount`, 0) AS `unpaidAmount`,
COALESCE(fr.`pending_receive_count`, 0) AS `pendingReceiveCount`,
COALESCE(fr.`pending_receive_amount`, 0) AS `pendingReceiveAmount`,
COALESCE(ff.`pending_fulfillment_count`, 0) AS `pendingFulfillmentCount`,
COALESCE(ff.`pending_fulfillment_item_count`, 0) AS `pendingFulfillmentItemCount`
INNER JOIN `gao_customer` c ON c.`id` = o.`customer_id` AND c.`deleted_at` IS NULL
LEFT JOIN (
SELECT
f.`store_id`,
f.`biz_id`,
COUNT(1) AS `pending_receive_count`,
COALESCE(SUM(f.`amount`), 0) AS `pending_receive_amount`
FROM `gao_fund_record` f
WHERE f.`deleted_at` IS NULL
AND f.`biz_type` = 'SALES_ORDER'
AND f.`operation_type` = 'RECEIVE'
AND f.`status` = 'PENDING'
GROUP BY f.`store_id`, f.`biz_id`
) fr ON fr.`store_id` = o.`store_id` AND fr.`biz_id` = o.`id`
LEFT JOIN (
SELECT
f.`store_id`,
f.`sales_order_id`,
COUNT(DISTINCT f.`id`) AS `pending_fulfillment_count`,
COUNT(fi.`id`) AS `pending_fulfillment_item_count`
FROM `gao_order_fulfillment` f
LEFT JOIN `gao_order_fulfillment_item` fi ON fi.`fulfillment_id` = f.`id`
WHERE f.`status` = 'PENDING'
GROUP BY f.`store_id`, f.`sales_order_id`
) ff ON ff.`store_id` = o.`store_id` AND ff.`sales_order_id` = o.`id`