commodity_trade_ine.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_data_crawler/models"
  6. "hongze/hongze_data_crawler/utils"
  7. "rdluck_tools/http"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type position []struct {
  13. ContractCode string `json:"INSTRUMENTID"`
  14. ProductSortNo int `json:"PRODUCTSORTNO"`
  15. Rank int `json:"RANK"`
  16. ParticipantID1 string `json:"PARTICIPANTID1"`
  17. ParticipantName1 string `json:"PARTICIPANTABBR1"`
  18. Deal int `json:"CJ1"`
  19. Change1 int `json:"CJ1_CHG"`
  20. ParticipantID2 string `json:"PARTICIPANTID2"`
  21. ParticipantName2 string `json:"PARTICIPANTABBR2"`
  22. BuyIn int `json:"CJ2"`
  23. Change2 int `json:"CJ2_CHG"`
  24. ParticipantID3 string `json:"PARTICIPANTID3"`
  25. ParticipantName3 string `json:"PARTICIPANTABBR3"`
  26. SoldOut int `json:"CJ3"`
  27. Change3 int `json:"CJ3_CHG"`
  28. ProductName string `json:"PRODUCTNAME"`
  29. }
  30. type message struct {
  31. Position Position `json:"o_cursor"`
  32. Length string `json:"showlength"`
  33. Code int `json:"o_code"`
  34. Msg string `json:"o_msg"`
  35. ReportDate string `json:"report_date"`
  36. UpdateDate string `json:"update_date"`
  37. PrintDate string `json:"print_date"`
  38. }
  39. var ineIndexCode string
  40. var ineIndexCodeMap = make(map[string]string)
  41. func IneIndexCodeGenerator(indexName,suffix string) string {
  42. ineIndexCode,_ := ineIndexCodeMap[indexName]
  43. if ineIndexCode == "" {
  44. ineIndexCode = fmt.Sprintf("INE%s", time.Now().Format(utils.FormatDateTimeUnSpace)+strconv.Itoa(utils.GetRandInt(1, 100))+suffix)
  45. ineIndexCodeMap[indexName] = ineIndexCode
  46. err := models.AddBaseFromTradeMapping(indexName, ineIndexCode, "INE")
  47. if err != nil {
  48. fmt.Println("add Code err:", err)
  49. }
  50. }
  51. return ineIndexCode
  52. }
  53. // SyncRankingFromIne 上海能源交易中心持单排名
  54. func SyncRankingFromIne() {
  55. allCode, err := models.GetIndexCodeFromMapping("Ine")
  56. if err != nil {
  57. fmt.Println("select Code err:", err)
  58. }
  59. for _, item := range allCode {
  60. ineIndexCodeMap[item.IndexName] = item.IndexCode
  61. }
  62. //获取新的指标信息
  63. for i := 180; i > 0; i-- {
  64. var message Message
  65. var item = new(models.BaseFromTradeIneIndex)
  66. zzUrl := "http://www.ine.com.cn/data/dailydata/kx/pm%s.dat"
  67. date := time.Now().AddDate(0, 0, -i)
  68. dateStr := date.Format(utils.FormatDateUnSpace)
  69. zzUrl = fmt.Sprintf(zzUrl, dateStr)
  70. fmt.Println(zzUrl)
  71. body, err := http.Get(zzUrl)
  72. if err != nil {
  73. fmt.Println("err:", err)
  74. }
  75. err = json.Unmarshal(body, &message)
  76. var position = message.Position
  77. var tradeDate = message.ReportDate
  78. existIndexMap := make(map[string]*models.BaseFromTradeIneIndex)
  79. //获取所有指标信息
  80. allIndex, err := models.GetBaseFromTradeIneIndexAll(dateStr)
  81. if err != nil {
  82. fmt.Println("select err:", err)
  83. }
  84. for _, v := range allIndex {
  85. indexKey := v.DealName + v.BuyName + v.SoldName + tradeDate
  86. existIndexMap[indexKey] = v
  87. ineIndexCodeMap[v.BuyName] = v.BuyCode
  88. ineIndexCodeMap[v.SoldName] = v.SoldCode
  89. ineIndexCodeMap[v.DealName] = v.DealCode
  90. }
  91. var itemVerifyCode int
  92. //处理指标
  93. for _, p := range position {
  94. if p.Rank > 0 && p.Rank < 40 && p.ParticipantName1 != "" {
  95. //成交量
  96. item.Rank = p.Rank
  97. item.DealShortName = p.ParticipantName1
  98. item.BuyShortName = p.ParticipantName2
  99. item.SoldShortName = p.ParticipantName3
  100. item.DealName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName1+"_成交量"+"_"+p.ProductName+"_"+p.ContractCode), " ", "", -1)
  101. item.BuyName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName2+"_持买单量"+"_"+p.ProductName+"_"+p.ContractCode), " ", "", -1)
  102. item.SoldName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName3+"_持卖单量"+"_"+p.ProductName+"_"+p.ContractCode), " ", "", -1)
  103. item.DealCode = IneIndexCodeGenerator(item.DealName, "deal")
  104. item.BuyCode = IneIndexCodeGenerator(item.BuyName, "buy")
  105. item.SoldCode = IneIndexCodeGenerator(item.SoldName, "sold")
  106. item.DealValue = p.Deal
  107. item.DealChange = p.Change1
  108. item.BuyValue = p.BuyIn
  109. item.BuyChange = p.Change2
  110. item.SoldValue = p.SoldOut
  111. item.SoldChange = p.Change3
  112. item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
  113. item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
  114. item.Frequency = "日度"
  115. item.CreateTime = time.Now()
  116. item.ModifyTime = time.Now()
  117. item.DataTime = tradeDate
  118. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  119. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName+tradeDate]; !ok {
  120. newID, err := models.AddBaseFromTradeIneIndex(item)
  121. if err != nil {
  122. fmt.Println("insert error:", err)
  123. }
  124. fmt.Println("insert new indexID:", newID)
  125. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  126. //更新
  127. err := models.ModifyBaseFromTradeIneIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeIneIndexId)
  128. if err != nil {
  129. fmt.Println("data update err:", err)
  130. }
  131. }
  132. }
  133. }
  134. }
  135. fmt.Println("end")
  136. }