edb_info_share.go 25 KB

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