speech_recognition.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. speeches := make([]*speech_recognition.SpeechRecognition, 0)
  173. nowTime := time.Now().Local()
  174. for _, v := range req.Files {
  175. t := new(speech_recognition.SpeechRecognition)
  176. t.FileName = v.FileName
  177. t.ResourceUrl = v.ResourceUrl
  178. t.MenuId = req.MenuId
  179. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  180. t.UniqueCode = utils.MD5(fmt.Sprintf("%s_%s", t.TableName(), timestamp))
  181. t.SysUserId = sysUser.AdminId
  182. t.SysUserName = sysUser.RealName
  183. t.State = speech_recognition.SpeechRecognitionStateWait
  184. t.CreateTime = nowTime
  185. t.ModifyTime = nowTime
  186. // CreateMulti拿不到主键, 此处用循环新增获取
  187. if e := t.Create(); e != nil {
  188. br.Msg = "操作失败"
  189. br.ErrMsg = "批量新增转写文件失败, Err: " + e.Error()
  190. return
  191. }
  192. speeches = append(speeches, t)
  193. }
  194. // 批量转写语音
  195. go func() {
  196. services.BatchConvertSpeech(speeches)
  197. }()
  198. br.Ret = 200
  199. br.Success = true
  200. br.Msg = "操作成功"
  201. }
  202. // ConvertList
  203. // @Title 转换列表
  204. // @Description 转换列表
  205. // @Success 200 {object} speech_recognition.SpeechRecognitionItem
  206. // @router /convert_list [get]
  207. func (this *SpeechRecognitionController) ConvertList() {
  208. br := new(models.BaseResponse).Init()
  209. defer func() {
  210. if br.ErrMsg == "" {
  211. br.IsSendEmail = false
  212. }
  213. this.Data["json"] = br
  214. this.ServeJSON()
  215. }()
  216. sysUser := this.SysUser
  217. if sysUser == nil {
  218. br.Msg = "请登录"
  219. br.ErrMsg = "请登录,SysUser Is Empty"
  220. br.Ret = 408
  221. return
  222. }
  223. // 仅取待转换和转换失败的
  224. states := []int{speech_recognition.SpeechRecognitionStateWait, speech_recognition.SpeechRecognitionStateFail}
  225. speechOb := new(speech_recognition.SpeechRecognition)
  226. cond := fmt.Sprintf(` AND %s = ? AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.SysUserId, speech_recognition.SpeechRecognitionCols.State, utils.GetOrmInReplace(len(states)))
  227. pars := make([]interface{}, 0)
  228. pars = append(pars, sysUser.AdminId, states)
  229. list, e := speechOb.GetItemsByCondition(cond, pars, []string{}, "")
  230. if e != nil {
  231. br.Msg = "获取失败"
  232. br.ErrMsg = "获取转写文件列表失败, Err: " + e.Error()
  233. return
  234. }
  235. resp := make([]*speech_recognition.SpeechRecognitionItem, 0)
  236. for _, v := range list {
  237. resp = append(resp, speech_recognition.FormatSpeechRecognition2Item(v))
  238. }
  239. br.Data = resp
  240. br.Ret = 200
  241. br.Success = true
  242. br.Msg = "获取成功"
  243. }
  244. // Save
  245. // @Title 保存内容
  246. // @Description 保存内容
  247. // @Param request body speech_recognition.SpeechRecognitionSaveReq true "type json string"
  248. // @Success 200 string "操作成功"
  249. // @router /save [post]
  250. func (this *SpeechRecognitionController) Save() {
  251. br := new(models.BaseResponse).Init()
  252. defer func() {
  253. if br.ErrMsg == "" {
  254. br.IsSendEmail = false
  255. }
  256. this.Data["json"] = br
  257. this.ServeJSON()
  258. }()
  259. sysUser := this.SysUser
  260. if sysUser == nil {
  261. br.Msg = "请登录"
  262. br.ErrMsg = "请登录,SysUser Is Empty"
  263. br.Ret = 408
  264. return
  265. }
  266. var req speech_recognition.SpeechRecognitionSaveReq
  267. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  268. br.Msg = "参数有误"
  269. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  270. return
  271. }
  272. if req.SpeechRecognitionId <= 0 {
  273. br.Msg = "参数有误"
  274. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  275. return
  276. }
  277. if len(req.Contents) == 0 {
  278. br.Ret = 200
  279. br.Success = true
  280. br.Msg = "操作成功"
  281. return
  282. }
  283. speechOb := new(speech_recognition.SpeechRecognition)
  284. _, e := speechOb.GetItemById(req.SpeechRecognitionId)
  285. if e != nil {
  286. if e.Error() == utils.ErrNoRow() {
  287. br.Msg = "转写文件不存在,请刷新页面"
  288. return
  289. }
  290. br.Msg = "操作失败"
  291. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  292. return
  293. }
  294. // 批量修改内容
  295. contentOb := new(speech_recognition.SpeechRecognitionContent)
  296. if e = contentOb.BatchUpdateContents(req.Contents); e != nil {
  297. br.Msg = "操作失败"
  298. br.ErrMsg = "批量修改内容失败, Err: " + e.Error()
  299. return
  300. }
  301. br.Ret = 200
  302. br.Success = true
  303. br.Msg = "操作成功"
  304. }
  305. // RemoveFile
  306. // @Title (软)删除文件
  307. // @Description (软)删除文件
  308. // @Param request body speech_recognition.SpeechRecognitionRemoveFileReq true "type json string"
  309. // @Success 200 string "操作成功"
  310. // @router /remove_file [post]
  311. func (this *SpeechRecognitionController) RemoveFile() {
  312. br := new(models.BaseResponse).Init()
  313. defer func() {
  314. if br.ErrMsg == "" {
  315. br.IsSendEmail = false
  316. }
  317. this.Data["json"] = br
  318. this.ServeJSON()
  319. }()
  320. sysUser := this.SysUser
  321. if sysUser == nil {
  322. br.Msg = "请登录"
  323. br.ErrMsg = "请登录,SysUser Is Empty"
  324. br.Ret = 408
  325. return
  326. }
  327. var req speech_recognition.SpeechRecognitionRemoveFileReq
  328. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  329. br.Msg = "参数有误"
  330. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  331. return
  332. }
  333. if req.SpeechRecognitionId <= 0 {
  334. br.Msg = "参数有误"
  335. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  336. return
  337. }
  338. speechOb := new(speech_recognition.SpeechRecognition)
  339. speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
  340. if e != nil {
  341. if e.Error() == utils.ErrNoRow() {
  342. br.Msg = "转写文件不存在,请刷新页面"
  343. return
  344. }
  345. br.Msg = "操作失败"
  346. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  347. return
  348. }
  349. speechItem.FileState = speech_recognition.SpeechRecognitionFileRemoveFlag
  350. speechItem.ModifyTime = time.Now().Local()
  351. updateCols := []string{speech_recognition.SpeechRecognitionCols.FileState, speech_recognition.SpeechRecognitionCols.ModifyTime}
  352. if e = speechItem.Update(updateCols); e != nil {
  353. br.Msg = "操作失败"
  354. br.ErrMsg = "软删除转写文件失败, Err: " + e.Error()
  355. return
  356. }
  357. br.Ret = 200
  358. br.Success = true
  359. br.Msg = "操作成功"
  360. }
  361. // Rename
  362. // @Title 重命名
  363. // @Description 重命名
  364. // @Param request body speech_recognition.SpeechRecognitionRenameReq true "type json string"
  365. // @Success 200 string "操作成功"
  366. // @router /rename [post]
  367. func (this *SpeechRecognitionController) Rename() {
  368. br := new(models.BaseResponse).Init()
  369. defer func() {
  370. if br.ErrMsg == "" {
  371. br.IsSendEmail = false
  372. }
  373. this.Data["json"] = br
  374. this.ServeJSON()
  375. }()
  376. sysUser := this.SysUser
  377. if sysUser == nil {
  378. br.Msg = "请登录"
  379. br.ErrMsg = "请登录,SysUser Is Empty"
  380. br.Ret = 408
  381. return
  382. }
  383. var req speech_recognition.SpeechRecognitionRenameReq
  384. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  385. br.Msg = "参数有误"
  386. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  387. return
  388. }
  389. if req.SpeechRecognitionId <= 0 {
  390. br.Msg = "参数有误"
  391. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  392. return
  393. }
  394. req.FileName = strings.TrimSpace(req.FileName)
  395. if req.FileName == "" {
  396. br.Msg = "请输入文件名称"
  397. return
  398. }
  399. speechOb := new(speech_recognition.SpeechRecognition)
  400. speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
  401. if e != nil {
  402. if e.Error() == utils.ErrNoRow() {
  403. br.Msg = "转写文件不存在,请刷新页面"
  404. return
  405. }
  406. br.Msg = "操作失败"
  407. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  408. return
  409. }
  410. // 重名校验
  411. {
  412. cond := fmt.Sprintf(` AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionCols.FileName, speech_recognition.SpeechRecognitionCols.SpeechRecognitionId)
  413. pars := make([]interface{}, 0)
  414. pars = append(pars, req.FileName, req.SpeechRecognitionId)
  415. exists, e := speechOb.GetItemByCondition(cond, pars, "")
  416. if e != nil && e.Error() != utils.ErrNoRow() {
  417. br.Msg = "操作失败"
  418. br.ErrMsg = "获取同名转写文件失败, Err: " + e.Error()
  419. return
  420. }
  421. if exists != nil && exists.SpeechRecognitionId > 0 {
  422. br.Msg = "文件名称已存在,请重新输入"
  423. return
  424. }
  425. }
  426. speechItem.FileName = req.FileName
  427. speechItem.ModifyTime = time.Now().Local()
  428. updateCols := []string{speech_recognition.SpeechRecognitionCols.FileName, speech_recognition.SpeechRecognitionCols.ModifyTime}
  429. if e = speechItem.Update(updateCols); e != nil {
  430. br.Msg = "操作失败"
  431. br.ErrMsg = "转写文件重命名失败, Err: " + e.Error()
  432. return
  433. }
  434. br.Ret = 200
  435. br.Success = true
  436. br.Msg = "操作成功"
  437. }
  438. // Remove
  439. // @Title 删除
  440. // @Description 删除
  441. // @Param request body speech_recognition.SpeechRecognitionRemoveReq true "type json string"
  442. // @Success 200 string "操作成功"
  443. // @router /remove [post]
  444. func (this *SpeechRecognitionController) Remove() {
  445. br := new(models.BaseResponse).Init()
  446. defer func() {
  447. if br.ErrMsg == "" {
  448. br.IsSendEmail = false
  449. }
  450. this.Data["json"] = br
  451. this.ServeJSON()
  452. }()
  453. sysUser := this.SysUser
  454. if sysUser == nil {
  455. br.Msg = "请登录"
  456. br.ErrMsg = "请登录,SysUser Is Empty"
  457. br.Ret = 408
  458. return
  459. }
  460. var req speech_recognition.SpeechRecognitionRemoveReq
  461. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  462. br.Msg = "参数有误"
  463. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  464. return
  465. }
  466. if req.SpeechRecognitionId <= 0 {
  467. br.Msg = "参数有误"
  468. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  469. return
  470. }
  471. speechOb := new(speech_recognition.SpeechRecognition)
  472. speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
  473. if e != nil {
  474. if e.Error() == utils.ErrNoRow() {
  475. br.Ret = 200
  476. br.Success = true
  477. br.Msg = "操作成功"
  478. return
  479. }
  480. br.Msg = "操作失败"
  481. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  482. return
  483. }
  484. if e = speechItem.Del(); e != nil {
  485. br.Msg = "操作失败"
  486. br.ErrMsg = "删除转写文件失败, Err: " + e.Error()
  487. return
  488. }
  489. // 清除关联
  490. go func() {
  491. contentOb := new(speech_recognition.SpeechRecognitionContent)
  492. _ = contentOb.ClearContentBySpeechId(req.SpeechRecognitionId)
  493. mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
  494. _ = mappingOb.ClearMappingBySpeechId(req.SpeechRecognitionId)
  495. }()
  496. br.Ret = 200
  497. br.Success = true
  498. br.Msg = "操作成功"
  499. }
  500. // SaveTag
  501. // @Title 保存标签
  502. // @Description 保存标签
  503. // @Param request body speech_recognition.SpeechRecognitionSaveTagReq true "type json string"
  504. // @Success 200 string "操作成功"
  505. // @router /save_tag [post]
  506. func (this *SpeechRecognitionController) SaveTag() {
  507. br := new(models.BaseResponse).Init()
  508. defer func() {
  509. if br.ErrMsg == "" {
  510. br.IsSendEmail = false
  511. }
  512. this.Data["json"] = br
  513. this.ServeJSON()
  514. }()
  515. sysUser := this.SysUser
  516. if sysUser == nil {
  517. br.Msg = "请登录"
  518. br.ErrMsg = "请登录,SysUser Is Empty"
  519. br.Ret = 408
  520. return
  521. }
  522. var req speech_recognition.SpeechRecognitionSaveTagReq
  523. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  524. br.Msg = "参数有误"
  525. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  526. return
  527. }
  528. if req.SpeechRecognitionId <= 0 {
  529. br.Msg = "参数有误"
  530. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  531. return
  532. }
  533. speechOb := new(speech_recognition.SpeechRecognition)
  534. _, e := speechOb.GetItemById(req.SpeechRecognitionId)
  535. if e != nil {
  536. if e.Error() == utils.ErrNoRow() {
  537. br.Msg = "转写文件不存在,请刷新页面"
  538. return
  539. }
  540. br.Msg = "操作失败"
  541. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  542. return
  543. }
  544. // 清除原标签
  545. mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
  546. if e = mappingOb.ClearMappingBySpeechId(req.SpeechRecognitionId); e != nil {
  547. br.Msg = "操作失败"
  548. br.ErrMsg = "清除转写文件标签失败, Err: " + e.Error()
  549. return
  550. }
  551. // 保存新标签
  552. if len(req.TagIds) > 0 {
  553. mappings := make([]*speech_recognition.SpeechRecognitionTagMapping, 0)
  554. for _, v := range req.TagIds {
  555. if v <= 0 {
  556. continue
  557. }
  558. mappings = append(mappings, &speech_recognition.SpeechRecognitionTagMapping{
  559. TagId: v,
  560. SpeechRecognitionId: req.SpeechRecognitionId,
  561. })
  562. }
  563. if e = mappingOb.CreateMulti(mappings); e != nil {
  564. br.Msg = "操作失败"
  565. br.ErrMsg = "批量新增转写文件标签失败, Err: " + e.Error()
  566. return
  567. }
  568. }
  569. br.Ret = 200
  570. br.Success = true
  571. br.Msg = "操作成功"
  572. }
  573. // List
  574. // @Title 语音识别列表
  575. // @Description 语音识别列表
  576. // @Param FileName query string false "文件名称"
  577. // @Param StartTime query string false "开始时间"
  578. // @Param EndTime query string false "结束时间"
  579. // @Param CreateUserId query int false "创建人ID"
  580. // @Param TagId query int false "标签ID"
  581. // @Param MenuId query int false "目录ID"
  582. // @Success 200 {object} speech_recognition.SpeechRecognitionListResp
  583. // @router /list [get]
  584. func (this *SpeechRecognitionController) List() {
  585. br := new(models.BaseResponse).Init()
  586. defer func() {
  587. if br.ErrMsg == "" {
  588. br.IsSendEmail = false
  589. }
  590. this.Data["json"] = br
  591. this.ServeJSON()
  592. }()
  593. sysUser := this.SysUser
  594. if sysUser == nil {
  595. br.Msg = "请登录"
  596. br.ErrMsg = "请登录,SysUser Is Empty"
  597. br.Ret = 408
  598. return
  599. }
  600. params := new(speech_recognition.SpeechRecognitionListReq)
  601. if e := this.ParseForm(params); e != nil {
  602. br.Msg = "获取失败"
  603. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  604. return
  605. }
  606. params.FileName = strings.TrimSpace(params.FileName)
  607. dataResp := new(speech_recognition.SpeechRecognitionListResp)
  608. cond := ``
  609. pars := make([]interface{}, 0)
  610. // 筛选项
  611. {
  612. if params.FileName != "" {
  613. cond += fmt.Sprintf(` AND %s LIKE ?`, speech_recognition.SpeechRecognitionCols.FileName)
  614. pars = append(pars, fmt.Sprint("%", params.FileName, "%"))
  615. }
  616. if params.StartTime != "" && params.EndTime != "" {
  617. _, e := time.Parse(utils.FormatDate, params.StartTime)
  618. if e != nil {
  619. br.Msg = "开始时间格式有误"
  620. return
  621. }
  622. _, e = time.Parse(utils.FormatDate, params.EndTime)
  623. if e != nil {
  624. br.Msg = "结束时间格式有误"
  625. return
  626. }
  627. st := fmt.Sprintf("%s 00:00:00", params.StartTime)
  628. ed := fmt.Sprintf("%s 23:59:59", params.EndTime)
  629. cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, speech_recognition.SpeechRecognitionCols.CreateTime)
  630. pars = append(pars, st, ed)
  631. }
  632. if params.CreateUserId > 0 {
  633. cond += fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionCols.SysUserId)
  634. pars = append(pars, params.CreateUserId)
  635. }
  636. if params.MenuId > 0 {
  637. cond += fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionCols.MenuId)
  638. pars = append(pars, params.MenuId)
  639. }
  640. // 标签筛选
  641. if params.TagId > 0 {
  642. mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
  643. tagSpeechIds, e := mappingOb.GetSpeechIdsByTagId(params.TagId)
  644. if e != nil {
  645. br.Msg = "获取失败"
  646. br.ErrMsg = "获取标签关联语音识别失败, Err: " + e.Error()
  647. return
  648. }
  649. if len(tagSpeechIds) == 0 {
  650. br.Data = dataResp
  651. br.Ret = 200
  652. br.Success = true
  653. br.Msg = "获取成功"
  654. return
  655. }
  656. cond += fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.SpeechRecognitionId, utils.GetOrmInReplace(len(tagSpeechIds)))
  657. pars = append(pars, tagSpeechIds)
  658. }
  659. }
  660. // 分页列表
  661. speechOb := new(speech_recognition.SpeechRecognition)
  662. total, e := speechOb.GetCountByCondition(cond, pars)
  663. if e != nil {
  664. br.Msg = "获取失败"
  665. br.ErrMsg = "获取语音识别列表总数失败, Err: " + e.Error()
  666. return
  667. }
  668. var startSize int
  669. if params.PageSize <= 0 {
  670. params.PageSize = utils.PageSize20
  671. }
  672. if params.CurrentIndex <= 0 {
  673. params.CurrentIndex = 1
  674. }
  675. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  676. list, e := speechOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, params.PageSize)
  677. if e != nil {
  678. br.Msg = "获取失败"
  679. br.ErrMsg = "获取语音识别列表失败, Err: " + e.Error()
  680. return
  681. }
  682. // 获取标签
  683. speechIds := make([]int, 0)
  684. for _, v := range list {
  685. speechIds = append(speechIds, v.SpeechRecognitionId)
  686. }
  687. mappingTags, e := speech_recognition.GetSpeechRecognitionTagsBySpeechIds(speechIds)
  688. if e != nil {
  689. br.Msg = "获取失败"
  690. br.ErrMsg = "获取语音识别列表标签失败, Err: " + e.Error()
  691. return
  692. }
  693. speechDetailTags := make(map[int][]*speech_recognition.SpeechRecognitionDetailTag)
  694. for _, v := range mappingTags {
  695. if speechDetailTags[v.SpeechRecognitionId] == nil {
  696. speechDetailTags[v.SpeechRecognitionId] = make([]*speech_recognition.SpeechRecognitionDetailTag, 0)
  697. }
  698. speechDetailTags[v.SpeechRecognitionId] = append(speechDetailTags[v.SpeechRecognitionId], &speech_recognition.SpeechRecognitionDetailTag{
  699. TagId: v.TagId,
  700. TagName: v.TagName,
  701. })
  702. }
  703. respList := make([]*speech_recognition.SpeechRecognitionDetailItem, 0)
  704. for _, v := range list {
  705. t := speech_recognition.FormatSpeechRecognition2DetailItem(v, make([]*speech_recognition.SpeechRecognitionContentItem, 0), speechDetailTags[v.SpeechRecognitionId])
  706. respList = append(respList, t)
  707. }
  708. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  709. dataResp.Paging = page
  710. dataResp.List = respList
  711. br.Data = dataResp
  712. br.Ret = 200
  713. br.Success = true
  714. br.Msg = "获取成功"
  715. }
  716. // Detail
  717. // @Title 语音识别详情
  718. // @Description 语音识别详情
  719. // @Param SpeechRecognitionId query int true "语音识别ID"
  720. // @Success 200 {object} speech_recognition.SpeechRecognitionDetailItem
  721. // @router /detail [get]
  722. func (this *SpeechRecognitionController) Detail() {
  723. br := new(models.BaseResponse).Init()
  724. defer func() {
  725. if br.ErrMsg == "" {
  726. br.IsSendEmail = false
  727. }
  728. this.Data["json"] = br
  729. this.ServeJSON()
  730. }()
  731. sysUser := this.SysUser
  732. if sysUser == nil {
  733. br.Msg = "请登录"
  734. br.ErrMsg = "请登录,SysUser Is Empty"
  735. br.Ret = 408
  736. return
  737. }
  738. speechId, _ := this.GetInt("SpeechRecognitionId")
  739. if speechId <= 0 {
  740. br.Msg = "参数有误"
  741. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", speechId)
  742. return
  743. }
  744. speechOb := new(speech_recognition.SpeechRecognition)
  745. speechItem, e := speechOb.GetItemById(speechId)
  746. if e != nil {
  747. if e.Error() == utils.ErrNoRow() {
  748. br.Msg = "转写文件不存在,请刷新页面"
  749. return
  750. }
  751. br.Msg = "获取失败"
  752. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  753. return
  754. }
  755. // 获取内容
  756. contents := make([]*speech_recognition.SpeechRecognitionContentItem, 0)
  757. {
  758. contentOb := new(speech_recognition.SpeechRecognitionContent)
  759. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionContentCols.SpeechRecognitionId)
  760. pars := make([]interface{}, 0)
  761. pars = append(pars, speechId)
  762. list, e := contentOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionContentCols.Sort))
  763. if e != nil {
  764. br.Msg = "获取失败"
  765. br.ErrMsg = "获取语音识别内容失败, Err: " + e.Error()
  766. return
  767. }
  768. for _, v := range list {
  769. if v.Content == "" {
  770. continue
  771. }
  772. contents = append(contents, speech_recognition.FormatSpeechRecognitionContent2Item(v))
  773. }
  774. }
  775. // 获取标签
  776. tags, e := speech_recognition.GetSpeechRecognitionTagBySpeechId(speechId)
  777. if e != nil {
  778. br.Msg = "获取失败"
  779. br.ErrMsg = "获取语音识别标签失败, Err: " + e.Error()
  780. return
  781. }
  782. detail := speech_recognition.FormatSpeechRecognition2DetailItem(speechItem, contents, tags)
  783. br.Data = detail
  784. br.Ret = 200
  785. br.Success = true
  786. br.Msg = "获取成功"
  787. }
  788. // Export
  789. // @Title 导出内容
  790. // @Description 导出内容
  791. // @Param request body speech_recognition.SpeechRecognitionContentExportReq true "type json string"
  792. // @Success 200 string "操作成功"
  793. // @router /export [post]
  794. func (this *SpeechRecognitionController) Export() {
  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. var req speech_recognition.SpeechRecognitionContentExportReq
  811. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  812. br.Msg = "参数有误"
  813. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  814. return
  815. }
  816. if req.SpeechRecognitionId <= 0 {
  817. br.Msg = "参数有误"
  818. br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
  819. return
  820. }
  821. speechOb := new(speech_recognition.SpeechRecognition)
  822. speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
  823. if e != nil {
  824. if e.Error() == utils.ErrNoRow() {
  825. br.Msg = "转写文件不存在,请刷新页面"
  826. return
  827. }
  828. br.Msg = "获取失败"
  829. br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
  830. return
  831. }
  832. contentOb := new(speech_recognition.SpeechRecognitionContent)
  833. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionContentCols.SpeechRecognitionId)
  834. pars := make([]interface{}, 0)
  835. pars = append(pars, req.SpeechRecognitionId)
  836. contents, e := contentOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionContentCols.Sort))
  837. if e != nil {
  838. br.Msg = "导出失败"
  839. br.ErrMsg = "获取语音识别内容失败, Err: " + e.Error()
  840. return
  841. }
  842. if len(contents) == 0 {
  843. br.Msg = "无内容导出"
  844. return
  845. }
  846. if req.ExportType != services.SpeechRecognitionExportTypeTxt && req.ExportType != services.SpeechRecognitionExportTypeDocx && req.ExportType != services.SpeechRecognitionExportTypePdf {
  847. br.Msg = "导出类型有误"
  848. return
  849. }
  850. suffixMap := map[int]string{services.SpeechRecognitionExportTypeTxt: ".txt", services.SpeechRecognitionExportTypeDocx: ".docx", services.SpeechRecognitionExportTypePdf: ".pdf"}
  851. suffix := suffixMap[req.ExportType]
  852. if suffix == "" {
  853. suffix = ".txt"
  854. }
  855. downloadPath, e := services.SpeechRecognitionContentExport(req.ExportType, req.Timestamp, speechItem.FileName, contents)
  856. if e != nil {
  857. br.Msg = "导出文件失败"
  858. br.ErrMsg = "导出语音识别内容文件失败, Err: " + e.Error()
  859. _ = os.Remove(downloadPath)
  860. return
  861. }
  862. defer func() {
  863. _ = os.Remove(downloadPath)
  864. }()
  865. downloadName := fmt.Sprintf("%s%s", speechItem.FileName, suffix)
  866. this.Ctx.Output.Download(downloadPath, downloadName)
  867. }