From 42b884aaf8383d511bbe75757716e6922a024e98 Mon Sep 17 00:00:00 2001 From: huang Date: Sat, 28 Mar 2026 11:31:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(03-01):=20DB=20=E8=BF=81=E7=A7=BB=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E8=AE=BE=E5=A4=87=E5=AD=97=E6=AE=B5=20+=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增迁移 000090:tb_device 扩展 5 个 Gateway sync-info 字段(online_status / last_online_time / software_version / switch_mode / last_gateway_sync_at) - 新增迁移 000091:tb_device_sim_binding 新增 is_current 字段(当前使用卡标识) - 更新 Device 模型新增对应 5 个字段 - 更新 DeviceSimBinding 模型新增 IsCurrent 字段 --- internal/model/device.go | 6 ++++++ internal/model/device_sim_binding.go | 1 + migrations/000090_device_fields_extension.down.sql | 6 ++++++ migrations/000090_device_fields_extension.up.sql | 14 ++++++++++++++ .../000091_device_sim_binding_is_current.down.sql | 2 ++ .../000091_device_sim_binding_is_current.up.sql | 5 +++++ 6 files changed, 34 insertions(+) create mode 100644 migrations/000090_device_fields_extension.down.sql create mode 100644 migrations/000090_device_fields_extension.up.sql create mode 100644 migrations/000091_device_sim_binding_is_current.down.sql create mode 100644 migrations/000091_device_sim_binding_is_current.up.sql diff --git a/internal/model/device.go b/internal/model/device.go index ef21183..e9b3d7b 100644 --- a/internal/model/device.go +++ b/internal/model/device.go @@ -37,6 +37,12 @@ type Device struct { FirstRechargeTriggeredBySeriesJSON string `gorm:"column:first_recharge_triggered_by_series;type:jsonb;default:'{}';comment:按套餐系列追踪的首充触发状态" json:"-"` AssetStatus int `gorm:"column:asset_status;type:int;not null;default:1;comment:业务状态 1-在库 2-已销售 3-已换货 4-已停用" json:"asset_status"` Generation int `gorm:"column:generation;type:int;not null;default:1;comment:资产世代编号" json:"generation"` + // 以下字段由 Gateway sync-info 接口同步,有离线存储意义 + OnlineStatus int `gorm:"column:online_status;type:int;not null;default:0;comment:在线状态:0未知 1在线 2离线" json:"online_status"` + LastOnlineTime *time.Time `gorm:"column:last_online_time;comment:最后在线时间(来自 Gateway sync-info)" json:"last_online_time,omitempty"` + SoftwareVersion string `gorm:"column:software_version;type:varchar(100);not null;default:'';comment:固件版本号" json:"software_version"` + SwitchMode string `gorm:"column:switch_mode;type:varchar(10);not null;default:'0';comment:切卡模式:0=自动 1=手动" json:"switch_mode"` + LastGatewaySyncAt *time.Time `gorm:"column:last_gateway_sync_at;comment:最后一次 sync-info 同步时间" json:"last_gateway_sync_at,omitempty"` } // TableName 指定表名 diff --git a/internal/model/device_sim_binding.go b/internal/model/device_sim_binding.go index 7610157..dc13f6c 100644 --- a/internal/model/device_sim_binding.go +++ b/internal/model/device_sim_binding.go @@ -17,6 +17,7 @@ type DeviceSimBinding struct { BindStatus int `gorm:"column:bind_status;type:int;default:1;comment:绑定状态 1-已绑定 2-已解绑" json:"bind_status"` BindTime *time.Time `gorm:"column:bind_time;comment:绑定时间" json:"bind_time"` UnbindTime *time.Time `gorm:"column:unbind_time;comment:解绑时间" json:"unbind_time"` + IsCurrent bool `gorm:"column:is_current;type:boolean;not null;default:false;comment:是否为当前使用的卡(同一设备同一时刻最多一条为 true)" json:"is_current"` } func (DeviceSimBinding) TableName() string { diff --git a/migrations/000090_device_fields_extension.down.sql b/migrations/000090_device_fields_extension.down.sql new file mode 100644 index 0000000..4262cbc --- /dev/null +++ b/migrations/000090_device_fields_extension.down.sql @@ -0,0 +1,6 @@ +ALTER TABLE tb_device + DROP COLUMN IF EXISTS online_status, + DROP COLUMN IF EXISTS last_online_time, + DROP COLUMN IF EXISTS software_version, + DROP COLUMN IF EXISTS switch_mode, + DROP COLUMN IF EXISTS last_gateway_sync_at; diff --git a/migrations/000090_device_fields_extension.up.sql b/migrations/000090_device_fields_extension.up.sql new file mode 100644 index 0000000..8f10d0e --- /dev/null +++ b/migrations/000090_device_fields_extension.up.sql @@ -0,0 +1,14 @@ +-- 扩展 tb_device 设备表,新增 Gateway sync-info 同步的存储字段 +-- 这些字段有"离线意义"(设备下线后仍需保留最后状态),非实时字段(rssi/battery 等)不入库 +ALTER TABLE tb_device + ADD COLUMN online_status INT NOT NULL DEFAULT 0, + ADD COLUMN last_online_time TIMESTAMP NULL, + ADD COLUMN software_version VARCHAR(100) NOT NULL DEFAULT '', + ADD COLUMN switch_mode VARCHAR(10) NOT NULL DEFAULT '0', + ADD COLUMN last_gateway_sync_at TIMESTAMP NULL; + +COMMENT ON COLUMN tb_device.online_status IS '在线状态:0未知,1在线,2离线'; +COMMENT ON COLUMN tb_device.last_online_time IS '最后在线时间(来自 Gateway sync-info)'; +COMMENT ON COLUMN tb_device.software_version IS '固件版本号'; +COMMENT ON COLUMN tb_device.switch_mode IS '切卡模式:0=自动,1=手动'; +COMMENT ON COLUMN tb_device.last_gateway_sync_at IS '最后一次 sync-info 同步时间'; diff --git a/migrations/000091_device_sim_binding_is_current.down.sql b/migrations/000091_device_sim_binding_is_current.down.sql new file mode 100644 index 0000000..e79c7b4 --- /dev/null +++ b/migrations/000091_device_sim_binding_is_current.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE tb_device_sim_binding + DROP COLUMN IF EXISTS is_current; diff --git a/migrations/000091_device_sim_binding_is_current.up.sql b/migrations/000091_device_sim_binding_is_current.up.sql new file mode 100644 index 0000000..cce4aab --- /dev/null +++ b/migrations/000091_device_sim_binding_is_current.up.sql @@ -0,0 +1,5 @@ +-- 新增 is_current 字段,标识设备当前正在使用的卡(由 sync-info current_iccid 字段决定) +ALTER TABLE tb_device_sim_binding + ADD COLUMN is_current BOOLEAN NOT NULL DEFAULT FALSE; + +COMMENT ON COLUMN tb_device_sim_binding.is_current IS '是否为当前使用的卡(同一设备同一时刻最多一条为 true)';