refreshtShipState修改,调用小程序发货信息录入接口trigger修改

This commit is contained in:
lxy 2025-03-19 17:12:13 +08:00
parent a2089585b8
commit 966118efee
4 changed files with 106 additions and 52 deletions

View File

@ -13,7 +13,7 @@ const timers: Array<Timer<EntityDict, 'ship', BRC>> = [
filter: {
type: 'express',
iState: {
$nin: ['received', 'cancelled', 'rejected'],
$nin: ['received', 'cancelled', 'rejected', 'receiving'],
},
entity: {
$exists: true
@ -33,7 +33,7 @@ const timers: Array<Timer<EntityDict, 'ship', BRC>> = [
fn: async (context, data) => {
const results = [] as OperationResult<EntityDict>[];
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);
}

View File

@ -94,6 +94,22 @@ const triggers: Trigger<EntityDict, 'ship', BRC>[] = [
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<EntityDict, 'ship', BRC>[] = [
}
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<EntityDict, 'ship', BRC>[] = [
}
return 1;
}
} as UpdateTriggerInTxn<EntityDict, 'ship', BRC>,
} as UpdateTriggerInTxn<EntityDict, 'ship', BRC>,
{
name: '当物流创建时,赋上对应的物流系统对象',
entity: 'ship',
@ -173,7 +206,7 @@ const triggers: Trigger<EntityDict, 'ship', BRC>[] = [
}
return count;
}
} as CreateTrigger<EntityDict, 'ship', BRC>,
} as CreateTrigger<EntityDict, 'ship', BRC>,
{
name: '当物流类的ship取消后调用外部接口取消下单',
entity: 'ship',
@ -215,12 +248,13 @@ const triggers: Trigger<EntityDict, 'ship', BRC>[] = [
}
} as UpdateTriggerCrossTxn<EntityDict, 'ship', BRC>,
{
name: '当ship签收后调用小程序确认收货提醒',
name: '当物流类的ship签收后调用小程序确认收货提醒',
entity: 'ship',
action: 'receive',
when: 'commit',
asRoot: true,
filter: {
type: 'express',
receiveAt: {
$exists: true,
}

View File

@ -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,
}
}, {});
}
}
}

View File

@ -14,6 +14,7 @@ const watchers: Watcher<EntityDict, 'ship', BRC>[] = [
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<EntityDict, 'ship', BRC>[] = [
fn: async (context, data) => {
const results = [] as OperationResult<EntityDict>[];
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);
}