speech_recognition.go 28 KB

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