WMS_MAIN/app/model/store_warehouse.js

265 lines
6.1 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.

'use strict';
module.exports = app => {
const { BIGINT, TEXT, STRING, DATE, TINYINT, INTEGER } = app.Sequelize;
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: '仓库备注',
},
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 } = 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',
});
StoreWarehouse.hasMany(StockWarehouse, {
foreignKey: 'store_id',
targetKey: 'store_id',
as: 'store_stock',
});
// 解除人
StoreWarehouse.belongsTo(Account, {
foreignKey: 'ended_by',
targetKey: 'id',
as: 'end_user',
});
StoreWarehouse.hasMany(Receiving, {
foreignKey: 'store_id',
as: 'receiving',
});
};
// 实例方法 - 检查授权是否有效
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;
};