From d8d62469d684fe90604c7630e10bb7b3c005b95d Mon Sep 17 00:00:00 2001 From: yanweidong Date: Mon, 3 Aug 2026 14:47:11 +0800 Subject: [PATCH] refactor: split iot models by entity --- backend/api/internal/models/iot_command.go | 41 ------------------- .../api/internal/models/iot_device_message.go | 22 ++++++++++ backend/api/internal/models/iot_migrate.go | 9 ++++ backend/api/internal/models/iot_outbox.go | 21 ++++++++++ 4 files changed, 52 insertions(+), 41 deletions(-) create mode 100644 backend/api/internal/models/iot_device_message.go create mode 100644 backend/api/internal/models/iot_migrate.go create mode 100644 backend/api/internal/models/iot_outbox.go diff --git a/backend/api/internal/models/iot_command.go b/backend/api/internal/models/iot_command.go index 6c00f1f..c62b433 100644 --- a/backend/api/internal/models/iot_command.go +++ b/backend/api/internal/models/iot_command.go @@ -2,8 +2,6 @@ package models import ( "time" - - "git.apinb.com/bsm-sdk/core/database" ) // IotCommand 是设备下行命令事实;受理、投递与设备执行状态必须分离。 @@ -23,42 +21,3 @@ type IotCommand struct { } func (*IotCommand) TableName() string { return "iot_command" } - -// IotOutbox 保证命令事实与待投递事件在同一数据库事务提交。 -type IotOutbox struct { - ID uint64 `gorm:"column:id;primaryKey;autoIncrement;comment:数据库自增主键" json:"-"` // 数据库主键 - Identity string `gorm:"column:identity;type:varchar(36);not null;uniqueIndex;comment:Outbox业务标识" json:"identity"` // Outbox业务标识 - CommandIdentity string `gorm:"column:command_identity;type:varchar(36);not null;uniqueIndex;comment:设备命令业务标识" json:"command_identity"` // 命令业务标识 - EventType string `gorm:"column:event_type;type:varchar(64);not null;comment:事件类型" json:"event_type"` // 事件类型 - Payload string `gorm:"column:payload;type:text;not null;comment:待投递事件载荷" json:"payload"` // 事件载荷 - OutboxStatus string `gorm:"column:outbox_status;type:varchar(24);not null;index;comment:Outbox投递状态" json:"outbox_status"` // 投递状态 - Attempts int `gorm:"column:attempts;not null;default:0;comment:投递尝试次数" json:"attempts"` // 尝试次数 - AvailableAt time.Time `gorm:"column:available_at;type:timestamptz;not null;index;comment:下次可投递时间" json:"available_at"` // 可投递时间 - CreatedAt time.Time `gorm:"column:created_at;type:timestamptz;not null;index;comment:创建时间" json:"created_at"` // 创建时间 - UpdatedAt time.Time `gorm:"column:updated_at;type:timestamptz;not null;comment:更新时间" json:"updated_at"` // 更新时间 -} - -func (*IotOutbox) TableName() string { return "iot_outbox" } - -// IotDeviceMessage 保存原始上行/回执和服务端接收时间,原始事实不可由前端修改。 -type IotDeviceMessage struct { - ID uint64 `gorm:"column:id;primaryKey;autoIncrement;comment:数据库自增主键" json:"-"` // 数据库主键 - Identity string `gorm:"column:identity;type:varchar(36);not null;uniqueIndex;comment:上行消息业务标识" json:"identity"` // 上行消息标识 - DeviceID string `gorm:"column:device_id;type:varchar(16);not null;index;comment:厂商设备标识" json:"device_id"` // 厂商设备标识 - Topic string `gorm:"column:topic;type:varchar(255);not null;comment:接收消息主题" json:"topic"` // MQTT主题 - MessageType string `gorm:"column:message_type;type:varchar(32);not null;index;comment:上行或回执消息类型" json:"message_type"` // 消息类型 - PayloadHex string `gorm:"column:payload_hex;type:text;not null;comment:原始报文十六进制" json:"payload_hex"` // 原始报文 - DecodedFrame string `gorm:"column:decoded_frame;type:text;not null;comment:协议解析结果快照" json:"decoded_frame"` // 解析快照 - DeviceOccurredAt *time.Time `gorm:"column:device_occurred_at;type:timestamptz;comment:设备原始采集时间" json:"device_occurred_at"` // 设备采集时间 - ReceivedAt time.Time `gorm:"column:received_at;type:timestamptz;not null;index;comment:服务端接收时间" json:"received_at"` // 服务端接收时间 - CreatedAt time.Time `gorm:"column:created_at;type:timestamptz;not null;index;comment:创建时间" json:"created_at"` // 创建时间 - UpdatedAt time.Time `gorm:"column:updated_at;type:timestamptz;not null;comment:更新时间" json:"updated_at"` // 更新时间 -} - -func (*IotDeviceMessage) TableName() string { return "iot_device_message" } - -func init() { - database.AppendMigrate(&IotCommand{}) - database.AppendMigrate(&IotOutbox{}) - database.AppendMigrate(&IotDeviceMessage{}) -} diff --git a/backend/api/internal/models/iot_device_message.go b/backend/api/internal/models/iot_device_message.go new file mode 100644 index 0000000..7fe0727 --- /dev/null +++ b/backend/api/internal/models/iot_device_message.go @@ -0,0 +1,22 @@ +package models + +import ( + "time" +) + +// IotDeviceMessage 保存原始上行/回执和服务端接收时间,原始事实不可由前端修改。 +type IotDeviceMessage struct { + ID uint64 `gorm:"column:id;primaryKey;autoIncrement;comment:数据库自增主键" json:"-"` // 数据库主键 + Identity string `gorm:"column:identity;type:varchar(36);not null;uniqueIndex;comment:上行消息业务标识" json:"identity"` // 上行消息标识 + DeviceID string `gorm:"column:device_id;type:varchar(16);not null;index;comment:厂商设备标识" json:"device_id"` // 厂商设备标识 + Topic string `gorm:"column:topic;type:varchar(255);not null;comment:接收消息主题" json:"topic"` // MQTT主题 + MessageType string `gorm:"column:message_type;type:varchar(32);not null;index;comment:上行或回执消息类型" json:"message_type"` // 消息类型 + PayloadHex string `gorm:"column:payload_hex;type:text;not null;comment:原始报文十六进制" json:"payload_hex"` // 原始报文 + DecodedFrame string `gorm:"column:decoded_frame;type:text;not null;comment:协议解析结果快照" json:"decoded_frame"` // 解析快照 + DeviceOccurredAt *time.Time `gorm:"column:device_occurred_at;type:timestamptz;comment:设备原始采集时间" json:"device_occurred_at"` // 设备采集时间 + ReceivedAt time.Time `gorm:"column:received_at;type:timestamptz;not null;index;comment:服务端接收时间" json:"received_at"` // 服务端接收时间 + CreatedAt time.Time `gorm:"column:created_at;type:timestamptz;not null;index;comment:创建时间" json:"created_at"` // 创建时间 + UpdatedAt time.Time `gorm:"column:updated_at;type:timestamptz;not null;comment:更新时间" json:"updated_at"` // 更新时间 +} + +func (*IotDeviceMessage) TableName() string { return "iot_device_message" } diff --git a/backend/api/internal/models/iot_migrate.go b/backend/api/internal/models/iot_migrate.go new file mode 100644 index 0000000..17b914c --- /dev/null +++ b/backend/api/internal/models/iot_migrate.go @@ -0,0 +1,9 @@ +package models + +import "git.apinb.com/bsm-sdk/core/database" + +func init() { + database.AppendMigrate(&IotCommand{}) + database.AppendMigrate(&IotOutbox{}) + database.AppendMigrate(&IotDeviceMessage{}) +} diff --git a/backend/api/internal/models/iot_outbox.go b/backend/api/internal/models/iot_outbox.go new file mode 100644 index 0000000..85f0c65 --- /dev/null +++ b/backend/api/internal/models/iot_outbox.go @@ -0,0 +1,21 @@ +package models + +import ( + "time" +) + +// IotOutbox 保证命令事实与待投递事件在同一数据库事务提交。 +type IotOutbox struct { + ID uint64 `gorm:"column:id;primaryKey;autoIncrement;comment:数据库自增主键" json:"-"` // 数据库主键 + Identity string `gorm:"column:identity;type:varchar(36);not null;uniqueIndex;comment:Outbox业务标识" json:"identity"` // Outbox业务标识 + CommandIdentity string `gorm:"column:command_identity;type:varchar(36);not null;uniqueIndex;comment:设备命令业务标识" json:"command_identity"` // 命令业务标识 + EventType string `gorm:"column:event_type;type:varchar(64);not null;comment:事件类型" json:"event_type"` // 事件类型 + Payload string `gorm:"column:payload;type:text;not null;comment:待投递事件载荷" json:"payload"` // 事件载荷 + OutboxStatus string `gorm:"column:outbox_status;type:varchar(24);not null;index;comment:Outbox投递状态" json:"outbox_status"` // 投递状态 + Attempts int `gorm:"column:attempts;not null;default:0;comment:投递尝试次数" json:"attempts"` // 尝试次数 + AvailableAt time.Time `gorm:"column:available_at;type:timestamptz;not null;index;comment:下次可投递时间" json:"available_at"` // 可投递时间 + CreatedAt time.Time `gorm:"column:created_at;type:timestamptz;not null;index;comment:创建时间" json:"created_at"` // 创建时间 + UpdatedAt time.Time `gorm:"column:updated_at;type:timestamptz;not null;comment:更新时间" json:"updated_at"` // 更新时间 +} + +func (*IotOutbox) TableName() string { return "iot_outbox" }