speech_recognition_menu.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. package speech_recognition
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/speech_recognition"
  7. "eta_gn/eta_api/services"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type SpeechRecognitionMenuController struct {
  16. controllers.BaseAuthController
  17. }
  18. // Add
  19. // @Title 新增分类
  20. // @Description 新增分类
  21. // @Param request body speech_recognition.SpeechRecognitionMenuAddReq true "type json string"
  22. // @Success 200 string "操作成功"
  23. // @router /menu/add [post]
  24. func (this *SpeechRecognitionMenuController) Add() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. if br.ErrMsg == "" {
  28. br.IsSendEmail = false
  29. }
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. sysUser := this.SysUser
  34. if sysUser == nil {
  35. br.Msg = "请登录"
  36. br.ErrMsg = "请登录,SysUser Is Empty"
  37. br.Ret = 408
  38. return
  39. }
  40. var req speech_recognition.SpeechRecognitionMenuAddReq
  41. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  42. br.Msg = "参数有误"
  43. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  44. return
  45. }
  46. req.MenuName = strings.TrimSpace(req.MenuName)
  47. if req.MenuName == "" {
  48. br.Msg = "请输入目录名称"
  49. return
  50. }
  51. // 校验同级目录是否有重名
  52. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  53. {
  54. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ParentId)
  55. pars := make([]interface{}, 0)
  56. pars = append(pars, req.MenuName, req.ParentId)
  57. exists, e := menuOb.GetItemByCondition(cond, pars, "")
  58. if e != nil && !utils.IsErrNoRow(e) {
  59. br.Msg = "操作失败"
  60. br.ErrMsg = "获取同名目录失败, Err: " + e.Error()
  61. return
  62. }
  63. if exists != nil && exists.SpeechRecognitionMenuId > 0 {
  64. br.Msg = "分类名称已存在,请重新输入"
  65. return
  66. }
  67. }
  68. // 获取目录层级和同级最大排序
  69. level := 1
  70. rootId := 0
  71. {
  72. if req.ParentId > 0 {
  73. parentMenu, e := menuOb.GetItemById(req.ParentId)
  74. if e != nil {
  75. br.Msg = "操作失败"
  76. br.ErrMsg = "获取父级目录失败, Err: " + e.Error()
  77. return
  78. }
  79. level += parentMenu.Level
  80. rootId = parentMenu.RootId
  81. }
  82. }
  83. sortMax, e := services.GetSpeechMenuMaxSort(req.ParentId)
  84. if e != nil {
  85. br.Msg = "操作失败"
  86. br.ErrMsg = "获取语音识别目录下最大排序失败, Err: " + e.Error()
  87. return
  88. }
  89. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  90. menuOb.UniqueCode = utils.MD5(fmt.Sprintf("%s_%s", menuOb.TableName(), timestamp))
  91. menuOb.MenuName = req.MenuName
  92. menuOb.ParentId = req.ParentId
  93. menuOb.Level = level
  94. menuOb.Sort = sortMax + 1
  95. menuOb.RootId = rootId
  96. menuOb.CreateTime = time.Now().Local()
  97. menuOb.ModifyTime = time.Now().Local()
  98. if e = menuOb.Create(); e != nil {
  99. br.Msg = "操作失败"
  100. br.ErrMsg = "新增目录失败, Err: " + e.Error()
  101. return
  102. }
  103. br.Ret = 200
  104. br.Success = true
  105. br.Msg = "操作成功"
  106. }
  107. // Edit
  108. // @Title 编辑分类
  109. // @Description 编辑分类
  110. // @Param request body speech_recognition.SpeechRecognitionMenuEditReq true "type json string"
  111. // @Success 200 string "操作成功"
  112. // @router /menu/edit [post]
  113. func (this *SpeechRecognitionMenuController) Edit() {
  114. br := new(models.BaseResponse).Init()
  115. defer func() {
  116. if br.ErrMsg == "" {
  117. br.IsSendEmail = false
  118. }
  119. this.Data["json"] = br
  120. this.ServeJSON()
  121. }()
  122. sysUser := this.SysUser
  123. if sysUser == nil {
  124. br.Msg = "请登录"
  125. br.ErrMsg = "请登录,SysUser Is Empty"
  126. br.Ret = 408
  127. return
  128. }
  129. var req speech_recognition.SpeechRecognitionMenuEditReq
  130. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  131. br.Msg = "参数有误"
  132. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  133. return
  134. }
  135. if req.MenuId <= 0 {
  136. br.Msg = "参数有误"
  137. br.ErrMsg = fmt.Sprintf("参数有误, MenuId: %d", req.MenuId)
  138. return
  139. }
  140. req.MenuName = strings.TrimSpace(req.MenuName)
  141. if req.MenuName == "" {
  142. br.Msg = "请输入目录名称"
  143. return
  144. }
  145. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  146. menuItem, e := menuOb.GetItemById(req.MenuId)
  147. if e != nil {
  148. if utils.IsErrNoRow(e) {
  149. br.Msg = "目录不存在,请刷新页面"
  150. return
  151. }
  152. br.Msg = "操作失败"
  153. br.ErrMsg = "获取目录失败, Err: " + e.Error()
  154. return
  155. }
  156. // 校验同级目录是否有重名
  157. {
  158. cond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ParentId, speech_recognition.SpeechRecognitionMenuCols.SpeechRecognitionMenuId)
  159. pars := make([]interface{}, 0)
  160. pars = append(pars, req.MenuName, menuItem.ParentId, req.MenuId)
  161. exists, e := menuOb.GetItemByCondition(cond, pars, "")
  162. if e != nil && !utils.IsErrNoRow(e) {
  163. br.Msg = "操作失败"
  164. br.ErrMsg = "获取同名目录失败, Err: " + e.Error()
  165. return
  166. }
  167. if exists != nil && exists.SpeechRecognitionMenuId > 0 {
  168. br.Msg = "分类名称已存在,请重新输入"
  169. return
  170. }
  171. }
  172. menuItem.MenuName = req.MenuName
  173. menuItem.ModifyTime = time.Now().Local()
  174. updateCols := []string{speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ModifyTime}
  175. if e = menuItem.Update(updateCols); e != nil {
  176. br.Msg = "操作失败"
  177. br.ErrMsg = "更新目录失败, Err: " + e.Error()
  178. return
  179. }
  180. br.Ret = 200
  181. br.Success = true
  182. br.Msg = "操作成功"
  183. }
  184. // Remove
  185. // @Title 删除分类
  186. // @Description 删除分类
  187. // @Param request body speech_recognition.SpeechRecognitionMenuRemoveReq true "type json string"
  188. // @Success 200 string "操作成功"
  189. // @router /menu/remove [post]
  190. func (this *SpeechRecognitionMenuController) Remove() {
  191. br := new(models.BaseResponse).Init()
  192. defer func() {
  193. if br.ErrMsg == "" {
  194. br.IsSendEmail = false
  195. }
  196. this.Data["json"] = br
  197. this.ServeJSON()
  198. }()
  199. sysUser := this.SysUser
  200. if sysUser == nil {
  201. br.Msg = "请登录"
  202. br.ErrMsg = "请登录,SysUser Is Empty"
  203. br.Ret = 408
  204. return
  205. }
  206. var req speech_recognition.SpeechRecognitionMenuRemoveReq
  207. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  208. br.Msg = "参数有误"
  209. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  210. return
  211. }
  212. if req.MenuId <= 0 {
  213. br.Msg = "参数有误"
  214. br.ErrMsg = fmt.Sprintf("参数有误, MenuId: %d", req.MenuId)
  215. return
  216. }
  217. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  218. menuItem, e := menuOb.GetItemById(req.MenuId)
  219. if e != nil {
  220. if utils.IsErrNoRow(e) {
  221. br.Ret = 200
  222. br.Success = true
  223. br.Msg = "操作成功"
  224. return
  225. }
  226. br.Msg = "操作失败"
  227. br.ErrMsg = "获取目录失败, Err: " + e.Error()
  228. return
  229. }
  230. // 删除校验
  231. checkResult, menuIds, e := services.CheckSpeechRecognitionMenuRemove(menuItem.SpeechRecognitionMenuId)
  232. if e != nil {
  233. br.Msg = "操作失败"
  234. br.ErrMsg = "目录删除校验失败, Err: " + e.Error()
  235. return
  236. }
  237. switch checkResult.CheckResult {
  238. case services.SpeechMenuCheckRemoveTypePass:
  239. // 可删除
  240. if e = menuItem.Del(); e != nil {
  241. br.Msg = "操作失败"
  242. br.ErrMsg = "删除目录失败, Err: " + e.Error()
  243. return
  244. }
  245. case services.SpeechMenuCheckRemoveTypeRefused:
  246. // 不可删除
  247. br.Msg = checkResult.Tips
  248. return
  249. case services.SpeechMenuCheckRemoveTypeWarning:
  250. // 删除目录及子目录
  251. if e = menuOb.MultiDel(menuIds); e != nil {
  252. br.Msg = "操作失败"
  253. br.ErrMsg = "批量删除目录失败, Err: " + e.Error()
  254. return
  255. }
  256. }
  257. br.Ret = 200
  258. br.Success = true
  259. br.Msg = "操作成功"
  260. }
  261. // RemoveCheck
  262. // @Title 删除校验
  263. // @Description 删除校验
  264. // @Param request body speech_recognition.SpeechRecognitionMenuRemoveReq true "type json string"
  265. // @Success 200 string "操作成功"
  266. // @router /menu/remove_check [post]
  267. func (this *SpeechRecognitionMenuController) RemoveCheck() {
  268. br := new(models.BaseResponse).Init()
  269. defer func() {
  270. if br.ErrMsg == "" {
  271. br.IsSendEmail = false
  272. }
  273. this.Data["json"] = br
  274. this.ServeJSON()
  275. }()
  276. sysUser := this.SysUser
  277. if sysUser == nil {
  278. br.Msg = "请登录"
  279. br.ErrMsg = "请登录,SysUser Is Empty"
  280. br.Ret = 408
  281. return
  282. }
  283. var req speech_recognition.SpeechRecognitionMenuRemoveReq
  284. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  285. br.Msg = "参数有误"
  286. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  287. return
  288. }
  289. if req.MenuId <= 0 {
  290. br.Msg = "参数有误"
  291. br.ErrMsg = fmt.Sprintf("参数有误, MenuId: %d", req.MenuId)
  292. return
  293. }
  294. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  295. menuItem, e := menuOb.GetItemById(req.MenuId)
  296. if e != nil {
  297. if utils.IsErrNoRow(e) {
  298. br.Ret = 200
  299. br.Success = true
  300. br.Msg = "操作成功"
  301. return
  302. }
  303. br.Msg = "操作失败"
  304. br.ErrMsg = "获取目录失败, Err: " + e.Error()
  305. return
  306. }
  307. resp, _, e := services.CheckSpeechRecognitionMenuRemove(menuItem.SpeechRecognitionMenuId)
  308. if e != nil {
  309. br.Msg = "操作失败"
  310. br.ErrMsg = "目录删除校验失败, Err: " + e.Error()
  311. return
  312. }
  313. br.Data = resp
  314. br.Ret = 200
  315. br.Success = true
  316. br.Msg = "操作成功"
  317. }
  318. // List
  319. // @Title 目录列表
  320. // @Description 目录列表
  321. // @Param ParentId query int false "父级ID"
  322. // @Success 200 {object} speech_recognition.SpeechRecognitionMenuNodeItem
  323. // @router /menu/list [get]
  324. func (this *SpeechRecognitionMenuController) List() {
  325. br := new(models.BaseResponse).Init()
  326. defer func() {
  327. if br.ErrMsg == "" {
  328. br.IsSendEmail = false
  329. }
  330. this.Data["json"] = br
  331. this.ServeJSON()
  332. }()
  333. sysUser := this.SysUser
  334. if sysUser == nil {
  335. br.Msg = "请登录"
  336. br.ErrMsg = "请登录,SysUser Is Empty"
  337. br.Ret = 408
  338. return
  339. }
  340. // 前端采用懒加载, 所以只查询子目录和内容
  341. parentId, _ := this.GetInt("ParentId")
  342. level := 0
  343. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  344. if parentId > 0 {
  345. parentMenu, e := menuOb.GetItemById(parentId)
  346. if e != nil {
  347. br.Msg = "获取失败"
  348. br.ErrMsg = "获取父级目录失败, Err: " + e.Error()
  349. return
  350. }
  351. level = parentMenu.Level + 1
  352. }
  353. menus := make([]*speech_recognition.SpeechRecognitionMenu, 0)
  354. {
  355. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionMenuCols.ParentId)
  356. pars := make([]interface{}, 0)
  357. pars = append(pars, parentId)
  358. list, e := menuOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionMenuCols.Sort))
  359. if e != nil {
  360. br.Msg = "获取失败"
  361. br.ErrMsg = "获取目录列表失败, Err: " + e.Error()
  362. return
  363. }
  364. menus = list
  365. }
  366. speeches := make([]*speech_recognition.SpeechRecognition, 0)
  367. {
  368. speechOb := new(speech_recognition.SpeechRecognition)
  369. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, speech_recognition.SpeechRecognitionCols.MenuId, speech_recognition.SpeechRecognitionCols.State)
  370. pars := make([]interface{}, 0)
  371. pars = append(pars, parentId, speech_recognition.SpeechRecognitionStateSuccess)
  372. list, e := speechOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s DESC", speech_recognition.SpeechRecognitionCols.Sort, speech_recognition.SpeechRecognitionCols.CreateTime))
  373. if e != nil {
  374. br.Msg = "获取失败"
  375. br.ErrMsg = "获取语音识别列表失败, Err: " + e.Error()
  376. return
  377. }
  378. speeches = list
  379. }
  380. resp := make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0)
  381. if len(menus) == 0 && len(speeches) == 0 {
  382. br.Data = resp
  383. br.Ret = 200
  384. br.Success = true
  385. br.Msg = "获取成功"
  386. return
  387. }
  388. for _, v := range menus {
  389. resp = append(resp, &speech_recognition.SpeechRecognitionMenuNodeItem{
  390. UniqueCode: v.UniqueCode,
  391. NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeDefault,
  392. MenuId: v.SpeechRecognitionMenuId,
  393. MenuName: v.MenuName,
  394. ParentId: v.ParentId,
  395. Level: v.Level,
  396. Sort: v.Sort,
  397. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  398. })
  399. }
  400. for _, v := range speeches {
  401. resp = append(resp, &speech_recognition.SpeechRecognitionMenuNodeItem{
  402. UniqueCode: v.UniqueCode,
  403. NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeSpeech,
  404. SpeechRecognitionId: v.SpeechRecognitionId,
  405. SpeechRecognitionName: v.FileName,
  406. ParentId: v.MenuId,
  407. Level: level,
  408. Sort: v.Sort,
  409. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  410. })
  411. }
  412. sort.Slice(resp, func(i, j int) bool {
  413. return resp[i].Sort < resp[j].Sort
  414. })
  415. br.Data = resp
  416. br.Ret = 200
  417. br.Success = true
  418. br.Msg = "获取成功"
  419. }
  420. // Tree
  421. // @Title 目录树
  422. // @Description 目录树
  423. // @Success 200 {object} speech_recognition.SpeechRecognitionMenuItem
  424. // @router /menu/tree [get]
  425. func (this *SpeechRecognitionMenuController) Tree() {
  426. br := new(models.BaseResponse).Init()
  427. defer func() {
  428. if br.ErrMsg == "" {
  429. br.IsSendEmail = false
  430. }
  431. this.Data["json"] = br
  432. this.ServeJSON()
  433. }()
  434. sysUser := this.SysUser
  435. if sysUser == nil {
  436. br.Msg = "请登录"
  437. br.ErrMsg = "请登录,SysUser Is Empty"
  438. br.Ret = 408
  439. return
  440. }
  441. menus := make([]*speech_recognition.SpeechRecognitionMenu, 0)
  442. {
  443. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  444. list, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, fmt.Sprintf("%s ASC, %s ASC", speech_recognition.SpeechRecognitionMenuCols.ParentId, speech_recognition.SpeechRecognitionMenuCols.Sort))
  445. if e != nil {
  446. br.Msg = "获取失败"
  447. br.ErrMsg = "获取目录列表失败, Err: " + e.Error()
  448. return
  449. }
  450. menus = list
  451. }
  452. // 递归处理成目录树
  453. resp := services.GetSpeechRecognitionMenuTreeRecursive(menus, 0)
  454. br.Data = resp
  455. br.Ret = 200
  456. br.Success = true
  457. br.Msg = "获取成功"
  458. }
  459. // Move
  460. // @Title 移动目录/语音识别
  461. // @Description 移动目录/语音识别
  462. // @Param request body speech_recognition.SpeechRecognitionMenuMoveReq true "type json string"
  463. // @Success 200 string "操作成功"
  464. // @router /menu/move [post]
  465. func (this *SpeechRecognitionMenuController) Move() {
  466. br := new(models.BaseResponse).Init()
  467. defer func() {
  468. if br.ErrMsg == "" {
  469. br.IsSendEmail = false
  470. }
  471. this.Data["json"] = br
  472. this.ServeJSON()
  473. }()
  474. sysUser := this.SysUser
  475. if sysUser == nil {
  476. br.Msg = "请登录"
  477. br.ErrMsg = "请登录,SysUser Is Empty"
  478. br.Ret = 408
  479. return
  480. }
  481. var req speech_recognition.SpeechRecognitionMenuMoveReq
  482. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  483. br.Msg = "参数有误"
  484. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  485. return
  486. }
  487. if req.MenuId <= 0 && req.SpeechId <= 0 {
  488. br.Msg = "请选择目录或语音识别"
  489. return
  490. }
  491. e, errMsg := services.MoveSpeechMenu(req)
  492. if e != nil || errMsg != "" {
  493. br.Msg = "操作失败"
  494. br.ErrMsg = "移动目录/语音识别失败, Err: " + e.Error() + ";errMsg:" + errMsg
  495. return
  496. }
  497. br.Ret = 200
  498. br.Success = true
  499. br.Msg = "操作成功"
  500. }