Files
yunrui_asset/backend/models/Consumable.js
T
zhang1106 7f1df28858 feat: 实现ID生成器并重构模型ID生成逻辑
refactor: 统一ID生成方式,使用新的idGenerator模块
refactor: 重构模型ID生成逻辑,允许空ID并在创建前自动生成
fix(roomSchema): 使机房ID字段变为可选并更新前端表单验证
style: 格式化代码并优化导入语句
test: 添加操作日志集成测试文件
docs: 添加错误处理模块文档
2026-04-02 10:34:13 +08:00

94 lines
2.0 KiB
JavaScript

const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const { generateId } = require('../utils/idGenerator');
const Consumable = sequelize.define(
'Consumable',
{
consumableId: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
category: {
type: DataTypes.STRING,
allowNull: false,
},
unit: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: '个',
},
currentStock: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
},
minStock: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 10,
},
maxStock: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
comment: '最大库存,0表示无限制',
},
unitPrice: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false,
defaultValue: 0,
},
supplier: {
type: DataTypes.STRING,
},
location: {
type: DataTypes.STRING,
comment: '存放位置',
},
description: {
type: DataTypes.TEXT,
},
snList: {
type: DataTypes.JSON,
allowNull: true,
defaultValue: [],
comment: 'SN序列号列表,JSON数组格式',
},
status: {
type: DataTypes.STRING,
defaultValue: 'active',
},
version: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
comment: '乐观锁版本号',
},
},
{
tableName: 'consumables',
timestamps: true,
indexes: [
{ fields: ['category'] },
{ fields: ['status'] },
{ fields: ['category', 'status'] },
{ fields: ['updatedAt'] },
],
hooks: {
beforeCreate: (consumable) => {
if (!consumable.consumableId) {
consumable.consumableId = generateId({ prefix: 'CON' });
}
},
},
}
);
module.exports = Consumable;