my_chart_classify.go 6.8 KB

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