123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- 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
- }
|