fix: 第1批 F1、M12 一资产一号与换货按手机号关联校验
- F1 一个资产同时只能有一个有效手机号关联,他号绑定返回 1192(不含号码),同号幂等 - 登录 need_bind_phone 改看资产是否已有有效关联,新增 asset_phone_bound、asset_phone_is_own;H5 绑定按客户/资产两侧状态分别写入 - 客户换号不再迁移资产关联 - M12 换货新资产有有效手机号关联即拒绝,号码只在 data.phone;换货完成清空旧资产客户绑定并失效旧资产关联(exchange_completed),均不迁移 - OpenSpec Change enforce-single-phone-per-asset:11/16,H5 短信与换货端到端验证待测试部署后完成
This commit is contained in:
@@ -98,58 +98,6 @@ func deviceAuditChange(device *model.Device, relation, role string, beforeData,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) loadAuditAssets(ctx context.Context, tx *gorm.DB, oldType string, oldID uint, newType string, newID uint) ([]accessauditapp.IotCardChange, []accessauditapp.DeviceChange, error) {
|
||||
cards := make([]accessauditapp.IotCardChange, 0, 2)
|
||||
devices := make([]accessauditapp.DeviceChange, 0, 2)
|
||||
appendAsset := func(assetType string, assetID uint, role string) error {
|
||||
switch normalizeAssetType(assetType) {
|
||||
case assetTypeIotCard:
|
||||
card, err := s.readCard(ctx, tx, assetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cards = append(cards, cardAuditChange(card, constants.AuditResourceRelationAffected, role, nil, nil))
|
||||
case assetTypeDevice:
|
||||
device, err := s.readDevice(ctx, tx, assetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
devices = append(devices, deviceAuditChange(device, constants.AuditResourceRelationAffected, role, nil, nil))
|
||||
default:
|
||||
return errors.New(errors.CodeInvalidParam, "无效的资产类型")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := appendAsset(oldType, oldID, constants.AuditResourceRolePersonalCustomerOldAsset); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := appendAsset(newType, newID, constants.AuditResourceRolePersonalCustomerNewAsset); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return cards, devices, nil
|
||||
}
|
||||
|
||||
func (s *Service) writeMigrationAudit(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
customerID uint,
|
||||
oldType string,
|
||||
oldID uint,
|
||||
newType string,
|
||||
newID uint,
|
||||
personalDevices []accessauditapp.PersonalCustomerDeviceChange,
|
||||
personalICCIDs []accessauditapp.PersonalCustomerICCIDChange,
|
||||
) error {
|
||||
cards, devices, err := s.loadAuditAssets(ctx, tx, oldType, oldID, newType, newID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.writeBindingAudit(
|
||||
ctx, tx, constants.AuditActionPersonalCustomerAssetBindingMigrated, "换货迁移个人客户资产绑定",
|
||||
customerID, personalDevices, personalICCIDs, cards, devices,
|
||||
)
|
||||
}
|
||||
|
||||
// UnbindByVirtualNo 按现有换货重置语义删除设备号绑定,并在同一事务记录实际删除关系。
|
||||
func (s *Service) UnbindByVirtualNo(ctx context.Context, tx *gorm.DB, assetType string, assetID uint, virtualNo string) error {
|
||||
if s.accessAudit == nil {
|
||||
|
||||
@@ -349,273 +349,103 @@ func (s *Service) bindViaPCI(ctx context.Context, pci pciOps, tx *gorm.DB, custo
|
||||
}, nil)
|
||||
}
|
||||
|
||||
// Migrate 将旧资产的所有有效客户绑定迁移到新资产(换货专用)
|
||||
// 无绑定时静默跳过;按旧/新资产虚拟号有无路由到 pcd 或 pci
|
||||
func (s *Service) Migrate(ctx context.Context, tx *gorm.DB, oldAssetType string, oldAssetID uint, newAssetType string, newAssetID uint) error {
|
||||
// ClearForExchange 换货完成时在同一事务内把旧资产的全部有效个人客户绑定置为无效,不迁移到新资产。
|
||||
// 定位口径同原换货迁移:卡的虚拟号绑定与 ICCID 绑定两边都清;设备按虚拟号,无虚拟号时按 IMEI。
|
||||
// 原客户之后重新扫码旧资产仍可按既有登录规则重新建立绑定。
|
||||
func (s *Service) ClearForExchange(ctx context.Context, tx *gorm.DB, oldAssetType string, oldAssetID uint) error {
|
||||
if s.accessAudit == nil {
|
||||
return errors.New(errors.CodeInvalidStatus, "个人客户资产审计接缝未配置")
|
||||
}
|
||||
if tx == nil {
|
||||
tx = s.db
|
||||
}
|
||||
pcd := s.makePCD(tx)
|
||||
pci := s.makePCI(tx)
|
||||
|
||||
switch normalizeAssetType(oldAssetType) {
|
||||
case assetTypeIotCard:
|
||||
oldCard, err := s.readCard(ctx, tx, oldAssetID)
|
||||
card, err := s.readCard(ctx, tx, oldAssetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if oldCard.VirtualNo != "" {
|
||||
return s.migrateFromPCD(ctx, tx, pcd, pci, oldCard.VirtualNo, oldAssetType, oldAssetID, newAssetType, newAssetID)
|
||||
assetChange := []accessauditapp.IotCardChange{cardAuditChange(card, constants.AuditResourceRelationAffected, constants.AuditResourceRolePersonalCustomerOldAsset, nil, nil)}
|
||||
if card.VirtualNo != "" {
|
||||
if err := s.clearDeviceBindings(ctx, tx, card.VirtualNo, assetChange, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return s.migrateFromPCI(ctx, tx, pcd, pci, oldCard.ICCID, oldAssetType, oldAssetID, newAssetType, newAssetID)
|
||||
|
||||
return s.clearICCIDBindings(ctx, tx, card.ICCID, assetChange)
|
||||
case assetTypeDevice:
|
||||
oldDevice, err := s.readDevice(ctx, tx, oldAssetID)
|
||||
device, err := s.readDevice(ctx, tx, oldAssetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := oldDevice.VirtualNo
|
||||
key := device.VirtualNo
|
||||
if key == "" {
|
||||
key = oldDevice.IMEI
|
||||
key = device.IMEI
|
||||
}
|
||||
return s.migrateFromPCD(ctx, tx, pcd, pci, key, oldAssetType, oldAssetID, newAssetType, newAssetID)
|
||||
if key == "" {
|
||||
return nil
|
||||
}
|
||||
assetChange := []accessauditapp.DeviceChange{deviceAuditChange(device, constants.AuditResourceRelationAffected, constants.AuditResourceRolePersonalCustomerOldAsset, nil, nil)}
|
||||
return s.clearDeviceBindings(ctx, tx, key, nil, assetChange)
|
||||
}
|
||||
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
// migrateFromPCD 将 tb_personal_customer_device 中 oldKey 的所有有效绑定迁移到新资产
|
||||
func (s *Service) migrateFromPCD(ctx context.Context, db *gorm.DB, pcd pcdOps, pci pciOps, oldKey string, oldAssetType string, oldAssetID uint, newAssetType string, newAssetID uint) error {
|
||||
records, err := pcd.GetByDeviceNo(ctx, oldKey)
|
||||
// clearDeviceBindings 把按虚拟号(或 IMEI)定位的有效绑定置为无效并逐条写审计。
|
||||
func (s *Service) clearDeviceBindings(ctx context.Context, tx *gorm.DB, key string, cards []accessauditapp.IotCardChange, devices []accessauditapp.DeviceChange) error {
|
||||
pcd := s.makePCD(tx)
|
||||
records, err := pcd.GetByDeviceNo(ctx, key)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "查询旧资产绑定记录失败")
|
||||
return errors.Wrap(errors.CodeInternalError, err, "查询旧资产客户绑定失败")
|
||||
}
|
||||
|
||||
// 仅处理 status=1 的有效记录
|
||||
var active []*model.PersonalCustomerDevice
|
||||
for _, r := range records {
|
||||
if r.Status == 1 {
|
||||
active = append(active, r)
|
||||
for _, record := range records {
|
||||
if record == nil || record.Status != 1 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if len(active) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch normalizeAssetType(newAssetType) {
|
||||
case assetTypeIotCard:
|
||||
newCard, err := s.readCard(ctx, db, newAssetID)
|
||||
if err != nil {
|
||||
if err := pcd.UpdateStatus(ctx, record.ID, 0); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "清空旧资产客户绑定失败")
|
||||
}
|
||||
if err := s.writeBindingAudit(
|
||||
ctx, tx, constants.AuditActionPersonalCustomerAssetUnbound, "换货完成清空旧资产个人客户绑定", record.CustomerID,
|
||||
[]accessauditapp.PersonalCustomerDeviceChange{{
|
||||
Binding: record, Role: constants.AuditResourceRolePersonalCustomerOldAssetBinding,
|
||||
BeforeData: map[string]any{"virtual_no": record.VirtualNo, "status": record.Status},
|
||||
AfterData: map[string]any{"virtual_no": record.VirtualNo, "status": 0},
|
||||
}}, nil, cards, devices,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if newCard.VirtualNo != "" {
|
||||
// 新卡有虚拟号:更新 pcd virtual_no
|
||||
for _, rec := range active {
|
||||
if err := pcd.UpdateVirtualNo(ctx, rec.ID, newCard.VirtualNo); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "迁移客户绑定关系失败")
|
||||
}
|
||||
after := *rec
|
||||
after.VirtualNo = newCard.VirtualNo
|
||||
if err := s.writeMigrationAudit(ctx, db, rec.CustomerID, oldAssetType, oldAssetID, newAssetType, newAssetID,
|
||||
[]accessauditapp.PersonalCustomerDeviceChange{{
|
||||
Binding: &after, Role: constants.AuditResourceRolePersonalCustomerAssetBinding,
|
||||
BeforeData: map[string]any{"virtual_no": rec.VirtualNo, "status": rec.Status},
|
||||
AfterData: map[string]any{"virtual_no": after.VirtualNo, "status": after.Status},
|
||||
}}, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 新卡无虚拟号:禁用旧 pcd + 创建新 pci
|
||||
iccid19 := newCard.ICCID
|
||||
if len(newCard.ICCID) == 20 {
|
||||
iccid19 = newCard.ICCID[:19]
|
||||
}
|
||||
for _, rec := range active {
|
||||
if err := pcd.UpdateStatus(ctx, rec.ID, 0); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "禁用旧客户绑定关系失败")
|
||||
}
|
||||
newPCI := &model.PersonalCustomerICCID{
|
||||
CustomerID: rec.CustomerID,
|
||||
ICCID: newCard.ICCID,
|
||||
ICCID19: iccid19,
|
||||
Status: 1,
|
||||
}
|
||||
if err := pci.Create(ctx, newPCI); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "创建新客户绑定关系失败")
|
||||
}
|
||||
if err := s.writeMigrationAudit(ctx, db, rec.CustomerID, oldAssetType, oldAssetID, newAssetType, newAssetID,
|
||||
[]accessauditapp.PersonalCustomerDeviceChange{{
|
||||
Binding: rec, Role: constants.AuditResourceRolePersonalCustomerOldAssetBinding,
|
||||
BeforeData: map[string]any{"virtual_no": rec.VirtualNo, "status": rec.Status},
|
||||
AfterData: map[string]any{"virtual_no": rec.VirtualNo, "status": 0},
|
||||
}}, []accessauditapp.PersonalCustomerICCIDChange{{
|
||||
Binding: newPCI, Role: constants.AuditResourceRolePersonalCustomerNewAssetBinding,
|
||||
AfterData: map[string]any{"iccid": newPCI.ICCID, "status": newPCI.Status},
|
||||
}}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case assetTypeDevice:
|
||||
newDevice, err := s.readDevice(ctx, db, newAssetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newKey := newDevice.VirtualNo
|
||||
if newKey == "" {
|
||||
newKey = newDevice.IMEI
|
||||
}
|
||||
for _, rec := range active {
|
||||
if err := pcd.UpdateVirtualNo(ctx, rec.ID, newKey); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "迁移设备客户绑定关系失败")
|
||||
}
|
||||
after := *rec
|
||||
after.VirtualNo = newKey
|
||||
if err := s.writeMigrationAudit(ctx, db, rec.CustomerID, oldAssetType, oldAssetID, newAssetType, newAssetID,
|
||||
[]accessauditapp.PersonalCustomerDeviceChange{{
|
||||
Binding: &after, Role: constants.AuditResourceRolePersonalCustomerAssetBinding,
|
||||
BeforeData: map[string]any{"virtual_no": rec.VirtualNo, "status": rec.Status},
|
||||
AfterData: map[string]any{"virtual_no": after.VirtualNo, "status": after.Status},
|
||||
}}, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// migrateFromPCI 将 tb_personal_customer_iccid 中 oldICCID 的所有有效绑定迁移到新资产
|
||||
func (s *Service) migrateFromPCI(ctx context.Context, db *gorm.DB, pcd pcdOps, pci pciOps, oldICCID string, oldAssetType string, oldAssetID uint, newAssetType string, newAssetID uint) error {
|
||||
records, err := pci.GetByICCID(ctx, oldICCID)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "查询旧 ICCID 绑定记录失败")
|
||||
}
|
||||
|
||||
var active []*model.PersonalCustomerICCID
|
||||
for _, r := range records {
|
||||
if r.Status == 1 {
|
||||
active = append(active, r)
|
||||
}
|
||||
}
|
||||
if len(active) == 0 {
|
||||
// clearICCIDBindings 把按 ICCID 定位的有效绑定置为无效并逐条写审计。
|
||||
func (s *Service) clearICCIDBindings(ctx context.Context, tx *gorm.DB, iccid string, cards []accessauditapp.IotCardChange) error {
|
||||
if iccid == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch normalizeAssetType(newAssetType) {
|
||||
case assetTypeIotCard:
|
||||
newCard, err := s.readCard(ctx, db, newAssetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if newCard.VirtualNo != "" {
|
||||
// 新卡有虚拟号:禁用旧 pci + 创建新 pcd
|
||||
for _, rec := range active {
|
||||
if err := pci.UpdateStatus(ctx, rec.ID, 0); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "禁用旧 ICCID 绑定关系失败")
|
||||
}
|
||||
newPCD := &model.PersonalCustomerDevice{
|
||||
CustomerID: rec.CustomerID,
|
||||
VirtualNo: newCard.VirtualNo,
|
||||
Status: 1,
|
||||
}
|
||||
if err := pcd.Create(ctx, newPCD); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "创建新客户绑定关系失败")
|
||||
}
|
||||
if err := s.writeMigrationAudit(ctx, db, rec.CustomerID, oldAssetType, oldAssetID, newAssetType, newAssetID,
|
||||
[]accessauditapp.PersonalCustomerDeviceChange{{
|
||||
Binding: newPCD, Role: constants.AuditResourceRolePersonalCustomerNewAssetBinding,
|
||||
AfterData: map[string]any{"virtual_no": newPCD.VirtualNo, "status": newPCD.Status},
|
||||
}}, []accessauditapp.PersonalCustomerICCIDChange{{
|
||||
Binding: rec, Role: constants.AuditResourceRolePersonalCustomerOldAssetBinding,
|
||||
BeforeData: map[string]any{"iccid": rec.ICCID, "status": rec.Status},
|
||||
AfterData: map[string]any{"iccid": rec.ICCID, "status": 0},
|
||||
}}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 新卡也无虚拟号:禁用旧 pci + 创建新 pci(新 ICCID)
|
||||
iccid19 := newCard.ICCID
|
||||
if len(newCard.ICCID) == 20 {
|
||||
iccid19 = newCard.ICCID[:19]
|
||||
}
|
||||
for _, rec := range active {
|
||||
if err := pci.UpdateStatus(ctx, rec.ID, 0); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "禁用旧 ICCID 绑定关系失败")
|
||||
}
|
||||
newPCI := &model.PersonalCustomerICCID{
|
||||
CustomerID: rec.CustomerID,
|
||||
ICCID: newCard.ICCID,
|
||||
ICCID19: iccid19,
|
||||
Status: 1,
|
||||
}
|
||||
if err := pci.Create(ctx, newPCI); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "创建新 ICCID 绑定关系失败")
|
||||
}
|
||||
if err := s.writeMigrationAudit(ctx, db, rec.CustomerID, oldAssetType, oldAssetID, newAssetType, newAssetID,
|
||||
nil, []accessauditapp.PersonalCustomerICCIDChange{
|
||||
{
|
||||
Binding: rec, Role: constants.AuditResourceRolePersonalCustomerOldAssetBinding,
|
||||
BeforeData: map[string]any{"iccid": rec.ICCID, "status": rec.Status},
|
||||
AfterData: map[string]any{"iccid": rec.ICCID, "status": 0},
|
||||
},
|
||||
{
|
||||
Binding: newPCI, Role: constants.AuditResourceRolePersonalCustomerNewAssetBinding,
|
||||
AfterData: map[string]any{"iccid": newPCI.ICCID, "status": newPCI.Status},
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case assetTypeDevice:
|
||||
newDevice, err := s.readDevice(ctx, db, newAssetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newKey := newDevice.VirtualNo
|
||||
if newKey == "" {
|
||||
newKey = newDevice.IMEI
|
||||
}
|
||||
for _, rec := range active {
|
||||
if err := pci.UpdateStatus(ctx, rec.ID, 0); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "禁用旧 ICCID 绑定关系失败")
|
||||
}
|
||||
newPCD := &model.PersonalCustomerDevice{
|
||||
CustomerID: rec.CustomerID,
|
||||
VirtualNo: newKey,
|
||||
Status: 1,
|
||||
}
|
||||
if err := pcd.Create(ctx, newPCD); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "创建新客户绑定关系失败")
|
||||
}
|
||||
if err := s.writeMigrationAudit(ctx, db, rec.CustomerID, oldAssetType, oldAssetID, newAssetType, newAssetID,
|
||||
[]accessauditapp.PersonalCustomerDeviceChange{{
|
||||
Binding: newPCD, Role: constants.AuditResourceRolePersonalCustomerNewAssetBinding,
|
||||
AfterData: map[string]any{"virtual_no": newPCD.VirtualNo, "status": newPCD.Status},
|
||||
}}, []accessauditapp.PersonalCustomerICCIDChange{{
|
||||
Binding: rec, Role: constants.AuditResourceRolePersonalCustomerOldAssetBinding,
|
||||
BeforeData: map[string]any{"iccid": rec.ICCID, "status": rec.Status},
|
||||
AfterData: map[string]any{"iccid": rec.ICCID, "status": 0},
|
||||
}}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
pci := s.makePCI(tx)
|
||||
records, err := pci.GetByICCID(ctx, iccid)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "查询旧 ICCID 客户绑定失败")
|
||||
}
|
||||
for _, record := range records {
|
||||
if record == nil || record.Status != 1 {
|
||||
continue
|
||||
}
|
||||
if err := pci.UpdateStatus(ctx, record.ID, 0); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "清空旧 ICCID 客户绑定失败")
|
||||
}
|
||||
if err := s.writeBindingAudit(
|
||||
ctx, tx, constants.AuditActionPersonalCustomerAssetUnbound, "换货完成清空旧资产个人客户绑定", record.CustomerID,
|
||||
nil, []accessauditapp.PersonalCustomerICCIDChange{{
|
||||
Binding: record, Role: constants.AuditResourceRolePersonalCustomerOldAssetBinding,
|
||||
BeforeData: map[string]any{"iccid": record.ICCID, "status": record.Status},
|
||||
AfterData: map[string]any{"iccid": record.ICCID, "status": 0},
|
||||
}}, cards, nil,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user