sso_eic_v2.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_crawler/models"
  5. "eta/eta_crawler/utils"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. "time"
  11. )
  12. type EicData struct {
  13. LastPage int `json:"last_page"`
  14. Total int `json:"total"`
  15. DataSet string `json:"dataset"`
  16. GasDay string `json:"gas_day"`
  17. Data []Children `json:"data"`
  18. Error string `json:"error"`
  19. Message string `json:"message"`
  20. }
  21. type Children struct {
  22. Name string `json:"name"`
  23. Code string `json:"code"`
  24. URL string `json:"url"`
  25. GasDayStart string `json:"gasDayStart"`
  26. GasInStorage string `json:"gasInStorage"`
  27. Consumption string `json:"consumption"`
  28. ConsumptionFull string `json:"consumptionFull"`
  29. Injection string `json:"injection"`
  30. Withdrawal string `json:"withdrawal"`
  31. WorkingGasVolume string `json:"workingGasVolume"`
  32. InjectionCapacity string `json:"injectionCapacity"`
  33. WithdrawalCapacity string `json:"withdrawalCapacity"`
  34. Status string `json:"status"`
  35. Trend string `json:"trend"`
  36. Full string `json:"full"`
  37. Info string `json:"-"`
  38. Children []Children `json:"children"`
  39. }
  40. type Storage1 struct {
  41. Status string `json:"status"`
  42. GasDayStartedOn string `json:"gasDayStartedOn"`
  43. GasInStorage string `json:"gasInStorage"`
  44. Full string `json:"full"`
  45. Trend string `json:"trend"`
  46. Injection string `json:"injection"`
  47. Withdrawal string `json:"withdrawal"`
  48. WorkingGasVolume string `json:"workingGasVolume"`
  49. InjectionCapacity string `json:"injectionCapacity"`
  50. WithdrawalCapacity string `json:"withdrawalCapacity"`
  51. Info string `json:"info"`
  52. }
  53. var eicIndexCodeMapV2 = make(map[string]string)
  54. func SyncStorageFromEicV2() {
  55. allCode, err := models.GetIndexCodeFromMapping("Eic")
  56. if err != nil {
  57. fmt.Println("select Code err:", err)
  58. }
  59. for _, item := range allCode {
  60. eicIndexCodeMapV2[item.IndexName] = item.IndexCode
  61. }
  62. for i := 10; i >= 0; i-- {
  63. baseUrl := "https://agsi.gie.eu/api?date=%s"
  64. //t, _ := time.Parse(utils.FormatDate, "2022-08-15")
  65. //st := t.AddDate(0, 0, i).Format(utils.FormatDate)
  66. st := time.Now().AddDate(0, 0, -i).Format(utils.FormatDate)
  67. tt, _ := time.Parse(utils.FormatDate, st)
  68. now := time.Now()
  69. if tt.After(now) {
  70. break
  71. }
  72. baseUrl = fmt.Sprintf(baseUrl, st)
  73. client := http.Client{}
  74. req, err := http.NewRequest(http.MethodGet, baseUrl, nil)
  75. if err != nil {
  76. fmt.Println("err")
  77. }
  78. // 添加请求头
  79. req.Header.Add("Content-type", "application/json;charset=utf-8")
  80. req.Header.Add("x-key", "3669f7349c0598d50948429d6ea60070")
  81. // 发送请求
  82. resp, err := client.Do(req)
  83. if err != nil {
  84. fmt.Println("err")
  85. }
  86. defer resp.Body.Close()
  87. body, err := ioutil.ReadAll(resp.Body)
  88. if err != nil {
  89. fmt.Println("err")
  90. }
  91. //body, err := http.Get(baseUrl)
  92. //
  93. //if err != nil {
  94. // fmt.Println("GetData Err:" + err.Error())
  95. // return
  96. //}
  97. fmt.Println("URL:", baseUrl)
  98. var eicData EicData
  99. err = json.Unmarshal(body, &eicData)
  100. if err != nil {
  101. fmt.Println("Unmarshal Err:" + err.Error())
  102. return
  103. }
  104. //洲际级
  105. var insertItems []*models.BaseFromTradeEicIndexV2
  106. for _, continent := range eicData.Data {
  107. var codeMapList []*models.BaseFromTradeMapping
  108. //为保持唯一性,code使用md5加密url
  109. code := utils.MD5(continent.URL)
  110. gasInStorage, _ := strconv.ParseFloat(continent.GasInStorage, 64)
  111. full, _ := strconv.ParseFloat(continent.Full, 64)
  112. trend, _ := strconv.ParseFloat(continent.Trend, 64)
  113. injection, _ := strconv.ParseFloat(continent.Injection, 64)
  114. withdrawal, _ := strconv.ParseFloat(continent.Withdrawal, 64)
  115. workingGasVolume, _ := strconv.ParseFloat(continent.WorkingGasVolume, 64)
  116. injectionCapacity, _ := strconv.ParseFloat(continent.InjectionCapacity, 64)
  117. withdrawalCapacity, _ := strconv.ParseFloat(continent.WithdrawalCapacity, 64)
  118. continentItem := models.BaseFromTradeEicIndexV2{
  119. BaseFromEicIndexId: 0,
  120. Type: "continent",
  121. EicCode: code,
  122. Name: continent.Name,
  123. Status: continent.Status,
  124. GasDayStart: continent.GasDayStart,
  125. GasInStorage: gasInStorage,
  126. GasInStorageCode: code + "GS",
  127. Consumption: continent.Consumption,
  128. ConsumptionCode: code + "C",
  129. ConsumptionFull: continent.ConsumptionFull,
  130. ConsumptionFullCode: code + "CF",
  131. Full: full,
  132. FullCode: code + "F",
  133. Trend: trend,
  134. TrendCode: code + "T",
  135. Injection: injection,
  136. InjectionCode: code + "In",
  137. Withdrawal: withdrawal,
  138. WithdrawalCode: code + "Out",
  139. WorkingGasVolume: workingGasVolume,
  140. WorkingGasVolumeCode: code + "WGV",
  141. InjectionCapacity: injectionCapacity,
  142. InjectionCapacityCode: code + "IC",
  143. WithdrawalCapacity: withdrawalCapacity,
  144. WithdrawalCapacityCode: code + "WC",
  145. Info: continent.Info,
  146. CreateTime: time.Now(),
  147. ModifyTime: time.Now(),
  148. }
  149. //校验
  150. existIndexMap := make(map[string]*models.BaseFromTradeEicIndexV2)
  151. allIndex, err := models.GetContinentFromEicIndexAllV2(continent.GasDayStart)
  152. if err != nil {
  153. fmt.Println("select err:", err)
  154. }
  155. for _, v := range allIndex {
  156. existIndexMap[v.GasDayStart+v.EicCode] = v
  157. }
  158. itemVerifyCode := continentItem.GasInStorage + continentItem.Full + continentItem.Trend + continentItem.Injection + continentItem.Withdrawal
  159. if existIndex, ok := existIndexMap[continentItem.GasDayStart+continentItem.EicCode]; !ok {
  160. //新增
  161. if continentItem.Status != "N" {
  162. insertItems = append(insertItems, &continentItem)
  163. existIndexMap[continentItem.GasDayStart+continentItem.EicCode] = &continentItem
  164. }
  165. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  166. //更新
  167. if continentItem.Status != "N" {
  168. err := models.ModifyBaseFromEicIndexV2(continentItem.GasInStorage, continentItem.Full, continentItem.Trend, continentItem.Injection, continentItem.Withdrawal, existIndex.BaseFromEicIndexId)
  169. if err != nil {
  170. fmt.Println("data update err:", err)
  171. }
  172. }
  173. }
  174. if _, ok := eicIndexCodeMapV2[continentItem.Name]; !ok {
  175. codeMappingItem := models.BaseFromTradeMapping{
  176. BaseFromTradeMappingId: 0,
  177. IndexName: continentItem.Name,
  178. IndexCode: continentItem.EicCode,
  179. Exchange: "EIC",
  180. }
  181. codeMapList = append(codeMapList, &codeMappingItem)
  182. eicIndexCodeMapV2[continentItem.Name] = continentItem.EicCode
  183. }
  184. //国家级
  185. for _, country := range continent.Children {
  186. //为保持唯一性,code使用md5加密url
  187. code := utils.MD5(country.URL)
  188. gasInStorage, _ := strconv.ParseFloat(country.GasInStorage, 64)
  189. full, _ := strconv.ParseFloat(country.Full, 64)
  190. trend, _ := strconv.ParseFloat(country.Trend, 64)
  191. injection, _ := strconv.ParseFloat(country.Injection, 64)
  192. withdrawal, _ := strconv.ParseFloat(country.Withdrawal, 64)
  193. workingGasVolume, _ := strconv.ParseFloat(country.WorkingGasVolume, 64)
  194. injectionCapacity, _ := strconv.ParseFloat(country.InjectionCapacity, 64)
  195. withdrawalCapacity, _ := strconv.ParseFloat(country.WithdrawalCapacity, 64)
  196. countryItem := models.BaseFromTradeEicIndexV2{
  197. BaseFromEicIndexId: 0,
  198. Type: "country",
  199. EicCode: code,
  200. Name: country.Name,
  201. Status: country.Status,
  202. GasDayStart: country.GasDayStart,
  203. GasInStorage: gasInStorage,
  204. GasInStorageCode: code + "GS",
  205. Consumption: country.Consumption,
  206. ConsumptionCode: code + "C",
  207. ConsumptionFull: country.ConsumptionFull,
  208. ConsumptionFullCode: code + "CF",
  209. Full: full,
  210. FullCode: code + "F",
  211. Trend: trend,
  212. TrendCode: code + "T",
  213. Injection: injection,
  214. InjectionCode: code + "In",
  215. Withdrawal: withdrawal,
  216. WithdrawalCode: code + "Out",
  217. WorkingGasVolume: workingGasVolume,
  218. WorkingGasVolumeCode: code + "WGV",
  219. InjectionCapacity: injectionCapacity,
  220. InjectionCapacityCode: code + "IC",
  221. WithdrawalCapacity: withdrawalCapacity,
  222. WithdrawalCapacityCode: code + "WC",
  223. Info: country.Info,
  224. Parent: continent.Name,
  225. CreateTime: time.Now(),
  226. ModifyTime: time.Now(),
  227. }
  228. //校验
  229. existIndexMap := make(map[string]*models.BaseFromTradeEicIndexV2)
  230. allIndex, err := models.GetCountryFromEicIndexAllV2(country.GasDayStart)
  231. if err != nil {
  232. fmt.Println("select err:", err)
  233. }
  234. for _, v := range allIndex {
  235. existIndexMap[v.GasDayStart+v.EicCode] = v
  236. }
  237. itemVerifyCode := countryItem.GasInStorage + countryItem.Full + countryItem.Trend + countryItem.Injection + countryItem.Withdrawal
  238. if existIndex, ok := existIndexMap[countryItem.GasDayStart+countryItem.EicCode]; !ok {
  239. //新增
  240. if countryItem.Status != "N" {
  241. insertItems = append(insertItems, &countryItem)
  242. existIndexMap[countryItem.GasDayStart+countryItem.EicCode] = &countryItem
  243. }
  244. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  245. //更新
  246. if countryItem.Status != "N" {
  247. err := models.ModifyBaseFromEicIndexV2(countryItem.GasInStorage, countryItem.Full, countryItem.Trend, countryItem.Injection, countryItem.Withdrawal, existIndex.BaseFromEicIndexId)
  248. if err != nil {
  249. fmt.Println("data update err:", err)
  250. }
  251. }
  252. }
  253. if _, ok := eicIndexCodeMapV2[countryItem.Name]; !ok {
  254. codeMappingItem := models.BaseFromTradeMapping{
  255. BaseFromTradeMappingId: 0,
  256. IndexName: countryItem.Name,
  257. IndexCode: countryItem.EicCode,
  258. Exchange: "EIC",
  259. }
  260. codeMapList = append(codeMapList, &codeMappingItem)
  261. eicIndexCodeMapV2[countryItem.Name] = countryItem.EicCode
  262. }
  263. //厂商级
  264. for _, sso := range country.Children {
  265. //为保持唯一性,code使用md5加密url
  266. code := utils.MD5(sso.URL)
  267. gasInStorage, _ := strconv.ParseFloat(sso.GasInStorage, 64)
  268. full, _ := strconv.ParseFloat(sso.Full, 64)
  269. trend, _ := strconv.ParseFloat(sso.Trend, 64)
  270. injection, _ := strconv.ParseFloat(sso.Injection, 64)
  271. withdrawal, _ := strconv.ParseFloat(sso.Withdrawal, 64)
  272. workingGasVolume, _ := strconv.ParseFloat(sso.WorkingGasVolume, 64)
  273. injectionCapacity, _ := strconv.ParseFloat(sso.InjectionCapacity, 64)
  274. withdrawalCapacity, _ := strconv.ParseFloat(sso.WithdrawalCapacity, 64)
  275. ssoItem := models.BaseFromTradeEicIndexV2{
  276. BaseFromEicIndexId: 0,
  277. Type: "sso",
  278. EicCode: code,
  279. Name: sso.Name,
  280. Status: sso.Status,
  281. GasDayStart: sso.GasDayStart,
  282. GasInStorage: gasInStorage,
  283. GasInStorageCode: code + "GS",
  284. Consumption: sso.Consumption,
  285. ConsumptionCode: code + "C",
  286. ConsumptionFull: sso.ConsumptionFull,
  287. ConsumptionFullCode: code + "CF",
  288. Full: full,
  289. FullCode: code + "F",
  290. Trend: trend,
  291. TrendCode: code + "T",
  292. Injection: injection,
  293. InjectionCode: code + "In",
  294. Withdrawal: withdrawal,
  295. WithdrawalCode: code + "Out",
  296. WorkingGasVolume: workingGasVolume,
  297. WorkingGasVolumeCode: code + "WGV",
  298. InjectionCapacity: injectionCapacity,
  299. InjectionCapacityCode: code + "IC",
  300. WithdrawalCapacity: withdrawalCapacity,
  301. WithdrawalCapacityCode: code + "WC",
  302. Info: sso.Info,
  303. Parent: country.Name,
  304. CreateTime: time.Now(),
  305. ModifyTime: time.Now(),
  306. }
  307. //校验
  308. existIndexMap := make(map[string]*models.BaseFromTradeEicIndexV2)
  309. allIndex, err := models.GetSSOFromEicIndexAllV2(sso.GasDayStart)
  310. if err != nil {
  311. fmt.Println("select err:", err)
  312. }
  313. for _, v := range allIndex {
  314. existIndexMap[v.GasDayStart+v.EicCode] = v
  315. }
  316. itemVerifyCode := ssoItem.GasInStorage + ssoItem.Full + ssoItem.Trend + ssoItem.Injection + ssoItem.Withdrawal
  317. if existIndex, ok := existIndexMap[ssoItem.GasDayStart+ssoItem.EicCode]; !ok {
  318. //新增
  319. if ssoItem.Status != "N" {
  320. insertItems = append(insertItems, &ssoItem)
  321. existIndexMap[ssoItem.GasDayStart+ssoItem.EicCode] = &ssoItem
  322. }
  323. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  324. //更新
  325. if ssoItem.Status != "N" {
  326. err := models.ModifyBaseFromEicIndexV2(ssoItem.GasInStorage, ssoItem.Full, ssoItem.Trend, ssoItem.Injection, ssoItem.Withdrawal, existIndex.BaseFromEicIndexId)
  327. if err != nil {
  328. fmt.Println("data update err:", err)
  329. }
  330. }
  331. }
  332. if _, ok := eicIndexCodeMapV2[ssoItem.Name]; !ok {
  333. codeMappingItem := models.BaseFromTradeMapping{
  334. BaseFromTradeMappingId: 0,
  335. IndexName: ssoItem.Name,
  336. IndexCode: ssoItem.EicCode,
  337. Exchange: "EIC",
  338. }
  339. codeMapList = append(codeMapList, &codeMappingItem)
  340. eicIndexCodeMapV2[ssoItem.Name] = ssoItem.EicCode
  341. }
  342. //设施级
  343. for _, fac := range sso.Children {
  344. //为保持唯一性,code使用md5加密url
  345. code := utils.MD5(fac.URL)
  346. gasInStorage, _ := strconv.ParseFloat(fac.GasInStorage, 64)
  347. full, _ := strconv.ParseFloat(fac.Full, 64)
  348. trend, _ := strconv.ParseFloat(fac.Trend, 64)
  349. injection, _ := strconv.ParseFloat(fac.Injection, 64)
  350. withdrawal, _ := strconv.ParseFloat(fac.Withdrawal, 64)
  351. workingGasVolume, _ := strconv.ParseFloat(fac.WorkingGasVolume, 64)
  352. injectionCapacity, _ := strconv.ParseFloat(fac.InjectionCapacity, 64)
  353. withdrawalCapacity, _ := strconv.ParseFloat(fac.WithdrawalCapacity, 64)
  354. facItem := models.BaseFromTradeEicIndexV2{
  355. BaseFromEicIndexId: 0,
  356. Type: "fac",
  357. EicCode: code,
  358. Name: fac.Name,
  359. Status: fac.Status,
  360. GasDayStart: fac.GasDayStart,
  361. GasInStorage: gasInStorage,
  362. GasInStorageCode: code + "GS",
  363. Consumption: fac.Consumption,
  364. ConsumptionCode: code + "C",
  365. ConsumptionFull: fac.ConsumptionFull,
  366. ConsumptionFullCode: code + "CF",
  367. Full: full,
  368. FullCode: code + "F",
  369. Trend: trend,
  370. TrendCode: code + "T",
  371. Injection: injection,
  372. InjectionCode: code + "In",
  373. Withdrawal: withdrawal,
  374. WithdrawalCode: code + "Out",
  375. WorkingGasVolume: workingGasVolume,
  376. WorkingGasVolumeCode: code + "WGV",
  377. InjectionCapacity: injectionCapacity,
  378. InjectionCapacityCode: code + "IC",
  379. WithdrawalCapacity: withdrawalCapacity,
  380. WithdrawalCapacityCode: code + "WC",
  381. Info: fac.Info,
  382. Parent: sso.Name,
  383. CreateTime: time.Now(),
  384. ModifyTime: time.Now(),
  385. }
  386. //校验
  387. existIndexMap := make(map[string]*models.BaseFromTradeEicIndexV2)
  388. allIndex, err := models.GetFacFromEicIndexAllV2(fac.GasDayStart)
  389. if err != nil {
  390. fmt.Println("select err:", err)
  391. }
  392. for _, v := range allIndex {
  393. existIndexMap[v.GasDayStart+v.EicCode] = v
  394. }
  395. itemVerifyCode := facItem.GasInStorage + facItem.Full + facItem.Trend + facItem.Injection + facItem.Withdrawal
  396. if existIndex, ok := existIndexMap[facItem.GasDayStart+facItem.EicCode]; !ok {
  397. //新增
  398. if facItem.Status != "N" {
  399. insertItems = append(insertItems, &facItem)
  400. existIndexMap[facItem.GasDayStart+facItem.EicCode] = &facItem
  401. }
  402. } else if existIndex != nil && itemVerifyCode != (existIndex.GasInStorage+existIndex.Full+existIndex.Trend+existIndex.Injection+existIndex.Withdrawal) {
  403. //更新
  404. if facItem.Status != "N" {
  405. err := models.ModifyBaseFromEicIndexV2(facItem.GasInStorage, facItem.Full, facItem.Trend, facItem.Injection, facItem.Withdrawal, existIndex.BaseFromEicIndexId)
  406. if err != nil {
  407. fmt.Println("data update err:", err)
  408. }
  409. }
  410. }
  411. if _, ok := eicIndexCodeMapV2[facItem.Name]; !ok {
  412. codeMappingItem := models.BaseFromTradeMapping{
  413. BaseFromTradeMappingId: 0,
  414. IndexName: facItem.Name,
  415. IndexCode: facItem.EicCode,
  416. Exchange: "EIC",
  417. }
  418. codeMapList = append(codeMapList, &codeMappingItem)
  419. eicIndexCodeMapV2[facItem.Name] = facItem.EicCode
  420. }
  421. }
  422. }
  423. }
  424. //codeMap新增
  425. if len(codeMapList) != 0 {
  426. successNums, err := models.AddEicCodeMulti(codeMapList)
  427. fmt.Println("codeMapping successNums:", successNums)
  428. if err != nil {
  429. fmt.Println("AddEicCodeMulti err:", err)
  430. }
  431. }
  432. }
  433. //洲际级新增
  434. if len(insertItems) != 0 {
  435. successNums, err := models.AddEicDataMultiV2(insertItems)
  436. fmt.Println("successNums:", successNums)
  437. if err != nil {
  438. fmt.Println("AddContinentDataMulti err:", err)
  439. }
  440. }
  441. }
  442. }