七月迭代短暂完结,还有很多后端的关键东西没有弄,这是一版赶时间做的东西
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m26s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m26s
This commit is contained in:
@@ -4,12 +4,14 @@ package customer_binding
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -144,6 +146,72 @@ func (s *Service) OwnsAsset(ctx context.Context, customerID uint, assetType stri
|
||||
return false, errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
// ActiveCustomerIDsByAsset 返回当前启用且关联指定资产的个人客户 ID,结果去重并稳定排序。
|
||||
func (s *Service) ActiveCustomerIDsByAsset(ctx context.Context, tx *gorm.DB, assetType string, assetID uint) ([]uint, error) {
|
||||
if tx == nil {
|
||||
tx = s.db
|
||||
}
|
||||
customerIDs := make(map[uint]struct{})
|
||||
switch normalizeAssetType(assetType) {
|
||||
case assetTypeIotCard:
|
||||
card, err := s.readCard(ctx, tx, assetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if card.VirtualNo != "" {
|
||||
records, err := s.makePCD(tx).GetByDeviceNo(ctx, card.VirtualNo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询卡关联客户失败")
|
||||
}
|
||||
collectActiveDeviceCustomerIDs(customerIDs, records)
|
||||
} else {
|
||||
records, err := s.makePCI(tx).GetByICCID(ctx, card.ICCID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询卡关联客户失败")
|
||||
}
|
||||
collectActiveCardCustomerIDs(customerIDs, records)
|
||||
}
|
||||
case assetTypeDevice:
|
||||
device, err := s.readDevice(ctx, tx, assetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
identifier := device.VirtualNo
|
||||
if identifier == "" {
|
||||
identifier = device.IMEI
|
||||
}
|
||||
records, err := s.makePCD(tx).GetByDeviceNo(ctx, identifier)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询设备关联客户失败")
|
||||
}
|
||||
collectActiveDeviceCustomerIDs(customerIDs, records)
|
||||
default:
|
||||
return nil, errors.New(errors.CodeInvalidParam, "无效的资产类型")
|
||||
}
|
||||
result := make([]uint, 0, len(customerIDs))
|
||||
for customerID := range customerIDs {
|
||||
result = append(result, customerID)
|
||||
}
|
||||
sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func collectActiveDeviceCustomerIDs(target map[uint]struct{}, records []*model.PersonalCustomerDevice) {
|
||||
for _, record := range records {
|
||||
if record != nil && record.Status == constants.StatusEnabled && record.CustomerID > 0 {
|
||||
target[record.CustomerID] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func collectActiveCardCustomerIDs(target map[uint]struct{}, records []*model.PersonalCustomerICCID) {
|
||||
for _, record := range records {
|
||||
if record != nil && record.Status == constants.StatusEnabled && record.CustomerID > 0 {
|
||||
target[record.CustomerID] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bind 在事务中创建客户与资产的绑定关系
|
||||
// 有虚拟号的 IoT 卡 / 设备 → tb_personal_customer_device
|
||||
// 无虚拟号的 IoT 卡 → tb_personal_customer_iccid(Issue 02)
|
||||
|
||||
Reference in New Issue
Block a user