commodity_trade_shanghai.go 7.8 KB

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