speech_recognition.go 30 KB

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