12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262 |
- package services
- import (
- "bufio"
- "errors"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/models/speech_recognition"
- "eta_gn/eta_api/services/alarm_msg"
- "eta_gn/eta_api/utils"
- "fmt"
- "os"
- "strconv"
- "sync"
- "time"
- "baliance.com/gooxml/document"
- "baliance.com/gooxml/measurement"
- "baliance.com/gooxml/schema/soo/wml"
- "github.com/jung-kurt/gofpdf"
- )
- const (
- SpeechRecognitionExportTypeTxt = 1
- SpeechRecognitionExportTypeDocx = 2
- SpeechRecognitionExportTypePdf = 3
- SpeechMenuCheckRemoveTypePass = 0 // 目录删除校验-可删除
- SpeechMenuCheckRemoveTypeRefused = 1 // 目录删除校验-不可删除
- SpeechMenuCheckRemoveTypeWarning = 2 // 目录删除校验-警告
- )
- func GetSpeechRecognitionMenuTreeRecursive(list []*speech_recognition.SpeechRecognitionMenu, parentId int) []*speech_recognition.SpeechRecognitionMenuItem {
- res := make([]*speech_recognition.SpeechRecognitionMenuItem, 0)
- for _, v := range list {
- if v.ParentId == parentId {
- t := new(speech_recognition.SpeechRecognitionMenuItem)
- t.UniqueCode = v.UniqueCode
- t.MenuId = v.SpeechRecognitionMenuId
- t.MenuName = v.MenuName
- t.ParentId = v.ParentId
- t.Level = v.Level
- t.Sort = v.Sort
- t.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
- t.Children = GetSpeechRecognitionMenuTreeRecursive(list, v.SpeechRecognitionMenuId)
- res = append(res, t)
- }
- }
- return res
- }
- func GetSpeechRecognitionTagMenuTreeRecursive(list []*speech_recognition.SpeechRecognitionTagMenu, parentId int) []*speech_recognition.SpeechRecognitionTagMenuItem {
- res := make([]*speech_recognition.SpeechRecognitionTagMenuItem, 0)
- for _, v := range list {
- if v.ParentId == parentId {
- t := new(speech_recognition.SpeechRecognitionTagMenuItem)
- t.UniqueCode = v.UniqueCode
- t.MenuId = v.SpeechRecognitionTagMenuId
- t.MenuName = v.MenuName
- t.ParentId = v.ParentId
- t.Level = v.Level
- t.Sort = v.Sort
- t.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
- t.Children = GetSpeechRecognitionTagMenuTreeRecursive(list, v.SpeechRecognitionTagMenuId)
- res = append(res, t)
- }
- }
- return res
- }
- func BatchConvertSpeech(speeches []*speech_recognition.SpeechRecognition) {
- var err error
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("批量转写语音失败, ErrMsg: %s", err.Error())
- utils.FileLog.Info(tips)
- go alarm_msg.SendAlarmMsg(tips, 1)
- }
- }()
- conf, e := models.GetBusinessConf()
- if e != nil {
- err = fmt.Errorf("获取配置失败, Err: %s", e.Error())
- return
- }
- if conf[models.BusinessConfTencentApiSecretId] == "" || conf[models.BusinessConfTencentApiSecretKey] == "" || conf[models.BusinessConfTencentApiRecTaskCallbackUrl] == "" {
- err = fmt.Errorf("API配置有误, SecretId: %s, SecretKey: %s, Callback: %s", conf[models.BusinessConfTencentApiSecretId], conf[models.BusinessConfTencentApiSecretKey], conf[models.BusinessConfTencentApiRecTaskCallbackUrl])
- return
- }
- apiLimit := make(chan struct{}, 20)
- var wg sync.WaitGroup
- for _, v := range speeches {
- wg.Add(1)
- go func(speech *speech_recognition.SpeechRecognition) {
- defer func() {
- wg.Done()
- <-apiLimit
- }()
- apiLimit <- struct{}{}
- var errMsg string
- var r TencentRecTaskReq
- r.FileUrl = speech.ResourceUrl
- r.SecretId = conf[models.BusinessConfTencentApiSecretId]
- r.SecretKey = conf[models.BusinessConfTencentApiSecretKey]
- r.CallbackUrl = conf[models.BusinessConfTencentApiRecTaskCallbackUrl]
- taskId, e := TencentCreateRecTask(r)
- if e != nil {
- errMsg = "创建语音识别任务失败"
- utils.FileLog.Info("TencentCreateRecTask创建语音识别任务失败, ErrMsg: %s", e.Error())
- }
- if errMsg == "" {
- apiLog := new(speech_recognition.SpeechRecognitionApiLog)
- apiLog.SpeechRecognitionId = speech.SpeechRecognitionId
- apiLog.RequestId = strconv.Itoa(taskId)
- apiLog.RequestCode = -1
- apiLog.CreateTime = time.Now().Local()
- apiLog.ModifyTime = time.Now().Local()
- if e = apiLog.Create(); e != nil {
- errMsg = "生成API请求失败"
- utils.FileLog.Info("CreateApiLog生成API请求记录失败, ErrMsg: %s", e.Error())
- return
- }
- }
- if errMsg == "" {
- return
- }
- speech.State = speech_recognition.SpeechRecognitionStateFail
- speech.ConvertRemark = errMsg
- speech.ModifyTime = time.Now().Local()
- updateCols := []string{speech_recognition.SpeechRecognitionCols.State, speech_recognition.SpeechRecognitionCols.ConvertRemark, speech_recognition.SpeechRecognitionCols.ModifyTime}
- if e = speech.Update(updateCols); e != nil {
- utils.FileLog.Info("UpdateSpeech更新语音识别状态失败, ErrMsg: %s", e.Error())
- return
- }
- }(v)
- }
- wg.Wait()
- return
- }
- func SpeechRecognitionContentExport(exportType int, exportTimestamp bool, fileName string, contents []*speech_recognition.SpeechRecognitionContent) (result string, err error) {
- defer func() {
- if err != nil {
- fmt.Println(err)
- }
- }()
- if len(contents) == 0 {
- return
- }
- exportText := ""
- exportArr := make([]string, 0)
- exportPdfArr := make([]string, 0)
- for _, v := range contents {
- if v.Content == "" {
- continue
- }
- sec := ""
- secPdf := ""
- if exportTimestamp {
- sec = fmt.Sprintf("%s\n%s\n\n", utils.MillisecondsToHHMMSS(v.StartMs), v.Content)
- secPdf = fmt.Sprintf("%s %s", utils.MillisecondsToHHMMSS(v.StartMs), v.Content)
- } else {
- sec = fmt.Sprintf("%s\n\n", v.Content)
- secPdf = v.Content
- }
- exportText += sec
- exportArr = append(exportArr, sec)
- exportPdfArr = append(exportPdfArr, secPdf)
- }
- if exportType == SpeechRecognitionExportTypeDocx {
- doc := document.New()
- for _, v := range exportArr {
- p := doc.AddParagraph()
- prop := p.Properties()
- prop.Spacing().SetLineSpacing(measurement.Distance(1.5*15*measurement.Point), wml.ST_LineSpacingRuleAuto)
- prop.SetAlignment(wml.ST_JcLeft)
- run := p.AddRun()
- runProp := run.Properties()
- runProp.SetSize(measurement.Distance(15 * measurement.Point))
- runProp.SetFontFamily("宋体")
- run.AddText(v)
- run.AddBreak()
- }
- filePath := fmt.Sprintf("%s.docx", fileName)
- if e := doc.SaveToFile(filePath); e != nil {
- err = fmt.Errorf("生成docx失败, Err: %s", e.Error())
- return
- }
- result = filePath
- return
- }
- if exportType == SpeechRecognitionExportTypePdf {
- pdf := gofpdf.New("P", "mm", "A4", "")
- pdf.AddPage()
- pdf.AddUTF8Font("SimHei", "", "static/SimHei.ttf") // 此处字体文件只能用本地的
- pdf.SetFont("SimHei", "", 14)
- w, _ := pdf.GetPageSize()
- marginLeft := 10.0
- marginRight := 10.0
- availableWidth := w - marginLeft - marginRight
- for _, v := range exportPdfArr {
- pdf.MultiCell(availableWidth, 10, v, "", "L", false)
- pdf.MultiCell(availableWidth, 5, "", "", "L", false) // 单纯的换行
- }
- filePath := fmt.Sprintf("%s.pdf", fileName)
- if e := pdf.OutputFileAndClose(filePath); e != nil {
- err = fmt.Errorf("生成pdf失败, Err: %s", e.Error())
- return
- }
- result = filePath
- return
- }
- filePath := fmt.Sprintf("%s.txt", fileName)
- file, e := os.Create(filePath)
- if e != nil {
- err = fmt.Errorf("生成txt文件失败, err: %s", e.Error())
- return
- }
- defer file.Close()
- writer := bufio.NewWriter(file)
- _, e = writer.WriteString(exportText)
- if e != nil {
- err = fmt.Errorf("写入txt文件失败, err: %s", e.Error())
- return
- }
- if e = writer.Flush(); e != nil {
- err = fmt.Errorf("刷新txt缓存失败, err: %s", e.Error())
- return
- }
- result = filePath
- return
- }
- func MoveSpeechMenu(req speech_recognition.SpeechRecognitionMenuMoveReq) (err error, errMsg string) {
- menuId := req.MenuId
- parentMenuId := req.ParentMenuId
- prevMenuId := req.PrevMenuId
- nextMenuId := req.NextMenuId
- speechId := req.SpeechId
- prevSpeechId := req.PrevSpeechId
- nextSpeechId := req.NextSpeechId
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- speechOb := new(speech_recognition.SpeechRecognition)
- var parentSpeechMenu *speech_recognition.SpeechRecognitionMenu
- if parentMenuId > 0 {
- t, e := menuOb.GetItemById(parentMenuId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级目录信息失败,Err:" + err.Error())
- return
- }
- parentSpeechMenu = t
- }
- var (
- speechMenu *speech_recognition.SpeechRecognitionMenu
- prevMenu *speech_recognition.SpeechRecognitionMenu
- nextMenu *speech_recognition.SpeechRecognitionMenu
- speechItem *speech_recognition.SpeechRecognition
- prevSpeech *speech_recognition.SpeechRecognition
- nextSpeech *speech_recognition.SpeechRecognition
- prevSort int
- nextSort int
- )
- if speechId == 0 {
- speechMenu, err = menuOb.GetItemById(menuId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- errMsg = "当前目录不存在"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- if parentMenuId > 0 && parentSpeechMenu.Level == 3 {
- errMsg = "最高只支持添加3级目录"
- err = errors.New(errMsg)
- return
- }
- {
- cond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ParentId, speech_recognition.SpeechRecognitionMenuCols.SpeechRecognitionMenuId)
- pars := make([]interface{}, 0)
- pars = append(pars, speechMenu.MenuName, parentMenuId, menuId)
- exists, e := menuOb.GetItemByCondition(cond, pars, "")
- if e != nil && !utils.IsErrNoRow(e) {
- errMsg = "移动失败"
- err = fmt.Errorf("获取父级目录下的同名目录失败, Err: %s", e.Error())
- return
- }
- if exists != nil && exists.SpeechRecognitionMenuId > 0 {
- errMsg = "移动失败,目录名称已存在"
- return
- }
- }
- } else {
- speechItem, err = speechOb.GetItemById(speechId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- errMsg = "当前语音识别不存在"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- if parentMenuId == 0 {
- errMsg = "移动失败,语音识别必须挂在目录下"
- err = errors.New(errMsg)
- return
- }
- }
- if prevMenuId > 0 {
- prevMenu, err = menuOb.GetItemById(prevMenuId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点目录信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevMenu.Sort
- } else if prevSpeechId > 0 {
- prevSpeech, err = speechOb.GetItemById(prevSpeechId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevSpeech.Sort
- }
- if nextMenuId > 0 {
- nextMenu, err = menuOb.GetItemById(nextMenuId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点目录信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextMenu.Sort
- } else if nextSpeechId > 0 {
- nextSpeech, err = speechOb.GetItemById(nextSpeechId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点目录信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextSpeech.Sort
- }
- err, errMsg = moveSpeechMenu(parentSpeechMenu, speechMenu, prevMenu, nextMenu, speechItem, prevSpeech, nextSpeech, parentMenuId, prevSort, nextSort)
- return
- }
- func moveSpeechMenu(parentSpeechMenu, speechMenu, prevMenu, nextMenu *speech_recognition.SpeechRecognitionMenu, speechItem, prevSpeech, nextSpeech *speech_recognition.SpeechRecognition, parentMenuId int, prevSort, nextSort int) (err error, errMsg string) {
- updateCol := make([]string, 0)
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- speechOb := new(speech_recognition.SpeechRecognition)
- if speechMenu != nil {
- oldParentId := speechMenu.ParentId
- oldLevel := speechMenu.Level
- var menuIds []int
- if oldParentId != parentMenuId {
- childList, e, m := GetChildSpeechMenuByMenuId(speechMenu.SpeechRecognitionMenuId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("查询子目录失败,Err:" + e.Error() + m)
- return
- }
- if len(childList) > 0 {
- for _, v := range childList {
- if v.SpeechRecognitionMenuId == speechMenu.SpeechRecognitionMenuId {
- continue
- }
- menuIds = append(menuIds, v.SpeechRecognitionMenuId)
- }
- }
- }
- if speechMenu.ParentId != parentMenuId && parentMenuId != 0 {
- if speechMenu.Level != parentSpeechMenu.Level+1 { //禁止层级调整
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- speechMenu.ParentId = parentSpeechMenu.SpeechRecognitionMenuId
- speechMenu.RootId = parentSpeechMenu.RootId
- speechMenu.Level = parentSpeechMenu.Level + 1
- speechMenu.ModifyTime = time.Now()
- updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
- } else if speechMenu.ParentId != parentMenuId && parentMenuId == 0 {
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
- if nextSort > 0 {
- if prevSort == nextSort || prevSort == speechMenu.Sort {
- updateSortStr := `sort + 2`
- if prevMenu != nil {
- _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionMenuId, prevMenu.Sort, updateSortStr)
- } else {
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
- }
- if prevSpeech != nil {
- _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, prevSpeech.SpeechRecognitionId, updateSortStr)
- } else {
- _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
- }
- } else {
- if nextSort-prevSort == 1 {
- updateSortStr := `sort + 1`
- if prevMenu != nil {
- _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionMenuId, prevSort, updateSortStr)
- } else {
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
- }
- if prevSpeech != nil {
- _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, prevSpeech.SpeechRecognitionId, updateSortStr)
- } else {
- _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- speechMenu.Sort = prevSort + 1
- speechMenu.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevMenu == nil && nextMenu == nil && prevSpeech == nil && nextSpeech == nil && parentMenuId > 0 {
- var maxSort int
- maxSort, err = GetSpeechMenuMaxSort(parentMenuId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- speechMenu.Sort = maxSort + 1 //那就是排在组内最后一位
- speechMenu.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- firstMenu, tmpErr := menuOb.GetFirstByParentId(parentMenuId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tmpErr.Error())
- return
- }
- if firstMenu != nil && firstMenu.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = menuOb.UpdateSortByParentId(parentMenuId, firstMenu.SpeechRecognitionMenuId-1, 0, updateSortStr)
- _ = speechOb.UpdateSortByMenuId(parentMenuId, 0, 0, updateSortStr)
- } else {
- firstSpeech, tErr := speechOb.GetFirstByMenuId(parentMenuId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tErr.Error())
- return
- }
- if firstSpeech != nil && firstSpeech.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = speechOb.UpdateSortByMenuId(parentMenuId, 0, firstSpeech.SpeechRecognitionId-1, updateSortStr)
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, 0, updateSortStr)
- }
- }
- speechMenu.Sort = 0 //那就是排在第一位
- speechMenu.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- if len(updateCol) > 0 {
- err = speechMenu.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- if oldParentId != parentMenuId {
- if len(menuIds) > 0 {
- levelStep := speechMenu.Level - oldLevel
- err = menuOb.UpdateChildByParentMenuId(menuIds, speechMenu.RootId, levelStep)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新子目录失败,Err:" + err.Error())
- return
- }
- }
- }
- }
- } else {
- if speechItem == nil {
- errMsg = "当前语音识别不存在"
- err = errors.New(errMsg)
- return
- }
- if speechItem.MenuId != parentMenuId {
- speechItem.MenuId = parentMenuId
- speechItem.ModifyTime = time.Now()
- updateCol = append(updateCol, speech_recognition.SpeechRecognitionCols.MenuId, speech_recognition.SpeechRecognitionCols.ModifyTime)
- }
- if prevSort > 0 {
- if nextSort > 0 {
- if prevSort == nextSort || prevSort == speechItem.Sort {
- updateSortStr := `sort + 2`
- if prevMenu != nil {
- _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionMenuId, prevMenu.Sort, updateSortStr)
- } else {
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
- }
- if prevSpeech != nil {
- _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, prevSpeech.SpeechRecognitionId, updateSortStr)
- } else {
- _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
- }
- } else {
- if nextSort-prevSort == 1 {
- updateSortStr := `sort + 1`
- if prevMenu != nil {
- _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionMenuId, prevSort, updateSortStr)
- } else {
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
- }
- if prevSpeech != nil {
- _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, prevSpeech.SpeechRecognitionId, updateSortStr)
- } else {
- _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- speechItem.Sort = prevSort + 1
- speechItem.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevMenu == nil && nextMenu == nil && prevSpeech == nil && nextSpeech == nil && parentMenuId > 0 {
- var maxSort int
- maxSort, err = GetSpeechMenuMaxSort(parentMenuId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- speechItem.Sort = maxSort + 1 //那就是排在组内最后一位
- speechItem.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- firstMenu, tmpErr := menuOb.GetFirstByParentId(parentMenuId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tmpErr.Error())
- return
- }
- if firstMenu != nil && firstMenu.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = menuOb.UpdateSortByParentId(parentMenuId, firstMenu.SpeechRecognitionMenuId-1, 0, updateSortStr)
- _ = speechOb.UpdateSortByMenuId(parentMenuId, 0, 0, updateSortStr)
- } else {
- firstSpeech, tErr := speechOb.GetFirstByMenuId(parentMenuId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tErr.Error())
- return
- }
- if firstSpeech != nil && firstSpeech.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = speechOb.UpdateSortByMenuId(parentMenuId, 0, firstSpeech.SpeechRecognitionId-1, updateSortStr)
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, 0, updateSortStr)
- }
- }
- speechItem.Sort = 0 //那就是排在第一位
- speechItem.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- if len(updateCol) > 0 {
- err = speechItem.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- }
- }
- return
- }
- func GetChildSpeechMenuByMenuId(menuId int) (targetList []*speech_recognition.SpeechRecognitionMenu, err error, errMsg string) {
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- speechMenu, err := menuOb.GetItemById(menuId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- errMsg = "当前目录不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- cond := fmt.Sprintf(" AND %s = ?", speech_recognition.SpeechRecognitionMenuCols.RootId)
- pars := make([]interface{}, 0)
- pars = append(pars, speechMenu.RootId)
- order := fmt.Sprintf("%s ASC, %s ASC, %s ASC", speech_recognition.SpeechRecognitionMenuCols.Level, speech_recognition.SpeechRecognitionMenuCols.Sort, speech_recognition.SpeechRecognitionMenuCols.SpeechRecognitionMenuId)
- tmpList, err := speechMenu.GetItemsByCondition(cond, pars, []string{}, order)
- if err != nil && !utils.IsErrNoRow(err) {
- errMsg = "获取失败"
- err = errors.New("获取数据失败,Err:" + err.Error())
- return
- }
- idMap := make(map[int]struct{})
- if len(tmpList) > 0 {
- for _, v := range tmpList {
- if v.SpeechRecognitionMenuId == speechMenu.SpeechRecognitionMenuId {
- idMap[v.SpeechRecognitionMenuId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ParentId]; ok {
- idMap[v.SpeechRecognitionMenuId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.SpeechRecognitionMenuId]; ok {
- targetList = append(targetList, v)
- }
- }
- }
- return
- }
- func GetSpeechMenuMaxSort(parentId int) (maxSort int, err error) {
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- speechOb := new(speech_recognition.SpeechRecognition)
- menuMax, err := menuOb.GetMaxSortByParentId(parentId)
- if err != nil {
- return
- }
- maxSort = menuMax
- speechMax, err := speechOb.GetMaxSortByMenuId(parentId)
- if err != nil {
- return
- }
- if maxSort < speechMax {
- maxSort = speechMax
- }
- return
- }
- func MoveSpeechTagMenu(req speech_recognition.SpeechRecognitionTagMenuMoveReq) (err error, errMsg string) {
- menuId := req.MenuId
- parentMenuId := req.ParentMenuId
- prevMenuId := req.PrevMenuId
- nextMenuId := req.NextMenuId
- tagId := req.TagId
- prevTagId := req.PrevTagId
- nextTagId := req.NextTagId
- menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
- tagOb := new(speech_recognition.SpeechRecognitionTag)
- var parentTagMenu *speech_recognition.SpeechRecognitionTagMenu
- if parentMenuId > 0 {
- t, e := menuOb.GetItemById(parentMenuId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级目录信息失败,Err:" + err.Error())
- return
- }
- parentTagMenu = t
- }
- var (
- tagMenu *speech_recognition.SpeechRecognitionTagMenu
- prevMenu *speech_recognition.SpeechRecognitionTagMenu
- nextMenu *speech_recognition.SpeechRecognitionTagMenu
- tagItem *speech_recognition.SpeechRecognitionTag
- prevTag *speech_recognition.SpeechRecognitionTag
- nextTag *speech_recognition.SpeechRecognitionTag
- prevSort int
- nextSort int
- )
- if tagId == 0 {
- tagMenu, err = menuOb.GetItemById(menuId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- errMsg = "当前目录不存在"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- if parentMenuId > 0 && parentTagMenu.Level == 3 {
- errMsg = "最高只支持添加3级目录"
- err = errors.New(errMsg)
- return
- }
- {
- cond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionTagMenuCols.MenuName, speech_recognition.SpeechRecognitionTagMenuCols.ParentId, speech_recognition.SpeechRecognitionTagMenuCols.SpeechRecognitionTagMenuId)
- pars := make([]interface{}, 0)
- pars = append(pars, tagMenu.MenuName, parentMenuId, menuId)
- exists, e := menuOb.GetItemByCondition(cond, pars, "")
- if e != nil && !utils.IsErrNoRow(e) {
- errMsg = "移动失败"
- err = fmt.Errorf("获取父级目录下的同名目录失败, Err: %s", e.Error())
- return
- }
- if exists != nil && exists.SpeechRecognitionTagMenuId > 0 {
- errMsg = "移动失败,目录名称已存在"
- return
- }
- }
- } else {
- tagItem, err = tagOb.GetItemById(tagId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- errMsg = "当前标签不存在"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- if parentMenuId == 0 {
- errMsg = "移动失败,标签必须挂在目录下"
- err = errors.New(errMsg)
- return
- }
- }
- if prevMenuId > 0 {
- prevMenu, err = menuOb.GetItemById(prevMenuId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点目录信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevMenu.Sort
- } else if prevTagId > 0 {
- prevTag, err = tagOb.GetItemById(prevTagId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevTag.Sort
- }
- if nextMenuId > 0 {
- nextMenu, err = menuOb.GetItemById(nextMenuId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点目录信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextMenu.Sort
- } else if nextTagId > 0 {
- nextTag, err = tagOb.GetItemById(nextTagId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点目录信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextTag.Sort
- }
- err, errMsg = moveSpeechTagMenu(parentTagMenu, tagMenu, prevMenu, nextMenu, tagItem, prevTag, nextTag, parentMenuId, prevSort, nextSort)
- return
- }
- func moveSpeechTagMenu(parentTagMenu, tagMenu, prevMenu, nextMenu *speech_recognition.SpeechRecognitionTagMenu, tagItem, prevTag, nextTag *speech_recognition.SpeechRecognitionTag, parentMenuId int, prevSort, nextSort int) (err error, errMsg string) {
- updateCol := make([]string, 0)
- menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
- tagOb := new(speech_recognition.SpeechRecognitionTag)
- if tagMenu != nil {
- oldParentId := tagMenu.ParentId
- oldLevel := tagMenu.Level
- var menuIds []int
- if oldParentId != parentMenuId {
- childList, e, m := GetChildSpeechTagMenuByMenuId(tagMenu.SpeechRecognitionTagMenuId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("查询子目录失败,Err:" + e.Error() + m)
- return
- }
- if len(childList) > 0 {
- for _, v := range childList {
- if v.SpeechRecognitionTagMenuId == tagMenu.SpeechRecognitionTagMenuId {
- continue
- }
- menuIds = append(menuIds, v.SpeechRecognitionTagMenuId)
- }
- }
- }
- if tagMenu.ParentId != parentMenuId && parentMenuId != 0 {
- if tagMenu.Level != parentTagMenu.Level+1 { //禁止层级调整
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- tagMenu.ParentId = parentTagMenu.SpeechRecognitionTagMenuId
- tagMenu.RootId = parentTagMenu.RootId
- tagMenu.Level = parentTagMenu.Level + 1
- tagMenu.ModifyTime = time.Now()
- updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
- } else if tagMenu.ParentId != parentMenuId && parentMenuId == 0 {
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
- if nextSort > 0 {
- if prevSort == nextSort || prevSort == tagMenu.Sort {
- updateSortStr := `sort + 2`
- if prevMenu != nil {
- _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionTagMenuId, prevMenu.Sort, updateSortStr)
- } else {
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
- }
- if prevTag != nil {
- _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, prevTag.SpeechRecognitionTagId, updateSortStr)
- } else {
- _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
- }
- } else {
- if nextSort-prevSort == 1 {
- updateSortStr := `sort + 1`
- if prevMenu != nil {
- _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionTagMenuId, prevSort, updateSortStr)
- } else {
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
- }
- if prevTag != nil {
- _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, prevTag.SpeechRecognitionTagId, updateSortStr)
- } else {
- _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- tagMenu.Sort = prevSort + 1
- tagMenu.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevMenu == nil && nextMenu == nil && prevTag == nil && nextTag == nil && parentMenuId > 0 {
- var maxSort int
- maxSort, err = GetSpeechTagMenuMaxSort(parentMenuId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- tagMenu.Sort = maxSort + 1 //那就是排在组内最后一位
- tagMenu.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- firstTagMenu, tmpErr := menuOb.GetFirstByParentId(parentMenuId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tmpErr.Error())
- return
- }
- if firstTagMenu != nil && firstTagMenu.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = menuOb.UpdateSortByParentId(parentMenuId, firstTagMenu.SpeechRecognitionTagMenuId-1, 0, updateSortStr)
- _ = tagOb.UpdateSortByMenuId(parentMenuId, 0, 0, updateSortStr)
- } else {
- firstTag, tErr := tagOb.GetFirstByMenuId(parentMenuId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tErr.Error())
- return
- }
- if firstTag != nil && firstTag.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = tagOb.UpdateSortByMenuId(parentMenuId, 0, firstTag.SpeechRecognitionTagId-1, updateSortStr)
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, 0, updateSortStr)
- }
- }
- tagMenu.Sort = 0 //那就是排在第一位
- tagMenu.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- if len(updateCol) > 0 {
- err = tagMenu.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- if oldParentId != parentMenuId {
- if len(menuIds) > 0 {
- levelStep := tagMenu.Level - oldLevel
- err = menuOb.UpdateChildByParentMenuId(menuIds, tagMenu.RootId, levelStep)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新子目录失败,Err:" + err.Error())
- return
- }
- }
- }
- }
- } else {
- if tagItem == nil {
- errMsg = "当前标签不存在"
- err = errors.New(errMsg)
- return
- }
- if tagItem.MenuId != parentMenuId {
- tagItem.MenuId = parentMenuId
- tagItem.ModifyTime = time.Now()
- updateCol = append(updateCol, speech_recognition.SpeechRecognitionTagCols.MenuId, speech_recognition.SpeechRecognitionTagCols.ModifyTime)
- }
- if prevSort > 0 {
- if nextSort > 0 {
- if prevSort == nextSort || prevSort == tagItem.Sort {
- updateSortStr := `sort + 2`
- if prevMenu != nil {
- _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionTagMenuId, prevMenu.Sort, updateSortStr)
- } else {
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
- }
- if prevTag != nil {
- _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, prevTag.SpeechRecognitionTagId, updateSortStr)
- } else {
- _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
- }
- } else {
- if nextSort-prevSort == 1 {
- updateSortStr := `sort + 1`
- if prevMenu != nil {
- _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionTagMenuId, prevSort, updateSortStr)
- } else {
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
- }
- if prevTag != nil {
- _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, prevTag.SpeechRecognitionTagId, updateSortStr)
- } else {
- _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- tagItem.Sort = prevSort + 1
- tagItem.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevMenu == nil && nextMenu == nil && prevTag == nil && nextTag == nil && parentMenuId > 0 {
- var maxSort int
- maxSort, err = GetSpeechTagMenuMaxSort(parentMenuId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- tagItem.Sort = maxSort + 1 //那就是排在组内最后一位
- tagItem.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- firstTagMenu, tmpErr := menuOb.GetFirstByParentId(parentMenuId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tmpErr.Error())
- return
- }
- if firstTagMenu != nil && firstTagMenu.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = menuOb.UpdateSortByParentId(parentMenuId, firstTagMenu.SpeechRecognitionTagMenuId-1, 0, updateSortStr)
- _ = tagOb.UpdateSortByMenuId(parentMenuId, 0, 0, updateSortStr)
- } else {
- firstTag, tErr := tagOb.GetFirstByMenuId(parentMenuId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tErr.Error())
- return
- }
- if firstTag != nil && firstTag.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = tagOb.UpdateSortByMenuId(parentMenuId, 0, firstTag.SpeechRecognitionTagId-1, updateSortStr)
- _ = menuOb.UpdateSortByParentId(parentMenuId, 0, 0, updateSortStr)
- }
- }
- tagItem.Sort = 0 //那就是排在第一位
- tagItem.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- if len(updateCol) > 0 {
- err = tagItem.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- }
- }
- return
- }
- func GetChildSpeechTagMenuByMenuId(menuId int) (targetList []*speech_recognition.SpeechRecognitionTagMenu, err error, errMsg string) {
- menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
- tagMenu, err := menuOb.GetItemById(menuId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- errMsg = "当前目录不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取目录信息失败,Err:" + err.Error())
- return
- }
- cond := fmt.Sprintf(" AND %s = ?", speech_recognition.SpeechRecognitionTagMenuCols.RootId)
- pars := make([]interface{}, 0)
- pars = append(pars, tagMenu.RootId)
- order := fmt.Sprintf("%s ASC, %s ASC, %s ASC", speech_recognition.SpeechRecognitionTagMenuCols.Level, speech_recognition.SpeechRecognitionTagMenuCols.Sort, speech_recognition.SpeechRecognitionTagMenuCols.SpeechRecognitionTagMenuId)
- tmpList, err := tagMenu.GetItemsByCondition(cond, pars, []string{}, order)
- if err != nil && !utils.IsErrNoRow(err) {
- errMsg = "获取失败"
- err = errors.New("获取数据失败,Err:" + err.Error())
- return
- }
- idMap := make(map[int]struct{})
- if len(tmpList) > 0 {
- for _, v := range tmpList {
- if v.SpeechRecognitionTagMenuId == tagMenu.SpeechRecognitionTagMenuId {
- idMap[v.SpeechRecognitionTagMenuId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ParentId]; ok {
- idMap[v.SpeechRecognitionTagMenuId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.SpeechRecognitionTagMenuId]; ok {
- targetList = append(targetList, v)
- }
- }
- }
- return
- }
- func GetSpeechTagMenuMaxSort(parentId int) (maxSort int, err error) {
- menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
- tagOb := new(speech_recognition.SpeechRecognitionTag)
- menuMax, err := menuOb.GetMaxSortByParentId(parentId)
- if err != nil {
- return
- }
- maxSort = menuMax
- speechMax, err := tagOb.GetMaxSortByMenuId(parentId)
- if err != nil {
- return
- }
- if maxSort < speechMax {
- maxSort = speechMax
- }
- return
- }
- func GetSpeechRecognitionMenuChildrenRecursive(list []*speech_recognition.SpeechRecognitionMenu, parentId int) []int {
- childIds := make([]int, 0)
- for _, v := range list {
- if v.ParentId == parentId {
- childIds = append(childIds, v.SpeechRecognitionMenuId)
- ids := GetSpeechRecognitionMenuChildrenRecursive(list, v.SpeechRecognitionMenuId)
- if len(ids) > 0 {
- childIds = append(childIds, ids...)
- }
- }
- }
- return childIds
- }
- func GetSpeechRecognitionTagMenuChildrenRecursive(list []*speech_recognition.SpeechRecognitionTagMenu, parentId int) []int {
- childIds := make([]int, 0)
- for _, v := range list {
- if v.ParentId == parentId {
- childIds = append(childIds, v.SpeechRecognitionTagMenuId)
- ids := GetSpeechRecognitionTagMenuChildrenRecursive(list, v.SpeechRecognitionTagMenuId)
- if len(ids) > 0 {
- childIds = append(childIds, ids...)
- }
- }
- }
- return childIds
- }
- func CheckSpeechRecognitionMenuRemove(menuId int) (result *speech_recognition.SpeechRecognitionMenuRemoveCheckResp, menuIds []int, err error) {
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- result = new(speech_recognition.SpeechRecognitionMenuRemoveCheckResp)
- menus, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
- if e != nil {
- err = fmt.Errorf("获取语音识别目录列表失败, err: %s", e.Error())
- return
- }
- childIds := GetSpeechRecognitionMenuChildrenRecursive(menus, menuId)
- menuIds = make([]int, 0)
- menuIds = append(menuIds, menuId)
- if len(childIds) > 0 {
- menuIds = append(menuIds, childIds...)
- }
- speechOb := new(speech_recognition.SpeechRecognition)
- cond := fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.MenuId, utils.GetOrmInReplace(len(menuIds)))
- pars := make([]interface{}, 0)
- pars = append(pars, menuIds)
- count, e := speechOb.GetCountByCondition(cond, pars)
- if e != nil {
- err = fmt.Errorf("获取目录下的语音识别数失败, err: %s", e.Error())
- return
- }
- if count > 0 {
- result.CheckResult = SpeechMenuCheckRemoveTypeRefused
- result.Tips = "该分类关联转写文件,删除失败!"
- return
- }
- if len(childIds) > 0 {
- result.CheckResult = SpeechMenuCheckRemoveTypeWarning
- result.Tips = "该分类包含内容为空的子分类"
- return
- }
- result.Tips = "校验通过,可以删除"
- return
- }
- func CheckSpeechRecognitionTagMenuRemove(menuId int) (result *speech_recognition.SpeechRecognitionMenuRemoveCheckResp, menuIds []int, err error) {
- menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
- result = new(speech_recognition.SpeechRecognitionMenuRemoveCheckResp)
- menus, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
- if e != nil {
- err = fmt.Errorf("获取标签目录列表失败, err: %s", e.Error())
- return
- }
- childIds := GetSpeechRecognitionTagMenuChildrenRecursive(menus, menuId)
- menuIds = make([]int, 0)
- menuIds = append(menuIds, menuId)
- if len(childIds) > 0 {
- menuIds = append(menuIds, childIds...)
- }
- tagOb := new(speech_recognition.SpeechRecognitionTag)
- cond := fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionTagCols.MenuId, utils.GetOrmInReplace(len(menuIds)))
- pars := make([]interface{}, 0)
- pars = append(pars, menuIds)
- count, e := tagOb.GetCountByCondition(cond, pars)
- if e != nil {
- err = fmt.Errorf("获取目录下的标签数失败, err: %s", e.Error())
- return
- }
- if count > 0 {
- result.CheckResult = SpeechMenuCheckRemoveTypeRefused
- result.Tips = "该分类关联标签,删除失败!"
- return
- }
- if len(childIds) > 0 {
- result.CheckResult = SpeechMenuCheckRemoveTypeWarning
- result.Tips = "该分类包含内容为空的子分类"
- return
- }
- result.Tips = "校验通过,可以删除"
- return
- }
- func GetSpeechRecognitionMenuPathRecursive(list []*speech_recognition.SpeechRecognitionMenu, menuId int) []*speech_recognition.SpeechRecognitionMenuItem {
- res := make([]*speech_recognition.SpeechRecognitionMenuItem, 0)
- for _, v := range list {
- if v.SpeechRecognitionMenuId == menuId {
- t := new(speech_recognition.SpeechRecognitionMenuItem)
- t.UniqueCode = v.UniqueCode
- t.MenuId = v.SpeechRecognitionMenuId
- t.MenuName = v.MenuName
- t.ParentId = v.ParentId
- t.Level = v.Level
- t.Sort = v.Sort
- t.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
- if v.ParentId > 0 {
- res = GetSpeechRecognitionMenuPathRecursive(list, v.ParentId)
- }
- res = append(res, t)
- }
- }
- return res
- }
|