commodity_trade_shanghai.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/mozillazg/go-pinyin"
  6. "hongze/hongze_data_crawler/models"
  7. "hongze/hongze_data_crawler/utils"
  8. "rdluck_tools/http"
  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 indexCode string
  40. var indexCodeMap = make(map[string]string)
  41. func shIndexCodeGenerator(shortName, indexName, contractCode, suffix string) string {
  42. if shortName == "" {
  43. indexCode = ""
  44. return indexCode
  45. }
  46. //取公司前两个字的全拼
  47. a := pinyin.NewArgs()
  48. rows := pinyin.Pinyin(shortName, a)
  49. strResult := ""
  50. for i := 0; i < len(rows[:2]); i++ {
  51. if len(rows[i]) != 0 {
  52. strResult += rows[i][0]
  53. }
  54. }
  55. indexCode, _ := indexCodeMap[indexName]
  56. if indexCode == "" {
  57. indexCode = strResult + contractCode + suffix
  58. indexCodeMap[indexName] = indexCode
  59. err := models.AddBaseFromTradeMapping(indexName, indexCode, "SH")
  60. if err != nil {
  61. fmt.Println("add Code err:", err)
  62. }
  63. }
  64. return strings.Replace(indexCode, " ", "", -1)
  65. }
  66. // SyncRankingFromShangHai 上海商品交易所持单排名
  67. func SyncRankingFromShangHai() {
  68. n := utils.GetRandInt(10, 120)
  69. time.Sleep(time.Duration(n) * time.Second)
  70. allCode, err := models.GetIndexCodeFromMapping("Z")
  71. if err != nil {
  72. fmt.Println("select Code err:", err)
  73. }
  74. for _, item := range allCode {
  75. indexCodeMap[item.IndexName] = item.IndexCode
  76. }
  77. //获取新的指标信息
  78. for i := 2; i > 0; i-- {
  79. var message Message
  80. var item = new(models.BaseFromTradeShanghaiIndex)
  81. zzUrl := "http://www.shfe.com.cn/data/dailydata/kx/pm%s.dat"
  82. date := time.Now().AddDate(0, 0, -i)
  83. dateStr := date.Format(utils.FormatDateUnSpace)
  84. zzUrl = fmt.Sprintf(zzUrl, dateStr)
  85. fmt.Println(zzUrl)
  86. body, err := http.Get(zzUrl)
  87. if err != nil {
  88. fmt.Println("err:", err)
  89. }
  90. err = json.Unmarshal(body, &message)
  91. var position = message.Position
  92. var tradeDate = message.ReportDate
  93. //获取所有指标信息
  94. allIndex, err := models.GetBaseFromTradeShangHaiIndexAll(dateStr)
  95. if err != nil {
  96. return
  97. }
  98. existIndexMap := make(map[string]*models.BaseFromTradeShanghaiIndex)
  99. for _, v := range allIndex {
  100. indexKey := v.DealName + v.BuyName + v.SoldName
  101. existIndexMap[indexKey] = v
  102. }
  103. var itemVerifyCode int
  104. //处理指标
  105. for _, p := range position {
  106. if p.Rank > 0 && p.Rank < 40 && p.ParticipantName1 != "" {
  107. if strings.Replace(p.ProductName, " ", "", -1) != "20号胶" && strings.Replace(p.ProductName, " ", "", -1) != "低硫燃料油" {
  108. //成交量
  109. item.Rank = p.Rank
  110. item.DealShortName = p.ParticipantName1
  111. item.BuyShortName = p.ParticipantName2
  112. item.SoldShortName = p.ParticipantName3
  113. item.DealName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName1+"_"+p.ContractCode+"_成交量"), " ", "", -1)
  114. item.BuyName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName2+"_"+p.ContractCode+"_持买单量"), " ", "", -1)
  115. item.SoldName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName3+"_"+p.ContractCode+"_持卖单量"), " ", "", -1)
  116. item.DealCode = shIndexCodeGenerator(item.DealShortName, item.DealName, p.ContractCode, "deal")
  117. item.BuyCode = shIndexCodeGenerator(item.BuyShortName, item.BuyName, p.ContractCode, "buy")
  118. item.SoldCode = shIndexCodeGenerator(item.SoldShortName, item.SoldName, p.ContractCode, "sold")
  119. item.DealValue = p.Deal
  120. item.DealChange = p.Change1
  121. item.BuyValue = p.BuyIn
  122. item.BuyChange = p.Change2
  123. item.SoldValue = p.SoldOut
  124. item.SoldChange = p.Change3
  125. item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
  126. item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
  127. item.Frequency = "日度"
  128. item.CreateTime = time.Now()
  129. item.ModifyTime = time.Now()
  130. item.DataTime = tradeDate
  131. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  132. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  133. newID, err := models.AddBaseFromTradeShangHaiIndex(item)
  134. if err != nil {
  135. fmt.Println("insert error:", err)
  136. }
  137. fmt.Println("insert new indexID:", newID)
  138. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  139. //更新
  140. err := models.ModifyBaseFromTradeShangHaiIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeShangHaiIndexId)
  141. if err != nil {
  142. fmt.Println("data update err:", err)
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. fmt.Println("end")
  150. }