speech_recognition.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. package speech_recognition
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/speech_recognition"
  7. "eta/eta_api/services"
  8. "eta/eta_api/services/alarm_msg"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "strings"
  13. "time"
  14. )
  15. type SpeechRecognitionController struct {
  16. controllers.BaseAuthController
  17. }
  18. type SpeechRecognitionCommonController struct {
  19. controllers.BaseCommonController
  20. }
  21. // RecTaskCallback
  22. // @Title 语音识别回调
  23. // @Description 语音识别回调
  24. // @Param request body services.TencentRecTaskCallback true "type json string"
  25. // @Success 200 string "操作成功"
  26. // @router /rec_task/callback [post]
  27. func (this *SpeechRecognitionCommonController) RecTaskCallback() {
  28. // 此接口返回指定响应体
  29. br := new(services.TencentRecTaskCallbackResp)
  30. var errMsg string
  31. defer func() {
  32. if errMsg != "" {
  33. br.Code = 403
  34. br.Message = "回调失败"
  35. go alarm_msg.SendAlarmMsg(fmt.Sprintf("语音识别回调失败, ErrMsg: %s", errMsg), 1)
  36. } else {
  37. br.Code = 0
  38. br.Message = "success"
  39. }
  40. _ = this.JSON(br, false, false)
  41. }()
  42. code, _ := this.GetInt("code", -1)
  43. requestId, _ := this.GetInt("requestId", 0)
  44. detail := this.GetString("resultDetail")
  45. utils.FileLog.Info(fmt.Sprintf("RecTaskCallback, requestId: %d", requestId))
  46. // 获取taskId对应的API请求及语音识别
  47. logOb := new(speech_recognition.SpeechRecognitionApiLog)
  48. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionApiLogCols.RequestId)
  49. pars := make([]interface{}, 0)
  50. pars = append(pars, requestId)
  51. apiLog, e := logOb.GetItemByCondition(cond, pars, "")
  52. if e != nil {
  53. errMsg = "获取API记录失败"
  54. utils.FileLog.Info("API回调-获取请求记录失败, Err: " + e.Error())
  55. return
  56. }
  57. speechOb := new(speech_recognition.SpeechRecognition)
  58. speechItem, e := speechOb.GetItemById(apiLog.SpeechRecognitionId)
  59. if e != nil {
  60. errMsg = "获取语音识别失败"
  61. utils.FileLog.Info("获取语音识别失败, Err: " + e.Error())
  62. return
  63. }
  64. // API结果返回有误
  65. nowTime := time.Now().Local()
  66. if code != speech_recognition.ApiRequestCodeSuccess {
  67. convertRemark := speech_recognition.ApiErrMsgMapping[code]
  68. if convertRemark == "" {
  69. convertRemark = fmt.Sprintf("未知错误: %d", code)
  70. }
  71. speechItem.ConvertRemark = convertRemark
  72. speechItem.State = speech_recognition.SpeechRecognitionStateFail
  73. speechItem.ModifyTime = nowTime
  74. speechCols := []string{speech_recognition.SpeechRecognitionCols.ConvertRemark, speech_recognition.SpeechRecognitionCols.State, speech_recognition.SpeechRecognitionCols.ModifyTime}
  75. apiLog.RequestCode = code
  76. apiLog.RequestResult = convertRemark
  77. apiLog.ModifyTime = nowTime
  78. apiLogCols := []string{speech_recognition.SpeechRecognitionApiLogCols.RequestCode, speech_recognition.SpeechRecognitionApiLogCols.RequestResult, speech_recognition.SpeechRecognitionApiLogCols.ModifyTime}
  79. // 更新语音识别及API记录
  80. if e := speech_recognition.UpdateSpeechAndApiLog(speechItem, speechCols, apiLog, apiLogCols); e != nil {
  81. errMsg = "更新API返回结果失败"
  82. utils.FileLog.Info("更新API返回结果失败, Err: " + e.Error())
  83. }
  84. return
  85. }
  86. // 解析转写段落内容
  87. sentences := make([]*services.TencentRecTaskSentenceDetail, 0)
  88. if e := json.Unmarshal([]byte(detail), &sentences); e != nil {
  89. errMsg = "解析语音识别内容失败"
  90. utils.FileLog.Info("解析语音识别内容失败, Err: " + e.Error())
  91. return
  92. }
  93. contents := make([]*speech_recognition.SpeechRecognitionContent, 0)
  94. sorts := 0 // API返回的结果本身是已排过序的
  95. var abstract string
  96. var abstractLimit int
  97. for _, v := range sentences {
  98. sorts += 1
  99. t := new(speech_recognition.SpeechRecognitionContent)
  100. t.SpeechRecognitionId = speechItem.SpeechRecognitionId
  101. t.Sort = sorts
  102. t.Content = v.FinalSentence
  103. t.StartMs = v.StartMs
  104. t.EndMs = v.EndMs
  105. t.CreateTime = nowTime
  106. t.ModifyTime = nowTime
  107. contents = append(contents, t)
  108. // 取前几段作为摘要保存
  109. if abstractLimit < 5 {
  110. abstractLimit += 1
  111. abstract += v.FinalSentence
  112. }
  113. }
  114. speechItem.Abstract = abstract
  115. speechItem.State = speech_recognition.SpeechRecognitionStateSuccess
  116. speechItem.ModifyTime = nowTime
  117. speechCols := []string{speech_recognition.SpeechRecognitionCols.Abstract, speech_recognition.SpeechRecognitionCols.State, speech_recognition.SpeechRecognitionCols.ModifyTime}
  118. apiLog.RequestCode = code
  119. apiLog.RequestResult = detail
  120. apiLog.ModifyTime = time.Now().Local()
  121. apiLogCols := []string{speech_recognition.SpeechRecognitionApiLogCols.RequestCode, speech_recognition.SpeechRecognitionApiLogCols.RequestResult, speech_recognition.SpeechRecognitionApiLogCols.ModifyTime}
  122. // 新增解析内容并更新语音识别及API记录
  123. if e := speech_recognition.CreateContentAndUpdateSpeechAndApiLog(contents, speechItem, speechCols, apiLog, apiLogCols); e != nil {
  124. errMsg = "新增API返回结果失败"
  125. utils.FileLog.Info("新增API返回结果失败, Err: " + e.Error())
  126. }
  127. }
  128. // Convert
  129. // @Title 语音转换
  130. // @Description 语音转换
  131. // @Param request body speech_recognition.SpeechRecognitionConvertReq true "type json string"
  132. // @Success 200 string "操作成功"
  133. // @router /convert [post]
  134. func (this *SpeechRecognitionController) Convert() {
  135. br := new(models.BaseResponse).Init()
  136. defer func() {
  137. if br.ErrMsg == "" {
  138. br.IsSendEmail = false
  139. }
  140. this.Data["json"] = br
  141. this.ServeJSON()
  142. }()
  143. sysUser := this.SysUser
  144. if sysUser == nil {
  145. br.Msg = "请登录"
  146. br.ErrMsg = "请登录,SysUser Is Empty"
  147. br.Ret = 408
  148. return
  149. }
  150. var req speech_recognition.SpeechRecognitionConvertReq
  151. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  152. br.Msg = "参数有误"
  153. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  154. return
  155. }
  156. if req.MenuId <= 0 {
  157. br.Msg = "请选择目录"
  158. return
  159. }
  160. if len(req.Files) == 0 {
  161. br.Msg = "请上传转写文件"
  162. return
  163. }
  164. for _, r := range req.Files {
  165. if r.FileName == "" && r.ResourceUrl == "" {
  166. br.Msg = "转写文件有误,请检查"
  167. return
  168. }
  169. }
  170. speeches := make([]*speech_recognition.SpeechRecognition, 0)
  171. nowTime := time.Now().Local()
  172. for _, v := range req.Files {
  173. t := new(speech_recognition.SpeechRecognition)
  174. t.FileName = v.FileName
  175. t.ResourceUrl = v.ResourceUrl
  176. t.MenuId = req.MenuId
  177. // TODO:所属目录位置
  178. t.SysUserId = sysUser.AdminId
  179. t.SysUserName = sysUser.RealName
  180. t.State = speech_recognition.SpeechRecognitionStateWait
  181. t.CreateTime = nowTime
  182. t.ModifyTime = nowTime
  183. // CreateMulti拿不到主键, 此处用循环新增获取
  184. if e := t.Create(); e != nil {
  185. br.Msg = "操作失败"
  186. br.ErrMsg = "批量新增转写文件失败, Err: " + e.Error()
  187. return
  188. }
  189. speeches = append(speeches, t)
  190. }
  191. // 批量转写语音
  192. go func() {
  193. services.BatchConvertSpeech(speeches)
  194. }()
  195. br.Ret = 200
  196. br.Success = true
  197. br.Msg = "操作成功"
  198. }
  199. // ConvertList
  200. // @Title 转换列表
  201. // @Description 转换列表
  202. // @Success 200 {object} speech_recognition.SpeechRecognitionItem
  203. // @router /convert_list [get]
  204. func (this *SpeechRecognitionController) ConvertList() {
  205. br := new(models.BaseResponse).Init()
  206. defer func() {
  207. if br.ErrMsg == "" {
  208. br.IsSendEmail = false
  209. }
  210. this.Data["json"] = br
  211. this.ServeJSON()
  212. }()
  213. sysUser := this.SysUser
  214. if sysUser == nil {
  215. br.Msg = "请登录"
  216. br.ErrMsg = "请登录,SysUser Is Empty"
  217. br.Ret = 408
  218. return
  219. }
  220. // 仅取待转换和转换失败的
  221. states := []int{speech_recognition.SpeechRecognitionStateWait, speech_recognition.SpeechRecognitionStateFail}
  222. speechOb := new(speech_recognition.SpeechRecognition)
  223. cond := fmt.Sprintf(` AND %s = ? AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.SysUserId, speech_recognition.SpeechRecognitionCols.State, utils.GetOrmInReplace(len(states)))
  224. pars := make([]interface{}, 0)
  225. pars = append(pars, sysUser.AdminId, states)
  226. list, e := speechOb.GetItemsByCondition(cond, pars, []string{}, "")
  227. if e != nil {
  228. br.Msg = "获取失败"
  229. br.ErrMsg = "获取转写文件列表失败, Err: " + e.Error()
  230. return
  231. }
  232. resp := make([]*speech_recognition.SpeechRecognitionItem, 0)
  233. for _, v := range list {
  234. resp = append(resp, speech_recognition.FormatSpeechRecognition2Item(v))
  235. }
  236. br.Data = resp
  237. br.Ret = 200
  238. br.Success = true
  239. br.Msg = "获取成功"
  240. }
  241. // Save
  242. // @Title 保存内容
  243. // @Description 保存内容
  244. // @Param request body speech_recognition.SpeechRecognitionSaveReq true "type json string"
  245. // @Success 200 string "操作成功"
  246. // @router /save [post]
  247. func (this *SpeechRecognitionController) Save() {
  248. br := new(models.BaseResponse).Init()
  249. defer func() {
  250. if br.ErrMsg == "" {
  251. br.IsSendEmail = false
  252. }
  253. this.Data["json"] = br
  254. this.ServeJSON()
  255. }()
  256. sysUser := this.SysUser
  257. if sysUser == nil {
  258. br.Msg = "请登录"
  259. br.ErrMsg = "请登录,SysUser Is Empty"
  260. br.Ret = 408
  261. return
  262. }
  263. var req speech_recognition.SpeechRecognitionSaveReq
  264. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  265. br.Msg = "参数有误"
  266. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  267. return
  268. }
  269. if req.SpeechRecognitionId <= 0 {
  270. br.Msg = "参数有误"
  271. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  272. return
  273. }
  274. if len(req.Contents) == 0 {
  275. br.Ret = 200
  276. br.Success = true
  277. br.Msg = "操作成功"
  278. return
  279. }
  280. speechOb := new(speech_recognition.SpeechRecognition)
  281. _, e := speechOb.GetItemById(req.SpeechRecognitionId)
  282. if e != nil {
  283. if e.Error() == utils.ErrNoRow() {
  284. br.Msg = "转写文件不存在,请刷新页面"
  285. return
  286. }
  287. br.Msg = "操作失败"
  288. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  289. return
  290. }
  291. // 批量修改内容
  292. contentOb := new(speech_recognition.SpeechRecognitionContent)
  293. if e = contentOb.BatchUpdateContents(req.Contents); e != nil {
  294. br.Msg = "操作失败"
  295. br.ErrMsg = "批量修改内容失败, Err: " + e.Error()
  296. return
  297. }
  298. br.Ret = 200
  299. br.Success = true
  300. br.Msg = "操作成功"
  301. }
  302. // RemoveFile
  303. // @Title (软)删除文件
  304. // @Description (软)删除文件
  305. // @Param request body speech_recognition.SpeechRecognitionRemoveFileReq true "type json string"
  306. // @Success 200 string "操作成功"
  307. // @router /remove_file [post]
  308. func (this *SpeechRecognitionController) RemoveFile() {
  309. br := new(models.BaseResponse).Init()
  310. defer func() {
  311. if br.ErrMsg == "" {
  312. br.IsSendEmail = false
  313. }
  314. this.Data["json"] = br
  315. this.ServeJSON()
  316. }()
  317. sysUser := this.SysUser
  318. if sysUser == nil {
  319. br.Msg = "请登录"
  320. br.ErrMsg = "请登录,SysUser Is Empty"
  321. br.Ret = 408
  322. return
  323. }
  324. var req speech_recognition.SpeechRecognitionRemoveFileReq
  325. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  326. br.Msg = "参数有误"
  327. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  328. return
  329. }
  330. if req.SpeechRecognitionId <= 0 {
  331. br.Msg = "参数有误"
  332. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  333. return
  334. }
  335. speechOb := new(speech_recognition.SpeechRecognition)
  336. speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
  337. if e != nil {
  338. if e.Error() == utils.ErrNoRow() {
  339. br.Msg = "转写文件不存在,请刷新页面"
  340. return
  341. }
  342. br.Msg = "操作失败"
  343. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  344. return
  345. }
  346. speechItem.FileState = speech_recognition.SpeechRecognitionFileRemoveFlag
  347. speechItem.ModifyTime = time.Now().Local()
  348. updateCols := []string{speech_recognition.SpeechRecognitionCols.FileState, speech_recognition.SpeechRecognitionCols.ModifyTime}
  349. if e = speechItem.Update(updateCols); e != nil {
  350. br.Msg = "操作失败"
  351. br.ErrMsg = "软删除转写文件失败, Err: " + e.Error()
  352. return
  353. }
  354. br.Ret = 200
  355. br.Success = true
  356. br.Msg = "操作成功"
  357. }
  358. // Rename
  359. // @Title 重命名
  360. // @Description 重命名
  361. // @Param request body speech_recognition.SpeechRecognitionRenameReq true "type json string"
  362. // @Success 200 string "操作成功"
  363. // @router /rename [post]
  364. func (this *SpeechRecognitionController) Rename() {
  365. br := new(models.BaseResponse).Init()
  366. defer func() {
  367. if br.ErrMsg == "" {
  368. br.IsSendEmail = false
  369. }
  370. this.Data["json"] = br
  371. this.ServeJSON()
  372. }()
  373. sysUser := this.SysUser
  374. if sysUser == nil {
  375. br.Msg = "请登录"
  376. br.ErrMsg = "请登录,SysUser Is Empty"
  377. br.Ret = 408
  378. return
  379. }
  380. var req speech_recognition.SpeechRecognitionRenameReq
  381. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  382. br.Msg = "参数有误"
  383. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  384. return
  385. }
  386. if req.SpeechRecognitionId <= 0 {
  387. br.Msg = "参数有误"
  388. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  389. return
  390. }
  391. req.FileName = strings.TrimSpace(req.FileName)
  392. if req.FileName == "" {
  393. br.Msg = "请输入文件名称"
  394. return
  395. }
  396. speechOb := new(speech_recognition.SpeechRecognition)
  397. speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
  398. if e != nil {
  399. if e.Error() == utils.ErrNoRow() {
  400. br.Msg = "转写文件不存在,请刷新页面"
  401. return
  402. }
  403. br.Msg = "操作失败"
  404. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  405. return
  406. }
  407. // 重名校验
  408. {
  409. cond := fmt.Sprintf(` AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionCols.FileName, speech_recognition.SpeechRecognitionCols.SpeechRecognitionId)
  410. pars := make([]interface{}, 0)
  411. pars = append(pars, req.FileName, req.SpeechRecognitionId)
  412. exists, e := speechOb.GetItemByCondition(cond, pars, "")
  413. if e != nil && e.Error() != utils.ErrNoRow() {
  414. br.Msg = "操作失败"
  415. br.ErrMsg = "获取同名转写文件失败, Err: " + e.Error()
  416. return
  417. }
  418. if exists != nil && exists.SpeechRecognitionId > 0 {
  419. br.Msg = "文件名称已存在,请重新输入"
  420. return
  421. }
  422. }
  423. speechItem.FileName = req.FileName
  424. speechItem.ModifyTime = time.Now().Local()
  425. updateCols := []string{speech_recognition.SpeechRecognitionCols.FileName, speech_recognition.SpeechRecognitionCols.ModifyTime}
  426. if e = speechItem.Update(updateCols); e != nil {
  427. br.Msg = "操作失败"
  428. br.ErrMsg = "转写文件重命名失败, Err: " + e.Error()
  429. return
  430. }
  431. br.Ret = 200
  432. br.Success = true
  433. br.Msg = "操作成功"
  434. }
  435. // Remove
  436. // @Title 删除
  437. // @Description 删除
  438. // @Param request body speech_recognition.SpeechRecognitionRemoveReq true "type json string"
  439. // @Success 200 string "操作成功"
  440. // @router /remove [post]
  441. func (this *SpeechRecognitionController) Remove() {
  442. br := new(models.BaseResponse).Init()
  443. defer func() {
  444. if br.ErrMsg == "" {
  445. br.IsSendEmail = false
  446. }
  447. this.Data["json"] = br
  448. this.ServeJSON()
  449. }()
  450. sysUser := this.SysUser
  451. if sysUser == nil {
  452. br.Msg = "请登录"
  453. br.ErrMsg = "请登录,SysUser Is Empty"
  454. br.Ret = 408
  455. return
  456. }
  457. var req speech_recognition.SpeechRecognitionRemoveReq
  458. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  459. br.Msg = "参数有误"
  460. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  461. return
  462. }
  463. if req.SpeechRecognitionId <= 0 {
  464. br.Msg = "参数有误"
  465. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  466. return
  467. }
  468. speechOb := new(speech_recognition.SpeechRecognition)
  469. speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
  470. if e != nil {
  471. if e.Error() == utils.ErrNoRow() {
  472. br.Ret = 200
  473. br.Success = true
  474. br.Msg = "操作成功"
  475. return
  476. }
  477. br.Msg = "操作失败"
  478. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  479. return
  480. }
  481. if e = speechItem.Del(); e != nil {
  482. br.Msg = "操作失败"
  483. br.ErrMsg = "删除转写文件失败, Err: " + e.Error()
  484. return
  485. }
  486. // 清除标签关联
  487. go func() {
  488. mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
  489. _ = mappingOb.ClearMappingBySpeechId(req.SpeechRecognitionId)
  490. }()
  491. br.Ret = 200
  492. br.Success = true
  493. br.Msg = "操作成功"
  494. }
  495. // SaveTag
  496. // @Title 保存标签
  497. // @Description 保存标签
  498. // @Param request body speech_recognition.SpeechRecognitionSaveTagReq true "type json string"
  499. // @Success 200 string "操作成功"
  500. // @router /save_tag [post]
  501. func (this *SpeechRecognitionController) SaveTag() {
  502. br := new(models.BaseResponse).Init()
  503. defer func() {
  504. if br.ErrMsg == "" {
  505. br.IsSendEmail = false
  506. }
  507. this.Data["json"] = br
  508. this.ServeJSON()
  509. }()
  510. sysUser := this.SysUser
  511. if sysUser == nil {
  512. br.Msg = "请登录"
  513. br.ErrMsg = "请登录,SysUser Is Empty"
  514. br.Ret = 408
  515. return
  516. }
  517. var req speech_recognition.SpeechRecognitionSaveTagReq
  518. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  519. br.Msg = "参数有误"
  520. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  521. return
  522. }
  523. if req.SpeechRecognitionId <= 0 {
  524. br.Msg = "参数有误"
  525. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  526. return
  527. }
  528. speechOb := new(speech_recognition.SpeechRecognition)
  529. _, e := speechOb.GetItemById(req.SpeechRecognitionId)
  530. if e != nil {
  531. if e.Error() == utils.ErrNoRow() {
  532. br.Msg = "转写文件不存在,请刷新页面"
  533. return
  534. }
  535. br.Msg = "操作失败"
  536. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  537. return
  538. }
  539. // 清除原标签
  540. mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
  541. if e = mappingOb.ClearMappingBySpeechId(req.SpeechRecognitionId); e != nil {
  542. br.Msg = "操作失败"
  543. br.ErrMsg = "清除转写文件标签失败, Err: " + e.Error()
  544. return
  545. }
  546. // 保存新标签
  547. if len(req.TagIds) > 0 {
  548. mappings := make([]*speech_recognition.SpeechRecognitionTagMapping, 0)
  549. for _, v := range req.TagIds {
  550. if v <= 0 {
  551. continue
  552. }
  553. mappings = append(mappings, &speech_recognition.SpeechRecognitionTagMapping{
  554. TagId: v,
  555. SpeechRecognitionId: req.SpeechRecognitionId,
  556. })
  557. }
  558. if e = mappingOb.CreateMulti(mappings); e != nil {
  559. br.Msg = "操作失败"
  560. br.ErrMsg = "批量新增转写文件标签失败, Err: " + e.Error()
  561. return
  562. }
  563. }
  564. br.Ret = 200
  565. br.Success = true
  566. br.Msg = "操作成功"
  567. }
  568. // List
  569. // @Title 语音识别列表
  570. // @Description 语音识别列表
  571. // @Param FileName query string false "文件名称"
  572. // @Param StartTime query string false "开始时间"
  573. // @Param EndTime query string false "结束时间"
  574. // @Param CreateUserId query int false "创建人ID"
  575. // @Param TagId query int false "标签ID"
  576. // @Success 200 {object} speech_recognition.SpeechRecognitionListResp
  577. // @router /list [get]
  578. func (this *SpeechRecognitionController) List() {
  579. br := new(models.BaseResponse).Init()
  580. defer func() {
  581. if br.ErrMsg == "" {
  582. br.IsSendEmail = false
  583. }
  584. this.Data["json"] = br
  585. this.ServeJSON()
  586. }()
  587. sysUser := this.SysUser
  588. if sysUser == nil {
  589. br.Msg = "请登录"
  590. br.ErrMsg = "请登录,SysUser Is Empty"
  591. br.Ret = 408
  592. return
  593. }
  594. params := new(speech_recognition.SpeechRecognitionListReq)
  595. if e := this.ParseForm(params); e != nil {
  596. br.Msg = "获取失败"
  597. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  598. return
  599. }
  600. params.FileName = strings.TrimSpace(params.FileName)
  601. dataResp := new(speech_recognition.SpeechRecognitionListResp)
  602. cond := ``
  603. pars := make([]interface{}, 0)
  604. // 筛选项
  605. {
  606. if params.FileName != "" {
  607. cond += fmt.Sprintf(` AND %s LIKE ?`, speech_recognition.SpeechRecognitionCols.FileName)
  608. pars = append(pars, fmt.Sprint("%", params.FileName, "%"))
  609. }
  610. if params.StartTime != "" && params.EndTime != "" {
  611. _, e := time.Parse(utils.FormatDate, params.StartTime)
  612. if e != nil {
  613. br.Msg = "开始时间格式有误"
  614. return
  615. }
  616. _, e = time.Parse(utils.FormatDate, params.EndTime)
  617. if e != nil {
  618. br.Msg = "结束时间格式有误"
  619. return
  620. }
  621. st := fmt.Sprintf("%s 00:00:00", params.StartTime)
  622. ed := fmt.Sprintf("%s 23:59:59", params.EndTime)
  623. cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, speech_recognition.SpeechRecognitionCols.CreateTime)
  624. pars = append(pars, st, ed)
  625. }
  626. if params.CreateUserId > 0 {
  627. cond += fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionCols.SysUserId)
  628. pars = append(pars, params.CreateUserId)
  629. }
  630. // 标签筛选
  631. if params.TagId > 0 {
  632. mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
  633. tagSpeechIds, e := mappingOb.GetSpeechIdsByTagId(params.TagId)
  634. if e != nil {
  635. br.Msg = "获取失败"
  636. br.ErrMsg = "获取标签关联语音识别失败, Err: " + e.Error()
  637. return
  638. }
  639. if len(tagSpeechIds) == 0 {
  640. br.Data = dataResp
  641. br.Ret = 200
  642. br.Success = true
  643. br.Msg = "获取成功"
  644. return
  645. }
  646. cond += fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.SpeechRecognitionId, utils.GetOrmInReplace(len(tagSpeechIds)))
  647. pars = append(pars, tagSpeechIds)
  648. }
  649. }
  650. // 分页列表
  651. speechOb := new(speech_recognition.SpeechRecognition)
  652. total, e := speechOb.GetCountByCondition(cond, pars)
  653. if e != nil {
  654. br.Msg = "获取失败"
  655. br.ErrMsg = "获取语音识别列表总数失败, Err: " + e.Error()
  656. return
  657. }
  658. var startSize int
  659. if params.PageSize <= 0 {
  660. params.PageSize = utils.PageSize20
  661. }
  662. if params.CurrentIndex <= 0 {
  663. params.CurrentIndex = 1
  664. }
  665. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  666. list, e := speechOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, params.PageSize)
  667. if e != nil {
  668. br.Msg = "获取失败"
  669. br.ErrMsg = "获取语音识别列表失败, Err: " + e.Error()
  670. return
  671. }
  672. // 获取标签
  673. speechIds := make([]int, 0)
  674. for _, v := range list {
  675. speechIds = append(speechIds, v.SpeechRecognitionId)
  676. }
  677. mappingTags, e := speech_recognition.GetSpeechRecognitionTagsBySpeechIds(speechIds)
  678. if e != nil {
  679. br.Msg = "获取失败"
  680. br.ErrMsg = "获取语音识别列表标签失败, Err: " + e.Error()
  681. return
  682. }
  683. speechDetailTags := make(map[int][]*speech_recognition.SpeechRecognitionDetailTag)
  684. for _, v := range mappingTags {
  685. if speechDetailTags[v.SpeechRecognitionId] == nil {
  686. speechDetailTags[v.SpeechRecognitionId] = make([]*speech_recognition.SpeechRecognitionDetailTag, 0)
  687. }
  688. speechDetailTags[v.SpeechRecognitionId] = append(speechDetailTags[v.SpeechRecognitionId], &speech_recognition.SpeechRecognitionDetailTag{
  689. TagId: v.TagId,
  690. TagName: v.TagName,
  691. })
  692. }
  693. respList := make([]*speech_recognition.SpeechRecognitionDetailItem, 0)
  694. for _, v := range list {
  695. t := speech_recognition.FormatSpeechRecognition2DetailItem(v, make([]*speech_recognition.SpeechRecognitionContentItem, 0), speechDetailTags[v.SpeechRecognitionId])
  696. respList = append(respList, t)
  697. }
  698. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  699. dataResp.Paging = page
  700. dataResp.List = respList
  701. br.Data = dataResp
  702. br.Ret = 200
  703. br.Success = true
  704. br.Msg = "获取成功"
  705. }
  706. // Detail
  707. // @Title 语音识别详情
  708. // @Description 语音识别详情
  709. // @Param SpeechRecognitionId query int true "语音识别ID"
  710. // @Success 200 {object} speech_recognition.SpeechRecognitionDetailItem
  711. // @router /detail [get]
  712. func (this *SpeechRecognitionController) Detail() {
  713. br := new(models.BaseResponse).Init()
  714. defer func() {
  715. if br.ErrMsg == "" {
  716. br.IsSendEmail = false
  717. }
  718. this.Data["json"] = br
  719. this.ServeJSON()
  720. }()
  721. sysUser := this.SysUser
  722. if sysUser == nil {
  723. br.Msg = "请登录"
  724. br.ErrMsg = "请登录,SysUser Is Empty"
  725. br.Ret = 408
  726. return
  727. }
  728. speechId, _ := this.GetInt("SpeechRecognitionId")
  729. if speechId <= 0 {
  730. br.Msg = "参数有误"
  731. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", speechId)
  732. return
  733. }
  734. speechOb := new(speech_recognition.SpeechRecognition)
  735. speechItem, e := speechOb.GetItemById(speechId)
  736. if e != nil {
  737. if e.Error() == utils.ErrNoRow() {
  738. br.Msg = "转写文件不存在,请刷新页面"
  739. return
  740. }
  741. br.Msg = "获取失败"
  742. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  743. return
  744. }
  745. // 获取内容
  746. contents := make([]*speech_recognition.SpeechRecognitionContentItem, 0)
  747. {
  748. contentOb := new(speech_recognition.SpeechRecognitionContent)
  749. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionContentCols.SpeechRecognitionId)
  750. pars := make([]interface{}, 0)
  751. pars = append(pars, speechId)
  752. list, e := contentOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionContentCols.Sort))
  753. if e != nil {
  754. br.Msg = "获取失败"
  755. br.ErrMsg = "获取语音识别内容失败, Err: " + e.Error()
  756. return
  757. }
  758. for _, v := range list {
  759. contents = append(contents, speech_recognition.FormatSpeechRecognitionContent2Item(v))
  760. }
  761. }
  762. // 获取标签
  763. tags, e := speech_recognition.GetSpeechRecognitionTagBySpeechId(speechId)
  764. if e != nil {
  765. br.Msg = "获取失败"
  766. br.ErrMsg = "获取语音识别标签失败, Err: " + e.Error()
  767. return
  768. }
  769. detail := speech_recognition.FormatSpeechRecognition2DetailItem(speechItem, contents, tags)
  770. br.Data = detail
  771. br.Ret = 200
  772. br.Success = true
  773. br.Msg = "获取成功"
  774. }
  775. // TODO:Move
  776. // @Title 移动语音识别/目录
  777. // @Description 移动标签/目录
  778. // @Param request body speech_recognition.SpeechRecognitionTagRemoveReq true "type json string"
  779. // @Success 200 string "操作成功"
  780. // @router /move [post]
  781. func (this *SpeechRecognitionController) Move() {
  782. br := new(models.BaseResponse).Init()
  783. defer func() {
  784. if br.ErrMsg == "" {
  785. br.IsSendEmail = false
  786. }
  787. this.Data["json"] = br
  788. this.ServeJSON()
  789. }()
  790. sysUser := this.SysUser
  791. if sysUser == nil {
  792. br.Msg = "请登录"
  793. br.ErrMsg = "请登录,SysUser Is Empty"
  794. br.Ret = 408
  795. return
  796. }
  797. // TODO:移动语音识别/目录
  798. br.Ret = 200
  799. br.Success = true
  800. br.Msg = "操作成功"
  801. }