'use strict'; /** * 扫码记录表模型(scan_records) * * 中控通过 POST /api/scan/records 上报扫码流水:条码 + 扫描时间 + 通道 id。 * 去重靠唯一键 (scanner_id, channel_id, barcode, scan_time):完全相同的上报只入库一次。 * * 本表是流水表,**不用软删除**(paranoid 关闭): * 软删除会让记录占用唯一键,导致同一条扫码数据再也无法入库; * 需要清理请按时间物理删除。 */ module.exports = (app) => { const { BIGINT, STRING, DATE } = app.Sequelize; const ScanRecord = app.model.define('scanRecord', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, scannerId: { type: BIGINT.UNSIGNED, allowNull: false, comment: '上报的中控(= scanners.id)' }, channelId: { type: STRING(64), allowNull: false, comment: '通道 id' }, barcode: { type: STRING(255), allowNull: false, comment: '条码内容' }, scanTime: { type: DATE, allowNull: false, comment: '扫描时间(中控上报,秒级)' }, reportedAt: { type: DATE, allowNull: false, defaultValue: app.Sequelize.NOW, comment: '服务端接收时间' }, }, { tableName: 'scan_records', paranoid: false, comment: '扫码记录表(去重)', }); ScanRecord.associate = function() { app.model.ScanRecord.belongsTo(app.model.Scanner, { foreignKey: 'scanner_id', as: 'scanner' }); }; return ScanRecord; };