package trafficcutoff import ( "context" "strings" "time" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/mcache" "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/logic/warningnotify" "github.com/gogf/gf/v2/frame/g" ) func notifyCutoffBatch(ctx context.Context, key cutoffBatchGroupKey, items []lockedCutoffCard, batchNo string, runTime time.Time) { if len(items) == 0 { return } deptNames := make(map[int64]string) events := make([]warningnotify.Event, 0, len(items)) for _, item := range items { card := item.Card deptID := cutoffNotificationDeptID(card, key.CreatedDept) if card.DeptId <= 0 { g.Log().Warningf(ctx, "TrafficCutoff notify fallback to created dept because card dept is empty, cardId:%d, iccid:%s, createdDept:%d", card.Id, card.Iccid, key.CreatedDept) } deptName, ok := deptNames[deptID] if !ok { deptName = cutoffDeptName(ctx, deptID) deptNames[deptID] = deptName } events = append(events, warningnotify.Event{ Business: warningnotify.BusinessTrafficCutoff, TenantID: key.TenantId, DeptID: deptID, DeptName: deptName, CreatedBy: key.CreatedBy, Category: "达量断网", Kind: "traffic_cutoff", ItemType: 1, ItemID: card.Id, ItemCode: card.Iccid, MSISDN: card.Msisdn, WarnLevel: 5, WarnMethods: warningnotify.MethodPlatform, UnitText: "MB", Threshold: roundCutoffFlow(card.TrafficCutoffThreshold), Actual: roundCutoffFlow(card.UsedFlowMb), TotalFlowMB: roundCutoffFlow(cutoffTotalFlowMb(card)), RemainFlowMB: roundCutoffFlow(cutoffRemainFlowMb(card)), Remark: "已用流量达到达量断网阈值,系统已生成停用数据服务批次", WarnTime: runTime, AffectOperation: true, OperationText: "已生成达量断网批次,worker 将执行停用数据服务", BatchNo: batchNo, }) } if err := warningnotify.Dispatch(ctx, events); err != nil { g.Log().Errorf(ctx, "TrafficCutoff.Check notify dispatch failed, batchNo:%s, cards:%d, err:%+v", batchNo, len(items), err) } } func cutoffNotificationDeptID(card cutoffCard, fallbackDeptID int64) int64 { if card.DeptId > 0 { return card.DeptId } return fallbackDeptID } func cutoffDeptName(ctx context.Context, deptID int64) string { if deptID <= 0 { return "" } deptName, err := mcache.GetDeptName(ctx, deptID) if err != nil { g.Log().Warningf(ctx, "TrafficCutoff load dept name failed, deptId:%d, err:%+v", deptID, err) return "" } return strings.TrimSpace(deptName) }