my_chart_classify.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package my_chart
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb/controller/response"
  5. "hongze/hongze_yb/models/request"
  6. responseModel "hongze/hongze_yb/models/response"
  7. "hongze/hongze_yb/models/tables/yb_my_chart"
  8. "hongze/hongze_yb/models/tables/yb_my_chart_classify"
  9. userService "hongze/hongze_yb/services/user"
  10. "hongze/hongze_yb/utils"
  11. "time"
  12. )
  13. // MyChartClassifyController 用户-我的图表分类
  14. type MyChartClassifyController struct{}
  15. func (this *MyChartClassifyController) List(c *gin.Context) {
  16. userInfo := userService.GetInfoByClaims(c)
  17. userId := int(userInfo.UserID)
  18. if userId <= 0 {
  19. response.Fail("请登录后查看", c)
  20. return
  21. }
  22. cond := `user_id = ?`
  23. pars := make([]interface{}, 0)
  24. pars = append(pars, userId)
  25. ob := new(yb_my_chart_classify.YbMyChartClassify)
  26. list, e := ob.List(cond, pars)
  27. if e != nil {
  28. response.FailMsg("获取失败", "获取用户图表分类失败, Err: "+e.Error(), c)
  29. return
  30. }
  31. respList := make([]*responseModel.MyChartClassifyItem, 0)
  32. // 新增一个默认分类
  33. respList = append(respList, &responseModel.MyChartClassifyItem{
  34. MyChartClassifyID: -1,
  35. MyChartClassifyName: "未分类",
  36. Sort: -1,
  37. UserID: userId,
  38. CreateTime: utils.TimeTransferString(utils.FormatDateTime, time.Now().Local()),
  39. })
  40. for i := range list {
  41. respList = append(respList, &responseModel.MyChartClassifyItem{
  42. MyChartClassifyID: list[i].MyChartClassifyID,
  43. MyChartClassifyName: list[i].MyChartClassifyName,
  44. Sort: list[i].Sort,
  45. UserID: list[i].UserID,
  46. CreateTime: utils.TimeTransferString(utils.FormatDateTime, list[i].CreateTime),
  47. })
  48. }
  49. response.OkData("获取成功", respList, c)
  50. }
  51. func (this *MyChartClassifyController) Add(c *gin.Context) {
  52. userInfo := userService.GetInfoByClaims(c)
  53. userId := int(userInfo.UserID)
  54. if userId <= 0 {
  55. response.Fail("请登录后操作", c)
  56. return
  57. }
  58. var req request.MyChartClassifyAddReq
  59. if c.ShouldBind(&req) != nil {
  60. response.Fail("参数有误", c)
  61. return
  62. }
  63. if req.ClassifyName == "" {
  64. response.Fail("请输入分类名称", c)
  65. return
  66. }
  67. if len([]rune(req.ClassifyName)) > 10 {
  68. response.Fail("分类名称不可超过10个字符", c)
  69. return
  70. }
  71. cond := `my_chart_classify_name = ? AND user_id = ?`
  72. pars := make([]interface{}, 0)
  73. pars = append(pars, req.ClassifyName, userId)
  74. ob := new(yb_my_chart_classify.YbMyChartClassify)
  75. exists, e := ob.FetchByCondition(cond, pars)
  76. if e != nil && e != utils.ErrNoRow {
  77. response.FailMsg("操作失败", "获取用户同名图表分类失败, Err: "+e.Error(), c)
  78. return
  79. }
  80. if exists != nil && exists.MyChartClassifyID > 0 {
  81. response.Fail("分类名称已存在", c)
  82. return
  83. }
  84. nowTime := time.Now().Local()
  85. ob.MyChartClassifyName = req.ClassifyName
  86. ob.UserID = userId
  87. ob.CreateTime = nowTime
  88. ob.ModifyTime = nowTime
  89. if e = ob.Create(); e != nil {
  90. response.FailMsg("操作失败", "新增用户图表分类失败, Err: "+e.Error(), c)
  91. return
  92. }
  93. response.Ok("操作成功", c)
  94. }
  95. func (this *MyChartClassifyController) Edit(c *gin.Context) {
  96. userInfo := userService.GetInfoByClaims(c)
  97. userId := int(userInfo.UserID)
  98. if userId <= 0 {
  99. response.Fail("请登录后操作", c)
  100. return
  101. }
  102. var req request.MyChartClassifyEditReq
  103. if c.ShouldBind(&req) != nil {
  104. response.Fail("参数有误", c)
  105. return
  106. }
  107. if req.ClassifyId <= 0 {
  108. response.Fail("请选择分类", c)
  109. return
  110. }
  111. if req.ClassifyName == "" {
  112. response.Fail("请输入分类名称", c)
  113. return
  114. }
  115. if len([]rune(req.ClassifyName)) > 10 {
  116. response.Fail("分类名称不可超过10个字符", c)
  117. return
  118. }
  119. cond := `my_chart_classify_name = ? AND user_id = ?`
  120. pars := make([]interface{}, 0)
  121. pars = append(pars, req.ClassifyName, userId)
  122. ob := new(yb_my_chart_classify.YbMyChartClassify)
  123. exists, e := ob.FetchByCondition(cond, pars)
  124. if e != nil && e != utils.ErrNoRow {
  125. response.FailMsg("操作失败", "获取用户同名图表分类失败, Err: "+e.Error(), c)
  126. return
  127. }
  128. if exists != nil && exists.MyChartClassifyID > 0 && exists.MyChartClassifyID != req.ClassifyId {
  129. response.Fail("分类名称已存在", c)
  130. return
  131. }
  132. item, e := ob.Fetch(req.ClassifyId)
  133. if e != nil {
  134. response.FailMsg("信息有误", "获取用户图表分类失败, Err: "+e.Error(), c)
  135. return
  136. }
  137. item.MyChartClassifyName = req.ClassifyName
  138. item.ModifyTime = time.Now().Local()
  139. if e = item.Update([]string{"MyChartClassifyName", "ModifyTime"}); e != nil {
  140. response.FailMsg("操作失败", "编辑用户图表分类失败, Err: "+e.Error(), c)
  141. return
  142. }
  143. response.Ok("操作成功", c)
  144. }
  145. func (this *MyChartClassifyController) Del(c *gin.Context) {
  146. userInfo := userService.GetInfoByClaims(c)
  147. userId := int(userInfo.UserID)
  148. if userId <= 0 {
  149. response.Fail("请登录后操作", c)
  150. return
  151. }
  152. var req request.MyChartClassifyDelReq
  153. if c.ShouldBind(&req) != nil {
  154. response.Fail("参数有误", c)
  155. return
  156. }
  157. if req.ClassifyId <= 0 {
  158. response.Fail("请选择分类", c)
  159. return
  160. }
  161. ob := new(yb_my_chart_classify.YbMyChartClassify)
  162. item, e := ob.Fetch(req.ClassifyId)
  163. if e != nil {
  164. response.FailMsg("分类不存在或已被删除", "获取用户图表分类失败, Err: "+e.Error(), c)
  165. return
  166. }
  167. if item.UserID != userId {
  168. response.Fail("无权操作", c)
  169. return
  170. }
  171. // 是否存在关联图表
  172. condRelate := `my_chart_classify_id = ?`
  173. parsRelate := make([]interface{}, 0)
  174. parsRelate = append(parsRelate, req.ClassifyId)
  175. chartOB := new(yb_my_chart.YbMyChart)
  176. count, e := chartOB.Count(condRelate, parsRelate)
  177. if e != nil {
  178. response.FailMsg("操作失败", "获取用户图表分类关联数失败, Err: "+e.Error(), c)
  179. return
  180. }
  181. if count > 0 {
  182. response.SpecificFail("", "删除失败, 该分类下有图表", c)
  183. return
  184. }
  185. if e = item.Delete(); e != nil {
  186. response.FailMsg("操作失败", "删除分类失败, Err: "+e.Error(), c)
  187. return
  188. }
  189. response.Ok("操作成功", c)
  190. }
  191. func (this *MyChartClassifyController) Sort(c *gin.Context) {
  192. userInfo := userService.GetInfoByClaims(c)
  193. userId := int(userInfo.UserID)
  194. if userId <= 0 {
  195. response.Fail("请登录后操作", c)
  196. return
  197. }
  198. var req []request.MyChartClassifySortReq
  199. if c.ShouldBind(&req) != nil {
  200. response.Fail("参数有误", c)
  201. return
  202. }
  203. if len(req) == 0 {
  204. response.Fail("请选择排序分类", c)
  205. return
  206. }
  207. sortMap := make(map[int]int)
  208. classifyIds := make([]int, 0)
  209. for i := range req {
  210. classifyIds = append(classifyIds, req[i].ClassifyId)
  211. sortMap[req[i].ClassifyId] = req[i].Sort
  212. }
  213. cond := `my_chart_classify_id IN ?`
  214. pars := make([]interface{}, 0)
  215. pars = append(pars, classifyIds)
  216. ob := new(yb_my_chart_classify.YbMyChartClassify)
  217. list, e := ob.List(cond, pars)
  218. if e != nil {
  219. response.FailMsg("操作失败", "获取用户分类列表失败, Err: "+e.Error(), c)
  220. return
  221. }
  222. nowTime := time.Now().Local()
  223. updateCols := []string{"Sort", "ModifyTime"}
  224. for i := range list {
  225. list[i].Sort = sortMap[list[i].MyChartClassifyID]
  226. list[i].ModifyTime = nowTime
  227. if e = list[i].Update(updateCols); e != nil {
  228. response.FailMsg("操作失败", "更新用户分类排序失败, Err: "+e.Error(), c)
  229. return
  230. }
  231. }
  232. response.Ok("操作成功", c)
  233. }