All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m4s
99 lines
2.3 KiB
Go
99 lines
2.3 KiB
Go
package exporter
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
)
|
|
|
|
// IotCardSceneStrategy IoT 卡导出场景策略。
|
|
type IotCardSceneStrategy struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewIotCardSceneStrategy 创建 IoT 卡场景策略。
|
|
func NewIotCardSceneStrategy(db *gorm.DB) *IotCardSceneStrategy {
|
|
return &IotCardSceneStrategy{db: db}
|
|
}
|
|
|
|
// Scene 场景编码。
|
|
func (s *IotCardSceneStrategy) Scene() string {
|
|
return constants.ExportTaskSceneIotCard
|
|
}
|
|
|
|
// Headers 导出表头。
|
|
func (s *IotCardSceneStrategy) Headers() []string {
|
|
return []string{"ID", "ICCID", "MSISDN", "虚拟号", "运营商名称", "状态", "店铺ID", "创建时间"}
|
|
}
|
|
|
|
// NextShardBoundary 获取下一段分片边界。
|
|
func (s *IotCardSceneStrategy) NextShardBoundary(ctx context.Context, task *model.ExportTask, afterID uint64, limit int) (*ShardBoundary, error) {
|
|
if limit <= 0 {
|
|
limit = constants.ExportDefaultShardSize
|
|
}
|
|
|
|
var ids []uint64
|
|
query := s.db.WithContext(ctx).
|
|
Model(&model.IotCard{}).
|
|
Where("id > ?", afterID).
|
|
Order("id ASC").
|
|
Limit(limit)
|
|
query = applyShopScopeForTask(query, task)
|
|
|
|
if err := query.Pluck("id", &ids).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if len(ids) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
return &ShardBoundary{
|
|
StartID: afterID,
|
|
EndID: ids[len(ids)-1],
|
|
RowCount: len(ids),
|
|
}, nil
|
|
}
|
|
|
|
// QueryRows 查询分片内数据行。
|
|
func (s *IotCardSceneStrategy) QueryRows(ctx context.Context, task *model.ExportTask, startID, endID uint64) ([][]string, error) {
|
|
if endID == 0 {
|
|
return [][]string{}, nil
|
|
}
|
|
|
|
var cards []model.IotCard
|
|
query := s.db.WithContext(ctx).
|
|
Model(&model.IotCard{}).
|
|
Where("id > ? AND id <= ?", startID, endID).
|
|
Order("id ASC")
|
|
query = applyShopScopeForTask(query, task)
|
|
|
|
if err := query.Find(&cards).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rows := make([][]string, 0, len(cards))
|
|
for _, item := range cards {
|
|
shopID := ""
|
|
if item.ShopID != nil {
|
|
shopID = strconv.FormatUint(uint64(*item.ShopID), 10)
|
|
}
|
|
|
|
rows = append(rows, []string{
|
|
strconv.FormatUint(uint64(item.ID), 10),
|
|
item.ICCID,
|
|
item.MSISDN,
|
|
item.VirtualNo,
|
|
item.CarrierName,
|
|
strconv.Itoa(item.Status),
|
|
shopID,
|
|
item.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
})
|
|
}
|
|
|
|
return rows, nil
|
|
}
|