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" "sort" "strconv" "strings" ) // ChartInfoShareController 数据管理 type ChartInfoShareController struct { controllers.BaseAuthController } // UserList // @Title 获取图表设置共享的详情 // @Description 获取图表详情接口 // @Param ChartInfoId query int true "图表id" // @Success 200 {object} response.ChartInfoShareUserResp // @router /chart_info/share/user_list [get] func (c *ChartInfoShareController) 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 } chartInfoId, _ := c.GetInt("ChartInfoId") if chartInfoId <= 0 { br.Msg = "参数错误" br.ErrMsg = "参数错误" return } _, err := data_manage.GetChartInfoById(chartInfoId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取图表信息失败,Err:" + err.Error() return } //// TODO 权限校验 //{ // classifyMap := make(map[int]*data_manage.ChartClassifyIdItems) // for _, v := range classifyList { // classifyMap[v.ClassifyId] = v // } // if currClassify, ok := classifyMap[chartInfo.ClassifyId]; ok { // haveOperaAuth, err := data_manage_permission.CheckChartPermission(chartInfo.IsJoinPermission, currClassify.IsJoinPermission, c.SysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ClassifyId) // if err != nil { // br.Msg = err.Error() // br.ErrMsg = "校验图表权限失败,err:" + err.Error() // return // } // resp.HaveOperaAuth = haveOperaAuth // } //} resp := response.ChartInfoShareUserResp{} obj := data_manage.ChartInfoShare{} list, err := obj.GetListByChartInfoId(chartInfoId) if err != nil { br.Msg = `获取失败` br.ErrMsg = `获取失败:` + err.Error() return } resp.List = list if len(list) > 0 { resp.ShareType = list[0].ShareType } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // Save // @Title 单个图表设置共享 // @Description 单个图表设置共享 // @Param request body request.SetChartInfoShareReq true "type json string" // @Success 200 {object} data_manage.ChartInfo // @router /chart_info/share/save [post] func (c *ChartInfoShareController) 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.SetChartInfoShareReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ChartInfoId <= 0 { br.Msg = `请选择图表` br.IsSendEmail = false return } chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId) if err != nil { br.Msg = "查找图表失败" br.ErrMsg = "查找图表失败,Err:" + err.Error() return } if chartItem.ChartInfoId <= 0 { br.Msg = "图表不存在" br.ErrMsg = "图表不存在,ChartInfoId:" + strconv.Itoa(req.ChartInfoId) br.IsSendEmail = false return } if chartItem.SysUserId != c.SysUser.AdminId { br.Msg = "您没有权限设置共享" br.ErrMsg = "您没有权限设置共享,EdbInfoId:" + strconv.Itoa(req.ChartInfoId) br.IsSendEmail = false return } obj := data_manage.ChartInfoShare{} err = obj.SaveChartInfoShare([]int{req.ChartInfoId}, req.UserIdList, req.ShareType) if err != nil { br.Msg = `保存失败` br.ErrMsg = `保存失败:` + err.Error() return } // 更新es data.AddOrEditChartInfoToEs(req.ChartInfoId) br.Ret = 200 br.Success = true br.Msg = "保存成功" } // Tree // @Title 获取共享图表的分类/图表树 // @Description 获取共享图表的分类/图表树 // @Success 200 {object} response.ChartShareListResp // @router /chart_info/share/tree [get] func (c *ChartInfoShareController) 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.ChartShareListResp{} // 我共享的 { sendList, err := data.GetAllShareChartListByFromUserId(sysUser.AdminId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取我共享的图表列表信息失败,Err:" + err.Error() return } nodeList, err := data.GetChartClassifyItemListByShareChartInfoQueryList(sendList) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取我共享出去的图表列表分类结构信息失败,Err:" + err.Error() return } var sortList data_manage.ChartClassifyItemList sortList = nodeList sort.Sort(sortList) resp.Send = sortList } // TODO 不确定加了用户这个顶级,会不会对前端造成一定影响 // 我收到的 { sendList, err := data.GetAllShareChartListByReceivedUserId(sysUser.AdminId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取我共享的图表列表信息失败,Err:" + err.Error() return } nodeList, err := data.GetChartClassifyItemListByShareChartInfoQueryList(sendList) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取我收到共享的图表列表分类结构信息失败,Err:" + err.Error() return } receivedMap := make(map[int]int) var receivedList data_manage.ChartClassifyItemList for _, v := range nodeList { index, ok := receivedMap[v.SysUserId] if !ok { index = len(receivedMap) item := &data_manage.ChartClassifyItems{ ChartClassifyId: v.SysUserId, ChartInfoId: 0, ChartClassifyName: v.SysUserRealName, ChartClassifyNameEn: v.SysUserRealName, ParentId: 0, Level: 0, Sort: 0, UniqueCode: utils.MD5(fmt.Sprint(v.SysUserId, "_", v.SysUserRealName)), Source: 0, SourceName: "", SysUserId: v.SysUserId, SysUserRealName: v.SysUserRealName, DateType: 0, StartDate: "", EndDate: "", ChartType: 0, Calendar: "", SeasonStartDate: "", SeasonEndDate: "", Children: []*data_manage.ChartClassifyItems{}, Button: data_manage.ChartClassifyItemsButton{}, IsJoinPermission: 0, HaveOperaAuth: false, ChartClassifyIdPath: "", } receivedList = append(receivedList, item) } receivedList[index].Children = append(receivedList[index].Children, v) } sort.Sort(receivedList) 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.ChartInfoChartListResp // @router /chart_info/share/list [get] func (c *ChartInfoShareController) 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 source = ? ` pars = append(pars, utils.CHART_TYPE_CURVE) // 分类筛选 classifyId, _ := c.GetInt("ClassifyId") if classifyId > 0 { childClassify, e, _ := data.GetChildClassifyByClassifyId(classifyId) if e != nil && !utils.IsErrNoRow(e) { br.Msg = "获取失败" br.ErrMsg = "获取分类信息失败, GetChartClassify,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.ChartInfoShare{} dataCount, respList, err := obj.GetShareChartInfoListPageList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取分享图表列表失败, %v", err) return } page = paging.GetPaging(currentIndex, pageSize, int(dataCount)) resp := response.ChartInfoChartListResp{ Paging: page, List: respList, } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // ListByEs // @Title 图表筛选接口 // @Description 图表筛选接口 // @Success 200 {object} data_manage.ChartInfoList // @Param request body request.SearchChartInfoShareReq true "type json string" // @Success 200 {object} data_manage.ChartInfoFilterDataResp // @router /chart_info/share/list/es [post] func (c *ChartInfoShareController) ListByEs() { br := new(models.BaseResponse).Init() defer func() { c.Data["json"] = br c.ServeJSON() }() var req request.SearchChartInfoShareReq 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 := []int{utils.CHART_TYPE_CURVE} // 图表分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未全部 chartShareList := req.ChartShareList if len(chartShareList) <= 0 { chartShareList = []int{} } chartShare := 0 // 0:全部,1:未共享,2:已共享 lenChartShareList := len(chartShareList) if len(chartShareList) > 0 { if lenChartShareList > 1 { chartShare = 0 } else { chartShare = chartShareList[0] } } // 图表类型 chartTypeList := req.ChartTypeList // 直图表所属分类id chartClassifyIdList := req.ClassifyIdList chartAuth := 1 // 选择范围是:只有我的图表 var chartInfoList []*data_manage.ChartInfoView sortMap := make(map[string]string) // 如果没有搜索关键词,则默认根据图表编码倒序排序 if keyword == `` { sortMap["ChartInfoId"] = `desc` } total, chartInfoList, err = elastic.SearchChartInfoDataByShared(keyword, startSize, pageSize, chartShare, sourceList, chartTypeList, chartClassifyIdList, chartAuth, c.SysUser.AdminId, sortMap) if err != nil { chartInfoList = make([]*data_manage.ChartInfoView, 0) } page := paging.GetPaging(currentIndex, pageSize, int(total)) // 因为是ES查找的,所以需要重新查一下图表的信息,主要是为了把是否授权字段找出来 if len(chartInfoList) > 0 { sysUserIdList := make([]int, 0) chartInfoIdList := make([]int, 0) for _, v := range chartInfoList { v.ConvertToResp() v.HaveOperaAuth = true chartInfoIdList = append(chartInfoIdList, v.ChartInfoId) if v.SharedUserIdList != nil && len(v.SharedUserIdList) > 0 { sysUserIdList = append(sysUserIdList, v.SharedUserIdList...) } } // 获取图表数据 tmpChartList, err := data_manage.GetChartInfoByIdList(chartInfoIdList) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取所有有权限的图表失败,Err:" + err.Error() return } chartInfoMap := make(map[int]*data_manage.ChartInfo) for _, v := range tmpChartList { chartInfoMap[v.ChartInfoId] = 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 chartInfoList { tmpChartInfo, ok := chartInfoMap[v.ChartInfoId] if !ok { continue } v.IsJoinPermission = tmpChartInfo.IsJoinPermission // 分享人 shareUserName := `` shareUserNameList := make([]string, 0) for _, userId := range v.SharedUserIdList { userName, ok := sysUserMap[userId] if !ok { continue } shareUserNameList = append(shareUserNameList, userName) } if len(shareUserNameList) > 0 { shareUserName = strings.Join(shareUserNameList, `,`) } v.SharedUserName = shareUserName } } resp := data_manage.ChartInfoFilterDataResp{ Paging: page, List: chartInfoList, } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // BatchSave // @Title 批量图表设置共享 // @Description 批量图表设置共享 // @Param request body request.SetChartSharePermissionReq true "type json string" // @Success 200 {object} data_manage.ChartInfo // @router /chart_info/share/batch_save [post] func (c *ChartInfoShareController) 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.SetChartSharePermissionReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } // 选中的图表id列表 selectChartInfoIdList := make([]int, 0) // 选择所有图表,所以需要通过es获取数据 if req.IsSelectAll { allEsChartInfoIdList, err := getAllChartInfoIdListByShared(req, c.SysUser.AdminId) if err != nil { br.Msg = "设置失败!" br.ErrMsg = "设置失败,Err:" + err.Error() return } if len(req.NoChartIdList) <= 0 { selectChartInfoIdList = allEsChartInfoIdList } else { noChartIdMap := make(map[int]bool) for _, v := range req.NoChartIdList { noChartIdMap[v] = true } for _, v := range allEsChartInfoIdList { if _, ok := noChartIdMap[v]; !ok { // 如果不在未选中的图表id列表中,那么就加入到选中的图表id列表 selectChartInfoIdList = append(selectChartInfoIdList, v) } } } } else { selectChartInfoIdList = req.ChartIdList } num := len(selectChartInfoIdList) if num > 30 { br.Msg = `图表数量不能超过30个` br.IsSendEmail = false return } // 设置 if num > 0 { obj := data_manage.ChartInfoShare{} err = obj.SaveChartInfoShare(selectChartInfoIdList, req.UserIdList, req.ShareType) if err != nil { br.Msg = `保存失败` br.ErrMsg = `保存失败:` + err.Error() return } // 更新es for _, chartInfoId := range selectChartInfoIdList { data.AddOrEditChartInfoToEs(chartInfoId) } } br.Ret = 200 br.Success = true br.Msg = "保存成功" } // getAllChartInfoIdListByShared // @Description: 获取所有的图表id列表 // @author: Roc // @datetime 2024-12-10 17:51:35 // @param req request.SetChartSharePermissionReq // @param userId int // @return chartInfoIdList []int // @return err error func getAllChartInfoIdListByShared(req request.SetChartSharePermissionReq, userId int) (chartInfoIdList []int, err error) { keyword := req.Keyword keyword = strings.TrimSpace(keyword) //移除字符串首尾空格 //图表来源 sourceList := []int{utils.CHART_TYPE_CURVE} // 图表分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未共享 chartShareList := req.ChartShareList if len(chartShareList) <= 0 { chartShareList = []int{1} } chartShare := 1 // 0:全部,1:未共享,2:已共享 lenChartShareList := len(chartShareList) if len(chartShareList) > 0 { if lenChartShareList > 1 { chartShare = 0 } else { chartShare = chartShareList[0] } } // 图表类型 chartTypeList := req.ChartTypeList // 图表分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未共享 chartClassifyIdList := req.ClassifyIdList chartAuth := 1 // 选择范围是:只有我的图表 sortMap := make(map[string]string) // 如果没有搜索关键词,则默认根据图表编码倒序排序 if keyword == `` { sortMap["ChartInfoId"] = `desc` } _, chartInfoList, err := getAllChartInfoDataByShared(keyword, 1, chartShare, sourceList, chartTypeList, chartClassifyIdList, chartAuth, userId, sortMap) if err != nil { return } // 返回图表id列表 for _, v := range chartInfoList { chartInfoIdList = append(chartInfoIdList, v.ChartInfoId) } return } // getAllChartInfoDataByShared // @Description: 获取所有的图表列表(设置共享的时候) // @author: Roc // @datetime 2024-12-10 17:51:18 // @param keyword string // @param currPage int // @param chartShare int // @param sourceList []int // @param classifyIdList []int // @param chartAuth int // @param sysUserId int // @param sortMap map[string]string // @return total int64 // @return list []*data_manage.ChartInfoView // @return err error func getAllChartInfoDataByShared(keyword string, currPage, chartShare int, sourceList, chartTypeList, classifyIdList []int, chartAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.ChartInfoView, err error) { // 每页获取数据的数量 pageSize := 5000 var startSize int if currPage <= 0 { currPage = 1 } startSize = paging.StartIndex(currPage, pageSize) total, list, err = elastic.SearchChartInfoDataByShared(keyword, startSize, pageSize, chartShare, sourceList, chartTypeList, classifyIdList, chartAuth, sysUserId, sortMap) if err != nil { return } page := paging.GetPaging(currPage, pageSize, int(total)) if !page.IsEnd { _, nextList, tmpErr := getAllChartInfoDataByShared(keyword, page.NextIndex, chartShare, sourceList, chartTypeList, classifyIdList, chartAuth, sysUserId, sortMap) if tmpErr != nil { err = tmpErr return } list = append(list, nextList...) } return }