speech_recognition_menu.go 13 KB

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