123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- package my_chart
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "hongze/hongze_yb/controller/response"
- "hongze/hongze_yb/models/request"
- chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
- "hongze/hongze_yb/models/tables/yb_config"
- "hongze/hongze_yb/models/tables/yb_my_chart"
- userService "hongze/hongze_yb/services/user"
- "hongze/hongze_yb/utils"
- "strconv"
- "time"
- )
- // MyChartController 用户-我的图表
- type MyChartController struct{}
- func (this *MyChartController) List(c *gin.Context) {
- }
- func (this *MyChartController) Detail(c *gin.Context) {
- }
- func (this *MyChartController) Collect(c *gin.Context) {
- var req request.MyChartCollectReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数有误", c)
- return
- }
- if req.Authorization == "" {
- response.Fail("用户身份有误", c)
- return
- }
- if req.UniqueCode == "" {
- response.Fail("请选择图表", c)
- return
- }
- if req.ReportId <= 0 {
- response.Fail("请选择报告", c)
- return
- }
- // 获取用户信息
- userInfo, e := userService.GetUserInfoByToken(req.Authorization)
- if e != nil {
- response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
- return
- }
- if userInfo.UserID <= 0 {
- response.FailMsg("操作失败", "Token获取有效用户失败", c)
- return
- }
- userId := int(userInfo.UserID)
- // 获取图表信息
- chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
- if e != nil {
- response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
- return
- }
- if chartInfo.ChartInfoId <= 0 {
- response.FailMsg("操作失败", "图表信息有误", c)
- return
- }
- // 图表收藏上限
- ob := new(yb_my_chart.YbMyChart)
- countCond := `user_id = ?`
- countPars := make([]interface{}, 0)
- countPars = append(countPars, userId)
- count, e := ob.Count(countCond, countPars)
- if e != nil {
- response.FailMsg("操作失败", "获取用户图表收藏数量失败, Err: "+e.Error(), c)
- return
- }
- // 获取图表收藏上限配置
- configCond := `config_code = ?`
- configPars := make([]interface{}, 0)
- configPars = append(configPars, yb_config.UserChartCollectMax)
- confOB := new(yb_config.YbConfig)
- conf, e := confOB.Fetch(configCond, configPars)
- if e != nil {
- response.FailMsg("操作失败", "获取图表收藏上限配置失败, Err: "+e.Error(), c)
- return
- }
- max, e := strconv.Atoi(conf.ConfigValue)
- if e != nil {
- response.FailMsg("操作失败", "图表收藏上限配置有误, Err: "+e.Error(), c)
- return
- }
- if int(count) >= max {
- response.Fail(fmt.Sprintf("图表最多可收藏%d张", max), c)
- return
- }
- // 是否已收藏
- cond := `user_id = ? AND chart_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, userId, chartInfo.ChartInfoId)
- exists, e := ob.FetchByCondition(cond, pars)
- if e != nil && e != utils.ErrNoRow {
- response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
- return
- }
- nowTime := time.Now().Local()
- if exists != nil && exists.MyChartID > 0 {
- // 更新收藏信息
- exists.ChartName = chartInfo.ChartName
- exists.ChartImage = chartInfo.ChartImage
- exists.ReportID = req.ReportId
- exists.ReportChapterID = req.ReportChapterId
- exists.ModifyTime = nowTime
- updateCols := []string{"ChartName", "ChartImage", "ReportID", "ReportChapterID", "ModifyTime"}
- if e = exists.Update(updateCols); e != nil {
- response.FailMsg("操作失败", "更新收藏失败, Err: "+e.Error(), c)
- return
- }
- } else {
- // 新增收藏
- ob.ChartInfoID = chartInfo.ChartInfoId
- ob.ChartName = chartInfo.ChartName
- ob.UniqueCode = chartInfo.UniqueCode
- ob.ChartImage = chartInfo.ChartImage
- ob.UserID = userId
- ob.ReportID = req.ReportId
- ob.ReportChapterID = req.ReportChapterId
- ob.CreateTime = nowTime
- ob.ModifyTime = nowTime
- if e = ob.Create(); e != nil {
- response.FailMsg("操作失败", "新增收藏失败, Err: "+e.Error(), c)
- return
- }
- }
- response.Ok("操作成功", c)
- }
- func (this *MyChartController) CollectCancel(c *gin.Context) {
- var req request.MyChartCollectCancelReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数有误", c)
- return
- }
- if req.Authorization == "" {
- response.Fail("用户身份有误", c)
- return
- }
- if req.UniqueCode == "" {
- response.Fail("请选择图表", c)
- return
- }
- // 获取用户信息
- userInfo, e := userService.GetUserInfoByToken(req.Authorization)
- if e != nil {
- response.FailMsg("操作失败", "Token获取有效用户失败, Err: "+e.Error(), c)
- return
- }
- if userInfo.UserID <= 0 {
- response.FailMsg("操作失败", "Token获取有效用户失败", c)
- return
- }
- userId := int(userInfo.UserID)
- // 获取图表信息
- chartInfo, e := chartInfoModel.GetChartInfoViewByUniqueCode(req.UniqueCode)
- if e != nil {
- response.FailMsg("操作失败", "UniqueCode获取图表信息失败, Err:"+e.Error(), c)
- return
- }
- if chartInfo.ChartInfoId <= 0 {
- response.FailMsg("操作失败", "图表信息有误", c)
- return
- }
- cond := `user_id = ? AND chart_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, userId, chartInfo.ChartInfoId)
- ob := new(yb_my_chart.YbMyChart)
- item, e := ob.FetchByCondition(cond, pars)
- if e != nil {
- response.FailMsg("操作失败", "获取用户图表失败, Err: "+e.Error(), c)
- return
- }
- if e = item.Delete(); e != nil {
- response.FailMsg("操作失败", "取消用户图表收藏失败, Err: "+e.Error(), c)
- return
- }
- response.Ok("操作成功", c)
- }
- func (this *MyChartController) RelateClassify(c *gin.Context) {
- userInfo := userService.GetInfoByClaims(c)
- userId := int(userInfo.UserID)
- if userId <= 0 {
- response.Fail("请登录后操作", c)
- return
- }
- var req request.MyChartRelateClassifyReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数有误", c)
- return
- }
- if req.ChartInfoId <= 0 {
- response.Fail("请选择图表", c)
- return
- }
- if req.ClassifyId <= 0 {
- response.Fail("请选择分类", c)
- return
- }
- cond := `user_id = ? AND chart_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, userId, req.ChartInfoId)
- ob := new(yb_my_chart.YbMyChart)
- item, e := ob.FetchByCondition(cond, pars)
- if e != nil {
- response.FailMsg("请先收藏该图表", "获取用户图表失败, Err: "+e.Error(), c)
- return
- }
- item.MyChartClassifyID = req.ClassifyId
- item.ModifyTime = time.Now().Local()
- if e = item.Update([]string{"MyChartClassifyID", "ModifyTime"}); e != nil {
- response.FailMsg("操作失败", "更新用户图表关联分类失败, Err: "+e.Error(), c)
- return
- }
- response.Ok("操作成功", c)
- }
|