package data_manage import ( "encoding/json" "eta_gn/eta_api/controllers" "eta_gn/eta_api/models" "eta_gn/eta_api/models/data_manage" "eta_gn/eta_api/services/data" "eta_gn/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "sort" "strings" "time" ) // ChartCollectController 图表收藏 type ChartCollectController struct { controllers.BaseAuthController } // List // @Title 收藏列表-分页 // @Description 收藏列表-分页 // @Param PageSize query int false "每页数据量" // @Param CurrentIndex query int false "页码" // @Param ClassifyId query int false "分类ID" // @Param Keyword query string false "搜索关键词:图表ID/图表名称" // @Success Ret=200 保存成功 // @router /chart_collect/list [get] func (this *ChartCollectController) List() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } //resp := new(response.ChartInfoChartListResp) resp := new(data_manage.CollectChartInfoListResp) resp.List = make([]*data_manage.CollectChartInfoItem, 0) // 分页 pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") var total int page := paging.GetPaging(currentIndex, pageSize, total) var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) // 当前用户 var cond string var pars []interface{} cond += ` AND a.sys_user_id = ?` pars = append(pars, sysUser.AdminId) // 分类筛选 classifyId, _ := this.GetInt("ClassifyId") if classifyId > 0 { classifyIds, e := data.GetChartCollectClassifyChildIds(classifyId, sysUser.AdminId, true) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取子分类IDs失败, %v", e) return } if len(classifyIds) == 0 { resp.Paging = page br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" return } cond += fmt.Sprintf(` AND a.chart_collect_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds))) pars = append(pars, classifyIds) } // 关键词搜索 keywords := this.GetString("Keyword") keywords = strings.TrimSpace(keywords) if keywords != "" { kw := fmt.Sprint("%", keywords, "%") cond += fmt.Sprintf(` AND (b.chart_code LIKE ? OR b.chart_name LIKE ?)`) pars = append(pars, kw, kw) } // 获取图表列表 dataCount, e := data_manage.GetCollectChartInfoCount(cond, pars) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取收藏图表总数失败, %v", e) return } if dataCount == 0 { resp.Paging = page br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" return } list, e := data_manage.GetCollectChartInfoPageList(cond, pars, startSize, pageSize) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取收藏图表列表失败, %v", e) return } for _, v := range list { resp.List = append(resp.List, data_manage.FormatChartInfo2CollectItem(v)) } sort.Slice(resp.List, func(i, j int) bool { return resp.List[i].Sort < resp.List[j].Sort }) page = paging.GetPaging(currentIndex, pageSize, int(dataCount)) resp.Paging = page br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Collect // @Title 新增收藏 // @Description 新增收藏 // @Param request body data_manage.ChartCollectReq true "type json string" // @Success Ret=200 保存成功 // @router /chart_collect/collect [post] func (this *ChartCollectController) Collect() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var req data_manage.ChartCollectReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e) return } if req.ChartInfoId <= 0 { br.Msg = "请选择图表" return } chartItem, e := data_manage.GetChartInfoById(req.ChartInfoId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "图表不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取图表信息失败: %v", e) return } if chartItem.ChartInfoId <= 0 { br.Msg = "图表不存在, 请刷新页面" br.IsSendEmail = false return } collectOb := new(data_manage.ChartCollect) // 待添加的分类配置 addList := make([]*data_manage.ChartCollect, 0) for _, classifyId := range req.ClassifyIdList { addList = append(addList, &data_manage.ChartCollect{ ChartCollectClassifyId: classifyId, ChartInfoId: req.ChartInfoId, SysUserId: sysUser.AdminId, SysRealName: sysUser.RealName, CreateTime: time.Now().Local(), ModifyTime: time.Now().Local(), }) } cond := fmt.Sprintf(" %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().ChartInfoId) pars := make([]interface{}, 0) pars = append(pars, sysUser.AdminId, req.ChartInfoId) // 清除原有保存收藏配置,并新增保存收藏 e = collectOb.RemoveAndCreateMulti(cond, pars, addList) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("新增图表收藏失败: %v", e) return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // CancelCollect // @Title 取消收藏 // @Description 取消收藏 // @Param request body data_manage.ChartCollectReq true "type json string" // @Success Ret=200 保存成功 // @router /chart_collect/cancel_collect [post] func (this *ChartCollectController) CancelCollect() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var req data_manage.ChartCollectReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e) return } if req.ChartInfoId <= 0 { br.Msg = "请选择图表" return } if req.ClassifyId <= 0 { br.Msg = "请选择分类" return } // 取消收藏 collectOb := new(data_manage.ChartCollect) cond := fmt.Sprintf("%s = ? AND %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().ChartCollectClassifyId, collectOb.Cols().ChartInfoId) pars := make([]interface{}, 0) pars = append(pars, sysUser.AdminId, req.ClassifyId, req.ChartInfoId) if e := collectOb.RemoveByCondition(cond, pars); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("取消收藏失败: %v", e) return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Move // @Title 移动收藏 // @Description 移动收藏 // @Success 200 {object} data_manage.ChartCollectMoveReq // @router /chart_collect/move [post] func (this *ChartCollectController) Move() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var req data_manage.ChartCollectMoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e) return } var newSort int if req.PrevChartInfoId <= 0 { firstOne, err := data_manage.GetChartCollectSort(sysUser.AdminId, req.ClassifyId, 0) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error() return } newSort = firstOne.Sort - 1 } else if req.NextChartInfoId <= 0 { lastOne, err := data_manage.GetChartCollectSort(sysUser.AdminId, req.ClassifyId, 1) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error() return } newSort = lastOne.Sort + 1 } else { preMapItem, err := data_manage.GetChartCollectByChartInfoId(sysUser.AdminId, req.PrevChartInfoId, req.ClassifyId) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error() return } nextMapItem, err := data_manage.GetChartCollectByChartInfoId(sysUser.AdminId, req.NextChartInfoId, req.ClassifyId) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error() return } newSort = (preMapItem.Sort + nextMapItem.Sort) / 2 newSort = preMapItem.Sort + 1 var updateSortStr string if preMapItem.Sort == nextMapItem.Sort { updateSortStr = `sort + 2` } else if nextMapItem.Sort-preMapItem.Sort == 1 { updateSortStr = `sort + 1` } if updateSortStr != `` { _ = data_manage.UpdateChartCollectSortByClassifyId(req.ClassifyId, preMapItem.Sort, preMapItem.ChartCollectId, updateSortStr) } } if e := data_manage.UpdateChartCollectMove(newSort, sysUser.AdminId, req.ChartInfoId, req.ClassifyId); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("更新图表收藏失败, %v", e) return } br.Ret = 200 br.Success = true br.Msg = "操作成功" }