77 lines
1.6 KiB
JavaScript
77 lines
1.6 KiB
JavaScript
const { DataTypes } = require('sequelize');
|
||
const sequelize = require('../config/database');
|
||
|
||
const SmartEarmark = sequelize.define('SmartEarmark', {
|
||
id: {
|
||
type: DataTypes.INTEGER,
|
||
primaryKey: true,
|
||
autoIncrement: true
|
||
},
|
||
earmark_id: {
|
||
type: DataTypes.STRING,
|
||
allowNull: false,
|
||
unique: true,
|
||
comment: '耳标编号'
|
||
},
|
||
name: {
|
||
type: DataTypes.STRING,
|
||
allowNull: false,
|
||
comment: '耳标名称'
|
||
},
|
||
status: {
|
||
type: DataTypes.ENUM('active', 'inactive', 'maintenance'),
|
||
allowNull: false,
|
||
defaultValue: 'inactive',
|
||
comment: '状态:active-使用中,inactive-未使用,maintenance-维护中'
|
||
},
|
||
battery: {
|
||
type: DataTypes.INTEGER,
|
||
allowNull: false,
|
||
defaultValue: 100,
|
||
validate: {
|
||
min: 0,
|
||
max: 100
|
||
},
|
||
comment: '电池电量(%)'
|
||
},
|
||
remark: {
|
||
type: DataTypes.TEXT,
|
||
allowNull: true,
|
||
comment: '备注'
|
||
},
|
||
created_at: {
|
||
type: DataTypes.DATE,
|
||
allowNull: false,
|
||
defaultValue: DataTypes.NOW,
|
||
comment: '创建时间'
|
||
},
|
||
updated_at: {
|
||
type: DataTypes.DATE,
|
||
allowNull: false,
|
||
defaultValue: DataTypes.NOW,
|
||
comment: '更新时间'
|
||
}
|
||
}, {
|
||
tableName: 'smart_earmarks',
|
||
timestamps: true,
|
||
createdAt: 'created_at',
|
||
updatedAt: 'updated_at',
|
||
paranoid: false,
|
||
indexes: [
|
||
{
|
||
name: 'idx_smart_earmark_id',
|
||
fields: ['earmark_id']
|
||
},
|
||
{
|
||
name: 'idx_smart_earmark_status',
|
||
fields: ['status']
|
||
}
|
||
]
|
||
});
|
||
|
||
// 钩子函数,在保存前更新更新时间
|
||
SmartEarmark.beforeSave((earmark) => {
|
||
earmark.updated_at = new Date();
|
||
});
|
||
|
||
module.exports = SmartEarmark; |