fix
zhangqian
2024-05-17 798221261fe82561d10904182c0d644484c7ab95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package controllers
 
import (
    "errors"
    "github.com/gin-gonic/gin"
    "speechAnalysis/extend/code"
    "speechAnalysis/extend/util"
    "speechAnalysis/models"
    "speechAnalysis/pkg/logx"
    "speechAnalysis/request"
)
 
type TextCtl struct{}
 
// AddText
// @Tags      文字库
// @Summary   新增文字
// @Produce   application/json
// @Param     object  body request.AddTextReq true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/text/add [post]
func (slf TextCtl) AddText(c *gin.Context) {
    var req request.AddTextReq
    if err := c.BindJSON(&req); err != nil {
        logx.Errorf("add text params err:%v", err)
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    text := models.Word{
        Content:          req.Content,
        LocomotiveNumber: req.LocomotiveNumber,
    }
 
    if err := slf.paramsCheck(text); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    if err := models.NewWordSearch().Create(&text); err != nil {
        util.ResponseFormat(c, code.SaveFail, "添加失败,请检查是否重复")
        return
    }
 
    util.ResponseFormat(c, code.Success, "添加成功")
}
 
func (slf TextCtl) paramsCheck(text models.Word) (err error) {
    if text.Content == "" || text.LocomotiveNumber == "" {
        return errors.New("参数缺失")
    }
    _, err = models.NewWordSearch().SetLocomotiveNumber(text.LocomotiveNumber).SetContent(text.Content).First()
    if err == nil {
        return errors.New("文字重复")
    }
    return nil
}
 
// List
// @Tags      文字库
// @Summary   文字库列表
// @Produce   application/json
// @Param     object  query    request.GetTextList true  "参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Word}  "成功"
// @Router    /api-sa/v1/text/list [get]
func (slf TextCtl) List(c *gin.Context) {
    var params request.GetTextList
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    if !params.PageInfo.Check() {
        util.ResponseFormat(c, code.RequestParamError, "分页参数错误")
        return
    }
 
    list, total, err := models.NewWordSearch().
        SetPage(params.Page, params.PageSize).
        SetKeyword(params.Keyword).
        Find()
 
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
 
    util.ResponseFormatList(c, code.Success, list, total)
}