classify.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package national_data
  2. import (
  3. "encoding/json"
  4. "eta/eta_crawler/models"
  5. "eta/eta_crawler/services/alarm_msg"
  6. "eta/eta_crawler/utils"
  7. "fmt"
  8. "net/url"
  9. "time"
  10. )
  11. // QuotaClassifyTreeResp 指标分类树响应
  12. type QuotaClassifyTreeResp struct {
  13. Id string `description:"分类ID(字符串)"`
  14. IsParent bool `description:"是否为父级"`
  15. Name string `description:"分类名称"`
  16. Pid string `description:"父级分类ID"`
  17. Dbcode string `description:"源-dbocde"`
  18. Wdcode string `description:"源-wdcode"`
  19. }
  20. // SyncQuotaClassifyTree 同步指标分类树
  21. func SyncQuotaClassifyTree() (err error) {
  22. defer func() {
  23. if err != nil {
  24. utils.FileLog.Error("统计局-同步指标分类树失败, ErrMsg: %s", err.Error())
  25. go alarm_msg.SendAlarmMsg(fmt.Sprintf("统计局-同步指标分类树失败, ErrMsg: %s", err.Error()), 3)
  26. }
  27. }()
  28. // 含指标维度
  29. dbCodes := []string{
  30. "hgyd", "hgjd", "hgnd", // 月度/季度/年度
  31. "fsyd", "fsjd", "fsnd", "csyd", "csnd", "gatyd", "gatnd", // 分省月度/分省季度/分省年度/城市月度价格/城市年度数据/港澳台月度/港澳台年度
  32. "gjyd", "gjydsdj", "gjydsc", "gjnd", // 主要国家月度/三大经济体月度/国际市场月度商品/国家年度
  33. }
  34. // 含地区维度
  35. regCodes := []string{
  36. "fsyd", "fsjd", "fsnd", "csyd", "csnd", "gatyd", "gatnd", // 分省月度/分省季度/分省年度/城市月度价格/城市年度数据/港澳台月度/港澳台年度
  37. "gjyd", "gjydsdj", "gjydsc", "gjnd", // 主要国家月度/三大经济体月度/国际市场月度商品/国家年度
  38. }
  39. for _, code := range dbCodes {
  40. fmt.Println("开始同步DbCode: ", code)
  41. // 指标维度
  42. items := make([]*models.BaseFromNationalStatisticsClassify, 0)
  43. resp, e := curlAndFormatQuotaClassify("zb", code, "zb", items)
  44. if e != nil {
  45. err = fmt.Errorf("递归指标分类失败, Err: %s", e.Error())
  46. return
  47. }
  48. items = resp
  49. // 地区维度
  50. if utils.InArrayByStr(regCodes, code) {
  51. regResp, e := curlAndFormatQuotaClassify("reg", code, "reg", items)
  52. if e != nil {
  53. err = fmt.Errorf("递归地区分类失败, Err: %s", e.Error())
  54. return
  55. }
  56. items = append(items, regResp...)
  57. }
  58. // 去重-code+维度code+ID
  59. classifyMap := make(map[string]bool)
  60. classifyOB := new(models.BaseFromNationalStatisticsClassify)
  61. classifyPars := make([]interface{}, 0)
  62. classifies, e := classifyOB.GetItemsByCondition("", classifyPars, []string{}, "")
  63. if e != nil {
  64. err = fmt.Errorf("获取指标分类列表失败, Err: %s", e.Error())
  65. return
  66. }
  67. for _, c := range classifies {
  68. classifyMap[fmt.Sprintf("%s%s%s", code, c.Wdcode, c.Id)] = true
  69. }
  70. finalList := make([]*models.BaseFromNationalStatisticsClassify, 0)
  71. for _, v := range items {
  72. if classifyMap[fmt.Sprintf("%s%s%s", code, v.Wdcode, v.Id)] {
  73. continue
  74. }
  75. finalList = append(finalList, v)
  76. }
  77. if e = classifyOB.CreateMulti(items); e != nil {
  78. err = fmt.Errorf("批量新增指标分类失败, Err: %s", e.Error())
  79. return
  80. }
  81. fmt.Println("结束同步DbCode: ", code)
  82. }
  83. return
  84. }
  85. // curlAndFormatQuotaClassify 递归请求分类树
  86. func curlAndFormatQuotaClassify(id, dbcode, wdcode string, items []*models.BaseFromNationalStatisticsClassify) (resp []*models.BaseFromNationalStatisticsClassify, err error) {
  87. f := url.Values{}
  88. f.Add("id", id)
  89. f.Add("dbcode", dbcode)
  90. f.Add("wdcode", wdcode)
  91. f.Add("m", "getTree")
  92. r, e := NationalHttpPost(NationalStatisticsBaseReqUrl, f.Encode())
  93. if e != nil {
  94. err = fmt.Errorf("请求指标分类树失败, Err: %s", e.Error())
  95. return
  96. }
  97. list := make([]*QuotaClassifyTreeResp, 0)
  98. if e = json.Unmarshal(r, &list); e != nil {
  99. err = fmt.Errorf("解析响应数据失败, Err: %s", e.Error())
  100. return
  101. }
  102. nowTime := time.Now().Local()
  103. for _, v := range list {
  104. isParent := 0
  105. if v.IsParent {
  106. isParent = 1
  107. }
  108. fmt.Printf("Dbcode: %s, ClassifyName: %s, Id: %s\n", v.Dbcode, v.Name, v.Id)
  109. items = append(items, &models.BaseFromNationalStatisticsClassify{
  110. ClassifyName: v.Name,
  111. Id: v.Id,
  112. Dbcode: v.Dbcode,
  113. Wdcode: v.Wdcode,
  114. Pid: v.Pid,
  115. IsParent: isParent,
  116. CreateTime: nowTime,
  117. ModifyTime: nowTime,
  118. })
  119. // 向下递归
  120. if isParent == 1 {
  121. time.Sleep(5 * time.Second) // 缓缓...毕竟接口是人家的...
  122. items, e = curlAndFormatQuotaClassify(v.Id, v.Dbcode, v.Wdcode, items)
  123. if e != nil {
  124. err = fmt.Errorf("递归请求分类树失败, Err: %s", e.Error())
  125. return
  126. }
  127. }
  128. }
  129. return items, nil
  130. }