package controllers import ( "eta/eta_mini_api/models" "eta/eta_mini_api/models/response" "eta/eta_mini_api/services" "eta/eta_mini_api/services/alarm_msg" "eta/eta_mini_api/utils" "fmt" ) type ChartController struct { BaseAuthController } // @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 *ChartController) List() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") result, err := services.GetChartList(currentIndex, pageSize) if err != nil { br.Msg = "查询图表失败" br.ErrMsg = "查询图表失败,系统异常,Err:" + err.Error() return } if result.Ret != 200 { br.Msg = "查询图表失败" br.ErrMsg = result.ErrMsg return } resp := new(response.ChartListResp) resp.List = result.Data.List resp.Paging = result.Data.Paging br.Msg = "查询图表成功" br.Data = result.Data 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 *ChartController) Detail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user.Status != utils.UserStatusFormal { br.Msg = "用户没有权限查看图表" return } 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 && result.Data.UniqueCode == "" { // 说明后台删除了这个图表,那么尝试将收藏的图表也删除 alarm_msg.SendAlarmMsg(fmt.Sprintf("图表不存在,删除图表,id:%d", chartInfoId), 1) 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 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 *ChartController) Locate() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") if currentIndex <= 0 { currentIndex = 1 } if pageSize <= 0 { pageSize = 2 } result, err := services.GetChartList(currentIndex, pageSize) if err != nil { br.Msg = "查询图表失败" br.ErrMsg = "查询图表失败,系统异常,Err:" + err.Error() return } if result.Ret != 200 { br.Msg = result.Msg br.ErrMsg = result.ErrMsg return } total := len(result.Data.List) charts := make([]*response.ChartLocateItem, 0) items := result.Data.List 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 } tmpLocate := &response.ChartLocateItem{ ChartInfoId: v.ChartInfoId, ChartName: v.ChartName, UniqueCode: v.UniqueCode, PrevChartInfoId: prevChartInfoId, NextChartInfoId: nextChartInfoId, } charts = append(charts, tmpLocate) } br.Data = charts br.Msg = "查询成功" br.Success = true br.Ret = 200 }