jiayue_platform.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. package index_data
  2. import (
  3. "eta/eta_bridge/global"
  4. "eta/eta_bridge/models/jiayue"
  5. "eta/eta_bridge/models/response"
  6. "eta/eta_bridge/services/alarm_msg"
  7. "eta/eta_bridge/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // GetIndexFromJiaYue 获取嘉悦指标信息
  14. func GetIndexFromJiaYue(indexCode string, sourceArr []string, indexCodeRequired int) (data *response.IndexResp, err error) {
  15. defer func() {
  16. if err != nil {
  17. global.LOG.Info("GetIndexFromJiaYue Err: " + err.Error())
  18. go alarm_msg.SendAlarmMsg("GetIndexFromJiaYue Err: "+err.Error(), 3)
  19. return
  20. }
  21. }()
  22. indexCond := ``
  23. indexPars := make([]interface{}, 0)
  24. if indexCodeRequired == 1 {
  25. indexCond = ` SOURCE_CODE = :1 `
  26. indexPars = append(indexPars, indexCode)
  27. } else {
  28. // 以ID为指标编码
  29. indexId, _ := strconv.Atoi(indexCode)
  30. if indexId <= 0 {
  31. global.LOG.Info(indexCode + "指标ID不存在")
  32. return
  33. }
  34. indexCond = ` ID = :1`
  35. indexPars = append(indexPars, indexId)
  36. }
  37. if len(sourceArr) > 0 {
  38. indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, utils.GetOracleInReplace(len(sourceArr)))
  39. for _, s := range sourceArr {
  40. indexPars = append(indexPars, s)
  41. }
  42. }
  43. indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "")
  44. if e != nil {
  45. err = fmt.Errorf("GetDictIndex err: %s", e.Error())
  46. return
  47. }
  48. if len(indexes) <= 0 {
  49. global.LOG.Info(indexCode + "指标信息不存在")
  50. return
  51. }
  52. index := indexes[0]
  53. if index.Id <= 0 {
  54. err = fmt.Errorf("指标信息有误")
  55. return
  56. }
  57. var (
  58. indexData []jiayue.DictData
  59. dictMenu []jiayue.DictCategory
  60. )
  61. data = FormatJiaYueIndexItem2Resp(index, indexData, dictMenu)
  62. return
  63. }
  64. // GetIndexAndDataFromJiaYue 获取指标和数据
  65. func GetIndexAndDataFromJiaYue(indexCode, startDate, endDate string, sourceArr []string, indexCodeRequired int) (data *response.IndexResp, err error) {
  66. defer func() {
  67. if err != nil {
  68. global.LOG.Info("GetIndexAndDataFromJiaYue Err: " + err.Error())
  69. go alarm_msg.SendAlarmMsg("GetIndexAndDataFromJiaYue Err: "+err.Error(), 3)
  70. return
  71. }
  72. }()
  73. indexCond := ``
  74. indexPars := make([]interface{}, 0)
  75. if indexCodeRequired == 1 {
  76. indexCond = ` SOURCE_CODE = :1 `
  77. indexPars = append(indexPars, indexCode)
  78. } else {
  79. // 以ID为指标编码
  80. indexId, _ := strconv.Atoi(indexCode)
  81. if indexId <= 0 {
  82. global.LOG.Info(indexCode + "指标ID不存在")
  83. return
  84. }
  85. indexCond = ` ID = :1`
  86. indexPars = append(indexPars, indexId)
  87. }
  88. if len(sourceArr) > 0 {
  89. indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, utils.GetOracleInReplace(len(sourceArr)))
  90. for _, s := range sourceArr {
  91. indexPars = append(indexPars, s)
  92. }
  93. }
  94. indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "")
  95. if e != nil {
  96. err = fmt.Errorf("GetDictIndex err: %s", e.Error())
  97. return
  98. }
  99. if len(indexes) <= 0 {
  100. global.LOG.Info(indexCode + "指标信息不存在")
  101. return
  102. }
  103. index := indexes[0]
  104. if index.Id <= 0 {
  105. err = fmt.Errorf("指标信息有误")
  106. return
  107. }
  108. if index.TableName == "" {
  109. err = fmt.Errorf("指标信息数据表名为空")
  110. return
  111. }
  112. // 指标数据
  113. dataCond := ` INDEX_ID = :1`
  114. dataPars := make([]interface{}, 0)
  115. dataPars = append(dataPars, index.Id)
  116. if startDate != "" {
  117. dataCond += ` AND INDEX_DATE >= TO_DATE (:1, 'yyyy-mm-dd')`
  118. dataPars = append(dataPars, startDate)
  119. }
  120. if endDate != "" {
  121. dataCond += ` AND INDEX_DATE <= TO_DATE (:1, 'yyyy-mm-dd')`
  122. dataPars = append(dataPars, endDate)
  123. }
  124. indexData, e := jiayue.GetDictData(index.TableName, dataCond, dataPars)
  125. if e != nil {
  126. err = fmt.Errorf("GetDictData err: %s", e.Error())
  127. return
  128. }
  129. // 指标目录
  130. menuCond := ` R.INDEX_ID = :1`
  131. menuPars := make([]interface{}, 0)
  132. menuPars = append(menuPars, index.Id)
  133. menus, e := jiayue.GetIndexCategory(menuCond, menuPars, "")
  134. if e != nil {
  135. err = fmt.Errorf("GetDictCategory err: %s", e.Error())
  136. return
  137. }
  138. data = FormatJiaYueIndexItem2Resp(index, indexData, menus)
  139. return
  140. }
  141. // GetPageIndexesFromJiaYue 分页获取指标数据
  142. func GetPageIndexesFromJiaYue(pageIndex, pageSize int, sourceArr []string, keyword, frequency string, sortField, sortRule int) (total int, result []jiayue.DictIndex, err error) {
  143. defer func() {
  144. if err != nil {
  145. global.LOG.Info("GetPageIndexesFromJiaYue Err: " + err.Error())
  146. go alarm_msg.SendAlarmMsg("GetPageIndexesFromJiaYue Err: "+err.Error(), 3)
  147. return
  148. }
  149. }()
  150. indexCond := ` 1=1`
  151. indexPars := make([]interface{}, 0)
  152. // 筛选项
  153. if len(sourceArr) > 0 {
  154. indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, utils.GetOracleInReplace(len(sourceArr)))
  155. for _, s := range sourceArr {
  156. indexPars = append(indexPars, s)
  157. }
  158. }
  159. keyword = strings.TrimSpace(keyword)
  160. if keyword != "" {
  161. kw := fmt.Sprint("%", keyword, "%")
  162. indexCond += ` AND (ID LIKE :1 OR SOURCE_CODE LIKE :1 OR NAME LIKE :1)`
  163. indexPars = append(indexPars, kw, kw, kw)
  164. }
  165. if frequency != "" {
  166. indexCond += ` AND FREQUENCY = :1`
  167. indexPars = append(indexPars, frequency)
  168. }
  169. // 总记录数
  170. t, e := jiayue.GetDictIndexCount(indexCond, indexPars)
  171. if e != nil {
  172. err = fmt.Errorf("GetDictIndexCount err: %s", e.Error())
  173. return
  174. }
  175. total = t
  176. // 列表数据
  177. indexes, e := jiayue.GetDictPageIndex(indexCond, indexPars, pageIndex, pageSize, sortField, sortRule)
  178. if e != nil {
  179. err = fmt.Errorf("GetDictPageIndex err: %s", e.Error())
  180. return
  181. }
  182. result = indexes
  183. return
  184. }
  185. // GetNewIndexAndDataFromJiaYue 获取就近新增的指标和数据
  186. func GetNewIndexAndDataFromJiaYue() (indexList []*response.IndexResp, err error) {
  187. defer func() {
  188. if err != nil {
  189. global.LOG.Info("GetNewIndexAndDataFromJiaYue Err: " + err.Error())
  190. go alarm_msg.SendAlarmMsg("GetNewIndexAndDataFromJiaYue Err: "+err.Error(), 3)
  191. return
  192. }
  193. }()
  194. indexCond := ` 1=1`
  195. indexPars := make([]interface{}, 0)
  196. // 同步多久前至今的新指标
  197. m := global.CONFIG.Business.JiaYueIndexSyncMinute
  198. if m == 0 {
  199. m = -30 // 默认半小时
  200. }
  201. timeBefore := time.Now().Local().Add(time.Duration(m) * time.Minute).Format(utils.FormatDateTime)
  202. indexCond += ` AND CREATE_TIME >= TO_DATE (:1, 'yyyy-mm-dd hh24:mi:ss') `
  203. indexPars = append(indexPars, timeBefore)
  204. // 测试
  205. //endTime := time.Date(2023, 6, 8, 0, 0, 0, 0, time.Local).Format(utils.FormatDateTime)
  206. //indexCond += fmt.Sprintf(`CREATE_TIME <= TO_DATE ('%s', 'yyyy-mm-dd hh24:mi:ss')`, endTime)
  207. //sourceArr := []string{"bloomberg", "bloomberg_tmp"}
  208. //if len(sourceArr) > 0 {
  209. // str := ``
  210. // for _, s := range sourceArr {
  211. // str += fmt.Sprintf(`'%s',`, s)
  212. // }
  213. // str = strings.TrimRight(str, ",")
  214. // indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, str)
  215. //}
  216. //indexCond = ` ID = :1`
  217. //indexPars = append(indexPars, 180399)
  218. indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "CREATE_TIME ASC")
  219. if e != nil {
  220. err = fmt.Errorf("GetDictIndex err: %s", e.Error())
  221. return
  222. }
  223. if len(indexes) <= 0 {
  224. global.LOG.Info("无新指标需要同步")
  225. return
  226. }
  227. // 每个指标对应的数据表不定, 所以这里还是用循环去查
  228. indexList = make([]*response.IndexResp, 0)
  229. for _, v := range indexes {
  230. // 指标数据
  231. //dataCond := " INDEX_ID = :1"
  232. //dataPars := make([]interface{}, 0)
  233. //dataPars = append(dataPars, v.Id)
  234. //indexData, e := jiayue.GetDictData(v.TableName, dataCond, dataPars)
  235. //if e != nil {
  236. // err = fmt.Errorf("GetDictData err: %s", e.Error())
  237. // return
  238. //}
  239. //
  240. // 指标目录
  241. //menuCond := ` R.INDEX_ID = :1`
  242. //menuPars := make([]interface{}, 0)
  243. //menuPars = append(menuPars, v.Id)
  244. //menus, e := jiayue.GetIndexCategory(menuCond, menuPars, "")
  245. //if e != nil {
  246. // err = fmt.Errorf("GetDictCategory err: %s", e.Error())
  247. // return
  248. //}
  249. var (
  250. indexData []jiayue.DictData
  251. dictMenu []jiayue.DictCategory
  252. )
  253. item := FormatJiaYueIndexItem2Resp(v, indexData, dictMenu)
  254. indexList = append(indexList, item)
  255. }
  256. return
  257. }
  258. // FormatJiaYueIndexItem2Resp 格式化指标响应体
  259. func FormatJiaYueIndexItem2Resp(item jiayue.DictIndex, dictData []jiayue.DictData, dictMenu []jiayue.DictCategory) (res *response.IndexResp) {
  260. res = new(response.IndexResp)
  261. res.Id = item.Id
  262. res.IndexCode = item.SourceCode // 注意这里的Code取的是表中的SOURCE_CODE
  263. res.IndexName = item.Name
  264. res.SourceType = item.SourceType
  265. res.Unit = item.Unit
  266. res.Frequency = item.Frequency
  267. res.LastDate = item.DateLast
  268. res.LastUpdateTime = item.TimeLastUpdate
  269. res.Status = item.Status
  270. if len(dictData) > 0 {
  271. for _, d := range dictData {
  272. res.IndexData = append(res.IndexData, response.IndexData{
  273. Val: d.IndexValue,
  274. DataTime: d.IndexDate,
  275. UpdateTime: d.UpdateTime,
  276. })
  277. }
  278. }
  279. if len(dictMenu) > 0 {
  280. firstMenu := dictMenu[0]
  281. r := FormatJiaYueDictCategory2Resp(firstMenu)
  282. res.MenuData = *r
  283. }
  284. return
  285. }
  286. // FormatJiaYueDictCategory2Resp 格式化指标目录响应体
  287. func FormatJiaYueDictCategory2Resp(item jiayue.DictCategory) (res *response.IndexMenuData) {
  288. res = new(response.IndexMenuData)
  289. res.Id = item.Id
  290. res.Type = item.Type
  291. res.Code = item.Code
  292. res.Name = item.Name
  293. res.Icon = item.Icon
  294. res.Sort = item.Sorting
  295. res.ParentId = item.ParentId
  296. res.ParentName = item.ParentName
  297. res.Path = item.Path
  298. return
  299. }
  300. type JiaYueImportIndexResp struct {
  301. FirstMenu string `description:"一级目录"`
  302. SecondMenu string `description:"二级目录"`
  303. ThirdMenu string `description:"三级目录"`
  304. FourthMenu string `description:"四级目录"`
  305. FifthMenu string `description:"五级目录"`
  306. SixthMenu string `description:"六级目录"`
  307. Id int `description:"指标ID"`
  308. IndexName string `description:"指标名称"`
  309. IndexCode string `description:"指标编码"`
  310. Unit string `description:"单位"`
  311. Frequency string `description:"频度"`
  312. SourceType string `description:"来源"`
  313. }
  314. func GetJiaYueImportData(sourceArr []string) (items []JiaYueImportIndexResp, err error) {
  315. defer func() {
  316. if err != nil {
  317. global.LOG.Info("GetJiaYueImportData Err: " + err.Error())
  318. go alarm_msg.SendAlarmMsg("GetJiaYueImportData Err: "+err.Error(), 3)
  319. return
  320. }
  321. }()
  322. // 获取指标
  323. indexCond := ` 1=1`
  324. indexPars := make([]interface{}, 0)
  325. if len(sourceArr) == 0 {
  326. sourceArr = []string{"webisite", "website", "website_gf", "platts", "reuter", "reuter_vessel_q", "路透", "bloomberg", "bloomberg_tmp", "wind", "wind_p", "wind_stop", "wind_tmp"}
  327. }
  328. indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, utils.GetOracleInReplace(len(sourceArr)))
  329. if len(sourceArr) > 0 {
  330. for _, s := range sourceArr {
  331. indexPars = append(indexPars, s)
  332. }
  333. }
  334. indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "SOURCE_TYPE ASC, ID ASC")
  335. if e != nil {
  336. err = fmt.Errorf("GetDictIndex err: %s", e.Error())
  337. return
  338. }
  339. if len(indexes) <= 0 {
  340. global.LOG.Info("无指标需要导入")
  341. return
  342. }
  343. index := indexes[0]
  344. if index.Id <= 0 {
  345. err = fmt.Errorf("指标信息有误")
  346. return
  347. }
  348. if index.TableName == "" {
  349. err = fmt.Errorf("指标信息数据表名为空")
  350. return
  351. }
  352. // 获取全部目录
  353. categories, e := jiayue.GetDictCategory(``, make([]interface{}, 0), "")
  354. if e != nil {
  355. err = fmt.Errorf("获取嘉业指标目录列表失败, err: %s", e.Error())
  356. return
  357. }
  358. categoryMap := make(map[int]jiayue.DictCategory)
  359. for _, v := range categories {
  360. categoryMap[v.Id] = v
  361. }
  362. items = make([]JiaYueImportIndexResp, 0)
  363. for _, v := range indexes {
  364. var t JiaYueImportIndexResp
  365. t.Id = v.Id
  366. t.IndexName = v.Name
  367. t.IndexCode = v.SourceCode
  368. t.Unit = v.Unit
  369. t.Frequency = v.Frequency
  370. t.SourceType = v.SourceType
  371. // 目录
  372. menuCond := ` R.INDEX_ID = :1`
  373. menuPars := make([]interface{}, 0)
  374. menuPars = append(menuPars, t.Id)
  375. menus, e := jiayue.GetIndexCategory(menuCond, menuPars, "")
  376. if e != nil {
  377. err = fmt.Errorf("GetDictCategory err: %s", e.Error())
  378. return
  379. }
  380. if len(menus) == 0 {
  381. continue
  382. }
  383. menu := menus[0]
  384. path := strings.TrimLeft(menu.Path, "/") // "path": "/61293/67576/67577/67579/67583"
  385. menuIds := make([]int, 0)
  386. menuIdArr := strings.Split(path, "/")
  387. for _, m := range menuIdArr {
  388. mid, _ := strconv.Atoi(m)
  389. menuIds = append(menuIds, mid)
  390. }
  391. // 取出目录名称
  392. for k, m := range menuIds {
  393. mu := categoryMap[m]
  394. if mu.Id <= 0 {
  395. continue
  396. }
  397. if k == 0 {
  398. t.FirstMenu = mu.Name
  399. }
  400. if k == 1 {
  401. t.SecondMenu = mu.Name
  402. }
  403. if k == 2 {
  404. t.ThirdMenu = mu.Name
  405. }
  406. if k == 3 {
  407. t.FourthMenu = mu.Name
  408. }
  409. if k == 4 {
  410. t.FifthMenu = mu.Name
  411. }
  412. if k == 5 {
  413. t.SixthMenu = mu.Name
  414. }
  415. }
  416. items = append(items, t)
  417. }
  418. return
  419. }