eta_training_video_classify.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. package eta_training_video
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_crm_api/controllers"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/eta_training_video"
  8. etaTrainingVideoService "hongze/hz_crm_api/services/eta_training_video"
  9. "hongze/hz_crm_api/utils"
  10. "html/template"
  11. "sort"
  12. "strings"
  13. "time"
  14. )
  15. // EtaTrainingVideoClassifyController ETA培训视频分类
  16. type EtaTrainingVideoClassifyController struct {
  17. controllers.BaseAuthController
  18. }
  19. // Tree
  20. // @Title 分类树
  21. // @Description 分类树
  22. // @Param Keyword query string false "关键词: 分类名称"
  23. // @Success 200 Ret=200 获取成功
  24. // @router /classify/tree [get]
  25. func (this *EtaTrainingVideoClassifyController) Tree() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. if br.ErrMsg == "" {
  29. br.IsSendEmail = false
  30. }
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. sysUser := this.SysUser
  35. if sysUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. br.Ret = 408
  39. return
  40. }
  41. resp := new(eta_training_video.EtaTrainingVideoClassifyResp)
  42. resp.List = make([]*eta_training_video.EtaTrainingVideoClassifyItem, 0)
  43. cond := ``
  44. pars := make([]interface{}, 0)
  45. // 关键词
  46. keyword := this.GetString("Keyword", "")
  47. keyword = strings.TrimSpace(keyword)
  48. keyword = template.HTMLEscapeString(keyword)
  49. // 获取所有分类
  50. ob := new(eta_training_video.EtaTrainingVideoClassify)
  51. classifies, e := ob.GetItemsByCondition(cond, pars, []string{}, ``)
  52. if e != nil {
  53. br.Msg = "获取失败"
  54. br.ErrMsg = "获取分类列表失败, Err: " + e.Error()
  55. return
  56. }
  57. list := make([]*eta_training_video.EtaTrainingVideoClassify, 0)
  58. if keyword != "" {
  59. classifyMap := make(map[int]*eta_training_video.EtaTrainingVideoClassify)
  60. parentMap := make(map[int]*eta_training_video.EtaTrainingVideoClassify)
  61. existMap := make(map[int]bool)
  62. for _, m := range classifies {
  63. classifyMap[m.EtaTrainingVideoClassifyId] = m
  64. }
  65. for _, m := range classifies {
  66. if m.ParentId > 0 {
  67. parentMap[m.EtaTrainingVideoClassifyId] = classifyMap[m.ParentId]
  68. }
  69. }
  70. // 遍历菜单, 取出跟关键词匹配的菜单(以后可能会更改成无限级分类, 所以相似度匹配在这里处理)
  71. for _, m := range classifies {
  72. if !strings.Contains(m.ClassifyName, keyword) {
  73. continue
  74. }
  75. if existMap[m.EtaTrainingVideoClassifyId] {
  76. continue
  77. }
  78. existMap[m.EtaTrainingVideoClassifyId] = true
  79. list = append(list, m)
  80. // 取出关键词所匹配的所有父级菜单
  81. if m.ParentId > 0 {
  82. parents := etaTrainingVideoService.GetClassifyParentsRecursive(classifies, m.ParentId)
  83. for _, p := range parents {
  84. if !existMap[p.EtaTrainingVideoClassifyId] {
  85. existMap[p.EtaTrainingVideoClassifyId] = true
  86. list = append(list, p)
  87. }
  88. }
  89. }
  90. }
  91. sort.Slice(list, func(i, j int) bool {
  92. return list[j].Sort > list[i].Sort
  93. })
  94. } else {
  95. list = classifies
  96. }
  97. items := make([]*eta_training_video.EtaTrainingVideoClassifyItem, 0)
  98. for _, v := range list {
  99. t := &eta_training_video.EtaTrainingVideoClassifyItem{
  100. ClassifyId: v.EtaTrainingVideoClassifyId,
  101. ClassifyName: v.ClassifyName,
  102. ParentId: v.ParentId,
  103. Sort: v.Sort,
  104. Children: make([]*eta_training_video.EtaTrainingVideoClassifyItem, 0),
  105. }
  106. items = append(items, t)
  107. }
  108. // 递归返回树形结构
  109. items = etaTrainingVideoService.GetClassifyTreeRecursive(items, 0)
  110. resp.List = items
  111. br.Data = resp
  112. br.Ret = 200
  113. br.Success = true
  114. br.Msg = "获取成功"
  115. }
  116. // Add
  117. // @Title 新增分类
  118. // @Description 新增分类
  119. // @Param request body eta_training_video.EtaTrainingVideoClassifyAddReq true "type json string"
  120. // @Success 200 Ret=200 操作成功
  121. // @router /classify/add [post]
  122. func (this *EtaTrainingVideoClassifyController) Add() {
  123. br := new(models.BaseResponse).Init()
  124. defer func() {
  125. if br.ErrMsg == "" {
  126. br.IsSendEmail = false
  127. }
  128. this.Data["json"] = br
  129. this.ServeJSON()
  130. }()
  131. sysUser := this.SysUser
  132. if sysUser == nil {
  133. br.Msg = "请登录"
  134. br.ErrMsg = "请登录,SysUser Is Empty"
  135. br.Ret = 408
  136. return
  137. }
  138. // 参数校验
  139. var req eta_training_video.EtaTrainingVideoClassifyAddReq
  140. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  141. br.Msg = "参数解析异常!"
  142. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  143. return
  144. }
  145. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  146. if req.ClassifyName == "" {
  147. br.Msg = "请输入分类名称"
  148. return
  149. }
  150. nameRune := []rune(req.ClassifyName)
  151. if len(nameRune) > 50 {
  152. br.Msg = "分类名称不可超过50个字符"
  153. return
  154. }
  155. // 重名校验
  156. {
  157. ob := new(eta_training_video.EtaTrainingVideoClassify)
  158. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, eta_training_video.VideoClassifyColumns.ParentId, eta_training_video.VideoClassifyColumns.ClassifyName)
  159. pars := make([]interface{}, 0)
  160. pars = append(pars, req.ParentId, req.ClassifyName)
  161. exist, e := ob.GetItemByCondition(cond, pars)
  162. if e != nil && e.Error() != utils.ErrNoRow() {
  163. br.Msg = "操作失败"
  164. br.ErrMsg = "获取重名分类失败, Err: " + e.Error()
  165. return
  166. }
  167. if exist != nil {
  168. br.Msg = "分类名称已存在"
  169. return
  170. }
  171. }
  172. item := new(eta_training_video.EtaTrainingVideoClassify)
  173. item.ClassifyName = req.ClassifyName
  174. item.ParentId = req.ParentId
  175. item.SysUserId = sysUser.AdminId
  176. item.SysRealName = sysUser.RealName
  177. item.CreateTime = time.Now().Local()
  178. item.ModifyTime = time.Now().Local()
  179. if e := item.Create(); e != nil {
  180. br.Msg = "操作失败"
  181. br.ErrMsg = "新增分类失败, Err: " + e.Error()
  182. return
  183. }
  184. br.Ret = 200
  185. br.Success = true
  186. br.Msg = "操作成功"
  187. }
  188. // Edit
  189. // @Title 编辑分类
  190. // @Description 编辑分类
  191. // @Param request body eta_training_video.EtaTrainingVideoTagEditReq true "type json string"
  192. // @Success 200 Ret=200 操作成功
  193. // @router /classify/edit [post]
  194. func (this *EtaTrainingVideoClassifyController) Edit() {
  195. br := new(models.BaseResponse).Init()
  196. defer func() {
  197. if br.ErrMsg == "" {
  198. br.IsSendEmail = false
  199. }
  200. this.Data["json"] = br
  201. this.ServeJSON()
  202. }()
  203. sysUser := this.SysUser
  204. if sysUser == nil {
  205. br.Msg = "请登录"
  206. br.ErrMsg = "请登录,SysUser Is Empty"
  207. br.Ret = 408
  208. return
  209. }
  210. // 参数校验
  211. var req eta_training_video.EtaTrainingVideoClassifyEditReq
  212. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  213. br.Msg = "参数解析异常!"
  214. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  215. return
  216. }
  217. if req.ClassifyId <= 0 {
  218. br.Msg = "参数有误"
  219. br.ErrMsg = fmt.Sprintf("参数有误, 分类ID: %d", req.ClassifyId)
  220. return
  221. }
  222. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  223. if req.ClassifyName == "" {
  224. br.Msg = "请输入分类名称"
  225. return
  226. }
  227. nameRune := []rune(req.ClassifyName)
  228. if len(nameRune) > 50 {
  229. br.Msg = "分类名称不可超过50个字符"
  230. return
  231. }
  232. ob := new(eta_training_video.EtaTrainingVideoClassify)
  233. item, e := ob.GetItemById(req.ClassifyId)
  234. if e != nil {
  235. if e.Error() != utils.ErrNoRow() {
  236. br.Msg = "分类不存在, 请刷新页面"
  237. return
  238. }
  239. br.Msg = "操作失败"
  240. br.ErrMsg = "获取分类失败, Err: " + e.Error()
  241. return
  242. }
  243. if item.ParentId == req.ParentId && item.ClassifyName == req.ClassifyName {
  244. br.Ret = 200
  245. br.Success = true
  246. br.Msg = "操作成功"
  247. return
  248. }
  249. // 重名校验
  250. {
  251. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, eta_training_video.VideoClassifyColumns.ParentId, eta_training_video.VideoClassifyColumns.ClassifyName)
  252. pars := make([]interface{}, 0)
  253. pars = append(pars, req.ParentId, req.ClassifyName)
  254. exist, e := ob.GetItemByCondition(cond, pars)
  255. if e != nil && e.Error() != utils.ErrNoRow() {
  256. br.Msg = "操作失败"
  257. br.ErrMsg = "获取重名分类失败, Err: " + e.Error()
  258. return
  259. }
  260. if exist != nil && exist.EtaTrainingVideoClassifyId != item.EtaTrainingVideoClassifyId {
  261. br.Msg = "分类已存在"
  262. return
  263. }
  264. }
  265. now := time.Now().Local()
  266. item.ClassifyName = req.ClassifyName
  267. item.ParentId = req.ParentId
  268. item.ModifyTime = now
  269. cols := []string{"ClassifyName", "ParentId", "ModifyTime"}
  270. if e := item.Update(cols); e != nil {
  271. br.Msg = "操作失败"
  272. br.ErrMsg = "更新分类失败, Err: " + e.Error()
  273. return
  274. }
  275. // 更新关联表冗余
  276. go func() {
  277. relateOB := new(eta_training_video.EtaTrainingVideoClassifyRelate)
  278. _ = relateOB.UpdateClassifyInfoByClassifyId(item.EtaTrainingVideoClassifyId, req.ParentId, req.ClassifyName)
  279. }()
  280. br.Ret = 200
  281. br.Success = true
  282. br.Msg = "操作成功"
  283. }
  284. // Remove
  285. // @Title 删除分类
  286. // @Description 删除分类
  287. // @Param request body eta_training_video.EtaTrainingVideoClassifyRemoveReq true "type json string"
  288. // @Success 200 Ret=200 操作成功
  289. // @router /classify/remove [post]
  290. func (this *EtaTrainingVideoClassifyController) Remove() {
  291. br := new(models.BaseResponse).Init()
  292. defer func() {
  293. if br.ErrMsg == "" {
  294. br.IsSendEmail = false
  295. }
  296. this.Data["json"] = br
  297. this.ServeJSON()
  298. }()
  299. sysUser := this.SysUser
  300. if sysUser == nil {
  301. br.Msg = "请登录"
  302. br.ErrMsg = "请登录,SysUser Is Empty"
  303. br.Ret = 408
  304. return
  305. }
  306. // 参数校验
  307. var req eta_training_video.EtaTrainingVideoClassifyRemoveReq
  308. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  309. br.Msg = "参数解析异常!"
  310. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  311. return
  312. }
  313. if req.ClassifyId <= 0 {
  314. br.Msg = "参数有误"
  315. br.ErrMsg = fmt.Sprintf("参数有误, 分类ID: %d", req.ClassifyId)
  316. return
  317. }
  318. ob := new(eta_training_video.EtaTrainingVideoClassify)
  319. item, e := ob.GetItemById(req.ClassifyId)
  320. if e != nil {
  321. if e.Error() == utils.ErrNoRow() {
  322. br.Ret = 200
  323. br.Success = true
  324. br.Msg = "操作成功"
  325. return
  326. }
  327. br.Msg = "操作失败"
  328. br.ErrMsg = "获取分类失败, Err: " + e.Error()
  329. return
  330. }
  331. // 删除校验
  332. {
  333. // 子分类
  334. childCond := fmt.Sprintf(` AND %s = ?`, eta_training_video.VideoClassifyColumns.ParentId)
  335. childPars := make([]interface{}, 0)
  336. childPars = append(childPars, item.EtaTrainingVideoClassifyId)
  337. childTotal, e := ob.GetCountByCondition(childCond, childPars)
  338. if e != nil {
  339. br.Msg = "操作失败"
  340. br.ErrMsg = "获取子分类数失败, Err: " + e.Error()
  341. return
  342. }
  343. if childTotal > 0 {
  344. br.Msg = "该分类下已关联内容, 不可删除"
  345. return
  346. }
  347. // 引用
  348. useOB := new(eta_training_video.EtaTrainingVideoClassifyRelate)
  349. useCond := fmt.Sprintf(` AND %s = ?`, eta_training_video.VideoClassifyRelateColumns.EtaTrainingVideoClassifyId)
  350. usePars := make([]interface{}, 0)
  351. usePars = append(usePars, item.EtaTrainingVideoClassifyId)
  352. useTotal, e := useOB.GetCountByCondition(useCond, usePars)
  353. if e != nil {
  354. br.Msg = "操作失败"
  355. br.ErrMsg = "获取分类引用数失败, Err: " + e.Error()
  356. return
  357. }
  358. if useTotal > 0 {
  359. br.Msg = "该分类下已关联内容, 不可删除"
  360. return
  361. }
  362. }
  363. if e := item.Del(); e != nil {
  364. br.Msg = "操作失败"
  365. br.ErrMsg = "删除分类失败, Err: " + e.Error()
  366. return
  367. }
  368. br.Ret = 200
  369. br.Success = true
  370. br.Msg = "操作成功"
  371. }