my_chart.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package my_chart
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/controller/response"
  6. "hongze/hongze_yb/models/request"
  7. chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
  8. "hongze/hongze_yb/models/tables/yb_config"
  9. "hongze/hongze_yb/models/tables/yb_my_chart"
  10. userService "hongze/hongze_yb/services/user"
  11. "hongze/hongze_yb/utils"
  12. "strconv"
  13. "time"
  14. )
  15. // MyChartController 用户-我的图表
  16. type MyChartController struct{}
  17. func (this *MyChartController) List(c *gin.Context) {
  18. }
  19. func (this *MyChartController) Detail(c *gin.Context) {
  20. }
  21. func (this *MyChartController) Collect(c *gin.Context) {
  22. var req request.MyChartCollectReq
  23. if c.ShouldBind(&req) != nil {
  24. response.Fail("参数有误", c)
  25. return
  26. }
  27. if req.Authorization == "" {
  28. response.Fail("用户身份有误", c)
  29. return
  30. }
  31. if req.UniqueCode == "" {
  32. response.Fail("请选择图表", c)
  33. return
  34. }
  35. if req.ReportId <= 0 {
  36. response.Fail("请选择报告", c)
  37. return
  38. }
  39. // 获取用户信息
  40. userInfo, e := userService.GetUserInfoByToken(req.Authorization)
  41. if e != nil {
  42. response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
  43. return
  44. }
  45. if userInfo.UserID <= 0 {
  46. response.FailMsg("操作失败", "Token获取有效用户失败", c)
  47. return
  48. }
  49. userId := int(userInfo.UserID)
  50. // 获取图表信息
  51. chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
  52. if e != nil {
  53. response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
  54. return
  55. }
  56. if chartInfo.ChartInfoId <= 0 {
  57. response.FailMsg("操作失败", "图表信息有误", c)
  58. return
  59. }
  60. // 图表收藏上限
  61. ob := new(yb_my_chart.YbMyChart)
  62. countCond := `user_id = ?`
  63. countPars := make([]interface{}, 0)
  64. countPars = append(countPars, userId)
  65. count, e := ob.Count(countCond, countPars)
  66. if e != nil {
  67. response.FailMsg("操作失败", "获取用户图表收藏数量失败, Err: "+e.Error(), c)
  68. return
  69. }
  70. // 获取图表收藏上限配置
  71. configCond := `config_code = ?`
  72. configPars := make([]interface{}, 0)
  73. configPars = append(configPars, yb_config.UserChartCollectMax)
  74. confOB := new(yb_config.YbConfig)
  75. conf, e := confOB.Fetch(configCond, configPars)
  76. if e != nil {
  77. response.FailMsg("操作失败", "获取图表收藏上限配置失败, Err: "+e.Error(), c)
  78. return
  79. }
  80. max, e := strconv.Atoi(conf.ConfigValue)
  81. if e != nil {
  82. response.FailMsg("操作失败", "图表收藏上限配置有误, Err: "+e.Error(), c)
  83. return
  84. }
  85. if int(count) >= max {
  86. response.Fail(fmt.Sprintf("图表最多可收藏%d张", max), c)
  87. return
  88. }
  89. // 是否已收藏
  90. cond := `user_id = ? AND chart_info_id = ?`
  91. pars := make([]interface{}, 0)
  92. pars = append(pars, userId, chartInfo.ChartInfoId)
  93. exists, e := ob.FetchByCondition(cond, pars)
  94. if e != nil && e != utils.ErrNoRow {
  95. response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
  96. return
  97. }
  98. nowTime := time.Now().Local()
  99. if exists != nil && exists.MyChartID > 0 {
  100. // 更新收藏信息
  101. exists.ChartName = chartInfo.ChartName
  102. exists.ChartImage = chartInfo.ChartImage
  103. exists.ReportID = req.ReportId
  104. exists.ReportChapterID = req.ReportChapterId
  105. exists.ModifyTime = nowTime
  106. updateCols := []string{"ChartName", "ChartImage", "ReportID", "ReportChapterID", "ModifyTime"}
  107. if e = exists.Update(updateCols); e != nil {
  108. response.FailMsg("操作失败", "更新收藏失败, Err: "+e.Error(), c)
  109. return
  110. }
  111. } else {
  112. // 新增收藏
  113. ob.ChartInfoID = chartInfo.ChartInfoId
  114. ob.ChartName = chartInfo.ChartName
  115. ob.UniqueCode = chartInfo.UniqueCode
  116. ob.ChartImage = chartInfo.ChartImage
  117. ob.UserID = userId
  118. ob.ReportID = req.ReportId
  119. ob.ReportChapterID = req.ReportChapterId
  120. ob.CreateTime = nowTime
  121. ob.ModifyTime = nowTime
  122. if e = ob.Create(); e != nil {
  123. response.FailMsg("操作失败", "新增收藏失败, Err: "+e.Error(), c)
  124. return
  125. }
  126. }
  127. response.Ok("操作成功", c)
  128. }
  129. func (this *MyChartController) CollectCancel(c *gin.Context) {
  130. var req request.MyChartCollectCancelReq
  131. if c.ShouldBind(&req) != nil {
  132. response.Fail("参数有误", c)
  133. return
  134. }
  135. if req.Authorization == "" {
  136. response.Fail("用户身份有误", c)
  137. return
  138. }
  139. if req.UniqueCode == "" {
  140. response.Fail("请选择图表", c)
  141. return
  142. }
  143. // 获取用户信息
  144. userInfo, e := userService.GetUserInfoByToken(req.Authorization)
  145. if e != nil {
  146. response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
  147. return
  148. }
  149. if userInfo.UserID <= 0 {
  150. response.FailMsg("操作失败", "Token获取有效用户失败", c)
  151. return
  152. }
  153. userId := int(userInfo.UserID)
  154. // 获取图表信息
  155. chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
  156. if e != nil {
  157. response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
  158. return
  159. }
  160. if chartInfo.ChartInfoId <= 0 {
  161. response.FailMsg("操作失败", "图表信息有误", c)
  162. return
  163. }
  164. cond := `user_id = ? AND chart_info_id = ?`
  165. pars := make([]interface{}, 0)
  166. pars = append(pars, userId, chartInfo.ChartInfoId)
  167. ob := new(yb_my_chart.YbMyChart)
  168. item, e := ob.FetchByCondition(cond, pars)
  169. if e != nil {
  170. response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
  171. return
  172. }
  173. if e = item.Delete(); e != nil {
  174. response.FailMsg("操作失败", "取消用户图表收藏失败, Err: "+e.Error(), c)
  175. return
  176. }
  177. response.Ok("操作成功", c)
  178. }
  179. func (this *MyChartController) RelateClassify(c *gin.Context) {
  180. userInfo := userService.GetInfoByClaims(c)
  181. userId := int(userInfo.UserID)
  182. if userId <= 0 {
  183. response.Fail("请登录后操作", c)
  184. return
  185. }
  186. var req request.MyChartRelateClassifyReq
  187. if c.ShouldBind(&req) != nil {
  188. response.Fail("参数有误", c)
  189. return
  190. }
  191. if req.ChartInfoId <= 0 {
  192. response.Fail("请选择图表", c)
  193. return
  194. }
  195. if req.ClassifyId <= 0 {
  196. response.Fail("请选择分类", c)
  197. return
  198. }
  199. cond := `user_id = ? AND chart_info_id = ?`
  200. pars := make([]interface{}, 0)
  201. pars = append(pars, userId, req.ChartInfoId)
  202. ob := new(yb_my_chart.YbMyChart)
  203. item, e := ob.FetchByCondition(cond, pars)
  204. if e != nil {
  205. response.FailMsg("请先收藏该图表", "获取用户图表失败, Err: "+e.Error(), c)
  206. return
  207. }
  208. item.MyChartClassifyID = req.ClassifyId
  209. item.ModifyTime = time.Now().Local()
  210. if e = item.Update([]string{"MyChartClassifyID", "ModifyTime"}); e != nil {
  211. response.FailMsg("操作失败", "更新用户图表关联分类失败, Err: "+e.Error(), c)
  212. return
  213. }
  214. response.Ok("操作成功", c)
  215. }