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

32 lines
1.2 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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.bizId && attributes.bizId.type === 'varchar');
(0, assert_1.default)(attributes.sort && attributes.sort.type === 'decimal');
ShipClazzEntityDict[entity] = clazzConstructor;
}
exports.registerShipClazzEntity = registerShipClazzEntity;
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;
}
exports.getShipClazz = getShipClazz;