package controllers import ( "encoding/json" "eta/eta_mini_api/models" "eta/eta_mini_api/models/request" "eta/eta_mini_api/models/response" "eta/eta_mini_api/services" "eta/eta_mini_api/utils" "time" "github.com/rdlucklib/rdluck_tools/paging" ) type MyChartController struct { BaseAuthController } // @Title 收藏图表 // @Description 收藏图表 // @Success 200 {object} models.BaseResponse // @router /collect [post] func (this *MyChartController) Collect() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MyChartCollectReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有权限收藏" return } if req.ChartInfoId == 0 { chartResp, err := services.GetChartDetail(req.ChartInfoId, req.UniqueCode) if err != nil { br.Msg = "收藏失败" br.Msg = "获取图表信息失败,系统错误,Err:" + err.Error() return } if chartResp.Ret != 200 { br.Msg = chartResp.Msg br.ErrMsg = chartResp.ErrMsg return } req.ChartInfoId = chartResp.Data.ChartInfoId } count, err := models.GetMyChartCount(user.UserId, req.UniqueCode) if err != nil { br.Msg = "收藏失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } if count > 0 { br.Msg = "该图表已收藏,请重新刷新页面" return } myChart := &models.MyChart{ UserId: user.UserId, UserRealName: user.RealName, UniqueCode: req.UniqueCode, ChartImage: req.ChartImage, ChartName: req.ChartName, ChartInfoId: req.ChartInfoId, CreateTime: time.Now(), ModifyTime: time.Now(), } err = myChart.Insert() if err != nil { br.Msg = "收藏失败" br.ErrMsg = "收藏失败,Err:" + err.Error() return } br.Msg = "收藏成功" br.Success = true br.Ret = 200 } // @Title 取消收藏 // @Description 取消收藏 // @Success 200 {object} models.BaseResponse // @router /collectCancel [post] func (this *MyChartController) CollectCancel() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MyChartCollectCancelReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有权限收藏" return } count, err := models.GetMyChartCount(user.UserId, req.UniqueCode) if err != nil { br.Msg = "取消收藏失败" br.ErrMsg = "获取收藏信息失败,Err:" + err.Error() return } if count == 0 { br.Msg = "该图表已取消收藏,请重新刷新页面" return } err = models.DeleteMyChart(user.UserId, req.UniqueCode) if err != nil { br.Msg = "取消收藏失败" br.ErrMsg = "取消收藏失败,Err:" + err.Error() return } br.Msg = "取消收藏成功" br.Success = true br.Ret = 200 } // @Title List // @Description create users // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Success 200 {object} models.BaseResponse // @router /list [get] func (this *MyChartController) List() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") if pageSize <= 0 { pageSize = 30 } if currentIndex <= 0 { currentIndex = 1 } user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有收藏权限" return } total, err := models.GetMyChartListCountById(user.UserId) if err != nil { br.Msg = "查询收藏数量失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } resp := new(response.MyChartListResp) startSize := utils.StartIndex(currentIndex, pageSize) items, err := models.GetMyChartListById(user.UserId, startSize, pageSize) if err != nil { br.Msg = "查询收藏失败" br.ErrMsg = "查询收藏失败,Err:" + err.Error() return } page := paging.GetPaging(currentIndex, pageSize, total) resp.List = items resp.Paging = page br.Data = resp br.Msg = "查询成功" br.Success = true br.Ret = 200 } // @Title Locate // @Description create users // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Success 200 {object} models.BaseResponse // @router /locate [get] func (this *MyChartController) Locate() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User total, err := models.GetMyChartListCountById(user.UserId) if err != nil { br.Msg = "查询收藏数量失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } charts := make([]*response.MyChartLocateItem, 0) items, err := models.GetMyChartListById(user.UserId, 0, total) if err != nil { br.Msg = "查询收藏失败" br.ErrMsg = "查询收藏失败,Err:" + err.Error() return } for k, v := range items { var prevChartInfoId, nextChartInfoId int switch k { case 0: prevChartInfoId = -1 if k < total-1 { nextChartInfoId = items[k+1].ChartInfoId } else { nextChartInfoId = -1 } case total - 1: nextChartInfoId = -1 if k > 0 { prevChartInfoId = items[k-1].ChartInfoId } default: prevChartInfoId = items[k-1].ChartInfoId nextChartInfoId = items[k+1].ChartInfoId } tmpChart := &response.MyChartLocateItem{ MyChartId: v.MyChartId, ChartInfoId: v.ChartInfoId, ChartName: v.ChartName, UniqueCode: v.UniqueCode, PrevChartInfoId: prevChartInfoId, NextChartInfoId: nextChartInfoId, } charts = append(charts, tmpChart) } br.Data = charts br.Msg = "查询成功" br.Success = true br.Ret = 200 } // @Title Detail // @Description 图表详情 // @Param ChartInfoId query int true "图表详情id" // @Success 200 {object} models.BaseResponse // @router /detail [get] func (this *MyChartController) Detail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User chartInfoId, _ := this.GetInt("ChartInfoId") if chartInfoId <= 0 { br.Msg = "图表id错误" return } result, err := services.GetChartDetail(chartInfoId, "") if err != nil { br.Msg = "获取图表详情失败" br.ErrMsg = "获取图表详情失败,Err:" + err.Error() return } if result.Ret != 200 { br.Msg = result.Msg br.ErrMsg = result.ErrMsg return } if result.Ret == 200 && result.Data.UniqueCode == "" { // 说明后台删除了这个图表,那么尝试将收藏的图表也删除 models.DeleteMyChartByUserIdAndChartInfoId(user.UserId, chartInfoId) br.Msg = "图表已删除或不存在,请刷新页面" return } count, err := models.GetMyChartCount(user.UserId, result.Data.UniqueCode) if err != nil { br.Msg = "图表已删除或不存在" br.ErrMsg = "获取图表详情失败,Err:" + err.Error() } if count > 0 { result.Data.IsCollect = true } br.Data = result.Data br.Msg = "查询成功" br.Success = true br.Ret = 200 } // @Title IsCollect // @Description create users // @Success 200 {object} models.BaseResponse // @router /isCollect [post] func (this *MyChartController) IsCollect() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MyChartIsCollectReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } user := this.User count, err := models.GetMyChartCount(user.UserId, req.UniqueCode) if err != nil { br.Msg = "查询收藏数量失败" br.ErrMsg = "查询收藏数量失败,Err:" + err.Error() return } resp := new(response.MyChartIsCollectResp) if count > 0 { resp.IsCollect = true } else { resp.IsCollect = false } br.Data = resp br.Msg = "查询成功" br.Success = true br.Ret = 200 }