zhangqian
2024-05-21 111676bec43d0698c3f605993fe5b09bf4c93008
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package controllers
 
import (
    "errors"
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "path"
    "speechAnalysis/constvar"
    "speechAnalysis/extend/code"
    "speechAnalysis/extend/util"
    "speechAnalysis/models"
    "speechAnalysis/pkg/logx"
    "speechAnalysis/request"
    "speechAnalysis/response"
    "speechAnalysis/service"
    "speechAnalysis/utils/upload"
    "strings"
    "time"
)
 
type AudioCtl struct{}
 
// Upload
// @Tags      音频
// @Summary   上传音频
// @Produce   application/json
// @Param file formData file true "音频文件"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/audio/upload [post]
func (slf AudioCtl) Upload(c *gin.Context) {
    _, header, err := c.Request.FormFile("file")
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    filename := path.Base(header.Filename)
 
    arr := strings.Split(filename, "_")
    if len(arr) != 6 {
        util.ResponseFormat(c, code.RequestParamError, "文件名称错误")
        return
    }
 
    _, err = models.NewAudioSearch().SetName(filename).First()
    if err != gorm.ErrRecordNotFound {
        util.ResponseFormat(c, code.RequestParamError, "重复上传")
        return
    }
 
    oss := upload.NewOss()
    filePath, filename, uploadErr := oss.UploadFile(header)
    if uploadErr != nil {
        logx.Errorf("upload audio err: %v", err)
        util.ResponseFormat(c, code.RequestParamError, "上传失败")
        return
    }
 
    timeStr := arr[4] + strings.Split(arr[5], ".")[0]
 
    t, err := time.ParseInLocation("20060102150405", timeStr, time.Local)
 
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "时间格式不对")
        return
    }
 
    audio := &models.Audio{
        Name:             filename,
        Size:             header.Size,
        FilePath:         filePath,
        AudioStatus:      constvar.AudioStatusUploadOk,
        LocomotiveNumber: arr[0],
        TrainNumber:      arr[1],
        DriverNumber:     arr[2],
        Station:          arr[3],
        OccurrenceAt:     t,
        IsFollowed:       0,
    }
 
    if err = models.NewAudioSearch().Create(audio); err != nil {
        util.ResponseFormat(c, code.SaveFail, "上传失败")
        return
    }
    go func() {
 
        var trainInfoNames = []string{arr[0], arr[1], arr[3]}
 
        var (
            info   *models.TrainInfo
            err    error
            parent models.TrainInfo
        )
        for i := 0; i < 3; i++ {
            name := trainInfoNames[i]
            class := constvar.Class(i + 1)
            info, err = models.NewTrainInfoSearch().SetName(name).SetClass(class).First()
            if err == gorm.ErrRecordNotFound {
                info = &models.TrainInfo{
                    Name:     name,
                    Class:    class,
                    ParentID: parent.ID,
                }
                _ = models.NewTrainInfoSearch().Create(info)
            }
            parent = *info
        }
 
    }()
 
    util.ResponseFormat(c, code.Success, "添加成功")
}
 
func (slf AudioCtl) ParamsCheck(filename string) (err error) {
    arr := strings.Split(filename, "_")
    if len(arr) != 6 {
        return errors.New("文件格式错误")
    }
    return nil
}
 
// TrainInfoList
// @Tags      音频
// @Summary   获取火车信息
// @Produce   application/json
// @Param     object  query    request.GetTrainInfoList true  "参数"
// @Success   200   {object}  util.ResponseList{data=[]models.TrainInfo}  "成功"
// @Router    /api-sa/v1/audio/trainInfoList [get]
func (slf AudioCtl) TrainInfoList(c *gin.Context) {
    var params request.GetTrainInfoList
    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.NewTrainInfoSearch().
        SetPage(params.Page, params.PageSize).
        SetClass(params.Class).
        SetParentId(params.ParentID).
        Find()
 
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
 
    util.ResponseFormatList(c, code.Success, list, total)
}
 
// List
// @Tags      音频
// @Summary   音频分析检索
// @Produce   application/json
// @Param     object  query    request.GetAudioList true  "参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Audio}  "成功"
// @Router    /api-sa/v1/audio/list [get]
func (slf AudioCtl) List(c *gin.Context) {
    var params request.GetAudioList
    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.NewAudioSearch().
        SetPage(params.Page, params.PageSize).
        SetKeyword(params.Keyword).
        SetLocomotiveNumber(params.LocomotiveNumber).
        SetTrainNumber(params.TrainNumber).
        SetDriverNumber(params.DriverNumber).
        SetStation(params.StationNumber).
        SetBeginTime(params.BeginTime).
        SetEndTime(params.EndTime).
        SetIsFollowed(params.IsFollowed).
        Find()
 
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
 
    util.ResponseFormatList(c, code.Success, list, total)
}
 
// Process
// @Tags      音频
// @Summary   处理音频
// @Produce   application/json
// @Param     object  body request.ProcessAudio true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/audio/process [post]
func (slf AudioCtl) Process(c *gin.Context) {
    var params request.ProcessAudio
    if err := c.ShouldBind(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    err := service.Process(params.ID)
    if err != nil {
        util.ResponseFormat(c, code.InternalError, err.Error())
        return
    }
 
    util.ResponseFormat(c, code.UpdateSuccess, "成功")
}
 
// AudioInfo
// @Tags      音频
// @Summary   音频详情,含解析结果
// @Produce   application/json
// @Param     object  query request.ProcessAudio true  "参数"
// @Success   200 {object} util.Response{data=models.Audio} "成功"
// @Router    /api-sa/v1/audio/info [get]
func (slf AudioCtl) AudioInfo(c *gin.Context) {
    var params request.ProcessAudio
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    audio, err := models.NewAudioSearch().SetID(params.ID).First()
    if err != nil {
        util.ResponseFormat(c, code.InternalError, "请求失败")
        return
    }
    audioText, err := models.NewAudioTextSearch().SetAudioID(audio.ID).First()
    if err == nil {
        audio.AudioText = audioText.AudioText
    }
 
    util.ResponseFormat(c, code.UpdateSuccess, audio)
}
 
// AudioDownload
// @Tags      音频
// @Summary   音频下载
// @Produce   application/json
// @Param     object  query request.ProcessAudio true  "参数"
// @Success   200 {object} util.Response{data=models.Audio} "成功"
// @Router    /api-sa/v1/audio/download [get]
func (slf AudioCtl) AudioDownload(c *gin.Context) {
    var params request.ProcessAudio
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    audio, err := models.NewAudioSearch().SetID(params.ID).First()
    if err != nil {
        util.ResponseFormat(c, code.InternalError, "查询失败")
        return
    }
 
    if audio.FilePath == "" {
        util.ResponseFormat(c, code.InternalError, "查询失败")
        return
    }
 
    c.Header("Content-Description", "File Transfer")
    c.Header("Content-Disposition", "attachment; filename="+audio.Name)
    c.Header("Content-Type", "application/octet-stream")
    c.File(audio.FilePath)
}
 
// BatchProcess
// @Tags      音频
// @Summary   批量处理音频
// @Produce   application/json
// @Param     object  body request.BatchProcessAudio true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/audio/batchProcess [post]
func (slf AudioCtl) BatchProcess(c *gin.Context) {
    var params request.BatchProcessAudio
    if err := c.ShouldBind(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    var failedNumber int
    for _, audioID := range params.IDs {
        err := service.Process(audioID)
        if err != nil {
            logx.Errorf("%v,编号: %v", err.Error(), audioID)
            failedNumber++
            continue
        }
    }
 
    if failedNumber == 0 {
        util.ResponseFormat(c, code.UpdateSuccess, "成功")
        return
    } else if failedNumber < len(params.IDs) {
        util.ResponseFormat(c, code.RequestParamError, "部分处理失败")
        return
    } else {
        util.ResponseFormat(c, code.RequestParamError, "全部处理失败")
        return
    }
}
 
// Delete
// @Tags      音频
// @Summary   删除音频
// @Produce   application/json
// @Param     object  body request.ProcessAudio true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/audio/delete [delete]
func (slf AudioCtl) Delete(c *gin.Context) {
    var params request.ProcessAudio
    if err := c.ShouldBind(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    err := service.DeleteAudio(params.ID)
    if err != nil {
        util.ResponseFormat(c, code.InternalError, err.Error())
        return
    }
 
    util.ResponseFormat(c, code.DeleteSuccess, "成功")
}
 
// BatchDelete
// @Tags      音频
// @Summary   批量删除音频
// @Produce   application/json
// @Param     object  body request.BatchProcessAudio true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/audio/batchDelete [delete]
func (slf AudioCtl) BatchDelete(c *gin.Context) {
    var params request.BatchProcessAudio
    if err := c.ShouldBind(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    err := service.BatchDeleteAudio(params.IDs)
    if err != nil {
        util.ResponseFormat(c, code.InternalError, err.Error())
        return
    }
 
    util.ResponseFormat(c, code.DeleteSuccess, "成功")
}
 
// Follow
// @Tags      音频
// @Summary   关注/取消关注
// @Produce   application/json
// @Param     object  body request.FollowReq true  "参数"
// @Success   200 {object} util.Response{data=response.FollowResp} "成功"
// @Router    /api-sa/v1/audio/follow [post]
func (slf AudioCtl) Follow(c *gin.Context) {
    var params request.ProcessAudio
    if err := c.ShouldBind(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    followStatus, err := service.Follow(params.ID)
    if err != nil {
        util.ResponseFormat(c, code.InternalError, err.Error())
        return
    }
    resp := response.FollowResp{FollowStatus: followStatus}
 
    util.ResponseFormat(c, code.UpdateSuccess, resp)
}