sso_eic.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_crawler/models"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "strconv"
  8. "time"
  9. )
  10. type EicListing struct {
  11. Name string `json:"name"`
  12. ShortName string `json:"short_name"`
  13. Type string `json:"type"`
  14. Eic string `json:"eic"`
  15. Country string `json:"country"`
  16. Url string `json:"url"`
  17. Facilities []Facilities `json:"facilities"`
  18. }
  19. type Facilities struct {
  20. Name string `json:"name"`
  21. Type string `json:"type"`
  22. Eic string `json:"eic"`
  23. Country string `json:"country"`
  24. Company string `json:"company"`
  25. Url string `json:"url"`
  26. }
  27. type Storage struct {
  28. Status string `json:"status"`
  29. GasDayStartedOn string `json:"gasDayStartedOn"`
  30. GasInStorage string `json:"gasInStorage"`
  31. Full string `json:"full"`
  32. Trend string `json:"trend"`
  33. Injection string `json:"injection"`
  34. Withdrawal string `json:"withdrawal"`
  35. WorkingGasVolume string `json:"workingGasVolume"`
  36. InjectionCapacity string `json:"injectionCapacity"`
  37. WithdrawalCapacity string `json:"withdrawalCapacity"`
  38. Info string `json:"info"`
  39. }
  40. var eicIndexCodeMap = make(map[string]string)
  41. func SyncStorageFromEic() {
  42. allCode, err := models.GetIndexCodeFromMapping("Eic")
  43. if err != nil {
  44. fmt.Println("select Code err:", err)
  45. }
  46. for _, item := range allCode {
  47. eicIndexCodeMap[item.IndexName] = item.IndexCode
  48. }
  49. baseUrl := "https://agsi.gie.eu/api/eic-listing/SSO/view"
  50. body, err := http.Get(baseUrl)
  51. if err != nil {
  52. fmt.Println("GetData Err:" + err.Error())
  53. return
  54. }
  55. var eicListing []EicListing
  56. err = json.Unmarshal(body, &eicListing)
  57. for _, sso := range eicListing {
  58. var codeMapList []*models.BaseFromTradeMapping
  59. existIndexMap := make(map[string]*models.BaseFromTradeEicIndex)
  60. //获取所有指标信息
  61. allIndex, err := models.GetSSOFromEicIndexAll(sso.Name)
  62. if err != nil {
  63. fmt.Println("select err:", err)
  64. }
  65. for _, v := range allIndex {
  66. existIndexMap[v.GasDayStartedOn+v.Name] = v
  67. }
  68. ssoUrl := sso.Url
  69. ssoBody, err := http.Get(ssoUrl)
  70. fmt.Println("ssoUrl:", ssoUrl)
  71. if err != nil {
  72. fmt.Println("GetData Err:" + err.Error())
  73. continue
  74. }
  75. var ssoStorages []Storage
  76. err = json.Unmarshal(ssoBody, &ssoStorages)
  77. //SSO item
  78. var ssoItems []*models.BaseFromTradeEicIndex
  79. for _, storageItem := range ssoStorages {
  80. gasInStorage, _ := strconv.ParseFloat(storageItem.GasInStorage, 64)
  81. full, _ := strconv.ParseFloat(storageItem.Full, 64)
  82. trend, _ := strconv.ParseFloat(storageItem.Trend, 64)
  83. injection, _ := strconv.ParseFloat(storageItem.Injection, 64)
  84. withdrawal, _ := strconv.ParseFloat(storageItem.Withdrawal, 64)
  85. workingGasVolume, _ := strconv.ParseFloat(storageItem.WorkingGasVolume, 64)
  86. injectionCapacity, _ := strconv.ParseFloat(storageItem.InjectionCapacity, 64)
  87. withdrawalCapacity, _ := strconv.ParseFloat(storageItem.WithdrawalCapacity, 64)
  88. ssoItem := models.BaseFromTradeEicIndex{
  89. BaseFromEicIndexId: 0,
  90. Country: sso.Country,
  91. Type: sso.Type,
  92. EicCode: sso.Country + sso.Eic,
  93. ShortName: sso.ShortName,
  94. Name: sso.Name,
  95. Status: storageItem.Status,
  96. GasDayStartedOn: storageItem.GasDayStartedOn,
  97. GasInStorage: gasInStorage,
  98. GasInStorageCode: sso.Country + sso.Eic + "GS",
  99. Full: full,
  100. FullCode: sso.Country + sso.Eic + "F",
  101. Trend: trend,
  102. TrendCode: sso.Country + sso.Eic + "T",
  103. Injection: injection,
  104. InjectionCode: sso.Country + sso.Eic + "In",
  105. Withdrawal: withdrawal,
  106. WithdrawalCode: sso.Country + sso.Eic + "Out",
  107. WorkingGasVolume: workingGasVolume,
  108. WorkingGasVolumeCode: sso.Country + sso.Eic + "WGV",
  109. InjectionCapacity: injectionCapacity,
  110. InjectionCapacityCode: sso.Country + sso.Eic + "IC",
  111. WithdrawalCapacity: withdrawalCapacity,
  112. WithdrawalCapacityCode: sso.Country + sso.Eic + "WC",
  113. Info: storageItem.Info,
  114. CreateTime: time.Now(),
  115. ModifyTime: time.Now(),
  116. }
  117. itemVerifyCode := ssoItem.GasInStorage + ssoItem.Full + ssoItem.Trend + ssoItem.Injection + ssoItem.Withdrawal
  118. if existIndex, ok := existIndexMap[ssoItem.GasDayStartedOn+ssoItem.Name]; !ok {
  119. ssoItems = append(ssoItems, &ssoItem)
  120. existIndexMap[ssoItem.GasDayStartedOn+ssoItem.Name] = &ssoItem
  121. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  122. //更新
  123. err := models.ModifyBaseFromEicIndex(ssoItem.GasInStorage, ssoItem.Full, ssoItem.Trend, ssoItem.Injection, ssoItem.Withdrawal, existIndex.BaseFromEicIndexId)
  124. if err != nil {
  125. fmt.Println("data update err:", err)
  126. }
  127. }
  128. }
  129. if _, ok := eicIndexCodeMap[sso.Country+"_"+sso.Name]; !ok {
  130. codeMappingItem := models.BaseFromTradeMapping{
  131. BaseFromTradeMappingId: 0,
  132. IndexName: sso.Country + "_" + sso.Name,
  133. IndexCode: sso.Country + sso.Eic,
  134. Exchange: "EIC",
  135. }
  136. codeMapList = append(codeMapList, &codeMappingItem)
  137. eicIndexCodeMap[sso.Country+"_"+sso.Name] = sso.Country + sso.Eic
  138. }
  139. if _, ok := eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"GS"]; !ok {
  140. codeMappingItem := models.BaseFromTradeMapping{
  141. BaseFromTradeMappingId: 0,
  142. IndexName: sso.Country + "_" + sso.Name + "_" + "GS",
  143. IndexCode: sso.Country + sso.Eic + "GS",
  144. Exchange: "EIC",
  145. }
  146. codeMapList = append(codeMapList, &codeMappingItem)
  147. eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"GS"] = sso.Country + sso.Eic + "GS"
  148. }
  149. if _, ok := eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"F"]; !ok {
  150. codeMappingItem := models.BaseFromTradeMapping{
  151. BaseFromTradeMappingId: 0,
  152. IndexName: sso.Country + "_" + sso.Name + "_" + "F",
  153. IndexCode: sso.Country + sso.Eic + "F",
  154. Exchange: "EIC",
  155. }
  156. codeMapList = append(codeMapList, &codeMappingItem)
  157. eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"F"] = sso.Country + sso.Eic + "F"
  158. }
  159. if _, ok := eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"T"]; !ok {
  160. codeMappingItem := models.BaseFromTradeMapping{
  161. BaseFromTradeMappingId: 0,
  162. IndexName: sso.Country + "_" + sso.Name + "_" + "T",
  163. IndexCode: sso.Country + sso.Eic + "T",
  164. Exchange: "EIC",
  165. }
  166. codeMapList = append(codeMapList, &codeMappingItem)
  167. eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"T"] = sso.Country + sso.Eic + "T"
  168. }
  169. if _, ok := eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"In"]; !ok {
  170. codeMappingItem := models.BaseFromTradeMapping{
  171. BaseFromTradeMappingId: 0,
  172. IndexName: sso.Country + "_" + sso.Name + "_" + "In",
  173. IndexCode: sso.Country + sso.Eic + "In",
  174. Exchange: "EIC",
  175. }
  176. codeMapList = append(codeMapList, &codeMappingItem)
  177. eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"In"] = sso.Country + sso.Eic + "In"
  178. }
  179. if _, ok := eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"Out"]; !ok {
  180. codeMappingItem := models.BaseFromTradeMapping{
  181. BaseFromTradeMappingId: 0,
  182. IndexName: sso.Country + "_" + sso.Name + "_" + "Out",
  183. IndexCode: sso.Country + sso.Eic + "Out",
  184. Exchange: "EIC",
  185. }
  186. codeMapList = append(codeMapList, &codeMappingItem)
  187. eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"Out"] = sso.Country + sso.Eic + "Out"
  188. }
  189. if _, ok := eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"WGV"]; !ok {
  190. codeMappingItem := models.BaseFromTradeMapping{
  191. BaseFromTradeMappingId: 0,
  192. IndexName: sso.Country + "_" + sso.Name + "_" + "WGV",
  193. IndexCode: sso.Country + sso.Eic + "WGV",
  194. Exchange: "EIC",
  195. }
  196. codeMapList = append(codeMapList, &codeMappingItem)
  197. eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"WGV"] = sso.Country + sso.Eic + "WGV"
  198. }
  199. if _, ok := eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"IC"]; !ok {
  200. codeMappingItem := models.BaseFromTradeMapping{
  201. BaseFromTradeMappingId: 0,
  202. IndexName: sso.Country + "_" + sso.Name + "_" + "IC",
  203. IndexCode: sso.Country + sso.Eic + "IC",
  204. Exchange: "EIC",
  205. }
  206. codeMapList = append(codeMapList, &codeMappingItem)
  207. eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"IC"] = sso.Country + sso.Eic + "IC"
  208. }
  209. if _, ok := eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"WC"]; !ok {
  210. codeMappingItem := models.BaseFromTradeMapping{
  211. BaseFromTradeMappingId: 0,
  212. IndexName: sso.Country + "_" + sso.Name + "_" + "WC",
  213. IndexCode: sso.Country + sso.Eic + "WC",
  214. Exchange: "EIC",
  215. }
  216. codeMapList = append(codeMapList, &codeMappingItem)
  217. eicIndexCodeMap[sso.Country+"_"+sso.Name+"_"+"WC"] = sso.Country + sso.Eic + "WC"
  218. }
  219. if len(ssoItems) != 0 {
  220. successNums, err := models.AddEicDataMulti(ssoItems)
  221. fmt.Println("SSO successNums:", successNums)
  222. if err != nil {
  223. fmt.Println("AddSSODataMulti err:", err)
  224. }
  225. }
  226. facilities := sso.Facilities
  227. var facItems []*models.BaseFromTradeEicIndex
  228. for _, facility := range facilities {
  229. //获取所有指标信息
  230. allFacIndex, err := models.GetFacFromEicIndexAll(facility.Name)
  231. if err != nil {
  232. fmt.Println("select err:", err)
  233. }
  234. existFacIndexMap := make(map[string]*models.BaseFromTradeEicIndex)
  235. for _, v := range allFacIndex {
  236. existFacIndexMap[v.Name+v.GasDayStartedOn] = v
  237. }
  238. facUrl := facility.Url
  239. facBody, err := http.Get(ssoUrl)
  240. fmt.Println("facUrl:", facUrl)
  241. if err != nil {
  242. fmt.Println("GetData Err:" + err.Error())
  243. continue
  244. }
  245. var facStorages []Storage
  246. err = json.Unmarshal(facBody, &facStorages)
  247. for _, storageItem := range facStorages {
  248. gasInStorage, _ := strconv.ParseFloat(storageItem.GasInStorage, 64)
  249. full, _ := strconv.ParseFloat(storageItem.Full, 64)
  250. trend, _ := strconv.ParseFloat(storageItem.Trend, 64)
  251. injection, _ := strconv.ParseFloat(storageItem.Injection, 64)
  252. withdrawal, _ := strconv.ParseFloat(storageItem.Withdrawal, 64)
  253. workingGasVolume, _ := strconv.ParseFloat(storageItem.WorkingGasVolume, 64)
  254. injectionCapacity, _ := strconv.ParseFloat(storageItem.InjectionCapacity, 64)
  255. withdrawalCapacity, _ := strconv.ParseFloat(storageItem.WithdrawalCapacity, 64)
  256. facItem := models.BaseFromTradeEicIndex{
  257. BaseFromEicIndexId: 0,
  258. Country: facility.Country,
  259. Type: facility.Type,
  260. EicCode: facility.Country + facility.Eic,
  261. ShortName: sso.ShortName,
  262. Name: facility.Name,
  263. Status: storageItem.Status,
  264. GasDayStartedOn: storageItem.GasDayStartedOn,
  265. GasInStorage: gasInStorage,
  266. GasInStorageCode: facility.Country + facility.Eic + "GS",
  267. Full: full,
  268. FullCode: facility.Country + facility.Eic + "F",
  269. Trend: trend,
  270. TrendCode: facility.Country + facility.Eic + "T",
  271. Injection: injection,
  272. InjectionCode: facility.Country + facility.Eic + "In",
  273. Withdrawal: withdrawal,
  274. WithdrawalCode: facility.Country + facility.Eic + "Out",
  275. WorkingGasVolume: workingGasVolume,
  276. WorkingGasVolumeCode: facility.Country + facility.Eic + "WGV",
  277. InjectionCapacity: injectionCapacity,
  278. InjectionCapacityCode: facility.Country + facility.Eic + "IC",
  279. WithdrawalCapacity: withdrawalCapacity,
  280. WithdrawalCapacityCode: facility.Country + facility.Eic + "WC",
  281. Info: storageItem.Info,
  282. CreateTime: time.Now(),
  283. ModifyTime: time.Now(),
  284. }
  285. itemVerifyCode := facItem.GasInStorage + facItem.Full + facItem.Trend + facItem.Injection + facItem.Withdrawal
  286. if existIndex, ok := existFacIndexMap[facItem.Name+facItem.GasDayStartedOn]; !ok {
  287. facItems = append(facItems, &facItem)
  288. existFacIndexMap[facItem.Name+facItem.GasDayStartedOn] = &facItem
  289. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  290. //更新
  291. err := models.ModifyBaseFromEicIndex(facItem.GasInStorage, facItem.Full, facItem.Trend, facItem.Injection, facItem.Withdrawal, existIndex.BaseFromEicIndexId)
  292. if err != nil {
  293. fmt.Println("data update err:", err)
  294. }
  295. }
  296. }
  297. if _, ok := eicIndexCodeMap[facility.Country+"_"+facility.Name]; !ok {
  298. codeMappingItem := models.BaseFromTradeMapping{
  299. BaseFromTradeMappingId: 0,
  300. IndexName: facility.Country + "_" + facility.Name,
  301. IndexCode: facility.Country + facility.Eic,
  302. Exchange: "EIC",
  303. }
  304. codeMapList = append(codeMapList, &codeMappingItem)
  305. eicIndexCodeMap[facility.Country+"_"+facility.Name] = facility.Country + facility.Eic
  306. }
  307. if _, ok := eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"GS"]; !ok {
  308. codeMappingItem := models.BaseFromTradeMapping{
  309. BaseFromTradeMappingId: 0,
  310. IndexName: facility.Country + "_" + facility.Name + "_" + "GS",
  311. IndexCode: facility.Country + facility.Eic + "GS",
  312. Exchange: "EIC",
  313. }
  314. codeMapList = append(codeMapList, &codeMappingItem)
  315. eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"GS"] = facility.Country + facility.Eic + "GS"
  316. }
  317. if _, ok := eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"F"]; !ok {
  318. codeMappingItem := models.BaseFromTradeMapping{
  319. BaseFromTradeMappingId: 0,
  320. IndexName: facility.Country + "_" + facility.Name + "_" + "F",
  321. IndexCode: facility.Country + facility.Eic + "F",
  322. Exchange: "EIC",
  323. }
  324. codeMapList = append(codeMapList, &codeMappingItem)
  325. eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"F"] = facility.Country + facility.Eic + "F"
  326. }
  327. if _, ok := eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"T"]; !ok {
  328. codeMappingItem := models.BaseFromTradeMapping{
  329. BaseFromTradeMappingId: 0,
  330. IndexName: facility.Country + "_" + facility.Name + "_" + "T",
  331. IndexCode: facility.Country + facility.Eic + "T",
  332. Exchange: "EIC",
  333. }
  334. codeMapList = append(codeMapList, &codeMappingItem)
  335. eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"T"] = facility.Country + facility.Eic + "T"
  336. }
  337. if _, ok := eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"In"]; !ok {
  338. codeMappingItem := models.BaseFromTradeMapping{
  339. BaseFromTradeMappingId: 0,
  340. IndexName: facility.Country + "_" + facility.Name + "_" + "In",
  341. IndexCode: facility.Country + facility.Eic + "In",
  342. Exchange: "EIC",
  343. }
  344. codeMapList = append(codeMapList, &codeMappingItem)
  345. eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"In"] = facility.Country + facility.Eic + "In"
  346. }
  347. if _, ok := eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"Out"]; !ok {
  348. codeMappingItem := models.BaseFromTradeMapping{
  349. BaseFromTradeMappingId: 0,
  350. IndexName: facility.Country + "_" + facility.Name + "_" + "Out",
  351. IndexCode: facility.Country + facility.Eic + "Out",
  352. Exchange: "EIC",
  353. }
  354. codeMapList = append(codeMapList, &codeMappingItem)
  355. eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"Out"] = facility.Country + facility.Eic + "Out"
  356. }
  357. if _, ok := eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"WGV"]; !ok {
  358. codeMappingItem := models.BaseFromTradeMapping{
  359. BaseFromTradeMappingId: 0,
  360. IndexName: facility.Country + "_" + facility.Name + "_" + "WGV",
  361. IndexCode: facility.Country + facility.Eic + "WGV",
  362. Exchange: "EIC",
  363. }
  364. codeMapList = append(codeMapList, &codeMappingItem)
  365. eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"WGV"] = facility.Country + facility.Eic + "WGV"
  366. }
  367. if _, ok := eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"IC"]; !ok {
  368. codeMappingItem := models.BaseFromTradeMapping{
  369. BaseFromTradeMappingId: 0,
  370. IndexName: facility.Country + "_" + facility.Name + "_" + "IC",
  371. IndexCode: facility.Country + facility.Eic + "IC",
  372. Exchange: "EIC",
  373. }
  374. codeMapList = append(codeMapList, &codeMappingItem)
  375. eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"IC"] = facility.Country + facility.Eic + "IC"
  376. }
  377. if _, ok := eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"WC"]; !ok {
  378. codeMappingItem := models.BaseFromTradeMapping{
  379. BaseFromTradeMappingId: 0,
  380. IndexName: facility.Country + "_" + facility.Name + "_" + "WC",
  381. IndexCode: facility.Country + facility.Eic + "WC",
  382. Exchange: "EIC",
  383. }
  384. codeMapList = append(codeMapList, &codeMappingItem)
  385. eicIndexCodeMap[facility.Country+"_"+facility.Name+"_"+"WC"] = facility.Country + facility.Eic + "WC"
  386. }
  387. }
  388. if len(facItems) != 0 {
  389. successNums, err := models.AddEicDataMulti(facItems)
  390. fmt.Println("fac successNums:", successNums)
  391. if err != nil {
  392. fmt.Println("AddFacDataMulti err:", err)
  393. }
  394. }
  395. if len(codeMapList) != 0 {
  396. successNums, err := models.AddEicCodeMulti(codeMapList)
  397. fmt.Println("codeMapping successNums:", successNums)
  398. if err != nil {
  399. fmt.Println("AddEicCodeMulti err:", err)
  400. }
  401. }
  402. }
  403. //国家级,洲际级统计
  404. var codeMapList []*models.BaseFromTradeMapping
  405. Countries := []string{"eu", "AT", "BE", "BG", "HR", "CZ",
  406. "DK", "FR", "DE", "HU", "IE", "IT", "LV", "NL", "PL",
  407. "PT", "RO", "SK", "ES", "SE", "GB", "ne", "RS", "UA"}
  408. for _, country := range Countries {
  409. var countryItems []*models.BaseFromTradeEicIndex
  410. //获取所有指标信息
  411. allFacIndex, err := models.GetCountryFromEicIndexAll(country)
  412. if err != nil {
  413. fmt.Println("select err:", err)
  414. }
  415. existFacIndexMap := make(map[string]*models.BaseFromTradeEicIndex)
  416. for _, v := range allFacIndex {
  417. existFacIndexMap[v.Name+v.GasDayStartedOn] = v
  418. }
  419. countryUrl := fmt.Sprintf("https://agsi.gie.eu/api/data/%s", country)
  420. fmt.Println("countryUrl:", countryUrl)
  421. body, err := http.Get(countryUrl)
  422. if err != nil {
  423. fmt.Println("GetData Err:" + err.Error())
  424. continue
  425. }
  426. var countryStorages []Storage
  427. err = json.Unmarshal(body, &countryStorages)
  428. for _, countryStorage := range countryStorages {
  429. gasInStorage, _ := strconv.ParseFloat(countryStorage.GasInStorage, 64)
  430. full, _ := strconv.ParseFloat(countryStorage.Full, 64)
  431. trend, _ := strconv.ParseFloat(countryStorage.Trend, 64)
  432. injection, _ := strconv.ParseFloat(countryStorage.Injection, 64)
  433. withdrawal, _ := strconv.ParseFloat(countryStorage.Withdrawal, 64)
  434. workingGasVolume, _ := strconv.ParseFloat(countryStorage.WorkingGasVolume, 64)
  435. injectionCapacity, _ := strconv.ParseFloat(countryStorage.InjectionCapacity, 64)
  436. withdrawalCapacity, _ := strconv.ParseFloat(countryStorage.WithdrawalCapacity, 64)
  437. countryItem := models.BaseFromTradeEicIndex{
  438. BaseFromEicIndexId: 0,
  439. Country: country,
  440. Type: "country",
  441. EicCode: country,
  442. ShortName: country,
  443. Name: country,
  444. Status: countryStorage.Status,
  445. GasDayStartedOn: countryStorage.GasDayStartedOn,
  446. GasInStorage: gasInStorage,
  447. GasInStorageCode: country + "GS",
  448. Full: full,
  449. FullCode: country + "F",
  450. Trend: trend,
  451. TrendCode: country + "T",
  452. Injection: injection,
  453. InjectionCode: country + "In",
  454. Withdrawal: withdrawal,
  455. WithdrawalCode: country + "Out",
  456. WorkingGasVolume: workingGasVolume,
  457. WorkingGasVolumeCode: country + "WGV",
  458. InjectionCapacity: injectionCapacity,
  459. InjectionCapacityCode: country + "IC",
  460. WithdrawalCapacity: withdrawalCapacity,
  461. WithdrawalCapacityCode: country + "WC",
  462. Info: countryStorage.Info,
  463. CreateTime: time.Now(),
  464. ModifyTime: time.Now(),
  465. }
  466. if country == "eu" || country == "ne" {
  467. countryItem.Type = "continent"
  468. }
  469. itemVerifyCode := countryItem.GasInStorage + countryItem.Full + countryItem.Trend + countryItem.Injection + countryItem.Withdrawal
  470. if existIndex, ok := existFacIndexMap[countryItem.Name+countryItem.GasDayStartedOn]; !ok {
  471. countryItems = append(countryItems, &countryItem)
  472. existFacIndexMap[countryItem.Name+countryItem.GasDayStartedOn] = &countryItem
  473. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  474. //更新
  475. err := models.ModifyBaseFromEicIndex(countryItem.GasInStorage, countryItem.Full, countryItem.Trend, countryItem.Injection, countryItem.Withdrawal, existIndex.BaseFromEicIndexId)
  476. if err != nil {
  477. fmt.Println("data update err:", err)
  478. }
  479. }
  480. }
  481. if len(countryItems) != 0 {
  482. successNums, err := models.AddEicDataMulti(countryItems)
  483. fmt.Println("Country successNums:", successNums)
  484. if err != nil {
  485. fmt.Println("AddCountryDataMulti err:", err)
  486. }
  487. }
  488. if _, ok := eicIndexCodeMap[country]; !ok {
  489. codeMappingItem := models.BaseFromTradeMapping{
  490. BaseFromTradeMappingId: 0,
  491. IndexName: country,
  492. IndexCode: country,
  493. Exchange: "EIC",
  494. }
  495. codeMapList = append(codeMapList, &codeMappingItem)
  496. eicIndexCodeMap[country] = country
  497. }
  498. if _, ok := eicIndexCodeMap[country+"_"+"GS"]; !ok {
  499. codeMappingItem := models.BaseFromTradeMapping{
  500. BaseFromTradeMappingId: 0,
  501. IndexName: country + "_" + "GS",
  502. IndexCode: country + "GS",
  503. Exchange: "EIC",
  504. }
  505. codeMapList = append(codeMapList, &codeMappingItem)
  506. eicIndexCodeMap[country+"_"+"GS"] = country + "GS"
  507. }
  508. if _, ok := eicIndexCodeMap[country+"_"+"F"]; !ok {
  509. codeMappingItem := models.BaseFromTradeMapping{
  510. BaseFromTradeMappingId: 0,
  511. IndexName: country + "_" + "F",
  512. IndexCode: country + "F",
  513. Exchange: "EIC",
  514. }
  515. codeMapList = append(codeMapList, &codeMappingItem)
  516. eicIndexCodeMap[country+"_"+"F"] = country + "F"
  517. }
  518. if _, ok := eicIndexCodeMap[country+"_"+"T"]; !ok {
  519. codeMappingItem := models.BaseFromTradeMapping{
  520. BaseFromTradeMappingId: 0,
  521. IndexName: country + "_" + "T",
  522. IndexCode: country + "T",
  523. Exchange: "EIC",
  524. }
  525. codeMapList = append(codeMapList, &codeMappingItem)
  526. eicIndexCodeMap[country+"_"+"T"] = country + "T"
  527. }
  528. if _, ok := eicIndexCodeMap[country+"_"+"In"]; !ok {
  529. codeMappingItem := models.BaseFromTradeMapping{
  530. BaseFromTradeMappingId: 0,
  531. IndexName: country + "_" + "In",
  532. IndexCode: country + "In",
  533. Exchange: "EIC",
  534. }
  535. codeMapList = append(codeMapList, &codeMappingItem)
  536. eicIndexCodeMap[country+"_"+"In"] = country + "In"
  537. }
  538. if _, ok := eicIndexCodeMap[country+"_"+"Out"]; !ok {
  539. codeMappingItem := models.BaseFromTradeMapping{
  540. BaseFromTradeMappingId: 0,
  541. IndexName: country + "_" + "Out",
  542. IndexCode: country + "Out",
  543. Exchange: "EIC",
  544. }
  545. codeMapList = append(codeMapList, &codeMappingItem)
  546. eicIndexCodeMap[country+"_"+"Out"] = country + "Out"
  547. }
  548. if _, ok := eicIndexCodeMap[country+"_"+"WGV"]; !ok {
  549. codeMappingItem := models.BaseFromTradeMapping{
  550. BaseFromTradeMappingId: 0,
  551. IndexName: country + "_" + "WGV",
  552. IndexCode: country + "WGV",
  553. Exchange: "EIC",
  554. }
  555. codeMapList = append(codeMapList, &codeMappingItem)
  556. eicIndexCodeMap[country+"_"+"WGV"] = country + "WGV"
  557. }
  558. if _, ok := eicIndexCodeMap[country+"_"+"IC"]; !ok {
  559. codeMappingItem := models.BaseFromTradeMapping{
  560. BaseFromTradeMappingId: 0,
  561. IndexName: country + "_" + "IC",
  562. IndexCode: country + "IC",
  563. Exchange: "EIC",
  564. }
  565. codeMapList = append(codeMapList, &codeMappingItem)
  566. eicIndexCodeMap[country+"_"+"IC"] = country + "IC"
  567. }
  568. if _, ok := eicIndexCodeMap[country+"_"+"WC"]; !ok {
  569. codeMappingItem := models.BaseFromTradeMapping{
  570. BaseFromTradeMappingId: 0,
  571. IndexName: country + "_" + "WC",
  572. IndexCode: country + "WC",
  573. Exchange: "EIC",
  574. }
  575. codeMapList = append(codeMapList, &codeMappingItem)
  576. eicIndexCodeMap[country+"_"+"WC"] = country + "WC"
  577. }
  578. }
  579. if len(codeMapList) != 0 {
  580. successNums, err := models.AddEicCodeMulti(codeMapList)
  581. fmt.Println("codeMapping successNums:", successNums)
  582. if err != nil {
  583. fmt.Println("AddEicCodeMulti err:", err)
  584. }
  585. }
  586. }