修改settlePlan更新order的相关trigger,均改为after

This commit is contained in:
lxy 2025-08-05 19:42:34 +08:00
parent 1096a25cd8
commit a536132e2c
6 changed files with 440 additions and 306 deletions

View File

@ -3,62 +3,50 @@ import assert from 'assert';
import { groupBy } from 'oak-domain/lib/utils/lodash';
const triggers = [
{
name: '当settlePlan创建更新关联order的settlePlanned',
name: '当settlePlan创建后,更新关联order的settlePlanned',
entity: 'settlePlan',
action: 'create',
when: 'before',
when: 'after',
asRoot: true,
priority: 99,
// priority: 99,
fn: async ({ operation }, context, option) => {
const { data } = operation;
let count = 0;
let ids = [];
if (data instanceof Array) {
const planArr = groupBy(data, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const [order] = await context.select('order', {
data: {
id: 1,
paid: 1,
refunded: 1,
settlePlanned: 1,
},
filter: {
id: orderId,
}
}, { forUpdate: true });
const { id, paid, refunded, settlePlanned } = order;
let planPrice = 0;
const plans = planArr[orderId];
plans.forEach((plan) => planPrice += plan.price);
const newSettlePlanned = planPrice + settlePlanned;
await context.operate('order', {
id: await generateNewIdAsync(),
action: 'update',
data: {
settlePlanned: newSettlePlanned,
},
filter: {
id,
}
}, option);
}
count += data.length;
ids = data.map((ele) => ele.id);
}
else {
ids = [data.id];
}
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
orderId: 1,
price: 1,
},
filter: {
id: {
$in: ids,
}
},
}, { forUpdate: true });
const planArr = groupBy(settlePlans, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const [order] = await context.select('order', {
data: {
id: 1,
paid: 1,
refunded: 1,
settlePlanned: 1,
},
filter: {
id: data.orderId,
id: orderId,
}
}, { forUpdate: true });
const { id, paid, refunded, settlePlanned } = order;
const newSettlePlanned = data.price + settlePlanned;
const { id, settlePlanned } = order;
let planPrice = 0;
const plans = planArr[orderId];
plans.forEach((plan) => planPrice += plan.price);
const newSettlePlanned = planPrice + settlePlanned;
await context.operate('order', {
id: await generateNewIdAsync(),
action: 'update',
@ -69,13 +57,12 @@ const triggers = [
id,
}
}, option);
count++;
}
return count;
return ids.length;
},
},
{
name: '当settlePlan执行settle时将关联的settlement执行settle并更新order的settled',
name: '当settlePlan执行settle时将关联的settlement执行settle',
entity: 'settlePlan',
action: 'settle',
when: 'before',
@ -99,6 +86,54 @@ const triggers = [
iState: 'unsettled',
}
},
},
filter,
}, { forUpdate: true });
const now = Date.now();
for (const plan of settlePlans) {
const { settlement$plan: settlements, } = plan;
assert(settlements && settlements.length > 0);
//关联的settlement均执行settle并生成accountOper
for (const settlement of settlements) {
const { id: settlementId, price: settlementPrice, accountId, } = settlement;
await context.operate('settlement', {
id: await generateNewIdAsync(),
action: 'settle',
data: {
settledAt: now,
accountOper$entity: [{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
totalPlus: settlementPrice,
availPlus: settlementPrice,
accountId: accountId,
type: 'settle',
}
}]
},
filter: {
id: settlementId,
}
}, option);
}
}
return settlePlans.length;
},
},
{
name: '当settlePlan执行settle后更新order的settled',
entity: 'settlePlan',
action: 'settle',
when: 'after',
asRoot: true,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
price: 1,
orderId: 1,
order: {
id: 1,
@ -109,43 +144,14 @@ const triggers = [
},
filter,
}, { forUpdate: true });
const now = Date.now();
assert(settlePlans.length > 0, '未查询到settlePlan请检查filter');
const planArr = groupBy(settlePlans, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const plans = planArr[orderId];
const order = planArr[orderId][0].order;
let planPrice = 0;
for (const plan of plans) {
const { id, price, settlement$plan: settlements, } = plan;
assert(settlements && settlements.length > 0);
//关联的settlement均执行settle并生成accountOper
for (const settlement of settlements) {
const { id: settlementId, price: settlementPrice, accountId, } = settlement;
await context.operate('settlement', {
id: await generateNewIdAsync(),
action: 'settle',
data: {
settledAt: now,
accountOper$entity: [{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
totalPlus: settlementPrice,
availPlus: settlementPrice,
accountId: accountId,
type: 'settle',
}
}]
},
filter: {
id: settlementId,
}
}, option);
}
planPrice += price;
}
plans.forEach((plan) => planPrice += plan.price);
//更新order的settled
const newSettledPrice = order?.settled + planPrice;
await context.operate('order', {
@ -159,11 +165,11 @@ const triggers = [
}
}, option);
}
return settlePlans.length;
return orderIds.length;
},
},
{
name: '当settlePlan执行close时将关联的settlement执行close并更新order的settlePlanned',
name: '当settlePlan执行close时将关联的settlement执行close',
entity: 'settlePlan',
action: 'close',
when: 'before',
@ -187,6 +193,43 @@ const triggers = [
iState: 'unsettled',
}
},
},
filter,
}, { forUpdate: true });
const now = Date.now();
for (const plan of settlePlans) {
const { settlement$plan: settlements, } = plan;
assert(settlements && settlements.length > 0);
//关联的settlement均执行close
const settlementIds = settlements.map((ele) => ele.id);
await context.operate('settlement', {
id: await generateNewIdAsync(),
action: 'close',
data: {
closedAt: now,
},
filter: {
id: {
$in: settlementIds,
},
}
}, option);
}
return settlePlans.length;
},
},
{
name: '当settlePlan执行close后并更新order的settlePlanned',
entity: 'settlePlan',
action: 'close',
when: 'after',
asRoot: true,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
price: 1,
orderId: 1,
order: {
id: 1,
@ -195,33 +238,14 @@ const triggers = [
},
filter,
}, { forUpdate: true });
const now = Date.now();
assert(settlePlans.length > 0, '未查询到settlePlan请检查filter');
const planArr = groupBy(settlePlans, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const plans = planArr[orderId];
const order = planArr[orderId][0].order;
let planPrice = 0;
for (const plan of plans) {
const { id, price, settlement$plan: settlements, } = plan;
assert(settlements && settlements.length > 0);
//关联的settlement均执行close
const settlementIds = settlements.map((ele) => ele.id);
await context.operate('settlement', {
id: await generateNewIdAsync(),
action: 'close',
data: {
closedAt: now,
},
filter: {
id: {
$in: settlementIds,
},
iState: 'unsettled'
}
}, option);
planPrice += price;
}
plans.forEach((plan) => planPrice += plan.price);
//更新order的settlePlanned
const newSettlePlanned = order?.settlePlanned - planPrice;
await context.operate('order', {
@ -235,7 +259,7 @@ const triggers = [
}
}, option);
}
return settlePlans.length;
return orderIds.length;
},
},
];

View File

@ -1,8 +1,10 @@
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
const watchers = [
{
//settle 更新order时filter不允许携带iState故使用fn方式
name: '当settlePlan达到结算时间时结算settlePlan',
entity: 'settlePlan',
filter: () => {
filter: async () => {
const now = Date.now();
return {
iState: 'unsettled',
@ -11,8 +13,25 @@ const watchers = [
},
};
},
action: 'settle',
actionData: {},
projection: {
id: 1,
},
fn: async (context, data) => {
if (data.length > 0) {
const ids = data.map((ele) => ele.id);
await context.operate('settlePlan', {
id: await generateNewIdAsync(),
action: 'settle',
data: {},
filter: {
id: {
$in: ids,
}
}
}, {});
}
return context.opResult;
}
}
];
export default watchers;

View File

@ -6,62 +6,50 @@ const assert_1 = tslib_1.__importDefault(require("assert"));
const lodash_1 = require("oak-domain/lib/utils/lodash");
const triggers = [
{
name: '当settlePlan创建更新关联order的settlePlanned',
name: '当settlePlan创建后,更新关联order的settlePlanned',
entity: 'settlePlan',
action: 'create',
when: 'before',
when: 'after',
asRoot: true,
priority: 99,
// priority: 99,
fn: async ({ operation }, context, option) => {
const { data } = operation;
let count = 0;
let ids = [];
if (data instanceof Array) {
const planArr = (0, lodash_1.groupBy)(data, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const [order] = await context.select('order', {
data: {
id: 1,
paid: 1,
refunded: 1,
settlePlanned: 1,
},
filter: {
id: orderId,
}
}, { forUpdate: true });
const { id, paid, refunded, settlePlanned } = order;
let planPrice = 0;
const plans = planArr[orderId];
plans.forEach((plan) => planPrice += plan.price);
const newSettlePlanned = planPrice + settlePlanned;
await context.operate('order', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
settlePlanned: newSettlePlanned,
},
filter: {
id,
}
}, option);
}
count += data.length;
ids = data.map((ele) => ele.id);
}
else {
ids = [data.id];
}
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
orderId: 1,
price: 1,
},
filter: {
id: {
$in: ids,
}
},
}, { forUpdate: true });
const planArr = (0, lodash_1.groupBy)(settlePlans, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const [order] = await context.select('order', {
data: {
id: 1,
paid: 1,
refunded: 1,
settlePlanned: 1,
},
filter: {
id: data.orderId,
id: orderId,
}
}, { forUpdate: true });
const { id, paid, refunded, settlePlanned } = order;
const newSettlePlanned = data.price + settlePlanned;
const { id, settlePlanned } = order;
let planPrice = 0;
const plans = planArr[orderId];
plans.forEach((plan) => planPrice += plan.price);
const newSettlePlanned = planPrice + settlePlanned;
await context.operate('order', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
@ -72,13 +60,12 @@ const triggers = [
id,
}
}, option);
count++;
}
return count;
return ids.length;
},
},
{
name: '当settlePlan执行settle时将关联的settlement执行settle并更新order的settled',
name: '当settlePlan执行settle时将关联的settlement执行settle',
entity: 'settlePlan',
action: 'settle',
when: 'before',
@ -102,6 +89,54 @@ const triggers = [
iState: 'unsettled',
}
},
},
filter,
}, { forUpdate: true });
const now = Date.now();
for (const plan of settlePlans) {
const { settlement$plan: settlements, } = plan;
(0, assert_1.default)(settlements && settlements.length > 0);
//关联的settlement均执行settle并生成accountOper
for (const settlement of settlements) {
const { id: settlementId, price: settlementPrice, accountId, } = settlement;
await context.operate('settlement', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'settle',
data: {
settledAt: now,
accountOper$entity: [{
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'create',
data: {
id: await (0, uuid_1.generateNewIdAsync)(),
totalPlus: settlementPrice,
availPlus: settlementPrice,
accountId: accountId,
type: 'settle',
}
}]
},
filter: {
id: settlementId,
}
}, option);
}
}
return settlePlans.length;
},
},
{
name: '当settlePlan执行settle后更新order的settled',
entity: 'settlePlan',
action: 'settle',
when: 'after',
asRoot: true,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
price: 1,
orderId: 1,
order: {
id: 1,
@ -112,43 +147,14 @@ const triggers = [
},
filter,
}, { forUpdate: true });
const now = Date.now();
(0, assert_1.default)(settlePlans.length > 0, '未查询到settlePlan请检查filter');
const planArr = (0, lodash_1.groupBy)(settlePlans, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const plans = planArr[orderId];
const order = planArr[orderId][0].order;
let planPrice = 0;
for (const plan of plans) {
const { id, price, settlement$plan: settlements, } = plan;
(0, assert_1.default)(settlements && settlements.length > 0);
//关联的settlement均执行settle并生成accountOper
for (const settlement of settlements) {
const { id: settlementId, price: settlementPrice, accountId, } = settlement;
await context.operate('settlement', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'settle',
data: {
settledAt: now,
accountOper$entity: [{
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'create',
data: {
id: await (0, uuid_1.generateNewIdAsync)(),
totalPlus: settlementPrice,
availPlus: settlementPrice,
accountId: accountId,
type: 'settle',
}
}]
},
filter: {
id: settlementId,
}
}, option);
}
planPrice += price;
}
plans.forEach((plan) => planPrice += plan.price);
//更新order的settled
const newSettledPrice = order?.settled + planPrice;
await context.operate('order', {
@ -162,11 +168,11 @@ const triggers = [
}
}, option);
}
return settlePlans.length;
return orderIds.length;
},
},
{
name: '当settlePlan执行close时将关联的settlement执行close并更新order的settlePlanned',
name: '当settlePlan执行close时将关联的settlement执行close',
entity: 'settlePlan',
action: 'close',
when: 'before',
@ -190,6 +196,43 @@ const triggers = [
iState: 'unsettled',
}
},
},
filter,
}, { forUpdate: true });
const now = Date.now();
for (const plan of settlePlans) {
const { settlement$plan: settlements, } = plan;
(0, assert_1.default)(settlements && settlements.length > 0);
//关联的settlement均执行close
const settlementIds = settlements.map((ele) => ele.id);
await context.operate('settlement', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'close',
data: {
closedAt: now,
},
filter: {
id: {
$in: settlementIds,
},
}
}, option);
}
return settlePlans.length;
},
},
{
name: '当settlePlan执行close后并更新order的settlePlanned',
entity: 'settlePlan',
action: 'close',
when: 'after',
asRoot: true,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
price: 1,
orderId: 1,
order: {
id: 1,
@ -198,33 +241,14 @@ const triggers = [
},
filter,
}, { forUpdate: true });
const now = Date.now();
(0, assert_1.default)(settlePlans.length > 0, '未查询到settlePlan请检查filter');
const planArr = (0, lodash_1.groupBy)(settlePlans, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const plans = planArr[orderId];
const order = planArr[orderId][0].order;
let planPrice = 0;
for (const plan of plans) {
const { id, price, settlement$plan: settlements, } = plan;
(0, assert_1.default)(settlements && settlements.length > 0);
//关联的settlement均执行close
const settlementIds = settlements.map((ele) => ele.id);
await context.operate('settlement', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'close',
data: {
closedAt: now,
},
filter: {
id: {
$in: settlementIds,
},
iState: 'unsettled'
}
}, option);
planPrice += price;
}
plans.forEach((plan) => planPrice += plan.price);
//更新order的settlePlanned
const newSettlePlanned = order?.settlePlanned - planPrice;
await context.operate('order', {
@ -238,7 +262,7 @@ const triggers = [
}
}, option);
}
return settlePlans.length;
return orderIds.length;
},
},
];

View File

@ -1,10 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const uuid_1 = require("oak-domain/lib/utils/uuid");
const watchers = [
{
//settle 更新order时filter不允许携带iState故使用fn方式
name: '当settlePlan达到结算时间时结算settlePlan',
entity: 'settlePlan',
filter: () => {
filter: async () => {
const now = Date.now();
return {
iState: 'unsettled',
@ -13,8 +15,25 @@ const watchers = [
},
};
},
action: 'settle',
actionData: {},
projection: {
id: 1,
},
fn: async (context, data) => {
if (data.length > 0) {
const ids = data.map((ele) => ele.id);
await context.operate('settlePlan', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'settle',
data: {},
filter: {
id: {
$in: ids,
}
}
}, {});
}
return context.opResult;
}
}
];
exports.default = watchers;

View File

@ -7,64 +7,52 @@ import { groupBy } from 'oak-domain/lib/utils/lodash';
const triggers: Trigger<EntityDict, 'settlePlan', BRC>[] = [
{
name: '当settlePlan创建更新关联order的settlePlanned',
name: '当settlePlan创建后,更新关联order的settlePlanned',
entity: 'settlePlan',
action: 'create',
when: 'before',
when: 'after',
asRoot: true,
priority: 99,
// priority: 99,
fn: async ({ operation }, context, option) => {
const { data } = operation;
let count = 0;
let ids = [];
if (data instanceof Array) {
const planArr = groupBy(data, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const [order] = await context.select('order', {
data: {
id: 1,
paid: 1,
refunded: 1,
settlePlanned: 1,
},
filter: {
id: orderId,
}
}, { forUpdate: true });
const { id, paid, refunded, settlePlanned } = order;
let planPrice = 0;
const plans = planArr[orderId];
plans!.forEach(
(plan) => planPrice += plan.price!
);
const newSettlePlanned = planPrice + settlePlanned!;
await context.operate('order', {
id: await generateNewIdAsync(),
action: 'update',
data: {
settlePlanned: newSettlePlanned,
},
filter: {
id,
}
}, option);
}
count += data.length;
ids = data.map((ele) => ele.id);
} else {
ids = [data.id];
}
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
orderId: 1,
price: 1,
},
filter: {
id: {
$in: ids,
}
},
}, { forUpdate: true });
const planArr = groupBy(settlePlans, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const [order] = await context.select('order', {
data: {
id: 1,
paid: 1,
refunded: 1,
settlePlanned: 1,
},
filter: {
id: data.orderId,
id: orderId,
}
}, { forUpdate: true });
const { id, paid, refunded, settlePlanned } = order;
const newSettlePlanned = data.price! + settlePlanned!;
const { id, settlePlanned } = order;
let planPrice = 0;
const plans = planArr[orderId];
plans!.forEach(
(plan) => planPrice += plan.price!
);
const newSettlePlanned = planPrice + settlePlanned!;
await context.operate('order', {
id: await generateNewIdAsync(),
action: 'update',
@ -75,13 +63,13 @@ const triggers: Trigger<EntityDict, 'settlePlan', BRC>[] = [
id,
}
}, option);
count++;
}
return count;
return ids.length;
},
} as CreateTriggerInTxn<EntityDict, 'settlePlan', BRC>,
{
name: '当settlePlan执行settle时将关联的settlement执行settle并更新order的settled',
name: '当settlePlan执行settle时将关联的settlement执行settle',
entity: 'settlePlan',
action: 'settle',
when: 'before',
@ -105,6 +93,54 @@ const triggers: Trigger<EntityDict, 'settlePlan', BRC>[] = [
iState: 'unsettled',
}
},
},
filter,
}, { forUpdate: true });
const now = Date.now();
for (const plan of settlePlans) {
const { settlement$plan: settlements, } = plan;
assert(settlements && settlements.length > 0);
//关联的settlement均执行settle并生成accountOper
for (const settlement of settlements) {
const { id: settlementId, price: settlementPrice, accountId, } = settlement;
await context.operate('settlement', {
id: await generateNewIdAsync(),
action: 'settle',
data: {
settledAt: now,
accountOper$entity: [{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
totalPlus: settlementPrice,
availPlus: settlementPrice,
accountId: accountId,
type: 'settle',
}
}]
},
filter: {
id: settlementId,
}
}, option);
}
}
return settlePlans.length;
},
},
{
name: '当settlePlan执行settle后更新order的settled',
entity: 'settlePlan',
action: 'settle',
when: 'after',
asRoot: true,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
price: 1,
orderId: 1,
order: {
id: 1,
@ -115,43 +151,16 @@ const triggers: Trigger<EntityDict, 'settlePlan', BRC>[] = [
},
filter,
}, { forUpdate: true });
const now = Date.now();
assert(settlePlans.length > 0, '未查询到settlePlan请检查filter');
const planArr = groupBy(settlePlans, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const plans = planArr[orderId];
const order = planArr[orderId][0].order;
let planPrice = 0;
for (const plan of plans) {
const { id, price, settlement$plan: settlements, } = plan;
assert(settlements && settlements.length > 0);
//关联的settlement均执行settle并生成accountOper
for (const settlement of settlements) {
const { id: settlementId, price: settlementPrice, accountId, } = settlement;
await context.operate('settlement', {
id: await generateNewIdAsync(),
action: 'settle',
data: {
settledAt: now,
accountOper$entity: [{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
totalPlus: settlementPrice,
availPlus: settlementPrice,
accountId: accountId,
type: 'settle',
}
}]
},
filter: {
id: settlementId,
}
}, option);
}
planPrice += price!;
}
plans!.forEach(
(plan) => planPrice += plan.price!
);
//更新order的settled
const newSettledPrice = order?.settled! + planPrice;
await context.operate('order', {
@ -165,11 +174,11 @@ const triggers: Trigger<EntityDict, 'settlePlan', BRC>[] = [
}
}, option);
}
return settlePlans.length;
return orderIds.length;
},
},
{
name: '当settlePlan执行close时将关联的settlement执行close并更新order的settlePlanned',
name: '当settlePlan执行close时将关联的settlement执行close',
entity: 'settlePlan',
action: 'close',
when: 'before',
@ -193,6 +202,43 @@ const triggers: Trigger<EntityDict, 'settlePlan', BRC>[] = [
iState: 'unsettled',
}
},
},
filter,
}, { forUpdate: true });
const now = Date.now();
for (const plan of settlePlans) {
const { settlement$plan: settlements, } = plan;
assert(settlements && settlements.length > 0);
//关联的settlement均执行close
const settlementIds = settlements.map((ele) => ele.id);
await context.operate('settlement', {
id: await generateNewIdAsync(),
action: 'close',
data: {
closedAt: now,
},
filter: {
id: {
$in: settlementIds,
},
}
}, option);
}
return settlePlans.length;
},
},
{
name: '当settlePlan执行close后并更新order的settlePlanned',
entity: 'settlePlan',
action: 'close',
when: 'after',
asRoot: true,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
price: 1,
orderId: 1,
order: {
id: 1,
@ -201,33 +247,16 @@ const triggers: Trigger<EntityDict, 'settlePlan', BRC>[] = [
},
filter,
}, { forUpdate: true });
const now = Date.now();
assert(settlePlans.length > 0, '未查询到settlePlan请检查filter');
const planArr = groupBy(settlePlans, 'orderId');
const orderIds = Object.keys(planArr);
for (const orderId of orderIds) {
const plans = planArr[orderId];
const order = planArr[orderId][0].order;
let planPrice = 0;
for (const plan of plans) {
const { id, price, settlement$plan: settlements, } = plan;
assert(settlements && settlements.length > 0);
//关联的settlement均执行close
const settlementIds = settlements.map((ele) => ele.id);
await context.operate('settlement', {
id: await generateNewIdAsync(),
action: 'close',
data: {
closedAt: now,
},
filter: {
id: {
$in: settlementIds,
},
iState: 'unsettled'
}
}, option);
planPrice += price!;
}
plans!.forEach(
(plan) => planPrice += plan.price!
);
//更新order的settlePlanned
const newSettlePlanned = order?.settlePlanned! - planPrice!;
await context.operate('order', {
@ -241,7 +270,7 @@ const triggers: Trigger<EntityDict, 'settlePlan', BRC>[] = [
}
}, option);
}
return settlePlans.length;
return orderIds.length;
},
},
];

View File

@ -1,12 +1,14 @@
import { BBWatcher, WBWatcher, Watcher } from 'oak-domain/lib/types/Watcher';
import { EntityDict } from '../oak-app-domain';
import { BRC } from '../types/RuntimeCxt';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
const watchers: Watcher<EntityDict, 'settlePlan', BRC>[] = [
{
//settle 更新order时filter不允许携带iState故使用fn方式
name: '当settlePlan达到结算时间时结算settlePlan',
entity: 'settlePlan',
filter: () => {
filter: async () => {
const now = Date.now();
return {
iState: 'unsettled',
@ -15,8 +17,25 @@ const watchers: Watcher<EntityDict, 'settlePlan', BRC>[] = [
},
};
},
action: 'settle',
actionData: {},
projection: {
id: 1,
},
fn: async (context, data) => {
if (data.length > 0) {
const ids = data.map((ele) => ele.id!);
await context.operate('settlePlan', {
id: await generateNewIdAsync(),
action: 'settle',
data: {},
filter: {
id: {
$in: ids,
}
}
}, {});
}
return context.opResult;
}
}
];