import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid'; import assert from 'assert'; import { getPayClazz } from '../utils/payClazz'; import { DATA_SUBSCRIBER_KEYS } from '../config/constants'; const triggers = [ /* { entity: 'deposit', name: '当充值完成后,生成相应的accountOper', action: 'after', when: 'before', fn: async ({ operation }, context) => { // 先放到pay succeedPaying的trigger里去一并处理了 return 0; } } as UpdateTriggerInTxn */ { entity: 'deposit', name: '当受发货限制的deposit充值成功后,计算相应的account以及system account中的余额变化', action: 'succeed', when: 'before', asRoot: true, fn: async ({ operation }, context) => { const { data, filter } = operation; assert(typeof filter.id === 'string'); const deposits = await context.select('deposit', { data: { id: 1, price: 1, loss: 1, accountId: 1, shipId: 1, pay$deposit: { $entity: 'pay', data: { id: 1, paid: 1, price: 1, entity: 1, entityId: 1, iState: 1, orderId: 1, application: { systemId: 1, }, refundable: 1, wpProduct: { id: 1, type: 1, }, } } }, filter, }, {}); assert(deposits.length === 1); const [deposit] = deposits; const { pay$deposit: pays, price, loss, shipId } = deposit; let cnt = 1; let accountOpers = []; if (shipId) { assert(pays); for (const pay of pays) { const { price: payPrice, paid, application, entity, entityId, wpProduct } = pay; //受发货限制的微信支付充值 const clazz = await getPayClazz(entity, entityId, context); const [tax, sysAccountEntity, sysAccountEntityId] = clazz.calcPayTax(paid); await context.operate('sysAccountOper', { id: await generateNewIdAsync(), action: 'create', data: { id: await generateNewIdAsync(), delta: paid - tax, entity: sysAccountEntity, entityId: sysAccountEntityId, payId: pay.id, type: 'pay', } }, {}); cnt++; if (tax !== 0) { // tax产生的损失由sys account来承担 const [account] = await context.select('account', { data: { id: 1, }, filter: { entity: 'system', entityId: application.systemId, } }, { dontCollect: true }); await context.operate('accountOper', { id: await generateNewIdAsync(), action: 'create', data: { id: await generateNewIdAsync(), accountId: account.id, type: 'tax', totalPlus: -tax, availPlus: -tax, entity: 'pay', entityId: pay.id, }, }, {}); } accountOpers.push({ id: await generateNewIdAsync(), data: { id: await generateNewIdAsync(), totalPlus: payPrice - loss, availPlus: payPrice - loss, refundablePlus: pay.refundable ? price : 0, accountId: deposit.accountId, type: 'deposit', }, action: 'create', }); if (loss > 0) { // 如果有loss就充入system的account账户 const [account] = await context.select('account', { data: { id: 1, }, filter: { entity: 'system', entityId: application?.systemId, }, }, { dontCollect: true }); accountOpers.push({ id: await generateNewIdAsync(), data: { id: await generateNewIdAsync(), totalPlus: loss, availPlus: loss, accountId: account.id, type: 'earn', }, action: 'create', }); cnt++; } } } if (accountOpers && accountOpers.length > 0) { data.accountOper$entity = accountOpers; } return cnt; } }, { entity: 'deposit', name: '当deposit状态发生变化时,尝试向关联的account订阅者推送', action: ['succeed', 'fail', 'ship'], when: 'after', fn: async ({ operation }, context, option) => { const { filter, id } = operation; const { id: depositId } = filter; const [deposit] = await context.select('deposit', { data: { id: 1, accountId: 1, iState: 1, pay$deposit: { $entity: 'pay', data: { id: 1, iState: 1, } } }, filter: { id: depositId, } }, {}); const { accountId, pay$deposit } = deposit; const payId = pay$deposit?.[0].id; const selectionId = await generateNewIdAsync(); await context.select('deposit', { id: selectionId, data: { id: 1, accountId: 1, account: { id: 1, avail: 1, total: 1, }, iState: 1, shipId: 1, ship: { id: 1, iState: 1, }, pay$deposit: { $entity: 'pay', data: { id: 1, iState: 1, } } }, filter: { id: depositId, } }, {}); if (payId) { context.saveOperationToEvent(selectionId, `${DATA_SUBSCRIBER_KEYS.depositStateChanged}-${payId}`); } if (accountId) { context.saveOperationToEvent(selectionId, `${DATA_SUBSCRIBER_KEYS.depositStateChanged}-${accountId}`); } return 1; } }, ]; export default triggers;