Files
Scoreboard/server/internal/httpapi/dto.go
T
AskaEth 830a4dbdb7 feat(joint-team): 联队 + 份额分成 —— 迁移 0024、引擎按份额拆、导出 / 大屏 / 录入三路打通;版本 dev_0.5.3
## 需求
现场 6 项球类比赛里,有两个单位因参赛人数不足**合并成一个单位参赛**。
做法不是新建「联队单位」(那样榜单会多出第 8 个单位,且分和奖牌分不到原单位头上),
而是**一条报名挂多个单位、按份额拆**。

## 拍板口径
- 份额**可配、默认均分**
- 积分**平均拆、接受 .5**(团体第 1 名 15 分 → 两个单位各 7.5 分)
- 奖牌**各记一枚**(两个单位各 1 枚完整金牌,不是各 0.5 枚)
- 显示上把联队名字写全(形如 `第一分公司+第二分公司联队`),成员单位的 logo 都放
- 导入表里单位编码写 `A+B`

## 一、核心层(数据模型 + 引擎 + 存储 + 报名写入 + 导入解析)
- 迁移 `0024_entry_team_shares.sql`:新建 `entry_team_shares(entry_id, team_id, points_share,
  medal_share, sort_order)`。**两个口径分开表达**是刻意的——积分要拆成小数(7.5)、奖牌要整枚
  各记,用一个数表达就会把「7.5 分」和「半枚金牌」绑在一起,奖牌榜立刻错。
  `entries.team_id` 保留为**主单位**;老数据 `INSERT OR IGNORE` 回填成「主单位独占 100%」。
- 引擎:`ResolveShares`(引擎与读侧共用)、`splitSharePoints`(按权重归一、**末位吞余数**,
  各份之和恒等于原来那一份)、`TeamShare.Medal`(缺省整枚)。积分按权重拆,奖牌每个份额各记整枚。
- 报名写入:`PUT /api/entries/{id}/team-shares`(recorder 及以上,保存后当场重算该小项组别并推榜)。
- 导入:`team_code` 支持 `A+B`(主单位 = 加号左边,均分,奖牌各整枚);非法形态
  (`A+` / `+B` / `A++B` / `A+A` / 成员 >5 / 单位不存在)**行级拦下、不落库**,给人话报错。

## 二、导出与导入模板
- 11 份导出件:联队写全成员单位(`第一分公司+第二分公司联队`),并注明**分拆给了哪几家、各多少**
  (份额占比 + 各单位实得积分,取自按份额拆完的流水)。
- 单位奖牌明细:奖牌格写「金牌(联队 …,各单位各记 1 枚)」;含联队时副标题说明
  「各单位奖牌数之和会大于实际颁发数」——这是口径 3 的必然结果,**不是 bug**。
- 按单位筛选导出把成员单位一起算上(否则 B 单位导自己的名单会看不到自己参加的那场联队比赛)。
- 分工作表模板:`A+B` 写法挂在「单位编码」表头的**单元格批注 + 输入提示**(中英各一份);
  **表头文字、列序、12 列、工作表命名一个字未动**(动了模板就导不回去)。

## 三、前端与大屏
- 录入页 / 复核页「单位」列:联队显示 `联队 第一分公司 67%(权重 2) 第二分公司 33%`,
  点该格打开「成员单位与份额」弹窗(加/删成员、改权重、主单位徽标、实时比例预览)。
  **普通报名(单单位)的文案、元素、类名一字未动**,只多了一个 `@click` 入口与悬停提示。
- 大屏:分项成绩 / 实时赛况单位列显示联队全名并**放上两个单位的 logo**(单位列宽按该行的
  logo 数留位);底栏同理。

## 四、验证(都留了证据)
- **老数据逐字节不变**(改算分引擎的硬门槛):引擎层 golden `a2aa1c97…`、接口层 golden
  `aa6cea37…`,两个哈希由主会话**在修前树上亲手重跑确认**;导出层另有 11 份导出件逐行对拍,
  差异 **0 处**。
- 前端 773 → **809 例**全绿(旧用例一行未改);服务端 14 包两轮全绿;Console 68 例全绿。
- 真机联调:录入页份额弹窗、大屏两个 logo、奖牌榜 A/B 各 1 金、写路径(移除成员 → 保存 → 200)。

## 五、版本
`dev_0.5.2` → **`dev_0.5.3`**(本批含数据迁移 0024 + 新功能,必须与上一包区分开);
安装器素材重生成(版本号烙在位图里,实测只有 `brand.bmp` 的 1x/2x 两张变化,其余 92 张逐字节未变)。
2026-09-14 03:52:34 +08:00

388 lines
14 KiB
Go

package httpapi
// 本文件是 REST 的 JSON 契约(camelCase,与 web/src/shared/types.ts 一一对应)。
// 数据库列是 snake_case,转换集中放在 repo.go 的扫描函数里。
type teamDTO struct {
ID int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
ShortName *string `json:"shortName"`
LogoPath *string `json:"logoPath"`
Color *string `json:"color"`
SortOrder int `json:"sortOrder"`
Active bool `json:"active"`
// DeletedAt 非空表示「彻底删除」(对界面不可见),行仍在库里;停用是另一回事(active = false)。
DeletedAt *string `json:"deletedAt"`
}
type athleteDTO struct {
ID int64 `json:"id"`
Name string `json:"name"`
Gender *string `json:"gender"`
TeamID int64 `json:"teamId"`
EmployeeNo *string `json:"employeeNo"`
IDCard *string `json:"idCard"`
Bib *string `json:"bib"`
Age *int `json:"age"`
Active bool `json:"active"`
}
type sportDTO struct {
ID int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
ShortName *string `json:"shortName"`
DefaultProfileID *int64 `json:"defaultProfileId"`
SortOrder int `json:"sortOrder"`
Active bool `json:"active"`
}
type categoryDTO struct {
ID int64 `json:"id"`
EventID int64 `json:"eventId"`
Name string `json:"name"`
Multiplier float64 `json:"multiplier"`
Gender *string `json:"gender"`
AgeMin *int `json:"ageMin"`
AgeMax *int `json:"ageMax"`
SortOrder int `json:"sortOrder"`
}
type channelDTO struct {
ID int64 `json:"id"`
EventID int64 `json:"eventId"`
Scope string `json:"scope"`
Basis string `json:"basis"`
ProfileID int64 `json:"profileId"`
SortOrder int `json:"sortOrder"`
// AggregateMode 是 basis=aggregate 通道的汇总口径:avg 按成员平均 / sum 按成员总和。
// 请求体里缺省(空串)按 avg 处理,不带该字段的旧客户端保存通道时口径不变。
// 响应里只下发非缺省值(sum);缺省(avg)不下发,老数据的响应体因此一个字节都不变。
AggregateMode string `json:"aggregateMode,omitempty"`
// CountToTeam 决定该通道产出的流水是否计入单位总分;请求体里缺省(null)按 true 处理,
// 「个人+团体」项目的个人通道传 false。响应里始终是具体值。
CountToTeam *bool `json:"countToTeam"`
// CountToMedals 决定该通道产出的名次是否发奖牌;缺省(null)按 true 处理(与库中默认值一致)。
CountToMedals *bool `json:"countToMedals"`
// CountToTeamMedals 决定该通道的个人奖牌是否并入单位奖牌榜;缺省按 true 处理。
// 只对 scope=athlete 的通道有意义:scope=team 的名次本来就记在单位头上。
CountToTeamMedals *bool `json:"countToTeamMedals"`
}
type eventDTO struct {
ID int64 `json:"id"`
SportID int64 `json:"sportId"`
Code string `json:"code"`
Name string `json:"name"`
EventType string `json:"eventType"`
ResultMode string `json:"resultMode"`
ResultType string `json:"resultType"`
Compare string `json:"compare"`
RecordEligible bool `json:"recordEligible"`
Scored bool `json:"scored"`
Multiplier float64 `json:"multiplier"`
Status string `json:"status"`
ScheduledAt *string `json:"scheduledAt"`
Venue *string `json:"venue"`
SortOrder int `json:"sortOrder"`
Remark *string `json:"remark"`
AvgMode string `json:"avgMode"`
// AwardMedals 决定该小项是否结算奖牌;请求体缺省按 true 处理(与库中默认值一致)。
// 关闭后小项的名次不产生任何奖牌,积分不受影响。
AwardMedals bool `json:"awardMedals"`
Categories []categoryDTO `json:"categories,omitempty"`
Channels []channelDTO `json:"channels,omitempty"`
}
type profileRuleDTO struct {
Rank int `json:"rank"`
Points float64 `json:"points"`
}
type profileDTO struct {
ID int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
TieMode string `json:"tieMode"`
RecordBonusMode string `json:"recordBonusMode"`
AwardRanks int `json:"awardRanks"`
LastPlaceZero bool `json:"lastPlaceZero"`
Rules []profileRuleDTO `json:"rules"`
}
type entryDTO struct {
ID int64 `json:"id"`
EventID int64 `json:"eventId"`
CategoryID int64 `json:"categoryId"`
TeamID int64 `json:"teamId"`
AthleteID *int64 `json:"athleteId"`
MemberNames []string `json:"memberNames"`
Bib *string `json:"bib"`
Seed *string `json:"seed"`
// Heat 是录入 / 展示用的批次号,null 表示「未分批」;批次不参与算分。
Heat *int64 `json:"heat"`
Status string `json:"status"`
Remark *string `json:"remark"`
AthleteName *string `json:"athleteName,omitempty"`
TeamCode string `json:"teamCode,omitempty"`
TeamName string `json:"teamName,omitempty"`
ShortName *string `json:"shortName,omitempty"`
CategoryName string `json:"categoryName,omitempty"`
// TeamShares 是这条报名的归属份额(联队):读侧至少一条,第一条是主单位(= teamId)。
// 普通报名也下发一条独占份额,前端不必为「有没有联队」写两套渲染。
TeamShares []entryTeamShareDTO `json:"teamShares"`
}
// entryTeamShareDTO 是报名份额的一条(联队的一个成员单位)。
//
// - weight:积分份额,**相对权重**(缺省 1)。引擎按权重之和归一,所以 1+1 与 0.5+0.5 等价;
// 入库也就存原值,不做归一,避免 2:1 被写成 0.666…/0.333… 这种除不尽的数。
// - medal :这枚奖牌是否**整枚**记给该单位(缺省 true)。联队的金牌两个单位各记 1 枚,
// 与 weight 无关——奖牌榜数整数枚,不按积分权重拆。
// - teamCode / teamName 是读侧补的展示字段(前端拼「A+B联队」用),写侧不需要带。
type entryTeamShareDTO struct {
TeamID int64 `json:"teamId"`
Weight float64 `json:"weight"`
Medal *bool `json:"medal,omitempty"`
TeamCode string `json:"teamCode,omitempty"`
TeamName string `json:"teamName,omitempty"`
}
type entryWithResultDTO struct {
entryDTO
Result *resultDTO `json:"result"`
}
// entryHeatResultDTO 是批量设置批次的结果:本批条目当前统一的批次号(null = 未分批)。
type entryHeatResultDTO struct {
Updated int `json:"updated"`
Heat *int64 `json:"heat"`
EntryIDs []int64 `json:"entryIds"`
}
type resultDTO struct {
ID int64 `json:"id"`
EventID int64 `json:"eventId"`
EntryID int64 `json:"entryId"`
Rank *int `json:"rank"`
ResultText string `json:"resultText"`
ResultValue *float64 `json:"resultValue"`
Unit string `json:"unit"`
IsTie bool `json:"isTie"`
PointsOverride *float64 `json:"pointsOverride"`
Status string `json:"status"`
State string `json:"state"`
RecordedBy *int64 `json:"recordedBy"`
RecordedAt *string `json:"recordedAt"`
VerifiedBy *int64 `json:"verifiedBy"`
VerifiedAt *string `json:"verifiedAt"`
Note *string `json:"note"`
Points *float64 `json:"points,omitempty"`
}
type athleteStandingDTO struct {
Rank int `json:"rank"`
AthleteID int64 `json:"athleteId"`
AthleteName string `json:"athleteName"`
Gender *string `json:"gender"`
TeamID int64 `json:"teamId"`
TeamCode string `json:"teamCode"`
TeamName string `json:"teamName"`
ShortName *string `json:"shortName"`
LogoPath *string `json:"logoPath"`
TotalPoints float64 `json:"totalPoints"`
Gold int `json:"gold"`
Silver int `json:"silver"`
Bronze int `json:"bronze"`
}
type breakdownItemDTO struct {
LedgerID int64 `json:"ledgerId"`
SourceType string `json:"sourceType"`
EventID *int64 `json:"eventId"`
EventCode *string `json:"eventCode"`
EventName *string `json:"eventName"`
CategoryName *string `json:"categoryName"`
AthleteID *int64 `json:"athleteId"`
AthleteName *string `json:"athleteName"`
Rank *int `json:"rank"`
ResultText *string `json:"resultText"`
Points float64 `json:"points"`
CreatedAt string `json:"createdAt"`
// 修订痕迹(口径见 internal/revisions):这条成绩发布之后被改过几次、最后一次因为什么改、什么时候。
//
// 三个字段一起看:RevisionCount 为 nil 表示**追不出来**(没有版本基线),为 0 表示追出来了、
// 确实没改过;前端只在 > 0 时显示「已修订 ×N」角标。宁可为空也不误报。
// LastRevisionReason / LastRevisedBy 都可能为 nil:现场没写原因、操作人已删号时就是空的。
RevisionCount *int `json:"revisionCount"`
LastRevisionAt *string `json:"lastRevisionAt"`
LastRevisionReason *string `json:"lastRevisionReason"`
LastRevisedBy *string `json:"lastRevisedBy"`
}
type teamBreakdownDTO struct {
TeamID int64 `json:"teamId"`
TeamCode string `json:"teamCode"`
TeamName string `json:"teamName"`
TotalPoints float64 `json:"totalPoints"`
Items []breakdownItemDTO `json:"items"`
}
type adjustmentDTO struct {
ID int64 `json:"id"`
TeamID int64 `json:"teamId"`
Delta float64 `json:"delta"`
Category string `json:"category"`
Reason string `json:"reason"`
EventID *int64 `json:"eventId"`
Voided bool `json:"voided"`
CreatedBy *int64 `json:"createdBy"`
CreatedAt string `json:"createdAt"`
}
type recordEntryDTO struct {
ID int64 `json:"id"`
EventID int64 `json:"eventId"`
Level string `json:"level"`
ResultValue float64 `json:"resultValue"`
ResultText string `json:"resultText"`
HolderName *string `json:"holderName"`
HolderTeamID *int64 `json:"holderTeamId"`
AchievedAt *string `json:"achievedAt"`
IsCurrent bool `json:"isCurrent"`
Source string `json:"source"`
}
type recordBreakDTO struct {
ID int64 `json:"id"`
EventID int64 `json:"eventId"`
EntryID int64 `json:"entryId"`
Level string `json:"level"`
ResultValue float64 `json:"resultValue"`
ResultText string `json:"resultText"`
BonusPoints float64 `json:"bonusPoints"`
State string `json:"state"`
ApprovedBy *int64 `json:"approvedBy"`
ApprovedAt *string `json:"approvedAt"`
// Note 是驳回 / 作废这条登记时写下的理由(0021 新增的列);从未被驳回的登记(含
// 老库里的历史行)为 null——空理由与「没有理由」是两回事,所以这里用指针。
Note *string `json:"note"`
}
type auditLogDTO struct {
ID int64 `json:"id"`
UserID *int64 `json:"userId"`
Action string `json:"action"`
EntityType string `json:"entityType"`
EntityID *int64 `json:"entityId"`
BeforeJSON *string `json:"beforeJson"`
AfterJSON *string `json:"afterJson"`
IP *string `json:"ip"`
RequestID *string `json:"requestId"`
CreatedAt string `json:"createdAt"`
}
type currentUserDTO struct {
ID int64 `json:"id"`
Username string `json:"username"`
DisplayName string `json:"displayName"`
Role string `json:"role"`
LastLoginAt *string `json:"lastLoginAt"`
MustChangePassword bool `json:"mustChangePassword"`
// SessionExpiresAt 是当前会话的到期时间(RFC3339 UTC),供界面显示
// 「当前会话将于 … 过期」;未登录时为 null。
SessionExpiresAt *string `json:"sessionExpiresAt"`
}
type backupResultDTO struct {
Path string `json:"path"`
CreatedAt string `json:"createdAt"`
}
// GET/PUT /api/settings 的线上结构见 internal/dto.EventSettings(与 snapshot.settings 共用)。
// ---------- 用户管理 ----------
type userListItemDTO struct {
ID int64 `json:"id"`
Username string `json:"username"`
DisplayName string `json:"displayName"`
Role string `json:"role"`
Active bool `json:"active"`
MustChangePassword bool `json:"mustChangePassword"`
LastLoginAt *string `json:"lastLoginAt"`
}
// resetPasswordResultDTO 里的 password 是唯一一次明文,落库只存哈希。
type resetPasswordResultDTO struct {
ID int64 `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
Generated bool `json:"generated"`
MustChangePassword bool `json:"mustChangePassword"`
}
// ---------- 变更历史 ----------
type revisionPageDTO struct {
Items []revisionListItemDTO `json:"items"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
type revisionListItemDTO struct {
ID int64 `json:"id"`
ParentID *int64 `json:"parentId"`
EventID int64 `json:"eventId"`
EventName string `json:"eventName"`
Action string `json:"action"`
ActorID *int64 `json:"actorId"`
ActorName string `json:"actorName"`
Message *string `json:"message"`
CreatedAt string `json:"createdAt"`
ChangedCount int `json:"changedCount"`
}
type revisionDetailDTO struct {
revisionListItemDTO
Snapshot []resultDTO `json:"snapshot"`
Diff revisionDiffDTO `json:"diff"`
}
type revisionDiffDTO struct {
Results []revisionResultChangeDTO `json:"results"`
Teams []revisionTeamDeltaDTO `json:"teams"`
}
type revisionResultChangeDTO struct {
ResultID int64 `json:"resultId"`
EntryID int64 `json:"entryId"`
TeamID *int64 `json:"teamId"`
Change string `json:"change"`
Fields []string `json:"fields,omitempty"`
Before *resultDTO `json:"before"`
After *resultDTO `json:"after"`
}
type revisionTeamDeltaDTO struct {
TeamID int64 `json:"teamId"`
TeamCode string `json:"teamCode"`
TeamName string `json:"teamName"`
PointsBefore float64 `json:"pointsBefore"`
PointsAfter float64 `json:"pointsAfter"`
PointsDelta float64 `json:"pointsDelta"`
ChangedResults int `json:"changedResults"`
}
// ---------- domain → DTO ----------
func nilIfEmpty(s string) *string {
if s == "" {
return nil
}
return &s
}