speech_recognition_menu.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. package speech_recognition
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/speech_recognition"
  7. "eta/eta_api/services"
  8. "eta/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. if menuItem != nil && menuItem.SpeechRecognitionMenuId <= 0 {
  231. br.Ret = 200
  232. br.Success = true
  233. br.Msg = "操作成功"
  234. return
  235. }
  236. // 删除校验
  237. checkResult, menuIds, e := services.CheckSpeechRecognitionMenuRemove(menuItem.SpeechRecognitionMenuId)
  238. if e != nil {
  239. br.Msg = "操作失败"
  240. br.ErrMsg = "目录删除校验失败, Err: " + e.Error()
  241. return
  242. }
  243. switch checkResult.CheckResult {
  244. case services.SpeechMenuCheckRemoveTypePass:
  245. // 可删除
  246. if e = menuItem.Del(); e != nil {
  247. br.Msg = "操作失败"
  248. br.ErrMsg = "删除目录失败, Err: " + e.Error()
  249. return
  250. }
  251. case services.SpeechMenuCheckRemoveTypeRefused:
  252. // 不可删除
  253. br.Msg = checkResult.Tips
  254. return
  255. case services.SpeechMenuCheckRemoveTypeWarning:
  256. // 删除目录及子目录
  257. if e = menuOb.MultiDel(menuIds); e != nil {
  258. br.Msg = "操作失败"
  259. br.ErrMsg = "批量删除目录失败, Err: " + e.Error()
  260. return
  261. }
  262. }
  263. br.Ret = 200
  264. br.Success = true
  265. br.Msg = "操作成功"
  266. }
  267. // RemoveCheck
  268. // @Title 删除校验
  269. // @Description 删除校验
  270. // @Param request body speech_recognition.SpeechRecognitionMenuRemoveReq true "type json string"
  271. // @Success 200 string "操作成功"
  272. // @router /menu/remove_check [post]
  273. func (this *SpeechRecognitionMenuController) RemoveCheck() {
  274. br := new(models.BaseResponse).Init()
  275. defer func() {
  276. if br.ErrMsg == "" {
  277. br.IsSendEmail = false
  278. }
  279. this.Data["json"] = br
  280. this.ServeJSON()
  281. }()
  282. sysUser := this.SysUser
  283. if sysUser == nil {
  284. br.Msg = "请登录"
  285. br.ErrMsg = "请登录,SysUser Is Empty"
  286. br.Ret = 408
  287. return
  288. }
  289. var req speech_recognition.SpeechRecognitionMenuRemoveReq
  290. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  291. br.Msg = "参数有误"
  292. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  293. return
  294. }
  295. if req.MenuId <= 0 {
  296. br.Msg = "参数有误"
  297. br.ErrMsg = fmt.Sprintf("参数有误, MenuId: %d", req.MenuId)
  298. return
  299. }
  300. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  301. menuItem, e := menuOb.GetItemById(req.MenuId)
  302. if e != nil {
  303. if utils.IsErrNoRow(e) {
  304. br.Ret = 200
  305. br.Success = true
  306. br.Msg = "操作成功"
  307. return
  308. }
  309. br.Msg = "操作失败"
  310. br.ErrMsg = "获取目录失败, Err: " + e.Error()
  311. return
  312. }
  313. resp, _, e := services.CheckSpeechRecognitionMenuRemove(menuItem.SpeechRecognitionMenuId)
  314. if e != nil {
  315. br.Msg = "操作失败"
  316. br.ErrMsg = "目录删除校验失败, Err: " + e.Error()
  317. return
  318. }
  319. br.Data = resp
  320. br.Ret = 200
  321. br.Success = true
  322. br.Msg = "操作成功"
  323. }
  324. // List
  325. // @Title 目录列表
  326. // @Description 目录列表
  327. // @Param ParentId query int false "父级ID"
  328. // @Success 200 {object} speech_recognition.SpeechRecognitionMenuNodeItem
  329. // @router /menu/list [get]
  330. func (this *SpeechRecognitionMenuController) List() {
  331. br := new(models.BaseResponse).Init()
  332. defer func() {
  333. if br.ErrMsg == "" {
  334. br.IsSendEmail = false
  335. }
  336. this.Data["json"] = br
  337. this.ServeJSON()
  338. }()
  339. sysUser := this.SysUser
  340. if sysUser == nil {
  341. br.Msg = "请登录"
  342. br.ErrMsg = "请登录,SysUser Is Empty"
  343. br.Ret = 408
  344. return
  345. }
  346. // 前端采用懒加载, 所以只查询子目录和内容
  347. parentId, _ := this.GetInt("ParentId")
  348. level := 0
  349. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  350. if parentId > 0 {
  351. parentMenu, e := menuOb.GetItemById(parentId)
  352. if e != nil {
  353. br.Msg = "获取失败"
  354. br.ErrMsg = "获取父级目录失败, Err: " + e.Error()
  355. return
  356. }
  357. level = parentMenu.Level + 1
  358. }
  359. menus := make([]*speech_recognition.SpeechRecognitionMenu, 0)
  360. {
  361. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionMenuCols.ParentId)
  362. pars := make([]interface{}, 0)
  363. pars = append(pars, parentId)
  364. list, e := menuOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionMenuCols.Sort))
  365. if e != nil {
  366. br.Msg = "获取失败"
  367. br.ErrMsg = "获取目录列表失败, Err: " + e.Error()
  368. return
  369. }
  370. menus = list
  371. }
  372. speeches := make([]*speech_recognition.SpeechRecognition, 0)
  373. {
  374. speechOb := new(speech_recognition.SpeechRecognition)
  375. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, speech_recognition.SpeechRecognitionCols.MenuId, speech_recognition.SpeechRecognitionCols.State)
  376. pars := make([]interface{}, 0)
  377. pars = append(pars, parentId, speech_recognition.SpeechRecognitionStateSuccess)
  378. list, e := speechOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s DESC", speech_recognition.SpeechRecognitionCols.Sort, speech_recognition.SpeechRecognitionCols.CreateTime))
  379. if e != nil {
  380. br.Msg = "获取失败"
  381. br.ErrMsg = "获取语音识别列表失败, Err: " + e.Error()
  382. return
  383. }
  384. speeches = list
  385. }
  386. resp := make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0)
  387. if len(menus) == 0 && len(speeches) == 0 {
  388. br.Data = resp
  389. br.Ret = 200
  390. br.Success = true
  391. br.Msg = "获取成功"
  392. return
  393. }
  394. for _, v := range menus {
  395. resp = append(resp, &speech_recognition.SpeechRecognitionMenuNodeItem{
  396. UniqueCode: v.UniqueCode,
  397. NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeDefault,
  398. MenuId: v.SpeechRecognitionMenuId,
  399. MenuName: v.MenuName,
  400. ParentId: v.ParentId,
  401. Level: v.Level,
  402. Sort: v.Sort,
  403. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  404. })
  405. }
  406. for _, v := range speeches {
  407. resp = append(resp, &speech_recognition.SpeechRecognitionMenuNodeItem{
  408. UniqueCode: v.UniqueCode,
  409. NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeSpeech,
  410. SpeechRecognitionId: v.SpeechRecognitionId,
  411. SpeechRecognitionName: v.FileName,
  412. ParentId: v.MenuId,
  413. Level: level,
  414. Sort: v.Sort,
  415. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  416. })
  417. }
  418. sort.Slice(resp, func(i, j int) bool {
  419. return resp[i].Sort < resp[j].Sort
  420. })
  421. br.Data = resp
  422. br.Ret = 200
  423. br.Success = true
  424. br.Msg = "获取成功"
  425. }
  426. // Tree
  427. // @Title 目录树
  428. // @Description 目录树
  429. // @Success 200 {object} speech_recognition.SpeechRecognitionMenuItem
  430. // @router /menu/tree [get]
  431. func (this *SpeechRecognitionMenuController) Tree() {
  432. br := new(models.BaseResponse).Init()
  433. defer func() {
  434. if br.ErrMsg == "" {
  435. br.IsSendEmail = false
  436. }
  437. this.Data["json"] = br
  438. this.ServeJSON()
  439. }()
  440. sysUser := this.SysUser
  441. if sysUser == nil {
  442. br.Msg = "请登录"
  443. br.ErrMsg = "请登录,SysUser Is Empty"
  444. br.Ret = 408
  445. return
  446. }
  447. menus := make([]*speech_recognition.SpeechRecognitionMenu, 0)
  448. {
  449. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  450. list, e := menuOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, fmt.Sprintf("%s ASC, %s ASC", speech_recognition.SpeechRecognitionMenuCols.ParentId, speech_recognition.SpeechRecognitionMenuCols.Sort))
  451. if e != nil {
  452. br.Msg = "获取失败"
  453. br.ErrMsg = "获取目录列表失败, Err: " + e.Error()
  454. return
  455. }
  456. menus = list
  457. }
  458. // 递归处理成目录树
  459. resp := services.GetSpeechRecognitionMenuTreeRecursive(menus, 0)
  460. br.Data = resp
  461. br.Ret = 200
  462. br.Success = true
  463. br.Msg = "获取成功"
  464. }
  465. // Move
  466. // @Title 移动目录/语音识别
  467. // @Description 移动目录/语音识别
  468. // @Param request body speech_recognition.SpeechRecognitionMenuMoveReq true "type json string"
  469. // @Success 200 string "操作成功"
  470. // @router /menu/move [post]
  471. func (this *SpeechRecognitionMenuController) Move() {
  472. br := new(models.BaseResponse).Init()
  473. defer func() {
  474. if br.ErrMsg == "" {
  475. br.IsSendEmail = false
  476. }
  477. this.Data["json"] = br
  478. this.ServeJSON()
  479. }()
  480. sysUser := this.SysUser
  481. if sysUser == nil {
  482. br.Msg = "请登录"
  483. br.ErrMsg = "请登录,SysUser Is Empty"
  484. br.Ret = 408
  485. return
  486. }
  487. var req speech_recognition.SpeechRecognitionMenuMoveReq
  488. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  489. br.Msg = "参数有误"
  490. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  491. return
  492. }
  493. if req.MenuId <= 0 && req.SpeechId <= 0 {
  494. br.Msg = "请选择目录或语音识别"
  495. return
  496. }
  497. e, _ := services.MoveSpeechMenu(req)
  498. if e != nil {
  499. br.Msg = "操作失败"
  500. br.ErrMsg = "移动目录/语音识别失败, Err: " + e.Error()
  501. return
  502. }
  503. br.Ret = 200
  504. br.Success = true
  505. br.Msg = "操作成功"
  506. }