当前卡的逻辑变更
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m53s

This commit is contained in:
2026-04-24 09:25:44 +08:00
parent a44c88005d
commit ba73913a41
3 changed files with 21 additions and 41 deletions

View File

@@ -222,6 +222,7 @@ type SyncDeviceInfoResp struct {
DeviceName FlexString `json:"device_name" description:"设备名称"`
IMEI FlexString `json:"imei" description:"IMEI号"`
CurrentIccid FlexString `json:"current_iccid" description:"当前使用的ICCID"`
CurrentSlotNo FlexInt `json:"current_slot_number" description:"当前使用卡槽号逻辑槽位1开始"`
DeviceType FlexString `json:"device_type" description:"设备类型1=诺行2=玺龙3=迎势达"`
SoftwareVersion FlexString `json:"software_version" description:"软件版本号"`
MacAddress FlexString `json:"mac_address" description:"MAC地址"`

View File

@@ -611,13 +611,12 @@ func (s *Service) updateDeviceFromSyncInfo(ctx context.Context, deviceID uint, r
return
}
if string(resp.CurrentIccid) != "" {
// 更新 is_current 标识事务原子操作per D-14 决策)
if err := s.deviceSimBindingStore.UpdateIsCurrentByDeviceID(ctx, deviceID, string(resp.CurrentIccid)); err != nil {
logger.GetAppLogger().Warn("sync-info 更新 is_current 失败",
zap.Uint("device_id", deviceID),
zap.Error(err))
}
// 更新 is_current 标识(事务原子操作,按 current_slot_number 逻辑槽位更新)
if err := s.deviceSimBindingStore.UpdateIsCurrentByDeviceID(ctx, deviceID, int(resp.CurrentSlotNo)); err != nil {
logger.GetAppLogger().Warn("sync-info 更新 is_current 失败",
zap.Uint("device_id", deviceID),
zap.Int("current_slot_number", int(resp.CurrentSlotNo)),
zap.Error(err))
}
}

View File

@@ -229,9 +229,9 @@ func (s *DeviceSimBindingStore) GetBoundICCIDs(ctx context.Context, iccids []str
}
// UpdateIsCurrentByDeviceID 原子更新设备当前使用的卡标识
// 使用事务两步操作:先全部设为 false再将 currentIccid 对应设为 true
// currentIccid 为空字符串时,仅执行全部清空(无当前卡的情况)
func (s *DeviceSimBindingStore) UpdateIsCurrentByDeviceID(ctx context.Context, deviceID uint, currentIccid string) error {
// 使用事务两步操作:先全部设为 false再将 currentSlotNumber 对应插槽设为 true
// currentSlotNumber <= 0 时,仅执行全部清空(无当前卡的情况)
func (s *DeviceSimBindingStore) UpdateIsCurrentByDeviceID(ctx context.Context, deviceID uint, currentSlotNumber int) error {
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&model.DeviceSimBinding{}).
Where("device_id = ? AND bind_status = 1", deviceID).
@@ -239,42 +239,22 @@ func (s *DeviceSimBindingStore) UpdateIsCurrentByDeviceID(ctx context.Context, d
return err
}
if currentIccid == "" {
if currentSlotNumber <= 0 {
return nil
}
var column string
switch len(currentIccid) {
case 19:
column = "iccid_19"
case 20:
column = "iccid_20"
default:
logger.GetAppLogger().Error("UpdateIsCurrentByDeviceID: ICCID 长度异常,跳过 is_current 更新",
zap.String("iccid", currentIccid),
result := tx.Model(&model.DeviceSimBinding{}).
Where("device_id = ? AND slot_position = ? AND bind_status = 1", deviceID, currentSlotNumber).
Update("is_current", true)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
logger.GetAppLogger().Warn("UpdateIsCurrentByDeviceID: 未命中插槽绑定记录,is_current 更新",
zap.Uint("device_id", deviceID),
zap.Int("length", len(currentIccid)),
zap.Int("current_slot_number", currentSlotNumber),
)
return nil
}
var iotCardID uint
if err := tx.Table("tb_iot_card").
Select("id").
Where(column+" = ? AND deleted_at IS NULL", currentIccid).
Scan(&iotCardID).Error; err != nil {
return err
}
if iotCardID == 0 {
logger.GetAppLogger().Warn("UpdateIsCurrentByDeviceID: 未命中卡记录is_current 不更新",
zap.String("iccid", currentIccid),
zap.Uint("device_id", deviceID),
zap.String("column_used", column),
)
return nil
}
return tx.Model(&model.DeviceSimBinding{}).
Where("device_id = ? AND iot_card_id = ? AND bind_status = 1", deviceID, iotCardID).
Update("is_current", true).Error
return nil
})
}