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/models/data_manage/request" "eta_gn/eta_api/services/data" "eta_gn/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "sort" "strconv" "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 = "操作成功" } // BatchCollect // @Title 批量收藏 // @Description 批量收藏 // @Param request body data_manage.EdbCollectReq true "type json string" // @Success Ret=200 保存成功 // @router /edb_collect/batch_collect [post] func (this *EdbCollectController) BatchCollect() { 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 request.BatchEdbCollectReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常: %v", err) return } cacheKey := "CACHE_EDB_INFO_COLLECT_" + strconv.Itoa(sysUser.AdminId) if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) { br.Msg = "系统处理中,请稍后重试!" br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody) return } defer func() { utils.Rc.Delete(cacheKey) }() if len(req.CollectClassifyIdList) <= 0 { br.Msg = "请选择需要收藏的分类" br.IsSendEmail = false return } // 选中的指标id列表 edbInfoList := make([]*data_manage.EdbInfoList, 0) // 选择所有指标,所以需要通过es获取数据 if req.IsSelectAll { tmpEdbInfoList, err, errMsg := data.GetAllGeneralEdbInfoListByGeneralEdbEsSearchReq(req.GeneralEdbEsSearchReq, this.SysUser.AdminId) if err != nil { br.Msg = "获取指标列表失败!" if errMsg != `` { br.Msg = errMsg } br.ErrMsg = "获取指标列表失败,Err:" + err.Error() return } // 如果有过滤指标,那么就过滤吧 if len(req.NoEdbIdList) > 0 { noEdbIdMap := make(map[int]bool) for _, v := range req.NoEdbIdList { noEdbIdMap[v] = true } for _, v := range tmpEdbInfoList { if _, ok := noEdbIdMap[v.EdbInfoId]; !ok { // 如果不在未选中的指标id列表中,那么就加入到选中的指标id列表 edbInfoList = append(edbInfoList, v) } } } else { edbInfoList = tmpEdbInfoList } // 因为是ES查找的,所以需要重新查一下指标的信息,主要是为了把是否授权字段找出来 if len(edbInfoList) > 0 { edbInfoIdList := make([]int, 0) for _, v := range edbInfoList { v.ConvertToResp() v.EdbNameAlias = v.EdbName v.HaveOperaAuth = true edbInfoIdList = append(edbInfoIdList, v.EdbInfoId) } tmpEdbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取所有有权限的指标失败,Err:" + err.Error() return } edbInfoMap := make(map[int]*data_manage.EdbInfo) for _, v := range tmpEdbList { edbInfoMap[v.EdbInfoId] = v } for _, v := range edbInfoList { tmpEdbInfo, ok := edbInfoMap[v.EdbInfoId] if !ok { continue } v.IsJoinPermission = tmpEdbInfo.IsJoinPermission } } } else { // 因为是ES查找的,所以需要重新查一下指标的信息,主要是为了把是否授权字段找出来 if len(req.EdbIdList) > 0 { edbInfoList, err = data_manage.GetEdbInfoListByCond(` AND edb_info_id in (?) `, []interface{}{req.EdbIdList}) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取所有有权限的指标失败,Err:" + err.Error() return } } } if len(edbInfoList) <= 0 { br.Msg = `请选择指标` return } edbInfoIdList := make([]int, 0) for _, v := range edbInfoList { edbInfoIdList = append(edbInfoIdList, v.EdbInfoId) } collectOb := new(data_manage.EdbCollect) cond := fmt.Sprintf(" AND %s = ? AND %s in (?) ", collectOb.Cols().SysUserId, collectOb.Cols().EdbInfoId) pars := make([]interface{}, 0) pars = append(pars, sysUser.AdminId, edbInfoIdList) collectList, err := collectOb.GetItemsByCondition(cond, pars, []string{}, ``) if err != nil { br.Msg = "操作失败" br.ErrMsg = "获取收藏列表列表,Err:" + err.Error() return } collectEdbClassifyMap := make(map[int]map[int]bool) for _, v := range collectList { edbClassifyMap, ok := collectEdbClassifyMap[v.EdbInfoId] if !ok { edbClassifyMap = make(map[int]bool) } edbClassifyMap[v.EdbCollectClassifyId] = true collectEdbClassifyMap[v.EdbInfoId] = edbClassifyMap } // 待添加的分类配置 addList := make([]*data_manage.EdbCollect, 0) for _, edbInfo := range edbInfoList { for _, classifyId := range req.CollectClassifyIdList { edbClassifyMap, ok := collectEdbClassifyMap[edbInfo.EdbInfoId] if ok { if _, ok2 := edbClassifyMap[classifyId]; ok2 { // 如果当前收藏分类下,存在该指标的收藏记录,那么就跳过 continue } } addList = append(addList, &data_manage.EdbCollect{ EdbCollectClassifyId: classifyId, EdbInfoId: edbInfo.EdbInfoId, EdbCode: edbInfo.EdbCode, SysUserId: sysUser.AdminId, SysRealName: sysUser.RealName, CreateTime: time.Now().Local(), ModifyTime: time.Now().Local(), }) } } // 并新增保存收藏 err = collectOb.CreateMulti(addList) if err != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("新增指标批量收藏失败: %v", err) return } br.Ret = 200 br.Success = true br.Msg = "操作成功" }