speech_recognition.go 48 KB

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