edb_info_refresh.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/data_manage/edb_refresh"
  7. "eta_gn/eta_api/models/data_manage/edb_refresh/request"
  8. "eta_gn/eta_api/services/data"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. )
  13. // @router /edb_info/refresh/source_list [get]
  14. func (c *EdbInfoController) RefreshSourceList() {
  15. br := new(models.BaseResponse).Init()
  16. defer func() {
  17. c.Data["json"] = br
  18. c.ServeJSON()
  19. }()
  20. item := edb_refresh.EdbRefreshSource{}
  21. tmpList, err := item.GetAllList()
  22. if err != nil {
  23. br.Msg = "获取失败"
  24. br.ErrMsg = "获取刷新数据源失败, Err: " + err.Error()
  25. return
  26. }
  27. list := make([]edb_refresh.EdbRefreshSourceList, 0)
  28. tmpMap := make(map[int]edb_refresh.EdbRefreshSourceList, 0)
  29. for _, v := range tmpList {
  30. tmp, ok := tmpMap[v.Source]
  31. if ok {
  32. continue
  33. }
  34. tmp = edb_refresh.EdbRefreshSourceList{
  35. Source: v.Source,
  36. SourceName: v.SourceName,
  37. SourceNameEn: v.SourceNameEn,
  38. Child: make([]edb_refresh.EdbRefreshSourceList, 0),
  39. HasChild: v.HasChild,
  40. }
  41. if v.HasChild == 1 {
  42. for _, v2 := range tmpList {
  43. if v2.Source == v.Source {
  44. tmp.Child = append(tmp.Child, edb_refresh.EdbRefreshSourceList{
  45. Source: v2.Source,
  46. SourceName: v2.SourceName,
  47. SubSource: v2.SubSource,
  48. SubSourceName: v2.SubSourceName,
  49. SourceNameEn: v2.SourceNameEn,
  50. SubSourceNameEn: v2.SubSourceNameEn,
  51. HasChild: 0,
  52. })
  53. }
  54. }
  55. }
  56. tmpMap[v.Source] = tmp
  57. list = append(list, tmp)
  58. }
  59. br.Ret = 200
  60. br.Success = true
  61. br.Msg = "获取成功"
  62. br.Data = list
  63. }
  64. // @router /edb_info/refresh/classify_list [get]
  65. func (c *EdbInfoController) RefreshClassifyList() {
  66. br := new(models.BaseResponse).Init()
  67. defer func() {
  68. c.Data["json"] = br
  69. c.ServeJSON()
  70. }()
  71. source, _ := c.GetInt("Source", utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
  72. list := make([]*edb_refresh.BaseClassifyItems, 0)
  73. switch source {
  74. default:
  75. tmpList, err := data_manage.GetAllEdbClassify()
  76. if err != nil && !utils.IsErrNoRow(err) {
  77. br.Msg = "获取失败"
  78. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  79. return
  80. }
  81. for _, v := range tmpList {
  82. list = append(list, &edb_refresh.BaseClassifyItems{
  83. ClassifyId: v.ClassifyId,
  84. ClassifyName: v.ClassifyName,
  85. ParentId: v.ParentId,
  86. UniqueCode: fmt.Sprint(v.ClassifyId),
  87. Children: nil,
  88. })
  89. }
  90. list = buildTree(list, 0)
  91. }
  92. br.Ret = 200
  93. br.Success = true
  94. br.Msg = "获取成功"
  95. br.Data = list
  96. }
  97. func buildTree(items []*edb_refresh.BaseClassifyItems, parentId int) []*edb_refresh.BaseClassifyItems {
  98. var result []*edb_refresh.BaseClassifyItems
  99. for i := range items {
  100. if items[i].ParentId == parentId {
  101. items[i].Children = buildTree(items, items[i].ClassifyId)
  102. result = append(result, items[i])
  103. }
  104. }
  105. return result
  106. }
  107. // @router /edb_info/refresh/edb_list [get]
  108. func (c *EdbInfoController) RefreshEdbList() {
  109. br := new(models.BaseResponse).Init()
  110. defer func() {
  111. c.Data["json"] = br
  112. c.ServeJSON()
  113. }()
  114. source, _ := c.GetInt("Source")
  115. subSource, _ := c.GetInt("SubSource")
  116. classifyId := c.GetString("ClassifyId")
  117. terminalCode := c.GetString("TerminalCode")
  118. sysUserId := c.GetString("SysUserId")
  119. frequency := c.GetString("Frequency")
  120. keyword := c.GetString("Keyword")
  121. status := c.GetString("Status")
  122. sortParam := c.GetString("SortParam")
  123. sortType := c.GetString("SortType")
  124. switch sortParam {
  125. case "end_date":
  126. default:
  127. sortParam = " end_date "
  128. }
  129. switch sortType {
  130. case "desc", "asc":
  131. default:
  132. sortType = "desc"
  133. }
  134. pageSize, _ := c.GetInt("PageSize")
  135. currentIndex, _ := c.GetInt("CurrentIndex")
  136. var startSize int
  137. if pageSize <= 0 {
  138. pageSize = utils.PageSize20
  139. }
  140. if currentIndex <= 0 {
  141. currentIndex = 1
  142. }
  143. startSize = utils.StartIndex(currentIndex, pageSize)
  144. total, list, err := data.GetList(source, subSource, classifyId, terminalCode, sysUserId, frequency, keyword, status, startSize, pageSize, sortParam, sortType)
  145. if err != nil && !utils.IsErrNoRow(err) {
  146. br.Msg = "获取失败"
  147. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  148. return
  149. }
  150. page := paging.GetPaging(currentIndex, pageSize, total)
  151. resp := data_manage.RefreshBaseEdbInfoResp{
  152. Paging: page,
  153. List: list,
  154. }
  155. list, err, errMsg, isSendEmail := data.HandleRefreshTime(source, subSource, list)
  156. if err != nil {
  157. br.Msg = errMsg
  158. br.ErrMsg = "保存失败,Err:" + err.Error()
  159. br.IsSendEmail = isSendEmail
  160. return
  161. }
  162. br.Ret = 200
  163. br.Success = true
  164. br.Msg = "获取成功"
  165. br.Data = resp
  166. }
  167. // @router /edb_info/refresh/default_config [get]
  168. func (c *EdbInfoController) GetEdbRefreshDefaultConfig() {
  169. br := new(models.BaseResponse).Init()
  170. defer func() {
  171. c.Data["json"] = br
  172. c.ServeJSON()
  173. }()
  174. source, _ := c.GetInt("Source")
  175. subSource, _ := c.GetInt("SubSource")
  176. frequency := c.GetString("Frequency")
  177. if source <= 0 {
  178. br.Msg = "来源不能为空"
  179. br.IsSendEmail = false
  180. return
  181. }
  182. if source != utils.DATA_SOURCE_YS && frequency == `` {
  183. br.Msg = "频度不能为空"
  184. br.IsSendEmail = false
  185. return
  186. }
  187. list, err := edb_refresh.GetListBySourceAndFrequency(source, subSource, frequency)
  188. if err != nil {
  189. br.Msg = "获取失败"
  190. br.ErrMsg = err.Error()
  191. return
  192. }
  193. br.Ret = 200
  194. br.Success = true
  195. br.Msg = "获取成功"
  196. br.Data = list
  197. }
  198. // @router /edb_info/refresh/default_config/save [post]
  199. func (c *EdbInfoController) SaveEdbRefreshDefaultConfig() {
  200. br := new(models.BaseResponse).Init()
  201. defer func() {
  202. c.Data["json"] = br
  203. c.ServeJSON()
  204. }()
  205. var req request.SaveEdbRefreshDefaultConfigReq
  206. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  207. if err != nil {
  208. br.Msg = "参数解析异常!"
  209. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  210. return
  211. }
  212. err, errMsg, isSendEmail := data.SaveEdbRefreshDefaultConfig(req.Source, req.SubSource, req.Frequency, req.List)
  213. if err != nil {
  214. br.Msg = errMsg
  215. br.ErrMsg = "保存失败,Err:" + err.Error()
  216. br.IsSendEmail = isSendEmail
  217. return
  218. }
  219. br.Ret = 200
  220. br.Success = true
  221. br.Msg = "保存成功"
  222. }
  223. // @router /edb_info/refresh/config/save [post]
  224. func (c *EdbInfoController) SaveEdbRefreshConfig() {
  225. br := new(models.BaseResponse).Init()
  226. defer func() {
  227. c.Data["json"] = br
  228. c.ServeJSON()
  229. }()
  230. var req request.SaveEdbRefreshConfigReq
  231. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  232. if err != nil {
  233. br.Msg = "参数解析异常!"
  234. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  235. return
  236. }
  237. err, errMsg, isSendEmail := data.SaveEdbRefreshConfig(req.Source, req.SubSource, req.ClassifyId, req.TerminalCode, req.Frequency, req.Keyword, req.Status, req.SysUserId, req.IsSelectAll, req.List, req.EdbSelectIdList, c.SysUser.AdminId, c.SysUser.RealName)
  238. if err != nil {
  239. br.Msg = errMsg
  240. br.ErrMsg = "保存失败,Err:" + err.Error()
  241. br.IsSendEmail = isSendEmail
  242. return
  243. }
  244. br.Ret = 200
  245. br.Success = true
  246. br.Msg = "保存成功"
  247. }
  248. // @router /edb_info/refresh/status/save [post]
  249. func (c *EdbInfoController) SaveEdbRefreshStatus() {
  250. br := new(models.BaseResponse).Init()
  251. defer func() {
  252. c.Data["json"] = br
  253. c.ServeJSON()
  254. }()
  255. var req request.SaveEdbRefreshStatusReq
  256. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  257. if err != nil {
  258. br.Msg = "参数解析异常!"
  259. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  260. return
  261. }
  262. if req.Source <= 0 {
  263. br.Msg = "来源不能为空"
  264. br.IsSendEmail = false
  265. return
  266. }
  267. edbIdList := make([]int, 0)
  268. edbCodeList := make([]string, 0)
  269. if req.IsSelectAll {
  270. _, edbList, err := data.GetList(req.Source, req.SubSource, req.ClassifyId, req.TerminalCode, req.SysUserId, req.Frequency, req.Keyword, req.Status, 0, 100000, "", "")
  271. if err != nil && !utils.IsErrNoRow(err) {
  272. br.Msg = "获取失败"
  273. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  274. return
  275. }
  276. notIdMap := make(map[int]int, 0)
  277. for _, v := range req.EdbSelectIdList {
  278. notIdMap[v] = v
  279. }
  280. for _, v := range edbList {
  281. _, ok := notIdMap[v.EdbInfoId]
  282. if ok {
  283. continue
  284. }
  285. edbIdList = append(edbIdList, v.EdbInfoId)
  286. edbCodeList = append(edbCodeList, v.IndexCode)
  287. }
  288. } else {
  289. edbIdList = req.EdbSelectIdList
  290. }
  291. if len(edbIdList) <= 0 {
  292. br.Msg = "指标不能为空"
  293. br.IsSendEmail = false
  294. return
  295. }
  296. isStop := 0
  297. if req.ModifyStatus == `暂停` {
  298. isStop = 1
  299. }
  300. if len(edbIdList) <= 0 {
  301. br.Msg = "指标不能为空"
  302. br.IsSendEmail = false
  303. return
  304. }
  305. calculateEdbIdList := make([]int, 0)
  306. if isStop == 1 {
  307. fromEdbIdList := make([]int, 0)
  308. edbList, e := data_manage.GetEdbInfoByIdList(edbIdList)
  309. if e != nil && !utils.IsErrNoRow(e) {
  310. br.Msg = "获取失败"
  311. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  312. return
  313. }
  314. for _, v := range edbList {
  315. if v.EdbInfoType == 0 {
  316. fromEdbIdList = append(fromEdbIdList, v.EdbInfoId)
  317. }
  318. }
  319. hasFind := make(map[int]struct{})
  320. calculateEdbIdList, err = data.GetCalculateEdbByFromEdbInfo(fromEdbIdList, calculateEdbIdList, hasFind)
  321. if err != nil {
  322. br.Msg = "查询计算指标信息失败"
  323. br.ErrMsg = "查询计算指标信息失败,Err:" + err.Error()
  324. return
  325. }
  326. }
  327. switch req.Source {
  328. default:
  329. err = data_manage.ModifyEdbInfoUpdateStatus(edbIdList, isStop, calculateEdbIdList)
  330. }
  331. if err != nil {
  332. br.Msg = `保存失败`
  333. br.ErrMsg = "保存失败,Err:" + err.Error()
  334. return
  335. }
  336. br.Ret = 200
  337. br.Success = true
  338. br.Msg = "保存成功"
  339. }
  340. // @router /edb_info/single_refresh/status/save [post]
  341. func (c *EdbInfoController) SaveEdbRefreshStatusSingle() {
  342. br := new(models.BaseResponse).Init()
  343. defer func() {
  344. c.Data["json"] = br
  345. c.ServeJSON()
  346. }()
  347. var req request.SaveEdbRefreshStatusSingleReq
  348. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  349. if err != nil {
  350. br.Msg = "参数解析异常!"
  351. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  352. return
  353. }
  354. if req.EdbInfoId <= 0 {
  355. br.Msg = "请选择指标"
  356. br.IsSendEmail = false
  357. return
  358. }
  359. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  360. if err != nil {
  361. if utils.IsErrNoRow(err) {
  362. br.Msg = "指标不存在"
  363. return
  364. }
  365. br.Msg = "查询指标失败"
  366. br.ErrMsg = "查询指标失败,Err:" + err.Error()
  367. return
  368. }
  369. calculateEdbIdList := make([]int, 0)
  370. isStop := 0
  371. if req.ModifyStatus == `暂停` {
  372. isStop = 1
  373. }
  374. if isStop == 1 && edbInfo.EdbInfoType == 0 { //基础指标, 只有在停用的情况下才需要查询计算指标
  375. hasFind := make(map[int]struct{})
  376. calculateEdbIdList, err = data.GetCalculateEdbByFromEdbInfo([]int{edbInfo.EdbInfoId}, calculateEdbIdList, hasFind)
  377. if err != nil {
  378. br.Msg = "查询计算指标信息失败"
  379. br.ErrMsg = "查询计算指标信息失败,Err:" + err.Error()
  380. return
  381. }
  382. }
  383. switch edbInfo.Source {
  384. default: // wind
  385. err = data_manage.EdbInfoUpdateStatusByEdbInfoId([]int{edbInfo.EdbInfoId}, isStop, calculateEdbIdList)
  386. }
  387. if err != nil {
  388. br.Msg = `保存失败`
  389. br.ErrMsg = "保存失败,Err:" + err.Error()
  390. return
  391. }
  392. br.Ret = 200
  393. br.Success = true
  394. br.Msg = "保存成功"
  395. }
  396. // @router /edb_info/relation/refresh/save [post]
  397. func (c *EdbInfoController) SaveRelationEdbRefreshStatus() {
  398. br := new(models.BaseResponse).Init()
  399. defer func() {
  400. c.Data["json"] = br
  401. c.ServeJSON()
  402. }()
  403. var req request.SaveRelationEdbRefreshStatusReq
  404. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  405. if err != nil {
  406. br.Msg = "参数解析异常!"
  407. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  408. return
  409. }
  410. if req.Source <= 0 {
  411. br.Msg = "来源不能为空"
  412. br.IsSendEmail = false
  413. return
  414. }
  415. edbType := 1 //基础指标
  416. switch req.Source {
  417. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND: // wind
  418. case -1:
  419. req.Source = 0
  420. edbType = 2
  421. default:
  422. br.Msg = "暂不支持设置其他来源的指标"
  423. return
  424. }
  425. edbIdList := make([]int, 0)
  426. edbCodeList := make([]string, 0)
  427. if req.IsSelectAll {
  428. _, edbList, err := data.GetEdbRelationList(req.Source, edbType, req.ClassifyId, req.SysUserId, req.Frequency, req.Keyword, req.Status, 0, 100000, "", "")
  429. if err != nil && !utils.IsErrNoRow(err) {
  430. br.Msg = "获取失败"
  431. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  432. return
  433. }
  434. notIdMap := make(map[int]int, 0)
  435. for _, v := range req.EdbSelectIdList {
  436. notIdMap[v] = v
  437. }
  438. for _, v := range edbList {
  439. _, ok := notIdMap[v.EdbInfoId]
  440. if ok {
  441. continue
  442. }
  443. edbIdList = append(edbIdList, v.EdbInfoId)
  444. }
  445. } else {
  446. edbIdList = req.EdbSelectIdList
  447. }
  448. if len(edbIdList) <= 0 {
  449. br.Msg = "指标不能为空"
  450. br.IsSendEmail = false
  451. return
  452. }
  453. edbList, e := data_manage.GetEdbInfoByIdList(edbIdList)
  454. if e != nil && !utils.IsErrNoRow(e) {
  455. br.Msg = "获取失败"
  456. br.ErrMsg = "获取数据失败,Err:" + e.Error()
  457. return
  458. }
  459. fromEdbIdList := make([]int, 0)
  460. for _, v := range edbList {
  461. if req.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  462. edbCodeList = append(edbCodeList, v.EdbCode)
  463. }
  464. if v.EdbInfoType == 0 {
  465. fromEdbIdList = append(fromEdbIdList, v.EdbInfoId)
  466. }
  467. }
  468. isStop := 0
  469. if req.ModifyStatus == `暂停` {
  470. isStop = 1
  471. }
  472. calculateEdbIdList := make([]int, 0)
  473. if isStop == 1 {
  474. hasFind := make(map[int]struct{})
  475. calculateEdbIdList, err = data.GetCalculateEdbByFromEdbInfo(fromEdbIdList, calculateEdbIdList, hasFind)
  476. if err != nil {
  477. br.Msg = "查询计算指标信息失败"
  478. br.ErrMsg = "查询计算指标信息失败,Err:" + err.Error()
  479. return
  480. }
  481. }
  482. switch req.Source {
  483. default:
  484. err = data_manage.EdbInfoUpdateStatusByEdbInfoId(edbIdList, isStop, calculateEdbIdList)
  485. }
  486. if err != nil {
  487. br.Msg = `保存失败`
  488. br.ErrMsg = "保存失败,Err:" + err.Error()
  489. return
  490. }
  491. br.Ret = 200
  492. br.Success = true
  493. br.Msg = "保存成功"
  494. }
  495. // @router /edb_info/refresh/edb_config [get]
  496. func (c *EdbInfoController) GetEdbRefreshEdbConfig() {
  497. br := new(models.BaseResponse).Init()
  498. defer func() {
  499. c.Data["json"] = br
  500. c.ServeJSON()
  501. }()
  502. source, _ := c.GetInt("Source")
  503. subSource, _ := c.GetInt("SubSource")
  504. edbInfoId, _ := c.GetInt("EdbInfoId")
  505. if source <= 0 {
  506. br.Msg = "来源不能为空"
  507. br.IsSendEmail = false
  508. return
  509. }
  510. if edbInfoId <= 0 {
  511. br.Msg = "指标不能为空"
  512. br.IsSendEmail = false
  513. return
  514. }
  515. list, err := edb_refresh.GetEdbRefreshConfigListBySourceAndeEdbInfoId(source, subSource, edbInfoId)
  516. if err != nil {
  517. br.Msg = "获取失败"
  518. br.ErrMsg = err.Error()
  519. return
  520. }
  521. if len(list) > 0 {
  522. br.Ret = 200
  523. br.Success = true
  524. br.Msg = "获取成功"
  525. br.Data = list
  526. return
  527. }
  528. var frequency string
  529. switch source {
  530. default:
  531. item, err := data_manage.GetEdbInfoById(edbInfoId)
  532. if err != nil {
  533. br.Msg = "获取指标信息失败"
  534. br.ErrMsg = err.Error()
  535. return
  536. }
  537. frequency = item.Frequency
  538. }
  539. if frequency == `` {
  540. br.Msg = "获取的指标频度信息异常"
  541. br.IsSendEmail = false
  542. return
  543. }
  544. tmpList, err := edb_refresh.GetListBySourceAndFrequency(source, subSource, frequency)
  545. if err != nil {
  546. br.Msg = "获取失败"
  547. br.ErrMsg = err.Error()
  548. return
  549. }
  550. list = make([]*edb_refresh.EdbRefreshConfigItem, 0)
  551. for _, v := range tmpList {
  552. list = append(list, &edb_refresh.EdbRefreshConfigItem{
  553. RefreshFrequency: v.RefreshFrequency,
  554. RefreshFrequencyDay: v.RefreshFrequencyDay,
  555. RefreshTime: v.RefreshTime,
  556. RefreshAllData: v.RefreshAllData,
  557. RefreshDataNum: v.RefreshDataNum,
  558. })
  559. }
  560. br.Ret = 200
  561. br.Success = true
  562. br.Msg = "获取成功"
  563. br.Data = list
  564. }