新增了错别字字典
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/dto"
|
||||
"knowledge/api/model"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/api/responses"
|
||||
"knowledge/global"
|
||||
"knowledge/utils"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type BaseErrorWord struct{}
|
||||
|
||||
// GetBaseErrorWordPage 获取列表-分页
|
||||
func (r *BaseErrorWord) GetBaseErrorWordPage(c *gin.Context) {
|
||||
baseErrorWordRequest := requests.BaseErrorWordRequest{}
|
||||
req := baseErrorWordRequest.GetBaseErrorWordPage
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := global.Validate.Struct(req); err != nil {
|
||||
responses.FailWithMessage(utils.Translate(err), c)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
baseErrorWordDao := dao.BaseErrorWordDao{}
|
||||
baseErrorWord, total, err := baseErrorWordDao.GetBaseErrorWordPageSearch(req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
g := dto.GetBaseErrorWordListDto(baseErrorWord)
|
||||
|
||||
result := make(map[string]interface{})
|
||||
result["page"] = req.Page
|
||||
result["page_size"] = req.PageSize
|
||||
result["total"] = total
|
||||
result["data"] = g
|
||||
responses.OkWithData(result, c)
|
||||
}
|
||||
|
||||
// AddBaseErrorWord 新增
|
||||
func (r *BaseErrorWord) AddBaseErrorWord(c *gin.Context) {
|
||||
baseErrorWordRequest := requests.BaseErrorWordRequest{}
|
||||
req := baseErrorWordRequest.AddBaseErrorWord
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := global.Validate.Struct(req); err != nil {
|
||||
responses.FailWithMessage(utils.Translate(err), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 新增
|
||||
baseErrorWordDao := dao.BaseErrorWordDao{}
|
||||
baseErrorWord := &model.BaseErrorWord{
|
||||
WordOld: req.WordOld,
|
||||
WordNew: req.WordNew,
|
||||
OperationTime: nil,
|
||||
}
|
||||
baseErrorWord, err := baseErrorWordDao.AddBaseErrorWord(tx, baseErrorWord)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("新增失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
// DeleteBaseErrorWord 删除
|
||||
func (r *BaseErrorWord) DeleteBaseErrorWord(c *gin.Context) {
|
||||
id := c.Param("word_id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
wordId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取详情
|
||||
baseErrorWordDao := dao.BaseErrorWordDao{}
|
||||
_, err = baseErrorWordDao.GetBaseErrorWordById(wordId)
|
||||
if err != nil {
|
||||
responses.FailWithMessage("数据不存在", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
err = baseErrorWordDao.DeleteBaseErrorWordById(tx, wordId)
|
||||
if err != nil {
|
||||
responses.FailWithMessage("删除失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
// OperationErrorWord 一键修改错别字
|
||||
func (r *BaseErrorWord) OperationErrorWord(c *gin.Context) {
|
||||
id := c.Param("word_id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
wordId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取详情
|
||||
baseErrorWordDao := dao.BaseErrorWordDao{}
|
||||
baseErrorWord, err := baseErrorWordDao.GetBaseErrorWordById(wordId)
|
||||
if err != nil {
|
||||
responses.FailWithMessage("数据不存在", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取相关错别字题目
|
||||
questionDao := dao.QuestionDao{}
|
||||
questions, err := questionDao.GetQuestionListByWord(baseErrorWord.WordOld)
|
||||
if err == nil && len(questions) <= 0 {
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
for _, question := range questions {
|
||||
questionName := utils.ReplaceString(question.QuestionName, baseErrorWord.WordOld, baseErrorWord.WordNew)
|
||||
questionAnswer := utils.ReplaceString(question.QuestionAnswer, baseErrorWord.WordOld, baseErrorWord.WordNew)
|
||||
questionAnalysis := utils.ReplaceString(question.QuestionAnalysis, baseErrorWord.WordOld, baseErrorWord.WordNew)
|
||||
|
||||
datas := make(map[string]interface{})
|
||||
if question.QuestionName != questionName {
|
||||
datas["question_name"] = questionName
|
||||
}
|
||||
|
||||
if question.QuestionAnswer != questionAnswer {
|
||||
datas["question_answer"] = questionAnswer
|
||||
}
|
||||
|
||||
if question.QuestionAnalysis != questionAnalysis {
|
||||
datas["question_analysis"] = questionAnalysis
|
||||
}
|
||||
|
||||
if len(datas) > 0 {
|
||||
err = questionDao.EditQuestionById(tx, question.QuestionId, datas)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
responses.Ok(c)
|
||||
}
|
||||
@@ -14,4 +14,5 @@ type Api struct {
|
||||
BaseToken // 基础数据-飞花令
|
||||
BaseTokenItem // 基础数据-飞花令-明细
|
||||
Editor // 配置-编辑器
|
||||
BaseErrorWord // 基础数据-错别字
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user