select
sum(t2.final_price) as settlement_amount
from(
select
t.order_item_id,
t.supplier_id,
t.supplier_price,
t.supplier_price*(t.num-t.replace_num-t.lack_num )- t.deduct_amount -t.fee_supplier-t.station_fee as final_price,
t.fee_supplier,
t.station_fee
from (
SELECT
o.order_no,
oi.id as order_item_id,
oi.supplier_id,
o.payment_time,
oi.supplier_price,
oi.num as num,
IFNULL( (select oic.deduct_amount from t_order_item_check oic where oic.deleted=0 and oic.order_item_id = oi.id and oic.type = 'reduce' and oic.audit_status='AGREED'),0) as deduct_amount,
IFNULL( (select oic.num from t_order_item_check oic where oic.deleted=0 and oic.order_item_id = oi.id and oic.type = 'replace' and oic.audit_status='AGREED'),0) as replace_num,
IFNULL( (select oic.num from t_order_item_check oic where oic.deleted=0 and oic.order_item_id = oi.id and oic.type = 'lack' and oic.audit_status='AGREED'),0) as lack_num,
IFNULL( (select ois.fee_supplier from t_order_item_sales ois where ois.deleted=0 and ois.order_item_id = oi.id and ois.status='AGREED'),0) as fee_supplier,
IFNULL( (select ois.station_fee from t_order_item_settlement ois where ois.deleted=0 and ois.order_item_id = oi.id ),0) as station_fee
FROM t_order_item oi
LEFT JOIN t_order o ON oi.order_id = o.id
WHERE o.deleted=0
and o.status_backend not in ('PENDING','CANCEL','REFUND','COMPLETED','EVALUATE')
and oi.supplier_id = #{dto.supplierId}
) t
) t2
select
sum(t2.final_price) as settlement_amount
from(
select
t.order_item_id,
t.supplier_id,
t.supplier_price,
t.supplier_price*(t.num-t.replace_num-t.lack_num )- t.deduct_amount -t.fee_supplier-t.station_fee as final_price,
t.fee_supplier,
t.station_fee
from (
SELECT
o.order_no,
oi.id as order_item_id,
oi.supplier_id,
o.payment_time,
oi.supplier_price,
oi.num as num,
IFNULL( (select oic.deduct_amount from t_order_item_check oic where oic.deleted=0 and oic.order_item_id = oi.id and oic.type = 'reduce' and oic.audit_status='AGREED'),0) as deduct_amount,
IFNULL( (select oic.num from t_order_item_check oic where oic.deleted=0 and oic.order_item_id = oi.id and oic.type = 'replace' and oic.audit_status='AGREED'),0) as replace_num,
IFNULL( (select oic.num from t_order_item_check oic where oic.deleted=0 and oic.order_item_id = oi.id and oic.type = 'lack' and oic.audit_status='AGREED'),0) as lack_num,
IFNULL( (select ois.fee_supplier from t_order_item_sales ois where ois.deleted=0 and ois.order_item_id = oi.id and ois.status='AGREED'),0) as fee_supplier,
IFNULL( (select ois.station_fee from t_order_item_settlement ois where ois.deleted=0 and ois.order_item_id = oi.id ),0) as station_fee
FROM t_order_item oi
LEFT JOIN t_order o ON oi.order_id = o.id
WHERE o.deleted=0
and o.status_backend not in ('PENDING','CANCEL','REFUND','COMPLETED')
and oi.supplier_id = #{dto.supplierId}
) t
) t2