123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
- 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.UserShareChartClassifyItemList
- 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.UserShareChartClassifyItemList
- for _, v := range nodeList {
- index, ok := receivedMap[v.SysUserId]
- if !ok {
- index = len(receivedMap)
- item := &data_manage.UserShareChartClassifyItem{
- UserId: v.SysUserId,
- ChartInfoId: 0,
- UserName: 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.UserShareChartClassifyItem{},
- Button: data_manage.ChartClassifyItemsButton{},
- IsJoinPermission: 0,
- HaveOperaAuth: true,
- ChartClassifyIdPath: "",
- }
- receivedList = append(receivedList, item)
- }
- receivedList[index].Children = append(receivedList[index].Children, v)
- receivedMap[v.SysUserId] = index
- }
- 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:别人共享给我的"
- // @Param ClassifyId query int false "分类id"
- // @Param UserId query int false "所属用户id"
- // @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)
- // 用户筛选
- 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 = "获取分类信息失败, 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
- }
|