package dailyusage import ( "context" "github.com/gogf/gf/v2/errors/gerror" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/dao" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity" ) // channelScanner augments, rather than replaces, the legacy scanner contract. // ListChannels is called once before any workers start. ScanChannel must not // mutate the legacy DatabaseScanner.channelMetadata cache. type channelScanner interface { ListChannels(context.Context) ([]channelMetadata, error) ScanChannel(context.Context, string, int64, int) ([]Card, error) } // This exact MySQL query shape is a release gate. Do not silently remove hints, // add an SQL upper bound, or move eligibility filtering after LIMIT. const channelPageSQL = `SELECT c.id, c.iccid, c.msisdn, c.imsi, c.chan_key, c.current_package_code FROM t_sim_card_info AS c FORCE INDEX (idx_card_chan_key) STRAIGHT_JOIN sys_dept AS d ON c.dept_id = d.dept_id AND c.tenant_id = d.tenant_id WHERE c.chan_key = ? AND c.id > ? AND c.iccid <> '' AND c.current_package_code <> '' AND d.deleted_at IS NULL ORDER BY c.id ASC LIMIT ?` func (s *DatabaseScanner) ListChannels(ctx context.Context) ([]channelMetadata, error) { var metadata []channelMetadata if err := dao.TSimCardInfo.DB().Model("t_sim_channels").Ctx(ctx).Fields("chan_key,temp_key,sync_enabled,operator").Scan(&metadata); err != nil { return nil, gerror.Wrap(err, "list channel metadata") } return metadata, nil } func (s *DatabaseScanner) ScanChannel(ctx context.Context, chanKey string, lastID int64, pageSize int) ([]Card, error) { if chanKey == "" || lastID < 0 || pageSize <= 0 { return nil, gerror.New("invalid channel pagination parameters") } var rows []entity.TSimCardInfo // Use the card DAO's database group, not a separately configured connection. if err := dao.TSimCardInfo.DB().Raw(channelPageSQL, chanKey, lastID, pageSize).Ctx(ctx).Scan(&rows); err != nil { return nil, gerror.Wrap(err, "scan channel cards") } cards := make([]Card, 0, len(rows)) for _, row := range rows { cards = append(cards, Card{ID: row.Id, ICCID: row.Iccid, MSISDN: row.Msisdn, IMSI: row.Imsi, ChanKey: row.ChanKey, PackageCode: row.CurrentPackageCode}) } return cards, nil } // Pool counters are shared with other worker traffic. Their deltas are direct // observations, not an attribution of all connection waits to this producer. func (*DatabaseScanner) poolWait(ctx context.Context) (int64, int64) { var count, nanos int64 for _, item := range dao.TSimCardInfo.DB().Stats(ctx) { stats := item.Stats() count += stats.WaitCount nanos += int64(stats.WaitDuration) } return count, nanos } // ValidateScanSettings is strict for values read from configuration. The Go // constructor first fills omitted zero values; explicit YAML zero is rejected. func ValidateScanSettings(mode string, concurrency int) error { if mode != "serial" && mode != "channel" { return gerror.Newf("invalid trafficUsageDailySync.producer.scanMode %q (serial or channel required)", mode) } if concurrency <= 0 { return gerror.New("trafficUsageDailySync.producer.scanConcurrency must be positive") } return nil }