collect_edb.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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/services/data"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "sort"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // EdbCollectController 指标收藏
  18. type EdbCollectController struct {
  19. controllers.BaseAuthController
  20. }
  21. // List
  22. // @Title 收藏列表-分页
  23. // @Description 收藏列表-分页
  24. // @Param PageSize query int false "每页数据量"
  25. // @Param CurrentIndex query int false "页码"
  26. // @Param ClassifyId query int false "分类ID"
  27. // @Param Keyword query string false "搜索关键词:指标ID/指标名称"
  28. // @Success Ret=200 保存成功
  29. // @router /edb_collect/list [get]
  30. func (this *EdbCollectController) List() {
  31. br := new(models.BaseResponse).Init()
  32. defer func() {
  33. if br.ErrMsg == "" {
  34. br.IsSendEmail = false
  35. }
  36. this.Data["json"] = br
  37. this.ServeJSON()
  38. }()
  39. sysUser := this.SysUser
  40. if sysUser == nil {
  41. br.Msg = "请登录"
  42. br.ErrMsg = "请登录,SysUser Is Empty"
  43. br.Ret = 408
  44. return
  45. }
  46. //resp := new(response.EdbInfoChartListResp)
  47. resp := new(data_manage.CollectEdbInfoListResp)
  48. resp.List = make([]*data_manage.CollectEdbInfoItem, 0)
  49. // 分页
  50. pageSize, _ := this.GetInt("PageSize")
  51. currentIndex, _ := this.GetInt("CurrentIndex")
  52. var total int
  53. page := paging.GetPaging(currentIndex, pageSize, total)
  54. var startSize int
  55. if pageSize <= 0 {
  56. pageSize = utils.PageSize20
  57. }
  58. if currentIndex <= 0 {
  59. currentIndex = 1
  60. }
  61. startSize = paging.StartIndex(currentIndex, pageSize)
  62. // 当前用户
  63. var cond string
  64. var pars []interface{}
  65. cond += ` AND a.sys_user_id = ?`
  66. pars = append(pars, sysUser.AdminId)
  67. // 分类筛选
  68. classifyId, _ := this.GetInt("ClassifyId")
  69. if classifyId > 0 {
  70. classifyIds, e := data.GetEdbCollectClassifyChildIds(classifyId, sysUser.AdminId, true)
  71. if e != nil {
  72. br.Msg = "获取失败"
  73. br.ErrMsg = fmt.Sprintf("获取子分类IDs失败, %v", e)
  74. return
  75. }
  76. if len(classifyIds) == 0 {
  77. resp.Paging = page
  78. br.Data = resp
  79. br.Ret = 200
  80. br.Success = true
  81. br.Msg = "获取成功"
  82. return
  83. }
  84. cond += fmt.Sprintf(` AND a.edb_collect_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  85. pars = append(pars, classifyIds)
  86. }
  87. // 获取当前账号的不可见指标
  88. {
  89. obj := data_manage.EdbInfoNoPermissionAdmin{}
  90. list, e := obj.GetAllListByAdminId(sysUser.AdminId)
  91. if e != nil {
  92. br.Msg = "获取失败"
  93. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + e.Error()
  94. return
  95. }
  96. var edbIds []int
  97. for _, v := range list {
  98. edbIds = append(edbIds, v.EdbInfoId)
  99. }
  100. edbLen := len(edbIds)
  101. if edbLen > 0 {
  102. cond += fmt.Sprintf(` AND a.edb_info_id NOT IN (%s)`, utils.GetOrmInReplace(edbLen))
  103. pars = append(pars, edbIds)
  104. }
  105. }
  106. // 关键词搜索
  107. keywords := this.GetString("Keyword")
  108. keywords = strings.TrimSpace(keywords)
  109. if keywords != "" {
  110. kw := fmt.Sprint("%", keywords, "%")
  111. cond += fmt.Sprintf(` AND (b.edb_code LIKE ? OR b.edb_name LIKE ?)`)
  112. pars = append(pars, kw, kw)
  113. }
  114. // 获取指标列表
  115. dataCount, e := data_manage.GetCollectEdbInfoCount(cond, pars)
  116. if e != nil {
  117. br.Msg = "获取失败"
  118. br.ErrMsg = fmt.Sprintf("获取收藏指标总数失败, %v", e)
  119. return
  120. }
  121. if dataCount == 0 {
  122. resp.Paging = page
  123. br.Data = resp
  124. br.Ret = 200
  125. br.Success = true
  126. br.Msg = "获取成功"
  127. return
  128. }
  129. list, e := data_manage.GetCollectEdbInfoPageList(cond, pars, startSize, pageSize)
  130. if e != nil {
  131. br.Msg = "获取失败"
  132. br.ErrMsg = fmt.Sprintf("获取收藏指标列表失败, %v", e)
  133. return
  134. }
  135. for _, v := range list {
  136. resp.List = append(resp.List, data_manage.FormatEdbInfo2CollectItem(v))
  137. }
  138. sort.Slice(resp.List, func(i, j int) bool {
  139. return resp.List[i].Sort < resp.List[j].Sort
  140. })
  141. page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
  142. resp.Paging = page
  143. br.Data = resp
  144. br.Ret = 200
  145. br.Success = true
  146. br.Msg = "获取成功"
  147. }
  148. // Collect
  149. // @Title 新增收藏
  150. // @Description 新增收藏
  151. // @Param request body data_manage.EdbCollectReq true "type json string"
  152. // @Success Ret=200 保存成功
  153. // @router /edb_collect/collect [post]
  154. func (this *EdbCollectController) Collect() {
  155. br := new(models.BaseResponse).Init()
  156. defer func() {
  157. if br.ErrMsg == "" {
  158. br.IsSendEmail = false
  159. }
  160. this.Data["json"] = br
  161. this.ServeJSON()
  162. }()
  163. sysUser := this.SysUser
  164. if sysUser == nil {
  165. br.Msg = "请登录"
  166. br.ErrMsg = "请登录,SysUser Is Empty"
  167. br.Ret = 408
  168. return
  169. }
  170. var req data_manage.EdbCollectReq
  171. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  172. br.Msg = "参数解析异常"
  173. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  174. return
  175. }
  176. if req.EdbInfoId <= 0 {
  177. br.Msg = "请选择指标"
  178. return
  179. }
  180. edbItem, e := data_manage.GetEdbInfoById(req.EdbInfoId)
  181. if e != nil {
  182. if utils.IsErrNoRow(e) {
  183. br.Msg = "指标不存在, 请刷新页面"
  184. return
  185. }
  186. br.Msg = "操作失败"
  187. br.ErrMsg = fmt.Sprintf("获取指标信息失败: %v", e)
  188. return
  189. }
  190. collectOb := new(data_manage.EdbCollect)
  191. // 待添加的分类配置
  192. addList := make([]*data_manage.EdbCollect, 0)
  193. for _, classifyId := range req.ClassifyIdList {
  194. addList = append(addList, &data_manage.EdbCollect{
  195. EdbCollectClassifyId: classifyId,
  196. EdbInfoId: req.EdbInfoId,
  197. EdbCode: edbItem.EdbCode,
  198. SysUserId: sysUser.AdminId,
  199. SysRealName: sysUser.RealName,
  200. CreateTime: time.Now().Local(),
  201. ModifyTime: time.Now().Local(),
  202. })
  203. }
  204. cond := fmt.Sprintf(" %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().EdbInfoId)
  205. pars := make([]interface{}, 0)
  206. pars = append(pars, sysUser.AdminId, req.EdbInfoId)
  207. // 清除原有保存收藏配置,并新增保存收藏
  208. e = collectOb.RemoveAndCreateMulti(cond, pars, addList)
  209. if e != nil {
  210. br.Msg = "操作失败"
  211. br.ErrMsg = fmt.Sprintf("新增指标收藏失败: %v", e)
  212. return
  213. }
  214. br.Ret = 200
  215. br.Success = true
  216. br.Msg = "操作成功"
  217. }
  218. // CancelCollect
  219. // @Title 取消收藏
  220. // @Description 取消收藏
  221. // @Param request body data_manage.EdbCollectReq true "type json string"
  222. // @Success Ret=200 保存成功
  223. // @router /edb_collect/cancel_collect [post]
  224. func (this *EdbCollectController) CancelCollect() {
  225. br := new(models.BaseResponse).Init()
  226. defer func() {
  227. if br.ErrMsg == "" {
  228. br.IsSendEmail = false
  229. }
  230. this.Data["json"] = br
  231. this.ServeJSON()
  232. }()
  233. sysUser := this.SysUser
  234. if sysUser == nil {
  235. br.Msg = "请登录"
  236. br.ErrMsg = "请登录,SysUser Is Empty"
  237. br.Ret = 408
  238. return
  239. }
  240. var req data_manage.EdbCollectReq
  241. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  242. br.Msg = "参数解析异常"
  243. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  244. return
  245. }
  246. if req.EdbInfoId <= 0 {
  247. br.Msg = "请选择指标"
  248. return
  249. }
  250. if req.ClassifyId <= 0 {
  251. br.Msg = "请选择分类"
  252. return
  253. }
  254. // 取消收藏
  255. collectOb := new(data_manage.EdbCollect)
  256. cond := fmt.Sprintf("%s = ? AND %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().EdbCollectClassifyId, collectOb.Cols().EdbInfoId)
  257. pars := make([]interface{}, 0)
  258. pars = append(pars, sysUser.AdminId, req.ClassifyId, req.EdbInfoId)
  259. if e := collectOb.RemoveByCondition(cond, pars); e != nil {
  260. br.Msg = "操作失败"
  261. br.ErrMsg = fmt.Sprintf("取消收藏失败: %v", e)
  262. return
  263. }
  264. br.Ret = 200
  265. br.Success = true
  266. br.Msg = "操作成功"
  267. }
  268. // Move
  269. // @Title 移动收藏
  270. // @Description 移动收藏
  271. // @Success 200 {object} data_manage.EdbCollectMoveReq
  272. // @router /edb_collect/move [post]
  273. func (this *EdbCollectController) Move() {
  274. br := new(models.BaseResponse).Init()
  275. defer func() {
  276. if br.ErrMsg == "" {
  277. br.IsSendEmail = false
  278. }
  279. this.Data["json"] = br
  280. this.ServeJSON()
  281. }()
  282. sysUser := this.SysUser
  283. if sysUser == nil {
  284. br.Msg = "请登录"
  285. br.ErrMsg = "请登录,SysUser Is Empty"
  286. br.Ret = 408
  287. return
  288. }
  289. var req data_manage.EdbCollectMoveReq
  290. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  291. br.Msg = "参数解析异常!"
  292. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  293. return
  294. }
  295. var newSort int
  296. if req.PrevEdbInfoId <= 0 {
  297. firstOne, err := data_manage.GetEdbCollectSort(sysUser.AdminId, req.ClassifyId, 0)
  298. if err != nil {
  299. br.Msg = "获取数据失败!"
  300. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  301. return
  302. }
  303. newSort = firstOne.Sort - 1
  304. } else if req.NextEdbInfoId <= 0 {
  305. lastOne, err := data_manage.GetEdbCollectSort(sysUser.AdminId, req.ClassifyId, 1)
  306. if err != nil {
  307. br.Msg = "获取数据失败!"
  308. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  309. return
  310. }
  311. newSort = lastOne.Sort + 1
  312. } else {
  313. preMapItem, err := data_manage.GetEdbCollectByEdbInfoId(sysUser.AdminId, req.PrevEdbInfoId, req.ClassifyId)
  314. if err != nil {
  315. br.Msg = "获取数据失败!"
  316. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  317. return
  318. }
  319. nextMapItem, err := data_manage.GetEdbCollectByEdbInfoId(sysUser.AdminId, req.NextEdbInfoId, req.ClassifyId)
  320. if err != nil {
  321. br.Msg = "获取数据失败!"
  322. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  323. return
  324. }
  325. newSort = (preMapItem.Sort + nextMapItem.Sort) / 2
  326. newSort = preMapItem.Sort + 1
  327. var updateSortStr string
  328. if preMapItem.Sort == nextMapItem.Sort {
  329. updateSortStr = `sort + 2`
  330. } else if nextMapItem.Sort-preMapItem.Sort == 1 {
  331. updateSortStr = `sort + 1`
  332. }
  333. if updateSortStr != `` {
  334. _ = data_manage.UpdateEdbCollectSortByClassifyId(req.ClassifyId, preMapItem.Sort, preMapItem.EdbCollectId, updateSortStr)
  335. }
  336. }
  337. if e := data_manage.UpdateEdbCollectMove(newSort, sysUser.AdminId, req.EdbInfoId, req.ClassifyId); e != nil {
  338. br.Msg = "操作失败"
  339. br.ErrMsg = fmt.Sprintf("更新指标收藏失败, %v", e)
  340. return
  341. }
  342. br.Ret = 200
  343. br.Success = true
  344. br.Msg = "操作成功"
  345. }
  346. // BatchCollect
  347. // @Title 批量收藏
  348. // @Description 批量收藏
  349. // @Param request body data_manage.EdbCollectReq true "type json string"
  350. // @Success Ret=200 保存成功
  351. // @router /edb_collect/batch_collect [post]
  352. func (this *EdbCollectController) BatchCollect() {
  353. br := new(models.BaseResponse).Init()
  354. defer func() {
  355. if br.ErrMsg == "" {
  356. br.IsSendEmail = false
  357. }
  358. this.Data["json"] = br
  359. this.ServeJSON()
  360. }()
  361. sysUser := this.SysUser
  362. if sysUser == nil {
  363. br.Msg = "请登录"
  364. br.ErrMsg = "请登录,SysUser Is Empty"
  365. br.Ret = 408
  366. return
  367. }
  368. var req request.BatchEdbCollectReq
  369. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  370. if err != nil {
  371. br.Msg = "参数解析异常"
  372. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", err)
  373. return
  374. }
  375. cacheKey := "CACHE_EDB_INFO_COLLECT_" + strconv.Itoa(sysUser.AdminId)
  376. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  377. br.Msg = "系统处理中,请稍后重试!"
  378. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  379. return
  380. }
  381. defer func() {
  382. utils.Rc.Delete(cacheKey)
  383. }()
  384. if len(req.CollectClassifyIdList) <= 0 {
  385. br.Msg = "请选择需要收藏的分类"
  386. br.IsSendEmail = false
  387. return
  388. }
  389. // 选中的指标id列表
  390. edbInfoList := make([]*data_manage.EdbInfoList, 0)
  391. // 选择所有指标,所以需要通过es获取数据
  392. if req.IsSelectAll {
  393. tmpEdbInfoList, err, errMsg := data.GetAllGeneralEdbInfoListByGeneralEdbEsSearchReq(req.GeneralEdbEsSearchReq, this.SysUser.AdminId)
  394. if err != nil {
  395. br.Msg = "获取指标列表失败!"
  396. if errMsg != `` {
  397. br.Msg = errMsg
  398. }
  399. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  400. return
  401. }
  402. // 如果有过滤指标,那么就过滤吧
  403. if len(req.NoEdbIdList) > 0 {
  404. noEdbIdMap := make(map[int]bool)
  405. for _, v := range req.NoEdbIdList {
  406. noEdbIdMap[v] = true
  407. }
  408. for _, v := range tmpEdbInfoList {
  409. if _, ok := noEdbIdMap[v.EdbInfoId]; !ok {
  410. // 如果不在未选中的指标id列表中,那么就加入到选中的指标id列表
  411. edbInfoList = append(edbInfoList, v)
  412. }
  413. }
  414. } else {
  415. edbInfoList = tmpEdbInfoList
  416. }
  417. // 因为是ES查找的,所以需要重新查一下指标的信息,主要是为了把是否授权字段找出来
  418. if len(edbInfoList) > 0 {
  419. edbInfoIdList := make([]int, 0)
  420. for _, v := range edbInfoList {
  421. v.ConvertToResp()
  422. v.EdbNameAlias = v.EdbName
  423. v.HaveOperaAuth = true
  424. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  425. }
  426. tmpEdbList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  427. if err != nil {
  428. br.Msg = "获取失败"
  429. br.ErrMsg = "获取所有有权限的指标失败,Err:" + err.Error()
  430. return
  431. }
  432. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  433. for _, v := range tmpEdbList {
  434. edbInfoMap[v.EdbInfoId] = v
  435. }
  436. for _, v := range edbInfoList {
  437. tmpEdbInfo, ok := edbInfoMap[v.EdbInfoId]
  438. if !ok {
  439. continue
  440. }
  441. v.IsJoinPermission = tmpEdbInfo.IsJoinPermission
  442. }
  443. }
  444. } else {
  445. // 因为是ES查找的,所以需要重新查一下指标的信息,主要是为了把是否授权字段找出来
  446. if len(req.EdbIdList) > 0 {
  447. edbInfoList, err = data_manage.GetEdbInfoListByCond(` AND edb_info_id in (?) `, []interface{}{req.EdbIdList})
  448. if err != nil {
  449. br.Msg = "获取失败"
  450. br.ErrMsg = "获取所有有权限的指标失败,Err:" + err.Error()
  451. return
  452. }
  453. }
  454. }
  455. if len(edbInfoList) <= 0 {
  456. br.Msg = `请选择指标`
  457. return
  458. }
  459. edbInfoIdList := make([]int, 0)
  460. for _, v := range edbInfoList {
  461. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  462. }
  463. collectOb := new(data_manage.EdbCollect)
  464. cond := fmt.Sprintf(" AND %s = ? AND %s in (?) ", collectOb.Cols().SysUserId, collectOb.Cols().EdbInfoId)
  465. pars := make([]interface{}, 0)
  466. pars = append(pars, sysUser.AdminId, edbInfoIdList)
  467. collectList, err := collectOb.GetItemsByCondition(cond, pars, []string{}, ``)
  468. if err != nil {
  469. br.Msg = "操作失败"
  470. br.ErrMsg = "获取收藏列表列表,Err:" + err.Error()
  471. return
  472. }
  473. collectEdbClassifyMap := make(map[int]map[int]bool)
  474. for _, v := range collectList {
  475. edbClassifyMap, ok := collectEdbClassifyMap[v.EdbInfoId]
  476. if !ok {
  477. edbClassifyMap = make(map[int]bool)
  478. }
  479. edbClassifyMap[v.EdbCollectClassifyId] = true
  480. collectEdbClassifyMap[v.EdbInfoId] = edbClassifyMap
  481. }
  482. // 待添加的分类配置
  483. addList := make([]*data_manage.EdbCollect, 0)
  484. for _, edbInfo := range edbInfoList {
  485. for _, classifyId := range req.CollectClassifyIdList {
  486. edbClassifyMap, ok := collectEdbClassifyMap[edbInfo.EdbInfoId]
  487. if ok {
  488. if _, ok2 := edbClassifyMap[classifyId]; ok2 {
  489. // 如果当前收藏分类下,存在该指标的收藏记录,那么就跳过
  490. continue
  491. }
  492. }
  493. addList = append(addList, &data_manage.EdbCollect{
  494. EdbCollectClassifyId: classifyId,
  495. EdbInfoId: edbInfo.EdbInfoId,
  496. EdbCode: edbInfo.EdbCode,
  497. SysUserId: sysUser.AdminId,
  498. SysRealName: sysUser.RealName,
  499. CreateTime: time.Now().Local(),
  500. ModifyTime: time.Now().Local(),
  501. })
  502. }
  503. }
  504. // 并新增保存收藏
  505. err = collectOb.CreateMulti(addList)
  506. if err != nil {
  507. br.Msg = "操作失败"
  508. br.ErrMsg = fmt.Sprintf("新增指标批量收藏失败: %v", err)
  509. return
  510. }
  511. br.Ret = 200
  512. br.Success = true
  513. br.Msg = "操作成功"
  514. }