题库增加题目数量展示

This commit is contained in:
李志强 2025-11-19 17:14:31 +08:00
parent 7664821d88
commit aae2fe1e14
3 changed files with 71 additions and 45 deletions

View File

@ -22,6 +22,7 @@
<el-table-column prop="id" label="ID" width="80" align="center" />
<el-table-column prop="bank_name" label="题库名称" min-width="200" show-overflow-tooltip />
<el-table-column prop="bank_desc" label="描述" min-width="200" show-overflow-tooltip />
<el-table-column prop="total_count" label="题目数量" min-width="60" align="center" />
<el-table-column label="操作" width="200" fixed="right" align="center">
<template #default="{ row }">
<el-button link type="primary" size="small" @click="handleEdit(row)">编辑</el-button>

View File

@ -2,9 +2,9 @@ package controllers
import (
"encoding/json"
"regexp"
"strconv"
"strings"
"regexp"
"server/models"
"server/services"
@ -18,27 +18,27 @@ type ExamQuestionController struct {
// normalizeText removes HTML tags, converts &nbsp; to space, and collapses whitespace
func normalizeText(s string) string {
str := strings.TrimSpace(s)
if str == "" {
return ""
}
// remove HTML tags
re := regexp.MustCompile("<[^>]*>")
str = re.ReplaceAllString(str, "")
// decode common entities
str = strings.ReplaceAll(str, "&nbsp;", " ")
str = strings.ReplaceAll(str, "&#160;", " ")
// collapse whitespace
reSpace := regexp.MustCompile(`\s+`)
str = reSpace.ReplaceAllString(str, " ")
return strings.TrimSpace(str)
str := strings.TrimSpace(s)
if str == "" {
return ""
}
// remove HTML tags
re := regexp.MustCompile("<[^>]*>")
str = re.ReplaceAllString(str, "")
// decode common entities
str = strings.ReplaceAll(str, "&nbsp;", " ")
str = strings.ReplaceAll(str, "&#160;", " ")
// collapse whitespace
reSpace := regexp.MustCompile(`\s+`)
str = reSpace.ReplaceAllString(str, " ")
return strings.TrimSpace(str)
}
// computeMatchRate returns a simple similarity percentage between two strings based on
// position-wise identical characters over the max length, rounded to integer 0-100.
func computeMatchRate(a, b string) int {
s1 := normalizeText(a)
s2 := normalizeText(b)
s1 := normalizeText(a)
s2 := normalizeText(b)
if s1 == "" && s2 == "" {
return 100
}
@ -83,7 +83,7 @@ type ExamQuestionBankController struct {
// @router /exam-questions [get]
func (c *ExamQuestionController) GetList() {
tenantId, _ := c.Ctx.Input.GetData("tenantId").(int)
keyword := c.GetString("keyword")
keyword := c.GetString("keyword")
typeStr := c.GetString("type")
bankId, _ := c.GetInt64("bank_id", 0)
minRate, _ := c.GetInt("min_rate", 0)
@ -99,38 +99,38 @@ func (c *ExamQuestionController) GetList() {
page, _ := c.GetInt("page", 1)
pageSize, _ := c.GetInt("pageSize", 10)
// use normalized keyword for DB searching to improve hit rate when editor HTML is posted
normKeyword := normalizeText(keyword)
list, total, err := services.GetExamQuestions(services.QuestionListParams{
TenantId: tenantId,
Keyword: normKeyword,
QuestionType: qtype,
BankId: bankId,
Page: page,
PageSize: pageSize,
})
// use normalized keyword for DB searching to improve hit rate when editor HTML is posted
normKeyword := normalizeText(keyword)
list, total, err := services.GetExamQuestions(services.QuestionListParams{
TenantId: tenantId,
Keyword: normKeyword,
QuestionType: qtype,
BankId: bankId,
Page: page,
PageSize: pageSize,
})
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "获取试题列表失败: " + err.Error(), "data": nil}
c.ServeJSON()
return
}
// 当启用相似度筛选且按关键字无结果时,回退到不带关键字的最近记录中做相似度匹配
if minRate > 0 && strings.TrimSpace(normKeyword) != "" && len(list) == 0 {
fbPageSize := 50
fbList, _, fbErr := services.GetExamQuestions(services.QuestionListParams{
TenantId: tenantId,
Keyword: "",
BankId: bankId,
Page: 1,
PageSize: fbPageSize,
})
if fbErr == nil {
list = fbList
}
}
// 当启用相似度筛选且按关键字无结果时,回退到不带关键字的最近记录中做相似度匹配
if minRate > 0 && strings.TrimSpace(normKeyword) != "" && len(list) == 0 {
fbPageSize := 50
fbList, _, fbErr := services.GetExamQuestions(services.QuestionListParams{
TenantId: tenantId,
Keyword: "",
BankId: bankId,
Page: 1,
PageSize: fbPageSize,
})
if fbErr == nil {
list = fbList
}
}
items := make([]map[string]interface{}, 0, len(list))
currentTitle := normalizeText(keyword)
currentTitle := normalizeText(keyword)
for _, q := range list {
if q == nil {
continue
@ -421,7 +421,7 @@ func (c *ExamQuestionBankController) GetBankList() {
tenantId, _ := c.Ctx.Input.GetData("tenantId").(int)
keyword := c.GetString("keyword")
page, _ := c.GetInt("page", 1)
pageSize, _ := c.GetInt("pageSize", 10)
pageSize, _ := c.GetInt("pageSize", 20)
list, total, err := services.GetExamQuestionBanks(services.BankListParams{
TenantId: tenantId,
Keyword: keyword,
@ -433,7 +433,22 @@ func (c *ExamQuestionBankController) GetBankList() {
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok", "data": map[string]interface{}{"list": list, "total": total}}
// enrich with total_count per bank
items := make([]map[string]interface{}, 0, len(list))
for _, b := range list {
if b == nil {
continue
}
cnt, _ := services.CountQuestionsInBank(tenantId, b.Id)
items = append(items, map[string]interface{}{
"id": b.Id,
"tenant_id": b.TenantId,
"bank_name": b.BankName,
"bank_desc": b.BankDesc,
"total_count": cnt,
})
}
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok", "data": map[string]interface{}{"list": items, "total": total}}
c.ServeJSON()
}

View File

@ -18,6 +18,16 @@ type QuestionListParams struct {
PageSize int
}
// CountQuestionsInBank 统计指定题库下的题目数量(仅统计未删除且启用的题目)
func CountQuestionsInBank(tenantId int, bankId int64) (int64, error) {
o := orm.NewOrm()
qs := o.QueryTable(new(models.ExamQuestion)).Filter("delete_time__isnull", true).Filter("status", 1).Filter("bank_id", bankId)
if tenantId > 0 {
qs = qs.Filter("tenant_id", tenantId)
}
return qs.Count()
}
type QuestionWithMeta struct {
Question *models.ExamQuestion
Options []*models.ExamQuestionOption