553 lines
14 KiB
JavaScript
553 lines
14 KiB
JavaScript
'use strict';
|
||
|
||
module.exports = app => {
|
||
const { BIGINT, TEXT, STRING, DATE, TINYINT, INTEGER, JSON: JSON_TYPE } = app.Sequelize;
|
||
|
||
const POLICY_CONFIG_FIELDS = Object.freeze([
|
||
Object.freeze({
|
||
key: 'receive_mode',
|
||
name: '收货模式',
|
||
type: 'enum',
|
||
default: 'standard',
|
||
required: false,
|
||
description: '控制货主在该仓下的收货校验模式',
|
||
options: [ 'standard', 'strict', 'manual' ],
|
||
}),
|
||
Object.freeze({
|
||
key: 'sku_visibility',
|
||
name: 'SKU 可见范围',
|
||
type: 'enum',
|
||
default: 'all',
|
||
required: false,
|
||
description: '控制货主在该仓下的 SKU 可见策略',
|
||
options: [ 'all', 'whitelist', 'blacklist' ],
|
||
}),
|
||
Object.freeze({
|
||
key: 'auto_allocate_bin',
|
||
name: '自动分配库位',
|
||
type: 'flag',
|
||
default: 0,
|
||
required: false,
|
||
description: '收货上架时是否自动推荐库位',
|
||
}),
|
||
Object.freeze({
|
||
key: 'allow_mix_batch',
|
||
name: '允许混批',
|
||
type: 'flag',
|
||
default: 0,
|
||
required: false,
|
||
description: '是否允许不同批次混合存放',
|
||
}),
|
||
Object.freeze({
|
||
key: 'allow_temp_storage',
|
||
name: '允许临时存放',
|
||
type: 'flag',
|
||
default: 0,
|
||
required: false,
|
||
description: '是否允许进入临时存放区',
|
||
}),
|
||
Object.freeze({
|
||
key: 'print_recharge_price',
|
||
name: '面单充值元/单',
|
||
type: 'number',
|
||
default: 0,
|
||
required: false,
|
||
description: '面单充值元/单,支持 2 位小数',
|
||
min: 0,
|
||
max: 1000000,
|
||
precision: 2,
|
||
step: 0.01,
|
||
}),
|
||
Object.freeze({
|
||
key: 'max_daily_inbound',
|
||
name: '单日最大收货量',
|
||
type: 'integer',
|
||
default: 0,
|
||
required: false,
|
||
description: '限制该货主在仓库内的日收货上限',
|
||
min: 0,
|
||
max: 1000000,
|
||
}),
|
||
Object.freeze({
|
||
key: 'remark',
|
||
name: '策略备注',
|
||
type: 'string',
|
||
default: '',
|
||
required: false,
|
||
description: '补充说明该货主在仓库内的特殊约束',
|
||
maxLength: 200,
|
||
}),
|
||
]);
|
||
const POLICY_CONFIG_FIELD_MAP = Object.freeze(POLICY_CONFIG_FIELDS.reduce((result, field) => {
|
||
result[field.key] = field;
|
||
return result;
|
||
}, {}));
|
||
|
||
const clonePolicyConfigField = field => ({
|
||
key: field.key,
|
||
name: field.name,
|
||
type: field.type,
|
||
default: field.default,
|
||
required: field.required,
|
||
description: field.description,
|
||
...(Array.isArray(field.options) ? { options: [ ...field.options ] } : {}),
|
||
...(field.min !== undefined ? { min: field.min } : {}),
|
||
...(field.max !== undefined ? { max: field.max } : {}),
|
||
...(field.precision !== undefined ? { precision: field.precision } : {}),
|
||
...(field.step !== undefined ? { step: field.step } : {}),
|
||
...(field.maxLength !== undefined ? { maxLength: field.maxLength } : {}),
|
||
});
|
||
|
||
const parsePolicyConfig = (value, fieldName = 'policy_config') => {
|
||
if (typeof value === 'string') {
|
||
try {
|
||
return JSON.parse(value);
|
||
} catch (error) {
|
||
throw new Error(`${fieldName} 不是合法JSON`);
|
||
}
|
||
}
|
||
return value;
|
||
};
|
||
|
||
const normalizePolicyConfig = (value, options = {}) => {
|
||
const {
|
||
fieldName = 'policy_config',
|
||
allowEmpty = false,
|
||
sanitizeOnly = false,
|
||
} = options;
|
||
|
||
if (value === undefined || value === null || value === '') {
|
||
if (allowEmpty) {
|
||
return null;
|
||
}
|
||
throw new Error(`${fieldName} 参数不能为空`);
|
||
}
|
||
|
||
const parsed = parsePolicyConfig(value, fieldName);
|
||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
||
throw new Error(`${fieldName} 必须为对象`);
|
||
}
|
||
|
||
const supportedKeys = Object.keys(POLICY_CONFIG_FIELD_MAP);
|
||
const unsupportedKeys = Object.keys(parsed).filter(key => !POLICY_CONFIG_FIELD_MAP[key]);
|
||
if (unsupportedKeys.length && !sanitizeOnly) {
|
||
throw new Error(`${fieldName} 包含不支持字段: ${unsupportedKeys.join(', ')};仅允许字段: ${supportedKeys.join(', ')}`);
|
||
}
|
||
|
||
const normalized = {};
|
||
for (const field of POLICY_CONFIG_FIELDS) {
|
||
if (!Object.prototype.hasOwnProperty.call(parsed, field.key)) {
|
||
continue;
|
||
}
|
||
|
||
const rawValue = parsed[field.key];
|
||
if (rawValue === undefined || rawValue === null || rawValue === '') {
|
||
if (field.type === 'string') {
|
||
normalized[field.key] = '';
|
||
}
|
||
continue;
|
||
}
|
||
|
||
try {
|
||
if (field.type === 'enum') {
|
||
const valueText = String(rawValue).trim();
|
||
if (!field.options.includes(valueText)) {
|
||
throw new Error(`${fieldName}.${field.key} 参数不合法`);
|
||
}
|
||
normalized[field.key] = valueText;
|
||
continue;
|
||
}
|
||
|
||
if (field.type === 'flag') {
|
||
if (rawValue === true || rawValue === 1 || rawValue === '1') {
|
||
normalized[field.key] = 1;
|
||
continue;
|
||
}
|
||
if (rawValue === false || rawValue === 0 || rawValue === '0') {
|
||
normalized[field.key] = 0;
|
||
continue;
|
||
}
|
||
throw new Error(`${fieldName}.${field.key} 参数不合法`);
|
||
}
|
||
|
||
if (field.type === 'integer') {
|
||
const num = Number(rawValue);
|
||
if (!Number.isFinite(num) || num < field.min || num > field.max) {
|
||
throw new Error(`${fieldName}.${field.key} 参数不合法`);
|
||
}
|
||
normalized[field.key] = Math.floor(num);
|
||
continue;
|
||
}
|
||
|
||
if (field.type === 'number') {
|
||
const num = Number(rawValue);
|
||
if (!Number.isFinite(num) || num < field.min || num > field.max) {
|
||
throw new Error(`${fieldName}.${field.key} 参数不合法`);
|
||
}
|
||
const precision = Number.isInteger(field.precision) ? field.precision : 2;
|
||
const factor = 10 ** precision;
|
||
normalized[field.key] = Math.round(num * factor) / factor;
|
||
continue;
|
||
}
|
||
|
||
if (field.type === 'string') {
|
||
const text = String(rawValue).trim();
|
||
if (text.length > field.maxLength) {
|
||
throw new Error(`${fieldName}.${field.key} 长度不能超过 ${field.maxLength}`);
|
||
}
|
||
normalized[field.key] = text;
|
||
}
|
||
} catch (error) {
|
||
if (!sanitizeOnly) {
|
||
throw error;
|
||
}
|
||
}
|
||
}
|
||
|
||
return Object.keys(normalized).length ? normalized : null;
|
||
};
|
||
|
||
const StoreWarehouse = app.model.define('store_warehouse', {
|
||
id: {
|
||
type: BIGINT(20),
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '授权ID',
|
||
},
|
||
store_id: {
|
||
type: BIGINT(20),
|
||
allowNull: false,
|
||
comment: '商家ID',
|
||
references: {
|
||
model: 'store',
|
||
key: 'id',
|
||
},
|
||
},
|
||
warehouse_id: {
|
||
type: BIGINT(20),
|
||
allowNull: false,
|
||
comment: '仓库ID',
|
||
references: {
|
||
model: 'store',
|
||
key: 'id',
|
||
},
|
||
},
|
||
auth_status: {
|
||
type: TINYINT(4),
|
||
allowNull: false,
|
||
defaultValue: 1,
|
||
validate: {
|
||
isIn: [[ 1, 2, 3, 4, 5 ]], // 1-申请中,2-已通过,3-已拒绝,4-已解除,5-已过期
|
||
},
|
||
comment: '授权状态:1-申请中,2-已通过,3-已拒绝,4-已解除,5-已过期',
|
||
},
|
||
apply_reason: {
|
||
type: TEXT,
|
||
allowNull: true,
|
||
comment: '申请理由',
|
||
},
|
||
review_comment: {
|
||
type: TEXT,
|
||
allowNull: true,
|
||
comment: '审核意见',
|
||
},
|
||
apply_time: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
comment: '申请时间',
|
||
},
|
||
print_quantity: {
|
||
type: BIGINT(12),
|
||
allowNull: false,
|
||
comment: '面单余额',
|
||
},
|
||
review_time: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '审核时间',
|
||
},
|
||
auth_start: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '授权开始日期',
|
||
},
|
||
auth_end: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '授权结束日期',
|
||
},
|
||
actual_end: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '实际结束时间',
|
||
},
|
||
applied_by: {
|
||
type: BIGINT(20),
|
||
allowNull: false,
|
||
comment: '申请人',
|
||
},
|
||
reviewed_by: {
|
||
type: BIGINT(20),
|
||
allowNull: true,
|
||
comment: '审核人',
|
||
},
|
||
ended_by: {
|
||
type: BIGINT(20),
|
||
allowNull: true,
|
||
comment: '解除人',
|
||
},
|
||
created_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: DATE,
|
||
allowNull: false,
|
||
comment: '更新时间',
|
||
},
|
||
store_remask: {
|
||
type: STRING(50),
|
||
allowNull: true,
|
||
validate: {
|
||
len: [ 0, 50 ],
|
||
},
|
||
comment: '商家备注',
|
||
},
|
||
warehouse_remask: {
|
||
type: STRING(50),
|
||
allowNull: true,
|
||
validate: {
|
||
len: [ 0, 50 ],
|
||
},
|
||
comment: '仓库备注',
|
||
},
|
||
policy_config: {
|
||
type: JSON_TYPE,
|
||
allowNull: true,
|
||
defaultValue: null,
|
||
comment: '货主在仓库下的固定策略配置(JSON)',
|
||
get() {
|
||
return normalizePolicyConfig(this.getDataValue('policy_config'), {
|
||
fieldName: 'policy_config',
|
||
allowEmpty: true,
|
||
sanitizeOnly: true,
|
||
});
|
||
},
|
||
set(value) {
|
||
this.setDataValue('policy_config', normalizePolicyConfig(value, {
|
||
fieldName: 'policy_config',
|
||
allowEmpty: true,
|
||
}));
|
||
},
|
||
},
|
||
use_status: {
|
||
type: INTEGER(1),
|
||
allowNull: false,
|
||
comment: '使用状态,0-不可用,1-可用(SKU可见,货主可见,即使解约了)',
|
||
},
|
||
}, {
|
||
tableName: 'wms_store_warehouse',
|
||
freezeTableName: true,
|
||
underscored: true,
|
||
timestamps: true,
|
||
createdAt: 'created_at',
|
||
updatedAt: 'updated_at',
|
||
comment: '商家仓库授权关系表',
|
||
indexes: [
|
||
{
|
||
name: 'uk_store_warehouse',
|
||
unique: true,
|
||
fields: [ 'store_id', 'warehouse_id' ],
|
||
},
|
||
{
|
||
name: 'idx_store_id',
|
||
fields: [ 'store_id' ],
|
||
},
|
||
{
|
||
name: 'idx_warehouse_id',
|
||
fields: [ 'warehouse_id' ],
|
||
},
|
||
{
|
||
name: 'idx_auth_status',
|
||
fields: [ 'auth_status' ],
|
||
},
|
||
{
|
||
name: 'idx_apply_time',
|
||
fields: [ 'apply_time' ],
|
||
},
|
||
{
|
||
name: 'idx_auth_period',
|
||
fields: [ 'auth_start', 'auth_end' ],
|
||
},
|
||
{
|
||
name: 'idx_applied_by',
|
||
fields: [ 'applied_by' ],
|
||
},
|
||
{
|
||
name: 'idx_reviewed_by',
|
||
fields: [ 'reviewed_by' ],
|
||
},
|
||
{
|
||
name: 'idx_auth_end',
|
||
fields: [ 'auth_end' ],
|
||
},
|
||
{
|
||
name: 'idx_status_apply_time',
|
||
fields: [ 'auth_status', 'apply_time' ],
|
||
},
|
||
],
|
||
});
|
||
|
||
// 定义关联关系
|
||
StoreWarehouse.associate = function() {
|
||
const { Store, Account, StockWarehouse, Receiving, StoreWarehousePrintQuantityLog } = app.model;
|
||
|
||
// 授权关系属于商家
|
||
StoreWarehouse.belongsTo(Store, {
|
||
foreignKey: 'store_id',
|
||
targetKey: 'id',
|
||
as: 'store',
|
||
});
|
||
|
||
// 授权关系属于仓库
|
||
StoreWarehouse.belongsTo(Store, {
|
||
foreignKey: 'warehouse_id',
|
||
targetKey: 'id',
|
||
as: 'warehouse',
|
||
});
|
||
|
||
// 申请人
|
||
StoreWarehouse.belongsTo(Account, {
|
||
foreignKey: 'applied_by',
|
||
targetKey: 'id',
|
||
as: 'applicant',
|
||
});
|
||
|
||
// 审核人
|
||
StoreWarehouse.belongsTo(Account, {
|
||
foreignKey: 'reviewed_by',
|
||
targetKey: 'id',
|
||
as: 'reviewer',
|
||
});
|
||
|
||
// store_id 在业务上需要与 warehouse_id 联合约束,模型层不声明单列外键约束。
|
||
StoreWarehouse.hasMany(StockWarehouse, {
|
||
foreignKey: 'store_id',
|
||
targetKey: 'store_id',
|
||
as: 'store_stock',
|
||
constraints: false,
|
||
});
|
||
|
||
// 解除人
|
||
StoreWarehouse.belongsTo(Account, {
|
||
foreignKey: 'ended_by',
|
||
targetKey: 'id',
|
||
as: 'end_user',
|
||
});
|
||
// receiving 同样依赖 store_id + warehouse_id 的复合语义。
|
||
StoreWarehouse.hasMany(Receiving, {
|
||
foreignKey: 'store_id',
|
||
as: 'receiving',
|
||
constraints: false,
|
||
});
|
||
|
||
StoreWarehouse.hasMany(StoreWarehousePrintQuantityLog, {
|
||
foreignKey: 'contract_id',
|
||
sourceKey: 'id',
|
||
as: 'print_quantity_logs',
|
||
constraints: false,
|
||
});
|
||
};
|
||
|
||
StoreWarehouse.getPolicyConfigFieldList = function() {
|
||
return POLICY_CONFIG_FIELDS.map(clonePolicyConfigField);
|
||
};
|
||
|
||
StoreWarehouse.normalizePolicyConfig = function(value, options = {}) {
|
||
return normalizePolicyConfig(value, options);
|
||
};
|
||
|
||
// 统一构造复合键语义的 where 条件,避免 service 侧重复散落。
|
||
StoreWarehouse.buildScopedWhere = function(options = {}) {
|
||
const {
|
||
warehouseId,
|
||
storeId,
|
||
useStatus = 1,
|
||
} = options;
|
||
|
||
const where = {
|
||
use_status: useStatus,
|
||
};
|
||
|
||
if (warehouseId !== undefined && warehouseId !== null) {
|
||
where.warehouse_id = warehouseId;
|
||
}
|
||
|
||
if (storeId !== undefined && storeId !== null) {
|
||
where.store_id = storeId;
|
||
}
|
||
|
||
return where;
|
||
};
|
||
|
||
// 统一构造 StockWarehouse -> StoreWarehouse(include as=store_stock) 的 include。
|
||
StoreWarehouse.buildStoreStockInclude = function(options = {}) {
|
||
const {
|
||
warehouseId,
|
||
storeId,
|
||
required = false,
|
||
useStatus = 1,
|
||
attributes = [ 'store_id', 'store_remask' ],
|
||
} = options;
|
||
|
||
return {
|
||
model: this,
|
||
as: 'store_stock',
|
||
attributes,
|
||
where: this.buildScopedWhere({
|
||
warehouseId,
|
||
storeId,
|
||
useStatus,
|
||
}),
|
||
required,
|
||
};
|
||
};
|
||
|
||
// 实例方法 - 检查授权是否有效
|
||
StoreWarehouse.prototype.isAuthValid = function() {
|
||
if (this.auth_status !== 2) return false; // 只有已通过状态才有效
|
||
if (!this.auth_start || !this.auth_end) return false;
|
||
|
||
const now = new Date();
|
||
const start = new Date(this.auth_start);
|
||
const end = new Date(this.auth_end);
|
||
|
||
return now >= start && now <= end;
|
||
};
|
||
|
||
// 实例方法 - 检查授权是否即将过期(30天内)
|
||
StoreWarehouse.prototype.isExpiringSoon = function() {
|
||
if (this.auth_status !== 2 || !this.auth_end) return false;
|
||
|
||
const now = new Date();
|
||
const end = new Date(this.auth_end);
|
||
const daysUntilExpire = Math.ceil((end - now) / (1000 * 60 * 60 * 24));
|
||
|
||
return daysUntilExpire <= 30 && daysUntilExpire > 0;
|
||
};
|
||
|
||
// 实例方法 - 获取授权剩余天数
|
||
StoreWarehouse.prototype.getRemainingDays = function() {
|
||
if (!this.isAuthValid()) return 0;
|
||
|
||
const now = new Date();
|
||
const end = new Date(this.auth_end);
|
||
const diffTime = end - now;
|
||
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
|
||
};
|
||
|
||
return StoreWarehouse;
|
||
};
|