package domain import "sort" // levelPoints 按档位编码取分值;分值全部来自规则包,代码不内置任何数字。 func (c RecordBonusConfig) levelPoints(code string) (float64, bool) { for _, l := range c.Levels { if l.Code == code { return l.Points, true } } return 0, false } // RecordBonusPoints 把「已审核通过」的破纪录登记按报名项聚合: // 同一人同时破多级时,max 取最高档、sum 累加,口径由配置决定。 // 未配置的档位视为无加分,避免脏 level 凭空加分。 func RecordBonusPoints(cfg RecordBonusConfig, breaks []RecordBreak) []RecordBonus { return recordBonusPoints(cfg, breaks, nil) } // recordBonusPoints 是聚合实现;modeByEntry 给出每个报名项命中通道所属模板的口径, // 缺失时退回全局 cfg.Mode,保证直接调用 RecordBonusPoints 的旧口径不变。 func recordBonusPoints(cfg RecordBonusConfig, breaks []RecordBreak, modeByEntry map[int64]RecordBonusMode) []RecordBonus { if !cfg.Enabled || len(breaks) == 0 { return nil } type group struct { first RecordBreak levels []string points []float64 } order := make([]int64, 0, len(breaks)) groups := make(map[int64]*group) for _, b := range breaks { if b.State != RecordBreakApproved { continue } pts, ok := cfg.levelPoints(b.Level) if !ok { continue } g, exists := groups[b.EntryID] if !exists { g = &group{first: b} groups[b.EntryID] = g order = append(order, b.EntryID) } seen := false for _, code := range g.levels { if code == b.Level { seen = true break } } if !seen { g.levels = append(g.levels, b.Level) g.points = append(g.points, pts) } } out := make([]RecordBonus, 0, len(order)) for _, entryID := range order { g := groups[entryID] bonus := RecordBonus{ EntryID: entryID, EventID: g.first.EventID, AthleteID: g.first.AthleteID, TeamID: g.first.TeamID, Levels: g.levels, } mode := cfg.Mode if m, ok := modeByEntry[entryID]; ok { mode = m } if mode == RecordBonusSum { for _, p := range g.points { bonus.Points += p } } else { for _, p := range g.points { if p > bonus.Points { bonus.Points = p } } } out = append(out, bonus) } sort.SliceStable(out, func(i, j int) bool { return out[i].EntryID < out[j].EntryID }) return out } // recordBonusLedger 把破纪录加分写成流水。 // 加分与成绩分相互独立:不乘项目权重、也不乘组别权重(case 6 的口径)。 // 只处理本组别的报名项:上层按组别逐个调用 ScoreEvent,不加组别过滤会让同一笔加分每组各记一次。 func recordBonusLedger(in EventScoreInput) ([]LedgerEntry, error) { if in.RecordBonus == nil || !in.RecordBonus.Enabled || len(in.RecordBreaks) == 0 { return nil, nil } // 只对「可破纪录」的项目加分;登记接口另有一层校验,这里是引擎侧的最后防线。 if !in.Event.RecordEligible { return nil, nil } breaks := make([]RecordBreak, 0, len(in.RecordBreaks)) for _, b := range in.RecordBreaks { if in.Event.ID != 0 && b.EventID != 0 && b.EventID != in.Event.ID { continue } breaks = append(breaks, b) } modes := make(map[int64]RecordBonusMode, len(in.Entries)) for _, e := range in.Entries { if in.Category.ID != 0 && e.CategoryID != in.Category.ID { continue } if m, ok := recordBonusModeFor(in, e); ok { modes[e.ID] = m } } bonuses := recordBonusPoints(*in.RecordBonus, breaks, modes) if len(bonuses) == 0 { return nil, nil } byID := make(map[int64]Entry, len(in.Entries)) for _, e := range in.Entries { if in.Category.ID != 0 && e.CategoryID != in.Category.ID { continue } byID[e.ID] = e } var out []LedgerEntry for _, b := range bonuses { if b.Points == 0 { continue } e, ok := byID[b.EntryID] if !ok { continue } athleteID := b.AthleteID if athleteID == 0 { athleteID = e.AthleteID } for _, s := range e.resolvedShares(in.Options.UnionAttribution) { out = append(out, LedgerEntry{ TeamID: s.TeamID, AthleteID: athleteID, EventID: in.Event.ID, CategoryID: in.Category.ID, EntryID: e.ID, SourceType: SourceRecordBonus, SourceID: e.ID, Points: b.Points * s.Weight, CountToTeam: true, }) } } return out, nil } // recordBonusModeFor 取报名项「命中通道」所属模板的破纪录口径。 // 命中通道按 channelBefore 的优先级挑选(athlete → team → 通道 id 升序), // 与奖牌去重同一口径;没有可命中通道或模板缺失时返回 false,由调用方退回全局口径。 func recordBonusModeFor(in EventScoreInput, e Entry) (RecordBonusMode, bool) { var ( picked Scope pickedID int64 profileID int64 found bool ) for _, ch := range in.Channels { if ch.Scope == ScopeAthlete && e.AthleteID == 0 { continue } if !found || channelBefore(ch.Scope, ch.ID, picked, pickedID) { picked, pickedID, profileID, found = ch.Scope, ch.ID, ch.ProfileID, true } } if !found { return "", false } p, ok := in.Profiles[profileID] if !ok { return "", false } return p.bonusMode(), true }