com_trade.go 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_crawler/models"
  6. "eta/eta_crawler/services/alarm_msg"
  7. "eta/eta_crawler/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. func SyncComTradeData(cont context.Context) (err error) {
  15. //comTradeId := 1
  16. //indexCode := "270730"
  17. //flowCode := "X"
  18. //year := 2021
  19. //partnerCode := 842
  20. //err = syncComTradeData(comTradeId, indexCode, flowCode, partnerCode, year)
  21. return
  22. }
  23. const ComTradeUrl = "https://comtradeapi.un.org/public/v1/preview/C/A/HS?"
  24. const ComTradeMonthUrl = "https://comtradeapi.un.org/public/v1/preview/C/M/HS?"
  25. //func sync() {
  26. // //yearList := []int{2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000}
  27. // yearList := []int{2022}
  28. // flowCodeList := []string{"X", "M"}
  29. //
  30. // for _, year := range yearList {
  31. // for _, flow := range flowCodeList {
  32. // fmt.Println("开始", year, "的", flow)
  33. // syncAllComTradeData2(year, 0, flow, "year")
  34. // fmt.Println("结束", year, "的", flow)
  35. // }
  36. // }
  37. //}
  38. //
  39. //func sync2() {
  40. // defer func() {
  41. // //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCoalCoastal ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  42. // msg := "UN数据同步完成"
  43. // go alarm_msg.SendAlarmMsg(msg, 3)
  44. // }()
  45. // //yearList := []int{2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000}
  46. // //yearList := []int{2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010}
  47. // yearList := []int{2023}
  48. // //monthList := []int{12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
  49. // monthList := []int{1, 2, 3, 4, 5, 6}
  50. // flowCodeList := []string{"X", "M"}
  51. //
  52. // for _, year := range yearList {
  53. // for _, month := range monthList {
  54. // for _, flow := range flowCodeList {
  55. // fmt.Println("开始", year, month, "的", flow)
  56. // syncAllComTradeData2(year, month, flow, "month")
  57. // fmt.Println("结束", year, "的", flow)
  58. // }
  59. // }
  60. // }
  61. //}
  62. // SyncYearComTrade 同步年度数据
  63. func SyncYearComTrade(cont context.Context) (err error) {
  64. errMsg := make([]string, 0)
  65. defer func() {
  66. if len(errMsg) > 0 {
  67. go alarm_msg.SendAlarmMsg("UN年度度数据同步失败:"+strings.Join(errMsg, "\n"), 3)
  68. }
  69. }()
  70. flowCodeList := []string{"X", "M"}
  71. year := time.Now().Year()
  72. for _, flow := range flowCodeList {
  73. fmt.Println("开始", year, "的", flow)
  74. err = syncAllComTradeData2(year, 0, flow, "year")
  75. if err != nil {
  76. errMsg = append(errMsg, fmt.Sprint(year, "的", flow, "数据爬取失败:", err.Error()))
  77. }
  78. fmt.Println("结束", year, "的", flow)
  79. }
  80. return
  81. }
  82. // SyncYearMonthComTrade 同步月度数据
  83. func SyncYearMonthComTrade(cont context.Context) (err error) {
  84. errMsg := make([]string, 0)
  85. defer func() {
  86. if len(errMsg) > 0 {
  87. go alarm_msg.SendAlarmMsg("UN月度数据同步失败:"+strings.Join(errMsg, "\n"), 3)
  88. }
  89. }()
  90. flowCodeList := []string{"X", "M"}
  91. year := time.Now().Year()
  92. month := int(time.Now().Month())
  93. for _, flow := range flowCodeList {
  94. fmt.Println("开始", year, month, "的", flow)
  95. err := syncAllComTradeData2(year, month, flow, "month")
  96. if err != nil {
  97. errMsg = append(errMsg, fmt.Sprint(year, month, "的", flow, "数据爬取失败:", err.Error()))
  98. }
  99. fmt.Println("结束", year, "的", flow)
  100. }
  101. return
  102. }
  103. type TmpStruct struct {
  104. IndexCode string
  105. Year int
  106. Month int
  107. FlowCode string
  108. PartnerCode string
  109. }
  110. func sync3() {
  111. defer func() {
  112. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCoalCoastal ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  113. //msg := "UN数据同步完成"
  114. //go alarm_msg.SendAlarmMsg(msg, 3)
  115. }()
  116. str := `2023/01/04 15:16:51.837 [I] [com_trade.go:183] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2022;月份:10;flow:M;partnerCode:170,410,246,703,826,233,699,784
  117. 2023/01/04 15:16:53.139 [I] [com_trade.go:183] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2022;月份:10;flow:M;partnerCode:757,218,764,710,124,579,70,528
  118. 2023/01/04 15:25:22.880 [I] [com_trade.go:183] 290121获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2022;月份:9;flow:X;partnerCode:178,275,328,768,748,84,762,496,108,270,533,894,136,670,418,174,478,446,462,72,204,690,854,562,417,678,104,60,716,52,882
  119. 2023/01/04 15:25:24.233 [I] [com_trade.go:183] 290121获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2022;月份:9;flow:X;partnerCode:470,222,400,268,450,686,860,96,484,352,68,480,512,591,740,780,508,24,858,558,634,646,242,834,340,807,308,116,524,231
  120. 2023/01/04 15:25:25.609 [I] [com_trade.go:183] 381900获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2022;月份:9;flow:X;partnerCode:392,458,600,40,300,608,498,191,360,214,36,100,704,376,32,348
  121. 2023/01/04 15:45:47.877 [I] [com_trade.go:183] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2022;月份:8;flow:M;partnerCode:156,170,804,764,246
  122. 2023/01/04 15:45:49.197 [I] [com_trade.go:183] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2022;月份:8;flow:M;partnerCode:440,100,196,31,498,36,392,348,376,40,208,360,76,682,191,214
  123. 2023/01/04 15:45:50.491 [I] [com_trade.go:183] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2022;月份:8;flow:M;partnerCode:300,600,32,458,554,608,704,112
  124. 2022/12/30 08:31:53.846 [I] [com_trade.go:176] 270730获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2019;月份:1;flow:X;partnerCode:458,36,360,376,76,214,40,348,608,498,112,682,208,600,392,196
  125. 2022/12/30 08:31:55.167 [I] [com_trade.go:176] 270730获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2019;月份:1;flow:X;partnerCode:100,554,191,300,32,704,31,440
  126. 2022/12/30 08:53:56.893 [I] [com_trade.go:176] 290230获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:12;flow:X;partnerCode:882,72,136,894,178,417,854,174,670,690,748,496,328,418,104,762,533,678,462,446,716,52,478,270,562,768,275,84,60,204,108
  127. 2022/12/30 08:53:59.079 [I] [com_trade.go:176] 290230获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:12;flow:X;partnerCode:40,458,600,76,376,682,608,31,32,348,704,100,300,498,392,360
  128. 2022/12/30 09:16:34.790 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:11;flow:X;partnerCode:703,826,702,757,218,428,752,56
  129. 2022/12/30 09:16:36.185 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:11;flow:X;partnerCode:410,842,528,246,170,70,579,792
  130. 2022/12/30 09:16:37.641 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:11;flow:X;partnerCode:156,620,251,616,764
  131. 2022/12/30 09:23:56.668 [I] [com_trade.go:176] 290244获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:11;flow:X;partnerCode:624,368,891,837,266,527,50,278,500,662,180,226,238,638,312,175,532,492,666,698,434,868,254,674,90,4,184,577,776,140,588,296,890,388,706,899,866,288,800,850,697,720,472,316,221,336,585,64,292,48,200,258,332,466,548,454,879,408,356,836,516,568,334,637,728,120,290,239,86,647,426,459,12,232,583,262,772,849,92,582,574,534,16,660,398,520,862,535,166,473,10,471,234,760,570,457,28,129,230,58,192,364,304,430,732,581,148,461,711,796,798,590,717,598,8,474,658,729,654,20,384,736,835,626,531,592,260,324,841,580,530,132,414,536,652,74,212,612,540,887,694,162,886,659,795,876,44,80,280,839,810,838,584,636
  132. 2022/12/30 09:23:58.075 [I] [com_trade.go:176] 290244获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:11;flow:X;partnerCode:705,710,642,699,490,724,442,784
  133. 2022/12/30 09:31:23.532 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:11;flow:M;partnerCode:757,752,620,642,702
  134. 2022/12/30 09:31:24.940 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:11;flow:M;partnerCode:174,328,748,84,562,882,204,104,670,60,418,678,108,768,178,275,533,462,690,446,496,716,270,478,894,417,854,72,762,52,136
  135. 2022/12/30 10:17:07.994 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:8;flow:X;partnerCode:583,368,711,810,838,28,652,666,849,868,772,175,304,570,577,638,12,80,592,580,798,132,288,356,332,239,492,461,4,180,588,887,474,516,659,534,886,312,760,862,58,527,582,732,472,324,520,662,729,166,531,698,90,388,879,674,624,500,891,626,140,795,20,200,736,316,568,234,426,162,336,850,636,776,530,64,148,835,800,184,262,612,120,260,232,890,414,796,585,292,540,192,258,10,44,384,536,654,658,899,590,841,876,334,50,697,278,74,836,434,454,647,837,408,266,457,8,86,296,866,584,212,728,535,471,129,398,430,459,48,238,706,581,473,466,694,92,226,548,364,574,660,720,230,717,598,221,839,280,532,290,637,16,254
  136. 2022/12/30 10:46:04.444 [I] [com_trade.go:176] 290244获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:7;flow:X;partnerCode:780,340,116,834,858,558,524,24,686,400,860,512,268,96,508,242,222,231,646,740,470,634,480,484,308,68,352,450,591,807
  137. 2022/12/30 10:46:05.864 [I] [com_trade.go:176] 290244获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:7;flow:X;partnerCode:682,76,704,348,376,498,600,608,360,214,300,392,100,40,32,31
  138. 2022/12/30 11:22:44.563 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:5;flow:M;partnerCode:276,688,784,792,56,616,124,703
  139. 2022/12/30 11:22:45.936 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:5;flow:M;partnerCode:428,579,702,170,620,528,643,764
  140. 2022/12/30 11:40:43.264 [I] [com_trade.go:176] 290244获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:4;flow:M;partnerCode:56,703,218,490,170,156,616,410
  141. 2022/12/30 11:40:44.553 [I] [com_trade.go:176] 290244获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2018;月份:4;flow:M;partnerCode:784,70,579,251,699,233,705,752
  142. 2022/12/30 22:41:22.216 [I] [com_trade.go:176] 290230获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:7;flow:M;partnerCode:528,642,70,784,792,710,842,56
  143. 2022/12/30 22:41:23.516 [I] [com_trade.go:176] 290230获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:7;flow:M;partnerCode:246,705,251,218,757
  144. 2022/12/30 22:41:24.824 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:7;flow:M;partnerCode:0
  145. 2022/12/30 22:41:26.138 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:7;flow:M;partnerCode:68,512,470,222,558,858,508,308,352,591,231,242,780,24,484,807,740,646,524,686,340,268,116,634,860,480,96,400,450,834
  146. 2022/12/30 22:41:27.457 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:7;flow:M;partnerCode:304,624,332,654,795,841,258,577,879,732,835,527,647,86,866,706,232,796,638,4,80,717,12,492,454,798,180,44,697,838,520,776,262,473,887,16,540,129,192,772,850,280,637,581,570,839,810,90,20,334,474,459,230,388,662,534,50,120,288,466,530,531,590,316,398,471,221,175,74,580,891,711,212,720,598,666,800,886,8,238,414,28,64,312,658,296,849,408,278,694,568,698,48,140,584,461,736,836,292,430,659,760,336,472,899,583,536,636,588,239,166,384,457,516,574,837,58,868,254,368,652,226,612,862,266,434,728,200,592,290,674,548,585,729,92,162,364,532,626,10,184,426,148,660,535,234,324,500,356,132,582,890,260,876
  147. 2022/12/30 23:13:14.705 [I] [com_trade.go:176] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:6;flow:M;partnerCode:200,624,636,776,384,140,266,304,12,278,28,260,835,674,531,841,837,430,659,296,472,612,474,520,86,120,457,10,74,500,132,368,637,454,316,434,221,536,459,839,585,810,890,706,862,148,582,698,492,336,795,581,592,577,80,162,548,868,232,426,568,626,262,580,570,654,729,866,175,598,732,64,717,736,838,899,720,398,290,658,212,292,527,638,129,8,92,662,728,879,588,234,58,414,711,760,408,324,388,583,584,534,530,471,192,886,666,697,574,184,90,4,20,516,647,312,364,473,652,694,48,50,590,254,258,772,796,230,288,332,334,849,876,660,540,887,16,466,891,44,166,180,532,850,800,535,226,238,356,239,798,836,461,280
  148. 2022/12/30 23:13:16.091 [I] [com_trade.go:176] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:6;flow:M;partnerCode:222,524,470,231,634,740,24,780,512,508,807,340,484,686,834,352,558,268,68,242,96,116,860,480,858,591,450,646,308,400
  149. 2022/12/30 23:13:17.486 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:6;flow:M;partnerCode:608,498,112,36,682,704,40,348,208,392,600,554,300,440,196,458
  150. 2022/12/31 00:05:18.625 [I] [com_trade.go:176] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:3;flow:M;partnerCode:826,56,757,724,233,251,752,702
  151. 2022/12/31 00:05:19.966 [I] [com_trade.go:176] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:3;flow:M;partnerCode:410,688,703,616,276,620,579,380
  152. 2022/12/31 00:05:21.315 [I] [com_trade.go:176] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:3;flow:M;partnerCode:170,710,528,490,699,428,124,792
  153. 2022/12/31 00:46:55.568 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:2;flow:M;partnerCode:0
  154. 2022/12/31 00:46:56.919 [I] [com_trade.go:176] 290121获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:2;flow:M;partnerCode:697,886,254,80,166,536,324,426,839,796,50,221,800,12,461,364,728,16,48,132,230,660,212,798,548,568,662,184,585,414,500,760,772,527,86,574,288,459,534,590,582,850,368,570,666,408,334,530,336,866,810,90,887,588,332,148,581,612,532,260,388,838,659,472,238,520,795,180,835,296,706,652,278,836,698,717,540,531,862,8,384,290,434,454,636,736,535,262,473,292,624,140,162,304,776,74,234,266,891,20,899,58,841,879,120,654,694,44,466,356,474,658,674,457,10,258,890,647,729,583,638,876,92,471,129,626,732,280,398,637,430,516,200,232,192,312,175,239,837,720,711,849,64,580,4,592,28,577,492,316,584,598,226,868
  155. 2022/12/31 00:46:58.356 [I] [com_trade.go:176] 290121获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2017;月份:2;flow:M;partnerCode:246,643,156,688,826,528,804,616
  156. 2022/12/31 04:15:22.974 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2016;月份:10;flow:M;partnerCode:404,372,604,422,504,788,344,188,51,320,566,586,144,152,818,499
  157. 2022/12/31 04:15:24.490 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2016;月份:10;flow:M;partnerCode:234,296,58,192,16,212,772,694,659,536,457,92,612,760,798,837,527,654,132,592,12,408,582,568,711,384,879,364,532,162,166,200,638,20,574,891,254,535,492,466,266,140,890,74,86,4,660,44,540,776,584,580,585,80,221,796,570,534,239,414,500,636,148,238,312,666,129,232,64,332,292,48,262,581,471,862,304,278,583,530,626,8,520,838,280,800,90,588,28,324,473,662,697,652,720,290,334,598,474,10,899,368,461,180,388,430,835,184,531,647,728,732,288,454,850,624,434,698,398,868,839,356,226,472,336,120,230,717,887,637,577,795,674,706,548,516,50,810,729,836,459,886,258,590,260,866,849,316,426,658,175,841,876,736
  158. 2022/12/31 04:15:26.001 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2016;月份:10;flow:M;partnerCode:231,646,834,591,340,480,68,524,24,308,558,400,352,268,450,116,96,740,860,222,634,780,484,512,242,807,858,470,508,686
  159. 2022/12/31 08:06:11.934 [I] [com_trade.go:176] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2015;月份:12;flow:M;partnerCode:699,642,705,380,579,490,643,703
  160. 2022/12/31 08:06:13.619 [I] [com_trade.go:176] 271114获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2015;月份:12;flow:M;partnerCode:724,710,156,428,620,70,784,616
  161. 2022/12/31 09:51:10.568 [I] [com_trade.go:176] 290244获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2015;月份:4;flow:X;partnerCode:826,70,156,702,428,705,792,764
  162. 2022/12/31 09:51:12.117 [I] [com_trade.go:176] 290244获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2015;月份:4;flow:X;partnerCode:642,203,579,620,752,410,528,804
  163. 2022/12/31 09:58:54.330 [I] [com_trade.go:176] 381900获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2015;月份:4;flow:M;partnerCode:218,276,826,842,764,699,124,643
  164. 2023/01/03 22:26:25.230 [I] [com_trade.go:176] 270730获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:8;flow:X;partnerCode:682,31,348,40,208,458,704,376,32,191,392,360,36,196,554,600
  165. 2023/01/03 22:34:20.413 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:8;flow:M;partnerCode:591,308,352,268,450,524,231,484,807,558,68,646,24,834,740,222,480,470,96,508,858,400,116,340,634,780,242,860,512,686
  166. 2023/01/03 22:34:22.005 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:8;flow:M;partnerCode:894,174,104,84,108,270,418,678,716,204,178,462,496,478,670,60,328,690,533,136,446,768,417,762,562,72,748,854,882,275,52
  167. 2023/01/03 22:47:23.927 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:7;flow:M;partnerCode:591,646,508,24,308,268,558,634,807,480,512,834,780,242,524,352,450,484,470,96,686,116,231,858,340,740,222,860,400,68
  168. 2023/01/03 22:47:25.509 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:7;flow:M;partnerCode:32,214,704,376,208,360,191,112,600,348,554,300,100,76,40,196
  169. 2023/01/03 22:47:26.957 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:7;flow:M;partnerCode:682,392,36,608,440,31,458,498
  170. 2023/01/03 22:56:11.582 [I] [com_trade.go:176] 290230获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:6;flow:X;partnerCode:68,508,400,860,231,740,242,512,96,807,470,268,858,524,222,450,308,646,686,591,480,558,780,634,834,24,340,116,484,352
  171. 2023/01/03 22:56:12.894 [I] [com_trade.go:176] 290230获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:6;flow:X;partnerCode:136,446,496,882,690,204,60,275,462,533,108,178,670,716,52,104,762,328,418,270,84,894,768,72,854,174,417,562,678,478,748
  172. 2023/01/03 23:09:42.931 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:5;flow:M;partnerCode:608,112,40,214,682,392,458,31,348,498,76,600,554,704,191,300
  173. 2023/01/03 23:09:44.481 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:5;flow:M;partnerCode:32,36,360,440,208,196,100,376
  174. 2023/01/03 23:17:27.853 [I] [com_trade.go:176] 270730获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:5;flow:M;partnerCode:608,112,40,214,682,392,458,31,348,498,76,600,554,704,191,300
  175. 2023/01/03 23:17:29.534 [I] [com_trade.go:176] 270730获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:5;flow:M;partnerCode:32,36,360,440,208,196,100,376
  176. 2023/01/03 23:25:00.828 [I] [com_trade.go:176] 381900获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2003;月份:4;flow:M;partnerCode:40,76,704,208,458,608,214,31,300,112,196,191,600,360,348,682
  177. 2023/01/04 01:02:18.808 [I] [com_trade.go:176] 290230获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2002;月份:6;flow:M;partnerCode:835,92,772,230,624,694,192,527,899,384,534,711,80,226,520,647,887,10,434,536,570,254,278,332,530,166,584,876,44,368,414,221,810,636,471,180,583,388,585,64,836,736,212,532,638,706,866,334,408,839,548,292,74,304,598,891,28,426,20,398,473,238,540,666,796,862,4,868,175,492,890,500,850,262,461,290,472,336,296,580,577,459,232,457,8,886,717,90,776,129,430,652,48,760,50,720,728,588,590,637,654,732,474,260,612,581,841,531,837,132,697,592,266,316,140,148,184,356,454,12,364,466,698,795,658,674,838,568,312,86,582,729,879,16,200,288,120,234,574,535,662,798,162,258,239,660,280,659,800,58,516,626,849,324
  178. 2023/01/04 01:02:20.180 [I] [com_trade.go:176] 290230获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2002;月份:6;flow:M;partnerCode:32,31,191,554,76,300,100,348,458,214,40,196,392,704,376,360
  179. 2023/01/04 01:09:53.528 [I] [com_trade.go:176] 290121获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2002;月份:5;flow:X;partnerCode:233,428,203,528,724,792,703,380
  180. 2023/01/04 01:52:58.039 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2002;月份:1;flow:X;partnerCode:0
  181. 2023/01/04 02:00:21.549 [I] [com_trade.go:176] 290122获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2002;月份:1;flow:M;partnerCode:0
  182. 2023/01/04 02:12:09.179 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:12;flow:X;partnerCode:688,579,752,380,764,842,156,70
  183. 2023/01/04 02:29:32.109 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:11;flow:M;partnerCode:710,442,699,616,703
  184. 2023/01/04 02:29:33.532 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:11;flow:M;partnerCode:0
  185. 2023/01/04 02:29:34.888 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:11;flow:M;partnerCode:768,204,84,854,60,72,716,136,275,748,178,762,562,496,446,690,104,678,894,478,52,328,533,882,108,270,462,417,670,418,174
  186. 2023/01/04 02:37:00.528 [I] [com_trade.go:176] 381900获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:10;flow:X;partnerCode:152,372,504,188,499,818,344,404,422,566,144,604,320,51,788,586
  187. 2023/01/04 02:37:01.816 [I] [com_trade.go:176] 381900获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:10;flow:X;partnerCode:882,533,690,84,270,275,496,446,104,462,60,762,204,417,418,328,748,478,72,52,854,136,178,562,670,716,108,678,894,174,768
  188. 2023/01/04 02:37:03.143 [I] [com_trade.go:176] 381900获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:10;flow:X;partnerCode:0
  189. 2023/01/04 02:44:23.164 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:10;flow:M;partnerCode:528,710,688,642,784,442,124,705
  190. 2023/01/04 02:44:24.971 [I] [com_trade.go:176] 290243获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:10;flow:M;partnerCode:616,410,724,804,490,826,380,620
  191. 2023/01/04 02:58:29.473 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:9;flow:M;partnerCode:498,682,600,458,392,32,440,360
  192. 2023/01/04 02:58:31.133 [I] [com_trade.go:176] 270750获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:9;flow:M;partnerCode:566,422,144,788,504,404,152,188,604,372,818,320,586,499,51,344
  193. 2023/01/04 03:24:13.204 [I] [com_trade.go:176] 270730获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:7;flow:M;partnerCode:604,144,320,372,586,566,422,344,499,188,788,504,818,51,404,152
  194. 2023/01/04 03:24:14.661 [I] [com_trade.go:176] 270730获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:7;flow:M;partnerCode:882,72,462,768,270,690,678,894,478,108,52,104,84,60,670,762,496,446,417,204,562,533,328,716,174,178,418,275,854,136,748
  195. 2023/01/04 03:33:55.137 [I] [com_trade.go:176] 270730获取数据失败,err:map[error:Something went wrong. Please try again or contact comtrade@un.org];errObject:<nil>;年份:2001;月份:6;flow:M;partnerCode:191,360,682,600,704,440,554,392,196,40,36,100,32,76,112,214`
  196. tmpList := strings.Split(str, "\n")
  197. list := make([]TmpStruct, 0)
  198. for _, v := range tmpList {
  199. //fmt.Println(v)
  200. tmp := strings.Split(v, "获取数据失败")
  201. //fmt.Println(tmp)
  202. // 指标编码
  203. codeList := strings.Split(tmp[0], " ")
  204. indexCode := codeList[2]
  205. //fmt.Println(indexCode)
  206. // 年度
  207. //年份:2001;月份:6;flow:M;partnerCode:191,360,682,600,704,440,554,392,196,40,36,100,32,76,112,214`
  208. tmp = strings.Split(tmp[1], "年份:")
  209. // 指标编码
  210. tmpList2 := strings.Split(tmp[1], ";")
  211. yearStr := tmpList2[0]
  212. year, _ := strconv.Atoi(yearStr)
  213. //fmt.Println(year)
  214. // 月度
  215. //月份:6;flow:M;partnerCode:191,360,682,600,704,440,554,392,196,40,36,100,32,76,112,214`
  216. tmp = strings.Split(tmp[1], "月份:")
  217. // 指标编码
  218. tmpList2 = strings.Split(tmp[1], ";")
  219. monthStr := tmpList2[0]
  220. month, _ := strconv.Atoi(monthStr)
  221. //fmt.Println(month)
  222. // flow
  223. //flow:M;partnerCode:191,360,682,600,704,440,554,392,196,40,36,100,32,76,112,214`
  224. tmp = strings.Split(tmp[1], "flow:")
  225. // 指标编码
  226. tmpList2 = strings.Split(tmp[1], ";")
  227. flowCode := tmpList2[0]
  228. //fmt.Println(flowCode)
  229. // partnerCode
  230. //flow:M;partnerCode:191,360,682,600,704,440,554,392,196,40,36,100,32,76,112,214`
  231. tmp = strings.Split(tmp[1], "partnerCode:")
  232. // 指标编码
  233. partnerCode := tmp[1]
  234. //fmt.Println(partnerCode)
  235. list = append(list, TmpStruct{
  236. IndexCode: indexCode,
  237. Year: year,
  238. Month: month,
  239. FlowCode: flowCode,
  240. PartnerCode: partnerCode,
  241. })
  242. }
  243. //year := 2021
  244. //month := 6
  245. //flowCode := "M"
  246. //comTradeId := 6
  247. //indexCode := `381900`
  248. //indexNameCn := `石脑油(出口退税率0%)`
  249. //thirdIdStr := `636,278,624,839,384,234,296,694,866,835,50,230,662,48,697,290,12,388,454,457,582,798,80,364,674,58,836,838,44,86,500,471,90,368,795,647,899,720,336,520,226,800,612,548,16,531,332,398,652,472,473,581,430,536,568,879,717,598,706,8,258,638,886,810,312,334,534,254,324,841,120,492,148,577,760,592,666`
  250. //thirdIdStr := `588,583,316,849,887,626,129,580,530,288,414,796,466,584,891,292,356,426,540,74,184,459,10,535,166,4,266,585,658,862,660,232,92,239,0,776,474,280,736,64,221,516,132,698,162,434,461,180,654,262,570,850,238,175,659,637,711,527,732,20,140,868,532,200,728,729,772,192,260,28,212,890,876,837,590,408,304,574`
  251. //thirdIdStr := `203,218,276,757`
  252. //thirdIdStr := `380,784,56,528`
  253. dateType := `month`
  254. //codeList := []string{
  255. // `835,729,120,221,500,184,200,0,666,258,624,426,532,574,720,536,534,471,581,312,434,568,659,408,334,590,162,296,530,140,80,90,8,711`,
  256. // `866,732,736,364,459,698,706,12,674,234,516,891,849,260,304,388,899,492,612,660,772,694,862,697,592,472,324,531,226,92,28,232,583,647,654,166,64,838,336,430,58`,
  257. //}
  258. codeMappingListMap := make(map[string]string)
  259. codeMappingList, err := models.GetAllComTradeCodeMappingList()
  260. if err != nil {
  261. return
  262. }
  263. for _, v := range codeMappingList {
  264. codeMappingListMap[v.Code] = v.Name
  265. }
  266. countryMap, err := GetAllCountry()
  267. comTradeList, err := models.GetAllComTradeIndexList()
  268. if err != nil {
  269. return
  270. }
  271. comTradeMap := make(map[string]*models.ComTradeIndex)
  272. for _, v := range comTradeList {
  273. comTradeMap[v.IndexCode] = v
  274. }
  275. for key, v := range list {
  276. comTradeInfo, ok := comTradeMap[v.IndexCode]
  277. if !ok {
  278. fmt.Println("找不到指标")
  279. }
  280. //fmt.Println(v)
  281. fmt.Println(key, ";开始:", ";code:", v.IndexCode, ";年份:", v.Year, ";月份:", v.Month, "的", v.FlowCode, ";国家:", v.PartnerCode)
  282. syncComTradeData(comTradeInfo.ComTradeId, v.IndexCode, comTradeInfo.IndexNameCn, v.FlowCode, v.PartnerCode, dateType, v.Year, v.Month, codeMappingListMap, countryMap)
  283. fmt.Println(key, ";结束:", ";code:", v.IndexCode, ";年份:", v.Year, ";月份:", v.Month, "的", v.FlowCode, ";国家:", v.PartnerCode)
  284. time.Sleep(1 * time.Second)
  285. }
  286. }
  287. func syncAllComTradeData2(year, month int, flowCode, dateType string) (err error) {
  288. codeMappingListMap := make(map[string]string)
  289. codeMappingList, err := models.GetAllComTradeCodeMappingList()
  290. if err != nil {
  291. return
  292. }
  293. for _, v := range codeMappingList {
  294. codeMappingListMap[v.Code] = v.Name
  295. }
  296. comTradeList, err := models.GetAllComTradeIndexList()
  297. if err != nil {
  298. return
  299. }
  300. lenComTradeList := len(comTradeList)
  301. countryMap, err := GetAllCountry()
  302. if err != nil {
  303. return
  304. }
  305. countryGroup := make(map[int][]models.ComTradeCountry)
  306. for _, v := range countryMap {
  307. togetherCountry := v.TogetherCountry
  308. if dateType == "month" {
  309. togetherCountry = v.MonthTogetherCountry
  310. }
  311. tmpList, ok := countryGroup[togetherCountry]
  312. if !ok {
  313. tmpList = make([]models.ComTradeCountry, 0)
  314. }
  315. countryGroup[togetherCountry] = append(tmpList, v)
  316. }
  317. //fmt.Println(countryGroup)
  318. i := 0
  319. startTime := time.Now()
  320. fmt.Println("整体开始了=============")
  321. for _, v := range comTradeList {
  322. fmt.Println("剩余", lenComTradeList-i, "个指标=============")
  323. syncSingleComTradeData(v.ComTradeId, year, month, flowCode, v.IndexCode, v.IndexNameCn, dateType, countryMap, codeMappingListMap, countryGroup)
  324. i++
  325. }
  326. fmt.Println("整体结束了=============")
  327. fmt.Println("耗时", time.Now().Sub(startTime).String())
  328. return
  329. }
  330. func syncAllComTradeData() (err error) {
  331. codeMappingListMap := make(map[string]string)
  332. codeMappingList, err := models.GetAllComTradeCodeMappingList()
  333. if err != nil {
  334. return
  335. }
  336. for _, v := range codeMappingList {
  337. codeMappingListMap[v.Code] = v.Name
  338. }
  339. comTradeList, err := models.GetAllComTradeIndexList()
  340. if err != nil {
  341. return
  342. }
  343. lenComTradeList := len(comTradeList)
  344. countryMap, err := GetAllCountry()
  345. if err != nil {
  346. return
  347. }
  348. countryGroup := make(map[int][]models.ComTradeCountry)
  349. for _, v := range countryMap {
  350. tmpList, ok := countryGroup[v.TogetherCountry]
  351. if !ok {
  352. tmpList = make([]models.ComTradeCountry, 0)
  353. }
  354. countryGroup[v.TogetherCountry] = append(tmpList, v)
  355. }
  356. //fmt.Println(countryGroup)
  357. //flowCode := "X"
  358. flowCode := "M"
  359. year := 2022
  360. i := 0
  361. startTime := time.Now()
  362. fmt.Println("整体开始了=============")
  363. for _, v := range comTradeList {
  364. fmt.Println("剩余", lenComTradeList-i, "个指标=============")
  365. syncSingleComTradeData(v.ComTradeId, year, 0, flowCode, v.IndexCode, v.IndexNameCn, "year", countryMap, codeMappingListMap, countryGroup)
  366. i++
  367. }
  368. fmt.Println("整体结束了=============")
  369. fmt.Println("耗时", time.Now().Sub(startTime).String())
  370. return
  371. }
  372. func syncSingleComTradeData(comTradeId, year, month int, flowCode, indexCode, indexNameCn, dateType string, countryMap map[int]models.ComTradeCountry, codeMappingListMap map[string]string, countryGroup map[int][]models.ComTradeCountry) {
  373. ////flowCode := "X"
  374. //flowCode := "M"
  375. //year := 2020
  376. //partnerCode := 842
  377. lenCountry := len(countryMap)
  378. i := 0
  379. fmt.Println(indexCode, "开始了")
  380. for needMergeNum, countryList := range countryGroup {
  381. mergeNum := 0
  382. thirdIdList := make([]string, 0)
  383. lenCountryList := len(countryList) - 1
  384. for countryIndex, v := range countryList {
  385. fmt.Println(indexCode, "剩余", lenCountry-i, "条数据")
  386. i++
  387. // 开始处理
  388. mergeNum++
  389. thirdIdList = append(thirdIdList, strconv.Itoa(v.ThirdId))
  390. if mergeNum < needMergeNum && countryIndex < lenCountryList {
  391. continue
  392. }
  393. syncComTradeData(comTradeId, indexCode, indexNameCn, flowCode, strings.Join(thirdIdList, ","), dateType, year, month, codeMappingListMap, countryMap)
  394. // 重置
  395. mergeNum = 0
  396. thirdIdList = make([]string, 0)
  397. time.Sleep(1 * time.Second)
  398. }
  399. }
  400. fmt.Println(indexCode, "结束了")
  401. }
  402. func SyncSingleComTradeData3() {
  403. comTradeId := 7
  404. year := 2022
  405. month := 11
  406. flowCode := "M"
  407. indexCode := "290121"
  408. indexNameCn := `乙烯(出口退税率13%)`
  409. dateType := `month`
  410. codeMappingListMap := make(map[string]string)
  411. codeMappingList, err := models.GetAllComTradeCodeMappingList()
  412. if err != nil {
  413. return
  414. }
  415. for _, v := range codeMappingList {
  416. codeMappingListMap[v.Code] = v.Name
  417. }
  418. countryMap, err := GetAllCountry()
  419. if err != nil {
  420. return
  421. }
  422. countryGroup := make(map[int][]models.ComTradeCountry)
  423. for _, v := range countryMap {
  424. togetherCountry := v.TogetherCountry
  425. if dateType == "month" {
  426. togetherCountry = v.MonthTogetherCountry
  427. }
  428. tmpList, ok := countryGroup[togetherCountry]
  429. if !ok {
  430. tmpList = make([]models.ComTradeCountry, 0)
  431. }
  432. countryGroup[togetherCountry] = append(tmpList, v)
  433. }
  434. //fmt.Println(countryGroup)
  435. fmt.Println(indexCode, "开始了")
  436. thirdIdStr := `849,192,12,296,212,473,234,364,879,580,698,20,582,850,316,568,459,697,866,540,4,280,288,461,658,44,430,598,798,868,92,180,536,810,574,266,841,16,140,148,652,334,637,660,232,760,471,332,520,548,516,839,414,531,200,64,166,581,837,221,592,500,466,292,336,48,886,290,80,120,262,583,795,612,836,258,530,736,838,368,8,28,129,278,90,800,184,674,588,492,887,230,570,534,776,239,527,891,0,356,729,226,324,876,626,720,426,654,590,312,772,706,50,304,457,796,408,434,624,454,474,254,532,584,10,132,835,662,666,717,260,728,398,711,238,890,899,74,388,585,472,58,577,647,659,638,862,732,535,86,175,636,694,162,384`
  437. syncComTradeData(comTradeId, indexCode, indexNameCn, flowCode, thirdIdStr, dateType, year, month, codeMappingListMap, countryMap)
  438. fmt.Println(indexCode, "结束了")
  439. }
  440. func syncComTradeData(comTradeId int, indexCode, indexNameCn, flowCode, partnerCode, dateType string, year, month int, codeMappingListMap map[string]string, countryMap map[int]models.ComTradeCountry) (err error) {
  441. // 获取数据
  442. //https://comtradeapi.un.org/public/v1/preview/C/A/HS?period=2021&partnerCode=842&cmdCode=290244&flowCode=X&customsCode=C00&motCode=0
  443. params := make(map[string]string)
  444. //period=2021&partnerCode=842
  445. params["motCode"] = "0" //运输方式
  446. params["customsCode"] = "C00" //海关编码
  447. params["cmdCode"] = indexCode //指标code
  448. params["flowCode"] = flowCode //贸易流向,X:出口(Export);M:进口(Import)
  449. params["partnerCode"] = partnerCode //进口国id
  450. params["period"] = strconv.Itoa(year) //年份(月份)
  451. dataTime := fmt.Sprint(year, "-12-31")
  452. codeStr := "Y"
  453. dataType := 1
  454. if dateType == "month" {
  455. monthStr := strconv.Itoa(month)
  456. if month < 10 {
  457. monthStr = "0" + monthStr
  458. }
  459. params["period"] = strconv.Itoa(year) + monthStr //年份(月份)
  460. dataTime = fmt.Sprint(year, "-", monthStr, "-01")
  461. dataTimeT, tmpErr := time.ParseInLocation(utils.FormatDate, dataTime, time.Local)
  462. if tmpErr != nil {
  463. fmt.Println(dataTime, "时间转换异常")
  464. return
  465. }
  466. dataTime = dataTimeT.AddDate(0, 1, -1).Format(utils.FormatDate)
  467. codeStr = "M"
  468. dataType = 2
  469. }
  470. // 这是获取数据的链接(月度的)
  471. comeData, err := queryComeTradeData(params, dateType)
  472. if err != nil {
  473. fmt.Println("读取失败", err)
  474. return
  475. }
  476. //fmt.Println(comeData)
  477. if comeData.Count >= 500 {
  478. msg := fmt.Sprint(indexCode, ";数据超限了", ";年份:", year, ";月份:", month, ";flow:", flowCode, ";partnerCode:", partnerCode)
  479. fmt.Println(msg)
  480. fmt.Println(comeData.Count)
  481. utils.FileLog.Info(msg)
  482. } else if comeData.Count == -1 {
  483. msg := fmt.Sprint(indexCode, ";获取数据失败,err:", comeData.Error, ";errObject:", comeData.ErrorObject, ";年份:", year, ";月份:", month, ";flow:", flowCode, ";partnerCode:", partnerCode)
  484. fmt.Println(msg, comeData)
  485. utils.FileLog.Info(msg)
  486. return
  487. }
  488. listMap := make(map[string][]*models.ComTradeData, 0)
  489. for _, v := range comeData.Data {
  490. //fmt.Println(v)
  491. var reporterName, partnerName, partner2Name, reporterCnName, partnerCnName, partner2CnName string
  492. if reporter, ok := countryMap[v.ReporterCode]; ok {
  493. reporterCnName = reporter.NameCn
  494. reporterName = reporter.Name
  495. }
  496. if partner, ok := countryMap[v.PartnerCode]; ok {
  497. partnerCnName = partner.NameCn
  498. partnerName = partner.Name
  499. }
  500. if partner, ok := countryMap[v.Partner2Code]; ok {
  501. partner2CnName = partner.NameCn
  502. partner2Name = partner.Name
  503. }
  504. //ValueCode:HS+Reporter+trade flow+Partner+Value 例如:270730ChinaXUSAValue
  505. //WeightCode:HS+Reporter+trade flow+Partner+Weight ,例如:270730ChinaXUSAWeight
  506. tmpIndexTradeCode := utils.TrimStr(fmt.Sprint(v.CmdCode, codeStr, reporterName, flowCode, partnerName, "2nd", partner2Name, "Value"))
  507. tmpIndexNetWeightCode := utils.TrimStr(fmt.Sprint(v.CmdCode, codeStr, reporterName, flowCode, partnerName, "2nd", partner2Name, "Weight"))
  508. tmpData := &models.ComTradeData{
  509. ComTradeDataId: 0,
  510. ComTradeId: comTradeId,
  511. IndexCode: v.CmdCode,
  512. Flow: flowCode,
  513. ReporterCode: v.ReporterCode,
  514. ReporterName: reporterCnName,
  515. PartnerCode: v.PartnerCode,
  516. PartnerName: partnerCnName,
  517. Partner2Code: v.Partner2Code,
  518. Partner2Name: partner2CnName,
  519. DateType: dataType,
  520. DataTime: dataTime,
  521. IndexTradeCode: tmpIndexTradeCode,
  522. TradeValue: v.PrimaryValue,
  523. IndexNetWeightCode: tmpIndexNetWeightCode,
  524. NetWeightValue: v.NetWgt,
  525. ModifyTime: time.Now(),
  526. CreateTime: time.Now(),
  527. }
  528. tmpList, ok := listMap[v.CmdCode]
  529. if !ok {
  530. tmpList = make([]*models.ComTradeData, 0)
  531. }
  532. listMap[v.CmdCode] = append(tmpList, tmpData)
  533. //指标关系
  534. {
  535. tmpName := indexNameCn + "年度" + reporterCnName
  536. if dateType == "month" {
  537. tmpName = indexNameCn + "月度" + reporterCnName
  538. }
  539. flow := ``
  540. if tmpData.Flow == `X` {
  541. flow = "出口"
  542. } else {
  543. flow = "进口"
  544. }
  545. tmpName += flow
  546. finalpartnerName := ``
  547. if partnerCnName == partner2CnName {
  548. if partnerCnName == "世界" {
  549. //若对手国和第二对手国是world时,则翻译为总量;
  550. finalpartnerName = "总量"
  551. } else {
  552. //若对手国或第二对手国是相同的,只需翻译一个;
  553. finalpartnerName = partnerCnName
  554. }
  555. } else {
  556. if partnerCnName == "世界" || partner2CnName == "世界" {
  557. //若对手国或第二对手国是world的,只需翻译非world的对手国;
  558. if partnerCnName == "世界" {
  559. finalpartnerName = partner2CnName
  560. } else {
  561. finalpartnerName = partnerCnName
  562. }
  563. } else {
  564. //若对手国和第二对手国不一样,翻译成:对手国和第二对手国
  565. finalpartnerName = partnerCnName + "和" + partner2CnName
  566. }
  567. }
  568. tmpName += finalpartnerName
  569. //fmt.Println(tmpData.IndexTradeCode)
  570. //fmt.Println(tmpName)
  571. valueName := tmpName + "贸易金额"
  572. weightName := tmpName + "贸易重量"
  573. if name, ok := codeMappingListMap[tmpData.IndexTradeCode]; !ok {
  574. comTradeCodeMappingType := 1
  575. if codeStr == "M" {
  576. comTradeCodeMappingType = 3
  577. }
  578. tmpComTradeCodeMapping := &models.ComTradeCodeMapping{
  579. Code: tmpData.IndexTradeCode,
  580. IndexCode: tmpData.IndexCode,
  581. Type: comTradeCodeMappingType,
  582. Name: valueName,
  583. CreateTime: time.Now(),
  584. }
  585. tmpErr := models.AddComTradeCodeMapping(tmpComTradeCodeMapping)
  586. if tmpErr != nil {
  587. fmt.Println(indexCode, "添加贸易金额失败,err:", err)
  588. } else {
  589. codeMappingListMap[tmpData.IndexTradeCode] = valueName
  590. }
  591. } else {
  592. if name != valueName {
  593. tmpComTradeCodeMapping := &models.ComTradeCodeMapping{
  594. Code: tmpData.IndexTradeCode,
  595. Name: valueName,
  596. }
  597. e := models.UpdateComTradeCodeMapping(tmpComTradeCodeMapping, []string{"Name"})
  598. if e != nil {
  599. err = e
  600. return
  601. }
  602. utils.FileLog.Info("更新指标名称", "旧:"+name, "新:"+valueName, "code:"+tmpData.IndexTradeCode)
  603. fmt.Println("更新指标名称", "旧:"+name, "新:"+valueName)
  604. }
  605. }
  606. if name, ok := codeMappingListMap[tmpData.IndexNetWeightCode]; !ok {
  607. comTradeCodeMappingType := 2
  608. if codeStr == "M" {
  609. comTradeCodeMappingType = 3
  610. }
  611. tmpComTradeCodeMapping := &models.ComTradeCodeMapping{
  612. Code: tmpData.IndexNetWeightCode,
  613. IndexCode: tmpData.IndexCode,
  614. Type: comTradeCodeMappingType,
  615. Name: weightName,
  616. CreateTime: time.Now(),
  617. }
  618. tmpErr := models.AddComTradeCodeMapping(tmpComTradeCodeMapping)
  619. if tmpErr != nil {
  620. fmt.Println(indexCode, "添加贸易重量失败,err:", err)
  621. } else {
  622. codeMappingListMap[tmpData.IndexNetWeightCode] = weightName
  623. }
  624. } else {
  625. if name != weightName {
  626. tmpComTradeCodeMapping := &models.ComTradeCodeMapping{
  627. Code: tmpData.IndexTradeCode,
  628. Name: valueName,
  629. }
  630. e := models.UpdateComTradeCodeMapping(tmpComTradeCodeMapping, []string{"Name"})
  631. if e != nil {
  632. err = e
  633. return
  634. }
  635. utils.FileLog.Info("更新指标名称", "旧:"+name, "新:"+valueName, "code:"+tmpData.IndexNetWeightCode)
  636. fmt.Println("更新指标名称", "旧:"+name, "新:"+valueName)
  637. }
  638. }
  639. }
  640. }
  641. for indexCode, list := range listMap {
  642. models.HandleComTradeData(list, indexCode, flowCode, dataTime, partnerCode)
  643. }
  644. return
  645. }
  646. // queryComeTradeData 接口请求网站数据
  647. func queryComeTradeData(params map[string]string, dateType string) (comTradeResp ComTradeResp, err error) {
  648. //ComTradeUrl
  649. // 待签名字符串
  650. paramStr := ``
  651. for index, val := range params {
  652. paramStr += index + `=` + val + `&`
  653. }
  654. paramStr = paramStr[:len(paramStr)-1]
  655. urlStr := ComTradeUrl + paramStr
  656. if dateType == "month" {
  657. urlStr = ComTradeMonthUrl + paramStr
  658. }
  659. //fmt.Println("urlStr:", urlStr)
  660. body, err := http.Get(urlStr)
  661. if err != nil {
  662. fmt.Println("err:", err)
  663. }
  664. //utils.FileLog.Info("queryComeTradeData 数据:" + ComTradeUrl + paramStr)
  665. err = json.Unmarshal(body, &comTradeResp)
  666. return
  667. }
  668. type ComTradeResp struct {
  669. ElapsedTime string `json:"elapsedTime"`
  670. Count int `json:"count"`
  671. Data []struct {
  672. TypeCode string `json:"typeCode"`
  673. FreqCode string `json:"freqCode"`
  674. RefPeriodId int `json:"refPeriodId"`
  675. RefYear int `json:"refYear"`
  676. RefMonth int `json:"refMonth"`
  677. Period string `json:"period"`
  678. ReporterCode int `json:"reporterCode"`
  679. ReporterISO interface{} `json:"reporterISO"`
  680. ReporterDesc interface{} `json:"reporterDesc"`
  681. FlowCode string `json:"flowCode"`
  682. FlowDesc interface{} `json:"flowDesc"`
  683. PartnerCode int `json:"partnerCode"`
  684. PartnerISO interface{} `json:"partnerISO"`
  685. PartnerDesc interface{} `json:"partnerDesc"`
  686. Partner2Code int `json:"partner2Code"`
  687. Partner2ISO interface{} `json:"partner2ISO"`
  688. Partner2Desc interface{} `json:"partner2Desc"`
  689. ClassificationCode string `json:"classificationCode"`
  690. ClassificationSearchCode string `json:"classificationSearchCode"`
  691. IsOriginalClassification bool `json:"isOriginalClassification"`
  692. CmdCode string `json:"cmdCode"`
  693. CmdDesc interface{} `json:"cmdDesc"`
  694. AggrLevel int `json:"aggrLevel"`
  695. IsLeaf interface{} `json:"isLeaf"`
  696. CustomsCode string `json:"customsCode"`
  697. CustomsDesc interface{} `json:"customsDesc"`
  698. MosCode string `json:"mosCode"`
  699. MotCode int `json:"motCode"`
  700. MotDesc interface{} `json:"motDesc"`
  701. QtyUnitCode int `json:"qtyUnitCode"`
  702. QtyUnitAbbr interface{} `json:"qtyUnitAbbr"`
  703. Qty float64 `json:"qty"`
  704. IsQtyEstimated bool `json:"isQtyEstimated"`
  705. AltQtyUnitCode int `json:"altQtyUnitCode"`
  706. AltQtyUnitAbbr interface{} `json:"altQtyUnitAbbr"`
  707. AltQty float64 `json:"altQty"`
  708. IsAltQtyEstimated bool `json:"isAltQtyEstimated"`
  709. NetWgt float64 `json:"netWgt"`
  710. IsNetWgtEstimated bool `json:"isNetWgtEstimated"`
  711. GrossWgt float64 `json:"grossWgt"`
  712. IsGrossWgtEstimated bool `json:"isGrossWgtEstimated"`
  713. Cifvalue float64 `json:"cifvalue"`
  714. Fobvalue float64 `json:"fobvalue"`
  715. PrimaryValue float64 `json:"primaryValue"`
  716. LegacyEstimationFlag int `json:"legacyEstimationFlag"`
  717. IsReported bool `json:"isReported"`
  718. IsAggregate bool `json:"isAggregate"`
  719. } `json:"data"`
  720. Error interface{} `json:"error"`
  721. ErrorObject interface{} `json:"errorObject"`
  722. }
  723. func GetAllCountry() (countryMap map[int]models.ComTradeCountry, err error) {
  724. countryMap = make(map[int]models.ComTradeCountry)
  725. tmpList, err := models.GetComTradeCountryAll()
  726. if err != nil {
  727. return
  728. }
  729. for _, v := range tmpList {
  730. countryMap[v.ThirdId] = *v
  731. }
  732. return
  733. }
  734. func syncAllComTradeMapping() (err error) {
  735. codeMappingListMap := make(map[string]bool)
  736. codeMappingList, err := models.GetAllComTradeCodeMappingList()
  737. if err != nil {
  738. return
  739. }
  740. for _, v := range codeMappingList {
  741. codeMappingListMap[v.Code] = true
  742. }
  743. comTradeIndexMap := make(map[string]string)
  744. comTradeIndexList, err := models.GetAllComTradeIndexList()
  745. if err != nil {
  746. return
  747. }
  748. for _, v := range comTradeIndexList {
  749. comTradeIndexMap[v.IndexCode] = v.IndexNameCn
  750. }
  751. list, err := models.GetAllComTradeDataList()
  752. if err != nil {
  753. return
  754. }
  755. countryMap, err := GetAllCountry()
  756. if err != nil {
  757. return
  758. }
  759. lenList := len(list)
  760. fmt.Println("总共", lenList, "条")
  761. for k, v := range list {
  762. fmt.Println("剩余:", lenList-k-1, "条")
  763. var reporterName, partnerName, partner2Name string
  764. if reporter, ok := countryMap[v.ReporterCode]; ok {
  765. reporterName = reporter.Name
  766. }
  767. if partner, ok := countryMap[v.PartnerCode]; ok {
  768. partnerName = partner.Name
  769. }
  770. if partner, ok := countryMap[v.Partner2Code]; ok {
  771. partner2Name = partner.Name
  772. }
  773. tmpValueCode := fmt.Sprint(v.IndexCode, "Y", reporterName, v.Flow, partnerName, "2nd", partner2Name, "Value")
  774. tmpNetWeightCode := fmt.Sprint(v.IndexCode, "Y", reporterName, v.Flow, partnerName, "2nd", partner2Name, "Weight")
  775. //v.IndexTradeCode = utils.TrimStr(tmpValueCode)
  776. //v.IndexNetWeightCode = utils.TrimStr(tmpNetWeightCode)
  777. //v.Update([]string{"IndexTradeCode", "IndexNetWeightCode"})
  778. models.UpdateComTradeData(utils.TrimStr(tmpValueCode), utils.TrimStr(tmpNetWeightCode), v.IndexTradeCode)
  779. }
  780. fmt.Println("整体结束了=============")
  781. return
  782. }
  783. func syncAllComTradeMapping2() (err error) {
  784. codeMappingListMap := make(map[string]bool)
  785. codeMappingList, err := models.GetAllComTradeCodeMappingList()
  786. if err != nil {
  787. return
  788. }
  789. for _, v := range codeMappingList {
  790. codeMappingListMap[v.Code] = true
  791. }
  792. comTradeIndexMap := make(map[string]string)
  793. comTradeIndexList, err := models.GetAllComTradeIndexList()
  794. if err != nil {
  795. return
  796. }
  797. for _, v := range comTradeIndexList {
  798. comTradeIndexMap[v.IndexCode] = v.IndexNameCn
  799. }
  800. list, err := models.GetAllComTradeDataList()
  801. if err != nil {
  802. return
  803. }
  804. countryMap, err := GetAllCountry()
  805. if err != nil {
  806. return
  807. }
  808. lenList := len(list)
  809. fmt.Println("总共", lenList, "条")
  810. dataList := make([]*models.ComTradeCodeMapping, 0)
  811. for k, v := range list {
  812. fmt.Println("剩余:", lenList-k-1, "条")
  813. var reporterName string
  814. if reporter, ok := countryMap[v.ReporterCode]; ok {
  815. reporterName = reporter.NameCn
  816. }
  817. tmpName := ``
  818. if comTradeIndexName, ok := comTradeIndexMap[v.IndexCode]; ok {
  819. tmpName = comTradeIndexName
  820. }
  821. tmpName += reporterName
  822. flow := ``
  823. if v.Flow == `X` {
  824. flow = "出口"
  825. } else {
  826. flow = "进口"
  827. }
  828. tmpName += flow
  829. valueName := tmpName + "贸易金额"
  830. weightName := tmpName + "贸易重量"
  831. if _, ok := codeMappingListMap[v.IndexTradeCode]; !ok {
  832. tmpData := &models.ComTradeCodeMapping{
  833. Code: v.IndexTradeCode,
  834. IndexCode: v.IndexCode,
  835. Type: 1,
  836. Name: valueName,
  837. CreateTime: time.Now(),
  838. }
  839. dataList = append(dataList, tmpData)
  840. codeMappingListMap[v.IndexTradeCode] = true
  841. //tmpErr := models.AddComTradeCodeMapping(tmpData)
  842. //if tmpErr != nil {
  843. // fmt.Println(indexCode, "添加贸易金额失败,err:", err)
  844. //} else {
  845. // codeMappingListMap[v.IndexTradeCode] = true
  846. //}
  847. }
  848. if _, ok := codeMappingListMap[v.IndexNetWeightCode]; !ok {
  849. tmpData := &models.ComTradeCodeMapping{
  850. Code: v.IndexNetWeightCode,
  851. IndexCode: v.IndexCode,
  852. Type: 2,
  853. Name: weightName,
  854. CreateTime: time.Now(),
  855. }
  856. dataList = append(dataList, tmpData)
  857. codeMappingListMap[v.IndexNetWeightCode] = true
  858. //tmpErr := models.AddComTradeCodeMapping(tmpData)
  859. //if tmpErr != nil {
  860. // fmt.Println(indexCode, "添加贸易重量失败,err:", err)
  861. //} else {
  862. // codeMappingListMap[v.IndexNetWeightCode] = true
  863. //}
  864. }
  865. if len(dataList) >= 2000 {
  866. tmpErr := models.AddMultiComTradeCodeMapping(dataList)
  867. if tmpErr != nil {
  868. fmt.Println(indexCode, "添加贸易重量失败,err:", tmpErr)
  869. }
  870. dataList = make([]*models.ComTradeCodeMapping, 0)
  871. }
  872. }
  873. if len(dataList) > 0 {
  874. tmpErr := models.AddMultiComTradeCodeMapping(dataList)
  875. if tmpErr != nil {
  876. fmt.Println(indexCode, "添加贸易重量失败,err:", tmpErr)
  877. }
  878. dataList = make([]*models.ComTradeCodeMapping, 0)
  879. }
  880. fmt.Println("整体结束了=============")
  881. return
  882. }
  883. func syncAllComTradeMapping3() {
  884. codeMappingListMap := make(map[string]*models.ComTradeCodeMapping)
  885. codeMappingList, err := models.GetAllComTradeCodeMappingList()
  886. if err != nil {
  887. return
  888. }
  889. for _, v := range codeMappingList {
  890. codeMappingListMap[v.Code] = v
  891. }
  892. comTradeIndexMap := make(map[string]string)
  893. comTradeIndexList, err := models.GetAllComTradeIndexList()
  894. if err != nil {
  895. return
  896. }
  897. for _, v := range comTradeIndexList {
  898. comTradeIndexMap[v.IndexCode] = v.IndexNameCn
  899. }
  900. list, err := models.GetAllComTradeDataList()
  901. if err != nil {
  902. return
  903. }
  904. countryMap, err := GetAllCountry()
  905. if err != nil {
  906. return
  907. }
  908. lenList := len(list)
  909. fmt.Println("总共", lenList, "条")
  910. dataList := make([]*models.ComTradeCodeMapping, 0)
  911. for k, v := range list {
  912. fmt.Println("剩余:", lenList-k-1, "条")
  913. var reporterCnName, partnerCnName, partner2CnName string
  914. if reporter, ok := countryMap[v.ReporterCode]; ok {
  915. reporterCnName = reporter.NameCn
  916. }
  917. if partner, ok := countryMap[v.PartnerCode]; ok {
  918. partnerCnName = partner.NameCn
  919. }
  920. if partner, ok := countryMap[v.Partner2Code]; ok {
  921. partner2CnName = partner.NameCn
  922. }
  923. indexNameCn := comTradeIndexMap[v.IndexCode]
  924. if indexNameCn == "" {
  925. fmt.Println(indexNameCn)
  926. fmt.Println(v.IndexCode)
  927. }
  928. codeStr := "Y"
  929. tmpName := indexNameCn + "年度" + reporterCnName
  930. if v.DateType == 2 {
  931. codeStr = "M"
  932. tmpName = indexNameCn + "月度" + reporterCnName
  933. }
  934. flow := ``
  935. if v.Flow == `X` {
  936. flow = "出口"
  937. } else {
  938. flow = "进口"
  939. }
  940. tmpName += flow
  941. finalpartnerName := ``
  942. if partnerCnName == partner2CnName {
  943. if partnerCnName == "世界" {
  944. //若对手国和第二对手国是world时,则翻译为总量;
  945. finalpartnerName = "总量"
  946. } else {
  947. //若对手国或第二对手国是相同的,只需翻译一个;
  948. finalpartnerName = partnerCnName
  949. }
  950. } else {
  951. if partnerCnName == "世界" || partner2CnName == "世界" {
  952. //若对手国或第二对手国是world的,只需翻译非world的对手国;
  953. if partnerCnName == "世界" {
  954. finalpartnerName = partner2CnName
  955. } else {
  956. finalpartnerName = partnerCnName
  957. }
  958. } else {
  959. //若对手国和第二对手国不一样,翻译成:对手国和第二对手国
  960. finalpartnerName = partnerCnName + "和" + partner2CnName
  961. }
  962. }
  963. tmpName += finalpartnerName
  964. valueName := tmpName + "贸易金额"
  965. weightName := tmpName + "贸易重量"
  966. if _, ok := codeMappingListMap[v.IndexTradeCode]; !ok {
  967. comTradeCodeMappingType := 1
  968. if codeStr == "M" {
  969. comTradeCodeMappingType = 3
  970. }
  971. tmpData := &models.ComTradeCodeMapping{
  972. Code: v.IndexTradeCode,
  973. IndexCode: v.IndexCode,
  974. Type: comTradeCodeMappingType,
  975. Name: valueName,
  976. CreateTime: time.Now(),
  977. }
  978. dataList = append(dataList, tmpData)
  979. codeMappingListMap[v.IndexTradeCode] = tmpData
  980. //tmpErr := models.AddComTradeCodeMapping(tmpData)
  981. //if tmpErr != nil {
  982. // fmt.Println(indexCode, "添加贸易金额失败,err:", err)
  983. //} else {
  984. // codeMappingListMap[v.IndexTradeCode] = true
  985. //}
  986. }
  987. if _, ok := codeMappingListMap[v.IndexNetWeightCode]; !ok {
  988. comTradeCodeMappingType := 2
  989. if codeStr == "M" {
  990. comTradeCodeMappingType = 4
  991. }
  992. tmpData := &models.ComTradeCodeMapping{
  993. Code: v.IndexNetWeightCode,
  994. IndexCode: v.IndexCode,
  995. Type: comTradeCodeMappingType,
  996. Name: weightName,
  997. CreateTime: time.Now(),
  998. }
  999. dataList = append(dataList, tmpData)
  1000. codeMappingListMap[v.IndexNetWeightCode] = tmpData
  1001. //tmpErr := models.AddComTradeCodeMapping(tmpData)
  1002. //if tmpErr != nil {
  1003. // fmt.Println(indexCode, "添加贸易重量失败,err:", err)
  1004. //} else {
  1005. // codeMappingListMap[v.IndexNetWeightCode] = true
  1006. //}
  1007. }
  1008. if len(dataList) >= 2000 {
  1009. tmpErr := models.AddMultiComTradeCodeMapping(dataList)
  1010. if tmpErr != nil {
  1011. fmt.Println(indexCode, "添加贸易重量失败,err:", tmpErr)
  1012. }
  1013. dataList = make([]*models.ComTradeCodeMapping, 0)
  1014. }
  1015. }
  1016. if len(dataList) > 0 {
  1017. tmpErr := models.AddMultiComTradeCodeMapping(dataList)
  1018. if tmpErr != nil {
  1019. fmt.Println(indexCode, "添加贸易重量失败,err:", tmpErr)
  1020. }
  1021. dataList = make([]*models.ComTradeCodeMapping, 0)
  1022. }
  1023. fmt.Println("整体结束了=============")
  1024. return
  1025. }