79 lines
2.4 KiB
JavaScript
79 lines
2.4 KiB
JavaScript
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.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(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;
|
||
}
|
||
/**
|
||
* 创建ship时,根据系统物流的配置去对接对应的物流系统
|
||
* @param shipId
|
||
* @param context
|
||
*/
|
||
export async function getShipEntity(shipServiceId, orderIds, 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) {
|
||
const clazz = await getShipClazz(e, row.id, context);
|
||
if (await clazz.available(shipServiceId, orderIds, context)) {
|
||
sort = row.sort;
|
||
entity = e;
|
||
entityId = row.id;
|
||
}
|
||
}
|
||
}
|
||
if (entity) {
|
||
return [entity, entityId];
|
||
}
|
||
}
|