my_chart.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. // 此处可能出现同一个报告两个同样的图表均有"取消收藏"按钮,点击第一个取消之后再点击第二个会报错,此时进行一个友好的提示"已取消收藏"
  227. item, e := ob.FetchByCondition(cond, pars)
  228. if e != nil {
  229. response.FailMsg("已取消收藏", "获取用户图表失败, Err: "+e.Error(), c)
  230. return
  231. }
  232. if e = item.Delete(); e != nil {
  233. response.FailMsg("已取消收藏", "取消用户图表收藏失败, Err: "+e.Error(), c)
  234. return
  235. }
  236. response.Ok("操作成功", c)
  237. }
  238. // RelateClassify 加入分类
  239. func (this *MyChartController) RelateClassify(c *gin.Context) {
  240. userInfo := userService.GetInfoByClaims(c)
  241. userId := int(userInfo.UserID)
  242. if userId <= 0 {
  243. response.Fail("请登录后操作", c)
  244. return
  245. }
  246. var req request.MyChartRelateClassifyReq
  247. if c.ShouldBind(&req) != nil {
  248. response.Fail("参数有误", c)
  249. return
  250. }
  251. if req.MyChartId <= 0 {
  252. response.Fail("请选择图表", c)
  253. return
  254. }
  255. if req.ClassifyId <= 0 {
  256. response.Fail("请选择分类", c)
  257. return
  258. }
  259. ob := new(yb_my_chart.YbMyChart)
  260. item, e := ob.Fetch(req.MyChartId)
  261. if e != nil {
  262. response.FailMsg("图表信息有误", "获取用户图表失败, Err: "+e.Error(), c)
  263. return
  264. }
  265. if item.UserID != userId {
  266. response.Fail("无权操作", c)
  267. return
  268. }
  269. item.MyChartClassifyID = req.ClassifyId
  270. item.ModifyTime = time.Now().Local()
  271. if e = item.Update([]string{"MyChartClassifyID", "ModifyTime"}); e != nil {
  272. response.FailMsg("操作失败", "更新用户图表关联分类失败, Err: "+e.Error(), c)
  273. return
  274. }
  275. response.Ok("操作成功", c)
  276. }
  277. // IsCollect 是否已收藏
  278. func (this *MyChartController) IsCollect(c *gin.Context) {
  279. var req request.MyChartIsCollectReq
  280. if c.Bind(&req) != nil {
  281. response.Fail("参数有误", c)
  282. return
  283. }
  284. if req.Authorization == "" {
  285. response.Fail("用户身份有误", c)
  286. return
  287. }
  288. if req.UniqueCode == "" {
  289. response.Fail("请选择图表", c)
  290. return
  291. }
  292. // 获取用户信息
  293. userInfo, e := userService.GetUserInfoByToken(req.Authorization)
  294. if e != nil {
  295. response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
  296. return
  297. }
  298. if userInfo.UserID <= 0 {
  299. response.FailMsg("操作失败", "Token获取有效用户失败", c)
  300. return
  301. }
  302. userId := int(userInfo.UserID)
  303. // 获取图表信息
  304. chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
  305. if e != nil {
  306. response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
  307. return
  308. }
  309. if chartInfo.ChartInfoId <= 0 {
  310. response.FailMsg("操作失败", "图表信息有误", c)
  311. return
  312. }
  313. // 是否已收藏
  314. ob := new(yb_my_chart.YbMyChart)
  315. cond := `user_id = ? AND chart_info_id = ?`
  316. pars := make([]interface{}, 0)
  317. pars = append(pars, userId, chartInfo.ChartInfoId)
  318. exists, e := ob.FetchByCondition(cond, pars)
  319. if e != nil && e != utils.ErrNoRow {
  320. response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
  321. return
  322. }
  323. isCollect := false
  324. if exists != nil && exists.MyChartID > 0 {
  325. isCollect = true
  326. }
  327. response.OkData("获取成功", isCollect, c)
  328. }