sso_eic.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_data_crawler/models"
  6. "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.Eic + sso.Country,
  93. ShortName: sso.ShortName,
  94. Name: sso.Name,
  95. Status: storageItem.Status,
  96. GasDayStartedOn: storageItem.GasDayStartedOn,
  97. GasInStorage: gasInStorage,
  98. Full: full,
  99. Trend: trend,
  100. Injection: injection,
  101. Withdrawal: withdrawal,
  102. WorkingGasVolume: workingGasVolume,
  103. InjectionCapacity: injectionCapacity,
  104. WithdrawalCapacity: withdrawalCapacity,
  105. Info: storageItem.Info,
  106. CreateTime: time.Now(),
  107. ModifyTime: time.Now(),
  108. }
  109. itemVerifyCode := ssoItem.GasInStorage + ssoItem.Full + ssoItem.Trend + ssoItem.Injection + ssoItem.Withdrawal
  110. if existIndex, ok := existIndexMap[ssoItem.GasDayStartedOn+ssoItem.Name]; !ok {
  111. ssoItems = append(ssoItems, &ssoItem)
  112. //newID, err := models.AddBaseFromEicIndex(&ssoItem)
  113. //if err != nil {
  114. // fmt.Println("insert error:", err)
  115. //}
  116. //fmt.Println("insert new indexID:", newID)
  117. existIndexMap[ssoItem.GasDayStartedOn+ssoItem.Name] = &ssoItem
  118. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  119. //更新
  120. err := models.ModifyBaseFromEicIndex(ssoItem.GasInStorage, ssoItem.Full, ssoItem.Trend, ssoItem.Injection, ssoItem.Withdrawal, existIndex.BaseFromEicIndexId)
  121. if err != nil {
  122. fmt.Println("data update err:", err)
  123. }
  124. }
  125. }
  126. if _, ok := eicIndexCodeMap[sso.Country+sso.Name]; !ok {
  127. codeMappingItem := models.BaseFromTradeMapping{
  128. BaseFromTradeMappingId: 0,
  129. IndexName: sso.Country+sso.Name,
  130. IndexCode: sso.Country + sso.Eic,
  131. Exchange: "EIC",
  132. }
  133. codeMapList = append(codeMapList, &codeMappingItem)
  134. eicIndexCodeMap[sso.Country+sso.Name] = sso.Country + sso.Eic
  135. }
  136. if len(ssoItems) != 0 {
  137. successNums, err := models.AddEicDataMulti(ssoItems)
  138. fmt.Println("SSO successNums:", successNums)
  139. if err != nil {
  140. fmt.Println("AddSSODataMulti err:", err)
  141. }
  142. }
  143. facilities := sso.Facilities
  144. var facItems []*models.BaseFromTradeEicIndex
  145. for _, facility := range facilities {
  146. //获取所有指标信息
  147. allFacIndex, err := models.GetFacFromEicIndexAll(facility.Name)
  148. if err != nil {
  149. fmt.Println("select err:", err)
  150. }
  151. existFacIndexMap := make(map[string]*models.BaseFromTradeEicIndex)
  152. for _, v := range allFacIndex {
  153. existFacIndexMap[v.Name+v.GasDayStartedOn] = v
  154. }
  155. facUrl := facility.Url
  156. facBody, err := http.Get(ssoUrl)
  157. fmt.Println("facUrl:", facUrl)
  158. if err != nil {
  159. fmt.Println("GetData Err:" + err.Error())
  160. continue
  161. }
  162. var facStorages []Storage
  163. err = json.Unmarshal(facBody, &facStorages)
  164. for _, storageItem := range facStorages {
  165. gasInStorage, _ := strconv.ParseFloat(storageItem.GasInStorage, 64)
  166. full, _ := strconv.ParseFloat(storageItem.Full, 64)
  167. trend, _ := strconv.ParseFloat(storageItem.Trend, 64)
  168. injection, _ := strconv.ParseFloat(storageItem.Injection, 64)
  169. withdrawal, _ := strconv.ParseFloat(storageItem.Withdrawal, 64)
  170. workingGasVolume, _ := strconv.ParseFloat(storageItem.WorkingGasVolume, 64)
  171. injectionCapacity, _ := strconv.ParseFloat(storageItem.InjectionCapacity, 64)
  172. withdrawalCapacity, _ := strconv.ParseFloat(storageItem.WithdrawalCapacity, 64)
  173. facItem := models.BaseFromTradeEicIndex{
  174. BaseFromEicIndexId: 0,
  175. Country: facility.Country,
  176. Type: facility.Type,
  177. EicCode: facility.Eic + facility.Country,
  178. ShortName: sso.ShortName,
  179. Name: facility.Name,
  180. Status: storageItem.Status,
  181. GasDayStartedOn: storageItem.GasDayStartedOn,
  182. GasInStorage: gasInStorage,
  183. Full: full,
  184. Trend: trend,
  185. Injection: injection,
  186. Withdrawal: withdrawal,
  187. WorkingGasVolume: workingGasVolume,
  188. InjectionCapacity: injectionCapacity,
  189. WithdrawalCapacity: withdrawalCapacity,
  190. Info: storageItem.Info,
  191. CreateTime: time.Now(),
  192. ModifyTime: time.Now(),
  193. }
  194. itemVerifyCode := facItem.GasInStorage + facItem.Full + facItem.Trend + facItem.Injection + facItem.Withdrawal
  195. if existIndex, ok := existFacIndexMap[facItem.Name+facItem.GasDayStartedOn]; !ok {
  196. facItems = append(facItems, &facItem)
  197. //newID, err := models.AddBaseFromEicIndex(&facItem)
  198. //if err != nil {
  199. // fmt.Println("insert error:", err)
  200. //}
  201. //fmt.Println("insert new indexID:", newID)
  202. existFacIndexMap[facItem.Name+facItem.GasDayStartedOn] = &facItem
  203. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  204. //更新
  205. err := models.ModifyBaseFromEicIndex(facItem.GasInStorage, facItem.Full, facItem.Trend, facItem.Injection, facItem.Withdrawal, existIndex.BaseFromEicIndexId)
  206. if err != nil {
  207. fmt.Println("data update err:", err)
  208. }
  209. }
  210. }
  211. if _, ok := eicIndexCodeMap[facility.Country+facility.Name]; !ok {
  212. codeMappingItem := models.BaseFromTradeMapping{
  213. BaseFromTradeMappingId: 0,
  214. IndexName: facility.Country+facility.Name,
  215. IndexCode: facility.Country+facility.Eic,
  216. Exchange: "EIC",
  217. }
  218. codeMapList = append(codeMapList, &codeMappingItem)
  219. eicIndexCodeMap[facility.Country+facility.Name] = facility.Country+facility.Eic
  220. }
  221. }
  222. if len(facItems) != 0 {
  223. successNums, err := models.AddEicDataMulti(facItems)
  224. fmt.Println("fac successNums:", successNums)
  225. if err != nil {
  226. fmt.Println("AddFacDataMulti err:", err)
  227. }
  228. }
  229. successNums, err := models.AddEicCodeMulti(codeMapList)
  230. fmt.Println("codeMapping successNums:", successNums)
  231. if err != nil {
  232. fmt.Println("AddEicCodeMulti err:", err)
  233. }
  234. }
  235. //国家级,洲际级统计
  236. var codeMapList []*models.BaseFromTradeMapping
  237. Countries := []string{"eu", "AT", "BE", "BG", "HR", "CZ",
  238. "DK", "FR", "DE", "HU", "IE", "IT", "LV", "NL", "PL",
  239. "PT", "RO", "SK", "ES", "SE", "GB", "ne", "RS", "UA"}
  240. for _, country := range Countries {
  241. var countryItems []*models.BaseFromTradeEicIndex
  242. //获取所有指标信息
  243. allFacIndex, err := models.GetCountryFromEicIndexAll(country)
  244. if err != nil {
  245. fmt.Println("select err:", err)
  246. }
  247. existFacIndexMap := make(map[string]*models.BaseFromTradeEicIndex)
  248. for _, v := range allFacIndex {
  249. existFacIndexMap[v.Name+v.GasDayStartedOn] = v
  250. }
  251. countryUrl := fmt.Sprintf("https://agsi.gie.eu/api/data/%s", country)
  252. fmt.Println("countryUrl:", countryUrl)
  253. body, err := http.Get(countryUrl)
  254. if err != nil {
  255. fmt.Println("GetData Err:" + err.Error())
  256. continue
  257. }
  258. var countryStorages []Storage
  259. err = json.Unmarshal(body, &countryStorages)
  260. for _, countryStorage := range countryStorages {
  261. gasInStorage, _ := strconv.ParseFloat(countryStorage.GasInStorage, 64)
  262. full, _ := strconv.ParseFloat(countryStorage.Full, 64)
  263. trend, _ := strconv.ParseFloat(countryStorage.Trend, 64)
  264. injection, _ := strconv.ParseFloat(countryStorage.Injection, 64)
  265. withdrawal, _ := strconv.ParseFloat(countryStorage.Withdrawal, 64)
  266. workingGasVolume, _ := strconv.ParseFloat(countryStorage.WorkingGasVolume, 64)
  267. injectionCapacity, _ := strconv.ParseFloat(countryStorage.InjectionCapacity, 64)
  268. withdrawalCapacity, _ := strconv.ParseFloat(countryStorage.WithdrawalCapacity, 64)
  269. countryItem := models.BaseFromTradeEicIndex{
  270. BaseFromEicIndexId: 0,
  271. Country: country,
  272. Type: "country",
  273. EicCode: country,
  274. ShortName: country,
  275. Name: country,
  276. Status: countryStorage.Status,
  277. GasDayStartedOn: countryStorage.GasDayStartedOn,
  278. GasInStorage: gasInStorage,
  279. Full: full,
  280. Trend: trend,
  281. Injection: injection,
  282. Withdrawal: withdrawal,
  283. WorkingGasVolume: workingGasVolume,
  284. InjectionCapacity: injectionCapacity,
  285. WithdrawalCapacity: withdrawalCapacity,
  286. Info: countryStorage.Info,
  287. CreateTime: time.Now(),
  288. ModifyTime: time.Now(),
  289. }
  290. if country == "eu" || country == "ne" {
  291. countryItem.Type = "continent"
  292. }
  293. itemVerifyCode := countryItem.GasInStorage + countryItem.Full + countryItem.Trend + countryItem.Injection + countryItem.Withdrawal
  294. if existIndex, ok := existFacIndexMap[countryItem.Name+countryItem.GasDayStartedOn]; !ok {
  295. countryItems = append(countryItems, &countryItem)
  296. //newID, err := models.AddBaseFromEicIndex(&countryItem)
  297. //if err != nil {
  298. // fmt.Println("insert error:", err)
  299. //}
  300. //fmt.Println("insert new indexID:", newID)
  301. existFacIndexMap[countryItem.Name+countryItem.GasDayStartedOn] = &countryItem
  302. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  303. //更新
  304. err := models.ModifyBaseFromEicIndex(countryItem.GasInStorage, countryItem.Full, countryItem.Trend, countryItem.Injection, countryItem.Withdrawal, existIndex.BaseFromEicIndexId)
  305. if err != nil {
  306. fmt.Println("data update err:", err)
  307. }
  308. }
  309. }
  310. if len(countryItems) != 0 {
  311. successNums, err := models.AddEicDataMulti(countryItems)
  312. fmt.Println("Country successNums:", successNums)
  313. if err != nil {
  314. fmt.Println("AddCountryDataMulti err:", err)
  315. }
  316. }
  317. if _, ok := eicIndexCodeMap[country]; !ok {
  318. codeMappingItem := models.BaseFromTradeMapping{
  319. BaseFromTradeMappingId: 0,
  320. IndexName: country,
  321. IndexCode: country,
  322. Exchange: "EIC",
  323. }
  324. codeMapList = append(codeMapList, &codeMappingItem)
  325. eicIndexCodeMap[country] = country
  326. }
  327. }
  328. successNums, err := models.AddEicCodeMulti(codeMapList)
  329. fmt.Println("codeMapping successNums:", successNums)
  330. if err != nil {
  331. fmt.Println("AddEicCodeMulti err:", err)
  332. }
  333. }