base_from_jiayue.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "strconv"
  10. "time"
  11. )
  12. // JiaYueController 嘉悦物产数据源
  13. type JiaYueController struct {
  14. BaseAuthController
  15. }
  16. // Add
  17. // @Title 嘉悦物产-新增指标接口
  18. // @Description 新增指标接口
  19. // @Success 200 {object} models.AddEdbInfoReq
  20. // @router /add [post]
  21. func (this *JiaYueController) Add() {
  22. br := new(models.BaseResponse).Init()
  23. var cacheKey string
  24. defer func() {
  25. if br.ErrMsg == "" {
  26. br.IsSendEmail = false
  27. }
  28. _ = utils.Rc.Delete(cacheKey)
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. var req models.AddEdbInfoReq
  33. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  34. br.Msg = "操作失败"
  35. br.ErrMsg = "参数解析异常, err: " + e.Error()
  36. return
  37. }
  38. if req.EdbCode == "" {
  39. br.Msg = "请输入指标编码"
  40. br.ErrMsg = "指标编码为空"
  41. return
  42. }
  43. if req.Source <= 0 {
  44. br.Msg = "请输入指标来源"
  45. br.ErrMsg = "指标来源为空"
  46. return
  47. }
  48. // 加锁
  49. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(req.Source) + "_" + req.EdbCode
  50. if utils.Rc.IsExist(cacheKey) {
  51. br.Ret = 501
  52. br.Success = true
  53. br.Msg = "系统处理中,请稍后重试"
  54. return
  55. }
  56. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  57. defer func() {
  58. _ = utils.Rc.Delete(cacheKey)
  59. }()
  60. // 校验数据源
  61. sourceItem := models.EdbSourceIdMap[req.Source]
  62. if sourceItem == nil {
  63. br.Msg = "数据源有误"
  64. br.ErrMsg = fmt.Sprintf("数据源有误, Source: %d", req.Source)
  65. return
  66. }
  67. // 从桥接服务获取指标和数据
  68. var params models.BridgeJiaYueIndexDataParams
  69. params.IndexCode = req.EdbCode
  70. params.SourceExtend = sourceItem.SourceExtend
  71. params.StartDate = utils.BASE_START_DATE
  72. params.EndDate = utils.BASE_END_DATE
  73. params.IndexCodeRequired = sourceItem.EdbCodeRequired
  74. indexData, e := services.GetJiaYueIndexDataFromBridge(params)
  75. if e != nil {
  76. br.Msg = "获取指标失败"
  77. br.ErrMsg = "获取指标数据失败, Err: " + e.Error()
  78. return
  79. }
  80. if indexData.Id <= 0 {
  81. br.Msg = "指标不存在"
  82. br.Success = true
  83. return
  84. }
  85. // 新增指标数据
  86. e = models.AddEdbDataFromJiaYue(sourceItem.TableName, req.EdbCode, indexData.IndexData)
  87. if e != nil {
  88. br.Msg = "操作失败"
  89. br.ErrMsg = "新增指标数据失败, Err: " + e.Error()
  90. return
  91. }
  92. br.Ret = 200
  93. br.Success = true
  94. br.Msg = "操作成功"
  95. }
  96. // Refresh
  97. // @Title 嘉悦物产-刷新指标接口
  98. // @Description 刷新指标接口
  99. // @Success 200 {object} models.RefreshEdbInfoReq
  100. // @router /refresh [post]
  101. func (this *JiaYueController) Refresh() {
  102. br := new(models.BaseResponse).Init()
  103. var cacheKey string
  104. defer func() {
  105. if br.ErrMsg == "" {
  106. br.IsSendEmail = false
  107. }
  108. this.Data["json"] = br
  109. this.ServeJSON()
  110. }()
  111. var req models.RefreshEdbInfoReq
  112. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  113. if err != nil {
  114. br.Msg = "参数解析异常!"
  115. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  116. return
  117. }
  118. if req.EdbCode == "" {
  119. br.Msg = "请输入指标编码!"
  120. br.ErrMsg = "请输入指标编码,指标编码为空"
  121. return
  122. }
  123. if req.EdbInfoId <= 0 {
  124. br.Msg = "请输入指标ID!"
  125. br.ErrMsg = "请输入指标ID"
  126. return
  127. }
  128. if req.Source <= 0 {
  129. br.Msg = "请输入指标来源"
  130. br.ErrMsg = "指标来源为空"
  131. return
  132. }
  133. startDate := req.StartDate
  134. if startDate == "" {
  135. startDate = utils.BASE_START_DATE
  136. }
  137. // 加锁
  138. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(req.Source) + "_" + req.EdbCode
  139. if utils.Rc.IsExist(cacheKey) {
  140. br.Ret = 501
  141. br.Success = true
  142. br.Msg = "系统处理中,请稍后重试"
  143. return
  144. }
  145. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  146. defer func() {
  147. _ = utils.Rc.Delete(cacheKey)
  148. }()
  149. // 校验指标和数据源
  150. edbInfo, e := models.GetEdbInfoById(req.EdbInfoId)
  151. if e != nil {
  152. br.Msg = "指标信息有误"
  153. br.ErrMsg = "查询指标信息失败, Err: " + e.Error()
  154. return
  155. }
  156. sourceItem := models.EdbSourceIdMap[req.Source]
  157. if sourceItem == nil {
  158. br.Msg = "数据源有误"
  159. br.ErrMsg = fmt.Sprintf("数据源有误, Source: %d", req.Source)
  160. return
  161. }
  162. // 从桥接服务获取指标和数据
  163. var params models.BridgeJiaYueIndexDataParams
  164. params.IndexCode = req.EdbCode
  165. params.SourceExtend = sourceItem.SourceExtend
  166. params.StartDate = startDate
  167. params.EndDate = utils.BASE_END_DATE
  168. params.IndexCodeRequired = sourceItem.EdbCodeRequired
  169. indexData, e := services.GetJiaYueIndexDataFromBridge(params)
  170. if e != nil {
  171. br.Msg = "获取指标失败"
  172. br.ErrMsg = "获取指标数据失败, Err: " + e.Error()
  173. return
  174. }
  175. if indexData.Id <= 0 {
  176. br.Msg = "指标不存在"
  177. br.Success = true
  178. return
  179. }
  180. // 刷新指标数据
  181. e = models.RefreshEdbDataFromJiaYue(req.Source, edbInfo.SubSource, req.EdbInfoId, sourceItem.TableName, req.EdbCode, startDate, indexData.IndexData)
  182. if e != nil {
  183. br.Msg = "刷新失败"
  184. br.ErrMsg = "刷新嘉悦指标失败, Err: " + e.Error()
  185. return
  186. }
  187. // 更新指标最大最小值
  188. e, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  189. if e != nil {
  190. br.Msg = errMsg
  191. br.ErrMsg = e.Error()
  192. return
  193. }
  194. // 更新ES
  195. go logic.UpdateEs(edbInfo.EdbInfoId)
  196. br.Ret = 200
  197. br.Success = true
  198. br.Msg = "操作成功"
  199. }
  200. // SyncNewIndex
  201. // @Title 嘉悦物产-同步增量指标
  202. // @Description 同步增量指标
  203. // @Success 200 {object} models.AddEdbInfoReq
  204. // @router /sync_new_index [post]
  205. func (this *JiaYueController) SyncNewIndex() {
  206. br := new(models.BaseResponse).Init()
  207. defer func() {
  208. if br.ErrMsg == "" {
  209. br.IsSendEmail = false
  210. }
  211. this.Data["json"] = br
  212. this.ServeJSON()
  213. }()
  214. // 从桥接服务获取增量指标
  215. newIndexes, e := services.GetJiaYueNewIndexFromBridge()
  216. if e != nil {
  217. br.Msg = "同步增量指标失败"
  218. br.ErrMsg = "同步增量指标失败, Err: " + e.Error()
  219. return
  220. }
  221. if len(newIndexes) == 0 {
  222. utils.FileLog.Info("无增量指标同步")
  223. return
  224. }
  225. // 获取指标目录
  226. indexMenus, e := services.GetJiaYueMenuListFromBridge()
  227. if e != nil {
  228. br.Msg = "获取指标目录失败"
  229. br.ErrMsg = "获取指标目录失败, Err: " + e.Error()
  230. return
  231. }
  232. go func() {
  233. for _, v := range newIndexes {
  234. // 错误信息在FileLog中这边不做返回
  235. _ = services.SyncJiaYueNewIndex(v, indexMenus)
  236. }
  237. }()
  238. br.Ret = 200
  239. br.Success = true
  240. br.Msg = "操作成功"
  241. }