Merge branch 'dev' into release

This commit is contained in:
Xu Chang 2025-08-08 11:35:26 +08:00
commit ad7917b34e
19 changed files with 548 additions and 315 deletions

View File

@ -6,6 +6,8 @@ export interface Schema extends EntityShape {
when?: Datetime;
order: Order;
price: Price;
settledAt?: Datetime;
closedAt?: Datetime;
}
type IState = 'unsettled' | 'settled' | 'closed';
type IAction = 'settle' | 'close';

View File

@ -15,6 +15,8 @@ export const entityDesc = {
order: '订单',
price: '结算金额',
iState: '结算计划状态',
settledAt: '结算时间',
closedAt: '关闭时间'
},
action: {
settle: '结算',

View File

@ -13,6 +13,12 @@ export const desc = {
notNull: true,
type: "money"
},
settledAt: {
type: "datetime"
},
closedAt: {
type: "datetime"
},
iState: {
type: "enum",
enumeration: ["unsettled", "settled", "closed"]

View File

@ -7,6 +7,8 @@ export type OpSchema = EntityShape & {
when?: Datetime | null;
orderId: ForeignKey<"order">;
price: Price;
settledAt?: Datetime | null;
closedAt?: Datetime | null;
iState?: IState | null;
} & {
[A in ExpressionKey]?: any;
@ -20,6 +22,8 @@ export type OpFilter = {
when: Q_DateValue;
orderId: Q_StringValue;
price: Q_NumberValue;
settledAt: Q_DateValue;
closedAt: Q_DateValue;
iState: Q_EnumValue<IState>;
} & ExprOp<OpAttr | string>;
export type OpProjection = {
@ -32,6 +36,8 @@ export type OpProjection = {
when?: number;
orderId?: number;
price?: number;
settledAt?: number;
closedAt?: number;
iState?: number;
} & Partial<ExprOp<OpAttr | string>>;
export type OpSortAttr = Partial<{
@ -42,6 +48,8 @@ export type OpSortAttr = Partial<{
when: number;
orderId: number;
price: number;
settledAt: number;
closedAt: number;
iState: number;
[k: string]: any;
} | ExprOp<OpAttr | string>>;

View File

@ -4,7 +4,9 @@
"when": "结算时间",
"order": "订单",
"price": "结算金额",
"iState": "结算计划状态"
"iState": "结算计划状态",
"settledAt": "结算时间",
"closedAt": "关闭时间"
},
"action": {
"settle": "结算",

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,20 +57,19 @@ 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',
asRoot: true,
priority: 99,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const { filter, data } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
@ -99,6 +86,63 @@ 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);
}
}
//为settlePlan赋上settledAt
if (data instanceof Array) {
for (const d of data) {
d.settledAt = now;
}
}
else {
data.settledAt = now;
}
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 +153,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,18 +174,18 @@ 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',
asRoot: true,
priority: 99,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const { filter, data } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
@ -187,6 +202,52 @@ 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);
}
//为settlePlan赋上closedAt
if (data instanceof Array) {
for (const d of data) {
d.closedAt = now;
}
}
else {
data.closedAt = now;
}
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 +256,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 +277,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,6 +6,8 @@ export interface Schema extends EntityShape {
when?: Datetime;
order: Order;
price: Price;
settledAt?: Datetime;
closedAt?: Datetime;
}
type IState = 'unsettled' | 'settled' | 'closed';
type IAction = 'settle' | 'close';

View File

@ -18,6 +18,8 @@ exports.entityDesc = {
order: '订单',
price: '结算金额',
iState: '结算计划状态',
settledAt: '结算时间',
closedAt: '关闭时间'
},
action: {
settle: '结算',

View File

@ -16,6 +16,12 @@ exports.desc = {
notNull: true,
type: "money"
},
settledAt: {
type: "datetime"
},
closedAt: {
type: "datetime"
},
iState: {
type: "enum",
enumeration: ["unsettled", "settled", "closed"]

View File

@ -7,6 +7,8 @@ export type OpSchema = EntityShape & {
when?: Datetime | null;
orderId: ForeignKey<"order">;
price: Price;
settledAt?: Datetime | null;
closedAt?: Datetime | null;
iState?: IState | null;
} & {
[A in ExpressionKey]?: any;
@ -20,6 +22,8 @@ export type OpFilter = {
when: Q_DateValue;
orderId: Q_StringValue;
price: Q_NumberValue;
settledAt: Q_DateValue;
closedAt: Q_DateValue;
iState: Q_EnumValue<IState>;
} & ExprOp<OpAttr | string>;
export type OpProjection = {
@ -32,6 +36,8 @@ export type OpProjection = {
when?: number;
orderId?: number;
price?: number;
settledAt?: number;
closedAt?: number;
iState?: number;
} & Partial<ExprOp<OpAttr | string>>;
export type OpSortAttr = Partial<{
@ -42,6 +48,8 @@ export type OpSortAttr = Partial<{
when: number;
orderId: number;
price: number;
settledAt: number;
closedAt: number;
iState: number;
[k: string]: any;
} | ExprOp<OpAttr | string>>;

View File

@ -4,7 +4,9 @@
"when": "结算时间",
"order": "订单",
"price": "结算金额",
"iState": "结算计划状态"
"iState": "结算计划状态",
"settledAt": "结算时间",
"closedAt": "关闭时间"
},
"action": {
"settle": "结算",

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,20 +60,19 @@ 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',
asRoot: true,
priority: 99,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const { filter, data } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
@ -102,6 +89,63 @@ 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);
}
}
//为settlePlan赋上settledAt
if (data instanceof Array) {
for (const d of data) {
d.settledAt = now;
}
}
else {
data.settledAt = now;
}
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 +156,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,18 +177,18 @@ 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',
asRoot: true,
priority: 99,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const { filter, data } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
@ -190,6 +205,52 @@ 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);
}
//为settlePlan赋上closedAt
if (data instanceof Array) {
for (const d of data) {
d.closedAt = now;
}
}
else {
data.closedAt = now;
}
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 +259,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 +280,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

@ -1,6 +1,6 @@
{
"name": "oak-pay-business",
"version": "3.1.2",
"version": "3.1.3",
"description": "",
"files": [
"lib/**/*",

View File

@ -10,6 +10,8 @@ export interface Schema extends EntityShape {
when?: Datetime;
order: Order;
price: Price;
settledAt?: Datetime;
closedAt?: Datetime;
};
type IState = 'unsettled' | 'settled' | 'closed';
@ -36,6 +38,8 @@ export const entityDesc: EntityDesc<Schema, Action, '', {
order: '订单',
price: '结算金额',
iState: '结算计划状态',
settledAt: '结算时间',
closedAt: '关闭时间'
},
action: {
settle: '结算',

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,20 +63,20 @@ 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',
asRoot: true,
priority: 99,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const { filter, data } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
@ -105,6 +93,62 @@ 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);
}
}
//为settlePlan赋上settledAt
if (data instanceof Array) {
for (const d of data) {
d.settledAt = now;
}
} else {
data.settledAt = now;
}
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 +159,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,18 +182,18 @@ 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',
asRoot: true,
priority: 99,
fn: async ({ operation }, context, option) => {
const { filter } = operation;
const { filter, data } = operation;
const settlePlans = await context.select('settlePlan', {
data: {
id: 1,
@ -193,6 +210,51 @@ 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);
}
//为settlePlan赋上closedAt
if (data instanceof Array) {
for (const d of data) {
d.closedAt = now;
}
} else {
data.closedAt = now;
}
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 +263,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 +286,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;
}
}
];

View File

@ -0,0 +1,3 @@
-- settlePlan --
ALTER TABLE `settlePlan` ADD COLUMN `settledAt` bigint NULL DEFAULT NULL;
ALTER TABLE `settlePlan` ADD COLUMN `closedAt` bigint NULL DEFAULT NULL;