edb_info_share.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/cache"
  5. "eta_gn/eta_api/controllers"
  6. "eta_gn/eta_api/models"
  7. "eta_gn/eta_api/models/data_manage"
  8. "eta_gn/eta_api/models/data_manage/request"
  9. "eta_gn/eta_api/models/data_manage/response"
  10. "eta_gn/eta_api/models/system"
  11. "eta_gn/eta_api/services/data"
  12. "eta_gn/eta_api/services/elastic"
  13. "eta_gn/eta_api/utils"
  14. "fmt"
  15. "github.com/rdlucklib/rdluck_tools/paging"
  16. "strconv"
  17. "strings"
  18. )
  19. // EdbInfoShareController 数据管理
  20. type EdbInfoShareController struct {
  21. controllers.BaseAuthController
  22. }
  23. // UserList
  24. // @Title 获取指标设置共享的详情
  25. // @Description 获取指标详情接口
  26. // @Param EdbInfoId query int true "指标id"
  27. // @Success 200 {object} response.EdbInfoShareUserResp
  28. // @router /edb_info/share/user_list [get]
  29. func (c *EdbInfoShareController) UserList() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. c.Data["json"] = br
  33. c.ServeJSON()
  34. }()
  35. sysUser := c.SysUser
  36. if sysUser == nil {
  37. br.Msg = "请登录"
  38. br.ErrMsg = "请登录,SysUser Is Empty"
  39. br.Ret = 408
  40. return
  41. }
  42. edbInfoId, _ := c.GetInt("EdbInfoId")
  43. if edbInfoId <= 0 {
  44. br.Msg = "参数错误"
  45. br.ErrMsg = "参数错误"
  46. return
  47. }
  48. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  49. if err != nil {
  50. br.Msg = "获取失败"
  51. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  52. return
  53. }
  54. if edbInfo.SysUserId != sysUser.AdminId {
  55. br.Msg = "您没有权限设置共享"
  56. br.ErrMsg = "您没有权限设置共享,EdbInfoId:" + strconv.Itoa(edbInfo.EdbInfoId)
  57. br.IsSendEmail = false
  58. return
  59. }
  60. resp := response.EdbInfoShareUserResp{}
  61. obj := data_manage.EdbInfoShare{}
  62. list, err := obj.GetListByEdbInfoId(edbInfoId)
  63. if err != nil {
  64. br.Msg = `获取失败`
  65. br.ErrMsg = `获取失败:` + err.Error()
  66. return
  67. }
  68. resp.List = list
  69. br.Ret = 200
  70. br.Success = true
  71. br.Msg = "获取成功"
  72. br.Data = resp
  73. }
  74. // Save
  75. // @Title 单个指标设置共享
  76. // @Description 单个指标设置共享
  77. // @Param request body request.SetEdbInfoShareReq true "type json string"
  78. // @Success 200 {object} data_manage.EdbInfo
  79. // @router /edb_info/share/save [post]
  80. func (c *EdbInfoShareController) Save() {
  81. br := new(models.BaseResponse).Init()
  82. defer func() {
  83. c.Data["json"] = br
  84. c.ServeJSON()
  85. }()
  86. sysUser := c.SysUser
  87. if sysUser == nil {
  88. br.Msg = "请登录"
  89. br.ErrMsg = "请登录,SysUser Is Empty"
  90. br.Ret = 408
  91. return
  92. }
  93. var req request.SetEdbInfoShareReq
  94. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  95. if err != nil {
  96. br.Msg = "参数解析异常!"
  97. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  98. return
  99. }
  100. if req.EdbInfoId <= 0 {
  101. br.Msg = `请选择指标`
  102. br.IsSendEmail = false
  103. return
  104. }
  105. edbItem, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  106. if err != nil {
  107. br.Msg = "查找指标失败"
  108. br.ErrMsg = "查找指标失败,Err:" + err.Error()
  109. return
  110. }
  111. if edbItem.EdbInfoId <= 0 {
  112. br.Msg = "指标不存在"
  113. br.ErrMsg = "指标不存在,ChartInfoId:" + strconv.Itoa(req.EdbInfoId)
  114. br.IsSendEmail = false
  115. return
  116. }
  117. if edbItem.SysUserId != c.SysUser.AdminId {
  118. br.Msg = "您没有权限设置共享"
  119. br.ErrMsg = "您没有权限设置共享,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  120. br.IsSendEmail = false
  121. return
  122. }
  123. obj := data_manage.EdbInfoShare{}
  124. // 如果指标未公开,则判断是否被其他用户使用
  125. if edbItem.PublicStatus != utils.DataPublicSuccess {
  126. checkUserIdList := []int{sysUser.AdminId}
  127. if len(req.UserIdList) > 0 {
  128. checkUserIdList = append(checkUserIdList, req.UserIdList...)
  129. }
  130. {
  131. userIdList, err := obj.GetAllUserIdListByEdbInfoIdAndNoShareType(req.EdbInfoId, req.ShareType)
  132. if err != nil {
  133. br.Msg = "设置失败!"
  134. br.ErrMsg = "设置失败,Err:" + err.Error()
  135. return
  136. }
  137. if len(userIdList) > 0 {
  138. checkUserIdList = append(checkUserIdList, userIdList...)
  139. }
  140. }
  141. // 校验是否被其他用户关联
  142. noReferenced, otherUserIdList, err, tips := data.CheckEdbReferencedByOthers(edbItem.EdbInfoType, []int{edbItem.EdbInfoId}, checkUserIdList)
  143. if err != nil {
  144. br.Msg = "保存失败"
  145. if tips != "" {
  146. br.Msg = tips
  147. }
  148. br.ErrMsg = "设置指标共享失败,查找指标的关联用户失败,Err:" + err.Error()
  149. return
  150. }
  151. if !noReferenced {
  152. userNameStr := ``
  153. if len(otherUserIdList) > 0 {
  154. userNameList := make([]string, 0)
  155. userList, err := system.GetAdminListByIdList(otherUserIdList)
  156. if err != nil {
  157. br.Msg = "保存失败"
  158. if tips != "" {
  159. br.Msg = tips
  160. }
  161. br.ErrMsg = "设置指标共享失败,查找用户信息失败,Err:" + err.Error()
  162. return
  163. }
  164. for _, userInfo := range userList {
  165. userNameList = append(userNameList, userInfo.RealName)
  166. }
  167. userNameStr = strings.Join(userNameList, "、")
  168. }
  169. msg := `指标被其他用户使用,无法设置共享`
  170. if userNameStr != "" {
  171. msg = fmt.Sprintf(`指标被用户%s使用,无法设置共享`, userNameStr)
  172. }
  173. if tips != "" {
  174. msg = fmt.Sprintf(`%s,用户为:%s,无法设置共享`, tips, userNameStr)
  175. }
  176. br.Msg = msg
  177. br.ErrMsg = "指标被其他用户共享,无法设置共享,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  178. br.IsSendEmail = false
  179. return
  180. }
  181. }
  182. // 修改
  183. err = obj.SaveEdbInfoShare([]int{req.EdbInfoId}, req.UserIdList, req.ShareType)
  184. if err != nil {
  185. br.Msg = `保存失败`
  186. br.ErrMsg = `保存失败:` + err.Error()
  187. return
  188. }
  189. // 更新es
  190. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  191. // 指标收藏操作处理
  192. go cache.AddDataCollectHandler(req.EdbInfoId, utils.DataCollectTypeEdb)
  193. br.Ret = 200
  194. br.Success = true
  195. br.Msg = "保存成功"
  196. }
  197. // Tree
  198. // @Title 获取共享指标的分类/指标树
  199. // @Description 获取共享指标的分类/指标树
  200. // @Success 200 {object} response.EdbShareListResp
  201. // @router /edb_info/share/tree [get]
  202. func (c *EdbInfoShareController) Tree() {
  203. br := new(models.BaseResponse).Init()
  204. defer func() {
  205. c.Data["json"] = br
  206. c.ServeJSON()
  207. }()
  208. sysUser := c.SysUser
  209. if sysUser == nil {
  210. br.Msg = "请登录"
  211. br.ErrMsg = "请登录,SysUser Is Empty"
  212. br.Ret = 408
  213. return
  214. }
  215. resp := response.EdbShareListResp{}
  216. // 我共享的
  217. {
  218. sendList, err := data.GetAllShareEdbListByFromUserId(sysUser.AdminId)
  219. if err != nil {
  220. br.Msg = "获取失败"
  221. br.ErrMsg = "获取我共享的指标列表信息失败,Err:" + err.Error()
  222. return
  223. }
  224. nodeList, err := data.GetEdbClassifyItemListByShareEdbInfoQueryList(sendList)
  225. if err != nil {
  226. br.Msg = "获取失败"
  227. br.ErrMsg = "获取我共享出去的指标列表分类结构信息失败,Err:" + err.Error()
  228. return
  229. }
  230. resp.Send = nodeList
  231. }
  232. // 我收到的
  233. {
  234. sendList, err := data.GetAllShareEdbListByReceivedUserId(sysUser.AdminId)
  235. if err != nil {
  236. br.Msg = "获取失败"
  237. br.ErrMsg = "获取我共享的指标列表信息失败,Err:" + err.Error()
  238. return
  239. }
  240. nodeList, err := data.GetEdbClassifyItemListByShareEdbInfoQueryList(sendList)
  241. if err != nil {
  242. br.Msg = "获取失败"
  243. br.ErrMsg = "获取我收到共享的指标列表分类结构信息失败,Err:" + err.Error()
  244. return
  245. }
  246. receivedMap := make(map[int]int)
  247. var receivedList data_manage.UserShareEdbClassifyItemList
  248. for _, v := range nodeList {
  249. index, ok := receivedMap[v.SysUserId]
  250. if !ok {
  251. index = len(receivedMap)
  252. item := &data_manage.UserShareEdbClassifyItem{
  253. UserId: v.SysUserId,
  254. UserName: v.SysUserRealName,
  255. EdbInfoId: 0,
  256. ClassifyId: v.SysUserId,
  257. ClassifyName: v.SysUserRealName,
  258. ClassifyType: 0,
  259. ClassifyNameEn: "",
  260. ParentId: 0,
  261. RootId: 0,
  262. Level: 0,
  263. Sort: 0,
  264. UniqueCode: utils.MD5(fmt.Sprint(v.SysUserId, "_", v.SysUserRealName)),
  265. Source: 0,
  266. SourceName: "",
  267. SysUserId: v.SysUserId,
  268. SysUserRealName: v.SysUserRealName,
  269. StartDate: "",
  270. EdbCode: "",
  271. EdbType: 0,
  272. Children: []*data_manage.UserShareEdbClassifyItem{},
  273. Button: data_manage.EdbClassifyItemsButton{},
  274. IsJoinPermission: 0,
  275. HaveOperaAuth: true,
  276. ClassifyIdPath: "",
  277. }
  278. receivedList = append(receivedList, item)
  279. }
  280. receivedList[index].Children = append(receivedList[index].Children, v)
  281. receivedMap[v.SysUserId] = index
  282. }
  283. resp.Received = receivedList
  284. }
  285. br.Ret = 200
  286. br.Success = true
  287. br.Msg = "获取成功"
  288. br.Data = resp
  289. }
  290. // List
  291. // @Title 指标列表接口
  292. // @Description 指标列表接口
  293. // @Param PageSize query int true "每页数据条数"
  294. // @Param CurrentIndex query int true "当前页页码,从1开始"
  295. // @Param ClassifyId query int true "分类id"
  296. // @Param FilterSource query int false "共享指标的列表数据来源,0:我共享的;1:别人共享给我的"
  297. // @Success 200 {object} response.EdbInfoChartListResp
  298. // @router /edb_info/share/list [get]
  299. func (c *EdbInfoShareController) List() {
  300. br := new(models.BaseResponse).Init()
  301. defer func() {
  302. if br.ErrMsg == "" {
  303. br.IsSendEmail = false
  304. }
  305. c.Data["json"] = br
  306. c.ServeJSON()
  307. }()
  308. sysUser := c.SysUser
  309. if sysUser == nil {
  310. br.Msg = "请登录"
  311. br.ErrMsg = "请登录,SysUser Is Empty"
  312. br.Ret = 408
  313. return
  314. }
  315. // 分页
  316. pageSize, _ := c.GetInt("PageSize")
  317. currentIndex, _ := c.GetInt("CurrentIndex")
  318. filterSource, _ := c.GetInt("FilterSource")
  319. var total int
  320. page := paging.GetPaging(currentIndex, pageSize, total)
  321. var startSize int
  322. if pageSize <= 0 {
  323. pageSize = utils.PageSize20
  324. }
  325. if currentIndex <= 0 {
  326. currentIndex = 1
  327. }
  328. startSize = paging.StartIndex(currentIndex, pageSize)
  329. // 基础指标
  330. var condition string
  331. var pars []interface{}
  332. condition += ` AND edb_info_type = ? `
  333. pars = append(pars, 0)
  334. // 指标类型
  335. edbType, _ := c.GetInt("EdbType", 0)
  336. if edbType > 0 {
  337. condition += ` AND edb_type = ? `
  338. pars = append(pars, edbType)
  339. }
  340. // 用户筛选
  341. userId, _ := c.GetInt("UserId")
  342. if userId > 0 {
  343. condition += ` AND b.sys_user_id = ? `
  344. pars = append(pars, userId)
  345. }
  346. // 分类筛选
  347. classifyId, _ := c.GetInt("ClassifyId")
  348. if classifyId > 0 {
  349. childClassify, e, _ := data.GetChildClassifyByClassifyId(classifyId)
  350. if e != nil && !utils.IsErrNoRow(e) {
  351. br.Msg = "获取失败"
  352. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  353. return
  354. }
  355. var classifyIds []int
  356. for _, v := range childClassify {
  357. classifyIds = append(classifyIds, v.ClassifyId)
  358. }
  359. condition += fmt.Sprintf(` AND b.classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  360. pars = append(pars, classifyIds)
  361. }
  362. switch filterSource {
  363. case 1: // 别人共享给我的
  364. condition += ` AND a.sys_user_id = ? AND b.sys_user_id != ? `
  365. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  366. default: // 我共享的
  367. condition += ` AND b.sys_user_id = ? `
  368. pars = append(pars, sysUser.AdminId)
  369. }
  370. obj := data_manage.EdbInfoShare{}
  371. dataCount, respList, err := obj.GetShareEdbInfoListPageList(condition, pars, startSize, pageSize)
  372. if err != nil {
  373. br.Msg = "获取失败"
  374. br.ErrMsg = fmt.Sprintf("获取分享指标列表失败, %v", err)
  375. return
  376. }
  377. editShareEdbInfoIdMap, err := data.GetAllEditSharedEdbInfoIdMapByReceivedUserId(c.SysUser.AdminId)
  378. if err != nil {
  379. br.Msg = "获取失败"
  380. br.ErrMsg = "获取所有有编辑权限的分享指标失败,Err:" + err.Error()
  381. return
  382. }
  383. for _, edbInfo := range respList {
  384. edbInfo.HaveOperaAuth = true
  385. edbInfo.Button = data.GetEdbOpButton(c.SysUser, edbInfo.SysUserId, edbInfo.EdbInfoId, edbInfo.EdbType, edbInfo.EdbInfoType, edbInfo.HaveOperaAuth, editShareEdbInfoIdMap)
  386. }
  387. page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
  388. resp := response.EdbInfoChartListResp{
  389. Paging: page,
  390. List: respList,
  391. }
  392. br.Ret = 200
  393. br.Success = true
  394. br.Msg = "获取成功"
  395. br.Data = resp
  396. }
  397. // ListByEs
  398. // @Title 指标筛选接口
  399. // @Description 指标筛选接口
  400. // @Success 200 {object} data_manage.EdbInfoList
  401. // @Param request body request.SearchEdbInfoShareReq true "type json string"
  402. // @Success 200 {object} data_manage.EdbInfoFilterDataResp
  403. // @router /edb_info/share/list/es [post]
  404. func (c *EdbInfoShareController) ListByEs() {
  405. br := new(models.BaseResponse).Init()
  406. defer func() {
  407. c.Data["json"] = br
  408. c.ServeJSON()
  409. }()
  410. var req request.SearchEdbInfoShareReq
  411. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  412. if err != nil {
  413. br.Msg = "参数解析异常!"
  414. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  415. return
  416. }
  417. pageSize := req.PageSize
  418. currentIndex := req.CurrentIndex
  419. var total int64
  420. var startSize int
  421. if pageSize <= 0 {
  422. pageSize = utils.PageSize20
  423. }
  424. if currentIndex <= 0 {
  425. currentIndex = 1
  426. }
  427. startSize = paging.StartIndex(currentIndex, pageSize)
  428. keyword := req.Keyword
  429. keyword = strings.TrimSpace(keyword) //移除字符串首尾空格
  430. //指标来源
  431. sourceList := req.SourceList
  432. edbInfoType := 0 // 普通指标
  433. edbTypeList := []int{utils.EdbTypeCalculate} // 普通指标中的计算指标
  434. // 指标分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未全部
  435. edbShareList := req.EdbShareList
  436. if len(edbShareList) <= 0 {
  437. edbShareList = []int{}
  438. }
  439. edbShare := 0 // 0:全部,1:未共享,2:已共享
  440. lenEdbShareList := len(edbShareList)
  441. if len(edbShareList) > 0 {
  442. if lenEdbShareList > 1 {
  443. edbShare = 0
  444. } else {
  445. edbShare = edbShareList[0]
  446. }
  447. }
  448. // 直指标所属分类id
  449. edbClassifyIdList := req.ClassifyIdList
  450. edbAuth := 1 // 选择范围是:只有我的指标
  451. var edbInfoList []*data_manage.EdbInfoList
  452. sortMap := make(map[string]string)
  453. // 如果没有搜索关键词,则默认根据指标编码倒序排序
  454. if keyword == `` {
  455. sortMap["EdbInfoId"] = `desc`
  456. }
  457. total, edbInfoList, err = elastic.SearchEdbInfoDataByShared(keyword, startSize, pageSize, edbShare, sourceList, edbClassifyIdList, edbTypeList, edbInfoType, edbAuth, c.SysUser.AdminId, sortMap)
  458. if err != nil {
  459. edbInfoList = make([]*data_manage.EdbInfoList, 0)
  460. }
  461. page := paging.GetPaging(currentIndex, pageSize, int(total))
  462. edbInfoListLen := len(edbInfoList)
  463. // 因为是ES查找的,所以需要重新查一下指标的信息,主要是为了把是否授权字段找出来
  464. if len(edbInfoList) > 0 {
  465. sysUserIdList := make([]int, 0)
  466. edbInfoIdList := make([]int, 0)
  467. for _, v := range edbInfoList {
  468. v.ConvertToResp()
  469. v.EdbNameAlias = v.EdbName
  470. v.HaveOperaAuth = true
  471. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  472. if v.SharedUserIdList != nil && len(v.SharedUserIdList) > 0 {
  473. sysUserIdList = append(sysUserIdList, v.SharedUserIdList...)
  474. }
  475. }
  476. // 获取指标数据
  477. tmpEdbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  478. if err != nil {
  479. br.Msg = "获取失败"
  480. br.ErrMsg = "获取所有有权限的指标失败,Err:" + err.Error()
  481. return
  482. }
  483. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  484. for _, v := range tmpEdbList {
  485. edbInfoMap[v.EdbInfoId] = v
  486. }
  487. // 获取用户数据
  488. sysUserMap := make(map[int]string)
  489. if len(sysUserIdList) > 0 {
  490. sysUserList, err := system.GetAdminListByIdList(sysUserIdList)
  491. if err != nil {
  492. br.Msg = "获取失败"
  493. br.ErrMsg = "获取分享用户列表失败,Err:" + err.Error()
  494. return
  495. }
  496. for _, v := range sysUserList {
  497. sysUserMap[v.AdminId] = v.RealName
  498. }
  499. }
  500. for _, v := range edbInfoList {
  501. tmpEdbInfo, ok := edbInfoMap[v.EdbInfoId]
  502. if !ok {
  503. continue
  504. }
  505. v.IsJoinPermission = tmpEdbInfo.IsJoinPermission
  506. // 分享人
  507. shareUserName := ``
  508. shareUserNameList := make([]string, 0)
  509. shareUserNameMap := make(map[int]bool)
  510. for _, userId := range v.SharedUserIdList {
  511. userName, ok := sysUserMap[userId]
  512. if !ok {
  513. continue
  514. }
  515. if _, ok = shareUserNameMap[userId]; ok {
  516. continue
  517. }
  518. shareUserNameMap[userId] = true
  519. shareUserNameList = append(shareUserNameList, userName)
  520. }
  521. if len(shareUserNameList) > 0 {
  522. shareUserName = strings.Join(shareUserNameList, `,`)
  523. }
  524. v.SharedUserName = shareUserName
  525. }
  526. }
  527. for i := 0; i < edbInfoListLen; i++ {
  528. for j := 0; j < edbInfoListLen; j++ {
  529. if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
  530. (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
  531. !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
  532. edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
  533. }
  534. }
  535. }
  536. resp := data_manage.EdbInfoFilterDataResp{
  537. Paging: page,
  538. List: edbInfoList,
  539. }
  540. br.Ret = 200
  541. br.Success = true
  542. br.Msg = "获取成功"
  543. br.Data = resp
  544. }
  545. // BatchSave
  546. // @Title 批量指标设置共享
  547. // @Description 批量指标设置共享
  548. // @Param request body request.SetEdbSharePermissionReq true "type json string"
  549. // @Success 200 {object} data_manage.EdbInfo
  550. // @router /edb_info/share/batch_save [post]
  551. func (c *EdbInfoShareController) BatchSave() {
  552. br := new(models.BaseResponse).Init()
  553. defer func() {
  554. c.Data["json"] = br
  555. c.ServeJSON()
  556. }()
  557. sysUser := c.SysUser
  558. if sysUser == nil {
  559. br.Msg = "请登录"
  560. br.ErrMsg = "请登录,SysUser Is Empty"
  561. br.Ret = 408
  562. return
  563. }
  564. var req request.SetEdbSharePermissionReq
  565. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  566. if err != nil {
  567. br.Msg = "参数解析异常!"
  568. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  569. return
  570. }
  571. // 选中的指标id列表
  572. selectEdbInfoIdList := make([]int, 0)
  573. // 选择所有指标,所以需要通过es获取数据
  574. if req.IsSelectAll {
  575. allEsEdbInfoIdList, err := getAllEdbInfoIdListByShared(req, c.SysUser.AdminId)
  576. if err != nil {
  577. br.Msg = "设置失败!"
  578. br.ErrMsg = "设置失败,Err:" + err.Error()
  579. return
  580. }
  581. if len(req.NoEdbIdList) <= 0 {
  582. selectEdbInfoIdList = allEsEdbInfoIdList
  583. } else {
  584. noEdbIdMap := make(map[int]bool)
  585. for _, v := range req.NoEdbIdList {
  586. noEdbIdMap[v] = true
  587. }
  588. for _, v := range allEsEdbInfoIdList {
  589. if _, ok := noEdbIdMap[v]; !ok {
  590. // 如果不在未选中的指标id列表中,那么就加入到选中的指标id列表
  591. selectEdbInfoIdList = append(selectEdbInfoIdList, v)
  592. }
  593. }
  594. }
  595. } else {
  596. selectEdbInfoIdList = req.EdbIdList
  597. }
  598. num := len(selectEdbInfoIdList)
  599. if num <= 0 {
  600. br.Msg = `请选择指标`
  601. br.IsSendEmail = false
  602. return
  603. }
  604. if num > 30 {
  605. br.Msg = `指标数量不能超过30个`
  606. br.IsSendEmail = false
  607. return
  608. }
  609. edbInfoList, err := data_manage.GetEdbInfoListByEdbInfoId(selectEdbInfoIdList)
  610. if err != nil {
  611. br.Msg = "设置失败!"
  612. br.ErrMsg = "设置失败,Err:" + err.Error()
  613. return
  614. }
  615. obj := data_manage.EdbInfoShare{}
  616. {
  617. //checkBaseEdbInfoIdList := make([]int, 0)
  618. //checkCalculateEdbInfoIdList := make([]int, 0)
  619. //
  620. //for _, edbItem := range edbInfoList {
  621. // // 设置公开的可以忽略
  622. // if edbItem.PublicStatus == utils.DataPublicSuccess {
  623. // continue
  624. // }
  625. //
  626. // if edbItem.EdbInfoType == utils.EDB_INFO_TYPE {
  627. // checkBaseEdbInfoIdList = append(checkBaseEdbInfoIdList, edbItem.EdbInfoId)
  628. // } else if edbItem.EdbInfoType == utils.PREDICT_EDB_INFO_TYPE {
  629. // checkCalculateEdbInfoIdList = append(checkCalculateEdbInfoIdList, edbItem.EdbInfoId)
  630. // }
  631. //}
  632. //
  633. //checkUserIdList := []int{sysUser.AdminId}
  634. //if len(req.UserIdList) > 0 {
  635. // checkUserIdList = append(checkUserIdList, req.UserIdList...)
  636. //}
  637. //
  638. //// 普通指标
  639. //{
  640. // noReferenced, otherUserIdList, err, tips := data.CheckEdbReferencedByOthers(utils.EDB_INFO_TYPE, checkBaseEdbInfoIdList, checkUserIdList)
  641. // if err != nil {
  642. // br.Msg = "保存失败"
  643. // if tips != "" {
  644. // br.Msg = tips
  645. // }
  646. // br.ErrMsg = "设置指标共享失败,查找指标的关联用户失败,Err:" + err.Error()
  647. // return
  648. // }
  649. //
  650. // if !noReferenced {
  651. // userNameStr := ``
  652. // if len(otherUserIdList) > 0 {
  653. // userNameList := make([]string, 0)
  654. // userList, err := system.GetAdminListByIdList(otherUserIdList)
  655. // if err != nil {
  656. // br.Msg = "保存失败"
  657. // if tips != "" {
  658. // br.Msg = tips
  659. // }
  660. // br.ErrMsg = "设置指标共享失败,查找用户信息失败,Err:" + err.Error()
  661. // return
  662. // }
  663. // for _, userInfo := range userList {
  664. // userNameList = append(userNameList, userInfo.RealName)
  665. // }
  666. // userNameStr = strings.Join(userNameList, "、")
  667. // }
  668. // msg := `指标被其他用户使用,无法设置共享`
  669. // if userNameStr != "" {
  670. // msg = fmt.Sprintf(`指标被用户%s使用,无法设置共享`, userNameStr)
  671. // }
  672. // if tips != "" {
  673. // msg = fmt.Sprintf(`%s,用户为:%s,无法设置共享`, tips, userNameStr)
  674. // }
  675. // br.Msg = msg
  676. // br.ErrMsg = "指标被其他用户共享,无法设置共享"
  677. // br.IsSendEmail = false
  678. // return
  679. // }
  680. //}
  681. //
  682. //// 预测指标
  683. //{
  684. // noReferenced, otherUserIdList, err, tips := data.CheckEdbReferencedByOthers(utils.PREDICT_EDB_INFO_TYPE, checkCalculateEdbInfoIdList, checkUserIdList)
  685. // if err != nil {
  686. // br.Msg = "保存失败"
  687. // if tips != "" {
  688. // br.Msg = tips
  689. // }
  690. // br.ErrMsg = "设置指标共享失败,查找指标的关联用户失败,Err:" + err.Error()
  691. // return
  692. // }
  693. //
  694. // if !noReferenced {
  695. // userNameStr := ``
  696. // if len(otherUserIdList) > 0 {
  697. // userNameList := make([]string, 0)
  698. // userList, err := system.GetAdminListByIdList(otherUserIdList)
  699. // if err != nil {
  700. // br.Msg = "保存失败"
  701. // if tips != "" {
  702. // br.Msg = tips
  703. // }
  704. // br.ErrMsg = "设置指标共享失败,查找用户信息失败,Err:" + err.Error()
  705. // return
  706. // }
  707. // for _, userInfo := range userList {
  708. // userNameList = append(userNameList, userInfo.RealName)
  709. // }
  710. // userNameStr = strings.Join(userNameList, "、")
  711. // }
  712. // msg := `指标被其他用户使用,无法设置共享`
  713. // if userNameStr != "" {
  714. // msg = fmt.Sprintf(`指标被用户%s使用,无法设置共享`, userNameStr)
  715. // }
  716. // if tips != "" {
  717. // msg = fmt.Sprintf(`%s,用户为:%s,无法设置共享`, tips, userNameStr)
  718. // }
  719. // br.Msg = msg
  720. // br.ErrMsg = "指标被其他用户共享,无法设置共享"
  721. // br.IsSendEmail = false
  722. // return
  723. // }
  724. //}
  725. for _, edbItem := range edbInfoList {
  726. // 如果指标已公开,则不判断是否被其他用户使用
  727. if edbItem.PublicStatus == utils.DataPublicSuccess {
  728. continue
  729. }
  730. if edbItem.PublicStatus != utils.DataPublicSuccess {
  731. checkUserIdList := []int{sysUser.AdminId}
  732. if len(req.UserIdList) > 0 {
  733. checkUserIdList = append(checkUserIdList, req.UserIdList...)
  734. }
  735. {
  736. userIdList, err := obj.GetAllUserIdListByEdbInfoIdAndNoShareType(edbItem.EdbInfoId, req.ShareType)
  737. if err != nil {
  738. br.Msg = "设置失败!"
  739. br.ErrMsg = "设置失败,Err:" + err.Error()
  740. return
  741. }
  742. if len(userIdList) > 0 {
  743. checkUserIdList = append(checkUserIdList, userIdList...)
  744. }
  745. }
  746. // 校验是否被其他用户关联
  747. noReferenced, otherUserIdList, err, tips := data.CheckEdbReferencedByOthers(edbItem.EdbInfoType, []int{edbItem.EdbInfoId}, checkUserIdList)
  748. if err != nil {
  749. br.Msg = "保存失败"
  750. if tips != "" {
  751. br.Msg = tips
  752. }
  753. br.ErrMsg = "设置指标共享失败,查找指标的关联用户失败,Err:" + err.Error()
  754. return
  755. }
  756. if !noReferenced {
  757. userNameStr := ``
  758. if len(otherUserIdList) > 0 {
  759. userNameList := make([]string, 0)
  760. userList, err := system.GetAdminListByIdList(otherUserIdList)
  761. if err != nil {
  762. br.Msg = "保存失败"
  763. if tips != "" {
  764. br.Msg = tips
  765. }
  766. br.ErrMsg = "设置指标共享失败,查找用户信息失败,Err:" + err.Error()
  767. return
  768. }
  769. for _, userInfo := range userList {
  770. userNameList = append(userNameList, userInfo.RealName)
  771. }
  772. userNameStr = strings.Join(userNameList, "、")
  773. }
  774. msg := fmt.Sprintf(`指标《%s》被其他用户使用,无法设置共享`, edbItem.EdbName)
  775. if userNameStr != "" {
  776. msg = fmt.Sprintf(`指标《%s》被用户%s使用,无法设置共享`, edbItem.EdbName, userNameStr)
  777. }
  778. if tips != "" {
  779. msg = fmt.Sprintf(`%s,用户为:%s,无法设置共享`, tips, userNameStr)
  780. }
  781. br.Msg = msg
  782. br.ErrMsg = "指标被其他用户共享,无法设置共享,EdbInfoId:" + strconv.Itoa(edbItem.EdbInfoId)
  783. br.IsSendEmail = false
  784. return
  785. }
  786. }
  787. }
  788. }
  789. // 设置
  790. err = obj.SaveEdbInfoShare(selectEdbInfoIdList, req.UserIdList, req.ShareType)
  791. if err != nil {
  792. br.Msg = `保存失败`
  793. br.ErrMsg = `保存失败:` + err.Error()
  794. return
  795. }
  796. // 更新es
  797. for _, edbInfoId := range selectEdbInfoIdList {
  798. data.AddOrEditEdbInfoToEs(edbInfoId)
  799. // 指标收藏操作处理
  800. go cache.AddDataCollectHandler(edbInfoId, utils.DataCollectTypeEdb)
  801. }
  802. br.Ret = 200
  803. br.Success = true
  804. br.Msg = "保存成功"
  805. }
  806. // getAllEdbInfoIdListByShared
  807. // @Description: 获取所有的指标id列表
  808. // @author: Roc
  809. // @datetime 2024-12-04 15:43:14
  810. // @param req request.SetEdbChartPermissionReq
  811. // @param userId int
  812. // @return edbInfoIdList []int
  813. // @return err error
  814. func getAllEdbInfoIdListByShared(req request.SetEdbSharePermissionReq, userId int) (edbInfoIdList []int, err error) {
  815. keyword := req.Keyword
  816. keyword = strings.TrimSpace(keyword) //移除字符串首尾空格
  817. //指标来源
  818. sourceList := req.SourceList
  819. edbInfoType := 0 // 普通指标
  820. edbTypeList := []int{utils.EdbTypeCalculate} // 普通指标中的计算指标
  821. // 指标分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未共享
  822. edbShareList := req.EdbShareList
  823. if len(edbShareList) <= 0 {
  824. edbShareList = []int{1}
  825. }
  826. edbShare := 1 // 0:全部,1:未共享,2:已共享
  827. lenEdbShareList := len(edbShareList)
  828. if len(edbShareList) > 0 {
  829. if lenEdbShareList > 1 {
  830. edbShare = 0
  831. } else {
  832. edbShare = edbShareList[0]
  833. }
  834. }
  835. // 指标分享状态:1-未共享;2-已共享。可多选,多选用英文,隔开;默认是未共享
  836. edbClassifyIdList := req.ClassifyIdList
  837. edbAuth := 1 // 选择范围是:只有我的指标
  838. sortMap := make(map[string]string)
  839. // 如果没有搜索关键词,则默认根据指标编码倒序排序
  840. if keyword == `` {
  841. sortMap["EdbInfoId"] = `desc`
  842. }
  843. _, edbInfoList, err := getAllEdbInfoDataByShared(keyword, 1, edbShare, sourceList, edbClassifyIdList, edbTypeList, edbInfoType, edbAuth, userId, sortMap)
  844. if err != nil {
  845. return
  846. }
  847. // 返回指标id列表
  848. for _, v := range edbInfoList {
  849. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  850. }
  851. return
  852. }
  853. // getAllEdbInfoDataByShared
  854. // @Description: 获取所有的指标列表(设置共享的时候)
  855. // @author: Roc
  856. // @datetime 2024-12-04 15:27:53
  857. // @param keyword string
  858. // @param currPage int
  859. // @param edbShare int
  860. // @param sourceList []int
  861. // @param classifyIdList []int
  862. // @param edbTypeList []int
  863. // @param edbInfoType int
  864. // @param edbAuth int
  865. // @param sysUserId int
  866. // @param sortMap map[string]string
  867. // @return total int64
  868. // @return list []*data_manage.EdbInfoList
  869. // @return err error
  870. 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) {
  871. // 每页获取数据的数量
  872. pageSize := 5000
  873. var startSize int
  874. if currPage <= 0 {
  875. currPage = 1
  876. }
  877. startSize = paging.StartIndex(currPage, pageSize)
  878. total, list, err = elastic.SearchEdbInfoDataByShared(keyword, startSize, pageSize, edbShare, sourceList, classifyIdList, edbTypeList, edbInfoType, edbAuth, sysUserId, sortMap)
  879. if err != nil {
  880. return
  881. }
  882. page := paging.GetPaging(currPage, pageSize, int(total))
  883. if !page.IsEnd {
  884. _, nextList, tmpErr := getAllEdbInfoDataByShared(keyword, page.NextIndex, edbShare, sourceList, classifyIdList, edbTypeList, edbInfoType, edbAuth, sysUserId, sortMap)
  885. if tmpErr != nil {
  886. err = tmpErr
  887. return
  888. }
  889. list = append(list, nextList...)
  890. }
  891. return
  892. }