collect_chart_classify.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. "eta_gn/eta_api/services/data"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // ChartCollectClassifyController 图表收藏分类
  16. type ChartCollectClassifyController struct {
  17. controllers.BaseAuthController
  18. }
  19. // List
  20. // @Title 分类列表-含图表
  21. // @Description 分类列表-含图表
  22. // @Param ParentId query int false "父级ID"
  23. // @Success 200 {object} data_manage.ChartCollectClassifyListItem
  24. // @router /chart_collect/classify/list [get]
  25. func (this *ChartCollectClassifyController) List() {
  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. parentId, _ := this.GetInt("ParentId")
  42. resp := make([]*data_manage.ChartCollectClassifyListItem, 0)
  43. // 查询分类
  44. classifyOb := new(data_manage.ChartCollectClassify)
  45. {
  46. cond := fmt.Sprintf(" AND %s = ? AND %s = ?", classifyOb.Cols().ParentId, classifyOb.Cols().SysUserId)
  47. pars := make([]interface{}, 0)
  48. pars = append(pars, parentId, sysUser.AdminId)
  49. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", classifyOb.Cols().Sort))
  50. if e != nil {
  51. br.Msg = "获取失败"
  52. br.ErrMsg = fmt.Sprintf("获取子分类失败, %v", e)
  53. return
  54. }
  55. for _, v := range list {
  56. resp = append(resp, &data_manage.ChartCollectClassifyListItem{
  57. NodeType: 1,
  58. ClassifyId: v.ChartCollectClassifyId,
  59. ClassifyName: v.ClassifyName,
  60. ParentId: v.ParentId,
  61. Level: v.Level,
  62. Sort: v.Sort,
  63. UniqueCode: v.UniqueCode,
  64. })
  65. }
  66. }
  67. // 查询分类下收藏的图表
  68. {
  69. list, e := data_manage.GetCollectChartInfoByClassifyId(parentId)
  70. if e != nil {
  71. br.Msg = "获取失败"
  72. br.ErrMsg = fmt.Sprintf("获取分类下图表失败, %v", e)
  73. return
  74. }
  75. for _, v := range list {
  76. resp = append(resp, &data_manage.ChartCollectClassifyListItem{
  77. NodeType: 2,
  78. ChartInfoId: v.ChartInfoId,
  79. ChartName: v.ChartName,
  80. ParentId: parentId,
  81. Sort: v.Sort,
  82. UniqueCode: v.UniqueCode,
  83. })
  84. }
  85. }
  86. sort.Slice(resp, func(i, j int) bool {
  87. return resp[i].Sort < resp[j].Sort
  88. })
  89. br.Data = resp
  90. br.Ret = 200
  91. br.Success = true
  92. br.Msg = "获取成功"
  93. }
  94. // Tree
  95. // @Title 分类树
  96. // @Description 分类树
  97. // @Success 200 {object} data_manage.ChartCollectClassifyItem
  98. // @router /chart_collect/classify/tree [get]
  99. func (this *ChartCollectClassifyController) Tree() {
  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. // 获取所有分类
  116. classifyOb := new(data_manage.ChartCollectClassify)
  117. cond := fmt.Sprintf(" AND %s = ?", classifyOb.Cols().SysUserId)
  118. pars := make([]interface{}, 0)
  119. pars = append(pars, sysUser.AdminId)
  120. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s ASC", classifyOb.Cols().ParentId, classifyOb.Cols().Sort))
  121. if e != nil {
  122. br.Msg = "获取失败"
  123. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  124. return
  125. }
  126. items := make([]*data_manage.ChartCollectClassifyItem, 0)
  127. for _, v := range list {
  128. items = append(items, v.Format2Item())
  129. }
  130. tree := data.GetChartCollectClassifyTreeRecursive(items, 0)
  131. br.Data = tree
  132. br.Ret = 200
  133. br.Success = true
  134. br.Msg = "获取成功"
  135. }
  136. // Add
  137. // @Title 新增分类
  138. // @Description 新增分类
  139. // @Param request body data_manage.ChartCollectClassifyAddReq true "type json string"
  140. // @Success Ret=200 保存成功
  141. // @router /chart_collect/classify/add [post]
  142. func (this *ChartCollectClassifyController) Add() {
  143. br := new(models.BaseResponse).Init()
  144. defer func() {
  145. if br.ErrMsg == "" {
  146. br.IsSendEmail = false
  147. }
  148. this.Data["json"] = br
  149. this.ServeJSON()
  150. }()
  151. sysUser := this.SysUser
  152. if sysUser == nil {
  153. br.Msg = "请登录"
  154. br.ErrMsg = "请登录,SysUser Is Empty"
  155. br.Ret = 408
  156. return
  157. }
  158. var req data_manage.ChartCollectClassifyAddReq
  159. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  160. br.Msg = "参数解析异常"
  161. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  162. return
  163. }
  164. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  165. if req.ClassifyName == "" {
  166. br.Msg = "请输入名称"
  167. return
  168. }
  169. classifyOb := new(data_manage.ChartCollectClassify)
  170. // 校验分类名称
  171. //{
  172. // cond := fmt.Sprintf(" AND %s = ? AND %s = ?", classifyOb.Cols().ParentId, classifyOb.Cols().ClassifyName)
  173. // pars := make([]interface{}, 0)
  174. // pars = append(pars, req.ParentId, req.ClassifyName)
  175. // count, e := classifyOb.GetCountByCondition(cond, pars)
  176. // if e != nil {
  177. // br.Msg = "操作失败"
  178. // br.ErrMsg = fmt.Sprintf("获取分类名称重复数失败, %v", e)
  179. // return
  180. // }
  181. // if count > 0 {
  182. // br.Msg = "分类名称已存在"
  183. // return
  184. // }
  185. //}
  186. // 层级路径
  187. var levelPath string
  188. var rootId int
  189. if req.ParentId > 0 {
  190. parent, e := classifyOb.GetItemById(req.ParentId)
  191. if e != nil {
  192. br.Msg = "上级分类有误"
  193. br.ErrMsg = fmt.Sprintf("获取上级分类失败, %v", e)
  194. return
  195. }
  196. levelPath = parent.LevelPath
  197. rootId = parent.RootId
  198. }
  199. sortMax, e := classifyOb.GetSortMax(req.ParentId)
  200. if e != nil {
  201. br.Msg = "操作失败"
  202. br.ErrMsg = fmt.Sprintf("获取分类最大排序失败, %v", e)
  203. return
  204. }
  205. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  206. classifyOb.ParentId = req.ParentId
  207. classifyOb.ClassifyName = req.ClassifyName
  208. classifyOb.Level = req.Level + 1
  209. classifyOb.Sort = sortMax + 1
  210. classifyOb.SysUserId = sysUser.AdminId
  211. classifyOb.SysUserRealName = sysUser.RealName
  212. classifyOb.UniqueCode = utils.MD5(classifyOb.TableName() + "_" + timestamp)
  213. classifyOb.CreateTime = time.Now().Local()
  214. classifyOb.ModifyTime = time.Now().Local()
  215. if e = classifyOb.Create(); e != nil {
  216. br.Msg = "操作失败"
  217. br.ErrMsg = fmt.Sprintf("新增分类失败, %v", e)
  218. return
  219. }
  220. if req.ParentId > 0 {
  221. classifyOb.LevelPath = fmt.Sprintf("%s,%d", levelPath, classifyOb.ChartCollectClassifyId)
  222. classifyOb.RootId = rootId
  223. } else {
  224. classifyOb.LevelPath = fmt.Sprint(classifyOb.ChartCollectClassifyId)
  225. classifyOb.RootId = classifyOb.ChartCollectClassifyId
  226. }
  227. if e = classifyOb.Update([]string{classifyOb.Cols().LevelPath, classifyOb.Cols().RootId}); e != nil {
  228. br.Msg = "操作失败"
  229. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  230. return
  231. }
  232. br.Data = classifyOb.Format2Item()
  233. br.Ret = 200
  234. br.Success = true
  235. br.Msg = "操作成功"
  236. }
  237. // Edit
  238. // @Title 编辑分类
  239. // @Description 编辑分类
  240. // @Param request body data_manage.ChartCollectClassifyEditReq true "type json string"
  241. // @Success Ret=200 保存成功
  242. // @router /chart_collect/classify/edit [post]
  243. func (this *ChartCollectClassifyController) Edit() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. if br.ErrMsg == "" {
  247. br.IsSendEmail = false
  248. }
  249. this.Data["json"] = br
  250. this.ServeJSON()
  251. }()
  252. sysUser := this.SysUser
  253. if sysUser == nil {
  254. br.Msg = "请登录"
  255. br.ErrMsg = "请登录,SysUser Is Empty"
  256. br.Ret = 408
  257. return
  258. }
  259. var req data_manage.ChartCollectClassifyEditReq
  260. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  261. br.Msg = "参数解析异常"
  262. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  263. return
  264. }
  265. if req.ClassifyId < 0 {
  266. br.Msg = "请选择分类"
  267. return
  268. }
  269. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  270. if req.ClassifyName == "" {
  271. br.Msg = "请输入分类名称"
  272. return
  273. }
  274. classifyOb := new(data_manage.ChartCollectClassify)
  275. classifyItem, e := classifyOb.GetItemById(req.ClassifyId)
  276. if e != nil {
  277. if utils.IsErrNoRow(e) {
  278. br.Msg = "分类不存在, 请刷新页面"
  279. return
  280. }
  281. br.Msg = "操作失败"
  282. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  283. return
  284. }
  285. // 校验分类名称
  286. //{
  287. // cond := fmt.Sprintf(" AND %s <> ? AND %s = ?", classifyOb.Cols().PrimaryId, classifyOb.Cols().ClassifyName)
  288. // pars := make([]interface{}, 0)
  289. // pars = append(pars, req.ClassifyId, req.ClassifyName)
  290. // count, e := classifyOb.GetCountByCondition(cond, pars)
  291. // if e != nil {
  292. // br.Msg = "操作失败"
  293. // br.ErrMsg = fmt.Sprintf("获取分类名称重复数失败, %v", e)
  294. // return
  295. // }
  296. // if count > 0 {
  297. // br.Msg = "分类名称已存在"
  298. // return
  299. // }
  300. //}
  301. classifyItem.ClassifyName = req.ClassifyName
  302. classifyItem.ModifyTime = time.Now().Local()
  303. updateCols := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().ModifyTime}
  304. if e = classifyItem.Update(updateCols); e != nil {
  305. br.Msg = "操作失败"
  306. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  307. return
  308. }
  309. br.Data = classifyItem.Format2Item()
  310. br.Ret = 200
  311. br.Success = true
  312. br.Msg = "操作成功"
  313. }
  314. // Remove
  315. // @Title 删除分类
  316. // @Description 删除分类
  317. // @Param request body data_manage.ChartCollectClassifyRemoveReq true "type json string"
  318. // @Success 200 string "操作成功"
  319. // @router /chart_collect/classify/remove [post]
  320. func (this *ChartCollectClassifyController) Remove() {
  321. br := new(models.BaseResponse).Init()
  322. defer func() {
  323. if br.ErrMsg == "" {
  324. br.IsSendEmail = false
  325. }
  326. this.Data["json"] = br
  327. this.ServeJSON()
  328. }()
  329. sysUser := this.SysUser
  330. if sysUser == nil {
  331. br.Msg = "请登录"
  332. br.ErrMsg = "请登录,SysUser Is Empty"
  333. br.Ret = 408
  334. return
  335. }
  336. var req data_manage.ChartCollectClassifyRemoveReq
  337. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  338. br.Msg = "参数解析异常"
  339. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  340. return
  341. }
  342. if req.ClassifyId < 0 {
  343. br.Msg = "请选择分类"
  344. return
  345. }
  346. classifyOb := new(data_manage.ChartCollectClassify)
  347. _, e := classifyOb.GetItemById(req.ClassifyId)
  348. if e != nil {
  349. if utils.IsErrNoRow(e) {
  350. br.Ret = 200
  351. br.Success = true
  352. br.Msg = "操作成功"
  353. return
  354. }
  355. br.Msg = "操作失败"
  356. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  357. return
  358. }
  359. // 获取子分类IDs
  360. classifyIds, e := data.GetChartCollectClassifyChildIds(req.ClassifyId, sysUser.AdminId, true)
  361. if e != nil {
  362. br.Msg = "操作失败"
  363. br.ErrMsg = fmt.Sprintf("获取子分类IDs失败, %v", e)
  364. return
  365. }
  366. if len(classifyIds) == 0 {
  367. br.Ret = 200
  368. br.Success = true
  369. br.Msg = "操作成功"
  370. return
  371. }
  372. // 移除所有子分类以及子分类所收藏的图表
  373. if e = classifyOb.RemoveClassifyAndCollect(classifyIds); e != nil {
  374. br.Msg = "操作失败"
  375. br.ErrMsg = fmt.Sprintf("移除分类及收藏失败, %v", e)
  376. return
  377. }
  378. br.Ret = 200
  379. br.Success = true
  380. br.Msg = "操作成功"
  381. }
  382. // Move
  383. // @Title 移动分类
  384. // @Description 移动分类
  385. // @Param request body data_manage.ChartCollectClassifyMoveReq true "type json string"
  386. // @Success 200 string "操作成功"
  387. // @router /chart_collect/classify/move [post]
  388. func (this *ChartCollectClassifyController) Move() {
  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. var req data_manage.ChartCollectClassifyMoveReq
  405. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  406. br.Msg = "参数解析异常!"
  407. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  408. return
  409. }
  410. classifyOb := new(data_manage.ChartCollectClassify)
  411. item, e := classifyOb.GetItemById(req.ClassifyId)
  412. if e != nil {
  413. if utils.IsErrNoRow(e) {
  414. br.Msg = "分类不存在, 请刷新页面"
  415. return
  416. }
  417. br.Msg = "操作失败"
  418. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  419. return
  420. }
  421. updateCol := make([]string, 0)
  422. //如果有传入 上一个兄弟节点分类id
  423. if req.PrevClassifyId > 0 {
  424. prevClassify, err := data_manage.GetChartCollectClassifyById(sysUser.AdminId, req.PrevClassifyId)
  425. if err != nil {
  426. br.Msg = "移动失败"
  427. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  428. return
  429. }
  430. //如果是移动在两个兄弟节点之间
  431. if req.NextClassifyId > 0 {
  432. //下一个兄弟节点
  433. nextClassify, err := data_manage.GetChartCollectClassifyById(sysUser.AdminId, req.NextClassifyId)
  434. if err != nil {
  435. br.Msg = "移动失败"
  436. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  437. return
  438. }
  439. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  440. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == item.Sort {
  441. //变更兄弟节点的排序
  442. updateSortStr := `sort + 2`
  443. _ = data_manage.UpdateChartCollectClassifySortByClassifyId(sysUser.AdminId, prevClassify.ChartCollectClassifyId, prevClassify.Sort, updateSortStr)
  444. } else {
  445. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  446. if nextClassify.Sort-prevClassify.Sort == 1 {
  447. //变更兄弟节点的排序
  448. updateSortStr := `sort + 1`
  449. _ = data_manage.UpdateChartCollectClassifySortByClassifyId(sysUser.AdminId, 0, prevClassify.Sort, updateSortStr)
  450. }
  451. }
  452. }
  453. item.Sort = prevClassify.Sort + 1
  454. item.ModifyTime = time.Now()
  455. updateCol = append(updateCol, "Sort", "ModifyTime")
  456. } else {
  457. firstClassify, err := data_manage.GetFirstChartCollectClassifyByAdminId(sysUser.AdminId)
  458. if err != nil && !utils.IsErrNoRow(err) {
  459. br.Msg = "移动失败"
  460. br.ErrMsg = "获取获取当前账号下的排序第一条的分类信息失败,Err:" + err.Error()
  461. return
  462. }
  463. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  464. if firstClassify != nil && firstClassify.ChartCollectClassifyId > 0 && firstClassify.Sort == 0 {
  465. updateSortStr := ` sort + 1 `
  466. _ = data_manage.UpdateChartCollectClassifySortByClassifyId(sysUser.AdminId, firstClassify.ChartCollectClassifyId-1, 0, updateSortStr)
  467. }
  468. item.Sort = 0 //那就是排在第一位
  469. item.ModifyTime = time.Now()
  470. updateCol = append(updateCol, "Sort", "ModifyTime")
  471. }
  472. if len(updateCol) > 0 {
  473. if e = item.Update(updateCol); e != nil {
  474. br.Msg = "操作失败"
  475. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  476. return
  477. }
  478. }
  479. br.Ret = 200
  480. br.Success = true
  481. br.Msg = "操作成功"
  482. }