base_from_jiayue.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services/alarm_msg"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. )
  15. var (
  16. BridgeApiJiaYueIndexDataUrl = "/api/index_data/jiayue/index_data" // 获取指标数据API
  17. BridgeApiJiaYueNewIndexUrl = "/api/index_data/jiayue/new_index" // 获取增量指标API
  18. BridgeApiJiaYueMenuListUrl = "/api/index_data/jiayue/menu_list" // 获取指标目录API
  19. )
  20. // GetJiaYueIndexDataFromBridge 从桥接服务获取指标数据
  21. func GetJiaYueIndexDataFromBridge(param models.BridgeJiaYueIndexDataParams) (indexData models.BridgeJiaYueIndexAndData, err error) {
  22. defer func() {
  23. if err != nil {
  24. b, _ := json.Marshal(param)
  25. tips := fmt.Sprintf("桥接服务-获取嘉悦指标数据失败, err: %s, params: %s", err.Error(), string(b))
  26. utils.FileLog.Info(tips)
  27. go alarm_msg.SendAlarmMsg(tips, 3)
  28. }
  29. }()
  30. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueIndexDataUrl)
  31. data, e := json.Marshal(param)
  32. if e != nil {
  33. err = fmt.Errorf("data json marshal err: %s", e.Error())
  34. return
  35. }
  36. body := ioutil.NopCloser(strings.NewReader(string(data)))
  37. client := &http.Client{}
  38. req, e := http.NewRequest("POST", url, body)
  39. if e != nil {
  40. err = fmt.Errorf("http create request err: %s", e.Error())
  41. return
  42. }
  43. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  44. contentType := "application/json;charset=utf-8"
  45. req.Header.Set("Content-Type", contentType)
  46. req.Header.Set("Authorization", checkToken)
  47. resp, e := client.Do(req)
  48. if e != nil {
  49. err = fmt.Errorf("http client do err: %s", e.Error())
  50. return
  51. }
  52. defer func() {
  53. _ = resp.Body.Close()
  54. }()
  55. b, e := ioutil.ReadAll(resp.Body)
  56. if e != nil {
  57. err = fmt.Errorf("resp body read err: %s", e.Error())
  58. return
  59. }
  60. if len(b) == 0 {
  61. err = fmt.Errorf("resp body is empty")
  62. return
  63. }
  64. // 生产环境解密
  65. if utils.RunMode == "release" {
  66. str := string(b)
  67. str = strings.Trim(str, `"`)
  68. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  69. }
  70. result := new(models.BridgeJiaYueResultIndexData)
  71. if e = json.Unmarshal(b, &result); e != nil {
  72. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  73. return
  74. }
  75. if result.Code != 200 {
  76. err = fmt.Errorf("result: %s", string(b))
  77. return
  78. }
  79. indexData = result.Data
  80. return
  81. }
  82. // GetJiaYueNewIndexFromBridge 从桥接服务获取增量指标
  83. func GetJiaYueNewIndexFromBridge() (indexData []models.BridgeJiaYueIndexAndData, err error) {
  84. defer func() {
  85. if err != nil {
  86. tips := fmt.Sprintf("桥接服务-获取嘉悦增量指标失败, err: %s", err.Error())
  87. utils.FileLog.Info(tips)
  88. go alarm_msg.SendAlarmMsg(tips, 3)
  89. }
  90. }()
  91. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueNewIndexUrl)
  92. body := ioutil.NopCloser(strings.NewReader(""))
  93. client := &http.Client{}
  94. req, e := http.NewRequest("POST", url, body)
  95. if e != nil {
  96. err = fmt.Errorf("http create request err: %s", e.Error())
  97. return
  98. }
  99. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  100. contentType := "application/json;charset=utf-8"
  101. req.Header.Set("Content-Type", contentType)
  102. req.Header.Set("Authorization", checkToken)
  103. resp, e := client.Do(req)
  104. if e != nil {
  105. err = fmt.Errorf("http client do err: %s", e.Error())
  106. return
  107. }
  108. defer func() {
  109. _ = resp.Body.Close()
  110. }()
  111. b, e := ioutil.ReadAll(resp.Body)
  112. if e != nil {
  113. err = fmt.Errorf("resp body read err: %s", e.Error())
  114. return
  115. }
  116. if len(b) == 0 {
  117. err = fmt.Errorf("resp body is empty")
  118. return
  119. }
  120. // 生产环境解密
  121. if utils.RunMode == "release" {
  122. str := string(b)
  123. str = strings.Trim(str, `"`)
  124. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  125. }
  126. result := new(models.BridgeJiaYueResultNewIndexData)
  127. if e = json.Unmarshal(b, &result); e != nil {
  128. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  129. return
  130. }
  131. if result.Code != 200 {
  132. err = fmt.Errorf("result: %s", string(b))
  133. return
  134. }
  135. indexData = result.Data
  136. return
  137. }
  138. // GetJiaYueMenuListFromBridge 从桥接服务获取指标目录列表
  139. func GetJiaYueMenuListFromBridge() (indexData []models.BridgeJiaYueIndexMenuData, err error) {
  140. defer func() {
  141. if err != nil {
  142. tips := fmt.Sprintf("桥接服务-获取嘉悦增量指标失败, err: %s", err.Error())
  143. utils.FileLog.Info(tips)
  144. go alarm_msg.SendAlarmMsg(tips, 3)
  145. }
  146. }()
  147. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiJiaYueMenuListUrl)
  148. body := ioutil.NopCloser(strings.NewReader(""))
  149. client := &http.Client{}
  150. req, e := http.NewRequest("POST", url, body)
  151. if e != nil {
  152. err = fmt.Errorf("http create request err: %s", e.Error())
  153. return
  154. }
  155. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  156. contentType := "application/json;charset=utf-8"
  157. req.Header.Set("Content-Type", contentType)
  158. req.Header.Set("Authorization", checkToken)
  159. resp, e := client.Do(req)
  160. if e != nil {
  161. err = fmt.Errorf("http client do err: %s", e.Error())
  162. return
  163. }
  164. defer func() {
  165. _ = resp.Body.Close()
  166. }()
  167. b, e := ioutil.ReadAll(resp.Body)
  168. if e != nil {
  169. err = fmt.Errorf("resp body read err: %s", e.Error())
  170. return
  171. }
  172. if len(b) == 0 {
  173. err = fmt.Errorf("resp body is empty")
  174. return
  175. }
  176. // 生产环境解密
  177. if utils.RunMode == "release" {
  178. str := string(b)
  179. str = strings.Trim(str, `"`)
  180. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  181. }
  182. result := new(models.BridgeJiaYueResultMenuListData)
  183. if e = json.Unmarshal(b, &result); e != nil {
  184. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  185. return
  186. }
  187. if result.Code != 200 {
  188. err = fmt.Errorf("result: %s", string(b))
  189. return
  190. }
  191. indexData = result.Data
  192. return
  193. }
  194. // SyncJiaYueNewIndex 同步嘉悦增量指标
  195. func SyncJiaYueNewIndex(item models.BridgeJiaYueIndexAndData, menus []models.BridgeJiaYueIndexMenuData) (err error) {
  196. defer func() {
  197. if err != nil {
  198. b, _ := json.Marshal(item)
  199. tips := fmt.Sprintf("SyncJiaYueNewIndex同步增量指标失败, Err: %s\nIndex: %s", err.Error(), string(b))
  200. utils.FileLog.Info(tips)
  201. go alarm_msg.SendAlarmMsg(tips, 3)
  202. }
  203. }()
  204. subSource := utils.DATA_SUB_SOURCE_EDB
  205. // 校验指标来源
  206. sourceId := models.EdbSourceExtendIdMap[item.SourceType]
  207. if sourceId <= 0 {
  208. utils.FileLog.Info(fmt.Sprintf("指标来源ID有误, 忽略同步, sourceId: %d; sourceType: %s", sourceId, item.SourceType))
  209. return
  210. }
  211. edbSource := models.EdbSourceIdMap[sourceId]
  212. if edbSource == nil {
  213. utils.FileLog.Info(fmt.Sprintf("指标来源有误, sourceId: %d", sourceId))
  214. return
  215. }
  216. sourceTableName := edbSource.TableName
  217. if sourceTableName == "" {
  218. utils.FileLog.Info(fmt.Sprintf("指标数据表有误, 忽略同步, sourceId: %d", sourceId))
  219. return
  220. }
  221. // 指标编码
  222. indexCode := ""
  223. if edbSource.EdbCodeRequired == 1 {
  224. indexCode = item.IndexCode
  225. } else {
  226. indexCode = strconv.Itoa(item.Id)
  227. }
  228. if indexCode == "" {
  229. utils.FileLog.Info("指标编码不存在")
  230. return
  231. }
  232. // 查询指标是否已存在, 存在则忽略
  233. exist, e := models.GetEdbInfoByEdbCode(sourceId, indexCode)
  234. if e != nil && e.Error() != utils.ErrNoRow() {
  235. err = fmt.Errorf("查询指标是否存在失败, err: %s", e.Error())
  236. return
  237. }
  238. if exist != nil && exist.EdbInfoId > 0 {
  239. utils.FileLog.Info(fmt.Sprintf("指标%s已存在, 忽略同步", indexCode))
  240. return
  241. }
  242. // 从桥接服务获取指标和数据
  243. var params models.BridgeJiaYueIndexDataParams
  244. params.IndexCode = indexCode
  245. params.SourceExtend = item.SourceType
  246. params.StartDate = utils.BASE_START_DATE
  247. params.EndDate = utils.BASE_END_DATE
  248. params.IndexCodeRequired = edbSource.EdbCodeRequired
  249. indexData, e := GetJiaYueIndexDataFromBridge(params)
  250. if e != nil {
  251. err = fmt.Errorf("GetJiaYueIndexDataFromBridge err: %s", e.Error())
  252. return
  253. }
  254. // 没找到数据, 频度有误的忽略掉
  255. if indexData.Id <= 0 {
  256. utils.FileLog.Info(fmt.Sprintf("SyncJiaYueNewIndex indexCode empty, IndexCode: %s", indexCode))
  257. return
  258. }
  259. frequency := TransJiaYueFrequency(item.Frequency)
  260. if frequency == "" {
  261. utils.FileLog.Info(fmt.Sprintf("SyncJiaYueNewIndex frequency empty, Frequency: %s", item.Frequency))
  262. return
  263. }
  264. // 获取指标目录对应的所有父级目录
  265. parentId := 0
  266. parentMenus := GetJiaYueParentMenusByMenu(indexData.MenuData, menus, 0)
  267. level := len(parentMenus)
  268. if level > 0 {
  269. // 将父级目录按照Level倒序排序(因为是从子目录往上找的, 所以Level最大的对应为顶级目录), 再进行遍历
  270. sort.Slice(parentMenus, func(i, j int) bool {
  271. return parentMenus[i].Level > parentMenus[j].Level
  272. })
  273. for _, p := range parentMenus {
  274. l := level - p.Level - 1
  275. c, _, msg := models.SaveEdbClassify(p.Menu.Name, parentId, l, 0, utils.InitAdminId, utils.InitAdminName)
  276. if msg != "" {
  277. err = fmt.Errorf("SaveEdbClassify err: %s", msg)
  278. return
  279. }
  280. parentId = c.ClassifyId
  281. }
  282. }
  283. classify, _, msg := models.SaveEdbClassify(indexData.MenuData.Name, parentId, level, 0, utils.InitAdminId, utils.InitAdminName)
  284. if msg != "" {
  285. err = fmt.Errorf("SaveEdbClassify err: %s", msg)
  286. return
  287. }
  288. // 新增指标
  289. addParams := new(models.AddEdbInfoParams)
  290. addParams.EdbCode = indexCode
  291. addParams.EdbName = item.IndexName
  292. addParams.Frequency = frequency
  293. addParams.Unit = item.Unit
  294. addParams.ClassifyId = classify.ClassifyId
  295. addParams.Source = sourceId
  296. adminId, _ := strconv.Atoi(utils.InitAdminId)
  297. edbInfo, e := models.EdbInfoAdd(addParams, "", adminId, utils.InitAdminName)
  298. if e != nil {
  299. err = fmt.Errorf("EdbInfoAdd err: %s", e.Error())
  300. return
  301. }
  302. // 刷新指标数据
  303. e = models.RefreshEdbDataFromJiaYue(sourceId, subSource, edbInfo.EdbInfoId, sourceTableName, edbInfo.EdbCode, params.StartDate, indexData.IndexData)
  304. if e != nil {
  305. err = fmt.Errorf("RefreshEdbDataFromJiaYue err: %s", e.Error())
  306. return
  307. }
  308. // 更新指标最大最小值
  309. e, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  310. if e != nil {
  311. err = fmt.Errorf("UnifiedModifyEdbInfoMaxAndMinInfo err: %s, errMsg: %s", e.Error(), errMsg)
  312. return
  313. }
  314. // 更新ES
  315. go logic.UpdateEs(edbInfo.EdbInfoId)
  316. return
  317. }
  318. // TransJiaYueFrequency 频度转换
  319. func TransJiaYueFrequency(origin string) string {
  320. mapping := map[string]string{
  321. "日": "日度",
  322. "周": "周度",
  323. "旬": "旬度",
  324. "半月": "旬度",
  325. "月": "月度",
  326. "季": "季度",
  327. "半年": "半年度",
  328. "年": "年度",
  329. "日度": "日度",
  330. "周度": "周度",
  331. "旬度": "旬度",
  332. "月度": "月度",
  333. "季度": "季度",
  334. "半年度": "半年度",
  335. "年度": "年度",
  336. }
  337. return mapping[origin]
  338. }
  339. // GetJiaYueParentMenusByMenu 获取指定目录的父级目录
  340. func GetJiaYueParentMenusByMenu(menu models.BridgeJiaYueIndexMenuData, menus []models.BridgeJiaYueIndexMenuData, level int) (results []models.BridgeJiaYueIndexMenuWithLevel) {
  341. results = make([]models.BridgeJiaYueIndexMenuWithLevel, 0)
  342. for _, m := range menus {
  343. if menu.ParentId == m.Id {
  344. results = append(results, models.BridgeJiaYueIndexMenuWithLevel{
  345. Level: level,
  346. Menu: m,
  347. })
  348. ps := GetJiaYueParentMenusByMenu(m, menus, level+1)
  349. if len(ps) > 0 {
  350. results = append(results, ps...)
  351. }
  352. }
  353. }
  354. return
  355. }