yinbangzhong
2024-07-29 9f78e3b126b15a9b331c3a1a318da1ceea30114c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package controllers
 
import (
    "errors"
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "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, "添加成功")
}
 
// UpdateText
// @Tags      文字库
// @Summary   修改文字
// @Produce   application/json
// @Param     object  body request.AddTextReq true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/text/update [post]
func (slf TextCtl) UpdateText(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{
        Model: gorm.Model{
            ID: req.Id,
        },
        Content:          req.Content,
        LocomotiveNumber: req.LocomotiveNumber,
    }
 
    if err := slf.paramsCheck(text); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    if req.Id == 0 {
        util.ResponseFormat(c, code.RequestParamError, errors.New("ID不能为空"))
        return
    }
 
    if err := models.NewWordSearch().Save(&text); err != nil {
        util.ResponseFormat(c, code.SaveFail, "修改失败,请检查是否重复")
        return
    }
 
    util.ResponseFormat(c, code.Success, "修改成功")
}
 
// DeleteText
// @Tags      文字库
// @Summary   修改文字
// @Produce   application/json
// @Param    object  body request.AddTextReq true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/text/delete [delete]
func (slf TextCtl) DeleteText(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
    }
 
    if err := models.NewWordSearch().SetID(req.Id).Delete(); 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)
}