classify.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. package cross_variety
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/controllers"
  5. "eta/eta_mobile/models"
  6. "eta/eta_mobile/models/data_manage"
  7. "eta/eta_mobile/services/data"
  8. "eta/eta_mobile/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. item, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  123. if err != nil {
  124. br.Msg = "保存失败"
  125. br.Msg = "获取分类信息失败,Err:" + err.Error()
  126. return
  127. }
  128. source := utils.CHART_SOURCE_CROSS_HEDGING
  129. if item.ChartClassifyName != req.ChartClassifyName {
  130. //count, err := data_manage.GetChartClassifyCount(req.ChartClassifyName, item.ParentId, source)
  131. //if err != nil {
  132. // br.Msg = "判断名称是否已存在失败"
  133. // br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  134. // return
  135. //}
  136. //if count > 0 {
  137. // br.Msg = "分类名称已存在,请重新输入"
  138. // br.IsSendEmail = false
  139. // return
  140. //}
  141. err = data_manage.EditChartClassify(req.ChartClassifyId, source, req.ChartClassifyName)
  142. if err != nil {
  143. br.Msg = "保存失败"
  144. br.ErrMsg = "保存失败,Err:" + err.Error()
  145. return
  146. }
  147. }
  148. br.Ret = 200
  149. br.Msg = "修改成功"
  150. br.Success = true
  151. br.IsAddLog = true
  152. }
  153. // DeleteChartClassifyCheck
  154. // @Title 删除图表检测接口
  155. // @Description 删除图表检测接口
  156. // @Param request body data_manage.ChartClassifyDeleteCheckResp true "type json string"
  157. // @Success 200 Ret=200 检测成功
  158. // @router /classify/delete/check [post]
  159. func (c *ClassifyController) DeleteChartClassifyCheck() {
  160. br := new(models.BaseResponse).Init()
  161. defer func() {
  162. c.Data["json"] = br
  163. c.ServeJSON()
  164. }()
  165. var req data_manage.ChartClassifyDeleteCheckReq
  166. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  167. if err != nil {
  168. br.Msg = "参数解析异常!"
  169. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  170. return
  171. }
  172. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  173. br.Msg = "参数错误"
  174. br.IsSendEmail = false
  175. return
  176. }
  177. var deleteStatus int
  178. var tipsMsg string
  179. //删除分类
  180. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  181. //判断跨品种分析图表分类下,是否含有图表
  182. count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  183. if err != nil {
  184. br.Msg = "删除失败"
  185. br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  186. return
  187. }
  188. if count > 0 {
  189. deleteStatus = 1
  190. tipsMsg = "该分类下关联图表不可删除"
  191. }
  192. }
  193. if deleteStatus != 1 && req.ChartInfoId == 0 {
  194. classifyCount, err := data_manage.GetChartClassifyCountByClassifyId(req.ChartClassifyId)
  195. if err != nil && err.Error() != utils.ErrNoRow() {
  196. br.Msg = "删除失败"
  197. br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  198. return
  199. }
  200. if classifyCount > 0 {
  201. deleteStatus = 2
  202. tipsMsg = "确认删除当前目录及包含的子目录吗"
  203. }
  204. }
  205. if deleteStatus == 0 {
  206. tipsMsg = "可删除,进行删除操作"
  207. }
  208. resp := new(data_manage.ChartClassifyDeleteCheckResp)
  209. resp.DeleteStatus = deleteStatus
  210. resp.TipsMsg = tipsMsg
  211. br.Ret = 200
  212. br.Msg = "检测成功"
  213. br.Success = true
  214. br.Data = resp
  215. }
  216. // DeleteChartClassify
  217. // @Title 删除跨品种分析图表分类/图表
  218. // @Description 删除跨品种分析图表分类/图表接口
  219. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  220. // @Success 200 Ret=200 删除成功
  221. // @router /classify/delete [post]
  222. func (c *ClassifyController) DeleteChartClassify() {
  223. br := new(models.BaseResponse).Init()
  224. defer func() {
  225. c.Data["json"] = br
  226. c.ServeJSON()
  227. }()
  228. sysUser := c.SysUser
  229. if sysUser == nil {
  230. br.Msg = "请登录"
  231. br.ErrMsg = "请登录,SysUser Is Empty"
  232. br.Ret = 408
  233. return
  234. }
  235. var req data_manage.DeleteChartClassifyReq
  236. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  237. if err != nil {
  238. br.Msg = "参数解析异常!"
  239. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  240. return
  241. }
  242. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  243. br.Msg = "参数错误"
  244. br.IsSendEmail = false
  245. return
  246. }
  247. //删除分类
  248. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  249. //判断是否含有指标
  250. count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  251. if err != nil && err.Error() != utils.ErrNoRow() {
  252. br.Msg = "删除失败"
  253. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  254. return
  255. }
  256. if count > 0 {
  257. br.Msg = "该目录下存在关联指标,不可删除"
  258. br.IsSendEmail = false
  259. return
  260. }
  261. err = data_manage.DeleteChartClassify(req.ChartClassifyId)
  262. if err != nil {
  263. br.Msg = "删除失败"
  264. br.ErrMsg = "删除失败,Err:" + err.Error()
  265. return
  266. }
  267. }
  268. resp := new(data_manage.AddChartInfoResp)
  269. //删除图表
  270. if req.ChartInfoId > 0 {
  271. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  272. if err != nil {
  273. if err.Error() == utils.ErrNoRow() {
  274. br.Msg = "图表已删除,请刷新页面"
  275. br.ErrMsg = "指标不存在,Err:" + err.Error()
  276. return
  277. } else {
  278. br.Msg = "删除失败"
  279. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  280. return
  281. }
  282. }
  283. if chartInfo == nil {
  284. br.Msg = "图表已删除,请刷新页面"
  285. return
  286. }
  287. //图表操作权限
  288. ok := data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  289. if !ok {
  290. br.Msg = "没有该图表的操作权限"
  291. br.ErrMsg = "没有该图表的操作权限"
  292. return
  293. }
  294. // 获取引用该图表的MyCharts, 用于ES删除
  295. var myCond string
  296. var myPars []interface{}
  297. myCond += ` AND a.chart_info_id = ? `
  298. myPars = append(myPars, chartInfo.ChartInfoId)
  299. myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
  300. if e != nil {
  301. br.Msg = "删除失败"
  302. br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
  303. return
  304. }
  305. myIds := make([]int, 0)
  306. for _, m := range myCharts {
  307. myIds = append(myIds, m.MyChartId)
  308. }
  309. source := chartInfo.Source // 跨品种分析图表(滚动跨品种分析)
  310. //删除图表及关联指标
  311. err = data_manage.DeleteChartInfoAndData(chartInfo.ChartInfoId)
  312. if err != nil {
  313. br.Msg = "删除失败"
  314. br.ErrMsg = "删除失败,Err:" + err.Error()
  315. return
  316. }
  317. //删除ES
  318. {
  319. go data.EsDeleteChartInfo(chartInfo.ChartInfoId)
  320. // 删除MY ETA 图表 es数据
  321. //go data.EsDeleteMyChartInfoByChartInfoId(chartInfo.ChartInfoId)
  322. go data.EsDeleteMyChartInfoByMyChartIds(myIds)
  323. }
  324. var condition string
  325. var pars []interface{}
  326. condition += " AND chart_classify_id=? AND source = ? "
  327. pars = append(pars, chartInfo.ChartClassifyId, source)
  328. condition += " AND chart_info_id>? ORDER BY create_time ASC LIMIT 1 "
  329. pars = append(pars, req.ChartInfoId)
  330. nextItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  331. if err != nil && err.Error() != utils.ErrNoRow() {
  332. br.Msg = "删除失败"
  333. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  334. return
  335. }
  336. if nextItem != nil {
  337. resp.UniqueCode = nextItem.UniqueCode
  338. resp.ChartInfoId = nextItem.ChartInfoId
  339. } else {
  340. var condition string
  341. var pars []interface{}
  342. condition += " AND level=1 "
  343. //pars = append(pars, chartInfo.ChartClassifyId)
  344. condition += " AND chart_classify_id>? ORDER BY chart_classify_id ASC LIMIT 1 "
  345. pars = append(pars, chartInfo.ChartClassifyId)
  346. classifyItem, err := data_manage.GetChartClassifyByCondition(condition, pars)
  347. if err != nil && err.Error() != utils.ErrNoRow() {
  348. br.Msg = "删除失败"
  349. br.ErrMsg = "获取下一级图库分类信息失败,Err:" + err.Error()
  350. return
  351. }
  352. if classifyItem != nil {
  353. nextItem, err = data_manage.GetNextChartInfo(chartInfo.ChartClassifyId)
  354. if err != nil && err.Error() != utils.ErrNoRow() {
  355. br.Msg = "删除失败"
  356. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  357. return
  358. }
  359. if nextItem != nil {
  360. resp.UniqueCode = nextItem.UniqueCode
  361. resp.ChartInfoId = nextItem.ChartInfoId
  362. }
  363. }
  364. }
  365. //新增操作日志
  366. {
  367. chartLog := new(data_manage.ChartInfoLog)
  368. chartLog.ChartName = chartInfo.ChartName
  369. chartLog.ChartInfoId = req.ChartInfoId
  370. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  371. chartLog.SysUserId = sysUser.AdminId
  372. chartLog.SysUserRealName = sysUser.RealName
  373. chartLog.UniqueCode = chartInfo.UniqueCode
  374. chartLog.CreateTime = time.Now()
  375. chartLog.Content = string(c.Ctx.Input.RequestBody)
  376. chartLog.Status = "删除图表"
  377. chartLog.Method = c.Ctx.Input.URI()
  378. go data_manage.AddChartInfoLog(chartLog)
  379. }
  380. }
  381. br.Ret = 200
  382. br.Msg = "删除成功"
  383. br.Success = true
  384. br.Data = resp
  385. br.IsAddLog = true
  386. }
  387. // ChartClassifyMove
  388. // @Title 相关性图表分类移动接口
  389. // @Description 相关性图表分类移动接口
  390. // @Success 200 {object} data_manage.MoveChartClassifyReq
  391. // @router /classify/move [post]
  392. func (c *ClassifyController) ChartClassifyMove() {
  393. br := new(models.BaseResponse).Init()
  394. defer func() {
  395. c.Data["json"] = br
  396. c.ServeJSON()
  397. }()
  398. sysUser := c.SysUser
  399. if sysUser == nil {
  400. br.Msg = "请登录"
  401. br.ErrMsg = "请登录,SysUser Is Empty"
  402. br.Ret = 408
  403. return
  404. }
  405. var req data_manage.MoveChartClassifyReq
  406. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  407. if err != nil {
  408. br.Msg = "参数解析异常!"
  409. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  410. return
  411. }
  412. if req.ClassifyId <= 0 {
  413. br.Msg = "参数错误"
  414. br.ErrMsg = "分类id小于等于0"
  415. return
  416. }
  417. //判断分类是否存在
  418. chartClassifyInfo, err := data_manage.GetChartClassifyById(req.ClassifyId)
  419. if err != nil {
  420. br.Msg = "移动失败"
  421. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  422. return
  423. }
  424. if chartClassifyInfo.Source != utils.CHART_SOURCE_CROSS_HEDGING {
  425. br.Msg = "分类异常"
  426. br.ErrMsg = "分类异常,不是跨品种分析图表的分类"
  427. return
  428. }
  429. updateCol := make([]string, 0)
  430. //如果有传入 上一个兄弟节点分类id
  431. if req.PrevClassifyId > 0 {
  432. //上一个兄弟节点
  433. prevClassify, err := data_manage.GetChartClassifyById(req.PrevClassifyId)
  434. if err != nil {
  435. br.Msg = "移动失败"
  436. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  437. return
  438. }
  439. if prevClassify.Source != utils.CHART_SOURCE_CROSS_HEDGING {
  440. br.Msg = "上级节点分类异常"
  441. br.ErrMsg = "上级节点分类异常,不是跨品种分析图表的分类"
  442. return
  443. }
  444. //如果是移动在两个兄弟节点之间
  445. if req.NextClassifyId > 0 {
  446. //下一个兄弟节点
  447. nextClassify, err := data_manage.GetChartClassifyById(req.NextClassifyId)
  448. if err != nil {
  449. br.Msg = "移动失败"
  450. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  451. return
  452. }
  453. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  454. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == chartClassifyInfo.Sort {
  455. //变更兄弟节点的排序
  456. updateSortStr := `sort + 2`
  457. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr)
  458. } else {
  459. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  460. if nextClassify.Sort-prevClassify.Sort == 1 {
  461. //变更兄弟节点的排序
  462. updateSortStr := `sort + 1`
  463. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  464. }
  465. }
  466. }
  467. chartClassifyInfo.Sort = prevClassify.Sort + 1
  468. chartClassifyInfo.ModifyTime = time.Now()
  469. updateCol = append(updateCol, "Sort", "ModifyTime")
  470. } else {
  471. firstClassify, err := data_manage.GetFirstChartClassifyByParentIdAndSource(chartClassifyInfo.ParentId, chartClassifyInfo.Source)
  472. if err != nil && err.Error() != utils.ErrNoRow() {
  473. br.Msg = "移动失败"
  474. br.ErrMsg = "获取跨品种分析图表分类排序第一条的分类信息失败,Err:" + err.Error()
  475. return
  476. }
  477. if firstClassify != nil && firstClassify.Source != utils.CHART_SOURCE_CROSS_HEDGING {
  478. br.Msg = "上级节点分类异常"
  479. br.ErrMsg = "上级节点分类异常,不是跨品种分析图表的分类"
  480. return
  481. }
  482. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  483. if firstClassify != nil && firstClassify.Sort == 0 {
  484. updateSortStr := ` sort + 1 `
  485. _ = data_manage.UpdateChartClassifySortByParentId(firstClassify.ParentId, firstClassify.ChartClassifyId-1, 0, updateSortStr)
  486. }
  487. chartClassifyInfo.Sort = 0 //那就是排在第一位
  488. chartClassifyInfo.ModifyTime = time.Now()
  489. updateCol = append(updateCol, "Sort", "ModifyTime")
  490. }
  491. //更新
  492. if len(updateCol) > 0 {
  493. err = chartClassifyInfo.Update(updateCol)
  494. if err != nil {
  495. br.Msg = "移动失败"
  496. br.ErrMsg = "修改失败,Err:" + err.Error()
  497. return
  498. }
  499. }
  500. br.Ret = 200
  501. br.Success = true
  502. br.Msg = "移动成功"
  503. }