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/models/data_manage/response" "eta_gn/eta_api/models/system" "eta_gn/eta_api/services/data" "eta_gn/eta_api/services/elastic" "eta_gn/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "strconv" "strings" ) // EdbInfoShareController 数据管理 type EdbInfoShareController struct { controllers.BaseAuthController } // UserList // @Title 获取指标设置共享的详情 // @Description 获取指标详情接口 // @Param EdbInfoId query int true "指标id" // @Success 200 {object} response.EdbInfoShareUserResp // @router /edb_info/share/user_list [get] func (c *EdbInfoShareController) UserList() { br := new(models.BaseResponse).Init() defer func() { c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } edbInfoId, _ := c.GetInt("EdbInfoId") if edbInfoId <= 0 { br.Msg = "参数错误" br.ErrMsg = "参数错误" return } edbInfo, err := data_manage.GetEdbInfoById(edbInfoId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取指标信息失败,Err:" + err.Error() return } if edbInfo.SysUserId != sysUser.AdminId { br.Msg = "您没有权限设置共享" br.ErrMsg = "您没有权限设置共享,EdbInfoId:" + strconv.Itoa(edbInfo.EdbInfoId) br.IsSendEmail = false return } resp := response.EdbInfoShareUserResp{} obj := data_manage.EdbInfoShare{} list, err := obj.GetListByEdbInfoId(edbInfoId) if err != nil { br.Msg = `获取失败` br.ErrMsg = `获取失败:` + err.Error() return } resp.List = list br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // Save // @Title 单个指标设置共享 // @Description 单个指标设置共享 // @Param request body request.SetEdbInfoShareReq true "type json string" // @Success 200 {object} data_manage.EdbInfo // @router /edb_info/share/save [post] func (c *EdbInfoShareController) Save() { br := new(models.BaseResponse).Init() defer func() { c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var req request.SetEdbInfoShareReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.EdbInfoId <= 0 { br.Msg = `请选择指标` br.IsSendEmail = false return } edbItem, err := data_manage.GetEdbInfoById(req.EdbInfoId) if err != nil { br.Msg = "查找指标失败" br.ErrMsg = "查找指标失败,Err:" + err.Error() return } if edbItem.EdbInfoId <= 0 { br.Msg = "指标不存在" br.ErrMsg = "指标不存在,ChartInfoId:" + strconv.Itoa(req.EdbInfoId) br.IsSendEmail = false return } if edbItem.SysUserId != c.SysUser.AdminId { br.Msg = "您没有权限设置共享" br.ErrMsg = "您没有权限设置共享,EdbInfoId:" + strconv.Itoa(req.EdbInfoId) br.IsSendEmail = false return } obj := data_manage.EdbInfoShare{} err = obj.SaveEdbInfoShare([]int{req.EdbInfoId}, req.UserIdList, req.ShareType) if err != nil { br.Msg = `保存失败` br.ErrMsg = `保存失败:` + err.Error() return } // 更新es data.AddOrEditEdbInfoToEs(req.EdbInfoId) br.Ret = 200 br.Success = true br.Msg = "保存成功" } // Tree // @Title 获取共享指标的分类/指标树 // @Description 获取共享指标的分类/指标树 // @Success 200 {object} response.EdbShareListResp // @router /edb_info/share/tree [get] func (c *EdbInfoShareController) Tree() { br := new(models.BaseResponse).Init() defer func() { c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } resp := response.EdbShareListResp{} // 我共享的 { sendList, err := data.GetAllShareEdbListByFromUserId(sysUser.AdminId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取我共享的指标列表信息失败,Err:" + err.Error() return } nodeList, err := data.GetEdbClassifyItemListByShareEdbInfoQueryList(sendList) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取我共享出去的指标列表分类结构信息失败,Err:" + err.Error() return } resp.Send = nodeList } // 我收到的 { sendList, err := data.GetAllShareEdbListByReceivedUserId(sysUser.AdminId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取我共享的指标列表信息失败,Err:" + err.Error() return } nodeList, err := data.GetEdbClassifyItemListByShareEdbInfoQueryList(sendList) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取我收到共享的指标列表分类结构信息失败,Err:" + err.Error() return } receivedMap := make(map[int]int) var receivedList data_manage.UserShareEdbClassifyItemList for _, v := range nodeList { index, ok := receivedMap[v.SysUserId] if !ok { index = len(receivedMap) item := &data_manage.UserShareEdbClassifyItem{ UserId: v.SysUserId, UserName: v.SysUserRealName, EdbInfoId: 0, ClassifyId: v.SysUserId, ClassifyName: v.SysUserRealName, ClassifyType: 0, ClassifyNameEn: "", ParentId: 0, RootId: 0, Level: 0, Sort: 0, UniqueCode: utils.MD5(fmt.Sprint(v.SysUserId, "_", v.SysUserRealName)), Source: 0, SourceName: "", SysUserId: v.SysUserId, SysUserRealName: v.SysUserRealName, StartDate: "", EdbCode: "", EdbType: 0, Children: []*data_manage.UserShareEdbClassifyItem{}, Button: data_manage.EdbClassifyItemsButton{}, IsJoinPermission: 0, HaveOperaAuth: true, ClassifyIdPath: "", } receivedList = append(receivedList, item) } receivedList[index].Children = append(receivedList[index].Children, v) receivedMap[v.SysUserId] = index } resp.Received = receivedList } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // List // @Title 指标列表接口 // @Description 指标列表接口 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param ClassifyId query int true "分类id" // @Param FilterSource query int false "共享指标的列表数据来源,0:我共享的;1:别人共享给我的" // @Success 200 {object} response.EdbInfoChartListResp // @router /edb_info/share/list [get] func (c *EdbInfoShareController) List() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 分页 pageSize, _ := c.GetInt("PageSize") currentIndex, _ := c.GetInt("CurrentIndex") filterSource, _ := c.GetInt("FilterSource") 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 condition string var pars []interface{} condition += ` AND edb_info_type = ? ` pars = append(pars, 0) // 指标类型 edbType, _ := c.GetInt("EdbType", 0) if edbType > 0 { condition += ` AND edb_type = ? ` pars = append(pars, edbType) } // 用户筛选 userId, _ := c.GetInt("UserId") if userId > 0 { condition += ` AND b.sys_user_id = ? ` pars = append(pars, userId) } // 分类筛选 classifyId, _ := c.GetInt("ClassifyId") if classifyId > 0 { childClassify, e, _ := data.GetChildClassifyByClassifyId(classifyId) if e != nil && !utils.IsErrNoRow(e) { br.Msg = "获取失败" br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error() return } var classifyIds []int for _, v := range childClassify { classifyIds = append(classifyIds, v.ClassifyId) } condition += fmt.Sprintf(` AND b.classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds))) pars = append(pars, classifyIds) } switch filterSource { case 1: // 别人共享给我的 condition += ` AND a.sys_user_id = ? AND b.sys_user_id != ? ` pars = append(pars, sysUser.AdminId, sysUser.AdminId) default: // 我共享的 condition += ` AND b.sys_user_id = ? ` pars = append(pars, sysUser.AdminId) } obj := data_manage.EdbInfoShare{} dataCount, respList, err := obj.GetShareEdbInfoListPageList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取分享指标列表失败, %v", err) return } editShareEdbInfoIdMap, err := data.GetAllEditSharedEdbInfoIdMapByReceivedUserId(c.SysUser.AdminId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取所有有编辑权限的分享指标失败,Err:" + err.Error() return } for _, edbInfo := range respList { edbInfo.HaveOperaAuth = true edbInfo.Button = data.GetEdbOpButton(c.SysUser, edbInfo.SysUserId, edbInfo.EdbInfoId, edbInfo.EdbType, edbInfo.EdbInfoType, edbInfo.HaveOperaAuth, editShareEdbInfoIdMap) } page = paging.GetPaging(currentIndex, pageSize, int(dataCount)) resp := response.EdbInfoChartListResp{ Paging: page, List: respList, } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // ListByEs // @Title 指标筛选接口 // @Description 指标筛选接口 // @Success 200 {object} data_manage.EdbInfoList // @Param request body request.SearchEdbInfoShareReq true "type json string" // @Success 200 {object} data_manage.EdbInfoFilterDataResp // @router /edb_info/share/list/es [post] func (c *EdbInfoShareController) ListByEs() { br := new(models.BaseResponse).Init() defer func() { c.Data["json"] = br c.ServeJSON() }() var req request.SearchEdbInfoShareReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } pageSize := req.PageSize currentIndex := req.CurrentIndex var total int64 var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) keyword := req.Keyword keyword = strings.TrimSpace(keyword) //移除字符串首尾空格 //指标来源 sourceList := req.SourceList edbInfoType := 0 // 普通指标 edbTypeList := []int{utils.EdbTypeCalculate} // 普通指标中的计算指标 // 指标分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未全部 edbShareList := req.EdbShareList if len(edbShareList) <= 0 { edbShareList = []int{} } edbShare := 0 // 0:全部,1:未共享,2:已共享 lenEdbShareList := len(edbShareList) if len(edbShareList) > 0 { if lenEdbShareList > 1 { edbShare = 0 } else { edbShare = edbShareList[0] } } // 直指标所属分类id edbClassifyIdList := req.ClassifyIdList edbAuth := 1 // 选择范围是:只有我的指标 var edbInfoList []*data_manage.EdbInfoList sortMap := make(map[string]string) // 如果没有搜索关键词,则默认根据指标编码倒序排序 if keyword == `` { sortMap["EdbInfoId"] = `desc` } total, edbInfoList, err = elastic.SearchEdbInfoDataByShared(keyword, startSize, pageSize, edbShare, sourceList, edbClassifyIdList, edbTypeList, edbInfoType, edbAuth, c.SysUser.AdminId, sortMap) if err != nil { edbInfoList = make([]*data_manage.EdbInfoList, 0) } page := paging.GetPaging(currentIndex, pageSize, int(total)) edbInfoListLen := len(edbInfoList) // 因为是ES查找的,所以需要重新查一下指标的信息,主要是为了把是否授权字段找出来 if len(edbInfoList) > 0 { sysUserIdList := make([]int, 0) edbInfoIdList := make([]int, 0) for _, v := range edbInfoList { v.ConvertToResp() v.EdbNameAlias = v.EdbName v.HaveOperaAuth = true edbInfoIdList = append(edbInfoIdList, v.EdbInfoId) if v.SharedUserIdList != nil && len(v.SharedUserIdList) > 0 { sysUserIdList = append(sysUserIdList, v.SharedUserIdList...) } } // 获取指标数据 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 } // 获取用户数据 sysUserMap := make(map[int]string) if len(sysUserIdList) > 0 { sysUserList, err := system.GetAdminListByIdList(sysUserIdList) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取分享用户列表失败,Err:" + err.Error() return } for _, v := range sysUserList { sysUserMap[v.AdminId] = v.RealName } } for _, v := range edbInfoList { tmpEdbInfo, ok := edbInfoMap[v.EdbInfoId] if !ok { continue } v.IsJoinPermission = tmpEdbInfo.IsJoinPermission // 分享人 shareUserName := `` shareUserNameList := make([]string, 0) shareUserNameMap := make(map[int]bool) for _, userId := range v.SharedUserIdList { userName, ok := sysUserMap[userId] if !ok { continue } if _, ok = shareUserNameMap[userId]; ok { continue } shareUserNameMap[userId] = true shareUserNameList = append(shareUserNameList, userName) } if len(shareUserNameList) > 0 { shareUserName = strings.Join(shareUserNameList, `,`) } v.SharedUserName = shareUserName } } for i := 0; i < edbInfoListLen; i++ { for j := 0; j < edbInfoListLen; j++ { if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) && (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) && !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) { edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")" } } } resp := data_manage.EdbInfoFilterDataResp{ Paging: page, List: edbInfoList, } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // BatchSave // @Title 批量指标设置共享 // @Description 批量指标设置共享 // @Param request body request.SetEdbSharePermissionReq true "type json string" // @Success 200 {object} data_manage.EdbInfo // @router /edb_info/share/batch_save [post] func (c *EdbInfoShareController) BatchSave() { br := new(models.BaseResponse).Init() defer func() { c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var req request.SetEdbSharePermissionReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } // 选中的指标id列表 selectEdbInfoIdList := make([]int, 0) // 选择所有指标,所以需要通过es获取数据 if req.IsSelectAll { allEsEdbInfoIdList, err := getAllEdbInfoIdListByShared(req, c.SysUser.AdminId) if err != nil { br.Msg = "设置失败!" br.ErrMsg = "设置失败,Err:" + err.Error() return } if len(req.NoEdbIdList) <= 0 { selectEdbInfoIdList = allEsEdbInfoIdList } else { noEdbIdMap := make(map[int]bool) for _, v := range req.NoEdbIdList { noEdbIdMap[v] = true } for _, v := range allEsEdbInfoIdList { if _, ok := noEdbIdMap[v]; !ok { // 如果不在未选中的指标id列表中,那么就加入到选中的指标id列表 selectEdbInfoIdList = append(selectEdbInfoIdList, v) } } } } else { selectEdbInfoIdList = req.EdbIdList } num := len(selectEdbInfoIdList) if num > 30 { br.Msg = `指标数量不能超过30个` br.IsSendEmail = false return } // 设置 if num > 0 { obj := data_manage.EdbInfoShare{} err = obj.SaveEdbInfoShare(selectEdbInfoIdList, req.UserIdList, req.ShareType) if err != nil { br.Msg = `保存失败` br.ErrMsg = `保存失败:` + err.Error() return } // 更新es for _, edbInfoId := range selectEdbInfoIdList { data.AddOrEditEdbInfoToEs(edbInfoId) } } br.Ret = 200 br.Success = true br.Msg = "保存成功" } // getAllEdbInfoIdListByShared // @Description: 获取所有的指标id列表 // @author: Roc // @datetime 2024-12-04 15:43:14 // @param req request.SetEdbChartPermissionReq // @param userId int // @return edbInfoIdList []int // @return err error func getAllEdbInfoIdListByShared(req request.SetEdbSharePermissionReq, userId int) (edbInfoIdList []int, err error) { keyword := req.Keyword keyword = strings.TrimSpace(keyword) //移除字符串首尾空格 //指标来源 sourceList := req.SourceList edbInfoType := 0 // 普通指标 edbTypeList := []int{utils.EdbTypeCalculate} // 普通指标中的计算指标 // 指标分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未共享 edbShareList := req.EdbShareList if len(edbShareList) <= 0 { edbShareList = []int{1} } edbShare := 1 // 0:全部,1:未共享,2:已共享 lenEdbShareList := len(edbShareList) if len(edbShareList) > 0 { if lenEdbShareList > 1 { edbShare = 0 } else { edbShare = edbShareList[0] } } // 指标分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未共享 edbClassifyIdList := req.ClassifyIdList edbAuth := 1 // 选择范围是:只有我的指标 sortMap := make(map[string]string) // 如果没有搜索关键词,则默认根据指标编码倒序排序 if keyword == `` { sortMap["EdbInfoId"] = `desc` } _, edbInfoList, err := getAllEdbInfoDataByShared(keyword, 1, edbShare, sourceList, edbClassifyIdList, edbTypeList, edbInfoType, edbAuth, userId, sortMap) if err != nil { return } // 返回指标id列表 for _, v := range edbInfoList { edbInfoIdList = append(edbInfoIdList, v.EdbInfoId) } return } // getAllEdbInfoDataByShared // @Description: 获取所有的指标列表(设置共享的时候) // @author: Roc // @datetime 2024-12-04 15:27:53 // @param keyword string // @param currPage int // @param edbShare int // @param sourceList []int // @param classifyIdList []int // @param edbTypeList []int // @param edbInfoType int // @param edbAuth int // @param sysUserId int // @param sortMap map[string]string // @return total int64 // @return list []*data_manage.EdbInfoList // @return err error func getAllEdbInfoDataByShared(keyword string, currPage, edbShare int, sourceList, classifyIdList, edbTypeList []int, edbInfoType, edbAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.EdbInfoList, err error) { // 每页获取数据的数量 pageSize := 5000 var startSize int if currPage <= 0 { currPage = 1 } startSize = paging.StartIndex(currPage, pageSize) total, list, err = elastic.SearchEdbInfoDataByShared(keyword, startSize, pageSize, edbShare, sourceList, classifyIdList, edbTypeList, edbInfoType, edbAuth, sysUserId, sortMap) if err != nil { return } page := paging.GetPaging(currPage, pageSize, int(total)) if !page.IsEnd { _, nextList, tmpErr := getAllEdbInfoDataByShared(keyword, page.NextIndex, edbShare, sourceList, classifyIdList, edbTypeList, edbInfoType, edbAuth, sysUserId, sortMap) if tmpErr != nil { err = tmpErr return } list = append(list, nextList...) } return }