From 966118efee9f8beb6c4710feab4b69a814661e7b Mon Sep 17 00:00:00 2001 From: lxy Date: Wed, 19 Mar 2025 17:12:13 +0800 Subject: [PATCH] =?UTF-8?q?refreshtShipState=E4=BF=AE=E6=94=B9=EF=BC=8C?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=B0=8F=E7=A8=8B=E5=BA=8F=E5=8F=91=E8=B4=A7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=BD=95=E5=85=A5=E6=8E=A5=E5=8F=A3trigger?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/timers/ship.ts | 4 +- src/triggers/ship.ts | 48 +++++++++++++++++--- src/utils/ship.ts | 103 +++++++++++++++++++++++++------------------ src/watchers/ship.ts | 3 +- 4 files changed, 106 insertions(+), 52 deletions(-) diff --git a/src/timers/ship.ts b/src/timers/ship.ts index 19f8f6cd..f658a368 100644 --- a/src/timers/ship.ts +++ b/src/timers/ship.ts @@ -13,7 +13,7 @@ const timers: Array> = [ filter: { type: 'express', iState: { - $nin: ['received', 'cancelled', 'rejected'], + $nin: ['received', 'cancelled', 'rejected', 'receiving'], }, entity: { $exists: true @@ -33,7 +33,7 @@ const timers: Array> = [ fn: async (context, data) => { const results = [] as OperationResult[]; for (const ship of data) { - const result = await refreshtShipState(ship as EntityDict['ship']['OpSchema'], context); + const result = await refreshtShipState(ship.id!, context); if (result) { results.push(result); } diff --git a/src/triggers/ship.ts b/src/triggers/ship.ts index 08873b4b..0623ec00 100644 --- a/src/triggers/ship.ts +++ b/src/triggers/ship.ts @@ -94,6 +94,22 @@ const triggers: Trigger[] = [ data: { id: 1, orderId: 1, + order: { + id: 1, + pay$order: { + $entity: 'pay', + data: { + id: 1, + meta: 1, + iState: 1, + entity: 1, + entityId: 1, + }, + filter: { + iState: 'paid' + } + } + } } }, @@ -112,10 +128,27 @@ const triggers: Trigger[] = [ } if (shipOrder$ship && shipOrder$ship.length > 0) { //订单 - const clazz = await getShipClazz(entity!, entityId!, context); - const { openId } = await clazz.getReceiverInfo(shipOrder$ship.map((ele) => ele.orderId!), wechatMpShip?.applicationId!, context); - if (openId) { - //当存在openId时调用小程序发货信息录入 + const wpPayIds = ship.shipOrder$ship?.map((ele) => ele.order).map((order) => order.pay$order).flat().filter((pay) => pay?.entity === 'wpProduct').map((ele) => ele?.id!); + let needUpload = false; + if (wpPayIds && wpPayIds.length > 0) { + const cnt = await context.count('pay', { + filter: { + id: { + $in: wpPayIds, + }, + iState: { + $ne: 'closed', + }, + wpProduct: { + needReceiving: true, + } + } + }, {}); + if (cnt > 0) { + needUpload = true; + } + } + if (needUpload) { await uploadShippingInfo(shipId!, context); return 1; } @@ -136,7 +169,7 @@ const triggers: Trigger[] = [ } return 1; } - } as UpdateTriggerInTxn, + } as UpdateTriggerInTxn, { name: '当物流创建时,赋上对应的物流系统对象', entity: 'ship', @@ -173,7 +206,7 @@ const triggers: Trigger[] = [ } return count; } - } as CreateTrigger, + } as CreateTrigger, { name: '当物流类的ship取消后,调用外部接口取消下单', entity: 'ship', @@ -215,12 +248,13 @@ const triggers: Trigger[] = [ } } as UpdateTriggerCrossTxn, { - name: '当ship签收后,调用小程序确认收货提醒', + name: '当物流类的ship签收后,调用小程序确认收货提醒', entity: 'ship', action: 'receive', when: 'commit', asRoot: true, filter: { + type: 'express', receiveAt: { $exists: true, } diff --git a/src/utils/ship.ts b/src/utils/ship.ts index 704097c0..2f9634a9 100644 --- a/src/utils/ship.ts +++ b/src/utils/ship.ts @@ -367,51 +367,70 @@ export async function getOrderShipState( */ export async function refreshtShipState( - ship: EntityDict['ship']['Schema'], + shipId: string, context: BRC, ) { - let ship2 = ship; - if (!ship2.iState || !ship2.entity || !ship2.entityId) { - const ships = await context.select('ship', { - data: shipProjection, - filter: { - id: ship.id, + const ships = await context.select('ship', { + data: shipProjection, + filter: { + id: shipId, + } + }, { + blockTrigger: true, + forUpdate: true, + }); + const ship = ships[0]; + const { entity, entityId } = ship; + if (entity && entityId) { + const clazz = await getShipClazz(entity!, entityId!, context); + const { state, time } = await clazz.syncState(ship.id!, context); + const wpPayIds = ship.shipOrder$ship?.map((ele) => ele.order).map((order) => order.pay$order).flat().filter((pay) => pay?.entity === 'wpProduct').map((ele) => ele?.id!); + let needReceiving = false; + if (wpPayIds && wpPayIds.length > 0) { + const cnt = await context.count('pay', { + filter: { + id: { + $in: wpPayIds, + }, + iState: { + $ne: 'closed', + }, + wpProduct: { + needReceiving: true, + } + } + }, {}); + if (cnt > 0) { + needReceiving = true; } - }, { - blockTrigger: true, - forUpdate: true, - }); - ship2 = ships[0] as typeof ship; - } - const { entity, entityId } = ship2; - const clazz = await getShipClazz(entity!, entityId!, context); - const { state, time } = await clazz.syncState(ship2.id!, context); - let action = undefined, updateData: EntityDict['ship']['UpdateOperationData'] = {}; - switch (state) { - case 'shipping': //已发货 - action = 'ship'; - break; - case 'received': //已收货 - action = 'receive'; - updateData = { - receiveAt: time - } - break; - case 'unknow': //不明 - action = 'unknow'; - break; - default: - action = undefined; - } - if (action && ship2.iState !== state) { - return await context.operate('ship', { - id: await generateNewIdAsync(), - action, - data: updateData, - filter: { - id: ship2.id, - } - }, {}); + } + let action = undefined, updateData: EntityDict['ship']['UpdateOperationData'] = {}; + switch (state) { + case 'shipping': //已发货 + action = 'ship'; + break; + case 'received': //已收货 + action = needReceiving ? 'startReceiving' : 'receive'; + updateData = { + receiveAt: time + } + break; + case 'unknow': //不明 + action = 'unknow'; + break; + default: + action = undefined; + } + if (action && ship.iState !== state) { + return await context.operate('ship', { + id: await generateNewIdAsync(), + action, + data: updateData, + filter: { + id: ship.id, + } + }, {}); + } } } diff --git a/src/watchers/ship.ts b/src/watchers/ship.ts index 4a2bab03..55772c7d 100644 --- a/src/watchers/ship.ts +++ b/src/watchers/ship.ts @@ -14,6 +14,7 @@ const watchers: Watcher[] = [ filter: async () => { const now = Date.now(); return { + type: 'express', iState: 'shipping', $$updateAt$$: { $lte: now - QUERY_PAYING_STATE_GAP, @@ -24,7 +25,7 @@ const watchers: Watcher[] = [ fn: async (context, data) => { const results = [] as OperationResult[]; for (const ship of data) { - const result = await refreshtShipState(ship as EntityDict['ship']['Schema'], context); + const result = await refreshtShipState(ship.id!, context); if (result) { results.push(result); }