oak-pay-business/lib/utils/shipClazz/index.js

83 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getShipEntity = exports.getShipClazz = exports.registerShipClazzEntity = void 0;
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
let MODULE_USED = false;
const ShipClazzDict = {};
const ShipClazzEntityDict = {};
function registerShipClazzEntity(entity, clazzConstructor, schema) {
(0, assert_1.default)(!MODULE_USED);
(0, assert_1.default)(!ShipClazzEntityDict.hasOwnProperty(entity));
const { attributes } = schema[entity];
(0, assert_1.default)(attributes.sort && attributes.sort.type === 'decimal');
(0, assert_1.default)(attributes.systemId && attributes.systemId.type === 'ref' && attributes.systemId.ref === 'system');
(0, assert_1.default)(attributes.disabled && attributes.disabled.type === 'boolean');
ShipClazzEntityDict[entity] = clazzConstructor;
}
exports.registerShipClazzEntity = registerShipClazzEntity;
async function getShipClazz(entity, entityId, context) {
if (!MODULE_USED) {
MODULE_USED = true;
}
const key = `${entity}.${entityId}`;
if (ShipClazzDict[key]) {
return ShipClazzDict[key];
}
const clazz = await ShipClazzEntityDict[entity](entityId, context);
ShipClazzDict[key] = clazz;
return clazz;
}
exports.getShipClazz = getShipClazz;
/**
* 创建ship时根据系统物流的配置去对接对应的物流系统
* @param shipId
* @param context
*/
async function getShipEntity(shipServiceId, orderIds, context) {
const systemId = context.getSystemId();
(0, assert_1.default)(systemId);
const entities = Object.keys(ShipClazzEntityDict);
if (!entities.length) {
return;
}
const projection = {
id: 1,
};
entities.forEach((ele) => {
Object.assign(projection, {
[`${ele}$system`]: {
$entity: ele,
data: {
id: 1,
sort: 1,
},
filter: {
disabled: false,
},
}
});
});
const [system] = await context.select('system', {
data: projection,
filter: {
id: systemId,
},
}, { dontCollect: true });
let entity = '', entityId = '';
let sort = 0;
for (const e of entities) {
const k = `${e}$system`;
const row = system[k][0];
if (row && row.sort > sort && await ShipClazzDict[e]?.available(shipServiceId, orderIds, context)) {
sort = row.sort;
entity = e;
entityId = row.id;
}
}
if (entity) {
return [entity, entityId];
}
}
exports.getShipEntity = getShipEntity;