speech_recognition_menu.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/utils"
  8. "fmt"
  9. "strings"
  10. "time"
  11. )
  12. type SpeechRecognitionMenuController struct {
  13. controllers.BaseAuthController
  14. }
  15. // Add
  16. // @Title 新增分类
  17. // @Description 新增分类
  18. // @Param request body speech_recognition.SpeechRecognitionMenuAddReq true "type json string"
  19. // @Success 200 string "操作成功"
  20. // @router /menu/add [post]
  21. func (this *SpeechRecognitionMenuController) Add() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. if br.ErrMsg == "" {
  25. br.IsSendEmail = false
  26. }
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,SysUser Is Empty"
  34. br.Ret = 408
  35. return
  36. }
  37. var req speech_recognition.SpeechRecognitionMenuAddReq
  38. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  39. br.Msg = "参数有误"
  40. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  41. return
  42. }
  43. req.MenuName = strings.TrimSpace(req.MenuName)
  44. if req.MenuName == "" {
  45. br.Msg = "请输入目录名称"
  46. return
  47. }
  48. // 校验同级目录是否有重名
  49. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  50. {
  51. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ParentId)
  52. pars := make([]interface{}, 0)
  53. pars = append(pars, req.MenuName, req.ParentId)
  54. exists, e := menuOb.GetItemByCondition(cond, pars, "")
  55. if e != nil && e.Error() != utils.ErrNoRow() {
  56. br.Msg = "操作失败"
  57. br.ErrMsg = "获取同名目录失败, Err: " + e.Error()
  58. return
  59. }
  60. if exists != nil && exists.SpeechRecognitionMenuId > 0 {
  61. br.Msg = "分类名称已存在,请重新输入"
  62. return
  63. }
  64. }
  65. // 获取目录层级
  66. level := 1
  67. {
  68. if req.ParentId > 0 {
  69. parentMenu, e := menuOb.GetItemById(req.ParentId)
  70. if e != nil {
  71. br.Msg = "操作失败"
  72. br.ErrMsg = "获取父级目录失败, Err: " + e.Error()
  73. return
  74. }
  75. level += parentMenu.Level
  76. }
  77. }
  78. menuOb.MenuName = req.MenuName
  79. menuOb.ParentId = req.ParentId
  80. menuOb.Level = level
  81. menuOb.CreateTime = time.Now().Local()
  82. menuOb.ModifyTime = time.Now().Local()
  83. e := menuOb.Create()
  84. if e != nil {
  85. br.Msg = "操作失败"
  86. br.ErrMsg = "新增目录失败, Err: " + e.Error()
  87. return
  88. }
  89. br.Ret = 200
  90. br.Success = true
  91. br.Msg = "操作成功"
  92. }
  93. // Edit
  94. // @Title 编辑分类
  95. // @Description 编辑分类
  96. // @Param request body speech_recognition.SpeechRecognitionMenuEditReq true "type json string"
  97. // @Success 200 string "操作成功"
  98. // @router /menu/edit [post]
  99. func (this *SpeechRecognitionMenuController) Edit() {
  100. br := new(models.BaseResponse).Init()
  101. defer func() {
  102. if br.ErrMsg == "" {
  103. br.IsSendEmail = false
  104. }
  105. this.Data["json"] = br
  106. this.ServeJSON()
  107. }()
  108. sysUser := this.SysUser
  109. if sysUser == nil {
  110. br.Msg = "请登录"
  111. br.ErrMsg = "请登录,SysUser Is Empty"
  112. br.Ret = 408
  113. return
  114. }
  115. var req speech_recognition.SpeechRecognitionMenuEditReq
  116. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  117. br.Msg = "参数有误"
  118. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  119. return
  120. }
  121. if req.MenuId <= 0 {
  122. br.Msg = "参数有误"
  123. br.ErrMsg = fmt.Sprintf("参数有误, MenuId: %d", req.MenuId)
  124. return
  125. }
  126. req.MenuName = strings.TrimSpace(req.MenuName)
  127. if req.MenuName == "" {
  128. br.Msg = "请输入目录名称"
  129. return
  130. }
  131. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  132. menuItem, e := menuOb.GetItemById(req.MenuId)
  133. if e != nil {
  134. if e.Error() == utils.ErrNoRow() {
  135. br.Msg = "目录不存在,请刷新页面"
  136. return
  137. }
  138. br.Msg = "操作失败"
  139. br.ErrMsg = "获取目录失败, Err: " + e.Error()
  140. return
  141. }
  142. // 校验同级目录是否有重名
  143. {
  144. cond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ParentId, speech_recognition.SpeechRecognitionMenuCols.SpeechRecognitionMenuId)
  145. pars := make([]interface{}, 0)
  146. pars = append(pars, req.MenuName, menuItem.ParentId, req.MenuId)
  147. exists, e := menuOb.GetItemByCondition(cond, pars, "")
  148. if e != nil && e.Error() != utils.ErrNoRow() {
  149. br.Msg = "操作失败"
  150. br.ErrMsg = "获取同名目录失败, Err: " + e.Error()
  151. return
  152. }
  153. if exists != nil && exists.SpeechRecognitionMenuId > 0 {
  154. br.Msg = "分类名称已存在,请重新输入"
  155. return
  156. }
  157. }
  158. menuItem.MenuName = req.MenuName
  159. menuItem.ModifyTime = time.Now().Local()
  160. updateCols := []string{speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ModifyTime}
  161. if e = menuItem.Update(updateCols); e != nil {
  162. br.Msg = "操作失败"
  163. br.ErrMsg = "更新目录失败, Err: " + e.Error()
  164. return
  165. }
  166. br.Ret = 200
  167. br.Success = true
  168. br.Msg = "操作成功"
  169. }
  170. // Remove
  171. // @Title 删除分类
  172. // @Description 删除分类
  173. // @Param request body speech_recognition.SpeechRecognitionMenuRemoveReq true "type json string"
  174. // @Success 200 string "操作成功"
  175. // @router /menu/remove [post]
  176. func (this *SpeechRecognitionMenuController) Remove() {
  177. br := new(models.BaseResponse).Init()
  178. defer func() {
  179. if br.ErrMsg == "" {
  180. br.IsSendEmail = false
  181. }
  182. this.Data["json"] = br
  183. this.ServeJSON()
  184. }()
  185. sysUser := this.SysUser
  186. if sysUser == nil {
  187. br.Msg = "请登录"
  188. br.ErrMsg = "请登录,SysUser Is Empty"
  189. br.Ret = 408
  190. return
  191. }
  192. var req speech_recognition.SpeechRecognitionMenuRemoveReq
  193. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  194. br.Msg = "参数有误"
  195. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  196. return
  197. }
  198. if req.MenuId <= 0 {
  199. br.Msg = "参数有误"
  200. br.ErrMsg = fmt.Sprintf("参数有误, MenuId: %d", req.MenuId)
  201. return
  202. }
  203. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  204. menuItem, e := menuOb.GetItemById(req.MenuId)
  205. if e != nil {
  206. if e.Error() == utils.ErrNoRow() {
  207. br.Ret = 200
  208. br.Success = true
  209. br.Msg = "操作成功"
  210. return
  211. }
  212. br.Msg = "操作失败"
  213. br.ErrMsg = "获取目录失败, Err: " + e.Error()
  214. return
  215. }
  216. // 校验目录下是否有内容
  217. {
  218. speechOb := new(speech_recognition.SpeechRecognition)
  219. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionCols.MenuId)
  220. pars := make([]interface{}, 0)
  221. pars = append(pars, req.MenuId)
  222. count, e := speechOb.GetCountByCondition(cond, pars)
  223. if e != nil {
  224. br.Msg = "操作失败"
  225. br.ErrMsg = "获取目录下的语音识别数失败, Err: " + e.Error()
  226. return
  227. }
  228. if count > 0 {
  229. br.Msg = "该分类关联转写文件,删除失败!"
  230. return
  231. }
  232. cond = fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionMenuCols.ParentId)
  233. pars = make([]interface{}, 0)
  234. pars = append(pars, req.MenuId)
  235. count, e = menuOb.GetCountByCondition(cond, pars)
  236. if e != nil {
  237. br.Msg = "操作失败"
  238. br.ErrMsg = "获取下级目录数失败, Err: " + e.Error()
  239. return
  240. }
  241. if count > 0 {
  242. br.Msg = "该分类含下级目录,删除失败!"
  243. return
  244. }
  245. }
  246. if e = menuItem.Del(); e != nil {
  247. br.Msg = "操作失败"
  248. br.ErrMsg = "删除目录失败, Err: " + e.Error()
  249. return
  250. }
  251. br.Ret = 200
  252. br.Success = true
  253. br.Msg = "操作成功"
  254. }
  255. // Tree
  256. // @Title 目录树
  257. // @Description 目录树
  258. // @Param ParentId query int false "父级ID"
  259. // @Success 200 {object} speech_recognition.SpeechRecognitionMenuNodeItem
  260. // @router /menu/tree [get]
  261. func (this *SpeechRecognitionMenuController) Tree() {
  262. br := new(models.BaseResponse).Init()
  263. defer func() {
  264. if br.ErrMsg == "" {
  265. br.IsSendEmail = false
  266. }
  267. this.Data["json"] = br
  268. this.ServeJSON()
  269. }()
  270. sysUser := this.SysUser
  271. if sysUser == nil {
  272. br.Msg = "请登录"
  273. br.ErrMsg = "请登录,SysUser Is Empty"
  274. br.Ret = 408
  275. return
  276. }
  277. // 前端采用懒加载, 所以只查询目录及当前目录下的语音识别
  278. parentId, _ := this.GetInt("ParentId")
  279. menus := make([]*speech_recognition.SpeechRecognitionMenu, 0)
  280. {
  281. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  282. cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionMenuCols.ParentId)
  283. pars := make([]interface{}, 0)
  284. pars = append(pars, parentId)
  285. list, e := menuOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionMenuCols.Sort))
  286. if e != nil {
  287. br.Msg = "获取失败"
  288. br.ErrMsg = "获取目录列表失败, Err: " + e.Error()
  289. return
  290. }
  291. menus = list
  292. }
  293. resp := make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0)
  294. if len(menus) == 0 {
  295. br.Data = resp
  296. br.Ret = 200
  297. br.Success = true
  298. br.Msg = "获取成功"
  299. return
  300. }
  301. menuIds := make([]int, 0)
  302. for _, m := range menus {
  303. menuIds = append(menuIds, m.SpeechRecognitionMenuId)
  304. }
  305. // 子目录
  306. childMenus := make([]*speech_recognition.SpeechRecognitionMenu, 0)
  307. {
  308. menuOb := new(speech_recognition.SpeechRecognitionMenu)
  309. cond := fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionMenuCols.ParentId, utils.GetOrmInReplace(len(menuIds)))
  310. pars := make([]interface{}, 0)
  311. pars = append(pars, menuIds)
  312. list, e := menuOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s ASC", speech_recognition.SpeechRecognitionMenuCols.Sort, speech_recognition.SpeechRecognitionMenuCols.ParentId))
  313. if e != nil {
  314. br.Msg = "获取失败"
  315. br.ErrMsg = "获取子目录列表失败, Err: " + e.Error()
  316. return
  317. }
  318. childMenus = list
  319. }
  320. menuChildren := make(map[int][]*speech_recognition.SpeechRecognitionMenuNodeItem)
  321. for _, m := range childMenus {
  322. if menuChildren[m.ParentId] == nil {
  323. menuChildren[m.ParentId] = make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0)
  324. }
  325. menuChildren[m.ParentId] = append(menuChildren[m.ParentId], &speech_recognition.SpeechRecognitionMenuNodeItem{
  326. NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeDefault,
  327. MenuId: m.SpeechRecognitionMenuId,
  328. MenuName: m.MenuName,
  329. ParentId: m.ParentId,
  330. Sort: m.Sort,
  331. CreateTime: utils.TimeTransferString(utils.FormatDateTime, m.CreateTime),
  332. })
  333. }
  334. // 目录下的语音识别
  335. speeches := make([]*speech_recognition.SpeechRecognition, 0)
  336. {
  337. speechOb := new(speech_recognition.SpeechRecognition)
  338. cond := fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.MenuId, utils.GetOrmInReplace(len(menuIds)))
  339. pars := make([]interface{}, 0)
  340. pars = append(pars, menuIds)
  341. list, e := speechOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s DESC", speech_recognition.SpeechRecognitionCols.Sort, speech_recognition.SpeechRecognitionCols.CreateTime))
  342. if e != nil {
  343. br.Msg = "获取失败"
  344. br.ErrMsg = "获取语音识别列表失败, Err: " + e.Error()
  345. return
  346. }
  347. speeches = list
  348. }
  349. menuSpeeches := make(map[int][]*speech_recognition.SpeechRecognitionMenuNodeItem)
  350. for _, s := range speeches {
  351. if menuSpeeches[s.MenuId] == nil {
  352. menuSpeeches[s.MenuId] = make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0)
  353. }
  354. menuSpeeches[s.MenuId] = append(menuSpeeches[s.MenuId], &speech_recognition.SpeechRecognitionMenuNodeItem{
  355. NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeSpeech,
  356. SpeechRecognitionId: s.SpeechRecognitionId,
  357. SpeechRecognitionName: s.FileName,
  358. ParentId: s.MenuId,
  359. Sort: s.Sort,
  360. CreateTime: utils.TimeTransferString(utils.FormatDateTime, s.CreateTime),
  361. })
  362. }
  363. for _, m := range menus {
  364. t := &speech_recognition.SpeechRecognitionMenuNodeItem{
  365. NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeDefault,
  366. MenuId: m.SpeechRecognitionMenuId,
  367. MenuName: m.MenuName,
  368. ParentId: m.ParentId,
  369. Sort: m.Sort,
  370. CreateTime: utils.TimeTransferString(utils.FormatDateTime, m.CreateTime),
  371. Children: make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0),
  372. }
  373. if menuSpeeches[m.SpeechRecognitionMenuId] != nil {
  374. t.Children = append(t.Children, menuSpeeches[m.SpeechRecognitionMenuId]...)
  375. }
  376. if menuChildren[m.SpeechRecognitionMenuId] != nil {
  377. t.Children = append(t.Children, menuChildren[m.SpeechRecognitionMenuId]...)
  378. }
  379. resp = append(resp, t)
  380. }
  381. br.Data = resp
  382. br.Ret = 200
  383. br.Success = true
  384. br.Msg = "获取成功"
  385. }