refactor(account): 移除卡类型字段、优化账号列表查询和权限检查
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m18s

- 移除 IoT 卡和号卡的 card_type 字段(数据库迁移)
- 优化账号列表查询,支持按店铺和企业筛选
- 账号响应增加店铺名称和企业名称字段
- 实现批量加载店铺和企业名称,避免 N+1 查询
- 更新权限检查中间件,完善权限验证逻辑
- 更新相关测试用例,确保功能正确性
This commit is contained in:
2026-02-03 10:59:44 +08:00
parent ad6d43e0cd
commit fba8e9e76b
31 changed files with 409 additions and 145 deletions

View File

@@ -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 {

View File

@@ -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() {

View File

@@ -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)

View File

@@ -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
}

View File

@@ -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))

View File

@@ -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
}