public_chart.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. dataApproveSerice "eta_gn/eta_api/services/data/data_approve"
  10. "eta_gn/eta_api/services/elastic"
  11. "eta_gn/eta_api/utils"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. "strconv"
  14. "strings"
  15. )
  16. // ChartPublicController 公共图表
  17. type ChartPublicController struct {
  18. controllers.BaseAuthController
  19. }
  20. // Save
  21. // @Title 单个图表设置公开
  22. // @Description 单个图表设置公开
  23. // @Param request body request.SetChartInfoShareReq true "type json string"
  24. // @Success 200 {object} data_manage.ChartInfo
  25. // @router /chart_info/public/save [post]
  26. func (c *ChartPublicController) Save() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. c.Data["json"] = br
  30. c.ServeJSON()
  31. }()
  32. sysUser := c.SysUser
  33. if sysUser == nil {
  34. br.Msg = "请登录"
  35. br.ErrMsg = "请登录,SysUser Is Empty"
  36. br.Ret = 408
  37. return
  38. }
  39. var req request.SetChartPublicReq
  40. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  41. if err != nil {
  42. br.Msg = "参数解析异常!"
  43. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  44. return
  45. }
  46. if len(req.ChartInfoList) <= 0 {
  47. br.Msg = `请选择图表`
  48. br.IsSendEmail = false
  49. return
  50. }
  51. // 待处理的资产
  52. dataPublicItemList := make([]dataApproveSerice.SetDataPublicItem, 0)
  53. // 校验是否重复存在已公开、已提交的图表
  54. {
  55. chartInfoIdList := make([]int, 0)
  56. for _, item := range req.ChartInfoList {
  57. chartInfoIdList = append(chartInfoIdList, item.ChartInfoId)
  58. dataPublicItemList = append(dataPublicItemList, dataApproveSerice.SetDataPublicItem{
  59. DataId: item.ChartInfoId,
  60. ClassifyId: item.ClassifyId,
  61. })
  62. }
  63. list, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  64. if err != nil {
  65. br.Msg = "保存失败!"
  66. br.ErrMsg = "获取图表数据失败,Err:" + err.Error()
  67. return
  68. }
  69. for _, v := range list {
  70. if v.PublicStatus != utils.DataPublicDefault {
  71. br.Msg = `选择图表中存在已经公开/提交审批的图表,请仅选择未公开图表!`
  72. br.IsSendEmail = false
  73. return
  74. }
  75. }
  76. }
  77. // 校验是否开启了审批流
  78. opening, e := dataApproveSerice.CheckOpenApprove(utils.DataApproveTypeChart)
  79. if e != nil {
  80. br.Msg = "操作失败"
  81. br.ErrMsg = "校验图表公开是否开启审批流失败, Err: " + e.Error()
  82. return
  83. }
  84. // 是否忽略审批
  85. var isIgnoreApprove bool
  86. {
  87. businessConf, e := models.GetBusinessConfByKey(models.IgnoreChartApproveUserId)
  88. if e != nil {
  89. br.Msg = "获取失败"
  90. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  91. return
  92. }
  93. ignoreChartApproveUserIdList := strings.Split(businessConf.ConfVal, `,`)
  94. if utils.InArrayByStr(ignoreChartApproveUserIdList, strconv.Itoa(c.SysUser.AdminId)) {
  95. isIgnoreApprove = true
  96. }
  97. }
  98. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoList[0].ChartInfoId)
  99. if err != nil {
  100. br.Msg = "获取图表失败"
  101. br.ErrMsg = "获取图表失败:" + err.Error()
  102. return
  103. }
  104. title := chartInfo.ChartName
  105. if len(req.ChartInfoList) > 1 {
  106. title += `等图表`
  107. }
  108. title += `公开审批`
  109. // 没开启审批流、或者无需审批
  110. if !opening || isIgnoreApprove {
  111. err = dataApproveSerice.UpdatePublicByDataList(utils.DataApproveTypeChart, dataApproveSerice.DataApproveStatePass, dataPublicItemList)
  112. } else {
  113. _, err = dataApproveSerice.SubmitDataApprove(utils.DataApproveTypeChart, dataPublicItemList, title, strings.TrimSpace(req.Description), c.SysUser.AdminId, c.SysUser.RealName)
  114. if err != nil {
  115. br.Msg = "提交审批失败"
  116. br.ErrMsg = "提交审批失败, Err: " + err.Error()
  117. return
  118. }
  119. // 更新ES
  120. for _, dataPublicItem := range dataPublicItemList {
  121. data.AddOrEditChartInfoToEs(dataPublicItem.DataId)
  122. }
  123. }
  124. if err != nil {
  125. br.Msg = "编辑失败"
  126. br.ErrMsg = "编辑失败,Err:" + err.Error()
  127. return
  128. }
  129. br.Ret = 200
  130. br.Success = true
  131. br.Msg = "保存成功"
  132. }
  133. // Cancel
  134. // @Title 撤销图表公开
  135. // @Description 撤销图表公开
  136. // @Param ChartInfoId query int true "图表id"
  137. // @Success 200 {object} data_manage.ChartInfo
  138. // @router /chart_info/public/cancel [post]
  139. func (c *ChartPublicController) Cancel() {
  140. br := new(models.BaseResponse).Init()
  141. defer func() {
  142. c.Data["json"] = br
  143. c.ServeJSON()
  144. }()
  145. // TODO 单个图表撤销的时候,需要校验状态,然后撤销
  146. sysUser := c.SysUser
  147. if sysUser == nil {
  148. br.Msg = "请登录"
  149. br.ErrMsg = "请登录,SysUser Is Empty"
  150. br.Ret = 408
  151. return
  152. }
  153. chartInfoId, _ := c.GetInt("ChartInfoId")
  154. if chartInfoId <= 0 {
  155. br.Msg = "参数错误"
  156. br.ErrMsg = "参数错误"
  157. return
  158. }
  159. var req request.SetChartPublicReq
  160. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  161. if err != nil {
  162. br.Msg = "参数解析异常!"
  163. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  164. return
  165. }
  166. if len(req.ChartInfoList) <= 0 {
  167. br.Msg = `请选择图表`
  168. br.IsSendEmail = false
  169. return
  170. }
  171. // 待处理的资产
  172. dataPublicItemList := make([]dataApproveSerice.SetDataPublicItem, 0)
  173. // 校验是否重复存在已公开、已提交的图表
  174. {
  175. chartInfoIdList := make([]int, 0)
  176. for _, item := range req.ChartInfoList {
  177. chartInfoIdList = append(chartInfoIdList, item.ChartInfoId)
  178. dataPublicItemList = append(dataPublicItemList, dataApproveSerice.SetDataPublicItem{
  179. DataId: item.ChartInfoId,
  180. ClassifyId: item.ClassifyId,
  181. })
  182. }
  183. list, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  184. if err != nil {
  185. br.Msg = "保存失败!"
  186. br.ErrMsg = "获取图表数据失败,Err:" + err.Error()
  187. return
  188. }
  189. for _, v := range list {
  190. if v.PublicStatus != utils.DataPublicDefault {
  191. br.Msg = `选择图表中存在已经公开/提交审批的图表,请仅选择未公开图表!`
  192. br.IsSendEmail = false
  193. return
  194. }
  195. }
  196. }
  197. br.Ret = 200
  198. br.Success = true
  199. br.Msg = "保存成功"
  200. }
  201. // ListByEs
  202. // @Title 图表筛选接口
  203. // @Description 图表筛选接口
  204. // @Success 200 {object} data_manage.ChartInfoList
  205. // @Param request body request.SearchPublicChartReq true "type json string"
  206. // @Success 200 {object} data_manage.ChartInfoFilterDataResp
  207. // @router /chart_info/public/list/es [post]
  208. func (c *ChartPublicController) ListByEs() {
  209. br := new(models.BaseResponse).Init()
  210. defer func() {
  211. c.Data["json"] = br
  212. c.ServeJSON()
  213. }()
  214. var req request.SearchPublicChartReq
  215. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  216. if err != nil {
  217. br.Msg = "参数解析异常!"
  218. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  219. return
  220. }
  221. pageSize := req.PageSize
  222. currentIndex := req.CurrentIndex
  223. var total int64
  224. var startSize int
  225. if pageSize <= 0 {
  226. pageSize = utils.PageSize20
  227. }
  228. if currentIndex <= 0 {
  229. currentIndex = 1
  230. }
  231. startSize = paging.StartIndex(currentIndex, pageSize)
  232. // 获取查询参数
  233. keyword, searchChartPublicList, sourceList, chartTypeList, chartClassifyIdList, publicClassifyIdList, chartAuth, sortMap := data.GetChartSearchPar(req)
  234. total, chartInfoList, err := elastic.SearchChartInfoDataByPublic(keyword, startSize, pageSize, searchChartPublicList, sourceList, chartTypeList, chartClassifyIdList, publicClassifyIdList, chartAuth, c.SysUser.AdminId, sortMap)
  235. if err != nil {
  236. chartInfoList = make([]*data_manage.ChartInfoView, 0)
  237. }
  238. page := paging.GetPaging(currentIndex, pageSize, int(total))
  239. // 因为是ES查找的,所以需要重新查一下图表的信息,主要是为了把是否授权字段找出来
  240. if len(chartInfoList) > 0 {
  241. // 图表map信息
  242. chartInfoMap := make(map[int]*data_manage.ChartInfo)
  243. classifyMap := make(map[int]*data_manage.ChartClassify)
  244. publicClassifyMap := make(map[int]*data_manage.ChartPublicClassify)
  245. // 指标id,分类id,公共分类id
  246. chartInfoIdList := make([]int, 0)
  247. tmpClassifyIdList := make([]int, 0)
  248. tmpPublicClassifyIdList := make([]int, 0)
  249. for _, v := range chartInfoList {
  250. v.ConvertToResp()
  251. v.HaveOperaAuth = true
  252. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  253. tmpClassifyIdList = append(tmpClassifyIdList, v.ChartClassifyId)
  254. tmpPublicClassifyIdList = append(tmpPublicClassifyIdList, v.ChartPublicClassifyId)
  255. }
  256. // 获取所有有权限的图表
  257. {
  258. tmpChartList, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  259. if err != nil {
  260. br.Msg = "获取失败"
  261. br.ErrMsg = "获取所有有权限的图表失败,Err:" + err.Error()
  262. return
  263. }
  264. for _, v := range tmpChartList {
  265. chartInfoMap[v.ChartInfoId] = v
  266. }
  267. }
  268. // 图表分类
  269. {
  270. tmpList, err := data_manage.GetChartClassifyByIdList(tmpClassifyIdList)
  271. if err != nil {
  272. br.Msg = "获取失败"
  273. br.ErrMsg = "获取所有有权限的图表分类失败,Err:" + err.Error()
  274. return
  275. }
  276. for _, v := range tmpList {
  277. classifyMap[v.ChartClassifyId] = v
  278. }
  279. }
  280. // 公共分类
  281. {
  282. obj := data_manage.ChartPublicClassify{}
  283. tmpList, err := obj.GetChartClassifyByIdList(tmpPublicClassifyIdList)
  284. if err != nil {
  285. br.Msg = "获取失败"
  286. br.ErrMsg = "获取所有有权限的公共分类失败,Err:" + err.Error()
  287. return
  288. }
  289. for _, v := range tmpList {
  290. publicClassifyMap[v.ChartPublicClassifyId] = v
  291. }
  292. }
  293. // 数据重新赋值
  294. for _, v := range chartInfoList {
  295. if tmpChartInfo, ok := chartInfoMap[v.ChartInfoId]; ok {
  296. v.IsJoinPermission = tmpChartInfo.IsJoinPermission
  297. }
  298. // 普通分类
  299. if classifyItem, ok := classifyMap[v.ChartClassifyId]; ok {
  300. v.ClassifyNamePath = classifyItem.ChartClassifyNamePath
  301. }
  302. // 公共分类
  303. if classifyItem, ok := publicClassifyMap[v.ChartPublicClassifyId]; ok {
  304. v.PublicClassifyNamePath = classifyItem.ChartPublicClassifyNamePath
  305. }
  306. }
  307. }
  308. resp := data_manage.ChartInfoFilterDataResp{
  309. Paging: page,
  310. List: chartInfoList,
  311. }
  312. br.Ret = 200
  313. br.Success = true
  314. br.Msg = "获取成功"
  315. br.Data = resp
  316. }
  317. // AllListByEs
  318. // @Title 图表筛选接口
  319. // @Description 图表筛选接口
  320. // @Success 200 {object} data_manage.ChartInfoList
  321. // @Param request body request.SearchChartInfoShareReq true "type json string"
  322. // @Success 200 {object} data_manage.ChartInfoFilterDataResp
  323. // @router /chart_info/public/list/es/all [post]
  324. func (c *ChartPublicController) AllListByEs() {
  325. br := new(models.BaseResponse).Init()
  326. defer func() {
  327. c.Data["json"] = br
  328. c.ServeJSON()
  329. }()
  330. var req request.AllSearchPublicChartReq
  331. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  332. if err != nil {
  333. br.Msg = "参数解析异常!"
  334. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  335. return
  336. }
  337. // 选择所有图表,所以需要通过es获取数据
  338. // 选中的图表id列表
  339. chartInfoList := make([]*data_manage.ChartInfoView, 0)
  340. // 选择所有图表,所以需要通过es获取数据
  341. if req.IsSelectAll {
  342. tmpChartInfoList, err := data.GetAllChartInfoListBySearchPublicChartReq(req.SearchPublicChartReq, c.SysUser.AdminId)
  343. if err != nil {
  344. br.Msg = "获取图表列表失败!"
  345. br.ErrMsg = "获取图表列表失败,Err:" + err.Error()
  346. return
  347. }
  348. // 如果有过滤图表,那么就过滤吧
  349. if len(req.NoChartIdList) > 0 {
  350. noChartIdMap := make(map[int]bool)
  351. for _, v := range req.NoChartIdList {
  352. noChartIdMap[v] = true
  353. }
  354. for _, v := range tmpChartInfoList {
  355. if _, ok := noChartIdMap[v.ChartInfoId]; !ok {
  356. // 如果不在未选中的图表id列表中,那么就加入到选中的图表id列表
  357. chartInfoList = append(chartInfoList, v)
  358. }
  359. }
  360. } else {
  361. chartInfoList = tmpChartInfoList
  362. }
  363. // 因为是ES查找的,所以需要重新查一下图表的信息,主要是为了把是否授权字段找出来
  364. if len(chartInfoList) > 0 {
  365. chartInfoIdList := make([]int, 0)
  366. for _, v := range chartInfoList {
  367. v.ConvertToResp()
  368. v.HaveOperaAuth = true
  369. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  370. }
  371. tmpChartList, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  372. if err != nil {
  373. br.Msg = "获取失败"
  374. br.ErrMsg = "获取所有有权限的图表失败,Err:" + err.Error()
  375. return
  376. }
  377. chartInfoMap := make(map[int]*data_manage.ChartInfo)
  378. for _, v := range tmpChartList {
  379. chartInfoMap[v.ChartInfoId] = v
  380. }
  381. for _, v := range chartInfoList {
  382. tmpChartInfo, ok := chartInfoMap[v.ChartInfoId]
  383. if !ok {
  384. continue
  385. }
  386. v.IsJoinPermission = tmpChartInfo.IsJoinPermission
  387. }
  388. }
  389. } else {
  390. // 因为是ES查找的,所以需要重新查一下图表的信息,主要是为了把是否授权字段找出来
  391. if len(req.ChartIdList) > 0 {
  392. chartInfoList, err = data_manage.GetChartInfoViewByIdList(req.ChartIdList)
  393. if err != nil {
  394. br.Msg = "获取失败"
  395. br.ErrMsg = "获取所有有权限的图表失败,Err:" + err.Error()
  396. return
  397. }
  398. }
  399. }
  400. resp := data_manage.ChartInfoFilterDataResp{
  401. List: chartInfoList,
  402. }
  403. br.Ret = 200
  404. br.Success = true
  405. br.Msg = "获取成功"
  406. br.Data = resp
  407. }
  408. // BatchMove
  409. // @Title 图表批量移动接口
  410. // @Description 图表批量移动接口
  411. // @Param request body request.MoveChartClassifyReq true "type json string"
  412. // @Success Ret=200 移动成功
  413. // @router /chart_info/public/batch_move [post]
  414. func (c *ChartPublicController) BatchMove() {
  415. br := new(models.BaseResponse).Init()
  416. defer func() {
  417. c.Data["json"] = br
  418. c.ServeJSON()
  419. }()
  420. var req request.MoveChartClassifyReq
  421. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  422. if err != nil {
  423. br.Msg = "参数解析异常!"
  424. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  425. return
  426. }
  427. if req.NewClassifyId <= 0 {
  428. br.Msg = `请选择要转移的新分类`
  429. br.IsSendEmail = false
  430. return
  431. }
  432. // 校验分类
  433. obj := data_manage.ChartPublicClassify{}
  434. publicClassifyItem, err := obj.GetChartClassifyById(req.NewClassifyId)
  435. if err != nil {
  436. br.Msg = "获取公共分类失败!"
  437. br.ErrMsg = "获取公共分类失败!" + err.Error()
  438. return
  439. }
  440. if publicClassifyItem.ChartPublicClassifyId <= 0 {
  441. br.Msg = "公共分类不存在!"
  442. br.ErrMsg = "公共分类不存在!"
  443. return
  444. }
  445. // 只是取公开
  446. req.SearchPublicChartReq.ChartPublicList = []int{3}
  447. chartInfoIdList := make([]int, 0)
  448. if req.IsSelectAll {
  449. // 获取图表
  450. chartInfoList, err := data.GetAllChartInfoListBySearchPublicChartReq(req.SearchPublicChartReq, c.SysUser.AdminId)
  451. if err != nil {
  452. br.Msg = "获取图表列表失败!"
  453. br.ErrMsg = "获取图表列表失败,Err:" + err.Error()
  454. return
  455. }
  456. // 如果有过滤图表,那么就过滤吧
  457. if len(req.NoChartIdList) > 0 {
  458. noChartIdMap := make(map[int]bool)
  459. for _, v := range req.NoChartIdList {
  460. noChartIdMap[v] = true
  461. }
  462. for _, chartInfo := range chartInfoList {
  463. if _, ok := noChartIdMap[chartInfo.ChartInfoId]; !ok {
  464. if chartInfo.PublicStatus != utils.DataPublicSuccess {
  465. br.Msg = `存在未公开的图表:` + chartInfo.ChartName
  466. br.IsSendEmail = false
  467. return
  468. }
  469. // 如果不在未选中的图表id列表中,那么就加入到选中的图表id列表
  470. chartInfoIdList = append(chartInfoIdList, chartInfo.ChartInfoId)
  471. }
  472. }
  473. } else {
  474. for _, chartInfo := range chartInfoList {
  475. if chartInfo.PublicStatus != utils.DataPublicSuccess {
  476. br.Msg = `存在未公开的图表:` + chartInfo.ChartName
  477. br.IsSendEmail = false
  478. return
  479. }
  480. chartInfoIdList = append(chartInfoIdList, chartInfo.ChartInfoId)
  481. }
  482. }
  483. } else {
  484. chartInfoList, err := data_manage.GetChartInfoViewByIdList(req.ChartIdList)
  485. if err != nil {
  486. br.Msg = "获取失败"
  487. br.ErrMsg = "获取所有有权限的图表失败,Err:" + err.Error()
  488. return
  489. }
  490. for _, v := range chartInfoList {
  491. if v.PublicStatus != utils.DataPublicSuccess {
  492. br.Msg = `存在未公开的图表:` + v.ChartName
  493. br.IsSendEmail = false
  494. return
  495. }
  496. }
  497. chartInfoIdList = req.ChartIdList
  498. }
  499. num := len(chartInfoIdList)
  500. if num <= 0 {
  501. br.Msg = `请选择要移动的图表!`
  502. br.IsSendEmail = false
  503. return
  504. }
  505. if num > 100 {
  506. br.Msg = `最多只能选择100条图表!`
  507. br.IsSendEmail = false
  508. return
  509. }
  510. // 开始批量修改图表分类
  511. if len(chartInfoIdList) > 0 {
  512. err = data_manage.UpdatePublicClassifyIdByChartInfoIdList(chartInfoIdList, req.NewClassifyId)
  513. if err != nil {
  514. br.Msg = `图表移动失败!`
  515. br.ErrMsg = `图表移动失败,ERR:` + err.Error()
  516. return
  517. }
  518. }
  519. br.Ret = 200
  520. br.Success = true
  521. br.Msg = "获取成功"
  522. }