packServer/backend/app/model/scan_record.js

40 lines
1.9 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';
/**
* 扫码记录表模型(scan_records)
*
* 中控通过 POST /api/scan/records 上报扫码流水:条码 + 扫描时间 + 通道 id。
* 去重靠唯一键 (scanner_id, channel_id, barcode, scan_time):完全相同的上报只入库一次。
*
* 本表是流水表,**不用软删除**(paranoid 关闭):
* 软删除会让记录占用唯一键,导致同一条扫码数据再也无法入库;
* 需要清理请按时间物理删除。
*/
module.exports = (app) => {
const { BIGINT, STRING, DATE, JSON: JSONType } = app.Sequelize;
const ScanRecord = app.model.define('scanRecord', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
subjectId: { type: BIGINT.UNSIGNED, comment: '所属主体 subjects.id(跟随中控)' },
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: '服务端接收时间' },
/** 扫码当时绑定的工作台(回放要按「当时」的配置拉流,不能按现在的) */
workstationId: { type: BIGINT.UNSIGNED, comment: '当时的工作台 workstations.id' },
workstationSnapshot: { type: JSONType, comment: '工作台快照(录像机/摄像头/布局/录制范围)' },
}, {
tableName: 'scan_records',
paranoid: false,
comment: '扫码记录表(去重)',
});
ScanRecord.associate = function() {
app.model.ScanRecord.belongsTo(app.model.Scanner, { foreignKey: 'scanner_id', as: 'scanner' });
};
return ScanRecord;
};