my_chart.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. responseModel "hongze/hongze_yb/models/response"
  8. chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
  9. "hongze/hongze_yb/models/tables/yb_config"
  10. "hongze/hongze_yb/models/tables/yb_my_chart"
  11. userService "hongze/hongze_yb/services/user"
  12. "hongze/hongze_yb/utils"
  13. "strconv"
  14. "time"
  15. )
  16. // MyChartController 用户-我的图表
  17. type MyChartController struct{}
  18. // List 我的图表列表
  19. func (this *MyChartController) List(c *gin.Context) {
  20. var req request.MyChartCollectListReq
  21. if c.Bind(&req) != nil {
  22. response.Fail("参数有误", c)
  23. return
  24. }
  25. cond := `1 = 1`
  26. pars := make([]interface{}, 0)
  27. if req.Keyword != "" {
  28. kw := fmt.Sprint("%", req.Keyword, "%")
  29. cond += ` AND chart_name LIKE ?`
  30. pars = append(pars, kw)
  31. }
  32. if req.ClassifyId > 0 {
  33. cond += ` AND my_chart_classify_id = ?`
  34. pars = append(pars, req.ClassifyId)
  35. }
  36. ob := new(yb_my_chart.YbMyChart)
  37. list, e := ob.List(cond, pars)
  38. if e != nil {
  39. response.FailMsg("获取失败", "获取用户图表列表失败, Err:"+e.Error(), c)
  40. return
  41. }
  42. respList := make([]*responseModel.MyChartItem, 0)
  43. for i := range list {
  44. respList = append(respList, &responseModel.MyChartItem{
  45. MyChartID: list[i].MyChartID,
  46. MyChartClassifyID: list[i].MyChartClassifyID,
  47. ChartInfoID: list[i].ChartInfoID,
  48. ChartName: list[i].ChartName,
  49. UniqueCode: list[i].UniqueCode,
  50. ChartImage: list[i].ChartImage,
  51. UserID: list[i].UserID,
  52. ReportID: list[i].ReportID,
  53. ReportChapterID: list[i].ReportChapterID,
  54. CreateTime: utils.TimeTransferString(utils.FormatDateTime, list[i].CreateTime),
  55. })
  56. }
  57. response.OkData("获取成功", respList, c)
  58. }
  59. // Collect 收藏图表
  60. func (this *MyChartController) Collect(c *gin.Context) {
  61. var req request.MyChartCollectReq
  62. if c.ShouldBind(&req) != nil {
  63. response.Fail("参数有误", c)
  64. return
  65. }
  66. if req.Authorization == "" {
  67. response.Fail("用户身份有误", c)
  68. return
  69. }
  70. if req.UniqueCode == "" {
  71. response.Fail("请选择图表", c)
  72. return
  73. }
  74. if req.ReportId <= 0 {
  75. response.Fail("请选择报告", c)
  76. return
  77. }
  78. // 获取用户信息
  79. userInfo, e := userService.GetUserInfoByToken(req.Authorization)
  80. if e != nil {
  81. response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
  82. return
  83. }
  84. if userInfo.UserID <= 0 {
  85. response.FailMsg("操作失败", "Token获取有效用户失败", c)
  86. return
  87. }
  88. userId := int(userInfo.UserID)
  89. // 获取图表信息
  90. chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
  91. if e != nil {
  92. response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
  93. return
  94. }
  95. if chartInfo.ChartInfoId <= 0 {
  96. response.FailMsg("操作失败", "图表信息有误", c)
  97. return
  98. }
  99. // 图表收藏上限
  100. ob := new(yb_my_chart.YbMyChart)
  101. countCond := `user_id = ?`
  102. countPars := make([]interface{}, 0)
  103. countPars = append(countPars, userId)
  104. count, e := ob.Count(countCond, countPars)
  105. if e != nil {
  106. response.FailMsg("操作失败", "获取用户图表收藏数量失败, Err: "+e.Error(), c)
  107. return
  108. }
  109. // 获取图表收藏上限配置
  110. configCond := `config_code = ?`
  111. configPars := make([]interface{}, 0)
  112. configPars = append(configPars, yb_config.UserChartCollectMax)
  113. confOB := new(yb_config.YbConfig)
  114. conf, e := confOB.Fetch(configCond, configPars)
  115. if e != nil {
  116. response.FailMsg("操作失败", "获取图表收藏上限配置失败, Err: "+e.Error(), c)
  117. return
  118. }
  119. max, e := strconv.Atoi(conf.ConfigValue)
  120. if e != nil {
  121. response.FailMsg("操作失败", "图表收藏上限配置有误, Err: "+e.Error(), c)
  122. return
  123. }
  124. if int(count) >= max {
  125. response.Fail(fmt.Sprintf("图表最多可收藏%d张", max), c)
  126. return
  127. }
  128. // 是否已收藏
  129. cond := `user_id = ? AND chart_info_id = ?`
  130. pars := make([]interface{}, 0)
  131. pars = append(pars, userId, chartInfo.ChartInfoId)
  132. exists, e := ob.FetchByCondition(cond, pars)
  133. if e != nil && e != utils.ErrNoRow {
  134. response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
  135. return
  136. }
  137. nowTime := time.Now().Local()
  138. if exists != nil && exists.MyChartID > 0 {
  139. // 更新收藏信息
  140. exists.ChartName = chartInfo.ChartName
  141. exists.ChartImage = chartInfo.ChartImage
  142. exists.ReportID = req.ReportId
  143. exists.ReportChapterID = req.ReportChapterId
  144. exists.ModifyTime = nowTime
  145. updateCols := []string{"ChartName", "ChartImage", "ReportID", "ReportChapterID", "ModifyTime"}
  146. if e = exists.Update(updateCols); e != nil {
  147. response.FailMsg("操作失败", "更新收藏失败, Err: "+e.Error(), c)
  148. return
  149. }
  150. } else {
  151. // 新增收藏
  152. ob.ChartInfoID = chartInfo.ChartInfoId
  153. ob.ChartName = chartInfo.ChartName
  154. ob.UniqueCode = chartInfo.UniqueCode
  155. ob.ChartImage = chartInfo.ChartImage
  156. ob.UserID = userId
  157. ob.ReportID = req.ReportId
  158. ob.ReportChapterID = req.ReportChapterId
  159. ob.CreateTime = nowTime
  160. ob.ModifyTime = nowTime
  161. if e = ob.Create(); e != nil {
  162. response.FailMsg("操作失败", "新增收藏失败, Err: "+e.Error(), c)
  163. return
  164. }
  165. }
  166. response.Ok("操作成功", c)
  167. }
  168. // 取消收藏 CollectCancel
  169. func (this *MyChartController) CollectCancel(c *gin.Context) {
  170. var req request.MyChartCollectCancelReq
  171. if c.ShouldBind(&req) != nil {
  172. response.Fail("参数有误", c)
  173. return
  174. }
  175. if req.Authorization == "" {
  176. response.Fail("用户身份有误", c)
  177. return
  178. }
  179. if req.UniqueCode == "" {
  180. response.Fail("请选择图表", c)
  181. return
  182. }
  183. // 获取用户信息
  184. userInfo, e := userService.GetUserInfoByToken(req.Authorization)
  185. if e != nil {
  186. response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
  187. return
  188. }
  189. if userInfo.UserID <= 0 {
  190. response.FailMsg("操作失败", "Token获取有效用户失败", c)
  191. return
  192. }
  193. userId := int(userInfo.UserID)
  194. // 获取图表信息
  195. chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
  196. if e != nil {
  197. response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
  198. return
  199. }
  200. if chartInfo.ChartInfoId <= 0 {
  201. response.FailMsg("操作失败", "图表信息有误", c)
  202. return
  203. }
  204. cond := `user_id = ? AND chart_info_id = ?`
  205. pars := make([]interface{}, 0)
  206. pars = append(pars, userId, chartInfo.ChartInfoId)
  207. ob := new(yb_my_chart.YbMyChart)
  208. item, e := ob.FetchByCondition(cond, pars)
  209. if e != nil {
  210. response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
  211. return
  212. }
  213. if e = item.Delete(); e != nil {
  214. response.FailMsg("操作失败", "取消用户图表收藏失败, Err: "+e.Error(), c)
  215. return
  216. }
  217. response.Ok("操作成功", c)
  218. }
  219. // RelateClassify 加入分类
  220. func (this *MyChartController) RelateClassify(c *gin.Context) {
  221. userInfo := userService.GetInfoByClaims(c)
  222. userId := int(userInfo.UserID)
  223. if userId <= 0 {
  224. response.Fail("请登录后操作", c)
  225. return
  226. }
  227. var req request.MyChartRelateClassifyReq
  228. if c.ShouldBind(&req) != nil {
  229. response.Fail("参数有误", c)
  230. return
  231. }
  232. if req.MyChartId <= 0 {
  233. response.Fail("请选择图表", c)
  234. return
  235. }
  236. if req.ClassifyId <= 0 {
  237. response.Fail("请选择分类", c)
  238. return
  239. }
  240. ob := new(yb_my_chart.YbMyChart)
  241. item, e := ob.Fetch(req.MyChartId)
  242. if e != nil {
  243. response.FailMsg("图表信息有误", "获取用户图表失败, Err: "+e.Error(), c)
  244. return
  245. }
  246. if item.UserID != userId {
  247. response.Fail("无权操作", c)
  248. return
  249. }
  250. item.MyChartClassifyID = req.ClassifyId
  251. item.ModifyTime = time.Now().Local()
  252. if e = item.Update([]string{"MyChartClassifyID", "ModifyTime"}); e != nil {
  253. response.FailMsg("操作失败", "更新用户图表关联分类失败, Err: "+e.Error(), c)
  254. return
  255. }
  256. response.Ok("操作成功", c)
  257. }
  258. // IsCollect 是否已收藏
  259. func (this *MyChartController) IsCollect(c *gin.Context) {
  260. var req request.MyChartIsCollectReq
  261. if c.Bind(&req) != nil {
  262. response.Fail("参数有误", c)
  263. return
  264. }
  265. if req.Authorization == "" {
  266. response.Fail("用户身份有误", c)
  267. return
  268. }
  269. if req.UniqueCode == "" {
  270. response.Fail("请选择图表", c)
  271. return
  272. }
  273. // 获取用户信息
  274. userInfo, e := userService.GetUserInfoByToken(req.Authorization)
  275. if e != nil {
  276. response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
  277. return
  278. }
  279. if userInfo.UserID <= 0 {
  280. response.FailMsg("操作失败", "Token获取有效用户失败", c)
  281. return
  282. }
  283. userId := int(userInfo.UserID)
  284. // 获取图表信息
  285. chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
  286. if e != nil {
  287. response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
  288. return
  289. }
  290. if chartInfo.ChartInfoId <= 0 {
  291. response.FailMsg("操作失败", "图表信息有误", c)
  292. return
  293. }
  294. // 是否已收藏
  295. ob := new(yb_my_chart.YbMyChart)
  296. cond := `user_id = ? AND chart_info_id = ?`
  297. pars := make([]interface{}, 0)
  298. pars = append(pars, userId, chartInfo.ChartInfoId)
  299. exists, e := ob.FetchByCondition(cond, pars)
  300. if e != nil && e != utils.ErrNoRow {
  301. response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
  302. return
  303. }
  304. isCollect := false
  305. if exists != nil && exists.MyChartID > 0 {
  306. isCollect = true
  307. }
  308. response.OkData("获取成功", isCollect, c)
  309. }