my_chart.go 9.8 KB

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