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" ) // EdbCollectController 指标收藏 type EdbCollectController 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 /edb_collect/list [get] func (this *EdbCollectController) 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.EdbInfoChartListResp) resp := new(data_manage.CollectEdbInfoListResp) resp.List = make([]*data_manage.CollectEdbInfoItem, 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.GetEdbCollectClassifyChildIds(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.edb_collect_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds))) pars = append(pars, classifyIds) } // 获取当前账号的不可见指标 { obj := data_manage.EdbInfoNoPermissionAdmin{} list, e := obj.GetAllListByAdminId(sysUser.AdminId) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取不可见指标配置数据失败,Err:" + e.Error() return } var edbIds []int for _, v := range list { edbIds = append(edbIds, v.EdbInfoId) } edbLen := len(edbIds) if edbLen > 0 { cond += fmt.Sprintf(` AND a.edb_info_id NOT IN (%s)`, utils.GetOrmInReplace(edbLen)) pars = append(pars, edbIds) } } // 关键词搜索 keywords := this.GetString("Keyword") keywords = strings.TrimSpace(keywords) if keywords != "" { kw := fmt.Sprint("%", keywords, "%") cond += fmt.Sprintf(` AND (b.edb_code LIKE ? OR b.edb_name LIKE ?)`) pars = append(pars, kw, kw) } // 获取指标列表 dataCount, e := data_manage.GetCollectEdbInfoCount(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.GetCollectEdbInfoPageList(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.FormatEdbInfo2CollectItem(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.EdbCollectReq true "type json string" // @Success Ret=200 保存成功 // @router /edb_collect/collect [post] func (this *EdbCollectController) 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.EdbCollectReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e) return } if req.EdbInfoId <= 0 { br.Msg = "请选择指标" return } edbItem, e := data_manage.GetEdbInfoById(req.EdbInfoId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "指标不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取指标信息失败: %v", e) return } collectOb := new(data_manage.EdbCollect) // 待添加的分类配置 addList := make([]*data_manage.EdbCollect, 0) for _, classifyId := range req.ClassifyIdList { addList = append(addList, &data_manage.EdbCollect{ EdbCollectClassifyId: classifyId, EdbInfoId: req.EdbInfoId, EdbCode: edbItem.EdbCode, 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().EdbInfoId) pars := make([]interface{}, 0) pars = append(pars, sysUser.AdminId, req.EdbInfoId) // 清除原有保存收藏配置,并新增保存收藏 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.EdbCollectReq true "type json string" // @Success Ret=200 保存成功 // @router /edb_collect/cancel_collect [post] func (this *EdbCollectController) 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.EdbCollectReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e) return } if req.EdbInfoId <= 0 { br.Msg = "请选择指标" return } if req.ClassifyId <= 0 { br.Msg = "请选择分类" return } // 取消收藏 collectOb := new(data_manage.EdbCollect) cond := fmt.Sprintf("%s = ? AND %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().EdbCollectClassifyId, collectOb.Cols().EdbInfoId) pars := make([]interface{}, 0) pars = append(pars, sysUser.AdminId, req.ClassifyId, req.EdbInfoId) 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.EdbCollectMoveReq // @router /edb_collect/move [post] func (this *EdbCollectController) 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.EdbCollectMoveReq 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.PrevEdbInfoId <= 0 { firstOne, err := data_manage.GetEdbCollectSort(sysUser.AdminId, req.ClassifyId, 0) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error() return } newSort = firstOne.Sort - 1 } else if req.NextEdbInfoId <= 0 { lastOne, err := data_manage.GetEdbCollectSort(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.GetEdbCollectByEdbInfoId(sysUser.AdminId, req.PrevEdbInfoId, req.ClassifyId) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error() return } nextMapItem, err := data_manage.GetEdbCollectByEdbInfoId(sysUser.AdminId, req.NextEdbInfoId, 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.UpdateEdbCollectSortByClassifyId(req.ClassifyId, preMapItem.Sort, preMapItem.EdbCollectId, updateSortStr) } } if e := data_manage.UpdateEdbCollectMove(newSort, sysUser.AdminId, req.EdbInfoId, req.ClassifyId); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("更新指标收藏失败, %v", e) return } br.Ret = 200 br.Success = true br.Msg = "操作成功" }