Files
iotlcm/server/internal/logic/trafficusage/traffic_usage_pool.go
T
MiFaZhan 59eb34976a chore: 初始开源发布
iotlcm-admin-svr:管理后台服务。

- Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件)
- 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、
  MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位;
  私网 IP → 文档地址段;库名 xiujie_admin → iotlcm
- 真实手机号/邮箱 → 合法 11 位编造号 / example.com
- 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、
  私有依赖 go get 行、明文 registry 登录口令
- 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名
- Dockerfile 改用公共基础镜像
- 拼错的 RADEME.md → README.md,README 重写
- 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用
- 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes

数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
2026-10-23 14:48:30 +08:00

157 lines
5.0 KiB
Go

package trafficusage
import (
"context"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
"time"
"github.com/gogf/gf/v2/frame/g"
)
type sTrafficPoolUsageSync struct {
}
type trafficPoolCardRow struct {
Iccid string `gconv:"iccid"`
FlowPoolCode string `gconv:"flow_pool_code"`
}
func NewTrafficPoolUsageSync() *sTrafficPoolUsageSync {
return &sTrafficPoolUsageSync{}
}
func init() {
service.RegisterTrafficPoolUsageSync(NewTrafficPoolUsageSync())
}
// TODO: 流量池查询逻辑还未确定
// SyncDaily 同步流量池的流量使用记录
func (s *sTrafficPoolUsageSync) SyncDaily(ctx context.Context) {
yesterday := time.Now().AddDate(0, 0, -1)
date := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, time.Local)
cards, err := queryTrafficPoolCards(ctx)
if err != nil {
g.Log().Errorf(ctx, "SyncTrafficPoolUsageDaily failed, date:%s, err:%+v", date.Format("2006-01-02"), err)
return
}
if len(cards) == 0 {
g.Log().Debug(ctx, "SyncTrafficPoolUsageDaily skipped, no flow pool cards")
return
}
aggregates, err := service.TrafficUsage().AggregatePoolUsageDaily(ctx, date, cards)
if err != nil {
g.Log().Errorf(ctx, "SyncTrafficPoolUsageDaily failed, date:%s, err:%+v", date.Format("2006-01-02"), err)
return
}
if items := buildTrafficPoolUsageItems(aggregates, date); len(items) > 0 {
if err = service.TrafficUsage().WritePoolUsageDaily(ctx, items); err != nil {
g.Log().Errorf(ctx, "SyncTrafficPoolUsageDaily failed, date:%s, err:%+v", date.Format("2006-01-02"), err)
return
}
}
if countItems := buildTrafficPoolCardCountItems(cards, date); len(countItems) > 0 {
if err = service.TrafficUsage().WritePoolCardCountDaily(ctx, countItems); err != nil {
g.Log().Errorf(ctx, "SyncTrafficPoolUsageDaily failed, date:%s, err:%+v", date.Format("2006-01-02"), err)
return
}
}
}
// SyncMonthly 同步流量池的流量使用记录
func (s *sTrafficPoolUsageSync) SyncMonthly(ctx context.Context) {
lastMonth := time.Now().AddDate(0, -1, 0)
month := time.Date(lastMonth.Year(), lastMonth.Month(), 1, 0, 0, 0, 0, time.Local)
cards, err := queryTrafficPoolCards(ctx)
if err != nil {
g.Log().Errorf(ctx, "SyncTrafficPoolUsageMonthly failed, month:%s, err:%+v", month.Format("2006-01"), err)
return
}
if len(cards) == 0 {
g.Log().Debug(ctx, "SyncTrafficPoolUsageMonthly skipped, no flow pool cards")
return
}
aggregates, err := service.TrafficUsage().AggregatePoolUsageMonthly(ctx, month, cards)
if err != nil {
g.Log().Errorf(ctx, "SyncTrafficPoolUsageMonthly failed, month:%s, err:%+v", month.Format("2006-01"), err)
return
}
if items := buildTrafficPoolUsageItems(aggregates, month); len(items) > 0 {
if err = service.TrafficUsage().WritePoolUsageMonthly(ctx, items); err != nil {
g.Log().Errorf(ctx, "SyncTrafficPoolUsageMonthly failed, month:%s, err:%+v", month.Format("2006-01"), err)
return
}
}
if countItems := buildTrafficPoolCardCountItems(cards, month); len(countItems) > 0 {
if err = service.TrafficUsage().WritePoolCardCountMonthly(ctx, countItems); err != nil {
g.Log().Errorf(ctx, "SyncTrafficPoolUsageMonthly failed, month:%s, err:%+v", month.Format("2006-01"), err)
return
}
}
}
func queryTrafficPoolCards(ctx context.Context) ([]model.TrafficPoolCard, error) {
rows := make([]trafficPoolCardRow, 0)
cardTable := dao.TSimCardInfo.Table()
columns := dao.TSimCardInfo.Columns()
if err := trafficUsageEligibleCardModel(ctx).
Fields(cardTable+"."+columns.Iccid+" AS iccid", cardTable+"."+columns.FlowPoolCode+" AS flow_pool_code").
Where(cardTable+"."+columns.IsFlowPool, 1).
Where(cardTable+"."+columns.FlowPoolCode+" <> ?", "").
Scan(&rows); err != nil {
return nil, err
}
cards := make([]model.TrafficPoolCard, 0, len(rows))
for _, row := range rows {
if row.Iccid == "" || row.FlowPoolCode == "" {
continue
}
cards = append(cards, model.TrafficPoolCard{
Iccid: row.Iccid,
FlowPoolCode: row.FlowPoolCode,
})
}
return cards, nil
}
func buildTrafficPoolUsageItems(aggregates []model.TrafficPoolUsageAggregate, date time.Time) []model.TrafficPoolUsageItem {
items := make([]model.TrafficPoolUsageItem, 0, len(aggregates))
for _, aggregate := range aggregates {
if aggregate.FlowPoolCode == "" {
continue
}
items = append(items, model.TrafficPoolUsageItem{
FlowPoolCode: aggregate.FlowPoolCode,
UsageMb: aggregate.UsageMb,
Date: date,
})
}
return items
}
func buildTrafficPoolCardCountItems(cards []model.TrafficPoolCard, date time.Time) []model.TrafficPoolCardCountItem {
countMap := make(map[string]int64)
for _, card := range cards {
if card.FlowPoolCode == "" {
continue
}
countMap[card.FlowPoolCode]++
}
items := make([]model.TrafficPoolCardCountItem, 0, len(countMap))
for flowPoolCode, cardCount := range countMap {
items = append(items, model.TrafficPoolCardCountItem{
FlowPoolCode: flowPoolCode,
CardCount: cardCount,
Date: date,
})
}
return items
}