speech_recognition.go 28 KB

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