200 lines
5.1 KiB
JavaScript
200 lines
5.1 KiB
JavaScript
'use strict';
|
||
|
||
module.exports = app => {
|
||
const { INTEGER, BIGINT, STRING, DATE, TINYINT, JSON: JSON_TYPE } = app.Sequelize;
|
||
|
||
const DEFAULT_SUBJECT_CONFIG = Object.freeze({
|
||
express_bind_printer: Object.freeze([]),
|
||
});
|
||
|
||
/**
|
||
* 规范化快递绑定打印机配置。
|
||
*
|
||
* @param {Array|null|undefined|string} rawList 原始快递绑定打印机配置。
|
||
* @return {Array<Object>} 规范化后的快递绑定打印机配置。
|
||
*/
|
||
const normalizeExpressBindPrinter = rawList => {
|
||
if (rawList === undefined || rawList === null || rawList === '') {
|
||
return [];
|
||
}
|
||
|
||
if (!Array.isArray(rawList)) {
|
||
throw new Error('subject_config.express_bind_printer 必须是数组');
|
||
}
|
||
|
||
return rawList.map((item, index) => {
|
||
const basePath = `subject_config.express_bind_printer[${index}]`;
|
||
if (!item || typeof item !== 'object' || Array.isArray(item)) {
|
||
throw new Error(`${basePath} 必须是对象`);
|
||
}
|
||
|
||
const printerName = String(item.printer_name === undefined || item.printer_name === null ? '' : item.printer_name).trim();
|
||
if (!printerName) {
|
||
throw new Error(`${basePath}.printer_name 不能为空`);
|
||
}
|
||
|
||
const expressCode = String(item.express_code === undefined || item.express_code === null ? '' : item.express_code).trim();
|
||
if (!expressCode) {
|
||
throw new Error(`${basePath}.express_code 不能为空`);
|
||
}
|
||
|
||
return {
|
||
printer_name: printerName,
|
||
express_code: expressCode,
|
||
};
|
||
});
|
||
};
|
||
|
||
/**
|
||
* 规范化打印台主体配置。
|
||
*
|
||
* @param {Object|string|null|undefined} rawValue 原始主体配置。
|
||
* @return {Object} 规范化后的主体配置。
|
||
*/
|
||
const normalizeSubjectConfig = rawValue => {
|
||
if (rawValue === undefined || rawValue === null || rawValue === '') {
|
||
return {
|
||
...DEFAULT_SUBJECT_CONFIG,
|
||
express_bind_printer: [],
|
||
};
|
||
}
|
||
|
||
let parsed = rawValue;
|
||
if (typeof parsed === 'string') {
|
||
try {
|
||
parsed = JSON.parse(parsed);
|
||
} catch (error) {
|
||
throw new Error('subject_config 必须是合法 JSON 对象');
|
||
}
|
||
}
|
||
|
||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
||
throw new Error('subject_config 必须是对象');
|
||
}
|
||
|
||
return {
|
||
...parsed,
|
||
express_bind_printer: normalizeExpressBindPrinter(parsed.express_bind_printer),
|
||
};
|
||
};
|
||
|
||
const PrintStations = app.model.define('print_stations', {
|
||
id: {
|
||
type: INTEGER.UNSIGNED,
|
||
autoIncrement: true,
|
||
primaryKey: true,
|
||
comment: '自增主键ID',
|
||
},
|
||
warehouse_id: {
|
||
type: BIGINT.UNSIGNED,
|
||
allowNull: false,
|
||
comment: '仓库ID',
|
||
},
|
||
default_printer_name: {
|
||
type: STRING(50),
|
||
allowNull: true,
|
||
comment: '默认打印机名称',
|
||
},
|
||
name: {
|
||
type: STRING(100),
|
||
allowNull: false,
|
||
comment: '打印台名称',
|
||
},
|
||
computer_id: {
|
||
type: INTEGER,
|
||
allowNull: false,
|
||
comment: '打印电脑ID',
|
||
},
|
||
custom_barcode: {
|
||
type: STRING(100),
|
||
allowNull: true,
|
||
comment: '自定义编码',
|
||
},
|
||
creator_id: {
|
||
type: BIGINT.UNSIGNED,
|
||
allowNull: false,
|
||
comment: '创建人工号ID',
|
||
},
|
||
creator_name: {
|
||
type: STRING(50),
|
||
allowNull: false,
|
||
comment: '创建人名称',
|
||
},
|
||
create_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
comment: '创建时间',
|
||
},
|
||
update_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
comment: '更新时间',
|
||
},
|
||
deleted: {
|
||
type: TINYINT,
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '软删除标记:0-未删除,1-已删除',
|
||
},
|
||
subject_config: {
|
||
type: JSON_TYPE,
|
||
allowNull: true,
|
||
defaultValue: { ...DEFAULT_SUBJECT_CONFIG, express_bind_printer: [] },
|
||
comment: '打印台主体配置',
|
||
get() {
|
||
return normalizeSubjectConfig(this.getDataValue('subject_config'));
|
||
},
|
||
set(value) {
|
||
this.setDataValue('subject_config', normalizeSubjectConfig(value));
|
||
},
|
||
validate: {
|
||
isValidSubjectConfig(value) {
|
||
normalizeSubjectConfig(value);
|
||
},
|
||
},
|
||
},
|
||
}, {
|
||
tableName: 'wms_print_stations',
|
||
freezeTableName: true,
|
||
underscored: false,
|
||
timestamps: false,
|
||
indexes: [
|
||
{
|
||
unique: true,
|
||
name: 'uk_print_station_id',
|
||
fields: [ 'print_station_id' ],
|
||
},
|
||
{
|
||
name: 'idx_warehouse_id',
|
||
fields: [ 'warehouse_id' ],
|
||
},
|
||
{
|
||
name: 'idx_computer_id',
|
||
fields: [ 'computer_id' ],
|
||
},
|
||
{
|
||
name: 'idx_name',
|
||
fields: [ 'name' ],
|
||
},
|
||
{
|
||
name: 'idx_custom_barcode',
|
||
fields: [ 'custom_barcode' ],
|
||
},
|
||
{
|
||
name: 'idx_creator_id',
|
||
fields: [ 'creator_id' ],
|
||
},
|
||
{
|
||
name: 'idx_create_at',
|
||
fields: [ 'create_at' ],
|
||
},
|
||
],
|
||
comment: '打印台信息表',
|
||
});
|
||
|
||
PrintStations.DEFAULT_SUBJECT_CONFIG = DEFAULT_SUBJECT_CONFIG;
|
||
PrintStations.normalizeSubjectConfig = normalizeSubjectConfig;
|
||
|
||
return PrintStations;
|
||
};
|