classify.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package cross_variety
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/services/data"
  8. "eta/eta_api/utils"
  9. "time"
  10. )
  11. // ClassifyController
  12. // @Description: 跨品种分析分类
  13. type ClassifyController struct {
  14. controllers.BaseAuthController
  15. }
  16. // List
  17. // @Title 跨品种分析分类列表
  18. // @Description 跨品种分析分类列表接口
  19. // @Param IsShowMe query bool true "是否只看我的,true、false"
  20. // @Param Source query int true "图表类型,3:跨品种分析,4:滚动跨品种分析"
  21. // @Success 200 {object} data_manage.ChartClassifyListResp
  22. // @router /classify/list [get]
  23. func (c *ClassifyController) List() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. c.Data["json"] = br
  27. c.ServeJSON()
  28. }()
  29. resp := new(data_manage.ChartClassifyListResp)
  30. // 获取当前账号的不可见指标
  31. noPermissionChartIdMap := make(map[int]bool)
  32. {
  33. obj := data_manage.EdbInfoNoPermissionAdmin{}
  34. confList, err := obj.GetAllChartListByAdminId(c.SysUser.AdminId)
  35. if err != nil && err.Error() != utils.ErrNoRow() {
  36. br.Msg = "获取失败"
  37. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  38. return
  39. }
  40. for _, v := range confList {
  41. noPermissionChartIdMap[v.ChartInfoId] = true
  42. }
  43. }
  44. isShowMe, _ := c.GetBool("IsShowMe")
  45. source, _ := c.GetInt("Source")
  46. if source <= 0 {
  47. source = utils.CHART_SOURCE_CROSS_HEDGING
  48. }
  49. rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_CROSS_HEDGING)
  50. if err != nil && err.Error() != utils.ErrNoRow() {
  51. br.Msg = "获取失败"
  52. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  53. return
  54. }
  55. allChartInfo, err := data_manage.GetChartInfoAll([]int{source})
  56. if err != nil && err.Error() != utils.ErrNoRow() {
  57. br.Msg = "获取失败"
  58. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  59. return
  60. }
  61. chartInfoMap := make(map[int][]*data_manage.ChartClassifyItems)
  62. for _, v := range allChartInfo {
  63. if !isShowMe {
  64. chartInfoMap[v.ChartClassifyId] = append(chartInfoMap[v.ChartClassifyId], v)
  65. continue
  66. }
  67. if v.SysUserId != c.SysUser.AdminId {
  68. continue
  69. }
  70. chartInfoMap[v.ChartClassifyId] = append(chartInfoMap[v.ChartClassifyId], v)
  71. }
  72. rootChildMap := make(map[int][]*data_manage.ChartClassifyItems)
  73. // 移除没有图表的分类
  74. allNodes := make([]*data_manage.ChartClassifyItems, 0)
  75. for _, v := range rootList {
  76. if v.SysUserId == c.SysUser.AdminId || c.SysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  77. v.Button.OpButton = true
  78. }
  79. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  80. if existItems, ok := chartInfoMap[v.ChartClassifyId]; ok {
  81. v.Children = existItems
  82. allNodes = append(allNodes, v)
  83. }
  84. }
  85. // 移除没有权限的图表
  86. allNodes = data.HandleNoPermissionChart(allNodes, noPermissionChartIdMap, c.SysUser.AdminId)
  87. resp.AllNodes = allNodes
  88. br.Ret = 200
  89. br.Success = true
  90. br.Msg = "获取成功"
  91. br.Data = resp
  92. }
  93. // EditChartClassify
  94. // @Title 修改跨品种分析图表分类
  95. // @Description 修改跨品种分析图表分类接口
  96. // @Param request body data_manage.EditChartClassifyReq true "type json string"
  97. // @Success 200 Ret=200 修改成功
  98. // @router /classify/edit [post]
  99. func (c *ClassifyController) EditChartClassify() {
  100. br := new(models.BaseResponse).Init()
  101. defer func() {
  102. c.Data["json"] = br
  103. c.ServeJSON()
  104. }()
  105. var req data_manage.EditChartClassifyReq
  106. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  107. if err != nil {
  108. br.Msg = "参数解析异常!"
  109. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  110. return
  111. }
  112. if req.ChartClassifyName == "" {
  113. br.Msg = "请输入分类名称"
  114. br.IsSendEmail = false
  115. return
  116. }
  117. if req.ChartClassifyId <= 0 {
  118. br.Msg = "参数错误"
  119. br.IsSendEmail = false
  120. return
  121. }
  122. // 编辑图表分类
  123. _, err, errMsg, isSendEmail := data.EditChartClassify(req.ChartClassifyId, utils.CHART_SOURCE_CROSS_HEDGING, req.ChartClassifyName, c.Lang, c.SysUser)
  124. if err != nil {
  125. br.Msg = errMsg
  126. br.ErrMsg = "保存分类失败,Err:" + err.Error()
  127. br.IsSendEmail = isSendEmail
  128. return
  129. }
  130. br.Ret = 200
  131. br.Msg = "修改成功"
  132. br.Success = true
  133. br.IsAddLog = true
  134. }
  135. // DeleteChartClassifyCheck
  136. // @Title 删除图表检测接口
  137. // @Description 删除图表检测接口
  138. // @Param request body data_manage.ChartClassifyDeleteCheckResp true "type json string"
  139. // @Success 200 Ret=200 检测成功
  140. // @router /classify/delete/check [post]
  141. func (c *ClassifyController) DeleteChartClassifyCheck() {
  142. br := new(models.BaseResponse).Init()
  143. defer func() {
  144. c.Data["json"] = br
  145. c.ServeJSON()
  146. }()
  147. var req data_manage.ChartClassifyDeleteCheckReq
  148. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  149. if err != nil {
  150. br.Msg = "参数解析异常!"
  151. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  152. return
  153. }
  154. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  155. br.Msg = "参数错误"
  156. br.IsSendEmail = false
  157. return
  158. }
  159. var deleteStatus int
  160. var tipsMsg string
  161. //删除分类
  162. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  163. //判断跨品种分析图表分类下,是否含有图表
  164. count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  165. if err != nil {
  166. br.Msg = "删除失败"
  167. br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  168. return
  169. }
  170. if count > 0 {
  171. deleteStatus = 1
  172. tipsMsg = "该分类下关联图表不可删除"
  173. }
  174. }
  175. if deleteStatus != 1 && req.ChartInfoId == 0 {
  176. classifyCount, err := data_manage.GetChartClassifyCountByClassifyId(req.ChartClassifyId)
  177. if err != nil && err.Error() != utils.ErrNoRow() {
  178. br.Msg = "删除失败"
  179. br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  180. return
  181. }
  182. if classifyCount > 0 {
  183. deleteStatus = 2
  184. tipsMsg = "确认删除当前目录及包含的子目录吗"
  185. }
  186. }
  187. if deleteStatus == 0 {
  188. tipsMsg = "可删除,进行删除操作"
  189. }
  190. resp := new(data_manage.ChartClassifyDeleteCheckResp)
  191. resp.DeleteStatus = deleteStatus
  192. resp.TipsMsg = tipsMsg
  193. br.Ret = 200
  194. br.Msg = "检测成功"
  195. br.Success = true
  196. br.Data = resp
  197. }
  198. // DeleteChartClassify
  199. // @Title 删除跨品种分析图表分类/图表
  200. // @Description 删除跨品种分析图表分类/图表接口
  201. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  202. // @Success 200 Ret=200 删除成功
  203. // @router /classify/delete [post]
  204. func (c *ClassifyController) DeleteChartClassify() {
  205. br := new(models.BaseResponse).Init()
  206. defer func() {
  207. c.Data["json"] = br
  208. c.ServeJSON()
  209. }()
  210. sysUser := c.SysUser
  211. if sysUser == nil {
  212. br.Msg = "请登录"
  213. br.ErrMsg = "请登录,SysUser Is Empty"
  214. br.Ret = 408
  215. return
  216. }
  217. var req data_manage.DeleteChartClassifyReq
  218. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  219. if err != nil {
  220. br.Msg = "参数解析异常!"
  221. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  222. return
  223. }
  224. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  225. br.Msg = "参数错误"
  226. br.IsSendEmail = false
  227. return
  228. }
  229. //删除分类
  230. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  231. //判断是否含有指标
  232. count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  233. if err != nil && err.Error() != utils.ErrNoRow() {
  234. br.Msg = "删除失败"
  235. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  236. return
  237. }
  238. if count > 0 {
  239. br.Msg = "该目录下存在关联指标,不可删除"
  240. br.IsSendEmail = false
  241. return
  242. }
  243. err = data_manage.DeleteChartClassify(req.ChartClassifyId)
  244. if err != nil {
  245. br.Msg = "删除失败"
  246. br.ErrMsg = "删除失败,Err:" + err.Error()
  247. return
  248. }
  249. }
  250. resp := new(data_manage.AddChartInfoResp)
  251. //删除图表
  252. if req.ChartInfoId > 0 {
  253. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  254. if err != nil {
  255. if err.Error() == utils.ErrNoRow() {
  256. br.Msg = "图表已删除,请刷新页面"
  257. br.ErrMsg = "指标不存在,Err:" + err.Error()
  258. return
  259. } else {
  260. br.Msg = "删除失败"
  261. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  262. return
  263. }
  264. }
  265. if chartInfo == nil {
  266. br.Msg = "图表已删除,请刷新页面"
  267. return
  268. }
  269. //图表操作权限
  270. ok := data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  271. if !ok {
  272. br.Msg = "没有该图表的操作权限"
  273. br.ErrMsg = "没有该图表的操作权限"
  274. return
  275. }
  276. // 获取引用该图表的MyCharts, 用于ES删除
  277. var myCond string
  278. var myPars []interface{}
  279. myCond += ` AND a.chart_info_id = ? `
  280. myPars = append(myPars, chartInfo.ChartInfoId)
  281. myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
  282. if e != nil {
  283. br.Msg = "删除失败"
  284. br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
  285. return
  286. }
  287. myIds := make([]int, 0)
  288. for _, m := range myCharts {
  289. myIds = append(myIds, m.MyChartId)
  290. }
  291. source := chartInfo.Source // 跨品种分析图表(滚动跨品种分析)
  292. //删除图表及关联指标
  293. err = data_manage.DeleteChartInfoAndData(chartInfo.ChartInfoId)
  294. if err != nil {
  295. br.Msg = "删除失败"
  296. br.ErrMsg = "删除失败,Err:" + err.Error()
  297. return
  298. }
  299. //删除ES
  300. {
  301. go data.EsDeleteChartInfo(chartInfo.ChartInfoId)
  302. // 删除MY ETA 图表 es数据
  303. //go data.EsDeleteMyChartInfoByChartInfoId(chartInfo.ChartInfoId)
  304. go data.EsDeleteMyChartInfoByMyChartIds(myIds)
  305. }
  306. var condition string
  307. var pars []interface{}
  308. condition += " AND chart_classify_id=? AND source = ? "
  309. pars = append(pars, chartInfo.ChartClassifyId, source)
  310. condition += " AND chart_info_id>? ORDER BY create_time ASC LIMIT 1 "
  311. pars = append(pars, req.ChartInfoId)
  312. nextItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  313. if err != nil && err.Error() != utils.ErrNoRow() {
  314. br.Msg = "删除失败"
  315. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  316. return
  317. }
  318. if nextItem != nil {
  319. resp.UniqueCode = nextItem.UniqueCode
  320. resp.ChartInfoId = nextItem.ChartInfoId
  321. } else {
  322. var condition string
  323. var pars []interface{}
  324. condition += " AND level=1 "
  325. //pars = append(pars, chartInfo.ChartClassifyId)
  326. condition += " AND chart_classify_id>? ORDER BY chart_classify_id ASC LIMIT 1 "
  327. pars = append(pars, chartInfo.ChartClassifyId)
  328. classifyItem, err := data_manage.GetChartClassifyByCondition(condition, pars)
  329. if err != nil && err.Error() != utils.ErrNoRow() {
  330. br.Msg = "删除失败"
  331. br.ErrMsg = "获取下一级图库分类信息失败,Err:" + err.Error()
  332. return
  333. }
  334. if classifyItem != nil {
  335. nextItem, err = data_manage.GetNextChartInfo(chartInfo.ChartClassifyId)
  336. if err != nil && err.Error() != utils.ErrNoRow() {
  337. br.Msg = "删除失败"
  338. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  339. return
  340. }
  341. if nextItem != nil {
  342. resp.UniqueCode = nextItem.UniqueCode
  343. resp.ChartInfoId = nextItem.ChartInfoId
  344. }
  345. }
  346. }
  347. //新增操作日志
  348. {
  349. chartLog := new(data_manage.ChartInfoLog)
  350. chartLog.ChartName = chartInfo.ChartName
  351. chartLog.ChartInfoId = req.ChartInfoId
  352. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  353. chartLog.SysUserId = sysUser.AdminId
  354. chartLog.SysUserRealName = sysUser.RealName
  355. chartLog.UniqueCode = chartInfo.UniqueCode
  356. chartLog.CreateTime = time.Now()
  357. chartLog.Content = string(c.Ctx.Input.RequestBody)
  358. chartLog.Status = "删除图表"
  359. chartLog.Method = c.Ctx.Input.URI()
  360. go data_manage.AddChartInfoLog(chartLog)
  361. }
  362. }
  363. br.Ret = 200
  364. br.Msg = "删除成功"
  365. br.Success = true
  366. br.Data = resp
  367. br.IsAddLog = true
  368. }
  369. // ChartClassifyMove
  370. // @Title 相关性图表分类移动接口
  371. // @Description 相关性图表分类移动接口
  372. // @Success 200 {object} data_manage.MoveChartClassifyReq
  373. // @router /classify/move [post]
  374. func (c *ClassifyController) ChartClassifyMove() {
  375. br := new(models.BaseResponse).Init()
  376. defer func() {
  377. c.Data["json"] = br
  378. c.ServeJSON()
  379. }()
  380. sysUser := c.SysUser
  381. if sysUser == nil {
  382. br.Msg = "请登录"
  383. br.ErrMsg = "请登录,SysUser Is Empty"
  384. br.Ret = 408
  385. return
  386. }
  387. var req data_manage.MoveChartClassifyReq
  388. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  389. if err != nil {
  390. br.Msg = "参数解析异常!"
  391. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  392. return
  393. }
  394. if req.ClassifyId <= 0 {
  395. br.Msg = "参数错误"
  396. br.ErrMsg = "分类id小于等于0"
  397. return
  398. }
  399. //判断分类是否存在
  400. chartClassifyInfo, err := data_manage.GetChartClassifyById(req.ClassifyId)
  401. if err != nil {
  402. br.Msg = "移动失败"
  403. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  404. return
  405. }
  406. if chartClassifyInfo.Source != utils.CHART_SOURCE_CROSS_HEDGING {
  407. br.Msg = "分类异常"
  408. br.ErrMsg = "分类异常,不是跨品种分析图表的分类"
  409. return
  410. }
  411. updateCol := make([]string, 0)
  412. //如果有传入 上一个兄弟节点分类id
  413. if req.PrevClassifyId > 0 {
  414. //上一个兄弟节点
  415. prevClassify, err := data_manage.GetChartClassifyById(req.PrevClassifyId)
  416. if err != nil {
  417. br.Msg = "移动失败"
  418. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  419. return
  420. }
  421. if prevClassify.Source != utils.CHART_SOURCE_CROSS_HEDGING {
  422. br.Msg = "上级节点分类异常"
  423. br.ErrMsg = "上级节点分类异常,不是跨品种分析图表的分类"
  424. return
  425. }
  426. //如果是移动在两个兄弟节点之间
  427. if req.NextClassifyId > 0 {
  428. //下一个兄弟节点
  429. nextClassify, err := data_manage.GetChartClassifyById(req.NextClassifyId)
  430. if err != nil {
  431. br.Msg = "移动失败"
  432. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  433. return
  434. }
  435. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  436. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == chartClassifyInfo.Sort {
  437. //变更兄弟节点的排序
  438. updateSortStr := `sort + 2`
  439. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr)
  440. } else {
  441. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  442. if nextClassify.Sort-prevClassify.Sort == 1 {
  443. //变更兄弟节点的排序
  444. updateSortStr := `sort + 1`
  445. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  446. }
  447. }
  448. }
  449. chartClassifyInfo.Sort = prevClassify.Sort + 1
  450. chartClassifyInfo.ModifyTime = time.Now()
  451. updateCol = append(updateCol, "Sort", "ModifyTime")
  452. } else {
  453. firstClassify, err := data_manage.GetFirstChartClassifyByParentIdAndSource(chartClassifyInfo.ParentId, chartClassifyInfo.Source)
  454. if err != nil && err.Error() != utils.ErrNoRow() {
  455. br.Msg = "移动失败"
  456. br.ErrMsg = "获取跨品种分析图表分类排序第一条的分类信息失败,Err:" + err.Error()
  457. return
  458. }
  459. if firstClassify != nil && firstClassify.Source != utils.CHART_SOURCE_CROSS_HEDGING {
  460. br.Msg = "上级节点分类异常"
  461. br.ErrMsg = "上级节点分类异常,不是跨品种分析图表的分类"
  462. return
  463. }
  464. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  465. if firstClassify != nil && firstClassify.Sort == 0 {
  466. updateSortStr := ` sort + 1 `
  467. _ = data_manage.UpdateChartClassifySortByParentId(firstClassify.ParentId, firstClassify.ChartClassifyId-1, 0, updateSortStr)
  468. }
  469. chartClassifyInfo.Sort = 0 //那就是排在第一位
  470. chartClassifyInfo.ModifyTime = time.Now()
  471. updateCol = append(updateCol, "Sort", "ModifyTime")
  472. }
  473. //更新
  474. if len(updateCol) > 0 {
  475. err = chartClassifyInfo.Update(updateCol)
  476. if err != nil {
  477. br.Msg = "移动失败"
  478. br.ErrMsg = "修改失败,Err:" + err.Error()
  479. return
  480. }
  481. }
  482. br.Ret = 200
  483. br.Success = true
  484. br.Msg = "移动成功"
  485. }