12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145 |
- package speech_recognition
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/speech_recognition"
- "eta/eta_api/services"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "os"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- type SpeechRecognitionController struct {
- controllers.BaseAuthController
- }
- type SpeechRecognitionCommonController struct {
- controllers.BaseCommonController
- }
- // RecTaskCallback
- // @Title 语音识别回调
- // @Description 语音识别回调
- // @Param request body services.TencentRecTaskCallback true "type json string"
- // @Success 200 string "操作成功"
- // @router /rec_task/callback [post]
- func (this *SpeechRecognitionCommonController) RecTaskCallback() {
- // 此接口返回指定响应体
- br := new(services.TencentRecTaskCallbackResp)
- var errMsg string
- defer func() {
- if errMsg != "" {
- br.Code = 403
- br.Message = "回调失败"
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("语音识别回调失败, ErrMsg: %s", errMsg), 1)
- } else {
- br.Code = 0
- br.Message = "success"
- }
- _ = this.JSON(br, false, false)
- }()
- code, _ := this.GetInt("code", -1)
- requestId, _ := this.GetInt("requestId", 0)
- detail := this.GetString("resultDetail")
- utils.FileLog.Info(fmt.Sprintf("RecTaskCallback, requestId: %d", requestId))
- // 获取taskId对应的API请求及语音识别
- logOb := new(speech_recognition.SpeechRecognitionApiLog)
- cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionApiLogCols.RequestId)
- pars := make([]interface{}, 0)
- pars = append(pars, requestId)
- apiLog, e := logOb.GetItemByCondition(cond, pars, "")
- if e != nil {
- errMsg = "获取API记录失败"
- utils.FileLog.Info("API回调-获取请求记录失败, Err: " + e.Error())
- return
- }
- speechOb := new(speech_recognition.SpeechRecognition)
- speechItem, e := speechOb.GetItemById(apiLog.SpeechRecognitionId)
- if e != nil {
- errMsg = "获取语音识别失败"
- utils.FileLog.Info("获取语音识别失败, Err: " + e.Error())
- return
- }
- // API结果返回有误
- nowTime := time.Now().Local()
- if code != speech_recognition.ApiRequestCodeSuccess {
- convertRemark := speech_recognition.ApiErrMsgMapping[code]
- if convertRemark == "" {
- convertRemark = fmt.Sprintf("未知错误: %d", code)
- }
- speechItem.ConvertRemark = convertRemark
- speechItem.State = speech_recognition.SpeechRecognitionStateFail
- speechItem.ModifyTime = nowTime
- speechCols := []string{speech_recognition.SpeechRecognitionCols.ConvertRemark, speech_recognition.SpeechRecognitionCols.State, speech_recognition.SpeechRecognitionCols.ModifyTime}
- apiLog.RequestCode = code
- apiLog.RequestResult = convertRemark
- apiLog.ModifyTime = nowTime
- apiLogCols := []string{speech_recognition.SpeechRecognitionApiLogCols.RequestCode, speech_recognition.SpeechRecognitionApiLogCols.RequestResult, speech_recognition.SpeechRecognitionApiLogCols.ModifyTime}
- // 更新语音识别及API记录
- if e := speech_recognition.UpdateSpeechAndApiLog(speechItem, speechCols, apiLog, apiLogCols); e != nil {
- errMsg = "更新API返回结果失败"
- utils.FileLog.Info("更新API返回结果失败, Err: " + e.Error())
- }
- return
- }
- // 解析转写段落内容
- sentences := make([]*services.TencentRecTaskSentenceDetail, 0)
- if e := json.Unmarshal([]byte(detail), &sentences); e != nil {
- errMsg = "解析语音识别内容失败"
- utils.FileLog.Info("解析语音识别内容失败, Err: " + e.Error())
- return
- }
- contents := make([]*speech_recognition.SpeechRecognitionContent, 0)
- sorts := 0 // API返回的结果本身是已排过序的
- var abstract string
- var abstractLimit int
- for _, v := range sentences {
- sorts += 1
- t := new(speech_recognition.SpeechRecognitionContent)
- t.SpeechRecognitionId = speechItem.SpeechRecognitionId
- t.Sort = sorts
- t.Content = v.FinalSentence
- t.StartMs = v.StartMs
- t.EndMs = v.EndMs
- t.CreateTime = nowTime
- t.ModifyTime = nowTime
- contents = append(contents, t)
- // 取前几段作为摘要保存
- if abstractLimit < 5 {
- abstractLimit += 1
- abstract += v.FinalSentence
- }
- }
- speechItem.Abstract = abstract
- speechItem.State = speech_recognition.SpeechRecognitionStateSuccess
- speechItem.ModifyTime = nowTime
- speechCols := []string{speech_recognition.SpeechRecognitionCols.Abstract, speech_recognition.SpeechRecognitionCols.State, speech_recognition.SpeechRecognitionCols.ModifyTime}
- apiLog.RequestCode = code
- apiLog.RequestResult = detail
- apiLog.ModifyTime = time.Now().Local()
- apiLogCols := []string{speech_recognition.SpeechRecognitionApiLogCols.RequestCode, speech_recognition.SpeechRecognitionApiLogCols.RequestResult, speech_recognition.SpeechRecognitionApiLogCols.ModifyTime}
- // 新增解析内容并更新语音识别及API记录
- if e := speech_recognition.CreateContentAndUpdateSpeechAndApiLog(contents, speechItem, speechCols, apiLog, apiLogCols); e != nil {
- errMsg = "新增API返回结果失败"
- utils.FileLog.Info("新增API返回结果失败, Err: " + e.Error())
- }
- }
- // Convert
- // @Title 语音转换
- // @Description 语音转换
- // @Param request body speech_recognition.SpeechRecognitionConvertReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /convert [post]
- func (this *SpeechRecognitionController) Convert() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionConvertReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- if req.MenuId <= 0 {
- br.Msg = "请选择目录"
- return
- }
- if len(req.Files) == 0 {
- br.Msg = "请上传转写文件"
- return
- }
- for _, r := range req.Files {
- if r.FileName == "" && r.ResourceUrl == "" {
- br.Msg = "转写文件有误,请检查"
- return
- }
- }
- sortMax, e := services.GetSpeechMenuMaxSort(req.MenuId)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取语音识别目录下最大排序失败, Err: " + e.Error()
- return
- }
- speeches := make([]*speech_recognition.SpeechRecognition, 0)
- nowTime := time.Now().Local()
- for _, v := range req.Files {
- sortMax += 1
- t := new(speech_recognition.SpeechRecognition)
- t.FileName = v.FileName
- t.ResourceUrl = v.ResourceUrl
- t.MenuId = req.MenuId
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- t.UniqueCode = utils.MD5(fmt.Sprintf("%s_%s", t.TableName(), timestamp))
- t.SysUserId = sysUser.AdminId
- t.SysUserName = sysUser.RealName
- t.State = speech_recognition.SpeechRecognitionStateWait
- t.Sort = sortMax
- t.FileSecond = v.FileSecond
- t.FileSize = v.FileSize
- t.CreateTime = nowTime
- t.ModifyTime = nowTime
- // CreateMulti拿不到主键, 此处用循环新增获取
- if e := t.Create(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "批量新增转写文件失败, Err: " + e.Error()
- return
- }
- speeches = append(speeches, t)
- }
- // 批量转写语音
- go func() {
- services.BatchConvertSpeech(speeches)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // ConvertList
- // @Title 转换列表
- // @Description 转换列表
- // @Success 200 {object} speech_recognition.SpeechRecognitionItem
- // @router /convert_list [get]
- func (this *SpeechRecognitionController) ConvertList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- // 仅取待转换和转换失败的
- states := []int{speech_recognition.SpeechRecognitionStateWait, speech_recognition.SpeechRecognitionStateFail}
- speechOb := new(speech_recognition.SpeechRecognition)
- cond := fmt.Sprintf(` AND %s = ? AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.SysUserId, speech_recognition.SpeechRecognitionCols.State, utils.GetOrmInReplace(len(states)))
- pars := make([]interface{}, 0)
- pars = append(pars, sysUser.AdminId, states)
- list, e := speechOb.GetItemsByCondition(cond, pars, []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取转写文件列表失败, Err: " + e.Error()
- return
- }
- resp := make([]*speech_recognition.SpeechRecognitionItem, 0)
- for _, v := range list {
- resp = append(resp, speech_recognition.FormatSpeechRecognition2Item(v))
- }
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // Save
- // @Title 保存内容
- // @Description 保存内容
- // @Param request body speech_recognition.SpeechRecognitionSaveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /save [post]
- func (this *SpeechRecognitionController) Save() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionSaveReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- if req.SpeechRecognitionId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
- return
- }
- if len(req.Contents) == 0 {
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- return
- }
- req.FileName = strings.TrimSpace(req.FileName)
- if req.FileName == "" {
- br.Msg = "请输入文件名称"
- return
- }
- speechOb := new(speech_recognition.SpeechRecognition)
- speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "转写文件不存在,请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
- return
- }
- if req.FileName != speechItem.FileName {
- // 校验重名
- {
- cond := fmt.Sprintf(` AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionCols.FileName, speech_recognition.SpeechRecognitionCols.SpeechRecognitionId)
- pars := make([]interface{}, 0)
- pars = append(pars, req.FileName, req.SpeechRecognitionId)
- exists, e := speechOb.GetItemByCondition(cond, pars, "")
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取同名转写文件失败, Err: " + e.Error()
- return
- }
- if exists != nil && exists.SpeechRecognitionId > 0 {
- br.Msg = "文件名称已存在,请重新输入"
- return
- }
- }
- speechItem.FileName = req.FileName
- speechItem.ModifyTime = time.Now().Local()
- updateCols := []string{speech_recognition.SpeechRecognitionCols.FileName, speech_recognition.SpeechRecognitionCols.ModifyTime}
- if e = speechItem.Update(updateCols); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新转写文件名称失败, Err: " + e.Error()
- return
- }
- }
- // 批量修改内容
- contentOb := new(speech_recognition.SpeechRecognitionContent)
- if e = contentOb.BatchUpdateContents(req.Contents); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "批量修改内容失败, Err: " + e.Error()
- return
- }
- // 更新标签
- mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
- if e = mappingOb.ClearMappingBySpeechId(req.SpeechRecognitionId); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "清除语音识别标签失败, Err: " + e.Error()
- return
- }
- tagMappings := make([]*speech_recognition.SpeechRecognitionTagMapping, 0)
- for _, v := range req.TagIds {
- tagMappings = append(tagMappings, &speech_recognition.SpeechRecognitionTagMapping{
- SpeechRecognitionId: req.SpeechRecognitionId,
- TagId: v,
- })
- }
- if e = mappingOb.CreateMulti(tagMappings); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "批量新增语音识别标签失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // RemoveFile
- // @Title (软)删除文件
- // @Description (软)删除文件
- // @Param request body speech_recognition.SpeechRecognitionRemoveFileReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /remove_file [post]
- func (this *SpeechRecognitionController) RemoveFile() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionRemoveFileReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- if req.SpeechRecognitionId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
- return
- }
- speechOb := new(speech_recognition.SpeechRecognition)
- speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "转写文件不存在,请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
- return
- }
- speechItem.FileState = speech_recognition.SpeechRecognitionFileRemoveFlag
- speechItem.ModifyTime = time.Now().Local()
- updateCols := []string{speech_recognition.SpeechRecognitionCols.FileState, speech_recognition.SpeechRecognitionCols.ModifyTime}
- if e = speechItem.Update(updateCols); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "软删除转写文件失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Rename
- // @Title 重命名
- // @Description 重命名
- // @Param request body speech_recognition.SpeechRecognitionRenameReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /rename [post]
- func (this *SpeechRecognitionController) Rename() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionRenameReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- if req.SpeechRecognitionId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
- return
- }
- req.FileName = strings.TrimSpace(req.FileName)
- if req.FileName == "" {
- br.Msg = "请输入文件名称"
- return
- }
- speechOb := new(speech_recognition.SpeechRecognition)
- speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "转写文件不存在,请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
- return
- }
- // 重名校验
- {
- cond := fmt.Sprintf(` AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionCols.FileName, speech_recognition.SpeechRecognitionCols.SpeechRecognitionId)
- pars := make([]interface{}, 0)
- pars = append(pars, req.FileName, req.SpeechRecognitionId)
- exists, e := speechOb.GetItemByCondition(cond, pars, "")
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取同名转写文件失败, Err: " + e.Error()
- return
- }
- if exists != nil && exists.SpeechRecognitionId > 0 {
- br.Msg = "文件名称已存在,请重新输入"
- return
- }
- }
- speechItem.FileName = req.FileName
- speechItem.ModifyTime = time.Now().Local()
- updateCols := []string{speech_recognition.SpeechRecognitionCols.FileName, speech_recognition.SpeechRecognitionCols.ModifyTime}
- if e = speechItem.Update(updateCols); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "转写文件重命名失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Remove
- // @Title 删除
- // @Description 删除
- // @Param request body speech_recognition.SpeechRecognitionRemoveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /remove [post]
- func (this *SpeechRecognitionController) Remove() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionRemoveReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- if req.SpeechRecognitionId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
- return
- }
- speechOb := new(speech_recognition.SpeechRecognition)
- speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
- return
- }
- if e = speechItem.Del(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "删除转写文件失败, Err: " + e.Error()
- return
- }
- // 清除关联
- go func() {
- contentOb := new(speech_recognition.SpeechRecognitionContent)
- _ = contentOb.ClearContentBySpeechId(req.SpeechRecognitionId)
- mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
- _ = mappingOb.ClearMappingBySpeechId(req.SpeechRecognitionId)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // SaveTag
- // @Title 保存标签
- // @Description 保存标签
- // @Param request body speech_recognition.SpeechRecognitionSaveTagReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /save_tag [post]
- func (this *SpeechRecognitionController) SaveTag() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionSaveTagReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- if req.SpeechRecognitionId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
- return
- }
- speechOb := new(speech_recognition.SpeechRecognition)
- _, e := speechOb.GetItemById(req.SpeechRecognitionId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "转写文件不存在,请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
- return
- }
- // 清除原标签
- mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
- if e = mappingOb.ClearMappingBySpeechId(req.SpeechRecognitionId); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "清除转写文件标签失败, Err: " + e.Error()
- return
- }
- // 保存新标签
- if len(req.TagIds) > 0 {
- mappings := make([]*speech_recognition.SpeechRecognitionTagMapping, 0)
- for _, v := range req.TagIds {
- if v <= 0 {
- continue
- }
- mappings = append(mappings, &speech_recognition.SpeechRecognitionTagMapping{
- TagId: v,
- SpeechRecognitionId: req.SpeechRecognitionId,
- })
- }
- if e = mappingOb.CreateMulti(mappings); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "批量新增转写文件标签失败, Err: " + e.Error()
- return
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // List
- // @Title 语音识别列表
- // @Description 语音识别列表
- // @Param FileName query string false "文件名称"
- // @Param StartTime query string false "开始时间"
- // @Param EndTime query string false "结束时间"
- // @Param CreateUserIds query string false "创建人ID"
- // @Param TagId query int false "标签ID"
- // @Param TagIds query string false "标签ID"
- // @Param MenuId query int false "目录ID"
- // @Param IsTagMenu query bool false "是否为标签目录"
- // @Success 200 {object} speech_recognition.SpeechRecognitionListResp
- // @router /list [get]
- func (this *SpeechRecognitionController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- params := new(speech_recognition.SpeechRecognitionListReq)
- if e := this.ParseForm(params); e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- params.FileName = strings.TrimSpace(params.FileName)
- dataResp := new(speech_recognition.SpeechRecognitionListResp)
- cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionCols.State)
- pars := make([]interface{}, 0)
- pars = append(pars, speech_recognition.SpeechRecognitionStateSuccess)
- // 筛选项
- {
- if params.FileName != "" {
- cond += fmt.Sprintf(` AND %s LIKE ?`, speech_recognition.SpeechRecognitionCols.FileName)
- pars = append(pars, fmt.Sprint("%", params.FileName, "%"))
- }
- if params.StartTime != "" && params.EndTime != "" {
- _, e := time.Parse(utils.FormatDate, params.StartTime)
- if e != nil {
- br.Msg = "开始时间格式有误"
- return
- }
- _, e = time.Parse(utils.FormatDate, params.EndTime)
- if e != nil {
- br.Msg = "结束时间格式有误"
- return
- }
- st := fmt.Sprintf("%s 00:00:00", params.StartTime)
- ed := fmt.Sprintf("%s 23:59:59", params.EndTime)
- cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, speech_recognition.SpeechRecognitionCols.CreateTime)
- pars = append(pars, st, ed)
- }
- if params.CreateUserId != "" {
- userArr := strings.Split(strings.TrimSpace(params.CreateUserId), ",")
- if len(userArr) > 0 {
- userIds := make([]int, 0)
- for _, v := range userArr {
- t, _ := strconv.Atoi(v)
- userIds = append(userIds, t)
- }
- if len(userIds) == 0 {
- br.Data = dataResp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- return
- }
- cond += fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.SysUserId, utils.GetOrmInReplace(len(userIds)))
- pars = append(pars, userIds)
- }
- }
- // 语音识别目录-筛选子目录集合
- if params.MenuId > 0 && !params.IsTagMenu {
- {
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- menus, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取语音识别目录列表失败, Err: " + e.Error()
- return
- }
- childIds := services.GetSpeechRecognitionMenuChildrenRecursive(menus, params.MenuId)
- menuIds := make([]int, 0)
- menuIds = append(menuIds, params.MenuId)
- if len(childIds) > 0 {
- menuIds = append(menuIds, childIds...)
- }
- cond += fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.MenuId, utils.GetOrmInReplace(len(menuIds)))
- pars = append(pars, menuIds)
- }
- }
- // 标签目录-筛选目录下所有标签
- tagIds := make([]int, 0)
- if params.MenuId > 0 && params.IsTagMenu {
- {
- menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
- menus, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取标签目录列表失败, Err: " + e.Error()
- return
- }
- childIds := services.GetSpeechRecognitionTagMenuChildrenRecursive(menus, params.MenuId)
- menuIds := make([]int, 0)
- menuIds = append(menuIds, params.MenuId)
- if len(childIds) > 0 {
- menuIds = append(menuIds, childIds...)
- }
- // 获取目录下所有标签
- tagOb := new(speech_recognition.SpeechRecognitionTag)
- ids, e := tagOb.GetTagIdsByMenuIds(menuIds)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "通过目录IDs获取标签IDs失败, Err: " + e.Error()
- return
- }
- // 此处查询无结果直接返回
- if len(ids) == 0 {
- br.Data = dataResp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- return
- }
- tagIds = ids
- }
- }
- // 标签筛选
- if params.TagId > 0 && params.TagIds == "" {
- tagIds = append(tagIds, params.TagId)
- }
- if params.TagId <= 0 && params.TagIds != "" {
- tagArr := strings.Split(params.TagIds, ",")
- if len(tagArr) > 0 {
- for _, v := range tagArr {
- t, _ := strconv.Atoi(v)
- tagIds = append(tagIds, t)
- }
- }
- }
- if len(tagIds) > 0 {
- mappingOb := new(speech_recognition.SpeechRecognitionTagMapping)
- tagSpeechIds, e := mappingOb.GetSpeechIdsByTagIds(tagIds)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取标签关联语音识别失败, Err: " + e.Error()
- return
- }
- if len(tagSpeechIds) == 0 {
- br.Data = dataResp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- return
- }
- cond += fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.SpeechRecognitionId, utils.GetOrmInReplace(len(tagSpeechIds)))
- pars = append(pars, tagSpeechIds)
- }
- }
- // 分页列表
- speechOb := new(speech_recognition.SpeechRecognition)
- total, e := speechOb.GetCountByCondition(cond, pars)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取语音识别列表总数失败, Err: " + e.Error()
- return
- }
- var startSize int
- if params.PageSize <= 0 {
- params.PageSize = utils.PageSize20
- }
- if params.CurrentIndex <= 0 {
- params.CurrentIndex = 1
- }
- startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
- list, e := speechOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, params.PageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取语音识别列表失败, Err: " + e.Error()
- return
- }
- // 获取标签
- speechIds := make([]int, 0)
- for _, v := range list {
- speechIds = append(speechIds, v.SpeechRecognitionId)
- }
- mappingTags, e := speech_recognition.GetSpeechRecognitionTagsBySpeechIds(speechIds)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取语音识别列表标签失败, Err: " + e.Error()
- return
- }
- speechDetailTags := make(map[int][]*speech_recognition.SpeechRecognitionDetailTag)
- for _, v := range mappingTags {
- if speechDetailTags[v.SpeechRecognitionId] == nil {
- speechDetailTags[v.SpeechRecognitionId] = make([]*speech_recognition.SpeechRecognitionDetailTag, 0)
- }
- speechDetailTags[v.SpeechRecognitionId] = append(speechDetailTags[v.SpeechRecognitionId], &speech_recognition.SpeechRecognitionDetailTag{
- TagId: v.TagId,
- TagName: v.TagName,
- })
- }
- respList := make([]*speech_recognition.SpeechRecognitionDetailItem, 0)
- for _, v := range list {
- t := speech_recognition.FormatSpeechRecognition2DetailItem(v, make([]*speech_recognition.SpeechRecognitionContentItem, 0), speechDetailTags[v.SpeechRecognitionId], make([]*speech_recognition.SpeechRecognitionMenuItem, 0))
- respList = append(respList, t)
- }
- page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
- dataResp.Paging = page
- dataResp.List = respList
- br.Data = dataResp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // Detail
- // @Title 语音识别详情
- // @Description 语音识别详情
- // @Param SpeechRecognitionId query int true "语音识别ID"
- // @Success 200 {object} speech_recognition.SpeechRecognitionDetailItem
- // @router /detail [get]
- func (this *SpeechRecognitionController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- speechId, _ := this.GetInt("SpeechRecognitionId")
- if speechId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", speechId)
- return
- }
- speechOb := new(speech_recognition.SpeechRecognition)
- speechItem, e := speechOb.GetItemById(speechId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "转写文件不存在,请刷新页面"
- return
- }
- br.Msg = "获取失败"
- br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
- return
- }
- // 获取内容
- contents := make([]*speech_recognition.SpeechRecognitionContentItem, 0)
- {
- contentOb := new(speech_recognition.SpeechRecognitionContent)
- cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionContentCols.SpeechRecognitionId)
- pars := make([]interface{}, 0)
- pars = append(pars, speechId)
- list, e := contentOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionContentCols.Sort))
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取语音识别内容失败, Err: " + e.Error()
- return
- }
- for _, v := range list {
- if v.Content == "" {
- continue
- }
- contents = append(contents, speech_recognition.FormatSpeechRecognitionContent2Item(v))
- }
- }
- // 跟踪目录路径
- menuPath := make([]*speech_recognition.SpeechRecognitionMenuItem, 0)
- {
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- menus, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, fmt.Sprintf("%s ASC, %s ASC", speech_recognition.SpeechRecognitionMenuCols.ParentId, speech_recognition.SpeechRecognitionMenuCols.Sort))
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取目录列表失败, Err: " + e.Error()
- return
- }
- menuPath = services.GetSpeechRecognitionMenuPathRecursive(menus, speechItem.MenuId)
- sort.Slice(menuPath, func(i, j int) bool {
- return menuPath[i].Level < menuPath[j].Level
- })
- }
- // 获取标签
- tags, e := speech_recognition.GetSpeechRecognitionTagBySpeechId(speechId)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取语音识别标签失败, Err: " + e.Error()
- return
- }
- detail := speech_recognition.FormatSpeechRecognition2DetailItem(speechItem, contents, tags, menuPath)
- br.Data = detail
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // Export
- // @Title 导出内容
- // @Description 导出内容
- // @Param request body speech_recognition.SpeechRecognitionContentExportReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /export [get]
- func (this *SpeechRecognitionController) Export() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- req := new(speech_recognition.SpeechRecognitionContentExportReq)
- if e := this.ParseForm(req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- if req.SpeechRecognitionId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, SpeechRecognitionId: %d", req.SpeechRecognitionId)
- return
- }
- speechOb := new(speech_recognition.SpeechRecognition)
- speechItem, e := speechOb.GetItemById(req.SpeechRecognitionId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "转写文件不存在,请刷新页面"
- return
- }
- br.Msg = "获取失败"
- br.ErrMsg = "获取转写文件失败, Err: " + e.Error()
- return
- }
- contentOb := new(speech_recognition.SpeechRecognitionContent)
- cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionContentCols.SpeechRecognitionId)
- pars := make([]interface{}, 0)
- pars = append(pars, req.SpeechRecognitionId)
- contents, e := contentOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionContentCols.Sort))
- if e != nil {
- br.Msg = "导出失败"
- br.ErrMsg = "获取语音识别内容失败, Err: " + e.Error()
- return
- }
- if len(contents) == 0 {
- br.Msg = "无内容导出"
- return
- }
- if req.ExportType != services.SpeechRecognitionExportTypeTxt && req.ExportType != services.SpeechRecognitionExportTypeDocx && req.ExportType != services.SpeechRecognitionExportTypePdf {
- br.Msg = "导出类型有误"
- return
- }
- suffixMap := map[int]string{services.SpeechRecognitionExportTypeTxt: ".txt", services.SpeechRecognitionExportTypeDocx: ".docx", services.SpeechRecognitionExportTypePdf: ".pdf"}
- suffix := suffixMap[req.ExportType]
- if suffix == "" {
- suffix = ".txt"
- }
- downloadPath, e := services.SpeechRecognitionContentExport(req.ExportType, req.Timestamp, speechItem.FileName, contents)
- if e != nil {
- br.Msg = "导出文件失败"
- br.ErrMsg = "导出语音识别内容文件失败, Err: " + e.Error()
- _ = os.Remove(downloadPath)
- return
- }
- defer func() {
- _ = os.Remove(downloadPath)
- }()
- downloadName := fmt.Sprintf("%s%s", speechItem.FileName, suffix)
- this.Ctx.Output.Download(downloadPath, downloadName)
- }
- // CheckFileName
- // @Title 文件重名校验
- // @Description 文件重名校验
- // @Param request body speech_recognition.SpeechRecognitionConvertCheckNameReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /convert/check_name [post]
- func (this *SpeechRecognitionController) CheckFileName() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionConvertCheckNameReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- req.FileName = strings.TrimSpace(req.FileName)
- if req.FileName == "" {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, FileName: %s", req.FileName)
- return
- }
- var checkResult bool
- speechOb := new(speech_recognition.SpeechRecognition)
- cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionCols.FileName)
- pars := make([]interface{}, 0)
- pars = append(pars, req.FileName)
- exists, e := speechOb.GetItemByCondition(cond, pars, "")
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取同名转写文件失败, Err: " + e.Error()
- return
- }
- if exists != nil && exists.SpeechRecognitionId > 0 {
- checkResult = true
- return
- }
- br.Data = checkResult
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
|