base_from_rzd.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // @Author gmy 2024/8/13 16:01:00
  2. package controllers
  3. import (
  4. "encoding/json"
  5. "eta/eta_index_lib/logic"
  6. "eta/eta_index_lib/models"
  7. "eta/eta_index_lib/services"
  8. "eta/eta_index_lib/utils"
  9. "fmt"
  10. "strconv"
  11. "time"
  12. )
  13. type BaseFromRzdController struct {
  14. BaseAuthController
  15. }
  16. // Add
  17. // @Title 新增睿姿得指标
  18. // @Description 新增粮油商务网指标
  19. // @router /add [post]
  20. func (this *BaseFromRzdController) Add() {
  21. br := new(models.BaseResponse).Init()
  22. var cacheKey string
  23. defer func() {
  24. utils.Rc.Delete(cacheKey)
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. source := utils.DATA_SOURCE_RZD
  29. var req models.AddEdbInfoReq
  30. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  31. if err != nil {
  32. br.Msg = "参数解析异常!"
  33. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  34. return
  35. }
  36. if req.EdbCode == "" {
  37. br.Msg = "请输入指标编码!"
  38. br.ErrMsg = "请输入指标编码,指标编码为空"
  39. return
  40. }
  41. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
  42. if !utils.Rc.IsExist(cacheKey) {
  43. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  44. err = models.AddEdbDataFromRzd(req.EdbCode)
  45. if err != nil {
  46. br.Msg = "获取指标信息失败!"
  47. br.ErrMsg = "获取指标信息失败 AddEdbDataFromSci99,Err:" + err.Error()
  48. return
  49. }
  50. br.Ret = 200
  51. br.Success = true
  52. br.Msg = "获取成功"
  53. } else {
  54. br.Ret = 501
  55. br.Success = true
  56. br.Msg = "系统处理中,请稍后重试"
  57. }
  58. }
  59. // Refresh
  60. // @Title 刷新粮油商务网指标接口
  61. // @Description 刷新粮油商务网指标接口
  62. // @Success 200 {object} models.RefreshEdbInfoReq
  63. // @router /refresh [post]
  64. func (this *BaseFromRzdController) Refresh() {
  65. br := new(models.BaseResponse).Init()
  66. var cacheKey string
  67. defer func() {
  68. if br.ErrMsg == "" {
  69. br.IsSendEmail = false
  70. }
  71. _ = utils.Rc.Delete(cacheKey)
  72. this.Data["json"] = br
  73. this.ServeJSON()
  74. }()
  75. source := utils.DATA_SOURCE_LY
  76. var req models.RefreshEdbInfoReq
  77. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  78. if err != nil {
  79. br.Msg = "参数解析异常!"
  80. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  81. return
  82. }
  83. if req.EdbCode == "" {
  84. br.Msg = "请输入指标编码!"
  85. br.ErrMsg = "请输入指标编码,指标编码为空"
  86. return
  87. }
  88. if req.EdbInfoId <= 0 {
  89. br.Msg = "请输入指标ID!"
  90. br.ErrMsg = "请输入指标ID"
  91. return
  92. }
  93. // 获取指标详情
  94. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  95. if err != nil {
  96. br.Msg = "指标不存在!"
  97. br.ErrMsg = "指标不存在"
  98. return
  99. }
  100. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  101. if utils.Rc.IsExist(cacheKey) {
  102. br.Ret = 501
  103. br.Success = true
  104. br.Msg = "系统处理中,请稍后重试"
  105. return
  106. }
  107. dataUpdateTime := time.Now().Format(utils.FormatDateTime)
  108. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  109. err = models.RefreshEdbDataFromBloomberg(req.EdbInfoId, req.EdbCode, req.StartDate)
  110. if err != nil && err.Error() != utils.ErrNoRow() {
  111. br.Msg = "刷新指标信息失败!"
  112. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromBloomberg,Err:" + err.Error()
  113. return
  114. }
  115. // 更新指标最大最小值
  116. erDataUpdateDate, err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfoDataUpdate(edbInfo, dataUpdateTime)
  117. if err != nil {
  118. br.Msg = errMsg
  119. br.ErrMsg = err.Error()
  120. return
  121. }
  122. // 添加指标刷新成功日志
  123. if erDataUpdateDate != "" {
  124. _ = services.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 1, "", 0, 0)
  125. } else {
  126. _ = services.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 2, "未刷新到数据", 0, 0)
  127. }
  128. // 更新ES
  129. go logic.UpdateEs(edbInfo.EdbInfoId)
  130. br.Ret = 200
  131. br.Success = true
  132. br.Msg = "获取成功"
  133. }
  134. // AddRzdClassify
  135. // @Title 新增分类
  136. // @Description 获取分类
  137. // @Success 200 {object} models.BaseFromRzdClassify
  138. // @router /add/rzd/classify [post]
  139. func (this *BaseFromRzdController) AddRzdClassify() {
  140. br := new(models.BaseResponse).Init()
  141. var cacheKey string
  142. defer func() {
  143. if br.ErrMsg == "" {
  144. br.IsSendEmail = false
  145. }
  146. _ = utils.Rc.Delete(cacheKey)
  147. this.Data["json"] = br
  148. this.ServeJSON()
  149. }()
  150. var reqData struct {
  151. ClassifyName string `json:"ClassifyName"`
  152. ParentId int `json:"parentId"`
  153. }
  154. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData)
  155. if err != nil {
  156. br.ErrMsg = "无法解析请求体"
  157. return
  158. }
  159. categoryName := reqData.ClassifyName
  160. if categoryName == "" {
  161. br.Msg = "请输入分类!"
  162. br.ErrMsg = "请输入分类"
  163. return
  164. }
  165. rzdClassify := models.BaseFromRzdClassify{
  166. ClassifyName: categoryName,
  167. ParentId: reqData.ParentId,
  168. CreateTime: utils.GetCurrentTime(),
  169. ModifyTime: utils.GetCurrentTime(),
  170. }
  171. lyClassify, err := models.AddRzdClassify(&rzdClassify)
  172. if err != nil {
  173. return
  174. }
  175. br.Ret = 200
  176. br.Success = true
  177. br.Data = lyClassify
  178. br.Msg = "获取成功"
  179. }
  180. // GetRzdClassifyByName
  181. // @Title 获取分类
  182. // @Description 获取分类
  183. // @Success 200 {object} models.BaseFromRzdClassify
  184. // @router /get/rzd/classify/by/name [post]
  185. func (this *BaseFromRzdController) GetRzdClassifyByName() {
  186. br := new(models.BaseResponse).Init()
  187. var cacheKey string
  188. defer func() {
  189. if br.ErrMsg == "" {
  190. br.IsSendEmail = false
  191. }
  192. _ = utils.Rc.Delete(cacheKey)
  193. this.Data["json"] = br
  194. this.ServeJSON()
  195. }()
  196. var reqData struct {
  197. ClassifyName string `json:"ClassifyName"`
  198. }
  199. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData)
  200. if err != nil {
  201. br.ErrMsg = "无法解析请求体"
  202. return
  203. }
  204. categoryName := reqData.ClassifyName
  205. if categoryName == "" {
  206. br.Msg = "请输入分类!"
  207. br.ErrMsg = "请输入分类"
  208. return
  209. }
  210. lyClassify, err := models.GetRzdClassifyByName(categoryName)
  211. if err != nil {
  212. return
  213. }
  214. br.Ret = 200
  215. br.Success = true
  216. br.Data = lyClassify
  217. br.Msg = "获取成功"
  218. }
  219. // AddBatchRzdData
  220. // @Title 新增数据源指标数据
  221. // @Description 新增数据源指标数据
  222. // @Success 200 string "处理成功"
  223. // @router /add/batch/rzd/data [post]
  224. func (this *BaseFromRzdController) AddBatchRzdData() {
  225. br := new(models.BaseResponse).Init()
  226. var cacheKey string
  227. defer func() {
  228. if br.ErrMsg == "" {
  229. br.IsSendEmail = false
  230. }
  231. _ = utils.Rc.Delete(cacheKey)
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. var req []models.BaseFromRzdData
  236. fmt.Println(string(this.Ctx.Input.RequestBody))
  237. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  238. if err != nil {
  239. br.Msg = "参数解析异常!"
  240. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  241. return
  242. }
  243. err = models.AddRzdDataList(req)
  244. if err != nil {
  245. br.Msg = "新增指标数据失败!"
  246. br.ErrMsg = "新增指标数据失败,Err:" + err.Error()
  247. return
  248. }
  249. br.Ret = 200
  250. br.Success = true
  251. br.Msg = "处理成功"
  252. }
  253. // AddRzdIndex
  254. // @Title 新增指标
  255. // @Description 新增指标
  256. // @Success 200 string "处理成功"
  257. // @router /add/rzd/index [post]
  258. func (this *BaseFromRzdController) AddRzdIndex() {
  259. br := new(models.BaseResponse).Init()
  260. var cacheKey string
  261. defer func() {
  262. if br.ErrMsg == "" {
  263. br.IsSendEmail = false
  264. }
  265. _ = utils.Rc.Delete(cacheKey)
  266. this.Data["json"] = br
  267. this.ServeJSON()
  268. }()
  269. var req models.BaseFromRzdIndex
  270. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  271. if err != nil {
  272. br.Msg = "参数解析异常!"
  273. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  274. return
  275. }
  276. indexId, err := models.AddRzdIndex(&req)
  277. if err != nil {
  278. return
  279. }
  280. br.Ret = 200
  281. br.Success = true
  282. br.Data = indexId
  283. br.Msg = "处理成功"
  284. }
  285. // GetRzdIndexDataByIndexIdAndDataTime
  286. // @Title 根据指标code和时间获取指标数据
  287. // @Description 根据指标code和时间获取指标数据
  288. // @Success 200 {object} models.BaseFromLyData
  289. // @router /get/rzd/index/data/by/code/and/time [post]
  290. func (this *BaseFromRzdController) GetRzdIndexDataByIndexIdAndDataTime() {
  291. br := new(models.BaseResponse).Init()
  292. var cacheKey string
  293. defer func() {
  294. if br.ErrMsg == "" {
  295. br.IsSendEmail = false
  296. }
  297. _ = utils.Rc.Delete(cacheKey)
  298. this.Data["json"] = br
  299. this.ServeJSON()
  300. }()
  301. var reqData struct {
  302. IndexCode string `json:"IndexCode"`
  303. DataTime string `json:"DataTime"`
  304. }
  305. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData)
  306. if err != nil {
  307. br.ErrMsg = "无法解析请求体"
  308. return
  309. }
  310. indexCode := reqData.IndexCode
  311. if indexCode == "" {
  312. br.Msg = "请输入指标code!"
  313. br.ErrMsg = "请输入指标code"
  314. return
  315. }
  316. dataTime := reqData.DataTime
  317. if dataTime == "" {
  318. br.Msg = "请输入时间!"
  319. br.ErrMsg = "请输入时间"
  320. return
  321. }
  322. rzdData, err := models.GetRzdDataByIndexCodeAndDataTime(indexCode, dataTime)
  323. if err != nil {
  324. return
  325. }
  326. br.Ret = 200
  327. br.Success = true
  328. br.Data = rzdData
  329. br.Msg = "获取成功"
  330. }
  331. // UpdateRzdDataById
  332. // @Title 更新数据源指标数据
  333. // @Description 更新数据源指标数据
  334. // @Success 200 string "处理成功"
  335. // @router /update/rzd/data/by/id [post]
  336. func (this *BaseFromRzdController) UpdateRzdDataById() {
  337. br := new(models.BaseResponse).Init()
  338. var cacheKey string
  339. defer func() {
  340. if br.ErrMsg == "" {
  341. br.IsSendEmail = false
  342. }
  343. _ = utils.Rc.Delete(cacheKey)
  344. this.Data["json"] = br
  345. this.ServeJSON()
  346. }()
  347. var reqData struct {
  348. Id int `json:"Id"`
  349. Value float64 `json:"Value"`
  350. }
  351. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData)
  352. if err != nil {
  353. br.ErrMsg = "无法解析请求体"
  354. return
  355. }
  356. id := reqData.Id
  357. if id == 0 {
  358. br.Msg = "请输入id!"
  359. br.ErrMsg = "请输入id"
  360. return
  361. }
  362. value := reqData.Value
  363. if value == 0 {
  364. br.Msg = "请输入值!"
  365. br.ErrMsg = "请输入值"
  366. return
  367. }
  368. err = models.UpdateRzdDataById(id, value)
  369. if err != nil {
  370. return
  371. }
  372. br.Ret = 200
  373. br.Success = true
  374. br.Msg = "处理成功"
  375. }
  376. // GetRzdEdbDataByIndexCodeAndDataTime
  377. // @Title 根据指标编码和精确日期获取指标库数据
  378. // @Description 根据指标编码和精确日期获取指标库数据
  379. // @Success 200 {object} []models.EdbDataRzd
  380. // @router /get/edb/rzd/data/by/code/and/time [post]
  381. func (this *BaseFromRzdController) GetRzdEdbDataByIndexCodeAndDataTime() {
  382. br := new(models.BaseResponse).Init()
  383. var cacheKey string
  384. defer func() {
  385. if br.ErrMsg == "" {
  386. br.IsSendEmail = false
  387. }
  388. _ = utils.Rc.Delete(cacheKey)
  389. this.Data["json"] = br
  390. this.ServeJSON()
  391. }()
  392. var reqData struct {
  393. IndexCode string `json:"IndexCode"`
  394. DataTime string `json:"DataTime"`
  395. }
  396. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData)
  397. if err != nil {
  398. br.ErrMsg = "无法解析请求体"
  399. return
  400. }
  401. indexCode := reqData.IndexCode
  402. if indexCode == "" {
  403. br.Msg = "请输入指标id!"
  404. br.ErrMsg = "请输入指标id"
  405. return
  406. }
  407. dataTime := reqData.DataTime
  408. if dataTime == "" {
  409. br.Msg = "请输入时间!"
  410. br.ErrMsg = "请输入时间"
  411. return
  412. }
  413. lyEdbData, err := models.GetLyEdbDataByIndexCodeAndExactDataTime(indexCode, dataTime)
  414. if err != nil {
  415. return
  416. }
  417. br.Ret = 200
  418. br.Success = true
  419. br.Data = lyEdbData
  420. br.Msg = "获取成功"
  421. }
  422. // GetRzdEdbInfoByIndexCode
  423. // @Title 根据指标编码获取指标库指标
  424. // @Description 根据指标编码获取指标库指标
  425. // @Success 200 {object} []models.EdbDataRzd
  426. // @router /get/rzd/edb/info/by/code [post]
  427. func (this *BaseFromRzdController) GetRzdEdbInfoByIndexCode() {
  428. br := new(models.BaseResponse).Init()
  429. var cacheKey string
  430. defer func() {
  431. if br.ErrMsg == "" {
  432. br.IsSendEmail = false
  433. }
  434. _ = utils.Rc.Delete(cacheKey)
  435. this.Data["json"] = br
  436. this.ServeJSON()
  437. }()
  438. var reqData struct {
  439. IndexCode string `json:"IndexCode"`
  440. Source int `json:"Source"`
  441. }
  442. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData)
  443. if err != nil {
  444. br.ErrMsg = "无法解析请求体"
  445. return
  446. }
  447. indexCode := reqData.IndexCode
  448. if indexCode == "" {
  449. br.Msg = "请输入指标id!"
  450. br.ErrMsg = "请输入指标id"
  451. return
  452. }
  453. source := reqData.Source
  454. if source == 0 {
  455. br.Msg = "请输入来源!"
  456. br.ErrMsg = "请输入来源"
  457. return
  458. }
  459. lyEdbData, err := models.GetEdbInfoByEdbCode(source, indexCode)
  460. if err != nil {
  461. return
  462. }
  463. br.Ret = 200
  464. br.Success = true
  465. br.Data = lyEdbData
  466. br.Msg = "获取成功"
  467. }
  468. // UpdateRzdEdbDataById
  469. // @Title 更新指标库数据 须根据指标编码和日期更新 仅适合月度数据
  470. // @Description 更新指标库数据 须根据指标编码和日期更新 仅适合月度数据
  471. // @Success 200 string "处理成功"
  472. // @router /update/rzd/edb/data/by/id [post]
  473. func (this *BaseFromRzdController) UpdateRzdEdbDataById() {
  474. br := new(models.BaseResponse).Init()
  475. var cacheKey string
  476. defer func() {
  477. if br.ErrMsg == "" {
  478. br.IsSendEmail = false
  479. }
  480. _ = utils.Rc.Delete(cacheKey)
  481. this.Data["json"] = br
  482. this.ServeJSON()
  483. }()
  484. var reqData struct {
  485. Id int `json:"Id"`
  486. Value float64 `json:"Value"`
  487. }
  488. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData)
  489. if err != nil {
  490. br.ErrMsg = "无法解析请求体"
  491. return
  492. }
  493. id := reqData.Id
  494. if id == 0 {
  495. br.Msg = "请输入id!"
  496. br.ErrMsg = "请输入id"
  497. return
  498. }
  499. value := reqData.Value
  500. if value == 0 {
  501. br.Msg = "请输入值!"
  502. br.ErrMsg = "请输入值"
  503. return
  504. }
  505. err = models.UpdateRzdEdbDataById(id, value)
  506. if err != nil {
  507. return
  508. }
  509. br.Ret = 200
  510. br.Success = true
  511. br.Msg = "处理成功"
  512. }
  513. // GetRzdIndexByCode
  514. // @Title 查询指标编码是否存在
  515. // @Description 查询指标编码是否存在
  516. // @Success 200 {object} models.BaseFromLyIndex
  517. // @router /get/rzd/index/by/code [post]
  518. func (this *BaseFromRzdController) GetRzdIndexByCode() {
  519. br := new(models.BaseResponse).Init()
  520. var cacheKey string
  521. defer func() {
  522. if br.ErrMsg == "" {
  523. br.IsSendEmail = false
  524. }
  525. _ = utils.Rc.Delete(cacheKey)
  526. this.Data["json"] = br
  527. this.ServeJSON()
  528. }()
  529. var reqData struct {
  530. IndexCode string `json:"IndexCode"`
  531. }
  532. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData)
  533. if err != nil {
  534. br.ErrMsg = "无法解析请求体"
  535. return
  536. }
  537. indexCode := reqData.IndexCode
  538. if indexCode == "" {
  539. br.Msg = "请输入指标id!"
  540. br.ErrMsg = "请输入指标id"
  541. return
  542. }
  543. rzdIndex, err := models.GetRzdIndexByCode(indexCode)
  544. if err != nil {
  545. return
  546. }
  547. br.Ret = 200
  548. br.Success = true
  549. br.Data = rzdIndex
  550. br.Msg = "获取成功"
  551. }
  552. // AddBatchRzdEdbData
  553. // @Title 批量增加睿咨得指标库数据
  554. // @Description 批量增加睿咨得指标库数据
  555. // @Success 200 string "处理成功"
  556. // @router /add/batch/rzd/edb/data [post]
  557. func (this *BaseFromRzdController) AddBatchRzdEdbData() {
  558. br := new(models.BaseResponse).Init()
  559. var cacheKey string
  560. defer func() {
  561. if br.ErrMsg == "" {
  562. br.IsSendEmail = false
  563. }
  564. _ = utils.Rc.Delete(cacheKey)
  565. this.Data["json"] = br
  566. this.ServeJSON()
  567. }()
  568. var req []models.EdbDataRzd
  569. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  570. if err != nil {
  571. br.Msg = "参数解析异常!"
  572. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  573. return
  574. }
  575. err = models.AddRzdEdbDataList(req)
  576. if err != nil {
  577. return
  578. }
  579. br.Ret = 200
  580. br.Success = true
  581. br.Msg = "处理成功"
  582. }
  583. // UpdateRzdEdbData
  584. // @Title 修改指标数据
  585. // @Description 修改指标数据
  586. // @Success 200 string "处理成功"
  587. // @router /update/rzd/edb/data [post]
  588. func (this *BaseFromRzdController) UpdateRzdEdbData() {
  589. br := new(models.BaseResponse).Init()
  590. var cacheKey string
  591. defer func() {
  592. if br.ErrMsg == "" {
  593. br.IsSendEmail = false
  594. }
  595. _ = utils.Rc.Delete(cacheKey)
  596. this.Data["json"] = br
  597. this.ServeJSON()
  598. }()
  599. var req models.BaseFromRzdData
  600. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  601. if err != nil {
  602. br.Msg = "参数解析异常!"
  603. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  604. return
  605. }
  606. err = models.UpdateRzdData(&req)
  607. if err != nil {
  608. return
  609. }
  610. br.Ret = 200
  611. br.Success = true
  612. br.Msg = "处理成功"
  613. }