import { pipeline } from 'oak-domain/lib/utils/executor'; import { assert } from 'oak-domain/lib/utils/assert'; const checkers = [ { /** * 1、settlement.price总和等于settlePlan.price * 2、settlePlan.price <= order.paid - order.refunded - order.settlePlanned */ entity: 'settlePlan', action: 'create', type: 'logicalData', priority: 1, checker: (operation, context) => { const { data } = operation; const checkfn = (settlePlan) => { return pipeline(() => context.select('order', { data: { id: 1, paid: 1, refunded: 1, settlePlanned: 1, }, filter: { id: settlePlan.orderId, }, }, { dontCollect: true }), (orders) => { const [order] = orders; const { paid, refunded, settlePlanned } = order; const orderPrice = paid - refunded - settlePlanned; assert(settlePlan.price <= orderPrice, '结算计划金额不大于订单可结算金额'); return context.select('settlement', { data: { id: 1, price: 1, iState: 1, planId: 1, }, filter: { planId: settlePlan.id, iState: 'unsettled', }, }, { dontCollect: true }); }, (settlements) => { let amount = 0; settlements.forEach((settlement) => amount += settlement.price); if (settlePlan.settlement$plan && settlePlan.settlement$plan.length > 0) { for (const settlementOper of settlePlan.settlement$plan) { const settlementData = settlementOper.data; if (settlementData instanceof Array) { settlementData.forEach((s) => amount += s.price ?? 0); } else { amount += settlementData?.price ?? 0; } } } assert(amount === settlePlan.price, '结算计划金额需等于关联的结算明细金额总和'); }); }; if (data instanceof Array) { return pipeline(...data.map(checkfn)); } else { return checkfn(data); } } }, { entity: 'settlePlan', action: 'close', type: 'logicalData', checker: (operation, context) => { const { filter } = operation; return pipeline(() => context.select('settlePlan', { data: { id: 1, price: 1, iState: 1, settlement$plan: { $entity: 'settlement', data: { id: 1, price: 1, iState: 1, }, filter: { iState: 'unsettled', } } }, filter, }, { dontCollect: true }), (settlePlans) => { for (const settlePlan of settlePlans) { const { price: planPrice, settlement$plan: settlements } = settlePlan; let amount = 0; settlements.forEach((settlement) => amount += settlement.price); assert(amount === planPrice, '结算计划金额需等于未结算的结算明细金额总和'); } }); } } ]; export default checkers;