commodity_trade_ine.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 ineIndexCode string
  40. var ineIndexCodeMap = make(map[string]string)
  41. func IneIndexCodeGenerator(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. ineIndexCode, _ := ineIndexCodeMap[indexName]
  56. if ineIndexCode == "" {
  57. ineIndexCode = strResult + contractCode + suffix
  58. ineIndexCodeMap[indexName] = ineIndexCode
  59. err := models.AddBaseFromTradeMapping(indexName, ineIndexCode, "INE")
  60. if err != nil {
  61. fmt.Println("add Code err:", err)
  62. }
  63. }
  64. return ineIndexCode
  65. }
  66. // SyncRankingFromIne 上海能源交易中心持单排名
  67. func SyncRankingFromIne() {
  68. n := utils.GetRandInt(10, 120)
  69. time.Sleep(time.Duration(n) * time.Second)
  70. allCode, err := models.GetIndexCodeFromMapping("Ine")
  71. if err != nil {
  72. fmt.Println("select Code err:", err)
  73. }
  74. for _, item := range allCode {
  75. ineIndexCodeMap[item.IndexName] = item.IndexCode
  76. }
  77. //获取新的指标信息
  78. for i := 180; i > 0; i-- {
  79. var message Message
  80. var item = new(models.BaseFromTradeIneIndex)
  81. zzUrl := "http://www.ine.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. existIndexMap := make(map[string]*models.BaseFromTradeIneIndex)
  94. //获取所有指标信息
  95. allIndex, err := models.GetBaseFromTradeIneIndexAll(dateStr)
  96. if err != nil {
  97. fmt.Println("select err:", err)
  98. }
  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. //成交量
  108. item.Rank = p.Rank
  109. item.DealShortName = p.ParticipantName1
  110. item.BuyShortName = p.ParticipantName2
  111. item.SoldShortName = p.ParticipantName3
  112. item.DealName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName1+"_"+p.ContractCode+"_成交量"), " ", "", -1)
  113. item.BuyName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName2+"_"+p.ContractCode+"_持买单量"), " ", "", -1)
  114. item.SoldName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName3+"_"+p.ContractCode+"_持卖单量"), " ", "", -1)
  115. item.DealCode = IneIndexCodeGenerator(item.DealShortName, item.DealName, p.ContractCode, "deal")
  116. item.BuyCode = IneIndexCodeGenerator(item.BuyShortName, item.BuyName, p.ContractCode, "buy")
  117. item.SoldCode = IneIndexCodeGenerator(item.SoldShortName, item.SoldName, p.ContractCode, "sold")
  118. item.DealValue = p.Deal
  119. item.DealChange = p.Change1
  120. item.BuyValue = p.BuyIn
  121. item.BuyChange = p.Change2
  122. item.SoldValue = p.SoldOut
  123. item.SoldChange = p.Change3
  124. item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
  125. item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
  126. item.Frequency = "日度"
  127. item.CreateTime = time.Now()
  128. item.ModifyTime = time.Now()
  129. item.DataTime = tradeDate
  130. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  131. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  132. newID, err := models.AddBaseFromTradeIneIndex(item)
  133. if err != nil {
  134. fmt.Println("insert error:", err)
  135. }
  136. fmt.Println("insert new indexID:", newID)
  137. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  138. //更新
  139. err := models.ModifyBaseFromTradeIneIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeIneIndexId)
  140. if err != nil {
  141. fmt.Println("data update err:", err)
  142. }
  143. }
  144. }
  145. }
  146. }
  147. fmt.Println("end")
  148. }