refactor(account): 移除卡类型字段、优化账号列表查询和权限检查
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m18s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m18s
- 移除 IoT 卡和号卡的 card_type 字段(数据库迁移) - 优化账号列表查询,支持按店铺和企业筛选 - 账号响应增加店铺名称和企业名称字段 - 实现批量加载店铺和企业名称,避免 N+1 查询 - 更新权限检查中间件,完善权限验证逻辑 - 更新相关测试用例,确保功能正确性
This commit is contained in:
@@ -20,27 +20,31 @@ type UpdateAccountRequest struct {
|
||||
|
||||
// AccountListRequest 账号列表查询请求
|
||||
type AccountListRequest 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:"每页数量"`
|
||||
Username string `json:"username" query:"username" validate:"omitempty,max=50" maxLength:"50" description:"用户名模糊查询"`
|
||||
Phone string `json:"phone" query:"phone" validate:"omitempty,max=20" maxLength:"20" description:"手机号模糊查询"`
|
||||
UserType *int `json:"user_type" query:"user_type" validate:"omitempty,min=1,max=4" minimum:"1" maximum:"4" description:"用户类型 (1:超级管理员, 2:平台用户, 3:代理账号, 4:企业账号)"`
|
||||
Status *int `json:"status" query:"status" validate:"omitempty,min=0,max=1" minimum:"0" maximum:"1" description:"状态 (0:禁用, 1:启用)"`
|
||||
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:"每页数量"`
|
||||
Username string `json:"username" query:"username" validate:"omitempty,max=50" maxLength:"50" description:"用户名模糊查询"`
|
||||
Phone string `json:"phone" query:"phone" validate:"omitempty,max=20" maxLength:"20" description:"手机号模糊查询"`
|
||||
UserType *int `json:"user_type" query:"user_type" validate:"omitempty,min=1,max=4" minimum:"1" maximum:"4" description:"用户类型 (1:超级管理员, 2:平台用户, 3:代理账号, 4:企业账号)"`
|
||||
Status *int `json:"status" query:"status" validate:"omitempty,min=0,max=1" minimum:"0" maximum:"1" description:"状态 (0:禁用, 1:启用)"`
|
||||
ShopID *uint `json:"shop_id" query:"shop_id" validate:"omitempty,min=1" minimum:"1" description:"店铺ID筛选"`
|
||||
EnterpriseID *uint `json:"enterprise_id" query:"enterprise_id" validate:"omitempty,min=1" minimum:"1" description:"企业ID筛选"`
|
||||
}
|
||||
|
||||
// AccountResponse 账号响应
|
||||
type AccountResponse struct {
|
||||
ID uint `json:"id" description:"账号ID"`
|
||||
Username string `json:"username" description:"用户名"`
|
||||
Phone string `json:"phone" description:"手机号"`
|
||||
UserType int `json:"user_type" description:"用户类型 (1:超级管理员, 2:平台用户, 3:代理账号, 4:企业账号)"`
|
||||
ShopID *uint `json:"shop_id,omitempty" description:"关联店铺ID"`
|
||||
EnterpriseID *uint `json:"enterprise_id,omitempty" description:"关联企业ID"`
|
||||
Status int `json:"status" description:"状态 (0:禁用, 1:启用)"`
|
||||
Creator uint `json:"creator" description:"创建人ID"`
|
||||
Updater uint `json:"updater" description:"更新人ID"`
|
||||
CreatedAt string `json:"created_at" description:"创建时间"`
|
||||
UpdatedAt string `json:"updated_at" description:"更新时间"`
|
||||
ID uint `json:"id" description:"账号ID"`
|
||||
Username string `json:"username" description:"用户名"`
|
||||
Phone string `json:"phone" description:"手机号"`
|
||||
UserType int `json:"user_type" description:"用户类型 (1:超级管理员, 2:平台用户, 3:代理账号, 4:企业账号)"`
|
||||
ShopID *uint `json:"shop_id,omitempty" description:"关联店铺ID"`
|
||||
ShopName string `json:"shop_name,omitempty" description:"店铺名称"`
|
||||
EnterpriseID *uint `json:"enterprise_id,omitempty" description:"关联企业ID"`
|
||||
EnterpriseName string `json:"enterprise_name,omitempty" description:"企业名称"`
|
||||
Status int `json:"status" description:"状态 (0:禁用, 1:启用)"`
|
||||
Creator uint `json:"creator" description:"创建人ID"`
|
||||
Updater uint `json:"updater" description:"更新人ID"`
|
||||
CreatedAt string `json:"created_at" description:"创建时间"`
|
||||
UpdatedAt string `json:"updated_at" description:"更新时间"`
|
||||
}
|
||||
|
||||
// AssignRolesRequest 分配角色请求
|
||||
|
||||
@@ -22,7 +22,6 @@ type ListStandaloneIotCardRequest struct {
|
||||
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:中国广电)"`
|
||||
|
||||
@@ -13,7 +13,6 @@ type IotCard struct {
|
||||
gorm.Model
|
||||
BaseModel `gorm:"embedded"`
|
||||
ICCID string `gorm:"column:iccid;type:varchar(20);uniqueIndex:idx_iot_card_iccid,where:deleted_at IS NULL;not null;comment:ICCID(唯一标识,电信19位/其他20位)" json:"iccid"`
|
||||
CardType string `gorm:"column:card_type;type:varchar(50);not null;comment:卡类型" json:"card_type"`
|
||||
CardCategory string `gorm:"column:card_category;type:varchar(20);default:'normal';not null;comment:卡业务类型 normal-普通卡 industry-行业卡" json:"card_category"`
|
||||
CarrierID uint `gorm:"column:carrier_id;index;not null;comment:运营商ID" json:"carrier_id"`
|
||||
CarrierType string `gorm:"column:carrier_type;type:varchar(20);comment:运营商类型(CMCC/CUCC/CTCC/CBN),导入时快照" json:"carrier_type"`
|
||||
|
||||
@@ -12,7 +12,6 @@ type NumberCard struct {
|
||||
BaseModel `gorm:"embedded"`
|
||||
VirtualProductCode string `gorm:"column:virtual_product_code;type:varchar(100);uniqueIndex:idx_number_card_code,where:deleted_at IS NULL;not null;comment:虚拟商品编码(用于对应运营商订单)" json:"virtual_product_code"`
|
||||
CardName string `gorm:"column:card_name;type:varchar(255);not null;comment:号卡名称" json:"card_name"`
|
||||
CardType string `gorm:"column:card_type;type:varchar(50);comment:号卡类型" json:"card_type"`
|
||||
Carrier string `gorm:"column:carrier;type:varchar(50);comment:运营商" json:"carrier"`
|
||||
DataAmountMB int64 `gorm:"column:data_amount_mb;type:bigint;comment:流量额度(MB)" json:"data_amount_mb"`
|
||||
Price int64 `gorm:"column:price;type:bigint;comment:价格(分为单位)" json:"price"`
|
||||
|
||||
@@ -336,7 +336,7 @@ func (s *Service) Delete(ctx context.Context, id uint) error {
|
||||
}
|
||||
|
||||
// List 查询账号列表
|
||||
func (s *Service) List(ctx context.Context, req *dto.AccountListRequest) ([]*model.Account, int64, error) {
|
||||
func (s *Service) List(ctx context.Context, req *dto.AccountListRequest) ([]*dto.AccountResponse, int64, error) {
|
||||
opts := &store.QueryOptions{
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
@@ -362,8 +362,28 @@ func (s *Service) List(ctx context.Context, req *dto.AccountListRequest) ([]*mod
|
||||
if req.Status != nil {
|
||||
filters["status"] = *req.Status
|
||||
}
|
||||
if req.ShopID != nil {
|
||||
filters["shop_id"] = *req.ShopID
|
||||
}
|
||||
if req.EnterpriseID != nil {
|
||||
filters["enterprise_id"] = *req.EnterpriseID
|
||||
}
|
||||
|
||||
return s.accountStore.List(ctx, opts, filters)
|
||||
accounts, total, err := s.accountStore.List(ctx, opts, filters)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
shopMap := s.loadShopNames(ctx, accounts)
|
||||
enterpriseMap := s.loadEnterpriseNames(ctx, accounts)
|
||||
|
||||
responses := make([]*dto.AccountResponse, 0, len(accounts))
|
||||
for _, acc := range accounts {
|
||||
resp := s.toAccountResponse(acc, shopMap, enterpriseMap)
|
||||
responses = append(responses, resp)
|
||||
}
|
||||
|
||||
return responses, total, nil
|
||||
}
|
||||
|
||||
// AssignRoles 为账号分配角色(支持空数组清空所有角色,超级管理员禁止分配)
|
||||
@@ -696,3 +716,78 @@ func (s *Service) CreateSystemAccount(ctx context.Context, account *model.Accoun
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// loadShopNames 批量加载店铺名称
|
||||
func (s *Service) loadShopNames(ctx context.Context, accounts []*model.Account) map[uint]string {
|
||||
shopIDs := make([]uint, 0)
|
||||
shopIDSet := make(map[uint]bool)
|
||||
|
||||
for _, acc := range accounts {
|
||||
if acc.ShopID != nil && *acc.ShopID > 0 && !shopIDSet[*acc.ShopID] {
|
||||
shopIDs = append(shopIDs, *acc.ShopID)
|
||||
shopIDSet[*acc.ShopID] = true
|
||||
}
|
||||
}
|
||||
|
||||
shopMap := make(map[uint]string)
|
||||
if len(shopIDs) > 0 {
|
||||
shops, err := s.shopStore.GetByIDs(ctx, shopIDs)
|
||||
if err == nil {
|
||||
for _, shop := range shops {
|
||||
shopMap[shop.ID] = shop.ShopName
|
||||
}
|
||||
}
|
||||
}
|
||||
return shopMap
|
||||
}
|
||||
|
||||
// loadEnterpriseNames 批量加载企业名称
|
||||
func (s *Service) loadEnterpriseNames(ctx context.Context, accounts []*model.Account) map[uint]string {
|
||||
enterpriseIDs := make([]uint, 0)
|
||||
enterpriseIDSet := make(map[uint]bool)
|
||||
|
||||
for _, acc := range accounts {
|
||||
if acc.EnterpriseID != nil && *acc.EnterpriseID > 0 && !enterpriseIDSet[*acc.EnterpriseID] {
|
||||
enterpriseIDs = append(enterpriseIDs, *acc.EnterpriseID)
|
||||
enterpriseIDSet[*acc.EnterpriseID] = true
|
||||
}
|
||||
}
|
||||
|
||||
enterpriseMap := make(map[uint]string)
|
||||
if len(enterpriseIDs) > 0 {
|
||||
enterprises, err := s.enterpriseStore.GetByIDs(ctx, enterpriseIDs)
|
||||
if err == nil {
|
||||
for _, ent := range enterprises {
|
||||
enterpriseMap[ent.ID] = ent.EnterpriseName
|
||||
}
|
||||
}
|
||||
}
|
||||
return enterpriseMap
|
||||
}
|
||||
|
||||
// toAccountResponse 组装账号响应,填充关联名称
|
||||
func (s *Service) toAccountResponse(acc *model.Account, shopMap map[uint]string, enterpriseMap map[uint]string) *dto.AccountResponse {
|
||||
resp := &dto.AccountResponse{
|
||||
ID: acc.ID,
|
||||
Username: acc.Username,
|
||||
Phone: acc.Phone,
|
||||
UserType: acc.UserType,
|
||||
ShopID: acc.ShopID,
|
||||
EnterpriseID: acc.EnterpriseID,
|
||||
Status: acc.Status,
|
||||
Creator: acc.Creator,
|
||||
Updater: acc.Updater,
|
||||
CreatedAt: acc.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: acc.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
|
||||
if acc.ShopID != nil && *acc.ShopID > 0 {
|
||||
resp.ShopName = shopMap[*acc.ShopID]
|
||||
}
|
||||
|
||||
if acc.EnterpriseID != nil && *acc.EnterpriseID > 0 {
|
||||
resp.EnterpriseName = enterpriseMap[*acc.EnterpriseID]
|
||||
}
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
@@ -37,6 +37,14 @@ func (m *MockShopStore) GetByID(ctx context.Context, id uint) (*model.Shop, erro
|
||||
return args.Get(0).(*model.Shop), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockShopStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Shop, error) {
|
||||
args := m.Called(ctx, ids)
|
||||
if args.Get(0) == nil {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return args.Get(0).([]*model.Shop), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockShopStore) GetSubordinateShopIDs(ctx context.Context, shopID uint) ([]uint, error) {
|
||||
args := m.Called(ctx, shopID)
|
||||
if args.Get(0) == nil {
|
||||
@@ -57,6 +65,14 @@ func (m *MockEnterpriseStore) GetByID(ctx context.Context, id uint) (*model.Ente
|
||||
return args.Get(0).(*model.Enterprise), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockEnterpriseStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Enterprise, error) {
|
||||
args := m.Called(ctx, ids)
|
||||
if args.Get(0) == nil {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return args.Get(0).([]*model.Enterprise), args.Error(1)
|
||||
}
|
||||
|
||||
func TestAccountService_Create_SuperAdminSuccess(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
|
||||
@@ -52,7 +52,6 @@ func TestAuthorizationService_BatchAuthorize_BoundCardRejected(t *testing.T) {
|
||||
|
||||
unboundCard := &model.IotCard{
|
||||
ICCID: "UNBOUND_CARD_001",
|
||||
CardType: "normal",
|
||||
CarrierID: carrier.ID,
|
||||
Status: 2,
|
||||
ShopID: &shop.ID,
|
||||
@@ -61,7 +60,6 @@ func TestAuthorizationService_BatchAuthorize_BoundCardRejected(t *testing.T) {
|
||||
|
||||
boundCard := &model.IotCard{
|
||||
ICCID: "BOUND_CARD_001",
|
||||
CardType: "normal",
|
||||
CarrierID: carrier.ID,
|
||||
Status: 2,
|
||||
ShopID: &shop.ID,
|
||||
@@ -132,7 +130,6 @@ func TestAuthorizationService_BatchAuthorize_BoundCardRejected(t *testing.T) {
|
||||
t.Run("混合卡列表中有绑定卡时整体拒绝", func(t *testing.T) {
|
||||
unboundCard2 := &model.IotCard{
|
||||
ICCID: "UNBOUND_CARD_002",
|
||||
CardType: "normal",
|
||||
CarrierID: carrier.ID,
|
||||
Status: 2,
|
||||
ShopID: &shop.ID,
|
||||
|
||||
@@ -93,7 +93,6 @@ func setupTestEnv(t *testing.T, prefix string) *testEnv {
|
||||
for i := 0; i < 4; i++ {
|
||||
cards[i] = &model.IotCard{
|
||||
ICCID: fmt.Sprintf("%s%04d", prefix, i+1),
|
||||
CardType: "normal",
|
||||
CarrierID: carrier.ID,
|
||||
Status: 2,
|
||||
ShopID: &shop.ID,
|
||||
@@ -775,7 +774,6 @@ func TestService_GetDeviceDetail_WithNetworkStatusOn(t *testing.T) {
|
||||
|
||||
card := &model.IotCard{
|
||||
ICCID: prefix + "0001",
|
||||
CardType: "normal",
|
||||
CarrierID: carrier.ID,
|
||||
Status: 2,
|
||||
NetworkStatus: 1,
|
||||
@@ -876,7 +874,6 @@ func TestService_ValidateCardOperation_RevokedDeviceAuth(t *testing.T) {
|
||||
|
||||
card := &model.IotCard{
|
||||
ICCID: prefix + "0001",
|
||||
CardType: "normal",
|
||||
CarrierID: carrier.ID,
|
||||
Status: 2,
|
||||
}
|
||||
|
||||
@@ -172,7 +172,6 @@ func (s *Service) toStandaloneResponse(card *model.IotCard, shopMap map[uint]str
|
||||
resp := &dto.StandaloneIotCardResponse{
|
||||
ID: card.ID,
|
||||
ICCID: card.ICCID,
|
||||
CardType: card.CardType,
|
||||
CardCategory: card.CardCategory,
|
||||
CarrierID: card.CarrierID,
|
||||
CarrierType: card.CarrierType,
|
||||
|
||||
@@ -56,9 +56,9 @@ func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
|
||||
|
||||
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},
|
||||
{ICCID: prefix + "001", CarrierID: 1, Status: 1, ShopID: &shop.ID},
|
||||
{ICCID: prefix + "002", CarrierID: 1, Status: 1, ShopID: &shop.ID},
|
||||
{ICCID: prefix + "003", CarrierID: 1, Status: 1, ShopID: nil},
|
||||
}
|
||||
require.NoError(t, iotCardStore.CreateBatch(ctx, cards))
|
||||
|
||||
|
||||
@@ -61,7 +61,6 @@ func createTestIotCard(t *testing.T, tx *gorm.DB, shopID *uint, seriesAllocation
|
||||
Updater: 1,
|
||||
},
|
||||
ICCID: fmt.Sprintf("89860%014d", timestamp%100000000000000),
|
||||
CardType: "流量卡",
|
||||
CardCategory: "normal",
|
||||
CarrierID: 1,
|
||||
CarrierType: "CMCC",
|
||||
|
||||
@@ -113,6 +113,12 @@ func (s *AccountStore) List(ctx context.Context, opts *store.QueryOptions, filte
|
||||
if status, ok := filters["status"].(int); ok {
|
||||
query = query.Where("status = ?", status)
|
||||
}
|
||||
if shopID, ok := filters["shop_id"].(uint); ok && shopID > 0 {
|
||||
query = query.Where("shop_id = ?", shopID)
|
||||
}
|
||||
if enterpriseID, ok := filters["enterprise_id"].(uint); ok && enterpriseID > 0 {
|
||||
query = query.Where("enterprise_id = ?", enterpriseID)
|
||||
}
|
||||
|
||||
// 计算总数
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestDeviceSimBindingStore_Create_DuplicateCard(t *testing.T) {
|
||||
require.NoError(t, deviceStore.Create(ctx, device1))
|
||||
require.NoError(t, deviceStore.Create(ctx, device2))
|
||||
|
||||
card := &model.IotCard{ICCID: "89860012345678910001", CardType: "data_card", CarrierID: 1, Status: 1}
|
||||
card := &model.IotCard{ICCID: "89860012345678910001", CarrierID: 1, Status: 1}
|
||||
require.NoError(t, cardStore.Create(ctx, card))
|
||||
|
||||
now := time.Now()
|
||||
@@ -70,8 +70,8 @@ func TestDeviceSimBindingStore_Create_DuplicateSlot(t *testing.T) {
|
||||
device := &model.Device{DeviceNo: "TEST-DEV-UC-003", Status: 1, MaxSimSlots: 4}
|
||||
require.NoError(t, deviceStore.Create(ctx, device))
|
||||
|
||||
card1 := &model.IotCard{ICCID: "89860012345678910011", CardType: "data_card", CarrierID: 1, Status: 1}
|
||||
card2 := &model.IotCard{ICCID: "89860012345678910012", CardType: "data_card", CarrierID: 1, Status: 1}
|
||||
card1 := &model.IotCard{ICCID: "89860012345678910011", CarrierID: 1, Status: 1}
|
||||
card2 := &model.IotCard{ICCID: "89860012345678910012", CarrierID: 1, Status: 1}
|
||||
require.NoError(t, cardStore.Create(ctx, card1))
|
||||
require.NoError(t, cardStore.Create(ctx, card2))
|
||||
|
||||
@@ -114,8 +114,8 @@ func TestDeviceSimBindingStore_Create_DifferentSlots(t *testing.T) {
|
||||
device := &model.Device{DeviceNo: "TEST-DEV-UC-004", Status: 1, MaxSimSlots: 4}
|
||||
require.NoError(t, deviceStore.Create(ctx, device))
|
||||
|
||||
card1 := &model.IotCard{ICCID: "89860012345678910021", CardType: "data_card", CarrierID: 1, Status: 1}
|
||||
card2 := &model.IotCard{ICCID: "89860012345678910022", CardType: "data_card", CarrierID: 1, Status: 1}
|
||||
card1 := &model.IotCard{ICCID: "89860012345678910021", CarrierID: 1, Status: 1}
|
||||
card2 := &model.IotCard{ICCID: "89860012345678910022", CarrierID: 1, Status: 1}
|
||||
require.NoError(t, cardStore.Create(ctx, card1))
|
||||
require.NoError(t, cardStore.Create(ctx, card2))
|
||||
|
||||
@@ -156,7 +156,7 @@ func TestDeviceSimBindingStore_ConcurrentBinding(t *testing.T) {
|
||||
require.NoError(t, deviceStore.Create(ctx, device1))
|
||||
require.NoError(t, deviceStore.Create(ctx, device2))
|
||||
|
||||
card := &model.IotCard{ICCID: "89860012345678920001", CardType: "data_card", CarrierID: 1, Status: 1}
|
||||
card := &model.IotCard{ICCID: "89860012345678920001", CarrierID: 1, Status: 1}
|
||||
require.NoError(t, cardStore.Create(ctx, card))
|
||||
|
||||
t.Cleanup(func() {
|
||||
|
||||
@@ -41,9 +41,9 @@ func TestEnterpriseCardAuthorizationStore_RevokeByDeviceAuthID(t *testing.T) {
|
||||
require.NoError(t, tx.Create(carrier).Error)
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: prefix + "0001", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0002", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0003", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0001", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0002", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0003", CarrierID: carrier.ID, Status: 2},
|
||||
}
|
||||
for _, c := range cards {
|
||||
require.NoError(t, tx.Create(c).Error)
|
||||
@@ -115,7 +115,6 @@ func TestEnterpriseCardAuthorizationStore_Create(t *testing.T) {
|
||||
|
||||
card := &model.IotCard{
|
||||
ICCID: prefix + "0001",
|
||||
CardType: "normal",
|
||||
CarrierID: carrier.ID,
|
||||
Status: 2,
|
||||
}
|
||||
@@ -162,8 +161,8 @@ func TestEnterpriseCardAuthorizationStore_BatchCreate(t *testing.T) {
|
||||
require.NoError(t, tx.Create(carrier).Error)
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: prefix + "0001", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0002", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0001", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0002", CarrierID: carrier.ID, Status: 2},
|
||||
}
|
||||
for _, c := range cards {
|
||||
require.NoError(t, tx.Create(c).Error)
|
||||
@@ -215,8 +214,8 @@ func TestEnterpriseCardAuthorizationStore_ListByEnterprise(t *testing.T) {
|
||||
require.NoError(t, tx.Create(carrier).Error)
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: prefix + "0001", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0002", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0001", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0002", CarrierID: carrier.ID, Status: 2},
|
||||
}
|
||||
for _, c := range cards {
|
||||
require.NoError(t, tx.Create(c).Error)
|
||||
@@ -274,9 +273,9 @@ func TestEnterpriseCardAuthorizationStore_GetActiveAuthsByCardIDs(t *testing.T)
|
||||
require.NoError(t, tx.Create(carrier).Error)
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: prefix + "0001", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0002", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0003", CardType: "normal", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0001", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0002", CarrierID: carrier.ID, Status: 2},
|
||||
{ICCID: prefix + "0003", CarrierID: carrier.ID, Status: 2},
|
||||
}
|
||||
for _, c := range cards {
|
||||
require.NoError(t, tx.Create(c).Error)
|
||||
|
||||
@@ -125,3 +125,15 @@ func (s *EnterpriseStore) GetPlatformEnterprises(ctx context.Context) ([]*model.
|
||||
}
|
||||
return enterprises, nil
|
||||
}
|
||||
|
||||
// GetByIDs 批量查询企业
|
||||
func (s *EnterpriseStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Enterprise, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*model.Enterprise{}, nil
|
||||
}
|
||||
var enterprises []*model.Enterprise
|
||||
if err := s.db.WithContext(ctx).Where("id IN ?", ids).Find(&enterprises).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return enterprises, nil
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ func TestIotCardStore_Create(t *testing.T) {
|
||||
|
||||
card := &model.IotCard{
|
||||
ICCID: "89860012345678901234",
|
||||
CardType: "data_card",
|
||||
CarrierID: 1,
|
||||
Status: 1,
|
||||
}
|
||||
@@ -47,7 +46,6 @@ func TestIotCardStore_ExistsByICCID(t *testing.T) {
|
||||
|
||||
card := &model.IotCard{
|
||||
ICCID: "89860012345678901111",
|
||||
CardType: "data_card",
|
||||
CarrierID: 1,
|
||||
Status: 1,
|
||||
}
|
||||
@@ -71,9 +69,9 @@ func TestIotCardStore_ExistsByICCIDBatch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: "89860012345678902001", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678902002", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678902003", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678902001", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678902002", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678902003", CarrierID: 1, Status: 1},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
@@ -102,15 +100,14 @@ func TestIotCardStore_ListStandalone(t *testing.T) {
|
||||
|
||||
prefix := uniqueICCIDPrefix()
|
||||
standaloneCards := []*model.IotCard{
|
||||
{ICCID: prefix + "0001", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: prefix + "0002", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: prefix + "0003", CardType: "data_card", CarrierID: 2, Status: 2},
|
||||
{ICCID: prefix + "0001", CarrierID: 1, Status: 1},
|
||||
{ICCID: prefix + "0002", CarrierID: 1, Status: 1},
|
||||
{ICCID: prefix + "0003", CarrierID: 2, Status: 2},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, standaloneCards))
|
||||
|
||||
boundCard := &model.IotCard{
|
||||
ICCID: prefix + "0004",
|
||||
CardType: "data_card",
|
||||
CarrierID: 1,
|
||||
Status: 1,
|
||||
}
|
||||
@@ -197,9 +194,9 @@ func TestIotCardStore_ListStandalone_Filters(t *testing.T) {
|
||||
shopID := uint(time.Now().UnixNano() % 1000000)
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: prefix + "A001", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: &shopID, BatchNo: batchPrefix + "01", MSISDN: msisdnPrefix + "01"},
|
||||
{ICCID: prefix + "A002", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil, BatchNo: batchPrefix + "01", MSISDN: msisdnPrefix + "02"},
|
||||
{ICCID: prefix + "A003", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil, BatchNo: batchPrefix + "02", MSISDN: msisdnPrefix + "03"},
|
||||
{ICCID: prefix + "A001", CarrierID: 1, Status: 1, ShopID: &shopID, BatchNo: batchPrefix + "01", MSISDN: msisdnPrefix + "01"},
|
||||
{ICCID: prefix + "A002", CarrierID: 1, Status: 1, ShopID: nil, BatchNo: batchPrefix + "01", MSISDN: msisdnPrefix + "02"},
|
||||
{ICCID: prefix + "A003", CarrierID: 1, Status: 1, ShopID: nil, BatchNo: batchPrefix + "02", MSISDN: msisdnPrefix + "03"},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
@@ -264,9 +261,9 @@ func TestIotCardStore_GetByICCIDs(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: "89860012345678905001", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678905002", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678905003", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678905001", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678905002", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678905003", CarrierID: 1, Status: 1},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
@@ -299,10 +296,10 @@ func TestIotCardStore_GetStandaloneByICCIDRange(t *testing.T) {
|
||||
|
||||
shopID := uint(100)
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: "89860012345678906001", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil},
|
||||
{ICCID: "89860012345678906002", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil},
|
||||
{ICCID: "89860012345678906003", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: &shopID},
|
||||
{ICCID: "89860012345678906004", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: &shopID},
|
||||
{ICCID: "89860012345678906001", CarrierID: 1, Status: 1, ShopID: nil},
|
||||
{ICCID: "89860012345678906002", CarrierID: 1, Status: 1, ShopID: nil},
|
||||
{ICCID: "89860012345678906003", CarrierID: 1, Status: 1, ShopID: &shopID},
|
||||
{ICCID: "89860012345678906004", CarrierID: 1, Status: 1, ShopID: &shopID},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
@@ -335,9 +332,9 @@ func TestIotCardStore_GetStandaloneByFilters(t *testing.T) {
|
||||
|
||||
shopID := uint(100)
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: "89860012345678907001", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil, BatchNo: "BATCH001"},
|
||||
{ICCID: "89860012345678907002", CardType: "data_card", CarrierID: 2, Status: 1, ShopID: nil, BatchNo: "BATCH002"},
|
||||
{ICCID: "89860012345678907003", CardType: "data_card", CarrierID: 1, Status: 2, ShopID: &shopID, BatchNo: "BATCH001"},
|
||||
{ICCID: "89860012345678907001", CarrierID: 1, Status: 1, ShopID: nil, BatchNo: "BATCH001"},
|
||||
{ICCID: "89860012345678907002", CarrierID: 2, Status: 1, ShopID: nil, BatchNo: "BATCH002"},
|
||||
{ICCID: "89860012345678907003", CarrierID: 1, Status: 2, ShopID: &shopID, BatchNo: "BATCH001"},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
@@ -367,8 +364,8 @@ func TestIotCardStore_BatchUpdateShopIDAndStatus(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: "89860012345678908001", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678908002", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678908001", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678908002", CarrierID: 1, Status: 1},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
@@ -400,9 +397,9 @@ func TestIotCardStore_GetBoundCardIDs(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: "89860012345678909001", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678909002", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678909003", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678909001", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678909002", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678909003", CarrierID: 1, Status: 1},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
@@ -435,8 +432,8 @@ func TestIotCardStore_BatchUpdateSeriesID(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: "89860012345678910001", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678910002", CardType: "data_card", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678910001", CarrierID: 1, Status: 1},
|
||||
{ICCID: "89860012345678910002", CarrierID: 1, Status: 1},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
@@ -482,9 +479,9 @@ func TestIotCardStore_ListBySeriesID(t *testing.T) {
|
||||
|
||||
seriesID := uint(200)
|
||||
cards := []*model.IotCard{
|
||||
{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},
|
||||
{ICCID: "89860012345678911001", CarrierID: 1, Status: 1, SeriesID: &seriesID},
|
||||
{ICCID: "89860012345678911002", CarrierID: 1, Status: 1, SeriesID: &seriesID},
|
||||
{ICCID: "89860012345678911003", CarrierID: 1, Status: 1, SeriesID: nil},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
@@ -507,9 +504,9 @@ func TestIotCardStore_ListStandalone_SeriesIDFilter(t *testing.T) {
|
||||
prefix := uniqueICCIDPrefix()
|
||||
seriesID := uint(300)
|
||||
cards := []*model.IotCard{
|
||||
{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},
|
||||
{ICCID: prefix + "S001", CarrierID: 1, Status: 1, SeriesID: &seriesID},
|
||||
{ICCID: prefix + "S002", CarrierID: 1, Status: 1, SeriesID: &seriesID},
|
||||
{ICCID: prefix + "S003", CarrierID: 1, Status: 1, SeriesID: nil},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
|
||||
@@ -203,3 +203,15 @@ func (s *ShopStore) GetByParentID(ctx context.Context, parentID uint) ([]*model.
|
||||
}
|
||||
return shops, nil
|
||||
}
|
||||
|
||||
// GetByIDs 批量查询店铺
|
||||
func (s *ShopStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Shop, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*model.Shop{}, nil
|
||||
}
|
||||
var shops []*model.Shop
|
||||
if err := s.db.WithContext(ctx).Where("id IN ?", ids).Find(&shops).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return shops, nil
|
||||
}
|
||||
|
||||
@@ -27,9 +27,9 @@ func TestDeviceImportHandler_ProcessBatch_AllOrNothingValidation(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
shopID := uint(100)
|
||||
platformCard := &model.IotCard{ICCID: "89860012345670001001", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
platformCard2 := &model.IotCard{ICCID: "89860012345670001003", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
shopCard := &model.IotCard{ICCID: "89860012345670001002", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: &shopID}
|
||||
platformCard := &model.IotCard{ICCID: "89860012345670001001", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
platformCard2 := &model.IotCard{ICCID: "89860012345670001003", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
shopCard := &model.IotCard{ICCID: "89860012345670001002", CarrierID: 1, Status: 1, ShopID: &shopID}
|
||||
require.NoError(t, cardStore.Create(ctx, platformCard))
|
||||
require.NoError(t, cardStore.Create(ctx, platformCard2))
|
||||
require.NoError(t, cardStore.Create(ctx, shopCard))
|
||||
@@ -119,8 +119,8 @@ func TestDeviceImportHandler_ProcessBatch_AllOrNothingValidation(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("多张卡全部可用-成功", func(t *testing.T) {
|
||||
newCard1 := &model.IotCard{ICCID: "89860012345670001010", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
newCard2 := &model.IotCard{ICCID: "89860012345670001011", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
newCard1 := &model.IotCard{ICCID: "89860012345670001010", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
newCard2 := &model.IotCard{ICCID: "89860012345670001011", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
require.NoError(t, cardStore.Create(ctx, newCard1))
|
||||
require.NoError(t, cardStore.Create(ctx, newCard2))
|
||||
|
||||
@@ -159,9 +159,9 @@ func TestDeviceImportHandler_ProcessImport_AllOrNothing(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
shopID := uint(200)
|
||||
platformCard1 := &model.IotCard{ICCID: "89860012345680001001", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
platformCard2 := &model.IotCard{ICCID: "89860012345680001002", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
shopCard := &model.IotCard{ICCID: "89860012345680001003", CardType: "data_card", CarrierID: 1, Status: 1, ShopID: &shopID}
|
||||
platformCard1 := &model.IotCard{ICCID: "89860012345680001001", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
platformCard2 := &model.IotCard{ICCID: "89860012345680001002", CarrierID: 1, Status: 1, ShopID: nil}
|
||||
shopCard := &model.IotCard{ICCID: "89860012345680001003", CarrierID: 1, Status: 1, ShopID: &shopID}
|
||||
require.NoError(t, cardStore.Create(ctx, platformCard1))
|
||||
require.NoError(t, cardStore.Create(ctx, platformCard2))
|
||||
require.NoError(t, cardStore.Create(ctx, shopCard))
|
||||
|
||||
@@ -54,7 +54,6 @@ func TestIotCardImportHandler_ProcessImport(t *testing.T) {
|
||||
t.Run("跳过已存在的ICCID", func(t *testing.T) {
|
||||
existingCard := &model.IotCard{
|
||||
ICCID: "89860012345678906001",
|
||||
CardType: "data_card",
|
||||
CarrierID: 1,
|
||||
Status: 1,
|
||||
}
|
||||
@@ -108,7 +107,6 @@ func TestIotCardImportHandler_ProcessImport(t *testing.T) {
|
||||
t.Run("混合场景-成功跳过和失败", func(t *testing.T) {
|
||||
existingCard := &model.IotCard{
|
||||
ICCID: "89860012345678908001",
|
||||
CardType: "data_card",
|
||||
CarrierID: 1,
|
||||
Status: 1,
|
||||
}
|
||||
@@ -166,7 +164,6 @@ func TestIotCardImportHandler_ProcessBatch(t *testing.T) {
|
||||
t.Run("验证行号和MSISDN正确记录", func(t *testing.T) {
|
||||
existingCard := &model.IotCard{
|
||||
ICCID: "89860012345678909002",
|
||||
CardType: "data_card",
|
||||
CarrierID: 1,
|
||||
Status: 1,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user