重构: 将卡/设备的套餐系列绑定从分配ID改为系列ID

- 数据库: 重命名 series_allocation_id → series_id
- Model: IotCard 和 Device 字段重命名
- DTO: 所有请求/响应字段统一为 series_id
- Store: 方法重命名,新增 GetByShopAndSeries 查询
- Service: 业务逻辑优化,系列验证和权限验证分离
- 测试: 更新所有测试用例,新增 shop_series_allocation_store_test.go
- 文档: 更新 API 文档说明参数变更

BREAKING CHANGE: API 参数从 series_allocation_id 改为 series_id
This commit is contained in:
2026-02-02 12:09:53 +08:00
parent a30b3036bb
commit 37f43d2e2d
27 changed files with 673 additions and 301 deletions

View File

@@ -107,9 +107,9 @@ func initServices(s *stores, deps *Dependencies) *services {
Authorization: enterpriseCardSvc.NewAuthorizationService(s.Enterprise, s.IotCard, s.EnterpriseCardAuthorization, deps.Logger),
CustomerAccount: customerAccountSvc.New(deps.DB, s.Account, s.Shop, s.Enterprise),
MyCommission: myCommissionSvc.New(deps.DB, s.Shop, s.Wallet, s.CommissionWithdrawalRequest, s.CommissionWithdrawalSetting, s.CommissionRecord, s.WalletTransaction),
IotCard: iotCardSvc.New(deps.DB, s.IotCard, s.Shop, s.AssetAllocationRecord, s.ShopSeriesAllocation, deps.GatewayClient, deps.Logger),
IotCard: iotCardSvc.New(deps.DB, s.IotCard, s.Shop, s.AssetAllocationRecord, s.ShopSeriesAllocation, s.PackageSeries, deps.GatewayClient, deps.Logger),
IotCardImport: iotCardImportSvc.New(deps.DB, s.IotCardImportTask, deps.QueueClient),
Device: deviceSvc.New(deps.DB, s.Device, s.DeviceSimBinding, s.IotCard, s.Shop, s.AssetAllocationRecord, s.ShopSeriesAllocation),
Device: deviceSvc.New(deps.DB, s.Device, s.DeviceSimBinding, s.IotCard, s.Shop, s.AssetAllocationRecord, s.ShopSeriesAllocation, s.PackageSeries),
DeviceImport: deviceImportSvc.New(deps.DB, s.DeviceImportTask, deps.QueueClient),
AssetAllocationRecord: assetAllocationRecordSvc.New(deps.DB, s.AssetAllocationRecord, s.Shop, s.Account),
Carrier: carrierSvc.New(s.Carrier),

View File

@@ -25,7 +25,7 @@ type Device struct {
DeviceUsername string `gorm:"column:device_username;type:varchar(100);comment:设备登录用户名" json:"device_username"`
DevicePasswordEncrypted string `gorm:"column:device_password_encrypted;type:varchar(255);comment:设备登录密码(加密)" json:"device_password_encrypted"`
DeviceAPIEndpoint string `gorm:"column:device_api_endpoint;type:varchar(500);comment:设备API端点" json:"device_api_endpoint"`
SeriesAllocationID *uint `gorm:"column:series_allocation_id;index;comment:套餐系列分配ID(关联ShopSeriesAllocation)" json:"series_allocation_id,omitempty"`
SeriesID *uint `gorm:"column:series_id;index;comment:套餐系列ID(关联PackageSeries)" json:"series_id,omitempty"`
FirstCommissionPaid bool `gorm:"column:first_commission_paid;type:boolean;default:false;comment:一次性佣金是否已发放" json:"first_commission_paid"`
AccumulatedRecharge int64 `gorm:"column:accumulated_recharge;type:bigint;default:0;comment:累计充值金额(分)" json:"accumulated_recharge"`
}

View File

@@ -3,18 +3,18 @@ package dto
import "time"
type ListDeviceRequest struct {
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
DeviceNo string `json:"device_no" query:"device_no" validate:"omitempty,max=100" maxLength:"100" description:"设备号(模糊查询)"`
DeviceName string `json:"device_name" query:"device_name" validate:"omitempty,max=255" maxLength:"255" description:"设备名称(模糊查询)"`
Status *int `json:"status" query:"status" validate:"omitempty,min=1,max=4" minimum:"1" maximum:"4" description:"状态 (1:在库, 2:已分销, 3:已激活, 4:已停用)"`
ShopID *uint `json:"shop_id" query:"shop_id" description:"店铺ID (NULL表示平台库存)"`
SeriesAllocationID *uint `json:"series_allocation_id" query:"series_allocation_id" description:"套餐系列分配ID"`
BatchNo string `json:"batch_no" query:"batch_no" validate:"omitempty,max=100" maxLength:"100" description:"批次号"`
DeviceType string `json:"device_type" query:"device_type" validate:"omitempty,max=50" maxLength:"50" description:"设备类型"`
Manufacturer string `json:"manufacturer" query:"manufacturer" validate:"omitempty,max=255" maxLength:"255" description:"制造商(模糊查询)"`
CreatedAtStart *time.Time `json:"created_at_start" query:"created_at_start" description:"创建时间起始"`
CreatedAtEnd *time.Time `json:"created_at_end" query:"created_at_end" description:"创建时间结束"`
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
DeviceNo string `json:"device_no" query:"device_no" validate:"omitempty,max=100" maxLength:"100" description:"设备号(模糊查询)"`
DeviceName string `json:"device_name" query:"device_name" validate:"omitempty,max=255" maxLength:"255" description:"设备名称(模糊查询)"`
Status *int `json:"status" query:"status" validate:"omitempty,min=1,max=4" minimum:"1" maximum:"4" description:"状态 (1:在库, 2:已分销, 3:已激活, 4:已停用)"`
ShopID *uint `json:"shop_id" query:"shop_id" description:"店铺ID (NULL表示平台库存)"`
SeriesID *uint `json:"series_id" query:"series_id" description:"套餐系列ID"`
BatchNo string `json:"batch_no" query:"batch_no" validate:"omitempty,max=100" maxLength:"100" description:"批次号"`
DeviceType string `json:"device_type" query:"device_type" validate:"omitempty,max=50" maxLength:"50" description:"设备类型"`
Manufacturer string `json:"manufacturer" query:"manufacturer" validate:"omitempty,max=255" maxLength:"255" description:"制造商(模糊查询)"`
CreatedAtStart *time.Time `json:"created_at_start" query:"created_at_start" description:"创建时间起始"`
CreatedAtEnd *time.Time `json:"created_at_end" query:"created_at_end" description:"创建时间结束"`
}
type DeviceResponse struct {
@@ -31,7 +31,7 @@ type DeviceResponse struct {
Status int `json:"status" description:"状态 (1:在库, 2:已分销, 3:已激活, 4:已停用)"`
StatusName string `json:"status_name" description:"状态名称"`
BoundCardCount int `json:"bound_card_count" description:"已绑定卡数量"`
SeriesAllocationID *uint `json:"series_allocation_id,omitempty" description:"套餐系列分配ID"`
SeriesID *uint `json:"series_id,omitempty" description:"套餐系列ID"`
FirstCommissionPaid bool `json:"first_commission_paid" description:"一次性佣金是否已发放"`
AccumulatedRecharge int64 `json:"accumulated_recharge" description:"累计充值金额(分)"`
ActivatedAt *time.Time `json:"activated_at,omitempty" description:"激活时间"`
@@ -129,8 +129,8 @@ type RecallDevicesResponse struct {
// BatchSetDeviceSeriesBindngRequest 批量设置设备的套餐系列绑定请求
type BatchSetDeviceSeriesBindngRequest struct {
DeviceIDs []uint `json:"device_ids" validate:"required,min=1,max=500,dive,required" required:"true" minItems:"1" maxItems:"500" description:"设备ID列表"`
SeriesAllocationID uint `json:"series_allocation_id" validate:"required,min=0" required:"true" minimum:"0" description:"套餐系列分配ID0表示清除关联"`
DeviceIDs []uint `json:"device_ids" validate:"required,min=1,max=500,dive,required" required:"true" minItems:"1" maxItems:"500" description:"设备ID列表"`
SeriesID uint `json:"series_id" validate:"required,min=0" required:"true" minimum:"0" description:"套餐系列ID0表示清除关联"`
}
// DeviceSeriesBindngFailedItem 设备系列绑定失败项

View File

@@ -3,20 +3,20 @@ package dto
import "time"
type ListStandaloneIotCardRequest struct {
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
Status *int `json:"status" query:"status" validate:"omitempty,min=1,max=4" minimum:"1" maximum:"4" description:"状态 (1:在库, 2:已分销, 3:已激活, 4:已停用)"`
CarrierID *uint `json:"carrier_id" query:"carrier_id" description:"运营商ID"`
ShopID *uint `json:"shop_id" query:"shop_id" description:"分销商ID"`
SeriesAllocationID *uint `json:"series_allocation_id" query:"series_allocation_id" description:"套餐系列分配ID"`
ICCID string `json:"iccid" query:"iccid" validate:"omitempty,max=20" maxLength:"20" description:"ICCID(模糊查询)"`
MSISDN string `json:"msisdn" query:"msisdn" validate:"omitempty,max=20" maxLength:"20" description:"卡接入号(模糊查询)"`
BatchNo string `json:"batch_no" query:"batch_no" validate:"omitempty,max=100" maxLength:"100" description:"批次号"`
PackageID *uint `json:"package_id" query:"package_id" description:"套餐ID"`
IsDistributed *bool `json:"is_distributed" query:"is_distributed" description:"是否已分销 (true:已分销, false:未分销)"`
IsReplaced *bool `json:"is_replaced" query:"is_replaced" description:"是否有换卡记录 (true:有换卡记录, false:无换卡记录)"`
ICCIDStart string `json:"iccid_start" query:"iccid_start" validate:"omitempty,max=20" maxLength:"20" description:"ICCID起始号"`
ICCIDEnd string `json:"iccid_end" query:"iccid_end" validate:"omitempty,max=20" maxLength:"20" description:"ICCID结束号"`
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
Status *int `json:"status" query:"status" validate:"omitempty,min=1,max=4" minimum:"1" maximum:"4" description:"状态 (1:在库, 2:已分销, 3:已激活, 4:已停用)"`
CarrierID *uint `json:"carrier_id" query:"carrier_id" description:"运营商ID"`
ShopID *uint `json:"shop_id" query:"shop_id" description:"分销商ID"`
SeriesID *uint `json:"series_id" query:"series_id" description:"套餐系列ID"`
ICCID string `json:"iccid" query:"iccid" validate:"omitempty,max=20" maxLength:"20" description:"ICCID(模糊查询)"`
MSISDN string `json:"msisdn" query:"msisdn" validate:"omitempty,max=20" maxLength:"20" description:"卡接入号(模糊查询)"`
BatchNo string `json:"batch_no" query:"batch_no" validate:"omitempty,max=100" maxLength:"100" description:"批次号"`
PackageID *uint `json:"package_id" query:"package_id" description:"套餐ID"`
IsDistributed *bool `json:"is_distributed" query:"is_distributed" description:"是否已分销 (true:已分销, false:未分销)"`
IsReplaced *bool `json:"is_replaced" query:"is_replaced" description:"是否有换卡记录 (true:有换卡记录, false:无换卡记录)"`
ICCIDStart string `json:"iccid_start" query:"iccid_start" validate:"omitempty,max=20" maxLength:"20" description:"ICCID起始号"`
ICCIDEnd string `json:"iccid_end" query:"iccid_end" validate:"omitempty,max=20" maxLength:"20" description:"ICCID结束号"`
}
type StandaloneIotCardResponse struct {
@@ -41,7 +41,7 @@ type StandaloneIotCardResponse struct {
RealNameStatus int `json:"real_name_status" description:"实名状态 (0:未实名, 1:已实名)"`
NetworkStatus int `json:"network_status" description:"网络状态 (0:停机, 1:开机)"`
DataUsageMB int64 `json:"data_usage_mb" description:"累计流量使用(MB)"`
SeriesAllocationID *uint `json:"series_allocation_id,omitempty" description:"套餐系列分配ID"`
SeriesID *uint `json:"series_id,omitempty" description:"套餐系列ID"`
FirstCommissionPaid bool `json:"first_commission_paid" description:"一次性佣金是否已发放"`
AccumulatedRecharge int64 `json:"accumulated_recharge" description:"累计充值金额(分)"`
CreatedAt time.Time `json:"created_at" description:"创建时间"`
@@ -133,8 +133,8 @@ type IotCardDetailResponse struct {
// BatchSetCardSeriesBindngRequest 批量设置卡的套餐系列绑定请求
type BatchSetCardSeriesBindngRequest struct {
ICCIDs []string `json:"iccids" validate:"required,min=1,max=500,dive,required" required:"true" minItems:"1" maxItems:"500" description:"ICCID列表"`
SeriesAllocationID uint `json:"series_allocation_id" validate:"required,min=0" required:"true" minimum:"0" description:"套餐系列分配ID0表示清除关联"`
ICCIDs []string `json:"iccids" validate:"required,min=1,max=500,dive,required" required:"true" minItems:"1" maxItems:"500" description:"ICCID列表"`
SeriesID uint `json:"series_id" validate:"required,min=0" required:"true" minimum:"0" description:"套餐系列ID0表示清除关联"`
}
// CardSeriesBindngFailedItem 卡系列绑定失败项

View File

@@ -35,7 +35,7 @@ type IotCard struct {
LastDataCheckAt *time.Time `gorm:"column:last_data_check_at;comment:最后一次流量检查时间" json:"last_data_check_at"`
LastRealNameCheckAt *time.Time `gorm:"column:last_real_name_check_at;comment:最后一次实名检查时间" json:"last_real_name_check_at"`
LastSyncTime *time.Time `gorm:"column:last_sync_time;comment:最后一次与Gateway同步时间" json:"last_sync_time"`
SeriesAllocationID *uint `gorm:"column:series_allocation_id;index;comment:套餐系列分配ID(关联ShopSeriesAllocation)" json:"series_allocation_id,omitempty"`
SeriesID *uint `gorm:"column:series_id;index;comment:套餐系列ID(关联PackageSeries)" json:"series_id,omitempty"`
FirstCommissionPaid bool `gorm:"column:first_commission_paid;type:boolean;default:false;comment:一次性佣金是否已发放" json:"first_commission_paid"`
AccumulatedRecharge int64 `gorm:"column:accumulated_recharge;type:bigint;default:0;comment:累计充值金额(分)" json:"accumulated_recharge"`
}

View File

@@ -137,7 +137,7 @@ func registerDeviceRoutes(router fiber.Router, handler *admin.DeviceHandler, imp
Register(devices, doc, groupPath, "PATCH", "/series-binding", handler.BatchSetSeriesBinding, RouteSpec{
Summary: "批量设置设备的套餐系列绑定",
Description: "批量设置或清除设备与套餐系列分配的关联关系。series_allocation_id 为 0 时表示清除关联。",
Description: "批量设置或清除设备与套餐系列分配的关联关系。参数:series_id套餐系列ID0表示清除关联。",
Tags: []string{"设备管理"},
Input: new(dto.BatchSetDeviceSeriesBindngRequest),
Output: new(dto.BatchSetDeviceSeriesBindngResponse),

View File

@@ -101,7 +101,7 @@ func registerIotCardRoutes(router fiber.Router, handler *admin.IotCardHandler, i
Register(iotCards, doc, groupPath, "PATCH", "/series-binding", handler.BatchSetSeriesBinding, RouteSpec{
Summary: "批量设置卡的套餐系列绑定",
Description: "批量设置或清除卡与套餐系列分配的关联关系。series_allocation_id 为 0 时表示清除关联。",
Description: "批量设置或清除卡与套餐系列分配的关联关系。参数:series_id套餐系列ID0表示清除关联。",
Tags: []string{"IoT卡管理"},
Input: new(dto.BatchSetCardSeriesBindngRequest),
Output: new(dto.BatchSetCardSeriesBindngResponse),

View File

@@ -202,11 +202,11 @@ func (s *Service) triggerOneTimeCommissionForCardInTx(ctx context.Context, tx *g
return errors.Wrap(errors.CodeDatabaseError, err, "获取卡信息失败")
}
if card.SeriesAllocationID == nil {
if card.SeriesID == nil || card.ShopID == nil {
return nil
}
allocation, err := s.shopSeriesAllocationStore.GetByID(ctx, *card.SeriesAllocationID)
allocation, err := s.shopSeriesAllocationStore.GetByShopAndSeries(ctx, *card.ShopID, *card.SeriesID)
if err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "获取系列分配失败")
}
@@ -302,11 +302,11 @@ func (s *Service) triggerOneTimeCommissionForDeviceInTx(ctx context.Context, tx
return errors.Wrap(errors.CodeDatabaseError, err, "获取设备信息失败")
}
if device.SeriesAllocationID == nil {
if device.SeriesID == nil || device.ShopID == nil {
return nil
}
allocation, err := s.shopSeriesAllocationStore.GetByID(ctx, *device.SeriesAllocationID)
allocation, err := s.shopSeriesAllocationStore.GetByShopAndSeries(ctx, *device.ShopID, *device.SeriesID)
if err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "获取系列分配失败")
}

View File

@@ -101,7 +101,7 @@ func TestCalculateCommission_PurchaseOnBehalf(t *testing.T) {
},
ICCID: "89860000000000000001",
ShopID: &shop.ID,
SeriesAllocationID: &allocation.ID,
SeriesID: &allocation.SeriesID,
AccumulatedRecharge: 0,
FirstCommissionPaid: false,
}
@@ -278,7 +278,7 @@ func TestCalculateCommission_Device_PurchaseOnBehalf(t *testing.T) {
},
DeviceNo: "DEV001",
ShopID: &shop.ID,
SeriesAllocationID: &allocation.ID,
SeriesID: &allocation.SeriesID,
AccumulatedRecharge: 0,
FirstCommissionPaid: false,
}

View File

@@ -20,6 +20,8 @@ type Service struct {
shopStore *postgres.ShopStore
assetAllocationRecordStore *postgres.AssetAllocationRecordStore
seriesAllocationStore *postgres.ShopSeriesAllocationStore
packageSeriesStore *postgres.PackageSeriesStore
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore
}
func New(
@@ -30,6 +32,7 @@ func New(
shopStore *postgres.ShopStore,
assetAllocationRecordStore *postgres.AssetAllocationRecordStore,
seriesAllocationStore *postgres.ShopSeriesAllocationStore,
packageSeriesStore *postgres.PackageSeriesStore,
) *Service {
return &Service{
db: db,
@@ -39,6 +42,8 @@ func New(
shopStore: shopStore,
assetAllocationRecordStore: assetAllocationRecordStore,
seriesAllocationStore: seriesAllocationStore,
packageSeriesStore: packageSeriesStore,
shopSeriesAllocationStore: seriesAllocationStore,
}
}
@@ -86,8 +91,8 @@ func (s *Service) List(ctx context.Context, req *dto.ListDeviceRequest) (*dto.Li
if req.CreatedAtEnd != nil {
filters["created_at_end"] = *req.CreatedAtEnd
}
if req.SeriesAllocationID != nil {
filters["series_allocation_id"] = *req.SeriesAllocationID
if req.SeriesID != nil {
filters["series_id"] = *req.SeriesID
}
devices, total, err := s.deviceStore.List(ctx, opts, filters)
@@ -466,7 +471,7 @@ func (s *Service) toDeviceResponse(device *model.Device, shopMap map[uint]string
Status: device.Status,
StatusName: s.getDeviceStatusName(device.Status),
BoundCardCount: int(bindingCounts[device.ID]),
SeriesAllocationID: device.SeriesAllocationID,
SeriesID: device.SeriesID,
FirstCommissionPaid: device.FirstCommissionPaid,
AccumulatedRecharge: device.AccumulatedRecharge,
ActivatedAt: device.ActivatedAt,
@@ -598,17 +603,18 @@ func (s *Service) BatchSetSeriesBinding(ctx context.Context, req *dto.BatchSetDe
deviceMap[device.ID] = device
}
var seriesAllocation *model.ShopSeriesAllocation
if req.SeriesAllocationID > 0 {
seriesAllocation, err = s.seriesAllocationStore.GetByID(ctx, req.SeriesAllocationID)
// 验证系列存在(仅当 SeriesID > 0 时)
var packageSeries *model.PackageSeries
if req.SeriesID > 0 {
packageSeries, err = s.packageSeriesStore.GetByID(ctx, req.SeriesID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, errors.New(errors.CodeNotFound, "套餐系列分配不存在")
return nil, errors.New(errors.CodeNotFound, "套餐系列不存在或已禁用")
}
return nil, err
}
if seriesAllocation.Status != 1 {
return nil, errors.New(errors.CodeInvalidParam, "套餐系列分配已禁用")
if packageSeries.Status != 1 {
return nil, errors.New(errors.CodeInvalidParam, "套餐系列不存在或已禁用")
}
}
@@ -626,17 +632,23 @@ func (s *Service) BatchSetSeriesBinding(ctx context.Context, req *dto.BatchSetDe
continue
}
if req.SeriesAllocationID > 0 {
if device.ShopID == nil || *device.ShopID != seriesAllocation.ShopID {
failedItems = append(failedItems, dto.DeviceSeriesBindngFailedItem{
DeviceID: device.ID,
DeviceNo: device.DeviceNo,
Reason: "设备不属于套餐系列分配的店铺",
})
continue
// 验证操作者权限(仅代理用户)
if operatorShopID != nil && req.SeriesID > 0 {
allocation, err := s.shopSeriesAllocationStore.GetByShopAndSeries(ctx, *operatorShopID, req.SeriesID)
if err != nil {
if err == gorm.ErrRecordNotFound || allocation.Status != 1 {
failedItems = append(failedItems, dto.DeviceSeriesBindngFailedItem{
DeviceID: deviceID,
DeviceNo: device.DeviceNo,
Reason: "您没有权限分配该套餐系列",
})
continue
}
return nil, err
}
}
// 验证设备权限(基于 device.ShopID
if operatorShopID != nil {
if device.ShopID == nil || *device.ShopID != *operatorShopID {
failedItems = append(failedItems, dto.DeviceSeriesBindngFailedItem{
@@ -652,11 +664,11 @@ func (s *Service) BatchSetSeriesBinding(ctx context.Context, req *dto.BatchSetDe
}
if len(successDeviceIDs) > 0 {
var seriesAllocationIDPtr *uint
if req.SeriesAllocationID > 0 {
seriesAllocationIDPtr = &req.SeriesAllocationID
var seriesIDPtr *uint
if req.SeriesID > 0 {
seriesIDPtr = &req.SeriesID
}
if err := s.deviceStore.BatchUpdateSeriesAllocation(ctx, successDeviceIDs, seriesAllocationIDPtr); err != nil {
if err := s.deviceStore.BatchUpdateSeriesID(ctx, successDeviceIDs, seriesIDPtr); err != nil {
return nil, err
}
}

View File

@@ -29,8 +29,9 @@ func TestDeviceService_BatchSetSeriesBinding(t *testing.T) {
shopStore := postgres.NewShopStore(tx, rdb)
assetAllocationRecordStore := postgres.NewAssetAllocationRecordStore(tx, rdb)
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
packageSeriesStore := postgres.NewPackageSeriesStore(tx)
svc := New(tx, deviceStore, deviceSimBindingStore, iotCardStore, shopStore, assetAllocationRecordStore, seriesAllocationStore)
svc := New(tx, deviceStore, deviceSimBindingStore, iotCardStore, shopStore, assetAllocationRecordStore, seriesAllocationStore, packageSeriesStore)
ctx := context.Background()
shop := &model.Shop{
@@ -65,8 +66,8 @@ func TestDeviceService_BatchSetSeriesBinding(t *testing.T) {
t.Run("成功设置系列绑定", func(t *testing.T) {
req := &dto.BatchSetDeviceSeriesBindngRequest{
DeviceIDs: []uint{devices[0].ID, devices[1].ID},
SeriesAllocationID: allocation.ID,
DeviceIDs: []uint{devices[0].ID, devices[1].ID},
SeriesID: allocation.SeriesID,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
@@ -77,15 +78,15 @@ func TestDeviceService_BatchSetSeriesBinding(t *testing.T) {
var updatedDevices []*model.Device
require.NoError(t, tx.Where("id IN ?", req.DeviceIDs).Find(&updatedDevices).Error)
for _, device := range updatedDevices {
require.NotNil(t, device.SeriesAllocationID)
assert.Equal(t, allocation.ID, *device.SeriesAllocationID)
require.NotNil(t, device.SeriesID)
assert.Equal(t, allocation.SeriesID, *device.SeriesID)
}
})
t.Run("设备不属于套餐系列分配的店铺", func(t *testing.T) {
req := &dto.BatchSetDeviceSeriesBindngRequest{
DeviceIDs: []uint{devices[2].ID},
SeriesAllocationID: allocation.ID,
DeviceIDs: []uint{devices[2].ID},
SeriesID: allocation.SeriesID,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
@@ -97,8 +98,8 @@ func TestDeviceService_BatchSetSeriesBinding(t *testing.T) {
t.Run("设备不存在", func(t *testing.T) {
req := &dto.BatchSetDeviceSeriesBindngRequest{
DeviceIDs: []uint{99999},
SeriesAllocationID: allocation.ID,
DeviceIDs: []uint{99999},
SeriesID: allocation.SeriesID,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
@@ -110,8 +111,8 @@ func TestDeviceService_BatchSetSeriesBinding(t *testing.T) {
t.Run("清除系列绑定", func(t *testing.T) {
req := &dto.BatchSetDeviceSeriesBindngRequest{
DeviceIDs: []uint{devices[0].ID},
SeriesAllocationID: 0,
DeviceIDs: []uint{devices[0].ID},
SeriesID: 0,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
@@ -120,14 +121,14 @@ func TestDeviceService_BatchSetSeriesBinding(t *testing.T) {
var updatedDevice model.Device
require.NoError(t, tx.First(&updatedDevice, devices[0].ID).Error)
assert.Nil(t, updatedDevice.SeriesAllocationID)
assert.Nil(t, updatedDevice.SeriesID)
})
t.Run("代理用户只能操作自己店铺的设备", func(t *testing.T) {
otherShopID := uint(99999)
req := &dto.BatchSetDeviceSeriesBindngRequest{
DeviceIDs: []uint{devices[1].ID},
SeriesAllocationID: 0,
DeviceIDs: []uint{devices[1].ID},
SeriesID: 0,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, &otherShopID)
@@ -139,8 +140,8 @@ func TestDeviceService_BatchSetSeriesBinding(t *testing.T) {
t.Run("套餐系列分配不存在", func(t *testing.T) {
req := &dto.BatchSetDeviceSeriesBindngRequest{
DeviceIDs: []uint{devices[1].ID},
SeriesAllocationID: 99999,
DeviceIDs: []uint{devices[1].ID},
SeriesID: 99999,
}
_, err := svc.BatchSetSeriesBinding(ctx, req, nil)

View File

@@ -20,6 +20,7 @@ type Service struct {
shopStore *postgres.ShopStore
assetAllocationRecordStore *postgres.AssetAllocationRecordStore
seriesAllocationStore *postgres.ShopSeriesAllocationStore
packageSeriesStore *postgres.PackageSeriesStore
gatewayClient *gateway.Client
logger *zap.Logger
}
@@ -30,6 +31,7 @@ func New(
shopStore *postgres.ShopStore,
assetAllocationRecordStore *postgres.AssetAllocationRecordStore,
seriesAllocationStore *postgres.ShopSeriesAllocationStore,
packageSeriesStore *postgres.PackageSeriesStore,
gatewayClient *gateway.Client,
logger *zap.Logger,
) *Service {
@@ -39,6 +41,7 @@ func New(
shopStore: shopStore,
assetAllocationRecordStore: assetAllocationRecordStore,
seriesAllocationStore: seriesAllocationStore,
packageSeriesStore: packageSeriesStore,
gatewayClient: gatewayClient,
logger: logger,
}
@@ -93,8 +96,8 @@ func (s *Service) ListStandalone(ctx context.Context, req *dto.ListStandaloneIot
if req.IsReplaced != nil {
filters["is_replaced"] = *req.IsReplaced
}
if req.SeriesAllocationID != nil {
filters["series_allocation_id"] = *req.SeriesAllocationID
if req.SeriesID != nil {
filters["series_id"] = *req.SeriesID
}
cards, total, err := s.iotCardStore.ListStandalone(ctx, opts, filters)
@@ -187,7 +190,7 @@ func (s *Service) toStandaloneResponse(card *model.IotCard, shopMap map[uint]str
RealNameStatus: card.RealNameStatus,
NetworkStatus: card.NetworkStatus,
DataUsageMB: card.DataUsageMB,
SeriesAllocationID: card.SeriesAllocationID,
SeriesID: card.SeriesID,
FirstCommissionPaid: card.FirstCommissionPaid,
AccumulatedRecharge: card.AccumulatedRecharge,
CreatedAt: card.CreatedAt,
@@ -571,17 +574,18 @@ func (s *Service) BatchSetSeriesBinding(ctx context.Context, req *dto.BatchSetCa
cardMap[card.ICCID] = card
}
var seriesAllocation *model.ShopSeriesAllocation
if req.SeriesAllocationID > 0 {
seriesAllocation, err = s.seriesAllocationStore.GetByID(ctx, req.SeriesAllocationID)
// 验证系列存在(仅当 SeriesID > 0 时)
var packageSeries *model.PackageSeries
if req.SeriesID > 0 {
packageSeries, err = s.packageSeriesStore.GetByID(ctx, req.SeriesID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, errors.New(errors.CodeNotFound, "套餐系列分配不存在")
return nil, errors.New(errors.CodeNotFound, "套餐系列不存在或已禁用")
}
return nil, err
}
if seriesAllocation.Status != 1 {
return nil, errors.New(errors.CodeInvalidParam, "套餐系列分配已禁用")
if packageSeries.Status != 1 {
return nil, errors.New(errors.CodeInvalidParam, "套餐系列不存在或已禁用")
}
}
@@ -598,16 +602,22 @@ func (s *Service) BatchSetSeriesBinding(ctx context.Context, req *dto.BatchSetCa
continue
}
if req.SeriesAllocationID > 0 {
if card.ShopID == nil || *card.ShopID != seriesAllocation.ShopID {
failedItems = append(failedItems, dto.CardSeriesBindngFailedItem{
ICCID: iccid,
Reason: "卡不属于套餐系列分配的店铺",
})
continue
// 验证操作者权限(仅代理用户)
if operatorShopID != nil && req.SeriesID > 0 {
allocation, err := s.seriesAllocationStore.GetByShopAndSeries(ctx, *operatorShopID, req.SeriesID)
if err != nil {
if err == gorm.ErrRecordNotFound || allocation.Status != 1 {
failedItems = append(failedItems, dto.CardSeriesBindngFailedItem{
ICCID: iccid,
Reason: "您没有权限分配该套餐系列",
})
continue
}
return nil, err
}
}
// 验证卡权限(基于 card.ShopID
if operatorShopID != nil {
if card.ShopID == nil || *card.ShopID != *operatorShopID {
failedItems = append(failedItems, dto.CardSeriesBindngFailedItem{
@@ -622,11 +632,11 @@ func (s *Service) BatchSetSeriesBinding(ctx context.Context, req *dto.BatchSetCa
}
if len(successCardIDs) > 0 {
var seriesAllocationIDPtr *uint
if req.SeriesAllocationID > 0 {
seriesAllocationIDPtr = &req.SeriesAllocationID
var seriesIDPtr *uint
if req.SeriesID > 0 {
seriesIDPtr = &req.SeriesID
}
if err := s.iotCardStore.BatchUpdateSeriesAllocation(ctx, successCardIDs, seriesAllocationIDPtr); err != nil {
if err := s.iotCardStore.BatchUpdateSeriesID(ctx, successCardIDs, seriesIDPtr); err != nil {
return nil, err
}
}

View File

@@ -28,7 +28,8 @@ func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
assetAllocationRecordStore := postgres.NewAssetAllocationRecordStore(tx, rdb)
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
svc := New(tx, iotCardStore, shopStore, assetAllocationRecordStore, seriesAllocationStore, nil, nil)
packageSeriesStore := postgres.NewPackageSeriesStore(tx)
svc := New(tx, iotCardStore, shopStore, assetAllocationRecordStore, seriesAllocationStore, packageSeriesStore, nil, nil)
ctx := context.Background()
shop := &model.Shop{
@@ -63,8 +64,8 @@ func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
t.Run("成功设置系列绑定", func(t *testing.T) {
req := &dto.BatchSetCardSeriesBindngRequest{
ICCIDs: []string{prefix + "001", prefix + "002"},
SeriesAllocationID: allocation.ID,
ICCIDs: []string{prefix + "001", prefix + "002"},
SeriesID: allocation.SeriesID,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
@@ -75,15 +76,15 @@ func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
var updatedCards []*model.IotCard
require.NoError(t, tx.Where("iccid IN ?", req.ICCIDs).Find(&updatedCards).Error)
for _, card := range updatedCards {
require.NotNil(t, card.SeriesAllocationID)
assert.Equal(t, allocation.ID, *card.SeriesAllocationID)
require.NotNil(t, card.SeriesID)
assert.Equal(t, allocation.SeriesID, *card.SeriesID)
}
})
t.Run("卡不属于套餐系列分配的店铺", func(t *testing.T) {
req := &dto.BatchSetCardSeriesBindngRequest{
ICCIDs: []string{prefix + "003"},
SeriesAllocationID: allocation.ID,
ICCIDs: []string{prefix + "003"},
SeriesID: allocation.SeriesID,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
@@ -95,8 +96,8 @@ func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
t.Run("卡不存在", func(t *testing.T) {
req := &dto.BatchSetCardSeriesBindngRequest{
ICCIDs: []string{"NOTEXIST001"},
SeriesAllocationID: allocation.ID,
ICCIDs: []string{"NOTEXIST001"},
SeriesID: allocation.SeriesID,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
@@ -108,8 +109,8 @@ func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
t.Run("清除系列绑定", func(t *testing.T) {
req := &dto.BatchSetCardSeriesBindngRequest{
ICCIDs: []string{prefix + "001"},
SeriesAllocationID: 0,
ICCIDs: []string{prefix + "001"},
SeriesID: 0,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
@@ -118,14 +119,14 @@ func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
var updatedCard model.IotCard
require.NoError(t, tx.Where("iccid = ?", prefix+"001").First(&updatedCard).Error)
assert.Nil(t, updatedCard.SeriesAllocationID)
assert.Nil(t, updatedCard.SeriesID)
})
t.Run("代理用户只能操作自己店铺的卡", func(t *testing.T) {
otherShopID := uint(99999)
req := &dto.BatchSetCardSeriesBindngRequest{
ICCIDs: []string{prefix + "002"},
SeriesAllocationID: 0,
ICCIDs: []string{prefix + "002"},
SeriesID: 0,
}
resp, err := svc.BatchSetSeriesBinding(ctx, req, &otherShopID)
@@ -137,8 +138,8 @@ func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
t.Run("套餐系列分配不存在", func(t *testing.T) {
req := &dto.BatchSetCardSeriesBindngRequest{
ICCIDs: []string{prefix + "002"},
SeriesAllocationID: 99999,
ICCIDs: []string{prefix + "002"},
SeriesID: 99999,
}
_, err := svc.BatchSetSeriesBinding(ctx, req, nil)

View File

@@ -99,21 +99,21 @@ func setupOrderTestEnv(t *testing.T) *testEnv {
shopIDPtr := &shop.ID
card := &model.IotCard{
ICCID: "89860000000000000002",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesAllocationID: &allocation.ID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
ICCID: "89860000000000000002",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesID: &allocation.SeriesID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
}
require.NoError(t, iotCardStore.Create(ctx, card))
device := &model.Device{
DeviceNo: "DEV_TEST_ORDER_001",
ShopID: shopIDPtr,
SeriesAllocationID: &allocation.ID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
DeviceNo: "DEV_TEST_ORDER_001",
ShopID: shopIDPtr,
SeriesID: &allocation.SeriesID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
}
require.NoError(t, deviceStore.Create(ctx, device))
@@ -569,12 +569,12 @@ func TestOrderService_IdempotencyAndConcurrency(t *testing.T) {
shopIDPtr := &shop.ID
card := &model.IotCard{
ICCID: "89860000000000000099",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesAllocationID: &allocation.ID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
ICCID: "89860000000000000099",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesID: &allocation.SeriesID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
}
require.NoError(t, iotCardStore.Create(ctx, card))
@@ -769,7 +769,7 @@ func TestOrderService_ForceRechargeValidation(t *testing.T) {
ICCID: "89860000000000000FR1",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesAllocationID: &allocation.ID,
SeriesID: &allocation.SeriesID,
Status: constants.StatusEnabled,
FirstCommissionPaid: false,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
@@ -820,7 +820,7 @@ func TestOrderService_ForceRechargeValidation(t *testing.T) {
ICCID: "89860000000000000FR2",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesAllocationID: &allocation.ID,
SeriesID: &allocation.SeriesID,
Status: constants.StatusEnabled,
FirstCommissionPaid: true,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
@@ -917,7 +917,7 @@ func TestOrderService_GetPurchaseCheck(t *testing.T) {
ICCID: "89860000000000000PC1",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesAllocationID: &allocation.ID,
SeriesID: &allocation.SeriesID,
Status: constants.StatusEnabled,
FirstCommissionPaid: false,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
@@ -949,7 +949,7 @@ func TestOrderService_GetPurchaseCheck(t *testing.T) {
ICCID: "89860000000000000PC2",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesAllocationID: &allocation.ID,
SeriesID: &allocation.SeriesID,
Status: constants.StatusEnabled,
FirstCommissionPaid: true,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
@@ -1055,12 +1055,12 @@ func TestOrderService_WalletPay_PurchaseOnBehalf(t *testing.T) {
shopIDPtr := &shop.ID
card := &model.IotCard{
ICCID: "89860000000000000WP1",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesAllocationID: &allocation.ID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
ICCID: "89860000000000000WP1",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesID: &allocation.SeriesID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
}
require.NoError(t, iotCardStore.Create(ctx, card))

View File

@@ -51,23 +51,11 @@ func (s *Service) ValidateCardPurchase(ctx context.Context, cardID uint, package
return nil, err
}
if card.SeriesAllocationID == nil || *card.SeriesAllocationID == 0 {
if card.SeriesID == nil || *card.SeriesID == 0 {
return nil, errors.New(errors.CodeInvalidParam, "该卡未关联套餐系列,无法购买套餐")
}
allocation, err := s.seriesAllocationStore.GetByID(ctx, *card.SeriesAllocationID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, errors.New(errors.CodeInvalidParam, "套餐系列分配不存在")
}
return nil, err
}
if allocation.Status != constants.StatusEnabled {
return nil, errors.New(errors.CodeInvalidParam, "套餐系列分配已禁用")
}
packages, totalPrice, err := s.validatePackages(ctx, packageIDs, allocation.SeriesID)
packages, totalPrice, err := s.validatePackages(ctx, packageIDs, *card.SeriesID)
if err != nil {
return nil, err
}
@@ -76,7 +64,6 @@ func (s *Service) ValidateCardPurchase(ctx context.Context, cardID uint, package
Card: card,
Packages: packages,
TotalPrice: totalPrice,
Allocation: allocation,
}, nil
}
@@ -89,23 +76,11 @@ func (s *Service) ValidateDevicePurchase(ctx context.Context, deviceID uint, pac
return nil, err
}
if device.SeriesAllocationID == nil || *device.SeriesAllocationID == 0 {
if device.SeriesID == nil || *device.SeriesID == 0 {
return nil, errors.New(errors.CodeInvalidParam, "该设备未关联套餐系列,无法购买套餐")
}
allocation, err := s.seriesAllocationStore.GetByID(ctx, *device.SeriesAllocationID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, errors.New(errors.CodeInvalidParam, "套餐系列分配不存在")
}
return nil, err
}
if allocation.Status != constants.StatusEnabled {
return nil, errors.New(errors.CodeInvalidParam, "套餐系列分配已禁用")
}
packages, totalPrice, err := s.validatePackages(ctx, packageIDs, allocation.SeriesID)
packages, totalPrice, err := s.validatePackages(ctx, packageIDs, *device.SeriesID)
if err != nil {
return nil, err
}
@@ -114,7 +89,6 @@ func (s *Service) ValidateDevicePurchase(ctx context.Context, deviceID uint, pac
Device: device,
Packages: packages,
TotalPrice: totalPrice,
Allocation: allocation,
}, nil
}

View File

@@ -75,21 +75,21 @@ func setupTestData(t *testing.T) (context.Context, *Service, *model.IotCard, *mo
shopIDPtr := &shop.ID
card := &model.IotCard{
ICCID: "89860000000000000001",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesAllocationID: &allocation.ID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
ICCID: "89860000000000000001",
ShopID: shopIDPtr,
CarrierID: carrier.ID,
SeriesID: &series.ID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
}
require.NoError(t, iotCardStore.Create(ctx, card))
device := &model.Device{
DeviceNo: "DEV_TEST_PV_001",
ShopID: shopIDPtr,
SeriesAllocationID: &allocation.ID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
DeviceNo: "DEV_TEST_PV_001",
ShopID: shopIDPtr,
SeriesID: &series.ID,
Status: constants.StatusEnabled,
BaseModel: model.BaseModel{Creator: 1, Updater: 1},
}
require.NoError(t, deviceStore.Create(ctx, device))

View File

@@ -374,7 +374,8 @@ func (s *Service) checkForceRechargeRequirement(ctx context.Context, resourceTyp
Message: "无强充要求,可自由充值",
}
var seriesAllocationID *uint
var seriesID *uint
var shopID *uint
var accumulatedRecharge int64
var firstCommissionPaid bool
@@ -387,7 +388,8 @@ func (s *Service) checkForceRechargeRequirement(ctx context.Context, resourceTyp
}
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询IoT卡失败")
}
seriesAllocationID = card.SeriesAllocationID
seriesID = card.SeriesID
shopID = card.ShopID
accumulatedRecharge = card.AccumulatedRecharge
firstCommissionPaid = card.FirstCommissionPaid
} else if resourceType == "device" {
@@ -398,7 +400,8 @@ func (s *Service) checkForceRechargeRequirement(ctx context.Context, resourceTyp
}
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询设备失败")
}
seriesAllocationID = device.SeriesAllocationID
seriesID = device.SeriesID
shopID = device.ShopID
accumulatedRecharge = device.AccumulatedRecharge
firstCommissionPaid = device.FirstCommissionPaid
}
@@ -406,13 +409,13 @@ func (s *Service) checkForceRechargeRequirement(ctx context.Context, resourceTyp
result.CurrentAccumulated = accumulatedRecharge
result.FirstCommissionPaid = firstCommissionPaid
// 2. 如果没有系列分配,无强充要求
if seriesAllocationID == nil {
// 2. 如果没有系列ID或店铺ID,无强充要求
if seriesID == nil || shopID == nil {
return result, nil
}
// 3. 查询系列分配配置
allocation, err := s.shopSeriesAllocationStore.GetByID(ctx, *seriesAllocationID)
allocation, err := s.shopSeriesAllocationStore.GetByShopAndSeries(ctx, *shopID, *seriesID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return result, nil
@@ -483,7 +486,7 @@ func (s *Service) updateAccumulatedRechargeInTx(ctx context.Context, tx *gorm.DB
// triggerOneTimeCommissionIfNeededInTx 触发一次性佣金(事务内使用)
// 检查是否满足一次性佣金触发条件,满足则创建佣金记录并入账
func (s *Service) triggerOneTimeCommissionIfNeededInTx(ctx context.Context, tx *gorm.DB, resourceType string, resourceID uint, rechargeAmount int64, userID uint) error {
var seriesAllocationID *uint
var seriesID *uint
var accumulatedRecharge int64
var firstCommissionPaid bool
var shopID *uint
@@ -494,7 +497,7 @@ func (s *Service) triggerOneTimeCommissionIfNeededInTx(ctx context.Context, tx *
if err := tx.First(&card, resourceID).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "查询IoT卡失败")
}
seriesAllocationID = card.SeriesAllocationID
seriesID = card.SeriesID
accumulatedRecharge = card.AccumulatedRecharge
firstCommissionPaid = card.FirstCommissionPaid
shopID = card.ShopID
@@ -503,14 +506,14 @@ func (s *Service) triggerOneTimeCommissionIfNeededInTx(ctx context.Context, tx *
if err := tx.First(&device, resourceID).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "查询设备失败")
}
seriesAllocationID = device.SeriesAllocationID
seriesID = device.SeriesID
accumulatedRecharge = device.AccumulatedRecharge
firstCommissionPaid = device.FirstCommissionPaid
shopID = device.ShopID
}
// 2. 如果没有系列分配或已发放佣金,跳过
if seriesAllocationID == nil || firstCommissionPaid {
// 2. 如果没有系列ID或已发放佣金,跳过
if seriesID == nil || firstCommissionPaid {
return nil
}
@@ -524,7 +527,7 @@ func (s *Service) triggerOneTimeCommissionIfNeededInTx(ctx context.Context, tx *
}
// 4. 查询系列分配配置
allocation, err := s.shopSeriesAllocationStore.GetByID(ctx, *seriesAllocationID)
allocation, err := s.shopSeriesAllocationStore.GetByShopAndSeries(ctx, *shopID, *seriesID)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil

View File

@@ -60,15 +60,15 @@ func createTestIotCard(t *testing.T, tx *gorm.DB, shopID *uint, seriesAllocation
Creator: 1,
Updater: 1,
},
ICCID: fmt.Sprintf("89860%014d", timestamp%100000000000000),
CardType: "流量卡",
CardCategory: "normal",
CarrierID: 1,
CarrierType: "CMCC",
CarrierName: "中国移动",
Status: 1,
ShopID: shopID,
SeriesAllocationID: seriesAllocationID,
ICCID: fmt.Sprintf("89860%014d", timestamp%100000000000000),
CardType: "流量卡",
CardCategory: "normal",
CarrierID: 1,
CarrierType: "CMCC",
CarrierName: "中国移动",
Status: 1,
ShopID: shopID,
SeriesID: seriesAllocationID,
}
require.NoError(t, tx.Create(card).Error)
return card
@@ -83,12 +83,12 @@ func createTestDevice(t *testing.T, tx *gorm.DB, shopID *uint, seriesAllocationI
Creator: 1,
Updater: 1,
},
DeviceNo: fmt.Sprintf("DEV%014d", timestamp%100000000000000),
DeviceName: "测试设备",
DeviceType: "GPS",
Status: 1,
ShopID: shopID,
SeriesAllocationID: seriesAllocationID,
DeviceNo: fmt.Sprintf("DEV%014d", timestamp%100000000000000),
DeviceName: "测试设备",
DeviceType: "GPS",
Status: 1,
ShopID: shopID,
SeriesID: seriesAllocationID,
}
require.NoError(t, tx.Create(device).Error)
return device

View File

@@ -106,8 +106,8 @@ func (s *DeviceStore) List(ctx context.Context, opts *store.QueryOptions, filter
if createdAtEnd, ok := filters["created_at_end"].(time.Time); ok && !createdAtEnd.IsZero() {
query = query.Where("created_at <= ?", createdAtEnd)
}
if seriesAllocationID, ok := filters["series_allocation_id"].(uint); ok && seriesAllocationID > 0 {
query = query.Where("series_allocation_id = ?", seriesAllocationID)
if seriesID, ok := filters["series_id"].(uint); ok && seriesID > 0 {
query = query.Where("series_id = ?", seriesID)
}
if err := query.Count(&total).Error; err != nil {
@@ -185,20 +185,20 @@ func (s *DeviceStore) GetByDeviceNos(ctx context.Context, deviceNos []string) ([
return devices, nil
}
// BatchUpdateSeriesAllocation 批量更新设备的套餐系列分配
func (s *DeviceStore) BatchUpdateSeriesAllocation(ctx context.Context, deviceIDs []uint, seriesAllocationID *uint) error {
// BatchUpdateSeriesID 批量更新设备的套餐系列ID
func (s *DeviceStore) BatchUpdateSeriesID(ctx context.Context, deviceIDs []uint, seriesID *uint) error {
if len(deviceIDs) == 0 {
return nil
}
return s.db.WithContext(ctx).Model(&model.Device{}).
Where("id IN ?", deviceIDs).
Update("series_allocation_id", seriesAllocationID).Error
Update("series_id", seriesID).Error
}
// ListBySeriesAllocationID 根据套餐系列分配ID查询设备列表
func (s *DeviceStore) ListBySeriesAllocationID(ctx context.Context, seriesAllocationID uint) ([]*model.Device, error) {
// ListBySeriesID 根据套餐系列ID查询设备列表
func (s *DeviceStore) ListBySeriesID(ctx context.Context, seriesID uint) ([]*model.Device, error) {
var devices []*model.Device
if err := s.db.WithContext(ctx).Where("series_allocation_id = ?", seriesAllocationID).Find(&devices).Error; err != nil {
if err := s.db.WithContext(ctx).Where("series_id = ?", seriesID).Find(&devices).Error; err != nil {
return nil, err
}
return devices, nil

View File

@@ -16,7 +16,7 @@ func uniqueDeviceNoPrefix() string {
return fmt.Sprintf("D%d", time.Now().UnixNano()%1000000000)
}
func TestDeviceStore_BatchUpdateSeriesAllocation(t *testing.T) {
func TestDeviceStore_BatchUpdateSeriesID(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
@@ -31,39 +31,39 @@ func TestDeviceStore_BatchUpdateSeriesAllocation(t *testing.T) {
}
require.NoError(t, s.CreateBatch(ctx, devices))
t.Run("设置系列分配ID", func(t *testing.T) {
seriesAllocationID := uint(100)
t.Run("设置系列ID", func(t *testing.T) {
seriesID := uint(100)
deviceIDs := []uint{devices[0].ID, devices[1].ID}
err := s.BatchUpdateSeriesAllocation(ctx, deviceIDs, &seriesAllocationID)
err := s.BatchUpdateSeriesID(ctx, deviceIDs, &seriesID)
require.NoError(t, err)
var updatedDevices []*model.Device
require.NoError(t, tx.Where("id IN ?", deviceIDs).Find(&updatedDevices).Error)
for _, device := range updatedDevices {
require.NotNil(t, device.SeriesAllocationID)
assert.Equal(t, seriesAllocationID, *device.SeriesAllocationID)
require.NotNil(t, device.SeriesID)
assert.Equal(t, seriesID, *device.SeriesID)
}
})
t.Run("清除系列分配ID", func(t *testing.T) {
t.Run("清除系列ID", func(t *testing.T) {
deviceIDs := []uint{devices[0].ID}
err := s.BatchUpdateSeriesAllocation(ctx, deviceIDs, nil)
err := s.BatchUpdateSeriesID(ctx, deviceIDs, nil)
require.NoError(t, err)
var updatedDevice model.Device
require.NoError(t, tx.First(&updatedDevice, devices[0].ID).Error)
assert.Nil(t, updatedDevice.SeriesAllocationID)
assert.Nil(t, updatedDevice.SeriesID)
})
t.Run("空列表不报错", func(t *testing.T) {
err := s.BatchUpdateSeriesAllocation(ctx, []uint{}, nil)
err := s.BatchUpdateSeriesID(ctx, []uint{}, nil)
require.NoError(t, err)
})
}
func TestDeviceStore_ListBySeriesAllocationID(t *testing.T) {
func TestDeviceStore_ListBySeriesID(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
@@ -72,23 +72,23 @@ func TestDeviceStore_ListBySeriesAllocationID(t *testing.T) {
ctx := context.Background()
prefix := uniqueDeviceNoPrefix()
seriesAllocationID := uint(200)
seriesID := uint(200)
devices := []*model.Device{
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1, SeriesAllocationID: &seriesAllocationID},
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1, SeriesAllocationID: &seriesAllocationID},
{DeviceNo: prefix + "003", DeviceName: "测试设备3", Status: 1, SeriesAllocationID: nil},
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1, SeriesID: &seriesID},
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1, SeriesID: &seriesID},
{DeviceNo: prefix + "003", DeviceName: "测试设备3", Status: 1, SeriesID: nil},
}
require.NoError(t, s.CreateBatch(ctx, devices))
result, err := s.ListBySeriesAllocationID(ctx, seriesAllocationID)
result, err := s.ListBySeriesID(ctx, seriesID)
require.NoError(t, err)
assert.Len(t, result, 2)
for _, device := range result {
assert.Equal(t, seriesAllocationID, *device.SeriesAllocationID)
assert.Equal(t, seriesID, *device.SeriesID)
}
}
func TestDeviceStore_List_SeriesAllocationFilter(t *testing.T) {
func TestDeviceStore_List_SeriesIDFilter(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
@@ -97,23 +97,23 @@ func TestDeviceStore_List_SeriesAllocationFilter(t *testing.T) {
ctx := context.Background()
prefix := uniqueDeviceNoPrefix()
seriesAllocationID := uint(300)
seriesID := uint(300)
devices := []*model.Device{
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1, SeriesAllocationID: &seriesAllocationID},
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1, SeriesAllocationID: &seriesAllocationID},
{DeviceNo: prefix + "003", DeviceName: "测试设备3", Status: 1, SeriesAllocationID: nil},
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1, SeriesID: &seriesID},
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1, SeriesID: &seriesID},
{DeviceNo: prefix + "003", DeviceName: "测试设备3", Status: 1, SeriesID: nil},
}
require.NoError(t, s.CreateBatch(ctx, devices))
filters := map[string]interface{}{
"series_allocation_id": seriesAllocationID,
"device_no": prefix,
"series_id": seriesID,
"device_no": prefix,
}
result, total, err := s.List(ctx, nil, filters)
require.NoError(t, err)
assert.Equal(t, int64(2), total)
assert.Len(t, result, 2)
for _, device := range result {
assert.Equal(t, seriesAllocationID, *device.SeriesAllocationID)
assert.Equal(t, seriesID, *device.SeriesID)
}
}

View File

@@ -147,8 +147,8 @@ func (s *IotCardStore) List(ctx context.Context, opts *store.QueryOptions, filte
if iccidEnd, ok := filters["iccid_end"].(string); ok && iccidEnd != "" {
query = query.Where("iccid <= ?", iccidEnd)
}
if seriesAllocationID, ok := filters["series_allocation_id"].(uint); ok && seriesAllocationID > 0 {
query = query.Where("series_allocation_id = ?", seriesAllocationID)
if seriesID, ok := filters["series_id"].(uint); ok && seriesID > 0 {
query = query.Where("series_id = ?", seriesID)
}
// 统计总数
@@ -242,8 +242,8 @@ func (s *IotCardStore) ListStandalone(ctx context.Context, opts *store.QueryOpti
Where("deleted_at IS NULL"))
}
}
if seriesAllocationID, ok := filters["series_allocation_id"].(uint); ok && seriesAllocationID > 0 {
query = query.Where("series_allocation_id = ?", seriesAllocationID)
if seriesID, ok := filters["series_id"].(uint); ok && seriesID > 0 {
query = query.Where("series_id = ?", seriesID)
}
if err := query.Count(&total).Error; err != nil {
@@ -381,22 +381,22 @@ func (s *IotCardStore) GetByIDsWithEnterpriseFilter(ctx context.Context, cardIDs
return cards, nil
}
// BatchUpdateSeriesAllocation 批量更新卡的套餐系列分配
// BatchUpdateSeriesID 批量更新卡的套餐系列ID
// 用于批量设置或清除卡与套餐系列的关联关系
func (s *IotCardStore) BatchUpdateSeriesAllocation(ctx context.Context, cardIDs []uint, seriesAllocationID *uint) error {
func (s *IotCardStore) BatchUpdateSeriesID(ctx context.Context, cardIDs []uint, seriesID *uint) error {
if len(cardIDs) == 0 {
return nil
}
return s.db.WithContext(ctx).Model(&model.IotCard{}).
Where("id IN ?", cardIDs).
Update("series_allocation_id", seriesAllocationID).Error
Update("series_id", seriesID).Error
}
// ListBySeriesAllocationID 根据套餐系列分配ID查询卡列表
// 用于查询某个套餐系列分配下的所有卡
func (s *IotCardStore) ListBySeriesAllocationID(ctx context.Context, seriesAllocationID uint) ([]*model.IotCard, error) {
// ListBySeriesID 根据套餐系列ID查询卡列表
// 用于查询某个套餐系列下的所有卡
func (s *IotCardStore) ListBySeriesID(ctx context.Context, seriesID uint) ([]*model.IotCard, error) {
var cards []*model.IotCard
if err := s.db.WithContext(ctx).Where("series_allocation_id = ?", seriesAllocationID).Find(&cards).Error; err != nil {
if err := s.db.WithContext(ctx).Where("series_id = ?", seriesID).Find(&cards).Error; err != nil {
return nil, err
}
return cards, nil

View File

@@ -426,7 +426,7 @@ func TestIotCardStore_GetBoundCardIDs(t *testing.T) {
})
}
func TestIotCardStore_BatchUpdateSeriesAllocation(t *testing.T) {
func TestIotCardStore_BatchUpdateSeriesID(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
@@ -440,39 +440,39 @@ func TestIotCardStore_BatchUpdateSeriesAllocation(t *testing.T) {
}
require.NoError(t, s.CreateBatch(ctx, cards))
t.Run("设置系列分配ID", func(t *testing.T) {
seriesAllocationID := uint(100)
t.Run("设置系列ID", func(t *testing.T) {
seriesID := uint(100)
cardIDs := []uint{cards[0].ID, cards[1].ID}
err := s.BatchUpdateSeriesAllocation(ctx, cardIDs, &seriesAllocationID)
err := s.BatchUpdateSeriesID(ctx, cardIDs, &seriesID)
require.NoError(t, err)
var updatedCards []*model.IotCard
require.NoError(t, tx.Where("id IN ?", cardIDs).Find(&updatedCards).Error)
for _, card := range updatedCards {
require.NotNil(t, card.SeriesAllocationID)
assert.Equal(t, seriesAllocationID, *card.SeriesAllocationID)
require.NotNil(t, card.SeriesID)
assert.Equal(t, seriesID, *card.SeriesID)
}
})
t.Run("清除系列分配ID", func(t *testing.T) {
t.Run("清除系列ID", func(t *testing.T) {
cardIDs := []uint{cards[0].ID}
err := s.BatchUpdateSeriesAllocation(ctx, cardIDs, nil)
err := s.BatchUpdateSeriesID(ctx, cardIDs, nil)
require.NoError(t, err)
var updatedCard model.IotCard
require.NoError(t, tx.First(&updatedCard, cards[0].ID).Error)
assert.Nil(t, updatedCard.SeriesAllocationID)
assert.Nil(t, updatedCard.SeriesID)
})
t.Run("空列表不报错", func(t *testing.T) {
err := s.BatchUpdateSeriesAllocation(ctx, []uint{}, nil)
err := s.BatchUpdateSeriesID(ctx, []uint{}, nil)
require.NoError(t, err)
})
}
func TestIotCardStore_ListBySeriesAllocationID(t *testing.T) {
func TestIotCardStore_ListBySeriesID(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
@@ -480,23 +480,23 @@ func TestIotCardStore_ListBySeriesAllocationID(t *testing.T) {
s := NewIotCardStore(tx, rdb)
ctx := context.Background()
seriesAllocationID := uint(200)
seriesID := uint(200)
cards := []*model.IotCard{
{ICCID: "89860012345678911001", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: &seriesAllocationID},
{ICCID: "89860012345678911002", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: &seriesAllocationID},
{ICCID: "89860012345678911003", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: nil},
{ICCID: "89860012345678911001", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: &seriesID},
{ICCID: "89860012345678911002", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: &seriesID},
{ICCID: "89860012345678911003", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: nil},
}
require.NoError(t, s.CreateBatch(ctx, cards))
result, err := s.ListBySeriesAllocationID(ctx, seriesAllocationID)
result, err := s.ListBySeriesID(ctx, seriesID)
require.NoError(t, err)
assert.Len(t, result, 2)
for _, card := range result {
assert.Equal(t, seriesAllocationID, *card.SeriesAllocationID)
assert.Equal(t, seriesID, *card.SeriesID)
}
}
func TestIotCardStore_ListStandalone_SeriesAllocationFilter(t *testing.T) {
func TestIotCardStore_ListStandalone_SeriesIDFilter(t *testing.T) {
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
@@ -505,23 +505,23 @@ func TestIotCardStore_ListStandalone_SeriesAllocationFilter(t *testing.T) {
ctx := context.Background()
prefix := uniqueICCIDPrefix()
seriesAllocationID := uint(300)
seriesID := uint(300)
cards := []*model.IotCard{
{ICCID: prefix + "S001", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: &seriesAllocationID},
{ICCID: prefix + "S002", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: &seriesAllocationID},
{ICCID: prefix + "S003", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: nil},
{ICCID: prefix + "S001", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: &seriesID},
{ICCID: prefix + "S002", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: &seriesID},
{ICCID: prefix + "S003", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: nil},
}
require.NoError(t, s.CreateBatch(ctx, cards))
filters := map[string]interface{}{
"series_allocation_id": seriesAllocationID,
"iccid": prefix,
"series_id": seriesID,
"iccid": prefix,
}
result, total, err := s.ListStandalone(ctx, nil, filters)
require.NoError(t, err)
assert.Equal(t, int64(2), total)
assert.Len(t, result, 2)
for _, card := range result {
assert.Equal(t, seriesAllocationID, *card.SeriesAllocationID)
assert.Equal(t, seriesID, *card.SeriesID)
}
}

View File

@@ -0,0 +1,114 @@
package postgres
import (
"context"
"testing"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/tests/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
func TestShopSeriesAllocationStore_GetByShopAndSeries(t *testing.T) {
tx := testutils.NewTestTransaction(t)
ctx := context.Background()
s := NewShopSeriesAllocationStore(tx)
// 创建测试数据
allocation := &model.ShopSeriesAllocation{
ShopID: 1,
SeriesID: 100,
AllocatorShopID: 0,
Status: 1,
}
require.NoError(t, s.Create(ctx, allocation))
t.Run("查询存在的分配", func(t *testing.T) {
result, err := s.GetByShopAndSeries(ctx, 1, 100)
require.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, uint(1), result.ShopID)
assert.Equal(t, uint(100), result.SeriesID)
})
t.Run("查询不存在的分配", func(t *testing.T) {
result, err := s.GetByShopAndSeries(ctx, 999, 999)
assert.Error(t, err)
assert.Equal(t, gorm.ErrRecordNotFound, err)
assert.Nil(t, result)
})
}
func TestShopSeriesAllocationStore_Create(t *testing.T) {
tx := testutils.NewTestTransaction(t)
ctx := context.Background()
s := NewShopSeriesAllocationStore(tx)
allocation := &model.ShopSeriesAllocation{
ShopID: 1,
SeriesID: 100,
AllocatorShopID: 0,
Status: 1,
}
err := s.Create(ctx, allocation)
require.NoError(t, err)
assert.NotZero(t, allocation.ID)
}
func TestShopSeriesAllocationStore_GetByID(t *testing.T) {
tx := testutils.NewTestTransaction(t)
ctx := context.Background()
s := NewShopSeriesAllocationStore(tx)
allocation := &model.ShopSeriesAllocation{
ShopID: 1,
SeriesID: 100,
AllocatorShopID: 0,
Status: 1,
}
require.NoError(t, s.Create(ctx, allocation))
result, err := s.GetByID(ctx, allocation.ID)
require.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, allocation.ID, result.ID)
}
func TestShopSeriesAllocationStore_List(t *testing.T) {
tx := testutils.NewTestTransaction(t)
ctx := context.Background()
s := NewShopSeriesAllocationStore(tx)
// 创建测试数据
allocations := []*model.ShopSeriesAllocation{
{ShopID: 1, SeriesID: 100, AllocatorShopID: 0, Status: 1},
{ShopID: 1, SeriesID: 101, AllocatorShopID: 0, Status: 1},
{ShopID: 2, SeriesID: 100, AllocatorShopID: 0, Status: 1},
}
for _, a := range allocations {
require.NoError(t, s.Create(ctx, a))
}
t.Run("按店铺ID过滤", func(t *testing.T) {
filters := map[string]interface{}{"shop_id": uint(1)}
result, total, err := s.List(ctx, nil, filters)
require.NoError(t, err)
assert.Equal(t, int64(2), total)
assert.Len(t, result, 2)
})
t.Run("按系列ID过滤", func(t *testing.T) {
filters := map[string]interface{}{"series_id": uint(100)}
result, total, err := s.List(ctx, nil, filters)
require.NoError(t, err)
assert.Equal(t, int64(2), total)
assert.Len(t, result, 2)
})
}