feat: 8 条功能 + 身份口径改为号码布为准(含批量退赛/移出、基础数据导入导出、登录有效期)
用户给出的 8 条功能 + 一条口径变更,全部带真机/真浏览器验证:
① 录入页姓名后直接显示号码布(原「号码」列保留)
② 录入页批次进度条:`第 2 批 已录 5/8 · 3 条有错`,录完变绿;已录判据复用
entryGuards.judgeEntryRow(与发布防呆同源,不会出现"进度条说录完、发布说缺 8 条")
⑦ 小项列表双击 → 跳到该小项的组别(编辑/删除按钮不误触)
③ 赛事开关 `standings.live_on_publish`(默认开=保持原行为):关闭后分批发布只上屏成绩,
名次/流水/榜单一起等该组录完再结算 —— 代理实读代码后纠正了我的前提:
奖牌榜读的是 results.rank(buildRanked)而非流水汇总,只延迟流水会出现
「有金牌但 0 分」。另提供 POST /api/results/settle 人工兜底 + GET /api/results/settlement 未结算清单
④ 组别「就地更新」PUT /api/events/{id}/categories/{categoryId}:有报名数据时也能改名/改年龄/
改性别/改权重(旧路径是整表替换,一改就被 409 拦下);改名不动报名/成绩/流水(真机逐字节比对);
权重真的变了才触发该组重算(容差 1e-9)
④辅 运动员列表多选+全选,POST /api/athletes/batch 一次请求完成整批停用/彻底删除(逐条回报,
dryRun 预览走同一段 SQL 但不提交、不写审计)——这才是绕开限流的正解
新增 报名项批量:POST /api/entries/batch(scratch/restore/remove)。退赛只动 registered、
跳过 dq/dns 并说明原因;移出是物理删除,必须显式清 score_ledger(该表 entry_id 是
ON DELETE SET NULL,不显式删会留下"人没了分还在"的孤儿流水,验证基线 orphan=0);
退赛同时清 results.rank(否则出现"0 分还挂着金牌")
⑤ GET /api/export/entries.xlsx 报名名单导出(13 列含批次,状态中文与录入页逐字一致)
⑥ 基础数据包:GET /api/export/basedata.xlsx + POST /api/export/basedata/import
(四张表、表头只有一份定义、表头行自动定位、列缺席≠写空、按编码 upsert、绝不删除、
行级中文错误带真实 Excel 行号、枚举走既有校验、新增单位一定是启用)
⑧ 登录有效期面板可配:系统设置新页签,1–720 小时,默认 12h → 24h;滑动续期 +
Cookie 同步刷新(只服务端续期是假的,浏览器侧的 Max-Age 到点仍会丢 Cookie)
身份口径:号码布为唯一标识、证件号退出匹配(号码布不同=同名的两个人,新建并单独报告);
同一份表重复证件号不再报错;两个不同的人共用证件号仍报行级错误(唯一索引保留)
其它对齐:分项成绩表草稿行不再消失(成绩列「分数未录入」、名次/积分 `—`,没成绩的行同占位);
大屏实时赛况单栏铺满(留白 370→78px)、字号上限 56→96px、≥7 行自动拆左右两块;
删除报名项/退赛/权重等改动都会重算并推榜单。
测试:服务端 13 包(连跑两遍)、前端 335 例、vue-tsc + build、Console 18 文件 —— 全绿。
文档:README、docs/ 四份(含 debug/使用说明 副本)、devdocs 05/08/04/13。
版本号 dev_0.1.5 → dev_0.1.6。
This commit is contained in:
1 parent
8dd599bd97
commit
631c8dd8e1
80 files changed
+12468
-519
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (s *server) lookupID(ctx context.Context, query string, args ...any) (int64, error) {
|
||||
@@ -35,10 +36,21 @@ func (in athleteImportRow) decisionKey() string {
|
||||
}
|
||||
|
||||
// athleteImportMatch 是人员匹配结果;Conflict 非空表示本轮不落库,等用户决策。
|
||||
// SameNameNewPerson 非空表示本次按「同名的另一个人」新建了档案(只作报告,不拦截)。
|
||||
type athleteImportMatch struct {
|
||||
AthleteID int64
|
||||
TeamID int64
|
||||
Conflict *importConflict
|
||||
AthleteID int64
|
||||
TeamID int64
|
||||
Conflict *importConflict
|
||||
SameNameNewPerson *sameNameNewPerson
|
||||
}
|
||||
|
||||
// sameNameNewPerson 是「号码布不同 → 按同名的另一个人新建」的清单项:
|
||||
// 导入响应里单独列出来给现场看,避免新建了档案却毫无提示。
|
||||
type sameNameNewPerson struct {
|
||||
Name string `json:"name"`
|
||||
TeamCode string `json:"teamCode"`
|
||||
NewBib string `json:"newBib"`
|
||||
ExistingBib string `json:"existingBib"`
|
||||
}
|
||||
|
||||
// existingAthlete 是库中已有人员的可比字段。
|
||||
@@ -53,35 +65,136 @@ type existingAthlete struct {
|
||||
Age *int
|
||||
}
|
||||
|
||||
// matchImportedAthlete 按契约顺序匹配人员:证件号 → 号码布 → 姓名+单位(库中证件号为空则回填)→ 新建。
|
||||
// matchImportedAthlete 按新口径匹配人员:号码布 → 姓名 + 单位 → 新建。
|
||||
// 号码布是单人唯一标识,证件号不参与匹配(只作为可选档案信息写入/更新)。
|
||||
// 命中人员字段与表里不一致时记为冲突;decisions 里对同一身份有决策时按决策落库。
|
||||
func (s *server) matchImportedAthlete(ctx context.Context, in athleteImportRow, decisions map[string]string) (athleteImportMatch, error) {
|
||||
existing, err := s.athleteByIdentity(ctx, in)
|
||||
switch {
|
||||
case err == nil:
|
||||
return s.matchExistingAthlete(ctx, in, existing, decisions)
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return s.matchOrCreateAthleteByName(ctx, in, decisions)
|
||||
default:
|
||||
return athleteImportMatch{}, internal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// athleteByIdentity 先按证件号精确匹配,未命中再按号码布精确匹配。
|
||||
func (s *server) athleteByIdentity(ctx context.Context, in athleteImportRow) (existingAthlete, error) {
|
||||
if in.IDCard != "" {
|
||||
ex, err := s.athleteByIDCard(ctx, in.IDCard)
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
return ex, err
|
||||
if in.Bib != "" {
|
||||
ex, err := s.athleteByBib(ctx, in.Bib)
|
||||
switch {
|
||||
case err == nil:
|
||||
return s.matchExistingAthlete(ctx, in, ex, decisions)
|
||||
case !errors.Is(err, sql.ErrNoRows):
|
||||
return athleteImportMatch{}, internal(err)
|
||||
}
|
||||
}
|
||||
if in.Bib != "" {
|
||||
return s.athleteByBib(ctx, in.Bib)
|
||||
}
|
||||
return existingAthlete{}, sql.ErrNoRows
|
||||
return s.matchByNameTeam(ctx, in, decisions)
|
||||
}
|
||||
|
||||
// matchExistingAthlete 处理证件号/号码布命中的人员:无差异时直接复用并补空字段,
|
||||
// matchByNameTeam 处理「号码布为空、或按号码布没找到」的情况,按姓名 + 单位找候选:
|
||||
// 0 条 → 新建;1 条且对方没有号码布 → 命中并回填;1 条但对方号码布不同 → 同名的另一个人,新建并报告;
|
||||
// 多条 → 仍有歧义,进冲突面板等用户决策。
|
||||
func (s *server) matchByNameTeam(ctx context.Context, in athleteImportRow, decisions map[string]string) (athleteImportMatch, error) {
|
||||
candidates, err := s.athletesByNameTeam(ctx, in.Name, in.TeamID)
|
||||
if err != nil {
|
||||
return athleteImportMatch{}, internal(err)
|
||||
}
|
||||
switch len(candidates) {
|
||||
case 0:
|
||||
return s.createImportedAthlete(ctx, in)
|
||||
case 1:
|
||||
ex := candidates[0]
|
||||
// 只有表里也给了号码布、且与库里不同时才算「同名的另一个人」:
|
||||
// 表里没给号码布只是「未指定」,不能凭空多建一个没有号码布的同名人。
|
||||
if ex.Bib != nil && *ex.Bib != "" && in.Bib != "" && *ex.Bib != in.Bib {
|
||||
created, err := s.createImportedAthlete(ctx, in)
|
||||
if err != nil {
|
||||
return athleteImportMatch{}, err
|
||||
}
|
||||
created.SameNameNewPerson = &sameNameNewPerson{
|
||||
Name: in.Name,
|
||||
TeamCode: in.TeamCode,
|
||||
NewBib: in.Bib,
|
||||
ExistingBib: *ex.Bib,
|
||||
}
|
||||
return created, nil
|
||||
}
|
||||
return s.matchExistingAthlete(ctx, in, ex, decisions)
|
||||
default:
|
||||
return s.matchAmbiguousCandidates(ctx, in, candidates, decisions)
|
||||
}
|
||||
}
|
||||
|
||||
// matchAmbiguousCandidates 处理姓名 + 单位命中多条候选:不能替用户挑人,一律进冲突面板。
|
||||
// 决策键是整行身份(证件号优先、否则号码布),无法区分同一行的多个候选,
|
||||
// 所以面板只以第一条候选为代表;有决策时按决策作用于该候选。
|
||||
func (s *server) matchAmbiguousCandidates(ctx context.Context, in athleteImportRow, candidates []existingAthlete, decisions map[string]string) (athleteImportMatch, error) {
|
||||
ex := candidates[0]
|
||||
if _, decided := decisions[in.decisionKey()]; decided {
|
||||
return s.matchExistingAthlete(ctx, in, ex, decisions)
|
||||
}
|
||||
conflict, err := s.importConflictFor(ctx, in, ex, changedAthleteFields(ex, in))
|
||||
if err != nil {
|
||||
return athleteImportMatch{}, err
|
||||
}
|
||||
return athleteImportMatch{Conflict: &conflict}, nil
|
||||
}
|
||||
|
||||
// createImportedAthlete 新建人员:证件号只作为可选档案信息写入,
|
||||
// 但它与库里另一个人撞号时仍然明确报错(id_card 的部分唯一索引保留不动,防的是录入错误)。
|
||||
func (s *server) createImportedAthlete(ctx context.Context, in athleteImportRow) (athleteImportMatch, error) {
|
||||
if err := s.idCardTakenError(ctx, in.IDCard, 0); err != nil {
|
||||
return athleteImportMatch{}, err
|
||||
}
|
||||
res, err := s.ev.ExecContext(ctx,
|
||||
`INSERT INTO athletes (name, gender, team_id, id_card, bib, age, active) VALUES (?, ?, ?, ?, ?, ?, 1)`,
|
||||
in.Name, nullStr(&in.Gender), in.TeamID, nullStr(&in.IDCard), nullStr(&in.Bib), nullInt(in.Age))
|
||||
switch {
|
||||
case isIDCardConflict(err):
|
||||
// 并发下唯一索引兜底:再查一次占用者,把「撞上谁」说清楚。
|
||||
if taken := s.idCardTakenError(ctx, in.IDCard, 0); taken != nil {
|
||||
return athleteImportMatch{}, taken
|
||||
}
|
||||
return athleteImportMatch{}, internal(err)
|
||||
case isBibConflict(err):
|
||||
return athleteImportMatch{}, conflict(fmt.Sprintf("号码布 %s 已被其它人员使用", in.Bib))
|
||||
case err != nil:
|
||||
return athleteImportMatch{}, internal(err)
|
||||
}
|
||||
id, err := lastInsertID(res)
|
||||
if err != nil {
|
||||
return athleteImportMatch{}, internal(err)
|
||||
}
|
||||
return athleteImportMatch{AthleteID: id, TeamID: in.TeamID}, nil
|
||||
}
|
||||
|
||||
// idCardTakenError 检查证件号是否已被另一个人占用,占用时返回可直接给现场看的中文错误。
|
||||
// 证件号不参与匹配,但库里一个证件号只能属于一个人(唯一索引保留):撞号时必须报清楚占用者是谁,
|
||||
// 不允许静默改库或静默跳过。excludeID 是被更新的人(新建传 0),允许他自己继续持有该证件号。
|
||||
func (s *server) idCardTakenError(ctx context.Context, idCard string, excludeID int64) error {
|
||||
if idCard == "" {
|
||||
return nil
|
||||
}
|
||||
var (
|
||||
name string
|
||||
bib sql.NullString
|
||||
)
|
||||
err := s.ev.QueryRowContext(ctx,
|
||||
`SELECT name, bib FROM athletes WHERE id_card = ? AND id <> ? ORDER BY id LIMIT 1`, idCard, excludeID).
|
||||
Scan(&name, &bib)
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil
|
||||
case err != nil:
|
||||
return internal(err)
|
||||
}
|
||||
occupied := "未填"
|
||||
if bib.Valid && bib.String != "" {
|
||||
occupied = bib.String
|
||||
}
|
||||
return conflict(fmt.Sprintf("证件号 %s 已被另一个人占用(姓名 %s,号码布 %s)", idCard, name, occupied))
|
||||
}
|
||||
|
||||
// importRowMessage 把业务错误转成行级提示:去掉「CONFLICT:」这类 code 前缀,只留中文 message。
|
||||
func importRowMessage(err error) string {
|
||||
var api *apiError
|
||||
if errors.As(err, &api) && api.message != "" {
|
||||
return api.message
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
// matchExistingAthlete 处理号码布或姓名 + 单位命中的人员:无差异时直接复用并补空字段,
|
||||
// 有差异时走冲突流程,decisions 里对该身份有决策才落库。
|
||||
func (s *server) matchExistingAthlete(ctx context.Context, in athleteImportRow, ex existingAthlete, decisions map[string]string) (athleteImportMatch, error) {
|
||||
changed := changedAthleteFields(ex, in)
|
||||
@@ -110,30 +223,6 @@ func (s *server) matchExistingAthlete(ctx context.Context, in athleteImportRow,
|
||||
}
|
||||
}
|
||||
|
||||
// matchOrCreateAthleteByName 处理证件号/号码布都未命中的情况:
|
||||
// 姓名+单位命中且库中证件号为空 → 视为同一人(差异按冲突处理);否则新建人员。
|
||||
func (s *server) matchOrCreateAthleteByName(ctx context.Context, in athleteImportRow, decisions map[string]string) (athleteImportMatch, error) {
|
||||
existing, err := s.athleteByNameTeam(ctx, in.Name, in.TeamID)
|
||||
switch {
|
||||
case err == nil:
|
||||
return s.matchExistingAthlete(ctx, in, existing, decisions)
|
||||
case !errors.Is(err, sql.ErrNoRows):
|
||||
return athleteImportMatch{}, internal(err)
|
||||
}
|
||||
|
||||
res, err := s.ev.ExecContext(ctx,
|
||||
`INSERT INTO athletes (name, gender, team_id, id_card, bib, age, active) VALUES (?, ?, ?, ?, ?, ?, 1)`,
|
||||
in.Name, nullStr(&in.Gender), in.TeamID, nullStr(&in.IDCard), nullStr(&in.Bib), nullInt(in.Age))
|
||||
if err != nil {
|
||||
return athleteImportMatch{}, internal(err)
|
||||
}
|
||||
id, err := lastInsertID(res)
|
||||
if err != nil {
|
||||
return athleteImportMatch{}, internal(err)
|
||||
}
|
||||
return athleteImportMatch{AthleteID: id, TeamID: in.TeamID}, nil
|
||||
}
|
||||
|
||||
// importAthleteColumns 是人员匹配要读的字段,证件号/号码布/年龄用于回填与冲突判断。
|
||||
const importAthleteColumns = `id, name, gender, team_id, employee_no, id_card, bib, age`
|
||||
|
||||
@@ -155,22 +244,30 @@ func scanExistingAthlete(row interface{ Scan(...any) error }) (existingAthlete,
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (s *server) athleteByIDCard(ctx context.Context, idCard string) (existingAthlete, error) {
|
||||
return scanExistingAthlete(s.ev.QueryRowContext(ctx,
|
||||
`SELECT `+importAthleteColumns+` FROM athletes WHERE id_card = ? LIMIT 1`, idCard))
|
||||
}
|
||||
|
||||
func (s *server) athleteByBib(ctx context.Context, bib string) (existingAthlete, error) {
|
||||
return scanExistingAthlete(s.ev.QueryRowContext(ctx,
|
||||
`SELECT `+importAthleteColumns+` FROM athletes WHERE bib = ? LIMIT 1`, bib))
|
||||
}
|
||||
|
||||
// athleteByNameTeam 按「姓名+单位」找库中还没填证件号的人,用于回填身份字段。
|
||||
func (s *server) athleteByNameTeam(ctx context.Context, name string, teamID int64) (existingAthlete, error) {
|
||||
return scanExistingAthlete(s.ev.QueryRowContext(ctx,
|
||||
`SELECT `+importAthleteColumns+` FROM athletes
|
||||
WHERE name = ? AND team_id = ? AND (id_card IS NULL OR id_card = '')
|
||||
ORDER BY id LIMIT 1`, name, teamID))
|
||||
// athletesByNameTeam 按「姓名 + 单位」列出全部候选,供调用方按候选数量决定是回填、新建还是进冲突面板。
|
||||
// 证件号不参与匹配,所以这里不再过滤证件号为空的人。
|
||||
func (s *server) athletesByNameTeam(ctx context.Context, name string, teamID int64) ([]existingAthlete, error) {
|
||||
rows, err := s.ev.QueryContext(ctx,
|
||||
`SELECT `+importAthleteColumns+` FROM athletes WHERE name = ? AND team_id = ? ORDER BY id`, name, teamID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
out := make([]existingAthlete, 0, 1)
|
||||
for rows.Next() {
|
||||
ex, err := scanExistingAthlete(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, ex)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// changedAthleteFields 列出表里与库中不一致的字段名,顺序固定便于前端展示。
|
||||
@@ -208,7 +305,15 @@ func identityDiffers(dbVal *string, inVal string) bool {
|
||||
// backfillEmptyAthleteFields 只补库中为空的证件号/号码布/年龄,已有值不动(差异已由 changedAthleteFields 判过)。
|
||||
func (s *server) backfillEmptyAthleteFields(ctx context.Context, ex existingAthlete, in athleteImportRow) error {
|
||||
if in.IDCard != "" && (ex.IDCard == nil || *ex.IDCard == "") {
|
||||
if err := s.idCardTakenError(ctx, in.IDCard, ex.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := s.ev.ExecContext(ctx, `UPDATE athletes SET id_card = ? WHERE id = ?`, in.IDCard, ex.ID); err != nil {
|
||||
if isIDCardConflict(err) {
|
||||
if taken := s.idCardTakenError(ctx, in.IDCard, ex.ID); taken != nil {
|
||||
return taken
|
||||
}
|
||||
}
|
||||
return internal(err)
|
||||
}
|
||||
}
|
||||
@@ -261,10 +366,23 @@ func (s *server) teamCodeByID(ctx context.Context, id int64) (string, error) {
|
||||
// overwriteAthleteFromImport 用表里的字段覆盖库中人员(keep-new 决策)。
|
||||
// 表里没填的字段保留库里的值,避免决策后反而丢字段。
|
||||
func (s *server) overwriteAthleteFromImport(ctx context.Context, id int64, in athleteImportRow) error {
|
||||
if in.IDCard != "" {
|
||||
if err := s.idCardTakenError(ctx, in.IDCard, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := s.ev.ExecContext(ctx,
|
||||
`UPDATE athletes SET name = ?, team_id = ?, gender = COALESCE(?, gender),
|
||||
id_card = COALESCE(?, id_card), bib = COALESCE(?, bib), age = COALESCE(?, age) WHERE id = ?`,
|
||||
in.Name, in.TeamID, nullStr(&in.Gender), nullStr(&in.IDCard), nullStr(&in.Bib), nullInt(in.Age), id); err != nil {
|
||||
switch {
|
||||
case isIDCardConflict(err):
|
||||
if taken := s.idCardTakenError(ctx, in.IDCard, id); taken != nil {
|
||||
return taken
|
||||
}
|
||||
case isBibConflict(err):
|
||||
return conflict(fmt.Sprintf("号码布 %s 已被其它人员使用", in.Bib))
|
||||
}
|
||||
return internal(err)
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user