collect_edb.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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/services/data"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "sort"
  12. "strings"
  13. "time"
  14. )
  15. // EdbCollectController 指标收藏
  16. type EdbCollectController struct {
  17. controllers.BaseAuthController
  18. }
  19. // List
  20. // @Title 收藏列表-分页
  21. // @Description 收藏列表-分页
  22. // @Param PageSize query int false "每页数据量"
  23. // @Param CurrentIndex query int false "页码"
  24. // @Param ClassifyId query int false "分类ID"
  25. // @Param Keyword query string false "搜索关键词:指标ID/指标名称"
  26. // @Success Ret=200 保存成功
  27. // @router /edb_collect/list [get]
  28. func (this *EdbCollectController) List() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. if br.ErrMsg == "" {
  32. br.IsSendEmail = false
  33. }
  34. this.Data["json"] = br
  35. this.ServeJSON()
  36. }()
  37. sysUser := this.SysUser
  38. if sysUser == nil {
  39. br.Msg = "请登录"
  40. br.ErrMsg = "请登录,SysUser Is Empty"
  41. br.Ret = 408
  42. return
  43. }
  44. //resp := new(response.EdbInfoChartListResp)
  45. resp := new(data_manage.CollectEdbInfoListResp)
  46. resp.List = make([]*data_manage.CollectEdbInfoItem, 0)
  47. // 分页
  48. pageSize, _ := this.GetInt("PageSize")
  49. currentIndex, _ := this.GetInt("CurrentIndex")
  50. var total int
  51. page := paging.GetPaging(currentIndex, pageSize, total)
  52. var startSize int
  53. if pageSize <= 0 {
  54. pageSize = utils.PageSize20
  55. }
  56. if currentIndex <= 0 {
  57. currentIndex = 1
  58. }
  59. startSize = paging.StartIndex(currentIndex, pageSize)
  60. // 当前用户
  61. var cond string
  62. var pars []interface{}
  63. cond += ` AND a.sys_user_id = ?`
  64. pars = append(pars, sysUser.AdminId)
  65. // 分类筛选
  66. classifyId, _ := this.GetInt("ClassifyId")
  67. if classifyId > 0 {
  68. classifyIds, e := data.GetEdbCollectClassifyChildIds(classifyId, sysUser.AdminId, true)
  69. if e != nil {
  70. br.Msg = "获取失败"
  71. br.ErrMsg = fmt.Sprintf("获取子分类IDs失败, %v", e)
  72. return
  73. }
  74. if len(classifyIds) == 0 {
  75. resp.Paging = page
  76. br.Data = resp
  77. br.Ret = 200
  78. br.Success = true
  79. br.Msg = "获取成功"
  80. return
  81. }
  82. cond += fmt.Sprintf(` AND a.edb_collect_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  83. pars = append(pars, classifyIds)
  84. }
  85. // 获取当前账号的不可见指标
  86. {
  87. obj := data_manage.EdbInfoNoPermissionAdmin{}
  88. list, e := obj.GetAllListByAdminId(sysUser.AdminId)
  89. if e != nil {
  90. br.Msg = "获取失败"
  91. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + e.Error()
  92. return
  93. }
  94. var edbIds []int
  95. for _, v := range list {
  96. edbIds = append(edbIds, v.EdbInfoId)
  97. }
  98. edbLen := len(edbIds)
  99. if edbLen > 0 {
  100. cond += fmt.Sprintf(` AND a.edb_info_id NOT IN (%s)`, utils.GetOrmInReplace(edbLen))
  101. pars = append(pars, edbIds)
  102. }
  103. }
  104. // 关键词搜索
  105. keywords := this.GetString("Keyword")
  106. keywords = strings.TrimSpace(keywords)
  107. if keywords != "" {
  108. kw := fmt.Sprint("%", keywords, "%")
  109. cond += fmt.Sprintf(` AND (b.edb_code LIKE ? OR b.edb_name LIKE ?)`)
  110. pars = append(pars, kw, kw)
  111. }
  112. // 获取指标列表
  113. dataCount, e := data_manage.GetCollectEdbInfoCount(cond, pars)
  114. if e != nil {
  115. br.Msg = "获取失败"
  116. br.ErrMsg = fmt.Sprintf("获取收藏指标总数失败, %v", e)
  117. return
  118. }
  119. if dataCount == 0 {
  120. resp.Paging = page
  121. br.Data = resp
  122. br.Ret = 200
  123. br.Success = true
  124. br.Msg = "获取成功"
  125. return
  126. }
  127. list, e := data_manage.GetCollectEdbInfoPageList(cond, pars, startSize, pageSize)
  128. if e != nil {
  129. br.Msg = "获取失败"
  130. br.ErrMsg = fmt.Sprintf("获取收藏指标列表失败, %v", e)
  131. return
  132. }
  133. for _, v := range list {
  134. resp.List = append(resp.List, data_manage.FormatEdbInfo2CollectItem(v))
  135. }
  136. sort.Slice(resp.List, func(i, j int) bool {
  137. return resp.List[i].Sort < resp.List[j].Sort
  138. })
  139. page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
  140. resp.Paging = page
  141. br.Data = resp
  142. br.Ret = 200
  143. br.Success = true
  144. br.Msg = "获取成功"
  145. }
  146. // Collect
  147. // @Title 新增收藏
  148. // @Description 新增收藏
  149. // @Param request body data_manage.EdbCollectReq true "type json string"
  150. // @Success Ret=200 保存成功
  151. // @router /edb_collect/collect [post]
  152. func (this *EdbCollectController) Collect() {
  153. br := new(models.BaseResponse).Init()
  154. defer func() {
  155. if br.ErrMsg == "" {
  156. br.IsSendEmail = false
  157. }
  158. this.Data["json"] = br
  159. this.ServeJSON()
  160. }()
  161. sysUser := this.SysUser
  162. if sysUser == nil {
  163. br.Msg = "请登录"
  164. br.ErrMsg = "请登录,SysUser Is Empty"
  165. br.Ret = 408
  166. return
  167. }
  168. var req data_manage.EdbCollectReq
  169. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  170. br.Msg = "参数解析异常"
  171. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  172. return
  173. }
  174. if req.EdbInfoId <= 0 {
  175. br.Msg = "请选择指标"
  176. return
  177. }
  178. edbItem, e := data_manage.GetEdbInfoById(req.EdbInfoId)
  179. if e != nil {
  180. if utils.IsErrNoRow(e) {
  181. br.Msg = "指标不存在, 请刷新页面"
  182. return
  183. }
  184. br.Msg = "操作失败"
  185. br.ErrMsg = fmt.Sprintf("获取指标信息失败: %v", e)
  186. return
  187. }
  188. collectOb := new(data_manage.EdbCollect)
  189. // 待添加的分类配置
  190. addList := make([]*data_manage.EdbCollect, 0)
  191. for _, classifyId := range req.ClassifyIdList {
  192. addList = append(addList, &data_manage.EdbCollect{
  193. EdbCollectClassifyId: classifyId,
  194. EdbInfoId: req.EdbInfoId,
  195. EdbCode: edbItem.EdbCode,
  196. SysUserId: sysUser.AdminId,
  197. SysRealName: sysUser.RealName,
  198. CreateTime: time.Now().Local(),
  199. ModifyTime: time.Now().Local(),
  200. })
  201. }
  202. cond := fmt.Sprintf(" %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().EdbInfoId)
  203. pars := make([]interface{}, 0)
  204. pars = append(pars, sysUser.AdminId, req.EdbInfoId)
  205. // 清除原有保存收藏配置,并新增保存收藏
  206. e = collectOb.RemoveAndCreateMulti(cond, pars, addList)
  207. if e != nil {
  208. br.Msg = "操作失败"
  209. br.ErrMsg = fmt.Sprintf("新增指标收藏失败: %v", e)
  210. return
  211. }
  212. br.Ret = 200
  213. br.Success = true
  214. br.Msg = "操作成功"
  215. }
  216. // CancelCollect
  217. // @Title 取消收藏
  218. // @Description 取消收藏
  219. // @Param request body data_manage.EdbCollectReq true "type json string"
  220. // @Success Ret=200 保存成功
  221. // @router /edb_collect/cancel_collect [post]
  222. func (this *EdbCollectController) CancelCollect() {
  223. br := new(models.BaseResponse).Init()
  224. defer func() {
  225. if br.ErrMsg == "" {
  226. br.IsSendEmail = false
  227. }
  228. this.Data["json"] = br
  229. this.ServeJSON()
  230. }()
  231. sysUser := this.SysUser
  232. if sysUser == nil {
  233. br.Msg = "请登录"
  234. br.ErrMsg = "请登录,SysUser Is Empty"
  235. br.Ret = 408
  236. return
  237. }
  238. var req data_manage.EdbCollectReq
  239. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  240. br.Msg = "参数解析异常"
  241. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  242. return
  243. }
  244. if req.EdbInfoId <= 0 {
  245. br.Msg = "请选择指标"
  246. return
  247. }
  248. if req.ClassifyId <= 0 {
  249. br.Msg = "请选择分类"
  250. return
  251. }
  252. // 取消收藏
  253. collectOb := new(data_manage.EdbCollect)
  254. cond := fmt.Sprintf("%s = ? AND %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().EdbCollectClassifyId, collectOb.Cols().EdbInfoId)
  255. pars := make([]interface{}, 0)
  256. pars = append(pars, sysUser.AdminId, req.ClassifyId, req.EdbInfoId)
  257. if e := collectOb.RemoveByCondition(cond, pars); e != nil {
  258. br.Msg = "操作失败"
  259. br.ErrMsg = fmt.Sprintf("取消收藏失败: %v", e)
  260. return
  261. }
  262. br.Ret = 200
  263. br.Success = true
  264. br.Msg = "操作成功"
  265. }
  266. // Move
  267. // @Title 移动收藏
  268. // @Description 移动收藏
  269. // @Success 200 {object} data_manage.EdbCollectMoveReq
  270. // @router /edb_collect/move [post]
  271. func (this *EdbCollectController) Move() {
  272. br := new(models.BaseResponse).Init()
  273. defer func() {
  274. if br.ErrMsg == "" {
  275. br.IsSendEmail = false
  276. }
  277. this.Data["json"] = br
  278. this.ServeJSON()
  279. }()
  280. sysUser := this.SysUser
  281. if sysUser == nil {
  282. br.Msg = "请登录"
  283. br.ErrMsg = "请登录,SysUser Is Empty"
  284. br.Ret = 408
  285. return
  286. }
  287. var req data_manage.EdbCollectMoveReq
  288. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  289. br.Msg = "参数解析异常!"
  290. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  291. return
  292. }
  293. var newSort int
  294. if req.PrevEdbInfoId <= 0 {
  295. firstOne, err := data_manage.GetEdbCollectSort(sysUser.AdminId, req.ClassifyId, 0)
  296. if err != nil {
  297. br.Msg = "获取数据失败!"
  298. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  299. return
  300. }
  301. newSort = firstOne.Sort - 1
  302. } else if req.NextEdbInfoId <= 0 {
  303. lastOne, err := data_manage.GetEdbCollectSort(sysUser.AdminId, req.ClassifyId, 1)
  304. if err != nil {
  305. br.Msg = "获取数据失败!"
  306. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  307. return
  308. }
  309. newSort = lastOne.Sort + 1
  310. } else {
  311. preMapItem, err := data_manage.GetEdbCollectByEdbInfoId(sysUser.AdminId, req.PrevEdbInfoId, req.ClassifyId)
  312. if err != nil {
  313. br.Msg = "获取数据失败!"
  314. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  315. return
  316. }
  317. nextMapItem, err := data_manage.GetEdbCollectByEdbInfoId(sysUser.AdminId, req.NextEdbInfoId, req.ClassifyId)
  318. if err != nil {
  319. br.Msg = "获取数据失败!"
  320. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  321. return
  322. }
  323. newSort = (preMapItem.Sort + nextMapItem.Sort) / 2
  324. newSort = preMapItem.Sort + 1
  325. var updateSortStr string
  326. if preMapItem.Sort == nextMapItem.Sort {
  327. updateSortStr = `sort + 2`
  328. } else if nextMapItem.Sort-preMapItem.Sort == 1 {
  329. updateSortStr = `sort + 1`
  330. }
  331. if updateSortStr != `` {
  332. _ = data_manage.UpdateEdbCollectSortByClassifyId(req.ClassifyId, preMapItem.Sort, preMapItem.EdbCollectId, updateSortStr)
  333. }
  334. }
  335. if e := data_manage.UpdateEdbCollectMove(newSort, sysUser.AdminId, req.EdbInfoId, req.ClassifyId); e != nil {
  336. br.Msg = "操作失败"
  337. br.ErrMsg = fmt.Sprintf("更新指标收藏失败, %v", e)
  338. return
  339. }
  340. br.Ret = 200
  341. br.Success = true
  342. br.Msg = "操作成功"
  343. }
  344. // BatchCollect
  345. // @Title 批量收藏
  346. // @Description 批量收藏
  347. // @Param request body data_manage.EdbCollectReq true "type json string"
  348. // @Success Ret=200 保存成功
  349. // @router /edb_collect/batch_collect [post]
  350. //func (this *EdbCollectController) BatchCollect() {
  351. // br := new(models.BaseResponse).Init()
  352. // defer func() {
  353. // if br.ErrMsg == "" {
  354. // br.IsSendEmail = false
  355. // }
  356. // this.Data["json"] = br
  357. // this.ServeJSON()
  358. // }()
  359. // sysUser := this.SysUser
  360. // if sysUser == nil {
  361. // br.Msg = "请登录"
  362. // br.ErrMsg = "请登录,SysUser Is Empty"
  363. // br.Ret = 408
  364. // return
  365. // }
  366. // var req data_manage.BatchEdbCollectReq
  367. // if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  368. // br.Msg = "参数解析异常"
  369. // br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  370. // return
  371. // }
  372. // if len(req.EdbInfoIdList) <= 0 {
  373. // br.Msg = "请选择指标"
  374. // br.IsSendEmail = false
  375. // return
  376. // }
  377. // if len(req.ClassifyIdList) <= 0 {
  378. // br.Msg = "请选择需要收藏的分类"
  379. // br.IsSendEmail = false
  380. // return
  381. // }
  382. //
  383. // // 获取es搜索参数
  384. // source, frequency, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList, edbInfoType, edbAuth, searchUserId, err, errMsg := data.GetGeneralEdbEsSearchParams(edbTypeStr, this.SysUser.AdminId, edbAuth, edbCollect, classifyId)
  385. //
  386. // edbItemList, e := data_manage.GetEdbInfoListByEdbInfoId(req.EdbInfoIdList)
  387. // if e != nil {
  388. // if utils.IsErrNoRow(e) {
  389. // br.Msg = "指标不存在, 请刷新页面"
  390. // return
  391. // }
  392. // br.Msg = "操作失败"
  393. // br.ErrMsg = fmt.Sprintf("获取指标信息失败: %v", e)
  394. // return
  395. // }
  396. //
  397. // collectOb := new(data_manage.EdbCollect)
  398. //
  399. // // 待添加的分类配置
  400. // addList := make([]*data_manage.EdbCollect, 0)
  401. // for _, classifyId := range req.ClassifyIdList {
  402. // addList = append(addList, &data_manage.EdbCollect{
  403. // EdbCollectClassifyId: classifyId,
  404. // EdbInfoId: req.EdbInfoId,
  405. // EdbCode: edbItem.EdbCode,
  406. // SysUserId: sysUser.AdminId,
  407. // SysRealName: sysUser.RealName,
  408. // CreateTime: time.Now().Local(),
  409. // ModifyTime: time.Now().Local(),
  410. // })
  411. //
  412. // }
  413. //
  414. // cond := fmt.Sprintf(" %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().EdbInfoId)
  415. // pars := make([]interface{}, 0)
  416. // pars = append(pars, sysUser.AdminId, req.EdbInfoId)
  417. //
  418. // // 清除原有保存收藏配置,并新增保存收藏
  419. // e = collectOb.RemoveAndCreateMulti(cond, pars, addList)
  420. // if e != nil {
  421. // br.Msg = "操作失败"
  422. // br.ErrMsg = fmt.Sprintf("新增指标收藏失败: %v", e)
  423. // return
  424. // }
  425. //
  426. // br.Ret = 200
  427. // br.Success = true
  428. // br.Msg = "操作成功"
  429. //}