audio.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package controllers
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. "gorm.io/gorm"
  6. "path"
  7. "speechAnalysis/constvar"
  8. "speechAnalysis/extend/code"
  9. "speechAnalysis/extend/util"
  10. "speechAnalysis/models"
  11. "speechAnalysis/pkg/logx"
  12. "speechAnalysis/request"
  13. "speechAnalysis/response"
  14. "speechAnalysis/service"
  15. "speechAnalysis/utils/upload"
  16. "strings"
  17. "time"
  18. )
  19. type AudioCtl struct{}
  20. // Upload
  21. // @Tags 音频
  22. // @Summary 上传音频
  23. // @Produce application/json
  24. // @Param file formData file true "音频文件"
  25. // @Success 200 {object} util.Response "成功"
  26. // @Router /api-sa/v1/audio/upload [post]
  27. func (slf AudioCtl) Upload(c *gin.Context) {
  28. _, header, err := c.Request.FormFile("file")
  29. if err != nil {
  30. util.ResponseFormat(c, code.RequestParamError, err.Error())
  31. return
  32. }
  33. filename := path.Base(header.Filename)
  34. arr := strings.Split(filename, "_")
  35. if len(arr) != 6 {
  36. util.ResponseFormat(c, code.RequestParamError, "文件名称错误")
  37. return
  38. }
  39. _, err = models.NewAudioSearch().SetName(filename).First()
  40. if err != gorm.ErrRecordNotFound {
  41. util.ResponseFormat(c, code.RequestParamError, "重复上传")
  42. return
  43. }
  44. oss := upload.NewOss()
  45. filePath, filename, uploadErr := oss.UploadFile(header)
  46. if uploadErr != nil {
  47. logx.Errorf("upload audio err: %v", err)
  48. util.ResponseFormat(c, code.RequestParamError, "上传失败")
  49. return
  50. }
  51. timeStr := arr[4] + strings.Split(arr[5], ".")[0]
  52. t, err := time.ParseInLocation("20060102150405", timeStr, time.Local)
  53. if err != nil {
  54. util.ResponseFormat(c, code.RequestParamError, "时间格式不对")
  55. return
  56. }
  57. audio := &models.Audio{
  58. Name: filename,
  59. Size: header.Size,
  60. FilePath: filePath,
  61. AudioStatus: constvar.AudioStatusUploadOk,
  62. LocomotiveNumber: arr[0],
  63. TrainNumber: arr[1],
  64. DriverNumber: arr[2],
  65. Station: arr[3],
  66. OccurrenceAt: t,
  67. IsFollowed: 0,
  68. }
  69. if err = models.NewAudioSearch().Create(audio); err != nil {
  70. util.ResponseFormat(c, code.SaveFail, "上传失败")
  71. return
  72. }
  73. go func() {
  74. var trainInfoNames = []string{arr[0], arr[1], arr[3]}
  75. var (
  76. info *models.TrainInfo
  77. err error
  78. parent models.TrainInfo
  79. )
  80. for i := 0; i < 3; i++ {
  81. name := trainInfoNames[i]
  82. class := constvar.Class(i + 1)
  83. info, err = models.NewTrainInfoSearch().SetName(name).SetClass(class).First()
  84. if err == gorm.ErrRecordNotFound {
  85. info = &models.TrainInfo{
  86. Name: name,
  87. Class: class,
  88. ParentID: parent.ID,
  89. }
  90. _ = models.NewTrainInfoSearch().Create(info)
  91. }
  92. parent = *info
  93. }
  94. }()
  95. util.ResponseFormat(c, code.Success, "添加成功")
  96. }
  97. func (slf AudioCtl) ParamsCheck(filename string) (err error) {
  98. arr := strings.Split(filename, "_")
  99. if len(arr) != 6 {
  100. return errors.New("文件格式错误")
  101. }
  102. return nil
  103. }
  104. // TrainInfoList
  105. // @Tags 音频
  106. // @Summary 获取火车信息
  107. // @Produce application/json
  108. // @Param object query request.GetTrainInfoList true "参数"
  109. // @Success 200 {object} util.ResponseList{data=[]models.TrainInfo} "成功"
  110. // @Router /api-sa/v1/audio/trainInfoList [get]
  111. func (slf AudioCtl) TrainInfoList(c *gin.Context) {
  112. var params request.GetTrainInfoList
  113. if err := c.ShouldBindQuery(&params); err != nil {
  114. util.ResponseFormat(c, code.RequestParamError, err.Error())
  115. return
  116. }
  117. if !params.PageInfo.Check() {
  118. util.ResponseFormat(c, code.RequestParamError, "分页参数错误")
  119. return
  120. }
  121. list, total, err := models.NewTrainInfoSearch().
  122. SetPage(params.Page, params.PageSize).
  123. SetClass(params.Class).
  124. SetParentId(params.ParentID).
  125. Find()
  126. if err != nil {
  127. util.ResponseFormat(c, code.RequestParamError, "查找失败")
  128. return
  129. }
  130. util.ResponseFormatList(c, code.Success, list, total)
  131. }
  132. // List
  133. // @Tags 音频
  134. // @Summary 音频分析检索
  135. // @Produce application/json
  136. // @Param object query request.GetAudioList true "参数"
  137. // @Success 200 {object} util.ResponseList{data=[]models.Audio} "成功"
  138. // @Router /api-sa/v1/audio/list [get]
  139. func (slf AudioCtl) List(c *gin.Context) {
  140. var params request.GetAudioList
  141. if err := c.ShouldBindQuery(&params); err != nil {
  142. util.ResponseFormat(c, code.RequestParamError, err.Error())
  143. return
  144. }
  145. if !params.PageInfo.Check() {
  146. util.ResponseFormat(c, code.RequestParamError, "分页参数错误")
  147. return
  148. }
  149. list, total, err := models.NewAudioSearch().
  150. SetPage(params.Page, params.PageSize).
  151. SetKeyword(params.Keyword).
  152. SetLocomotiveNumber(params.LocomotiveNumber).
  153. SetTrainNumber(params.TrainNumber).
  154. SetDriverNumber(params.DriverNumber).
  155. SetStation(params.StationNumber).
  156. Find()
  157. if err != nil {
  158. util.ResponseFormat(c, code.RequestParamError, "查找失败")
  159. return
  160. }
  161. util.ResponseFormatList(c, code.Success, list, total)
  162. }
  163. // Process
  164. // @Tags 音频
  165. // @Summary 处理音频
  166. // @Produce application/json
  167. // @Param object body request.ProcessAudio true "参数"
  168. // @Success 200 {object} util.Response "成功"
  169. // @Router /api-sa/v1/audio/process [post]
  170. func (slf AudioCtl) Process(c *gin.Context) {
  171. var params request.ProcessAudio
  172. if err := c.ShouldBind(&params); err != nil {
  173. util.ResponseFormat(c, code.RequestParamError, err.Error())
  174. return
  175. }
  176. err := service.Process(params.ID)
  177. if err != nil {
  178. util.ResponseFormat(c, code.InternalError, err.Error())
  179. return
  180. }
  181. util.ResponseFormat(c, code.UpdateSuccess, "成功")
  182. }
  183. // BatchProcess
  184. // @Tags 音频
  185. // @Summary 批量处理音频
  186. // @Produce application/json
  187. // @Param object body request.BatchProcessAudio true "参数"
  188. // @Success 200 {object} util.Response "成功"
  189. // @Router /api-sa/v1/audio/batchProcess [post]
  190. func (slf AudioCtl) BatchProcess(c *gin.Context) {
  191. var params request.BatchProcessAudio
  192. if err := c.ShouldBind(&params); err != nil {
  193. util.ResponseFormat(c, code.RequestParamError, err.Error())
  194. return
  195. }
  196. var failedNumber int
  197. for _, audioID := range params.IDs {
  198. err := service.Process(audioID)
  199. if err != nil {
  200. logx.Errorf("%v,编号: %v", err.Error(), audioID)
  201. failedNumber++
  202. continue
  203. }
  204. }
  205. if failedNumber == 0 {
  206. util.ResponseFormat(c, code.UpdateSuccess, "成功")
  207. return
  208. } else if failedNumber < len(params.IDs) {
  209. util.ResponseFormat(c, code.RequestParamError, "部分处理失败")
  210. return
  211. } else {
  212. util.ResponseFormat(c, code.RequestParamError, "全部处理失败")
  213. return
  214. }
  215. }
  216. // Delete
  217. // @Tags 音频
  218. // @Summary 删除音频
  219. // @Produce application/json
  220. // @Param object body request.ProcessAudio true "参数"
  221. // @Success 200 {object} util.Response "成功"
  222. // @Router /api-sa/v1/audio/delete [delete]
  223. func (slf AudioCtl) Delete(c *gin.Context) {
  224. var params request.ProcessAudio
  225. if err := c.ShouldBind(&params); err != nil {
  226. util.ResponseFormat(c, code.RequestParamError, err.Error())
  227. return
  228. }
  229. err := service.DeleteAudio(params.ID)
  230. if err != nil {
  231. util.ResponseFormat(c, code.InternalError, err.Error())
  232. return
  233. }
  234. util.ResponseFormat(c, code.DeleteSuccess, "成功")
  235. }
  236. // BatchDelete
  237. // @Tags 音频
  238. // @Summary 批量删除音频
  239. // @Produce application/json
  240. // @Param object body request.BatchProcessAudio true "参数"
  241. // @Success 200 {object} util.Response "成功"
  242. // @Router /api-sa/v1/audio/batchDelete [delete]
  243. func (slf AudioCtl) BatchDelete(c *gin.Context) {
  244. var params request.BatchProcessAudio
  245. if err := c.ShouldBind(&params); err != nil {
  246. util.ResponseFormat(c, code.RequestParamError, err.Error())
  247. return
  248. }
  249. err := service.BatchDeleteAudio(params.IDs)
  250. if err != nil {
  251. util.ResponseFormat(c, code.InternalError, err.Error())
  252. return
  253. }
  254. util.ResponseFormat(c, code.DeleteSuccess, "成功")
  255. }
  256. // Follow
  257. // @Tags 音频
  258. // @Summary 关注/取消关注
  259. // @Produce application/json
  260. // @Param object body request.FollowReq true "参数"
  261. // @Success 200 {object} util.Response{data=response.FollowResp} "成功"
  262. // @Router /api-sa/v1/audio/follow [post]
  263. func (slf AudioCtl) Follow(c *gin.Context) {
  264. var params request.ProcessAudio
  265. if err := c.ShouldBind(&params); err != nil {
  266. util.ResponseFormat(c, code.RequestParamError, err.Error())
  267. return
  268. }
  269. followStatus, err := service.Follow(params.ID)
  270. if err != nil {
  271. util.ResponseFormat(c, code.InternalError, err.Error())
  272. return
  273. }
  274. resp := response.FollowResp{FollowStatus: followStatus}
  275. util.ResponseFormat(c, code.UpdateSuccess, resp)
  276. }