speech_recognition.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. // 语音识别目录-筛选子目录集合
  645. if params.MenuId > 0 && !params.IsTagMenu {
  646. {
  647. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  648. menus, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  649. if e != nil {
  650. br.Msg = "获取失败"
  651. br.ErrMsg = "获取语音识别目录列表失败, Err: " + e.Error()
  652. return
  653. }
  654. childIds := services.GetSpeechRecognitionMenuChildrenRecursive(menus, params.MenuId)
  655. menuIds := make([]int, 0)
  656. menuIds = append(menuIds, params.MenuId)
  657. if len(childIds) > 0 {
  658. menuIds = append(menuIds, childIds...)
  659. }
  660. cond += fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.MenuId, utils.GetOrmInReplace(len(menuIds)))
  661. pars = append(pars, menuIds)
  662. }
  663. }
  664. // 标签目录-筛选目录下所有标签
  665. tagIds := make([]int, 0)
  666. if params.MenuId > 0 && params.IsTagMenu {
  667. {
  668. menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
  669. menus, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  670. if e != nil {
  671. br.Msg = "获取失败"
  672. br.ErrMsg = "获取标签目录列表失败, Err: " + e.Error()
  673. return
  674. }
  675. childIds := services.GetSpeechRecognitionTagMenuChildrenRecursive(menus, params.MenuId)
  676. menuIds := make([]int, 0)
  677. menuIds = append(menuIds, params.MenuId)
  678. if len(childIds) > 0 {
  679. menuIds = append(menuIds, childIds...)
  680. }
  681. // 获取目录下所有标签
  682. tagOb := new(speech_recognition.SpeechRecognitionTag)
  683. ids, e := tagOb.GetTagIdsByMenuIds(menuIds)
  684. if e != nil {
  685. br.Msg = "获取失败"
  686. br.ErrMsg = "通过目录IDs获取标签IDs失败, Err: " + e.Error()
  687. return
  688. }
  689. // 此处查询无结果直接返回
  690. if len(ids) == 0 {
  691. br.Data = dataResp
  692. br.Ret = 200
  693. br.Success = true
  694. br.Msg = "获取成功"
  695. return
  696. }
  697. tagIds = ids
  698. }
  699. }
  700. // 标签筛选
  701. if params.TagId > 0 && params.TagIds == "" {
  702. tagIds = append(tagIds, params.TagId)
  703. }
  704. if params.TagId <= 0 && params.TagIds != "" {
  705. tagArr := strings.Split(params.TagIds, ",")
  706. if len(tagArr) > 0 {
  707. for _, v := range tagArr {
  708. t, _ := strconv.Atoi(v)
  709. tagIds = append(tagIds, t)
  710. }
  711. }
  712. }
  713. if len(tagIds) > 0 {
  714. mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
  715. tagSpeechIds, e := mappingOb.GetSpeechIdsByTagIds(tagIds)
  716. if e != nil {
  717. br.Msg = "获取失败"
  718. br.ErrMsg = "获取标签关联语音识别失败, Err: " + e.Error()
  719. return
  720. }
  721. if len(tagSpeechIds) == 0 {
  722. br.Data = dataResp
  723. br.Ret = 200
  724. br.Success = true
  725. br.Msg = "获取成功"
  726. return
  727. }
  728. cond += fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.SpeechRecognitionId, utils.GetOrmInReplace(len(tagSpeechIds)))
  729. pars = append(pars, tagSpeechIds)
  730. }
  731. }
  732. // 分页列表
  733. speechOb := new(speech_recognition.SpeechRecognition)
  734. total, e := speechOb.GetCountByCondition(cond, pars)
  735. if e != nil {
  736. br.Msg = "获取失败"
  737. br.ErrMsg = "获取语音识别列表总数失败, Err: " + e.Error()
  738. return
  739. }
  740. var startSize int
  741. if params.PageSize <= 0 {
  742. params.PageSize = utils.PageSize20
  743. }
  744. if params.CurrentIndex <= 0 {
  745. params.CurrentIndex = 1
  746. }
  747. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  748. list, e := speechOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, params.PageSize)
  749. if e != nil {
  750. br.Msg = "获取失败"
  751. br.ErrMsg = "获取语音识别列表失败, Err: " + e.Error()
  752. return
  753. }
  754. // 获取标签
  755. speechIds := make([]int, 0)
  756. for _, v := range list {
  757. speechIds = append(speechIds, v.SpeechRecognitionId)
  758. }
  759. mappingTags, e := speech_recognition.GetSpeechRecognitionTagsBySpeechIds(speechIds)
  760. if e != nil {
  761. br.Msg = "获取失败"
  762. br.ErrMsg = "获取语音识别列表标签失败, Err: " + e.Error()
  763. return
  764. }
  765. speechDetailTags := make(map[int][]*speech_recognition.SpeechRecognitionDetailTag)
  766. for _, v := range mappingTags {
  767. if speechDetailTags[v.SpeechRecognitionId] == nil {
  768. speechDetailTags[v.SpeechRecognitionId] = make([]*speech_recognition.SpeechRecognitionDetailTag, 0)
  769. }
  770. speechDetailTags[v.SpeechRecognitionId] = append(speechDetailTags[v.SpeechRecognitionId], &speech_recognition.SpeechRecognitionDetailTag{
  771. TagId: v.TagId,
  772. TagName: v.TagName,
  773. })
  774. }
  775. respList := make([]*speech_recognition.SpeechRecognitionDetailItem, 0)
  776. for _, v := range list {
  777. t := speech_recognition.FormatSpeechRecognition2DetailItem(v, make([]*speech_recognition.SpeechRecognitionContentItem, 0), speechDetailTags[v.SpeechRecognitionId])
  778. respList = append(respList, t)
  779. }
  780. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  781. dataResp.Paging = page
  782. dataResp.List = respList
  783. br.Data = dataResp
  784. br.Ret = 200
  785. br.Success = true
  786. br.Msg = "获取成功"
  787. }
  788. // Detail
  789. // @Title 语音识别详情
  790. // @Description 语音识别详情
  791. // @Param SpeechRecognitionId query int true "语音识别ID"
  792. // @Success 200 {object} speech_recognition.SpeechRecognitionDetailItem
  793. // @router /detail [get]
  794. func (this *SpeechRecognitionController) Detail() {
  795. br := new(models.BaseResponse).Init()
  796. defer func() {
  797. if br.ErrMsg == "" {
  798. br.IsSendEmail = false
  799. }
  800. this.Data["json"] = br
  801. this.ServeJSON()
  802. }()
  803. sysUser := this.SysUser
  804. if sysUser == nil {
  805. br.Msg = "请登录"
  806. br.ErrMsg = "请登录,SysUser Is Empty"
  807. br.Ret = 408
  808. return
  809. }
  810. speechId, _ := this.GetInt("SpeechRecognitionId")
  811. if speechId <= 0 {
  812. br.Msg = "参数有误"
  813. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", speechId)
  814. return
  815. }
  816. speechOb := new(speech_recognition.SpeechRecognition)
  817. speechItem, e := speechOb.GetItemById(speechId)
  818. if e != nil {
  819. if e.Error() == utils.ErrNoRow() {
  820. br.Msg = "转写文件不存在,请刷新页面"
  821. return
  822. }
  823. br.Msg = "获取失败"
  824. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  825. return
  826. }
  827. // 获取内容
  828. contents := make([]*speech_recognition.SpeechRecognitionContentItem, 0)
  829. {
  830. contentOb := new(speech_recognition.SpeechRecognitionContent)
  831. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionContentCols.SpeechRecognitionId)
  832. pars := make([]interface{}, 0)
  833. pars = append(pars, speechId)
  834. list, e := contentOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionContentCols.Sort))
  835. if e != nil {
  836. br.Msg = "获取失败"
  837. br.ErrMsg = "获取语音识别内容失败, Err: " + e.Error()
  838. return
  839. }
  840. for _, v := range list {
  841. if v.Content == "" {
  842. continue
  843. }
  844. contents = append(contents, speech_recognition.FormatSpeechRecognitionContent2Item(v))
  845. }
  846. }
  847. // 获取标签
  848. tags, e := speech_recognition.GetSpeechRecognitionTagBySpeechId(speechId)
  849. if e != nil {
  850. br.Msg = "获取失败"
  851. br.ErrMsg = "获取语音识别标签失败, Err: " + e.Error()
  852. return
  853. }
  854. detail := speech_recognition.FormatSpeechRecognition2DetailItem(speechItem, contents, tags)
  855. br.Data = detail
  856. br.Ret = 200
  857. br.Success = true
  858. br.Msg = "获取成功"
  859. }
  860. // Export
  861. // @Title 导出内容
  862. // @Description 导出内容
  863. // @Param request body speech_recognition.SpeechRecognitionContentExportReq true "type json string"
  864. // @Success 200 string "操作成功"
  865. // @router /export [post]
  866. func (this *SpeechRecognitionController) Export() {
  867. br := new(models.BaseResponse).Init()
  868. defer func() {
  869. if br.ErrMsg == "" {
  870. br.IsSendEmail = false
  871. }
  872. this.Data["json"] = br
  873. this.ServeJSON()
  874. }()
  875. sysUser := this.SysUser
  876. if sysUser == nil {
  877. br.Msg = "请登录"
  878. br.ErrMsg = "请登录,SysUser Is Empty"
  879. br.Ret = 408
  880. return
  881. }
  882. var req speech_recognition.SpeechRecognitionContentExportReq
  883. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  884. br.Msg = "参数有误"
  885. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  886. return
  887. }
  888. if req.SpeechRecognitionId <= 0 {
  889. br.Msg = "参数有误"
  890. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  891. return
  892. }
  893. speechOb := new(speech_recognition.SpeechRecognition)
  894. speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
  895. if e != nil {
  896. if e.Error() == utils.ErrNoRow() {
  897. br.Msg = "转写文件不存在,请刷新页面"
  898. return
  899. }
  900. br.Msg = "获取失败"
  901. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  902. return
  903. }
  904. contentOb := new(speech_recognition.SpeechRecognitionContent)
  905. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionContentCols.SpeechRecognitionId)
  906. pars := make([]interface{}, 0)
  907. pars = append(pars, req.SpeechRecognitionId)
  908. contents, e := contentOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionContentCols.Sort))
  909. if e != nil {
  910. br.Msg = "导出失败"
  911. br.ErrMsg = "获取语音识别内容失败, Err: " + e.Error()
  912. return
  913. }
  914. if len(contents) == 0 {
  915. br.Msg = "无内容导出"
  916. return
  917. }
  918. if req.ExportType != services.SpeechRecognitionExportTypeTxt && req.ExportType != services.SpeechRecognitionExportTypeDocx && req.ExportType != services.SpeechRecognitionExportTypePdf {
  919. br.Msg = "导出类型有误"
  920. return
  921. }
  922. suffixMap := map[int]string{services.SpeechRecognitionExportTypeTxt: ".txt", services.SpeechRecognitionExportTypeDocx: ".docx", services.SpeechRecognitionExportTypePdf: ".pdf"}
  923. suffix := suffixMap[req.ExportType]
  924. if suffix == "" {
  925. suffix = ".txt"
  926. }
  927. downloadPath, e := services.SpeechRecognitionContentExport(req.ExportType, req.Timestamp, speechItem.FileName, contents)
  928. if e != nil {
  929. br.Msg = "导出文件失败"
  930. br.ErrMsg = "导出语音识别内容文件失败, Err: " + e.Error()
  931. _ = os.Remove(downloadPath)
  932. return
  933. }
  934. defer func() {
  935. _ = os.Remove(downloadPath)
  936. }()
  937. downloadName := fmt.Sprintf("%s%s", speechItem.FileName, suffix)
  938. this.Ctx.Output.Download(downloadPath, downloadName)
  939. }