speech_recognition.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. package services
  2. import (
  3. "bufio"
  4. "errors"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/speech_recognition"
  7. "eta_gn/eta_api/services/alarm_msg"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "os"
  11. "strconv"
  12. "sync"
  13. "time"
  14. "baliance.com/gooxml/document"
  15. "baliance.com/gooxml/measurement"
  16. "baliance.com/gooxml/schema/soo/wml"
  17. "github.com/jung-kurt/gofpdf"
  18. )
  19. const (
  20. SpeechRecognitionExportTypeTxt = 1
  21. SpeechRecognitionExportTypeDocx = 2
  22. SpeechRecognitionExportTypePdf = 3
  23. SpeechMenuCheckRemoveTypePass = 0 // 目录删除校验-可删除
  24. SpeechMenuCheckRemoveTypeRefused = 1 // 目录删除校验-不可删除
  25. SpeechMenuCheckRemoveTypeWarning = 2 // 目录删除校验-警告
  26. )
  27. func GetSpeechRecognitionMenuTreeRecursive(list []*speech_recognition.SpeechRecognitionMenu, parentId int) []*speech_recognition.SpeechRecognitionMenuItem {
  28. res := make([]*speech_recognition.SpeechRecognitionMenuItem, 0)
  29. for _, v := range list {
  30. if v.ParentId == parentId {
  31. t := new(speech_recognition.SpeechRecognitionMenuItem)
  32. t.UniqueCode = v.UniqueCode
  33. t.MenuId = v.SpeechRecognitionMenuId
  34. t.MenuName = v.MenuName
  35. t.ParentId = v.ParentId
  36. t.Level = v.Level
  37. t.Sort = v.Sort
  38. t.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
  39. t.Children = GetSpeechRecognitionMenuTreeRecursive(list, v.SpeechRecognitionMenuId)
  40. res = append(res, t)
  41. }
  42. }
  43. return res
  44. }
  45. func GetSpeechRecognitionTagMenuTreeRecursive(list []*speech_recognition.SpeechRecognitionTagMenu, parentId int) []*speech_recognition.SpeechRecognitionTagMenuItem {
  46. res := make([]*speech_recognition.SpeechRecognitionTagMenuItem, 0)
  47. for _, v := range list {
  48. if v.ParentId == parentId {
  49. t := new(speech_recognition.SpeechRecognitionTagMenuItem)
  50. t.UniqueCode = v.UniqueCode
  51. t.MenuId = v.SpeechRecognitionTagMenuId
  52. t.MenuName = v.MenuName
  53. t.ParentId = v.ParentId
  54. t.Level = v.Level
  55. t.Sort = v.Sort
  56. t.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
  57. t.Children = GetSpeechRecognitionTagMenuTreeRecursive(list, v.SpeechRecognitionTagMenuId)
  58. res = append(res, t)
  59. }
  60. }
  61. return res
  62. }
  63. func BatchConvertSpeech(speeches []*speech_recognition.SpeechRecognition) {
  64. var err error
  65. defer func() {
  66. if err != nil {
  67. tips := fmt.Sprintf("批量转写语音失败, ErrMsg: %s", err.Error())
  68. utils.FileLog.Info(tips)
  69. go alarm_msg.SendAlarmMsg(tips, 1)
  70. }
  71. }()
  72. conf, e := models.GetBusinessConf()
  73. if e != nil {
  74. err = fmt.Errorf("获取配置失败, Err: %s", e.Error())
  75. return
  76. }
  77. if conf[models.BusinessConfTencentApiSecretId] == "" || conf[models.BusinessConfTencentApiSecretKey] == "" || conf[models.BusinessConfTencentApiRecTaskCallbackUrl] == "" {
  78. err = fmt.Errorf("API配置有误, SecretId: %s, SecretKey: %s, Callback: %s", conf[models.BusinessConfTencentApiSecretId], conf[models.BusinessConfTencentApiSecretKey], conf[models.BusinessConfTencentApiRecTaskCallbackUrl])
  79. return
  80. }
  81. apiLimit := make(chan struct{}, 20)
  82. var wg sync.WaitGroup
  83. for _, v := range speeches {
  84. wg.Add(1)
  85. go func(speech *speech_recognition.SpeechRecognition) {
  86. defer func() {
  87. wg.Done()
  88. <-apiLimit
  89. }()
  90. apiLimit <- struct{}{}
  91. var errMsg string
  92. var r TencentRecTaskReq
  93. r.FileUrl = speech.ResourceUrl
  94. r.SecretId = conf[models.BusinessConfTencentApiSecretId]
  95. r.SecretKey = conf[models.BusinessConfTencentApiSecretKey]
  96. r.CallbackUrl = conf[models.BusinessConfTencentApiRecTaskCallbackUrl]
  97. taskId, e := TencentCreateRecTask(r)
  98. if e != nil {
  99. errMsg = "创建语音识别任务失败"
  100. utils.FileLog.Info("TencentCreateRecTask创建语音识别任务失败, ErrMsg: %s", e.Error())
  101. }
  102. if errMsg == "" {
  103. apiLog := new(speech_recognition.SpeechRecognitionApiLog)
  104. apiLog.SpeechRecognitionId = speech.SpeechRecognitionId
  105. apiLog.RequestId = strconv.Itoa(taskId)
  106. apiLog.RequestCode = -1
  107. apiLog.CreateTime = time.Now().Local()
  108. apiLog.ModifyTime = time.Now().Local()
  109. if e = apiLog.Create(); e != nil {
  110. errMsg = "生成API请求失败"
  111. utils.FileLog.Info("CreateApiLog生成API请求记录失败, ErrMsg: %s", e.Error())
  112. return
  113. }
  114. }
  115. if errMsg == "" {
  116. return
  117. }
  118. speech.State = speech_recognition.SpeechRecognitionStateFail
  119. speech.ConvertRemark = errMsg
  120. speech.ModifyTime = time.Now().Local()
  121. updateCols := []string{speech_recognition.SpeechRecognitionCols.State, speech_recognition.SpeechRecognitionCols.ConvertRemark, speech_recognition.SpeechRecognitionCols.ModifyTime}
  122. if e = speech.Update(updateCols); e != nil {
  123. utils.FileLog.Info("UpdateSpeech更新语音识别状态失败, ErrMsg: %s", e.Error())
  124. return
  125. }
  126. }(v)
  127. }
  128. wg.Wait()
  129. return
  130. }
  131. func SpeechRecognitionContentExport(exportType int, exportTimestamp bool, fileName string, contents []*speech_recognition.SpeechRecognitionContent) (result string, err error) {
  132. defer func() {
  133. if err != nil {
  134. fmt.Println(err)
  135. }
  136. }()
  137. if len(contents) == 0 {
  138. return
  139. }
  140. exportText := ""
  141. exportArr := make([]string, 0)
  142. exportPdfArr := make([]string, 0)
  143. for _, v := range contents {
  144. if v.Content == "" {
  145. continue
  146. }
  147. sec := ""
  148. secPdf := ""
  149. if exportTimestamp {
  150. sec = fmt.Sprintf("%s\n%s\n\n", utils.MillisecondsToHHMMSS(v.StartMs), v.Content)
  151. secPdf = fmt.Sprintf("%s %s", utils.MillisecondsToHHMMSS(v.StartMs), v.Content)
  152. } else {
  153. sec = fmt.Sprintf("%s\n\n", v.Content)
  154. secPdf = v.Content
  155. }
  156. exportText += sec
  157. exportArr = append(exportArr, sec)
  158. exportPdfArr = append(exportPdfArr, secPdf)
  159. }
  160. if exportType == SpeechRecognitionExportTypeDocx {
  161. doc := document.New()
  162. for _, v := range exportArr {
  163. p := doc.AddParagraph()
  164. prop := p.Properties()
  165. prop.Spacing().SetLineSpacing(measurement.Distance(1.5*15*measurement.Point), wml.ST_LineSpacingRuleAuto)
  166. prop.SetAlignment(wml.ST_JcLeft)
  167. run := p.AddRun()
  168. runProp := run.Properties()
  169. runProp.SetSize(measurement.Distance(15 * measurement.Point))
  170. runProp.SetFontFamily("宋体")
  171. run.AddText(v)
  172. run.AddBreak()
  173. }
  174. filePath := fmt.Sprintf("%s.docx", fileName)
  175. if e := doc.SaveToFile(filePath); e != nil {
  176. err = fmt.Errorf("生成docx失败, Err: %s", e.Error())
  177. return
  178. }
  179. result = filePath
  180. return
  181. }
  182. if exportType == SpeechRecognitionExportTypePdf {
  183. pdf := gofpdf.New("P", "mm", "A4", "")
  184. pdf.AddPage()
  185. pdf.AddUTF8Font("SimHei", "", "static/SimHei.ttf") // 此处字体文件只能用本地的
  186. pdf.SetFont("SimHei", "", 14)
  187. w, _ := pdf.GetPageSize()
  188. marginLeft := 10.0
  189. marginRight := 10.0
  190. availableWidth := w - marginLeft - marginRight
  191. for _, v := range exportPdfArr {
  192. pdf.MultiCell(availableWidth, 10, v, "", "L", false)
  193. pdf.MultiCell(availableWidth, 5, "", "", "L", false) // 单纯的换行
  194. }
  195. filePath := fmt.Sprintf("%s.pdf", fileName)
  196. if e := pdf.OutputFileAndClose(filePath); e != nil {
  197. err = fmt.Errorf("生成pdf失败, Err: %s", e.Error())
  198. return
  199. }
  200. result = filePath
  201. return
  202. }
  203. filePath := fmt.Sprintf("%s.txt", fileName)
  204. file, e := os.Create(filePath)
  205. if e != nil {
  206. err = fmt.Errorf("生成txt文件失败, err: %s", e.Error())
  207. return
  208. }
  209. defer file.Close()
  210. writer := bufio.NewWriter(file)
  211. _, e = writer.WriteString(exportText)
  212. if e != nil {
  213. err = fmt.Errorf("写入txt文件失败, err: %s", e.Error())
  214. return
  215. }
  216. if e = writer.Flush(); e != nil {
  217. err = fmt.Errorf("刷新txt缓存失败, err: %s", e.Error())
  218. return
  219. }
  220. result = filePath
  221. return
  222. }
  223. func MoveSpeechMenu(req speech_recognition.SpeechRecognitionMenuMoveReq) (err error, errMsg string) {
  224. menuId := req.MenuId
  225. parentMenuId := req.ParentMenuId
  226. prevMenuId := req.PrevMenuId
  227. nextMenuId := req.NextMenuId
  228. speechId := req.SpeechId
  229. prevSpeechId := req.PrevSpeechId
  230. nextSpeechId := req.NextSpeechId
  231. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  232. speechOb := new(speech_recognition.SpeechRecognition)
  233. var parentSpeechMenu *speech_recognition.SpeechRecognitionMenu
  234. if parentMenuId > 0 {
  235. t, e := menuOb.GetItemById(parentMenuId)
  236. if e != nil {
  237. errMsg = "移动失败"
  238. err = errors.New("获取上级目录信息失败,Err:" + err.Error())
  239. return
  240. }
  241. parentSpeechMenu = t
  242. }
  243. var (
  244. speechMenu *speech_recognition.SpeechRecognitionMenu
  245. prevMenu *speech_recognition.SpeechRecognitionMenu
  246. nextMenu *speech_recognition.SpeechRecognitionMenu
  247. speechItem *speech_recognition.SpeechRecognition
  248. prevSpeech *speech_recognition.SpeechRecognition
  249. nextSpeech *speech_recognition.SpeechRecognition
  250. prevSort int
  251. nextSort int
  252. )
  253. if speechId == 0 {
  254. speechMenu, err = menuOb.GetItemById(menuId)
  255. if err != nil {
  256. if utils.IsErrNoRow(err) {
  257. errMsg = "当前目录不存在"
  258. err = errors.New("获取目录信息失败,Err:" + err.Error())
  259. return
  260. }
  261. errMsg = "移动失败"
  262. err = errors.New("获取目录信息失败,Err:" + err.Error())
  263. return
  264. }
  265. if parentMenuId > 0 && parentSpeechMenu.Level == 3 {
  266. errMsg = "最高只支持添加3级目录"
  267. err = errors.New(errMsg)
  268. return
  269. }
  270. {
  271. cond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ParentId, speech_recognition.SpeechRecognitionMenuCols.SpeechRecognitionMenuId)
  272. pars := make([]interface{}, 0)
  273. pars = append(pars, speechMenu.MenuName, parentMenuId, menuId)
  274. exists, e := menuOb.GetItemByCondition(cond, pars, "")
  275. if e != nil && !utils.IsErrNoRow(e) {
  276. errMsg = "移动失败"
  277. err = fmt.Errorf("获取父级目录下的同名目录失败, Err: %s", e.Error())
  278. return
  279. }
  280. if exists != nil && exists.SpeechRecognitionMenuId > 0 {
  281. errMsg = "移动失败,目录名称已存在"
  282. return
  283. }
  284. }
  285. } else {
  286. speechItem, err = speechOb.GetItemById(speechId)
  287. if err != nil {
  288. if utils.IsErrNoRow(err) {
  289. errMsg = "当前语音识别不存在"
  290. err = errors.New("获取目录信息失败,Err:" + err.Error())
  291. return
  292. }
  293. errMsg = "移动失败"
  294. err = errors.New("获取目录信息失败,Err:" + err.Error())
  295. return
  296. }
  297. if parentMenuId == 0 {
  298. errMsg = "移动失败,语音识别必须挂在目录下"
  299. err = errors.New(errMsg)
  300. return
  301. }
  302. }
  303. if prevMenuId > 0 {
  304. prevMenu, err = menuOb.GetItemById(prevMenuId)
  305. if err != nil {
  306. errMsg = "移动失败"
  307. err = errors.New("获取上一个兄弟节点目录信息失败,Err:" + err.Error())
  308. return
  309. }
  310. prevSort = prevMenu.Sort
  311. } else if prevSpeechId > 0 {
  312. prevSpeech, err = speechOb.GetItemById(prevSpeechId)
  313. if err != nil {
  314. errMsg = "移动失败"
  315. err = errors.New("获取上一个兄弟节点信息失败,Err:" + err.Error())
  316. return
  317. }
  318. prevSort = prevSpeech.Sort
  319. }
  320. if nextMenuId > 0 {
  321. nextMenu, err = menuOb.GetItemById(nextMenuId)
  322. if err != nil {
  323. errMsg = "移动失败"
  324. err = errors.New("获取下一个兄弟节点目录信息失败,Err:" + err.Error())
  325. return
  326. }
  327. nextSort = nextMenu.Sort
  328. } else if nextSpeechId > 0 {
  329. nextSpeech, err = speechOb.GetItemById(nextSpeechId)
  330. if err != nil {
  331. errMsg = "移动失败"
  332. err = errors.New("获取下一个兄弟节点目录信息失败,Err:" + err.Error())
  333. return
  334. }
  335. nextSort = nextSpeech.Sort
  336. }
  337. err, errMsg = moveSpeechMenu(parentSpeechMenu, speechMenu, prevMenu, nextMenu, speechItem, prevSpeech, nextSpeech, parentMenuId, prevSort, nextSort)
  338. return
  339. }
  340. func moveSpeechMenu(parentSpeechMenu, speechMenu, prevMenu, nextMenu *speech_recognition.SpeechRecognitionMenu, speechItem, prevSpeech, nextSpeech *speech_recognition.SpeechRecognition, parentMenuId int, prevSort, nextSort int) (err error, errMsg string) {
  341. updateCol := make([]string, 0)
  342. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  343. speechOb := new(speech_recognition.SpeechRecognition)
  344. if speechMenu != nil {
  345. oldParentId := speechMenu.ParentId
  346. oldLevel := speechMenu.Level
  347. var menuIds []int
  348. if oldParentId != parentMenuId {
  349. childList, e, m := GetChildSpeechMenuByMenuId(speechMenu.SpeechRecognitionMenuId)
  350. if e != nil {
  351. errMsg = "移动失败"
  352. err = errors.New("查询子目录失败,Err:" + e.Error() + m)
  353. return
  354. }
  355. if len(childList) > 0 {
  356. for _, v := range childList {
  357. if v.SpeechRecognitionMenuId == speechMenu.SpeechRecognitionMenuId {
  358. continue
  359. }
  360. menuIds = append(menuIds, v.SpeechRecognitionMenuId)
  361. }
  362. }
  363. }
  364. if speechMenu.ParentId != parentMenuId && parentMenuId != 0 {
  365. if speechMenu.Level != parentSpeechMenu.Level+1 { //禁止层级调整
  366. errMsg = "移动失败"
  367. err = errors.New("不支持目录层级变更")
  368. return
  369. }
  370. speechMenu.ParentId = parentSpeechMenu.SpeechRecognitionMenuId
  371. speechMenu.RootId = parentSpeechMenu.RootId
  372. speechMenu.Level = parentSpeechMenu.Level + 1
  373. speechMenu.ModifyTime = time.Now()
  374. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  375. } else if speechMenu.ParentId != parentMenuId && parentMenuId == 0 {
  376. errMsg = "移动失败"
  377. err = errors.New("不支持目录层级变更")
  378. return
  379. }
  380. if prevSort > 0 {
  381. if nextSort > 0 {
  382. if prevSort == nextSort || prevSort == speechMenu.Sort {
  383. updateSortStr := `sort + 2`
  384. if prevMenu != nil {
  385. _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionMenuId, prevMenu.Sort, updateSortStr)
  386. } else {
  387. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
  388. }
  389. if prevSpeech != nil {
  390. _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, prevSpeech.SpeechRecognitionId, updateSortStr)
  391. } else {
  392. _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
  393. }
  394. } else {
  395. if nextSort-prevSort == 1 {
  396. updateSortStr := `sort + 1`
  397. if prevMenu != nil {
  398. _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionMenuId, prevSort, updateSortStr)
  399. } else {
  400. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
  401. }
  402. if prevSpeech != nil {
  403. _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, prevSpeech.SpeechRecognitionId, updateSortStr)
  404. } else {
  405. _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
  406. }
  407. }
  408. }
  409. }
  410. speechMenu.Sort = prevSort + 1
  411. speechMenu.ModifyTime = time.Now()
  412. updateCol = append(updateCol, "Sort", "ModifyTime")
  413. } else if prevMenu == nil && nextMenu == nil && prevSpeech == nil && nextSpeech == nil && parentMenuId > 0 {
  414. var maxSort int
  415. maxSort, err = GetSpeechMenuMaxSort(parentMenuId)
  416. if err != nil {
  417. errMsg = "移动失败"
  418. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  419. return
  420. }
  421. speechMenu.Sort = maxSort + 1 //那就是排在组内最后一位
  422. speechMenu.ModifyTime = time.Now()
  423. updateCol = append(updateCol, "Sort", "ModifyTime")
  424. } else {
  425. firstMenu, tmpErr := menuOb.GetFirstByParentId(parentMenuId)
  426. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  427. errMsg = "移动失败"
  428. err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tmpErr.Error())
  429. return
  430. }
  431. if firstMenu != nil && firstMenu.Sort == 0 {
  432. updateSortStr := ` sort + 1 `
  433. _ = menuOb.UpdateSortByParentId(parentMenuId, firstMenu.SpeechRecognitionMenuId-1, 0, updateSortStr)
  434. _ = speechOb.UpdateSortByMenuId(parentMenuId, 0, 0, updateSortStr)
  435. } else {
  436. firstSpeech, tErr := speechOb.GetFirstByMenuId(parentMenuId)
  437. if tErr != nil && !utils.IsErrNoRow(tErr) {
  438. errMsg = "移动失败"
  439. err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tErr.Error())
  440. return
  441. }
  442. if firstSpeech != nil && firstSpeech.Sort == 0 {
  443. updateSortStr := ` sort + 1 `
  444. _ = speechOb.UpdateSortByMenuId(parentMenuId, 0, firstSpeech.SpeechRecognitionId-1, updateSortStr)
  445. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, 0, updateSortStr)
  446. }
  447. }
  448. speechMenu.Sort = 0 //那就是排在第一位
  449. speechMenu.ModifyTime = time.Now()
  450. updateCol = append(updateCol, "Sort", "ModifyTime")
  451. }
  452. if len(updateCol) > 0 {
  453. err = speechMenu.Update(updateCol)
  454. if err != nil {
  455. errMsg = "移动失败"
  456. err = errors.New("修改失败,Err:" + err.Error())
  457. return
  458. }
  459. if oldParentId != parentMenuId {
  460. if len(menuIds) > 0 {
  461. levelStep := speechMenu.Level - oldLevel
  462. err = menuOb.UpdateChildByParentMenuId(menuIds, speechMenu.RootId, levelStep)
  463. if err != nil {
  464. errMsg = "移动失败"
  465. err = errors.New("更新子目录失败,Err:" + err.Error())
  466. return
  467. }
  468. }
  469. }
  470. }
  471. } else {
  472. if speechItem == nil {
  473. errMsg = "当前语音识别不存在"
  474. err = errors.New(errMsg)
  475. return
  476. }
  477. if speechItem.MenuId != parentMenuId {
  478. speechItem.MenuId = parentMenuId
  479. speechItem.ModifyTime = time.Now()
  480. updateCol = append(updateCol, speech_recognition.SpeechRecognitionCols.MenuId, speech_recognition.SpeechRecognitionCols.ModifyTime)
  481. }
  482. if prevSort > 0 {
  483. if nextSort > 0 {
  484. if prevSort == nextSort || prevSort == speechItem.Sort {
  485. updateSortStr := `sort + 2`
  486. if prevMenu != nil {
  487. _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionMenuId, prevMenu.Sort, updateSortStr)
  488. } else {
  489. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
  490. }
  491. if prevSpeech != nil {
  492. _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, prevSpeech.SpeechRecognitionId, updateSortStr)
  493. } else {
  494. _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
  495. }
  496. } else {
  497. if nextSort-prevSort == 1 {
  498. updateSortStr := `sort + 1`
  499. if prevMenu != nil {
  500. _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionMenuId, prevSort, updateSortStr)
  501. } else {
  502. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
  503. }
  504. if prevSpeech != nil {
  505. _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, prevSpeech.SpeechRecognitionId, updateSortStr)
  506. } else {
  507. _ = speechOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
  508. }
  509. }
  510. }
  511. }
  512. speechItem.Sort = prevSort + 1
  513. speechItem.ModifyTime = time.Now()
  514. updateCol = append(updateCol, "Sort", "ModifyTime")
  515. } else if prevMenu == nil && nextMenu == nil && prevSpeech == nil && nextSpeech == nil && parentMenuId > 0 {
  516. var maxSort int
  517. maxSort, err = GetSpeechMenuMaxSort(parentMenuId)
  518. if err != nil {
  519. errMsg = "移动失败"
  520. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  521. return
  522. }
  523. speechItem.Sort = maxSort + 1 //那就是排在组内最后一位
  524. speechItem.ModifyTime = time.Now()
  525. updateCol = append(updateCol, "Sort", "ModifyTime")
  526. } else {
  527. firstMenu, tmpErr := menuOb.GetFirstByParentId(parentMenuId)
  528. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  529. errMsg = "移动失败"
  530. err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tmpErr.Error())
  531. return
  532. }
  533. if firstMenu != nil && firstMenu.Sort == 0 {
  534. updateSortStr := ` sort + 1 `
  535. _ = menuOb.UpdateSortByParentId(parentMenuId, firstMenu.SpeechRecognitionMenuId-1, 0, updateSortStr)
  536. _ = speechOb.UpdateSortByMenuId(parentMenuId, 0, 0, updateSortStr)
  537. } else {
  538. firstSpeech, tErr := speechOb.GetFirstByMenuId(parentMenuId)
  539. if tErr != nil && !utils.IsErrNoRow(tErr) {
  540. errMsg = "移动失败"
  541. err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tErr.Error())
  542. return
  543. }
  544. if firstSpeech != nil && firstSpeech.Sort == 0 {
  545. updateSortStr := ` sort + 1 `
  546. _ = speechOb.UpdateSortByMenuId(parentMenuId, 0, firstSpeech.SpeechRecognitionId-1, updateSortStr)
  547. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, 0, updateSortStr)
  548. }
  549. }
  550. speechItem.Sort = 0 //那就是排在第一位
  551. speechItem.ModifyTime = time.Now()
  552. updateCol = append(updateCol, "Sort", "ModifyTime")
  553. }
  554. if len(updateCol) > 0 {
  555. err = speechItem.Update(updateCol)
  556. if err != nil {
  557. errMsg = "移动失败"
  558. err = errors.New("修改失败,Err:" + err.Error())
  559. return
  560. }
  561. }
  562. }
  563. return
  564. }
  565. func GetChildSpeechMenuByMenuId(menuId int) (targetList []*speech_recognition.SpeechRecognitionMenu, err error, errMsg string) {
  566. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  567. speechMenu, err := menuOb.GetItemById(menuId)
  568. if err != nil {
  569. if utils.IsErrNoRow(err) {
  570. errMsg = "当前目录不存在"
  571. err = errors.New(errMsg)
  572. return
  573. }
  574. errMsg = "获取失败"
  575. err = errors.New("获取目录信息失败,Err:" + err.Error())
  576. return
  577. }
  578. cond := fmt.Sprintf(" AND %s = ?", speech_recognition.SpeechRecognitionMenuCols.RootId)
  579. pars := make([]interface{}, 0)
  580. pars = append(pars, speechMenu.RootId)
  581. order := fmt.Sprintf("%s ASC, %s ASC, %s ASC", speech_recognition.SpeechRecognitionMenuCols.Level, speech_recognition.SpeechRecognitionMenuCols.Sort, speech_recognition.SpeechRecognitionMenuCols.SpeechRecognitionMenuId)
  582. tmpList, err := speechMenu.GetItemsByCondition(cond, pars, []string{}, order)
  583. if err != nil && !utils.IsErrNoRow(err) {
  584. errMsg = "获取失败"
  585. err = errors.New("获取数据失败,Err:" + err.Error())
  586. return
  587. }
  588. idMap := make(map[int]struct{})
  589. if len(tmpList) > 0 {
  590. for _, v := range tmpList {
  591. if v.SpeechRecognitionMenuId == speechMenu.SpeechRecognitionMenuId {
  592. idMap[v.SpeechRecognitionMenuId] = struct{}{}
  593. }
  594. }
  595. for _, v := range tmpList {
  596. if _, ok := idMap[v.ParentId]; ok {
  597. idMap[v.SpeechRecognitionMenuId] = struct{}{}
  598. }
  599. }
  600. for _, v := range tmpList {
  601. if _, ok := idMap[v.SpeechRecognitionMenuId]; ok {
  602. targetList = append(targetList, v)
  603. }
  604. }
  605. }
  606. return
  607. }
  608. func GetSpeechMenuMaxSort(parentId int) (maxSort int, err error) {
  609. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  610. speechOb := new(speech_recognition.SpeechRecognition)
  611. menuMax, err := menuOb.GetMaxSortByParentId(parentId)
  612. if err != nil {
  613. return
  614. }
  615. maxSort = menuMax
  616. speechMax, err := speechOb.GetMaxSortByMenuId(parentId)
  617. if err != nil {
  618. return
  619. }
  620. if maxSort < speechMax {
  621. maxSort = speechMax
  622. }
  623. return
  624. }
  625. func MoveSpeechTagMenu(req speech_recognition.SpeechRecognitionTagMenuMoveReq) (err error, errMsg string) {
  626. menuId := req.MenuId
  627. parentMenuId := req.ParentMenuId
  628. prevMenuId := req.PrevMenuId
  629. nextMenuId := req.NextMenuId
  630. tagId := req.TagId
  631. prevTagId := req.PrevTagId
  632. nextTagId := req.NextTagId
  633. menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
  634. tagOb := new(speech_recognition.SpeechRecognitionTag)
  635. var parentTagMenu *speech_recognition.SpeechRecognitionTagMenu
  636. if parentMenuId > 0 {
  637. t, e := menuOb.GetItemById(parentMenuId)
  638. if e != nil {
  639. errMsg = "移动失败"
  640. err = errors.New("获取上级目录信息失败,Err:" + err.Error())
  641. return
  642. }
  643. parentTagMenu = t
  644. }
  645. var (
  646. tagMenu *speech_recognition.SpeechRecognitionTagMenu
  647. prevMenu *speech_recognition.SpeechRecognitionTagMenu
  648. nextMenu *speech_recognition.SpeechRecognitionTagMenu
  649. tagItem *speech_recognition.SpeechRecognitionTag
  650. prevTag *speech_recognition.SpeechRecognitionTag
  651. nextTag *speech_recognition.SpeechRecognitionTag
  652. prevSort int
  653. nextSort int
  654. )
  655. if tagId == 0 {
  656. tagMenu, err = menuOb.GetItemById(menuId)
  657. if err != nil {
  658. if utils.IsErrNoRow(err) {
  659. errMsg = "当前目录不存在"
  660. err = errors.New("获取目录信息失败,Err:" + err.Error())
  661. return
  662. }
  663. errMsg = "移动失败"
  664. err = errors.New("获取目录信息失败,Err:" + err.Error())
  665. return
  666. }
  667. if parentMenuId > 0 && parentTagMenu.Level == 3 {
  668. errMsg = "最高只支持添加3级目录"
  669. err = errors.New(errMsg)
  670. return
  671. }
  672. {
  673. cond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionTagMenuCols.MenuName, speech_recognition.SpeechRecognitionTagMenuCols.ParentId, speech_recognition.SpeechRecognitionTagMenuCols.SpeechRecognitionTagMenuId)
  674. pars := make([]interface{}, 0)
  675. pars = append(pars, tagMenu.MenuName, parentMenuId, menuId)
  676. exists, e := menuOb.GetItemByCondition(cond, pars, "")
  677. if e != nil && !utils.IsErrNoRow(e) {
  678. errMsg = "移动失败"
  679. err = fmt.Errorf("获取父级目录下的同名目录失败, Err: %s", e.Error())
  680. return
  681. }
  682. if exists != nil && exists.SpeechRecognitionTagMenuId > 0 {
  683. errMsg = "移动失败,目录名称已存在"
  684. return
  685. }
  686. }
  687. } else {
  688. tagItem, err = tagOb.GetItemById(tagId)
  689. if err != nil {
  690. if utils.IsErrNoRow(err) {
  691. errMsg = "当前标签不存在"
  692. err = errors.New("获取目录信息失败,Err:" + err.Error())
  693. return
  694. }
  695. errMsg = "移动失败"
  696. err = errors.New("获取目录信息失败,Err:" + err.Error())
  697. return
  698. }
  699. if parentMenuId == 0 {
  700. errMsg = "移动失败,标签必须挂在目录下"
  701. err = errors.New(errMsg)
  702. return
  703. }
  704. }
  705. if prevMenuId > 0 {
  706. prevMenu, err = menuOb.GetItemById(prevMenuId)
  707. if err != nil {
  708. errMsg = "移动失败"
  709. err = errors.New("获取上一个兄弟节点目录信息失败,Err:" + err.Error())
  710. return
  711. }
  712. prevSort = prevMenu.Sort
  713. } else if prevTagId > 0 {
  714. prevTag, err = tagOb.GetItemById(prevTagId)
  715. if err != nil {
  716. errMsg = "移动失败"
  717. err = errors.New("获取上一个兄弟节点信息失败,Err:" + err.Error())
  718. return
  719. }
  720. prevSort = prevTag.Sort
  721. }
  722. if nextMenuId > 0 {
  723. nextMenu, err = menuOb.GetItemById(nextMenuId)
  724. if err != nil {
  725. errMsg = "移动失败"
  726. err = errors.New("获取下一个兄弟节点目录信息失败,Err:" + err.Error())
  727. return
  728. }
  729. nextSort = nextMenu.Sort
  730. } else if nextTagId > 0 {
  731. nextTag, err = tagOb.GetItemById(nextTagId)
  732. if err != nil {
  733. errMsg = "移动失败"
  734. err = errors.New("获取下一个兄弟节点目录信息失败,Err:" + err.Error())
  735. return
  736. }
  737. nextSort = nextTag.Sort
  738. }
  739. err, errMsg = moveSpeechTagMenu(parentTagMenu, tagMenu, prevMenu, nextMenu, tagItem, prevTag, nextTag, parentMenuId, prevSort, nextSort)
  740. return
  741. }
  742. func moveSpeechTagMenu(parentTagMenu, tagMenu, prevMenu, nextMenu *speech_recognition.SpeechRecognitionTagMenu, tagItem, prevTag, nextTag *speech_recognition.SpeechRecognitionTag, parentMenuId int, prevSort, nextSort int) (err error, errMsg string) {
  743. updateCol := make([]string, 0)
  744. menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
  745. tagOb := new(speech_recognition.SpeechRecognitionTag)
  746. if tagMenu != nil {
  747. oldParentId := tagMenu.ParentId
  748. oldLevel := tagMenu.Level
  749. var menuIds []int
  750. if oldParentId != parentMenuId {
  751. childList, e, m := GetChildSpeechTagMenuByMenuId(tagMenu.SpeechRecognitionTagMenuId)
  752. if e != nil {
  753. errMsg = "移动失败"
  754. err = errors.New("查询子目录失败,Err:" + e.Error() + m)
  755. return
  756. }
  757. if len(childList) > 0 {
  758. for _, v := range childList {
  759. if v.SpeechRecognitionTagMenuId == tagMenu.SpeechRecognitionTagMenuId {
  760. continue
  761. }
  762. menuIds = append(menuIds, v.SpeechRecognitionTagMenuId)
  763. }
  764. }
  765. }
  766. if tagMenu.ParentId != parentMenuId && parentMenuId != 0 {
  767. if tagMenu.Level != parentTagMenu.Level+1 { //禁止层级调整
  768. errMsg = "移动失败"
  769. err = errors.New("不支持目录层级变更")
  770. return
  771. }
  772. tagMenu.ParentId = parentTagMenu.SpeechRecognitionTagMenuId
  773. tagMenu.RootId = parentTagMenu.RootId
  774. tagMenu.Level = parentTagMenu.Level + 1
  775. tagMenu.ModifyTime = time.Now()
  776. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  777. } else if tagMenu.ParentId != parentMenuId && parentMenuId == 0 {
  778. errMsg = "移动失败"
  779. err = errors.New("不支持目录层级变更")
  780. return
  781. }
  782. if prevSort > 0 {
  783. if nextSort > 0 {
  784. if prevSort == nextSort || prevSort == tagMenu.Sort {
  785. updateSortStr := `sort + 2`
  786. if prevMenu != nil {
  787. _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionTagMenuId, prevMenu.Sort, updateSortStr)
  788. } else {
  789. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
  790. }
  791. if prevTag != nil {
  792. _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, prevTag.SpeechRecognitionTagId, updateSortStr)
  793. } else {
  794. _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
  795. }
  796. } else {
  797. if nextSort-prevSort == 1 {
  798. updateSortStr := `sort + 1`
  799. if prevMenu != nil {
  800. _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionTagMenuId, prevSort, updateSortStr)
  801. } else {
  802. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
  803. }
  804. if prevTag != nil {
  805. _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, prevTag.SpeechRecognitionTagId, updateSortStr)
  806. } else {
  807. _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
  808. }
  809. }
  810. }
  811. }
  812. tagMenu.Sort = prevSort + 1
  813. tagMenu.ModifyTime = time.Now()
  814. updateCol = append(updateCol, "Sort", "ModifyTime")
  815. } else if prevMenu == nil && nextMenu == nil && prevTag == nil && nextTag == nil && parentMenuId > 0 {
  816. var maxSort int
  817. maxSort, err = GetSpeechTagMenuMaxSort(parentMenuId)
  818. if err != nil {
  819. errMsg = "移动失败"
  820. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  821. return
  822. }
  823. tagMenu.Sort = maxSort + 1 //那就是排在组内最后一位
  824. tagMenu.ModifyTime = time.Now()
  825. updateCol = append(updateCol, "Sort", "ModifyTime")
  826. } else {
  827. firstTagMenu, tmpErr := menuOb.GetFirstByParentId(parentMenuId)
  828. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  829. errMsg = "移动失败"
  830. err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tmpErr.Error())
  831. return
  832. }
  833. if firstTagMenu != nil && firstTagMenu.Sort == 0 {
  834. updateSortStr := ` sort + 1 `
  835. _ = menuOb.UpdateSortByParentId(parentMenuId, firstTagMenu.SpeechRecognitionTagMenuId-1, 0, updateSortStr)
  836. _ = tagOb.UpdateSortByMenuId(parentMenuId, 0, 0, updateSortStr)
  837. } else {
  838. firstTag, tErr := tagOb.GetFirstByMenuId(parentMenuId)
  839. if tErr != nil && !utils.IsErrNoRow(tErr) {
  840. errMsg = "移动失败"
  841. err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tErr.Error())
  842. return
  843. }
  844. if firstTag != nil && firstTag.Sort == 0 {
  845. updateSortStr := ` sort + 1 `
  846. _ = tagOb.UpdateSortByMenuId(parentMenuId, 0, firstTag.SpeechRecognitionTagId-1, updateSortStr)
  847. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, 0, updateSortStr)
  848. }
  849. }
  850. tagMenu.Sort = 0 //那就是排在第一位
  851. tagMenu.ModifyTime = time.Now()
  852. updateCol = append(updateCol, "Sort", "ModifyTime")
  853. }
  854. if len(updateCol) > 0 {
  855. err = tagMenu.Update(updateCol)
  856. if err != nil {
  857. errMsg = "移动失败"
  858. err = errors.New("修改失败,Err:" + err.Error())
  859. return
  860. }
  861. if oldParentId != parentMenuId {
  862. if len(menuIds) > 0 {
  863. levelStep := tagMenu.Level - oldLevel
  864. err = menuOb.UpdateChildByParentMenuId(menuIds, tagMenu.RootId, levelStep)
  865. if err != nil {
  866. errMsg = "移动失败"
  867. err = errors.New("更新子目录失败,Err:" + err.Error())
  868. return
  869. }
  870. }
  871. }
  872. }
  873. } else {
  874. if tagItem == nil {
  875. errMsg = "当前标签不存在"
  876. err = errors.New(errMsg)
  877. return
  878. }
  879. if tagItem.MenuId != parentMenuId {
  880. tagItem.MenuId = parentMenuId
  881. tagItem.ModifyTime = time.Now()
  882. updateCol = append(updateCol, speech_recognition.SpeechRecognitionTagCols.MenuId, speech_recognition.SpeechRecognitionTagCols.ModifyTime)
  883. }
  884. if prevSort > 0 {
  885. if nextSort > 0 {
  886. if prevSort == nextSort || prevSort == tagItem.Sort {
  887. updateSortStr := `sort + 2`
  888. if prevMenu != nil {
  889. _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionTagMenuId, prevMenu.Sort, updateSortStr)
  890. } else {
  891. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
  892. }
  893. if prevTag != nil {
  894. _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, prevTag.SpeechRecognitionTagId, updateSortStr)
  895. } else {
  896. _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
  897. }
  898. } else {
  899. if nextSort-prevSort == 1 {
  900. updateSortStr := `sort + 1`
  901. if prevMenu != nil {
  902. _ = menuOb.UpdateSortByParentId(parentMenuId, prevMenu.SpeechRecognitionTagMenuId, prevSort, updateSortStr)
  903. } else {
  904. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, prevSort, updateSortStr)
  905. }
  906. if prevTag != nil {
  907. _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, prevTag.SpeechRecognitionTagId, updateSortStr)
  908. } else {
  909. _ = tagOb.UpdateSortByMenuId(parentMenuId, prevSort, 0, updateSortStr)
  910. }
  911. }
  912. }
  913. }
  914. tagItem.Sort = prevSort + 1
  915. tagItem.ModifyTime = time.Now()
  916. updateCol = append(updateCol, "Sort", "ModifyTime")
  917. } else if prevMenu == nil && nextMenu == nil && prevTag == nil && nextTag == nil && parentMenuId > 0 {
  918. var maxSort int
  919. maxSort, err = GetSpeechTagMenuMaxSort(parentMenuId)
  920. if err != nil {
  921. errMsg = "移动失败"
  922. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  923. return
  924. }
  925. tagItem.Sort = maxSort + 1 //那就是排在组内最后一位
  926. tagItem.ModifyTime = time.Now()
  927. updateCol = append(updateCol, "Sort", "ModifyTime")
  928. } else {
  929. firstTagMenu, tmpErr := menuOb.GetFirstByParentId(parentMenuId)
  930. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  931. errMsg = "移动失败"
  932. err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tmpErr.Error())
  933. return
  934. }
  935. if firstTagMenu != nil && firstTagMenu.Sort == 0 {
  936. updateSortStr := ` sort + 1 `
  937. _ = menuOb.UpdateSortByParentId(parentMenuId, firstTagMenu.SpeechRecognitionTagMenuId-1, 0, updateSortStr)
  938. _ = tagOb.UpdateSortByMenuId(parentMenuId, 0, 0, updateSortStr)
  939. } else {
  940. firstTag, tErr := tagOb.GetFirstByMenuId(parentMenuId)
  941. if tErr != nil && !utils.IsErrNoRow(tErr) {
  942. errMsg = "移动失败"
  943. err = errors.New("获取获取当前父级目录下的排序第一条的目录信息失败,Err:" + tErr.Error())
  944. return
  945. }
  946. if firstTag != nil && firstTag.Sort == 0 {
  947. updateSortStr := ` sort + 1 `
  948. _ = tagOb.UpdateSortByMenuId(parentMenuId, 0, firstTag.SpeechRecognitionTagId-1, updateSortStr)
  949. _ = menuOb.UpdateSortByParentId(parentMenuId, 0, 0, updateSortStr)
  950. }
  951. }
  952. tagItem.Sort = 0 //那就是排在第一位
  953. tagItem.ModifyTime = time.Now()
  954. updateCol = append(updateCol, "Sort", "ModifyTime")
  955. }
  956. if len(updateCol) > 0 {
  957. err = tagItem.Update(updateCol)
  958. if err != nil {
  959. errMsg = "移动失败"
  960. err = errors.New("修改失败,Err:" + err.Error())
  961. return
  962. }
  963. }
  964. }
  965. return
  966. }
  967. func GetChildSpeechTagMenuByMenuId(menuId int) (targetList []*speech_recognition.SpeechRecognitionTagMenu, err error, errMsg string) {
  968. menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
  969. tagMenu, err := menuOb.GetItemById(menuId)
  970. if err != nil {
  971. if utils.IsErrNoRow(err) {
  972. errMsg = "当前目录不存在"
  973. err = errors.New(errMsg)
  974. return
  975. }
  976. errMsg = "获取失败"
  977. err = errors.New("获取目录信息失败,Err:" + err.Error())
  978. return
  979. }
  980. cond := fmt.Sprintf(" AND %s = ?", speech_recognition.SpeechRecognitionTagMenuCols.RootId)
  981. pars := make([]interface{}, 0)
  982. pars = append(pars, tagMenu.RootId)
  983. order := fmt.Sprintf("%s ASC, %s ASC, %s ASC", speech_recognition.SpeechRecognitionTagMenuCols.Level, speech_recognition.SpeechRecognitionTagMenuCols.Sort, speech_recognition.SpeechRecognitionTagMenuCols.SpeechRecognitionTagMenuId)
  984. tmpList, err := tagMenu.GetItemsByCondition(cond, pars, []string{}, order)
  985. if err != nil && !utils.IsErrNoRow(err) {
  986. errMsg = "获取失败"
  987. err = errors.New("获取数据失败,Err:" + err.Error())
  988. return
  989. }
  990. idMap := make(map[int]struct{})
  991. if len(tmpList) > 0 {
  992. for _, v := range tmpList {
  993. if v.SpeechRecognitionTagMenuId == tagMenu.SpeechRecognitionTagMenuId {
  994. idMap[v.SpeechRecognitionTagMenuId] = struct{}{}
  995. }
  996. }
  997. for _, v := range tmpList {
  998. if _, ok := idMap[v.ParentId]; ok {
  999. idMap[v.SpeechRecognitionTagMenuId] = struct{}{}
  1000. }
  1001. }
  1002. for _, v := range tmpList {
  1003. if _, ok := idMap[v.SpeechRecognitionTagMenuId]; ok {
  1004. targetList = append(targetList, v)
  1005. }
  1006. }
  1007. }
  1008. return
  1009. }
  1010. func GetSpeechTagMenuMaxSort(parentId int) (maxSort int, err error) {
  1011. menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
  1012. tagOb := new(speech_recognition.SpeechRecognitionTag)
  1013. menuMax, err := menuOb.GetMaxSortByParentId(parentId)
  1014. if err != nil {
  1015. return
  1016. }
  1017. maxSort = menuMax
  1018. speechMax, err := tagOb.GetMaxSortByMenuId(parentId)
  1019. if err != nil {
  1020. return
  1021. }
  1022. if maxSort < speechMax {
  1023. maxSort = speechMax
  1024. }
  1025. return
  1026. }
  1027. func GetSpeechRecognitionMenuChildrenRecursive(list []*speech_recognition.SpeechRecognitionMenu, parentId int) []int {
  1028. childIds := make([]int, 0)
  1029. for _, v := range list {
  1030. if v.ParentId == parentId {
  1031. childIds = append(childIds, v.SpeechRecognitionMenuId)
  1032. ids := GetSpeechRecognitionMenuChildrenRecursive(list, v.SpeechRecognitionMenuId)
  1033. if len(ids) > 0 {
  1034. childIds = append(childIds, ids...)
  1035. }
  1036. }
  1037. }
  1038. return childIds
  1039. }
  1040. func GetSpeechRecognitionTagMenuChildrenRecursive(list []*speech_recognition.SpeechRecognitionTagMenu, parentId int) []int {
  1041. childIds := make([]int, 0)
  1042. for _, v := range list {
  1043. if v.ParentId == parentId {
  1044. childIds = append(childIds, v.SpeechRecognitionTagMenuId)
  1045. ids := GetSpeechRecognitionTagMenuChildrenRecursive(list, v.SpeechRecognitionTagMenuId)
  1046. if len(ids) > 0 {
  1047. childIds = append(childIds, ids...)
  1048. }
  1049. }
  1050. }
  1051. return childIds
  1052. }
  1053. func CheckSpeechRecognitionMenuRemove(menuId int) (result *speech_recognition.SpeechRecognitionMenuRemoveCheckResp, menuIds []int, err error) {
  1054. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  1055. result = new(speech_recognition.SpeechRecognitionMenuRemoveCheckResp)
  1056. menus, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  1057. if e != nil {
  1058. err = fmt.Errorf("获取语音识别目录列表失败, err: %s", e.Error())
  1059. return
  1060. }
  1061. childIds := GetSpeechRecognitionMenuChildrenRecursive(menus, menuId)
  1062. menuIds = make([]int, 0)
  1063. menuIds = append(menuIds, menuId)
  1064. if len(childIds) > 0 {
  1065. menuIds = append(menuIds, childIds...)
  1066. }
  1067. speechOb := new(speech_recognition.SpeechRecognition)
  1068. cond := fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.MenuId, utils.GetOrmInReplace(len(menuIds)))
  1069. pars := make([]interface{}, 0)
  1070. pars = append(pars, menuIds)
  1071. count, e := speechOb.GetCountByCondition(cond, pars)
  1072. if e != nil {
  1073. err = fmt.Errorf("获取目录下的语音识别数失败, err: %s", e.Error())
  1074. return
  1075. }
  1076. if count > 0 {
  1077. result.CheckResult = SpeechMenuCheckRemoveTypeRefused
  1078. result.Tips = "该分类关联转写文件,删除失败!"
  1079. return
  1080. }
  1081. if len(childIds) > 0 {
  1082. result.CheckResult = SpeechMenuCheckRemoveTypeWarning
  1083. result.Tips = "该分类包含内容为空的子分类"
  1084. return
  1085. }
  1086. result.Tips = "校验通过,可以删除"
  1087. return
  1088. }
  1089. func CheckSpeechRecognitionTagMenuRemove(menuId int) (result *speech_recognition.SpeechRecognitionMenuRemoveCheckResp, menuIds []int, err error) {
  1090. menuOb := new(speech_recognition.SpeechRecognitionTagMenu)
  1091. result = new(speech_recognition.SpeechRecognitionMenuRemoveCheckResp)
  1092. menus, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  1093. if e != nil {
  1094. err = fmt.Errorf("获取标签目录列表失败, err: %s", e.Error())
  1095. return
  1096. }
  1097. childIds := GetSpeechRecognitionTagMenuChildrenRecursive(menus, menuId)
  1098. menuIds = make([]int, 0)
  1099. menuIds = append(menuIds, menuId)
  1100. if len(childIds) > 0 {
  1101. menuIds = append(menuIds, childIds...)
  1102. }
  1103. tagOb := new(speech_recognition.SpeechRecognitionTag)
  1104. cond := fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionTagCols.MenuId, utils.GetOrmInReplace(len(menuIds)))
  1105. pars := make([]interface{}, 0)
  1106. pars = append(pars, menuIds)
  1107. count, e := tagOb.GetCountByCondition(cond, pars)
  1108. if e != nil {
  1109. err = fmt.Errorf("获取目录下的标签数失败, err: %s", e.Error())
  1110. return
  1111. }
  1112. if count > 0 {
  1113. result.CheckResult = SpeechMenuCheckRemoveTypeRefused
  1114. result.Tips = "该分类关联标签,删除失败!"
  1115. return
  1116. }
  1117. if len(childIds) > 0 {
  1118. result.CheckResult = SpeechMenuCheckRemoveTypeWarning
  1119. result.Tips = "该分类包含内容为空的子分类"
  1120. return
  1121. }
  1122. result.Tips = "校验通过,可以删除"
  1123. return
  1124. }
  1125. func GetSpeechRecognitionMenuPathRecursive(list []*speech_recognition.SpeechRecognitionMenu, menuId int) []*speech_recognition.SpeechRecognitionMenuItem {
  1126. res := make([]*speech_recognition.SpeechRecognitionMenuItem, 0)
  1127. for _, v := range list {
  1128. if v.SpeechRecognitionMenuId == menuId {
  1129. t := new(speech_recognition.SpeechRecognitionMenuItem)
  1130. t.UniqueCode = v.UniqueCode
  1131. t.MenuId = v.SpeechRecognitionMenuId
  1132. t.MenuName = v.MenuName
  1133. t.ParentId = v.ParentId
  1134. t.Level = v.Level
  1135. t.Sort = v.Sort
  1136. t.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
  1137. if v.ParentId > 0 {
  1138. res = GetSpeechRecognitionMenuPathRecursive(list, v.ParentId)
  1139. }
  1140. res = append(res, t)
  1141. }
  1142. }
  1143. return res
  1144. }