feat: 不轮询清单与批量开关轮询
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m35s

- 新增不轮询清单 tb_card_polling_pause(迁移 000256 补写存量),所有改 enable_polling 的入口同事务写清单
- 卡、设备勾选批量开关轮询(单次 500,逐项结果,开启跳过风险停机与已销户)与 CSV 文件导入
- 订单套餐生效、改有效期后自动恢复轮询并按规则评估复机
- 不轮询清单查询与导出;卡、设备列表与导出增加是否轮询
- 失败告警停机被拒视图、导出与批量关闭轮询;开关轮询影响说明

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
邹进
2026-10-26 03:07:20 +09:00
parent a4e3fe4d8f
commit 7ddb9312e3
45 changed files with 2250 additions and 50 deletions

View File

@@ -15,7 +15,9 @@ const (
deviceExportBaseHeaderCount = 6
deviceExportCardGroupSize = 5
// deviceExportTailHeaderCount 是当前尾部固定列数(含「关联手机号」「设备合计(GB)」列)。
deviceExportTailHeaderCount = 7
deviceExportTailHeaderCount = 8
// deviceExportGBTailHeaderCount 是新增「是否轮询」之前、带「设备合计(GB)」的尾部列数,供历史任务表头反解。
deviceExportGBTailHeaderCount = 7
// deviceExportPhoneTailHeaderCount 是新增「设备合计(GB)」之前、带「关联手机号」的尾部列数,供历史任务表头反解。
deviceExportPhoneTailHeaderCount = 6
// deviceExportLegacyTailHeaderCount 是新增「关联手机号」之前的尾部列数,供历史任务表头反解。
@@ -25,6 +27,8 @@ const (
deviceExportAssociatedPhoneHeader = "关联手机号"
// deviceExportTotalGBHeader 是「设备合计(GB)」列的表头,固定位于导出最后一列。
deviceExportTotalGBHeader = "设备合计(GB)"
// deviceExportPollingHeader 是「是否轮询」列的表头,位于导出最后一列。
deviceExportPollingHeader = "是否轮询"
// deviceExportMBPerGB MB 换算 GB 的进制。
deviceExportMBPerGB = 1024
)
@@ -177,6 +181,9 @@ func (s *DeviceDataSource) applyFilters(query *gorm.DB, params ExportParams) *go
if hasActivePackage, ok := filterOptionalBool(params.Filters, "has_active_package"); ok {
query = s.applyHasActivePackageFilter(query, hasActivePackage)
}
if enablePolling, ok := filterOptionalBool(params.Filters, "enable_polling"); ok {
query = query.Where("d.enable_polling = ?", enablePolling)
}
return query
}
@@ -265,7 +272,8 @@ func (s *DeviceDataSource) fetchDevices(ctx context.Context, params ExportParams
d.device_model,
COALESCE(sh.shop_name, '') AS shop_name,
COALESCE(ps.series_name, '') AS series_name,
COALESCE(aw.balance, 0) AS wallet_balance
COALESCE(aw.balance, 0) AS wallet_balance,
d.enable_polling
`).
Order("d.id ASC").
Limit(limit).
@@ -352,6 +360,7 @@ type deviceExportRow struct {
ShopName string `gorm:"column:shop_name"`
SeriesName string `gorm:"column:series_name"`
WalletBalance int64 `gorm:"column:wallet_balance"`
EnablePolling bool `gorm:"column:enable_polling"`
}
type deviceExportCardRow struct {
@@ -394,6 +403,7 @@ func buildDeviceExportHeaders(cardGroups int) []string {
"钱包余额",
deviceExportAssociatedPhoneHeader,
deviceExportTotalGBHeader,
deviceExportPollingHeader,
)
return headers
}
@@ -444,9 +454,12 @@ func buildDeviceExportRow(item deviceExportRow, cards []deviceExportCardRow, pkg
if tailCount >= deviceExportPhoneTailHeaderCount {
row = append(row, associatedPhone)
}
if tailCount >= deviceExportTailHeaderCount {
if tailCount >= deviceExportGBTailHeaderCount {
row = append(row, formatMB(totalUsage/deviceExportMBPerGB))
}
if tailCount >= deviceExportTailHeaderCount {
row = append(row, yesNo(item.EnablePolling))
}
return row
}
@@ -462,8 +475,10 @@ func deviceExportTailCount(headers []string) int {
return deviceExportTailHeaderCount
}
switch headers[len(headers)-1] {
case deviceExportTotalGBHeader:
case deviceExportPollingHeader:
return deviceExportTailHeaderCount
case deviceExportTotalGBHeader:
return deviceExportGBTailHeaderCount
case deviceExportAssociatedPhoneHeader:
return deviceExportPhoneTailHeaderCount
default:

View File

@@ -40,7 +40,7 @@ func (s *IotCardDataSource) Count(ctx context.Context, params ExportParams) (int
// Headers 返回 IoT 卡导出表头。
func (s *IotCardDataSource) Headers(ctx context.Context, params ExportParams) ([]string, error) {
// 「关联手机号」「套餐到期时间」「使用流量(GB)」「剩余流量(GB)」依次固定追加在尾部:导出表头在 dispatch 阶段落库,历史任务重导出沿用同一列序。
return []string{"ICCID", "MSISDN", "绑定设备虚拟号", "运营商", "店铺名称", "绑定设备名称", "是否实名", "实名时间", "网络状态", "套餐名称", "使用流量(MB)", "剩余流量(MB)", iotCardExportAssociatedPhoneHeader, iotCardExportFinalExpiryHeader, iotCardExportUsageGBHeader, iotCardExportRemainingGBHeader}, nil
return []string{"ICCID", "MSISDN", "绑定设备虚拟号", "运营商", "店铺名称", "绑定设备名称", "是否实名", "实名时间", "网络状态", "套餐名称", "使用流量(MB)", "剩余流量(MB)", iotCardExportAssociatedPhoneHeader, iotCardExportFinalExpiryHeader, iotCardExportUsageGBHeader, iotCardExportRemainingGBHeader, iotCardExportPollingHeader}, nil
}
// Fetch 按 offset/limit 查询 IoT 卡导出数据。
@@ -60,6 +60,7 @@ func (s *IotCardDataSource) Fetch(ctx context.Context, params ExportParams, offs
COALESCE(sh.shop_name, '') AS shop_name,
COALESCE(d.device_name, '') AS device_name,
c.real_name_status,
c.enable_polling,
c.first_realname_at,
c.network_status,
COALESCE(pkg.package_name, '') AS package_name,
@@ -81,7 +82,7 @@ func (s *IotCardDataSource) Fetch(ctx context.Context, params ExportParams, offs
if err != nil {
return nil, err
}
includeExpiry, includeGB := iotCardExportTailColumns(params.ResolvedHeaders)
includeExpiry, includeGB, includePolling := iotCardExportTailColumns(params.ResolvedHeaders)
var finalExpiries map[uint]string
if includeExpiry {
finalExpiries, err = s.loadFinalExpiries(ctx, cardIDs)
@@ -114,6 +115,9 @@ func (s *IotCardDataSource) Fetch(ctx context.Context, params ExportParams, offs
if includeGB {
row = append(row, formatMB(float64(item.DataUsageMB)/deviceExportMBPerGB), formatMB(float64(remainingMB)/deviceExportMBPerGB))
}
if includePolling {
row = append(row, yesNo(item.EnablePolling))
}
rows = append(rows, row)
}
return rows, nil
@@ -135,19 +139,21 @@ func (s *IotCardDataSource) loadFinalExpiries(ctx context.Context, cardIDs []uin
return result, nil
}
// iotCardExportTailColumns 按冻结表头末列识别尾部版本:末列「剩余流量(GB)」输出到期列与 GB 列,
// 末列「套餐到期时间」只输出到期列,更早的表头两者都不输出;无冻结表头按当前版本输出。
func iotCardExportTailColumns(headers []string) (includeExpiry, includeGB bool) {
// iotCardExportTailColumns 按冻结表头末列识别尾部版本:末列「是否轮询」输出到期、GB 与是否轮询列,
// 末列「剩余流量(GB)」输出到期列与 GB 列,末列「套餐到期时间」只输出到期列,更早的表头都不输出;无冻结表头按当前版本输出。
func iotCardExportTailColumns(headers []string) (includeExpiry, includeGB, includePolling bool) {
if len(headers) == 0 {
return true, true
return true, true, true
}
switch headers[len(headers)-1] {
case iotCardExportPollingHeader:
return true, true, true
case iotCardExportRemainingGBHeader:
return true, true
return true, true, false
case iotCardExportFinalExpiryHeader:
return true, false
return true, false, false
default:
return false, false
return false, false, false
}
}
@@ -268,6 +274,9 @@ func (s *IotCardDataSource) applyFilters(ctx context.Context, query *gorm.DB, pa
if carrierName, ok := filterString(params.Filters, "carrier_name"); ok {
query = query.Where("c.carrier_name LIKE ?", "%"+carrierName+"%")
}
if enablePolling, ok := filterOptionalBool(params.Filters, "enable_polling"); ok {
query = query.Where("c.enable_polling = ?", enablePolling)
}
if hasActivePackage, ok := filterOptionalBool(params.Filters, "has_active_package"); ok {
subQuery := s.db.WithContext(ctx).Table("tb_package_usage AS pu").
Select("1").
@@ -291,6 +300,8 @@ const iotCardExportFinalExpiryHeader = "套餐到期时间"
const (
iotCardExportUsageGBHeader = "使用流量(GB)"
iotCardExportRemainingGBHeader = "剩余流量(GB)"
// iotCardExportPollingHeader 是「是否轮询」列的表头,位于 GB 流量列之后、导出最后一列。
iotCardExportPollingHeader = "是否轮询"
)
type iotCardExportRow struct {
@@ -307,6 +318,7 @@ type iotCardExportRow struct {
PackageName string `gorm:"column:package_name"`
DataUsageMB int64 `gorm:"column:data_usage_mb"`
DataLimitMB int64 `gorm:"column:data_limit_mb"`
EnablePolling bool `gorm:"column:enable_polling"`
}
func remainingPackageDataMB(limit, used int64) int64 {

View File

@@ -0,0 +1,226 @@
package exporter
import (
"context"
"strconv"
"time"
"gorm.io/gorm"
"github.com/break/junhong_cmp_fiber/internal/query/pollingalert"
pausequery "github.com/break/junhong_cmp_fiber/internal/query/pollingpause"
"github.com/break/junhong_cmp_fiber/pkg/constants"
)
// 不轮询清单与停机被拒告警导出(add-polling-pause-list-and-batch-toggle),仅超级管理员与平台账号。
var pollingPauseHeaders = []string{
"资产类型", "资产ID", "ICCID或IMEI", "运营商账户", "当前是否轮询", "网关扩展状态", "状态", "关闭来源", "关闭原因",
"关闭人", "关闭时间", "导入任务ID", "恢复来源", "恢复原因", "恢复人", "恢复时间",
}
// PollingPauseDataSource 不轮询清单导出数据源。
type PollingPauseDataSource struct {
query *pausequery.Query
}
// NewPollingPauseDataSource 创建不轮询清单导出数据源。
func NewPollingPauseDataSource(db *gorm.DB) *PollingPauseDataSource {
return &PollingPauseDataSource{query: pausequery.NewQuery(db)}
}
// Scene 返回导出场景编码。
func (s *PollingPauseDataSource) Scene() string { return constants.ExportTaskScenePollingPause }
func (s *PollingPauseDataSource) filter(params ExportParams) (pausequery.Filter, error) {
if err := pausequery.EnsureAllowed(params.UserType); err != nil {
return pausequery.Filter{}, err
}
filter := pausequery.Filter{}
filter.AssetType, _ = filterString(params.Filters, "asset_type")
filter.Source, _ = filterString(params.Filters, "source")
filter.Identifier, _ = filterString(params.Filters, "identifier")
if id, ok := filterUint(params.Filters, "carrier_id"); ok && id > 0 {
filter.CarrierID = &id
}
if id, ok := filterUint(params.Filters, "operator_id"); ok && id > 0 {
filter.OperatorID = &id
}
if status, ok := filterInt(params.Filters, "status"); ok && status > 0 {
filter.Status = &status
}
if t, ok := filterTime(params.Filters, "start_time"); ok {
filter.StartTime = &t
}
if t, ok := filterTime(params.Filters, "end_time"); ok {
filter.EndTime = &t
}
return filter, nil
}
// Count 统计导出行数。
func (s *PollingPauseDataSource) Count(ctx context.Context, params ExportParams) (int, error) {
filter, err := s.filter(params)
if err != nil {
return 0, err
}
total, err := s.query.Count(ctx, filter)
return int(total), err
}
// Headers 返回导出表头。
func (s *PollingPauseDataSource) Headers(ctx context.Context, params ExportParams) ([]string, error) {
if err := pausequery.EnsureAllowed(params.UserType); err != nil {
return nil, err
}
return pollingPauseHeaders, nil
}
// Fetch 按 offset/limit 返回导出行。
func (s *PollingPauseDataSource) Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error) {
filter, err := s.filter(params)
if err != nil {
return nil, err
}
rows, err := s.query.Page(ctx, filter, offset, limit)
if err != nil {
return nil, err
}
result := make([][]string, 0, len(rows))
for _, row := range rows {
assetType := "卡"
if row.AssetType == constants.AssetTypeDevice {
assetType = "设备"
}
importID := ""
if row.ImportTaskID > 0 {
importID = strconv.FormatUint(uint64(row.ImportTaskID), 10)
}
result = append(result, []string{
assetType, strconv.FormatUint(uint64(row.AssetID), 10), row.Identifier, row.CarrierName, yesNo(row.EnablePolling), row.GatewayExtend,
constants.PollingPauseStatusName(row.Status), constants.PollingToggleSourceName(row.CloseSource), row.CloseReason,
operatorName(row.ClosedBy, row.ClosedByName), formatExportTime(&row.ClosedAt), importID,
constants.PollingToggleSourceName(row.RestoreSource), row.RestoreReason, operatorName(row.RestoredBy, row.RestoredByName),
formatExportTime(row.RestoredAt),
})
}
return result, nil
}
var stopRefusedHeaders = []string{
"告警ID", "ICCID", "运营商账户", "所属设备ID", "网络状态", "有生效套餐", "停机原因", "错误类型", "上游返回信息",
"首次失败时间", "最近失败时间", "失败次数", "是否轮询", "处理状态", "处理人", "处理时间", "处理备注",
}
// StopRefusedAlertDataSource 停机被拒告警导出数据源。
type StopRefusedAlertDataSource struct {
query *pollingalert.Query
threshold func(ctx context.Context) int
}
// NewStopRefusedAlertDataSource 创建停机被拒告警导出数据源;threshold 为空时取默认阈值。
func NewStopRefusedAlertDataSource(db *gorm.DB, threshold func(ctx context.Context) int) *StopRefusedAlertDataSource {
return &StopRefusedAlertDataSource{query: pollingalert.NewQuery(db), threshold: threshold}
}
// Scene 返回导出场景编码。
func (s *StopRefusedAlertDataSource) Scene() string {
return constants.ExportTaskScenePollingStopRefusedAlert
}
func (s *StopRefusedAlertDataSource) filter(ctx context.Context, params ExportParams) (pollingalert.StopRefusedFilter, int, error) {
if err := pausequery.EnsureAllowed(params.UserType); err != nil {
return pollingalert.StopRefusedFilter{}, 0, err
}
filter := pollingalert.StopRefusedFilter{}
filter.ICCID, _ = filterString(params.Filters, "iccid")
if id, ok := filterUint(params.Filters, "carrier_id"); ok && id > 0 {
filter.CarrierID = &id
}
if status, ok := filterInt(params.Filters, "status"); ok && status > 0 {
filter.Status = &status
}
if t, ok := filterTime(params.Filters, "start_time"); ok {
filter.StartTime = &t
}
if t, ok := filterTime(params.Filters, "end_time"); ok {
filter.EndTime = &t
}
threshold := 0
if s.threshold != nil {
threshold = s.threshold(ctx)
}
return filter, threshold, nil
}
// Count 统计导出行数。
func (s *StopRefusedAlertDataSource) Count(ctx context.Context, params ExportParams) (int, error) {
filter, threshold, err := s.filter(ctx, params)
if err != nil {
return 0, err
}
total, err := s.query.Count(ctx, filter, threshold)
return int(total), err
}
// Headers 返回导出表头。
func (s *StopRefusedAlertDataSource) Headers(ctx context.Context, params ExportParams) ([]string, error) {
if err := pausequery.EnsureAllowed(params.UserType); err != nil {
return nil, err
}
return stopRefusedHeaders, nil
}
// Fetch 按 offset/limit 返回导出行。
func (s *StopRefusedAlertDataSource) Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error) {
filter, threshold, err := s.filter(ctx, params)
if err != nil {
return nil, err
}
rows, err := s.query.Page(ctx, filter, threshold, offset, limit)
if err != nil {
return nil, err
}
result := make([][]string, 0, len(rows))
for _, row := range rows {
device := ""
if row.DeviceID > 0 {
device = strconv.FormatUint(uint64(row.DeviceID), 10)
}
network := "停机"
if row.NetworkStatus == constants.NetworkStatusOnline {
network = "开机"
}
result = append(result, []string{
strconv.FormatUint(uint64(row.ID), 10), row.ICCID, row.CarrierName, device, network, yesNo(row.HasPackage), row.StopReason,
constants.GatewayErrorClassName(row.ErrorCategory), row.LastError, formatExportTime(row.FirstFailedAt), formatExportTime(row.LastFailedAt),
strconv.Itoa(row.FailCount), yesNo(row.EnablePolling), constants.PollingAlertStatusName(row.Status),
operatorName(row.HandledBy, row.HandledByName), formatExportTime(row.HandledAt), row.HandleRemark,
})
}
return result, nil
}
func yesNo(v bool) string {
if v {
return "是"
}
return "否"
}
func operatorName(id uint, name string) string {
if id == 0 {
return "系统"
}
if name == "" {
return strconv.FormatUint(uint64(id), 10)
}
return name
}
func formatExportTime(t *time.Time) string {
if t == nil || t.IsZero() {
return ""
}
return t.In(time.FixedZone("CST", 8*3600)).Format("2006-01-02 15:04:05")
}

View File

@@ -1,6 +1,9 @@
package exporter
import (
"context"
"github.com/break/junhong_cmp_fiber/internal/polling/carrierqueue"
"sort"
"gorm.io/gorm"
@@ -42,6 +45,8 @@ func NewDefaultRegistry(db *gorm.DB) *Registry {
NewOperationsActivationDataSource(db),
NewOperationsRenewalDataSource(db),
NewStopResumeRuleDataSource(db),
NewPollingPauseDataSource(db),
NewStopRefusedAlertDataSource(db, stopResumeAlertThreshold(db)),
)
}
@@ -89,3 +94,9 @@ func IsSupportedScene(scene string) bool {
return false
}
}
// stopResumeAlertThreshold 读取停复机连续失败告警阈值(停机被拒视图口径)。
func stopResumeAlertThreshold(db *gorm.DB) func(ctx context.Context) int {
settings := carrierqueue.NewDefaultSettingsProvider(db, nil, nil)
return func(ctx context.Context) int { return settings.Current(ctx).SRAlertFailCount }
}