WMS_MAIN/app/model/shop.js
liufumingren dc4894cb6d 1
2026-05-25 17:08:56 +08:00

157 lines
3.5 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';
/**
* 店铺模型定义。
* @param {import('egg').Application} app Egg 应用实例。
* @return {import('sequelize').ModelCtor<import('sequelize').Model>} 店铺模型。
*/
module.exports = app => {
const { BIGINT, STRING, DATE, TINYINT, JSON } = app.Sequelize;
const Shop = app.model.define('shops', {
id: {
type: BIGINT.UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
warehouse_id: {
type: BIGINT.UNSIGNED,
allowNull: false,
comment: '仓库ID',
},
platform_code: {
type: STRING(50),
allowNull: false,
comment: '平台编码',
},
shop_name: {
type: STRING(100),
allowNull: false,
comment: '店铺名称',
},
custom_name: {
type: STRING(100),
allowNull: true,
comment: '店铺自定义名称',
},
status: {
type: TINYINT(1),
allowNull: false,
defaultValue: 0,
validate: {
isIn: {
args: [[ 0, 1 ]],
msg: '状态值不合法,0=未授权,1=已授权',
},
},
comment: '授权状态:0=未授权,1=已授权',
},
auth_expire_at: {
type: DATE,
allowNull: true,
comment: '授权到期时间',
},
auth_random_code: {
type: STRING(128),
allowNull: true,
comment: '授权随机码',
},
owner_id: {
type: STRING(64),
allowNull: true,
comment: '平台店铺/主体ID',
},
access_token: {
type: STRING(255),
allowNull: true,
comment: '访问令牌',
},
refresh_token: {
type: STRING(255),
allowNull: true,
comment: '刷新令牌',
},
token_expires_at: {
type: DATE,
allowNull: true,
comment: '访问令牌过期时间',
},
refresh_token_expires_at: {
type: DATE,
allowNull: true,
comment: '刷新令牌过期时间',
},
scope: {
type: JSON,
allowNull: true,
comment: '授权范围',
},
created_at: {
type: DATE,
allowNull: false,
comment: '创建时间',
},
updated_at: {
type: DATE,
allowNull: false,
comment: '更新时间',
},
}, {
tableName: 'wms_shops',
freezeTableName: true,
underscored: true,
timestamps: true,
paranoid: false,
createdAt: 'created_at',
updatedAt: 'updated_at',
comment: '店铺表',
indexes: [
{
name: 'idx_warehouse_id',
fields: [ 'warehouse_id' ],
},
{
name: 'idx_platform_code',
fields: [ 'platform_code' ],
},
{
name: 'idx_status',
fields: [ 'status' ],
},
{
name: 'uk_warehouse_platform_shop_name',
unique: true,
fields: [ 'warehouse_id', 'platform_code', 'shop_name' ],
},
{
name: 'uk_auth_random_code',
unique: true,
fields: [ 'auth_random_code' ],
},
],
});
/**
* 定义店铺与打印平台的关联关系。
*
* 说明:
* - `wms_shops.platform_code` 对应 `wms_print_platform.platform_code`
*/
Shop.associate = function() {
const { PrintPlatform } = app.model;
if (PrintPlatform && typeof Shop.belongsTo === 'function') {
Shop.belongsTo(PrintPlatform, {
foreignKey: 'platform_code',
targetKey: 'platform_code',
as: 'printPlatform',
constraints: false,
});
}
};
return Shop;
};