From 68702e8af7963a10abc5563c9786bd6cfb23c6cf Mon Sep 17 00:00:00 2001 From: wangguixi Date: Tue, 18 Nov 2025 16:46:35 +0800 Subject: [PATCH] =?UTF-8?q?11.17=20=E4=BF=AE=E6=94=B9=E4=BA=86jwcode?= =?UTF-8?q?=E7=9A=84=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Knowledge_Test_Go/api/v1/questionBank.go | 2 +- .../internal/logic/questionBank/questionBank.go | 43 ++++++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Knowledge_Test_Go/api/v1/questionBank.go b/Knowledge_Test_Go/api/v1/questionBank.go index b8857f8..c8ebacc 100644 --- a/Knowledge_Test_Go/api/v1/questionBank.go +++ b/Knowledge_Test_Go/api/v1/questionBank.go @@ -129,7 +129,7 @@ type UserScoreOutputRes struct { type UserScoreOutputReq struct { UserName string `json:"user_name"` // 用户名(精准查询) UserIdentity string `json:"user_identity"` // 用户身份(过滤) - Jwcode int `json:"jwcode"` // 精网号(精准查询) + Jwcode string `json:"jwcode"` // 精网号(精准查询) StartTime string `json:"start_time"` // 开始时间(提交时间范围) EndTime string `json:"end_time"` // 结束时间(提交时间范围) Page int `json:"page"` diff --git a/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go b/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go index fdffb7c..621c5f5 100644 --- a/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go +++ b/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go @@ -6,6 +6,7 @@ import ( "Knowledge_Test_Go/utility/response" "context" "fmt" + "regexp" "strconv" "github.com/gogf/gf/v2/database/gdb" @@ -145,18 +146,52 @@ func (s *sQuestionBank) UserScoreOutput(ctx context.Context, req *v1.UserScoreOu // 添加查询条件 if req.UserName != "" { - db = db.Where("a.user_name = ?", req.UserName) + db = db.Where("a.user_name LIKE ?", "%"+req.UserName+"%") } if req.UserIdentity != "" { db = db.Where("a.user_identity = ?", req.UserIdentity) } - if req.Jwcode != 0 { + // 预编译正则:匹配8位数字(^表示开头,$表示结尾,\d{8}表示8位数字) + var jwcodeRegex = regexp.MustCompile(`^\d{8}$`) + + if req.Jwcode != "" { // 假设字符串类型的默认值是空串 + // 校验:长度8位且全为数字 + if !jwcodeRegex.MatchString(req.Jwcode) { + return nil, 0, fmt.Errorf("Jwcode必须是8位数字") + } + // 校验通过,添加查询条件 db = db.Where("a.jwcode = ?", req.Jwcode) } - // 时间范围查询 + //// 1. 定义时间格式(根据业务实际格式调整) + //const timeLayout = "2006-01-02 15:04:05" + //// 2. 解析开始时间和结束时间(校验格式) + //var startTime, endTime time.Time + // + //if req.StartTime != "" { + // if startTime, err = time.Parse(timeLayout, req.StartTime); err != nil { + // return nil, 0, fmt.Errorf("开始时间格式错误,需为 %s", timeLayout) + // } + //} + //if req.EndTime != "" { + // if endTime, err = time.Parse(timeLayout, req.EndTime); err != nil { + // return nil, 0, fmt.Errorf("结束时间格式错误,需为 %s", timeLayout) + // } + //} + //// 3. 校验结束时间不能早于开始时间(两者都存在时) + //if req.StartTime != "" && req.EndTime != "" { + // if endTime.Before(startTime) { // 用time.Before()判断时间顺序 + // return nil, 0, fmt.Errorf("结束时间不能早于开始时间") + // } + //} + if req.StartTime != "" && req.EndTime != "" { + if req.StartTime > req.EndTime { + return nil, 0, fmt.Errorf("结束时间不能早于开始时间") + } + } + // 4.时间范围查询 if req.StartTime != "" && req.EndTime != "" { db = db.Where("b.created_at BETWEEN ? AND ?", req.StartTime, req.EndTime) } else if req.StartTime != "" { @@ -378,7 +413,7 @@ func (s *sQuestionBank) ExportUserScore(r *ghttp.Request, ctx context.Context, r fileName := "用户成绩导出.xlsx" // === 4. 设置表头 === - headers := []string{"序号", "用户名", "用户身份", "精网号", "成绩", "创建时间"} + headers := []string{"序号", "用户名", "用户身份", "精网号", "成绩", "提交时间"} for i, header := range headers { col := string('A' + i) excelFile.SetCellValue(sheetName, col+"1", header)