This commit is contained in:
@@ -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
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user