同步微信自动确认收货状态

This commit is contained in:
lxy 2025-04-07 11:28:47 +08:00
parent 70ed1ca5a6
commit a1e466f19c
11 changed files with 258 additions and 22 deletions

View File

@ -1,4 +1,4 @@
import { refreshtShipState } from '../utils/ship';
import { refreshShipState, refreshMpOrderShipState } from '../utils/ship';
import { mergeOperationResult } from 'oak-domain/lib/utils/operationResult';
const timers = [
{
@ -28,7 +28,34 @@ const timers = [
fn: async (context, data) => {
const results = [];
for (const ship of data) {
const result = await refreshtShipState(ship.id, context);
const result = await refreshShipState(ship.id, context);
if (result) {
results.push(result);
}
}
if (results.length === 0) {
return {};
}
return results.reduce((prev, cur) => mergeOperationResult(prev, cur));
},
},
{
name: '同步微信小程序虚拟ship状态',
cron: '0 0 * * * ?',
entity: 'ship',
filter: {
type: 'virtual',
iState: {
$nin: ['received', 'cancelled', 'rejected'],
},
},
projection: {
id: 1,
},
fn: async (context, data) => {
const results = [];
for (const ship of data) {
const result = await refreshMpOrderShipState(ship.id, context);
if (result) {
results.push(result);
}

11
es/utils/ship.d.ts vendored
View File

@ -14,19 +14,26 @@ export declare function maskPhone(phone: string): string;
*/
export declare function uploadShippingInfo(shipId: string, context: BRC): Promise<void>;
/**
*
*
* @param context
* @param shipId
* @returns
*/
export declare function getOrderShipState(context: BRC, shipId: string): Promise<string | undefined>;
/**
* ()
* @param context
* @param shipId
* @returns
*/
export declare function refreshMpOrderShipState(shipId: string, context: BRC): Promise<import("oak-domain/lib/types").OperationResult<EntityDict> | undefined>;
/**
* ship的物流状态
* @param ship
* @param context
* @returns
*/
export declare function refreshtShipState(shipId: string, context: BRC): Promise<import("oak-domain/lib/types").OperationResult<EntityDict> | undefined>;
export declare function refreshShipState(shipId: string, context: BRC): Promise<import("oak-domain/lib/types").OperationResult<EntityDict> | undefined>;
/**
*
*/

View File

@ -246,7 +246,7 @@ export async function uploadShippingInfo(shipId, context) {
}
}
/**
* 获取小程序物流状态
* 获取小程序订单状态
* @param context
* @param shipId
* @returns
@ -315,13 +315,58 @@ export async function getOrderShipState(context, shipId) {
}
}
}
/**
* 刷新小程序订单状态(自动确认收货)
* @param context
* @param shipId
* @returns
*/
export async function refreshMpOrderShipState(shipId, context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
iState: 1,
},
filter: {
id: shipId,
}
}, {
blockTrigger: true,
forUpdate: true
});
const mpState = await getOrderShipState(context, shipId);
let action = undefined;
switch (mpState) {
case 'shipping': //已发货
action = 'ship';
break;
case 'received': //已收货
action = 'succeedReceiving';
break;
case 'unknow': //不明
action = 'unknow';
break;
default:
action = undefined;
}
if (action && ship.iState !== mpState) {
return await context.operate('ship', {
id: await generateNewIdAsync(),
action,
data: {},
filter: {
id: ship.id,
}
}, {});
}
}
/**
* 刷新ship的物流状态
* @param ship
* @param context
* @returns
*/
export async function refreshtShipState(shipId, context) {
export async function refreshShipState(shipId, context) {
const ships = await context.select('ship', {
data: shipProjection,
filter: {

View File

@ -1,5 +1,5 @@
import { mergeOperationResult } from 'oak-domain/lib/utils/operationResult';
import { shipProjection, refreshtShipState } from '../utils/ship';
import { shipProjection, refreshShipState } from '../utils/ship';
const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
const watchers = [
{
@ -19,7 +19,7 @@ const watchers = [
fn: async (context, data) => {
const results = [];
for (const ship of data) {
const result = await refreshtShipState(ship.id, context);
const result = await refreshShipState(ship.id, context);
if (result) {
results.push(result);
}

View File

@ -30,7 +30,34 @@ const timers = [
fn: async (context, data) => {
const results = [];
for (const ship of data) {
const result = await (0, ship_1.refreshtShipState)(ship.id, context);
const result = await (0, ship_1.refreshShipState)(ship.id, context);
if (result) {
results.push(result);
}
}
if (results.length === 0) {
return {};
}
return results.reduce((prev, cur) => (0, operationResult_1.mergeOperationResult)(prev, cur));
},
},
{
name: '同步微信小程序虚拟ship状态',
cron: '0 0 * * * ?',
entity: 'ship',
filter: {
type: 'virtual',
iState: {
$nin: ['received', 'cancelled', 'rejected'],
},
},
projection: {
id: 1,
},
fn: async (context, data) => {
const results = [];
for (const ship of data) {
const result = await (0, ship_1.refreshMpOrderShipState)(ship.id, context);
if (result) {
results.push(result);
}

11
lib/utils/ship.d.ts vendored
View File

@ -14,19 +14,26 @@ export declare function maskPhone(phone: string): string;
*/
export declare function uploadShippingInfo(shipId: string, context: BRC): Promise<void>;
/**
*
*
* @param context
* @param shipId
* @returns
*/
export declare function getOrderShipState(context: BRC, shipId: string): Promise<string | undefined>;
/**
* ()
* @param context
* @param shipId
* @returns
*/
export declare function refreshMpOrderShipState(shipId: string, context: BRC): Promise<import("oak-domain/lib/types").OperationResult<EntityDict> | undefined>;
/**
* ship的物流状态
* @param ship
* @param context
* @returns
*/
export declare function refreshtShipState(shipId: string, context: BRC): Promise<import("oak-domain/lib/types").OperationResult<EntityDict> | undefined>;
export declare function refreshShipState(shipId: string, context: BRC): Promise<import("oak-domain/lib/types").OperationResult<EntityDict> | undefined>;
/**
*
*/

View File

@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.notifyConfirmReceive = exports.refreshtShipState = exports.getOrderShipState = exports.uploadShippingInfo = exports.maskPhone = exports.shipProjection = void 0;
exports.notifyConfirmReceive = exports.refreshShipState = exports.refreshMpOrderShipState = exports.getOrderShipState = exports.uploadShippingInfo = exports.maskPhone = exports.shipProjection = void 0;
const tslib_1 = require("tslib");
const oak_external_sdk_1 = require("oak-external-sdk");
const assert_1 = require("oak-domain/lib/utils/assert");
@ -252,7 +252,7 @@ async function uploadShippingInfo(shipId, context) {
}
exports.uploadShippingInfo = uploadShippingInfo;
/**
* 获取小程序物流状态
* 获取小程序订单状态
* @param context
* @param shipId
* @returns
@ -322,13 +322,59 @@ async function getOrderShipState(context, shipId) {
}
}
exports.getOrderShipState = getOrderShipState;
/**
* 刷新小程序订单状态(自动确认收货)
* @param context
* @param shipId
* @returns
*/
async function refreshMpOrderShipState(shipId, context) {
const [ship] = await context.select('ship', {
data: {
id: 1,
iState: 1,
},
filter: {
id: shipId,
}
}, {
blockTrigger: true,
forUpdate: true
});
const mpState = await getOrderShipState(context, shipId);
let action = undefined;
switch (mpState) {
case 'shipping': //已发货
action = 'ship';
break;
case 'received': //已收货
action = 'succeedReceiving';
break;
case 'unknow': //不明
action = 'unknow';
break;
default:
action = undefined;
}
if (action && ship.iState !== mpState) {
return await context.operate('ship', {
id: await (0, uuid_1.generateNewIdAsync)(),
action,
data: {},
filter: {
id: ship.id,
}
}, {});
}
}
exports.refreshMpOrderShipState = refreshMpOrderShipState;
/**
* 刷新ship的物流状态
* @param ship
* @param context
* @returns
*/
async function refreshtShipState(shipId, context) {
async function refreshShipState(shipId, context) {
const ships = await context.select('ship', {
data: exports.shipProjection,
filter: {
@ -392,7 +438,7 @@ async function refreshtShipState(shipId, context) {
}
}
}
exports.refreshtShipState = refreshtShipState;
exports.refreshShipState = refreshShipState;
/**
* 小程序确认收货提醒
*/

View File

@ -21,7 +21,7 @@ const watchers = [
fn: async (context, data) => {
const results = [];
for (const ship of data) {
const result = await (0, ship_1.refreshtShipState)(ship.id, context);
const result = await (0, ship_1.refreshShipState)(ship.id, context);
if (result) {
results.push(result);
}

View File

@ -2,7 +2,7 @@ import { Timer } from 'oak-domain/lib/types/Timer';
import { EntityDict } from '@oak-app-domain';
import { BRC } from '../types/RuntimeCxt';
import { OperationResult } from 'oak-domain/lib/types';
import { refreshtShipState } from '../utils/ship';
import { refreshShipState, refreshMpOrderShipState } from '../utils/ship';
import { mergeOperationResult } from 'oak-domain/lib/utils/operationResult';
const timers: Array<Timer<EntityDict, 'ship', BRC>> = [
@ -33,7 +33,36 @@ 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.id!, context);
const result = await refreshShipState(ship.id!, context);
if (result) {
results.push(result);
}
}
if (results.length === 0) {
return {};
}
return results.reduce(
(prev, cur) => mergeOperationResult(prev, cur)
);
},
},
{
name: '同步微信小程序虚拟ship状态',
cron: '0 0 * * * ?',
entity: 'ship',
filter: {
type: 'virtual',
iState: {
$nin: ['received', 'cancelled', 'rejected'],
},
},
projection: {
id: 1,
},
fn: async (context, data) => {
const results = [] as OperationResult<EntityDict>[];
for (const ship of data) {
const result = await refreshMpOrderShipState(ship.id!, context);
if (result) {
results.push(result);
}

View File

@ -276,7 +276,7 @@ export async function uploadShippingInfo(
}
/**
*
*
* @param context
* @param shipId
* @returns
@ -358,6 +358,54 @@ export async function getOrderShipState(
}
}
/**
* ()
* @param context
* @param shipId
* @returns
*/
export async function refreshMpOrderShipState(
shipId: string,
context: BRC,
) {
const [ship] = await context.select('ship', {
data: {
id: 1,
iState: 1,
},
filter: {
id: shipId,
}
}, {
blockTrigger: true,
forUpdate: true
});
const mpState = await getOrderShipState(context, shipId);
let action = undefined;
switch (mpState) {
case 'shipping': //已发货
action = 'ship';
break;
case 'received': //已收货
action = 'succeedReceiving';
break;
case 'unknow': //不明
action = 'unknow';
break;
default:
action = undefined;
}
if (action && ship.iState !== mpState) {
return await context.operate('ship', {
id: await generateNewIdAsync(),
action,
data: {},
filter: {
id: ship.id,
}
}, {});
}
}
/**
* ship的物流状态
@ -366,7 +414,7 @@ export async function getOrderShipState(
* @returns
*/
export async function refreshtShipState(
export async function refreshShipState(
shipId: string,
context: BRC,
) {

View File

@ -3,7 +3,7 @@ import { EntityDict } from '../oak-app-domain';
import { BRC } from '../types/RuntimeCxt';
import { OperationResult } from 'oak-domain/lib/types';
import { mergeOperationResult } from 'oak-domain/lib/utils/operationResult';
import { shipProjection, refreshtShipState } from '../utils/ship';
import { shipProjection, refreshShipState } from '../utils/ship';
const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
@ -25,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.id!, context);
const result = await refreshShipState(ship.id!, context);
if (result) {
results.push(result);
}