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

78 lines
2.3 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.

import assert from 'assert';
let MODULE_USED = false;
const ShipClazzDict = {};
const ShipClazzEntityDict = {};
export function registerShipClazzEntity(entity, clazzConstructor, schema) {
assert(!MODULE_USED);
assert(!ShipClazzEntityDict.hasOwnProperty(entity));
const { attributes } = schema[entity];
assert(attributes.bizId && attributes.bizId.type === 'varchar');
assert(attributes.sort && attributes.sort.type === 'decimal');
assert(attributes.systemId && attributes.systemId.type === 'ref' && attributes.systemId.ref === 'system');
assert(attributes.disabled && attributes.disabled.type === 'boolean');
ShipClazzEntityDict[entity] = clazzConstructor;
}
export async function getShipClazz(ship, context) {
if (!MODULE_USED) {
MODULE_USED = true;
}
const { entity, entityId } = ship;
const key = `${entity}.${entityId}`;
if (ShipClazzDict[key]) {
return ShipClazzDict[key];
}
const clazz = await ShipClazzEntityDict[entity](entityId, context);
ShipClazzDict[key] = clazz;
return clazz;
}
/**
* 创建ship时根据系统物流的配置去对接对应的物流系统
* @param shipId
* @param context
*/
export async function getShipEntity(context) {
const systemId = context.getSystemId();
assert(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) {
sort = row.sort;
entity = e;
entityId = row.id;
}
}
if (entity) {
return [entity, entityId];
}
}