feat: 实现卡和设备的套餐系列绑定功能
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m37s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m37s
- 添加 Device 和 IotCard 模型的 SeriesID 字段 - 实现 DeviceService 和 IotCardService 的套餐系列绑定逻辑 - 添加 DeviceStore 和 IotCardStore 的数据库操作方法 - 更新 API 接口和路由支持套餐系列绑定 - 创建数据库迁移脚本(000027_add_series_binding_fields) - 添加完整的单元测试和集成测试 - 更新 OpenAPI 文档 - 归档 OpenSpec 变更文档 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -78,9 +78,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),
|
||||
IotCard: iotCardSvc.New(deps.DB, s.IotCard, s.Shop, s.AssetAllocationRecord, s.ShopSeriesAllocation),
|
||||
IotCardImport: iotCardImportSvc.New(deps.DB, s.IotCardImportTask, deps.QueueClient),
|
||||
Device: deviceSvc.New(deps.DB, s.Device, s.DeviceSimBinding, s.IotCard, s.Shop, s.AssetAllocationRecord),
|
||||
Device: deviceSvc.New(deps.DB, s.Device, s.DeviceSimBinding, s.IotCard, s.Shop, s.AssetAllocationRecord, s.ShopSeriesAllocation),
|
||||
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),
|
||||
|
||||
@@ -195,3 +195,28 @@ func (h *DeviceHandler) Recall(c *fiber.Ctx) error {
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *DeviceHandler) BatchSetSeriesBinding(c *fiber.Ctx) error {
|
||||
var req dto.BatchSetDeviceSeriesBindngRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
ctx := c.UserContext()
|
||||
userType := middleware.GetUserTypeFromContext(ctx)
|
||||
|
||||
var operatorShopID *uint
|
||||
if userType == constants.UserTypeAgent {
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
if shopID > 0 {
|
||||
operatorShopID = &shopID
|
||||
}
|
||||
}
|
||||
|
||||
result, err := h.service.BatchSetSeriesBinding(ctx, &req, operatorShopID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
@@ -98,3 +98,28 @@ func (h *IotCardHandler) RecallCards(c *fiber.Ctx) error {
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
func (h *IotCardHandler) BatchSetSeriesBinding(c *fiber.Ctx) error {
|
||||
var req dto.BatchSetCardSeriesBindngRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
}
|
||||
|
||||
ctx := c.UserContext()
|
||||
userType := middleware.GetUserTypeFromContext(ctx)
|
||||
|
||||
var operatorShopID *uint
|
||||
if userType == constants.UserTypeAgent {
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
if shopID > 0 {
|
||||
operatorShopID = &shopID
|
||||
}
|
||||
}
|
||||
|
||||
result, err := h.service.BatchSetSeriesBinding(ctx, &req, operatorShopID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@ 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"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
|
||||
@@ -3,36 +3,40 @@ 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表示平台库存)"`
|
||||
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表示平台库存)"`
|
||||
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:"创建时间结束"`
|
||||
}
|
||||
|
||||
type DeviceResponse struct {
|
||||
ID uint `json:"id" description:"设备ID"`
|
||||
DeviceNo string `json:"device_no" description:"设备号"`
|
||||
DeviceName string `json:"device_name" description:"设备名称"`
|
||||
DeviceModel string `json:"device_model" description:"设备型号"`
|
||||
DeviceType string `json:"device_type" description:"设备类型"`
|
||||
MaxSimSlots int `json:"max_sim_slots" description:"最大插槽数"`
|
||||
Manufacturer string `json:"manufacturer" description:"制造商"`
|
||||
BatchNo string `json:"batch_no" description:"批次号"`
|
||||
ShopID *uint `json:"shop_id,omitempty" description:"店铺ID"`
|
||||
ShopName string `json:"shop_name,omitempty" description:"店铺名称"`
|
||||
Status int `json:"status" description:"状态 (1:在库, 2:已分销, 3:已激活, 4:已停用)"`
|
||||
StatusName string `json:"status_name" description:"状态名称"`
|
||||
BoundCardCount int `json:"bound_card_count" description:"已绑定卡数量"`
|
||||
ActivatedAt *time.Time `json:"activated_at,omitempty" description:"激活时间"`
|
||||
CreatedAt time.Time `json:"created_at" description:"创建时间"`
|
||||
UpdatedAt time.Time `json:"updated_at" description:"更新时间"`
|
||||
ID uint `json:"id" description:"设备ID"`
|
||||
DeviceNo string `json:"device_no" description:"设备号"`
|
||||
DeviceName string `json:"device_name" description:"设备名称"`
|
||||
DeviceModel string `json:"device_model" description:"设备型号"`
|
||||
DeviceType string `json:"device_type" description:"设备类型"`
|
||||
MaxSimSlots int `json:"max_sim_slots" description:"最大插槽数"`
|
||||
Manufacturer string `json:"manufacturer" description:"制造商"`
|
||||
BatchNo string `json:"batch_no" description:"批次号"`
|
||||
ShopID *uint `json:"shop_id,omitempty" description:"店铺ID"`
|
||||
ShopName string `json:"shop_name,omitempty" description:"店铺名称"`
|
||||
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"`
|
||||
FirstCommissionPaid bool `json:"first_commission_paid" description:"一次性佣金是否已发放"`
|
||||
AccumulatedRecharge int64 `json:"accumulated_recharge" description:"累计充值金额(分)"`
|
||||
ActivatedAt *time.Time `json:"activated_at,omitempty" description:"激活时间"`
|
||||
CreatedAt time.Time `json:"created_at" description:"创建时间"`
|
||||
UpdatedAt time.Time `json:"updated_at" description:"更新时间"`
|
||||
}
|
||||
|
||||
type ListDeviceResponse struct {
|
||||
@@ -122,3 +126,23 @@ type RecallDevicesResponse struct {
|
||||
FailCount int `json:"fail_count" description:"失败数量"`
|
||||
FailedItems []AllocationDeviceFailedItem `json:"failed_items" description:"失败详情列表"`
|
||||
}
|
||||
|
||||
// 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:"套餐系列分配ID(0表示清除关联)"`
|
||||
}
|
||||
|
||||
// DeviceSeriesBindngFailedItem 设备系列绑定失败项
|
||||
type DeviceSeriesBindngFailedItem struct {
|
||||
DeviceID uint `json:"device_id" description:"设备ID"`
|
||||
DeviceNo string `json:"device_no" description:"设备号"`
|
||||
Reason string `json:"reason" description:"失败原因"`
|
||||
}
|
||||
|
||||
// BatchSetDeviceSeriesBindngResponse 批量设置设备的套餐系列绑定响应
|
||||
type BatchSetDeviceSeriesBindngResponse struct {
|
||||
SuccessCount int `json:"success_count" description:"成功数量"`
|
||||
FailCount int `json:"fail_count" description:"失败数量"`
|
||||
FailedItems []DeviceSeriesBindngFailedItem `json:"failed_items" description:"失败详情列表"`
|
||||
}
|
||||
|
||||
@@ -3,45 +3,49 @@ 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"`
|
||||
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"`
|
||||
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结束号"`
|
||||
}
|
||||
|
||||
type StandaloneIotCardResponse struct {
|
||||
ID uint `json:"id" description:"卡ID"`
|
||||
ICCID string `json:"iccid" description:"ICCID"`
|
||||
CardType string `json:"card_type" description:"卡类型"`
|
||||
CardCategory string `json:"card_category" description:"卡业务类型 (normal:普通卡, industry:行业卡)"`
|
||||
CarrierID uint `json:"carrier_id" description:"运营商ID"`
|
||||
CarrierType string `json:"carrier_type,omitempty" description:"运营商类型 (CMCC:中国移动, CUCC:中国联通, CTCC:中国电信, CBN:中国广电)"`
|
||||
CarrierName string `json:"carrier_name,omitempty" description:"运营商名称"`
|
||||
IMSI string `json:"imsi,omitempty" description:"IMSI"`
|
||||
MSISDN string `json:"msisdn,omitempty" description:"卡接入号"`
|
||||
BatchNo string `json:"batch_no,omitempty" description:"批次号"`
|
||||
Supplier string `json:"supplier,omitempty" description:"供应商"`
|
||||
CostPrice int64 `json:"cost_price" description:"成本价(分)"`
|
||||
DistributePrice int64 `json:"distribute_price" description:"分销价(分)"`
|
||||
Status int `json:"status" description:"状态 (1:在库, 2:已分销, 3:已激活, 4:已停用)"`
|
||||
ShopID *uint `json:"shop_id,omitempty" description:"店铺ID"`
|
||||
ShopName string `json:"shop_name,omitempty" description:"店铺名称"`
|
||||
ActivatedAt *time.Time `json:"activated_at,omitempty" description:"激活时间"`
|
||||
ActivationStatus int `json:"activation_status" description:"激活状态 (0:未激活, 1:已激活)"`
|
||||
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)"`
|
||||
CreatedAt time.Time `json:"created_at" description:"创建时间"`
|
||||
UpdatedAt time.Time `json:"updated_at" description:"更新时间"`
|
||||
ID uint `json:"id" description:"卡ID"`
|
||||
ICCID string `json:"iccid" description:"ICCID"`
|
||||
CardType string `json:"card_type" description:"卡类型"`
|
||||
CardCategory string `json:"card_category" description:"卡业务类型 (normal:普通卡, industry:行业卡)"`
|
||||
CarrierID uint `json:"carrier_id" description:"运营商ID"`
|
||||
CarrierType string `json:"carrier_type,omitempty" description:"运营商类型 (CMCC:中国移动, CUCC:中国联通, CTCC:中国电信, CBN:中国广电)"`
|
||||
CarrierName string `json:"carrier_name,omitempty" description:"运营商名称"`
|
||||
IMSI string `json:"imsi,omitempty" description:"IMSI"`
|
||||
MSISDN string `json:"msisdn,omitempty" description:"卡接入号"`
|
||||
BatchNo string `json:"batch_no,omitempty" description:"批次号"`
|
||||
Supplier string `json:"supplier,omitempty" description:"供应商"`
|
||||
CostPrice int64 `json:"cost_price" description:"成本价(分)"`
|
||||
DistributePrice int64 `json:"distribute_price" description:"分销价(分)"`
|
||||
Status int `json:"status" description:"状态 (1:在库, 2:已分销, 3:已激活, 4:已停用)"`
|
||||
ShopID *uint `json:"shop_id,omitempty" description:"店铺ID"`
|
||||
ShopName string `json:"shop_name,omitempty" description:"店铺名称"`
|
||||
ActivatedAt *time.Time `json:"activated_at,omitempty" description:"激活时间"`
|
||||
ActivationStatus int `json:"activation_status" description:"激活状态 (0:未激活, 1:已激活)"`
|
||||
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"`
|
||||
FirstCommissionPaid bool `json:"first_commission_paid" description:"一次性佣金是否已发放"`
|
||||
AccumulatedRecharge int64 `json:"accumulated_recharge" description:"累计充值金额(分)"`
|
||||
CreatedAt time.Time `json:"created_at" description:"创建时间"`
|
||||
UpdatedAt time.Time `json:"updated_at" description:"更新时间"`
|
||||
}
|
||||
|
||||
type ListStandaloneIotCardResponse struct {
|
||||
@@ -126,3 +130,22 @@ type GetIotCardByICCIDRequest struct {
|
||||
type IotCardDetailResponse struct {
|
||||
StandaloneIotCardResponse
|
||||
}
|
||||
|
||||
// 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:"套餐系列分配ID(0表示清除关联)"`
|
||||
}
|
||||
|
||||
// CardSeriesBindngFailedItem 卡系列绑定失败项
|
||||
type CardSeriesBindngFailedItem struct {
|
||||
ICCID string `json:"iccid" description:"ICCID"`
|
||||
Reason string `json:"reason" description:"失败原因"`
|
||||
}
|
||||
|
||||
// BatchSetCardSeriesBindngResponse 批量设置卡的套餐系列绑定响应
|
||||
type BatchSetCardSeriesBindngResponse struct {
|
||||
SuccessCount int `json:"success_count" description:"成功数量"`
|
||||
FailCount int `json:"fail_count" description:"失败数量"`
|
||||
FailedItems []CardSeriesBindngFailedItem `json:"failed_items" description:"失败详情列表"`
|
||||
}
|
||||
|
||||
@@ -35,6 +35,9 @@ 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"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
|
||||
@@ -132,4 +132,13 @@ func registerDeviceRoutes(router fiber.Router, handler *admin.DeviceHandler, imp
|
||||
Output: new(dto.DeviceImportTaskDetailResponse),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(devices, doc, groupPath, "PATCH", "/series-binding", handler.BatchSetSeriesBinding, RouteSpec{
|
||||
Summary: "批量设置设备的套餐系列绑定",
|
||||
Description: "批量设置或清除设备与套餐系列分配的关联关系。series_allocation_id 为 0 时表示清除关联。",
|
||||
Tags: []string{"设备管理"},
|
||||
Input: new(dto.BatchSetDeviceSeriesBindngRequest),
|
||||
Output: new(dto.BatchSetDeviceSeriesBindngResponse),
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -92,4 +92,13 @@ func registerIotCardRoutes(router fiber.Router, handler *admin.IotCardHandler, i
|
||||
Output: new(dto.RecallStandaloneCardsResponse),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(iotCards, doc, groupPath, "PATCH", "/series-binding", handler.BatchSetSeriesBinding, RouteSpec{
|
||||
Summary: "批量设置卡的套餐系列绑定",
|
||||
Description: "批量设置或清除卡与套餐系列分配的关联关系。series_allocation_id 为 0 时表示清除关联。",
|
||||
Tags: []string{"IoT卡管理"},
|
||||
Input: new(dto.BatchSetCardSeriesBindngRequest),
|
||||
Output: new(dto.BatchSetCardSeriesBindngResponse),
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ type Service struct {
|
||||
iotCardStore *postgres.IotCardStore
|
||||
shopStore *postgres.ShopStore
|
||||
assetAllocationRecordStore *postgres.AssetAllocationRecordStore
|
||||
seriesAllocationStore *postgres.ShopSeriesAllocationStore
|
||||
}
|
||||
|
||||
func New(
|
||||
@@ -28,6 +29,7 @@ func New(
|
||||
iotCardStore *postgres.IotCardStore,
|
||||
shopStore *postgres.ShopStore,
|
||||
assetAllocationRecordStore *postgres.AssetAllocationRecordStore,
|
||||
seriesAllocationStore *postgres.ShopSeriesAllocationStore,
|
||||
) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
@@ -36,6 +38,7 @@ func New(
|
||||
iotCardStore: iotCardStore,
|
||||
shopStore: shopStore,
|
||||
assetAllocationRecordStore: assetAllocationRecordStore,
|
||||
seriesAllocationStore: seriesAllocationStore,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +86,9 @@ 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
|
||||
}
|
||||
|
||||
devices, total, err := s.deviceStore.List(ctx, opts, filters)
|
||||
if err != nil {
|
||||
@@ -448,21 +454,24 @@ func (s *Service) extractDeviceIDs(devices []*model.Device) []uint {
|
||||
|
||||
func (s *Service) toDeviceResponse(device *model.Device, shopMap map[uint]string, bindingCounts map[uint]int64) *dto.DeviceResponse {
|
||||
resp := &dto.DeviceResponse{
|
||||
ID: device.ID,
|
||||
DeviceNo: device.DeviceNo,
|
||||
DeviceName: device.DeviceName,
|
||||
DeviceModel: device.DeviceModel,
|
||||
DeviceType: device.DeviceType,
|
||||
MaxSimSlots: device.MaxSimSlots,
|
||||
Manufacturer: device.Manufacturer,
|
||||
BatchNo: device.BatchNo,
|
||||
ShopID: device.ShopID,
|
||||
Status: device.Status,
|
||||
StatusName: s.getDeviceStatusName(device.Status),
|
||||
BoundCardCount: int(bindingCounts[device.ID]),
|
||||
ActivatedAt: device.ActivatedAt,
|
||||
CreatedAt: device.CreatedAt,
|
||||
UpdatedAt: device.UpdatedAt,
|
||||
ID: device.ID,
|
||||
DeviceNo: device.DeviceNo,
|
||||
DeviceName: device.DeviceName,
|
||||
DeviceModel: device.DeviceModel,
|
||||
DeviceType: device.DeviceType,
|
||||
MaxSimSlots: device.MaxSimSlots,
|
||||
Manufacturer: device.Manufacturer,
|
||||
BatchNo: device.BatchNo,
|
||||
ShopID: device.ShopID,
|
||||
Status: device.Status,
|
||||
StatusName: s.getDeviceStatusName(device.Status),
|
||||
BoundCardCount: int(bindingCounts[device.ID]),
|
||||
SeriesAllocationID: device.SeriesAllocationID,
|
||||
FirstCommissionPaid: device.FirstCommissionPaid,
|
||||
AccumulatedRecharge: device.AccumulatedRecharge,
|
||||
ActivatedAt: device.ActivatedAt,
|
||||
CreatedAt: device.CreatedAt,
|
||||
UpdatedAt: device.UpdatedAt,
|
||||
}
|
||||
|
||||
if device.ShopID != nil && *device.ShopID > 0 {
|
||||
@@ -568,3 +577,105 @@ func (s *Service) buildRecallRecords(devices []*model.Device, successDeviceIDs [
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
// BatchSetSeriesBinding 批量设置设备的套餐系列绑定
|
||||
func (s *Service) BatchSetSeriesBinding(ctx context.Context, req *dto.BatchSetDeviceSeriesBindngRequest, operatorShopID *uint) (*dto.BatchSetDeviceSeriesBindngResponse, error) {
|
||||
devices, err := s.deviceStore.GetByIDs(ctx, req.DeviceIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(devices) == 0 {
|
||||
return &dto.BatchSetDeviceSeriesBindngResponse{
|
||||
SuccessCount: 0,
|
||||
FailCount: len(req.DeviceIDs),
|
||||
FailedItems: s.buildDeviceNotFoundFailedItems(req.DeviceIDs),
|
||||
}, nil
|
||||
}
|
||||
|
||||
deviceMap := make(map[uint]*model.Device)
|
||||
for _, device := range devices {
|
||||
deviceMap[device.ID] = device
|
||||
}
|
||||
|
||||
var seriesAllocation *model.ShopSeriesAllocation
|
||||
if req.SeriesAllocationID > 0 {
|
||||
seriesAllocation, err = s.seriesAllocationStore.GetByID(ctx, req.SeriesAllocationID)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, errors.New(errors.CodeNotFound, "套餐系列分配不存在")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if seriesAllocation.Status != 1 {
|
||||
return nil, errors.New(errors.CodeInvalidParam, "套餐系列分配已禁用")
|
||||
}
|
||||
}
|
||||
|
||||
var successDeviceIDs []uint
|
||||
var failedItems []dto.DeviceSeriesBindngFailedItem
|
||||
|
||||
for _, deviceID := range req.DeviceIDs {
|
||||
device, exists := deviceMap[deviceID]
|
||||
if !exists {
|
||||
failedItems = append(failedItems, dto.DeviceSeriesBindngFailedItem{
|
||||
DeviceID: deviceID,
|
||||
DeviceNo: "",
|
||||
Reason: "设备不存在",
|
||||
})
|
||||
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 {
|
||||
if device.ShopID == nil || *device.ShopID != *operatorShopID {
|
||||
failedItems = append(failedItems, dto.DeviceSeriesBindngFailedItem{
|
||||
DeviceID: device.ID,
|
||||
DeviceNo: device.DeviceNo,
|
||||
Reason: "无权操作此设备",
|
||||
})
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
successDeviceIDs = append(successDeviceIDs, device.ID)
|
||||
}
|
||||
|
||||
if len(successDeviceIDs) > 0 {
|
||||
var seriesAllocationIDPtr *uint
|
||||
if req.SeriesAllocationID > 0 {
|
||||
seriesAllocationIDPtr = &req.SeriesAllocationID
|
||||
}
|
||||
if err := s.deviceStore.BatchUpdateSeriesAllocation(ctx, successDeviceIDs, seriesAllocationIDPtr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &dto.BatchSetDeviceSeriesBindngResponse{
|
||||
SuccessCount: len(successDeviceIDs),
|
||||
FailCount: len(failedItems),
|
||||
FailedItems: failedItems,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) buildDeviceNotFoundFailedItems(deviceIDs []uint) []dto.DeviceSeriesBindngFailedItem {
|
||||
items := make([]dto.DeviceSeriesBindngFailedItem, len(deviceIDs))
|
||||
for i, id := range deviceIDs {
|
||||
items[i] = dto.DeviceSeriesBindngFailedItem{
|
||||
DeviceID: id,
|
||||
DeviceNo: "",
|
||||
Reason: "设备不存在",
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
149
internal/service/device/service_test.go
Normal file
149
internal/service/device/service_test.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func uniqueTestDeviceNoPrefix() string {
|
||||
return fmt.Sprintf("D%d", time.Now().UnixNano()%1000000000)
|
||||
}
|
||||
|
||||
func TestDeviceService_BatchSetSeriesBinding(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
deviceStore := postgres.NewDeviceStore(tx, rdb)
|
||||
deviceSimBindingStore := postgres.NewDeviceSimBindingStore(tx, rdb)
|
||||
iotCardStore := postgres.NewIotCardStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
assetAllocationRecordStore := postgres.NewAssetAllocationRecordStore(tx, rdb)
|
||||
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
|
||||
|
||||
svc := New(tx, deviceStore, deviceSimBindingStore, iotCardStore, shopStore, assetAllocationRecordStore, seriesAllocationStore)
|
||||
ctx := context.Background()
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试店铺",
|
||||
ShopCode: fmt.Sprintf("SHOP%d", time.Now().UnixNano()%1000000),
|
||||
Level: 1,
|
||||
Status: 1,
|
||||
}
|
||||
require.NoError(t, tx.Create(shop).Error)
|
||||
|
||||
series := &model.PackageSeries{
|
||||
SeriesCode: fmt.Sprintf("SERIES%d", time.Now().UnixNano()%1000000),
|
||||
SeriesName: "测试系列",
|
||||
Status: 1,
|
||||
}
|
||||
require.NoError(t, tx.Create(series).Error)
|
||||
|
||||
allocation := &model.ShopSeriesAllocation{
|
||||
ShopID: shop.ID,
|
||||
SeriesID: series.ID,
|
||||
Status: 1,
|
||||
}
|
||||
require.NoError(t, tx.Create(allocation).Error)
|
||||
|
||||
prefix := uniqueTestDeviceNoPrefix()
|
||||
devices := []*model.Device{
|
||||
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1, ShopID: &shop.ID},
|
||||
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1, ShopID: &shop.ID},
|
||||
{DeviceNo: prefix + "003", DeviceName: "测试设备3", Status: 1, ShopID: nil},
|
||||
}
|
||||
require.NoError(t, deviceStore.CreateBatch(ctx, devices))
|
||||
|
||||
t.Run("成功设置系列绑定", func(t *testing.T) {
|
||||
req := &dto.BatchSetDeviceSeriesBindngRequest{
|
||||
DeviceIDs: []uint{devices[0].ID, devices[1].ID},
|
||||
SeriesAllocationID: allocation.ID,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, resp.SuccessCount)
|
||||
assert.Equal(t, 0, resp.FailCount)
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("设备不属于套餐系列分配的店铺", func(t *testing.T) {
|
||||
req := &dto.BatchSetDeviceSeriesBindngRequest{
|
||||
DeviceIDs: []uint{devices[2].ID},
|
||||
SeriesAllocationID: allocation.ID,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, resp.SuccessCount)
|
||||
assert.Equal(t, 1, resp.FailCount)
|
||||
assert.Equal(t, "设备不属于套餐系列分配的店铺", resp.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("设备不存在", func(t *testing.T) {
|
||||
req := &dto.BatchSetDeviceSeriesBindngRequest{
|
||||
DeviceIDs: []uint{99999},
|
||||
SeriesAllocationID: allocation.ID,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, resp.SuccessCount)
|
||||
assert.Equal(t, 1, resp.FailCount)
|
||||
assert.Equal(t, "设备不存在", resp.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("清除系列绑定", func(t *testing.T) {
|
||||
req := &dto.BatchSetDeviceSeriesBindngRequest{
|
||||
DeviceIDs: []uint{devices[0].ID},
|
||||
SeriesAllocationID: 0,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, resp.SuccessCount)
|
||||
|
||||
var updatedDevice model.Device
|
||||
require.NoError(t, tx.First(&updatedDevice, devices[0].ID).Error)
|
||||
assert.Nil(t, updatedDevice.SeriesAllocationID)
|
||||
})
|
||||
|
||||
t.Run("代理用户只能操作自己店铺的设备", func(t *testing.T) {
|
||||
otherShopID := uint(99999)
|
||||
req := &dto.BatchSetDeviceSeriesBindngRequest{
|
||||
DeviceIDs: []uint{devices[1].ID},
|
||||
SeriesAllocationID: 0,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, &otherShopID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, resp.SuccessCount)
|
||||
assert.Equal(t, 1, resp.FailCount)
|
||||
assert.Equal(t, "无权操作此设备", resp.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("套餐系列分配不存在", func(t *testing.T) {
|
||||
req := &dto.BatchSetDeviceSeriesBindngRequest{
|
||||
DeviceIDs: []uint{devices[1].ID},
|
||||
SeriesAllocationID: 99999,
|
||||
}
|
||||
|
||||
_, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
@@ -17,6 +17,7 @@ type Service struct {
|
||||
iotCardStore *postgres.IotCardStore
|
||||
shopStore *postgres.ShopStore
|
||||
assetAllocationRecordStore *postgres.AssetAllocationRecordStore
|
||||
seriesAllocationStore *postgres.ShopSeriesAllocationStore
|
||||
}
|
||||
|
||||
func New(
|
||||
@@ -24,12 +25,14 @@ func New(
|
||||
iotCardStore *postgres.IotCardStore,
|
||||
shopStore *postgres.ShopStore,
|
||||
assetAllocationRecordStore *postgres.AssetAllocationRecordStore,
|
||||
seriesAllocationStore *postgres.ShopSeriesAllocationStore,
|
||||
) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
iotCardStore: iotCardStore,
|
||||
shopStore: shopStore,
|
||||
assetAllocationRecordStore: assetAllocationRecordStore,
|
||||
seriesAllocationStore: seriesAllocationStore,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +85,9 @@ 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
|
||||
}
|
||||
|
||||
cards, total, err := s.iotCardStore.ListStandalone(ctx, opts, filters)
|
||||
if err != nil {
|
||||
@@ -153,28 +159,31 @@ func (s *Service) loadShopNames(ctx context.Context, cards []*model.IotCard) map
|
||||
|
||||
func (s *Service) toStandaloneResponse(card *model.IotCard, shopMap map[uint]string) *dto.StandaloneIotCardResponse {
|
||||
resp := &dto.StandaloneIotCardResponse{
|
||||
ID: card.ID,
|
||||
ICCID: card.ICCID,
|
||||
CardType: card.CardType,
|
||||
CardCategory: card.CardCategory,
|
||||
CarrierID: card.CarrierID,
|
||||
CarrierType: card.CarrierType,
|
||||
CarrierName: card.CarrierName,
|
||||
IMSI: card.IMSI,
|
||||
MSISDN: card.MSISDN,
|
||||
BatchNo: card.BatchNo,
|
||||
Supplier: card.Supplier,
|
||||
CostPrice: card.CostPrice,
|
||||
DistributePrice: card.DistributePrice,
|
||||
Status: card.Status,
|
||||
ShopID: card.ShopID,
|
||||
ActivatedAt: card.ActivatedAt,
|
||||
ActivationStatus: card.ActivationStatus,
|
||||
RealNameStatus: card.RealNameStatus,
|
||||
NetworkStatus: card.NetworkStatus,
|
||||
DataUsageMB: card.DataUsageMB,
|
||||
CreatedAt: card.CreatedAt,
|
||||
UpdatedAt: card.UpdatedAt,
|
||||
ID: card.ID,
|
||||
ICCID: card.ICCID,
|
||||
CardType: card.CardType,
|
||||
CardCategory: card.CardCategory,
|
||||
CarrierID: card.CarrierID,
|
||||
CarrierType: card.CarrierType,
|
||||
CarrierName: card.CarrierName,
|
||||
IMSI: card.IMSI,
|
||||
MSISDN: card.MSISDN,
|
||||
BatchNo: card.BatchNo,
|
||||
Supplier: card.Supplier,
|
||||
CostPrice: card.CostPrice,
|
||||
DistributePrice: card.DistributePrice,
|
||||
Status: card.Status,
|
||||
ShopID: card.ShopID,
|
||||
ActivatedAt: card.ActivatedAt,
|
||||
ActivationStatus: card.ActivationStatus,
|
||||
RealNameStatus: card.RealNameStatus,
|
||||
NetworkStatus: card.NetworkStatus,
|
||||
DataUsageMB: card.DataUsageMB,
|
||||
SeriesAllocationID: card.SeriesAllocationID,
|
||||
FirstCommissionPaid: card.FirstCommissionPaid,
|
||||
AccumulatedRecharge: card.AccumulatedRecharge,
|
||||
CreatedAt: card.CreatedAt,
|
||||
UpdatedAt: card.UpdatedAt,
|
||||
}
|
||||
|
||||
if card.ShopID != nil && *card.ShopID > 0 {
|
||||
@@ -533,3 +542,101 @@ func (s *Service) buildRecallRecords(cards []*model.IotCard, successCardIDs []ui
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
// BatchSetSeriesBinding 批量设置卡的套餐系列绑定
|
||||
func (s *Service) BatchSetSeriesBinding(ctx context.Context, req *dto.BatchSetCardSeriesBindngRequest, operatorShopID *uint) (*dto.BatchSetCardSeriesBindngResponse, error) {
|
||||
cards, err := s.iotCardStore.GetByICCIDs(ctx, req.ICCIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(cards) == 0 {
|
||||
return &dto.BatchSetCardSeriesBindngResponse{
|
||||
SuccessCount: 0,
|
||||
FailCount: len(req.ICCIDs),
|
||||
FailedItems: s.buildCardNotFoundFailedItems(req.ICCIDs),
|
||||
}, nil
|
||||
}
|
||||
|
||||
cardMap := make(map[string]*model.IotCard)
|
||||
for _, card := range cards {
|
||||
cardMap[card.ICCID] = card
|
||||
}
|
||||
|
||||
var seriesAllocation *model.ShopSeriesAllocation
|
||||
if req.SeriesAllocationID > 0 {
|
||||
seriesAllocation, err = s.seriesAllocationStore.GetByID(ctx, req.SeriesAllocationID)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, errors.New(errors.CodeNotFound, "套餐系列分配不存在")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if seriesAllocation.Status != 1 {
|
||||
return nil, errors.New(errors.CodeInvalidParam, "套餐系列分配已禁用")
|
||||
}
|
||||
}
|
||||
|
||||
var successCardIDs []uint
|
||||
var failedItems []dto.CardSeriesBindngFailedItem
|
||||
|
||||
for _, iccid := range req.ICCIDs {
|
||||
card, exists := cardMap[iccid]
|
||||
if !exists {
|
||||
failedItems = append(failedItems, dto.CardSeriesBindngFailedItem{
|
||||
ICCID: iccid,
|
||||
Reason: "卡不存在",
|
||||
})
|
||||
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 {
|
||||
if card.ShopID == nil || *card.ShopID != *operatorShopID {
|
||||
failedItems = append(failedItems, dto.CardSeriesBindngFailedItem{
|
||||
ICCID: iccid,
|
||||
Reason: "无权操作此卡",
|
||||
})
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
successCardIDs = append(successCardIDs, card.ID)
|
||||
}
|
||||
|
||||
if len(successCardIDs) > 0 {
|
||||
var seriesAllocationIDPtr *uint
|
||||
if req.SeriesAllocationID > 0 {
|
||||
seriesAllocationIDPtr = &req.SeriesAllocationID
|
||||
}
|
||||
if err := s.iotCardStore.BatchUpdateSeriesAllocation(ctx, successCardIDs, seriesAllocationIDPtr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &dto.BatchSetCardSeriesBindngResponse{
|
||||
SuccessCount: len(successCardIDs),
|
||||
FailCount: len(failedItems),
|
||||
FailedItems: failedItems,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) buildCardNotFoundFailedItems(iccids []string) []dto.CardSeriesBindngFailedItem {
|
||||
items := make([]dto.CardSeriesBindngFailedItem, len(iccids))
|
||||
for i, iccid := range iccids {
|
||||
items[i] = dto.CardSeriesBindngFailedItem{
|
||||
ICCID: iccid,
|
||||
Reason: "卡不存在",
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
147
internal/service/iot_card/service_test.go
Normal file
147
internal/service/iot_card/service_test.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package iot_card
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func uniqueTestICCIDPrefix() string {
|
||||
return fmt.Sprintf("T%d", time.Now().UnixNano()%1000000000)
|
||||
}
|
||||
|
||||
func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
iotCardStore := postgres.NewIotCardStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
assetAllocationRecordStore := postgres.NewAssetAllocationRecordStore(tx, rdb)
|
||||
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
|
||||
|
||||
svc := New(tx, iotCardStore, shopStore, assetAllocationRecordStore, seriesAllocationStore)
|
||||
ctx := context.Background()
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试店铺",
|
||||
ShopCode: fmt.Sprintf("SHOP%d", time.Now().UnixNano()%1000000),
|
||||
Level: 1,
|
||||
Status: 1,
|
||||
}
|
||||
require.NoError(t, tx.Create(shop).Error)
|
||||
|
||||
series := &model.PackageSeries{
|
||||
SeriesCode: fmt.Sprintf("SERIES%d", time.Now().UnixNano()%1000000),
|
||||
SeriesName: "测试系列",
|
||||
Status: 1,
|
||||
}
|
||||
require.NoError(t, tx.Create(series).Error)
|
||||
|
||||
allocation := &model.ShopSeriesAllocation{
|
||||
ShopID: shop.ID,
|
||||
SeriesID: series.ID,
|
||||
Status: 1,
|
||||
}
|
||||
require.NoError(t, tx.Create(allocation).Error)
|
||||
|
||||
prefix := uniqueTestICCIDPrefix()
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: prefix + "001", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: &shop.ID},
|
||||
{ICCID: prefix + "002", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: &shop.ID},
|
||||
{ICCID: prefix + "003", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil},
|
||||
}
|
||||
require.NoError(t, iotCardStore.CreateBatch(ctx, cards))
|
||||
|
||||
t.Run("成功设置系列绑定", func(t *testing.T) {
|
||||
req := &dto.BatchSetCardSeriesBindngRequest{
|
||||
ICCIDs: []string{prefix + "001", prefix + "002"},
|
||||
SeriesAllocationID: allocation.ID,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, resp.SuccessCount)
|
||||
assert.Equal(t, 0, resp.FailCount)
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("卡不属于套餐系列分配的店铺", func(t *testing.T) {
|
||||
req := &dto.BatchSetCardSeriesBindngRequest{
|
||||
ICCIDs: []string{prefix + "003"},
|
||||
SeriesAllocationID: allocation.ID,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, resp.SuccessCount)
|
||||
assert.Equal(t, 1, resp.FailCount)
|
||||
assert.Equal(t, "卡不属于套餐系列分配的店铺", resp.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("卡不存在", func(t *testing.T) {
|
||||
req := &dto.BatchSetCardSeriesBindngRequest{
|
||||
ICCIDs: []string{"NOTEXIST001"},
|
||||
SeriesAllocationID: allocation.ID,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, resp.SuccessCount)
|
||||
assert.Equal(t, 1, resp.FailCount)
|
||||
assert.Equal(t, "卡不存在", resp.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("清除系列绑定", func(t *testing.T) {
|
||||
req := &dto.BatchSetCardSeriesBindngRequest{
|
||||
ICCIDs: []string{prefix + "001"},
|
||||
SeriesAllocationID: 0,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, resp.SuccessCount)
|
||||
|
||||
var updatedCard model.IotCard
|
||||
require.NoError(t, tx.Where("iccid = ?", prefix+"001").First(&updatedCard).Error)
|
||||
assert.Nil(t, updatedCard.SeriesAllocationID)
|
||||
})
|
||||
|
||||
t.Run("代理用户只能操作自己店铺的卡", func(t *testing.T) {
|
||||
otherShopID := uint(99999)
|
||||
req := &dto.BatchSetCardSeriesBindngRequest{
|
||||
ICCIDs: []string{prefix + "002"},
|
||||
SeriesAllocationID: 0,
|
||||
}
|
||||
|
||||
resp, err := svc.BatchSetSeriesBinding(ctx, req, &otherShopID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, resp.SuccessCount)
|
||||
assert.Equal(t, 1, resp.FailCount)
|
||||
assert.Equal(t, "无权操作此卡", resp.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("套餐系列分配不存在", func(t *testing.T) {
|
||||
req := &dto.BatchSetCardSeriesBindngRequest{
|
||||
ICCIDs: []string{prefix + "002"},
|
||||
SeriesAllocationID: 99999,
|
||||
}
|
||||
|
||||
_, err := svc.BatchSetSeriesBinding(ctx, req, nil)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
@@ -106,6 +106,9 @@ 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 err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
@@ -181,3 +184,22 @@ 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 {
|
||||
if len(deviceIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return s.db.WithContext(ctx).Model(&model.Device{}).
|
||||
Where("id IN ?", deviceIDs).
|
||||
Update("series_allocation_id", seriesAllocationID).Error
|
||||
}
|
||||
|
||||
// ListBySeriesAllocationID 根据套餐系列分配ID查询设备列表
|
||||
func (s *DeviceStore) ListBySeriesAllocationID(ctx context.Context, seriesAllocationID uint) ([]*model.Device, error) {
|
||||
var devices []*model.Device
|
||||
if err := s.db.WithContext(ctx).Where("series_allocation_id = ?", seriesAllocationID).Find(&devices).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
119
internal/store/postgres/device_store_test.go
Normal file
119
internal/store/postgres/device_store_test.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func uniqueDeviceNoPrefix() string {
|
||||
return fmt.Sprintf("D%d", time.Now().UnixNano()%1000000000)
|
||||
}
|
||||
|
||||
func TestDeviceStore_BatchUpdateSeriesAllocation(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
s := NewDeviceStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
prefix := uniqueDeviceNoPrefix()
|
||||
devices := []*model.Device{
|
||||
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1},
|
||||
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, devices))
|
||||
|
||||
t.Run("设置系列分配ID", func(t *testing.T) {
|
||||
seriesAllocationID := uint(100)
|
||||
deviceIDs := []uint{devices[0].ID, devices[1].ID}
|
||||
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, deviceIDs, &seriesAllocationID)
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("清除系列分配ID", func(t *testing.T) {
|
||||
deviceIDs := []uint{devices[0].ID}
|
||||
|
||||
err := s.BatchUpdateSeriesAllocation(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)
|
||||
})
|
||||
|
||||
t.Run("空列表不报错", func(t *testing.T) {
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, []uint{}, nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeviceStore_ListBySeriesAllocationID(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
s := NewDeviceStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
prefix := uniqueDeviceNoPrefix()
|
||||
seriesAllocationID := 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},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, devices))
|
||||
|
||||
result, err := s.ListBySeriesAllocationID(ctx, seriesAllocationID)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, result, 2)
|
||||
for _, device := range result {
|
||||
assert.Equal(t, seriesAllocationID, *device.SeriesAllocationID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceStore_List_SeriesAllocationFilter(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
s := NewDeviceStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
prefix := uniqueDeviceNoPrefix()
|
||||
seriesAllocationID := 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},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, devices))
|
||||
|
||||
filters := map[string]interface{}{
|
||||
"series_allocation_id": seriesAllocationID,
|
||||
"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)
|
||||
}
|
||||
}
|
||||
@@ -147,6 +147,9 @@ 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 err := query.Count(&total).Error; err != nil {
|
||||
@@ -239,6 +242,9 @@ 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 err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
@@ -374,3 +380,24 @@ func (s *IotCardStore) GetByIDsWithEnterpriseFilter(ctx context.Context, cardIDs
|
||||
}
|
||||
return cards, nil
|
||||
}
|
||||
|
||||
// BatchUpdateSeriesAllocation 批量更新卡的套餐系列分配
|
||||
// 用于批量设置或清除卡与套餐系列的关联关系
|
||||
func (s *IotCardStore) BatchUpdateSeriesAllocation(ctx context.Context, cardIDs []uint, seriesAllocationID *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
|
||||
}
|
||||
|
||||
// ListBySeriesAllocationID 根据套餐系列分配ID查询卡列表
|
||||
// 用于查询某个套餐系列分配下的所有卡
|
||||
func (s *IotCardStore) ListBySeriesAllocationID(ctx context.Context, seriesAllocationID uint) ([]*model.IotCard, error) {
|
||||
var cards []*model.IotCard
|
||||
if err := s.db.WithContext(ctx).Where("series_allocation_id = ?", seriesAllocationID).Find(&cards).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cards, nil
|
||||
}
|
||||
|
||||
@@ -425,3 +425,103 @@ func TestIotCardStore_GetBoundCardIDs(t *testing.T) {
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIotCardStore_BatchUpdateSeriesAllocation(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
s := NewIotCardStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: "89860012345678910001", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678910002", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
t.Run("设置系列分配ID", func(t *testing.T) {
|
||||
seriesAllocationID := uint(100)
|
||||
cardIDs := []uint{cards[0].ID, cards[1].ID}
|
||||
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, cardIDs, &seriesAllocationID)
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("清除系列分配ID", func(t *testing.T) {
|
||||
cardIDs := []uint{cards[0].ID}
|
||||
|
||||
err := s.BatchUpdateSeriesAllocation(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)
|
||||
})
|
||||
|
||||
t.Run("空列表不报错", func(t *testing.T) {
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, []uint{}, nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIotCardStore_ListBySeriesAllocationID(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
s := NewIotCardStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
seriesAllocationID := 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},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
result, err := s.ListBySeriesAllocationID(ctx, seriesAllocationID)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, result, 2)
|
||||
for _, card := range result {
|
||||
assert.Equal(t, seriesAllocationID, *card.SeriesAllocationID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIotCardStore_ListStandalone_SeriesAllocationFilter(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
s := NewIotCardStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
prefix := uniqueICCIDPrefix()
|
||||
seriesAllocationID := 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},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
filters := map[string]interface{}{
|
||||
"series_allocation_id": seriesAllocationID,
|
||||
"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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user