commodity_trade_ine.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 ineIndexCode string
  40. var ineIndexCodeMap = make(map[string]string)
  41. func IneIndexCodeGenerator(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. ineIndexCode, _ := ineIndexCodeMap[indexName]
  57. if ineIndexCode == "" {
  58. ineIndexCode = strResult + contractCode + suffix
  59. ineIndexCode = strings.Replace(ineIndexCode, " ", "", -1)
  60. ineIndexCodeMap[indexName] = ineIndexCode
  61. err := models.AddBaseFromTradeMapping(indexName, ineIndexCode, "INE")
  62. if err != nil {
  63. fmt.Println("add Code err:", err)
  64. }
  65. }
  66. return strings.Replace(ineIndexCode, " ", "", -1)
  67. }
  68. // SyncRankingFromIne 上海能源交易中心持单排名
  69. func SyncRankingFromIne() {
  70. n := utils.GetRandInt(10, 120)
  71. time.Sleep(time.Duration(n) * time.Second)
  72. allCode, err := models.GetIndexCodeFromMapping("Ine")
  73. if err != nil {
  74. fmt.Println("select Code err:", err)
  75. }
  76. for _, item := range allCode {
  77. ineIndexCodeMap[item.IndexName] = item.IndexCode
  78. }
  79. //获取新的指标信息
  80. for i := 2; i >= 0; i-- {
  81. var message Message
  82. var item = new(models.BaseFromTradeIneIndex)
  83. zzUrl := "http://www.ine.com.cn/data/dailydata/kx/pm%s.dat"
  84. date := time.Now().AddDate(0, 0, -i)
  85. dateStr := date.Format(utils.FormatDateUnSpace)
  86. zzUrl = fmt.Sprintf(zzUrl, dateStr)
  87. fmt.Println(zzUrl)
  88. body, err := http.Get(zzUrl)
  89. if err != nil {
  90. fmt.Println("err:", err)
  91. }
  92. err = json.Unmarshal(body, &message)
  93. var position = message.Position
  94. var tradeDate = message.ReportDate
  95. existIndexMap := make(map[string]*models.BaseFromTradeIneIndex)
  96. //获取所有指标信息
  97. allIndex, err := models.GetBaseFromTradeIneIndexAll(dateStr)
  98. if err != nil {
  99. fmt.Println("select err:", err)
  100. }
  101. for _, v := range allIndex {
  102. indexKey := v.DealName + v.BuyName + v.SoldName
  103. existIndexMap[indexKey] = v
  104. }
  105. var itemVerifyCode int
  106. //处理指标
  107. for _, p := range position {
  108. if p.Rank > 0 && p.Rank < 40 && p.ParticipantName1 != "" {
  109. //成交量
  110. item.Rank = p.Rank
  111. item.DealShortName = p.ParticipantName1
  112. item.BuyShortName = p.ParticipantName2
  113. item.SoldShortName = p.ParticipantName3
  114. item.DealName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName1+"_"+p.ContractCode+"_成交量(手)"), " ", "", -1)
  115. item.BuyName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName2+"_"+p.ContractCode+"_持买单量(手)"), " ", "", -1)
  116. item.SoldName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName3+"_"+p.ContractCode+"_持卖单量(手)"), " ", "", -1)
  117. item.DealCode = IneIndexCodeGenerator(item.DealShortName, item.DealName, p.ContractCode, "deal")
  118. item.BuyCode = IneIndexCodeGenerator(item.BuyShortName, item.BuyName, p.ContractCode, "buy")
  119. item.SoldCode = IneIndexCodeGenerator(item.SoldShortName, item.SoldName, p.ContractCode, "sold")
  120. item.DealValue = p.Deal
  121. item.DealChange = p.Change1
  122. item.BuyValue = p.BuyIn
  123. item.BuyChange = p.Change2
  124. item.SoldValue = p.SoldOut
  125. item.SoldChange = p.Change3
  126. item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
  127. item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
  128. item.Frequency = "日度"
  129. item.CreateTime = time.Now()
  130. item.ModifyTime = time.Now()
  131. item.DataTime = tradeDate
  132. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  133. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  134. newID, err := models.AddBaseFromTradeIneIndex(item)
  135. if err != nil {
  136. fmt.Println("insert error:", err)
  137. }
  138. fmt.Println("insert new indexID:", newID)
  139. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  140. //更新
  141. err := models.ModifyBaseFromTradeIneIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeIneIndexId)
  142. if err != nil {
  143. fmt.Println("data update err:", err)
  144. }
  145. }
  146. } else if p.Rank == 999 {
  147. //Top 20
  148. item.Rank = p.Rank
  149. item.DealShortName = p.ParticipantName1
  150. item.BuyShortName = p.ParticipantName2
  151. item.SoldShortName = p.ParticipantName3
  152. item.DealName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.ContractCode+"_成交量(手)"), " ", "", -1)
  153. item.BuyName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.ContractCode+"_持买单量(手)"), " ", "", -1)
  154. item.SoldName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.ContractCode+"_持卖单量(手)"), " ", "", -1)
  155. item.DealCode = IneIndexCodeGenerator("top20", item.DealName, p.ContractCode, "deal")
  156. item.BuyCode = IneIndexCodeGenerator("top20", item.BuyName, p.ContractCode, "buy")
  157. item.SoldCode = IneIndexCodeGenerator("top20", item.SoldName, p.ContractCode, "sold")
  158. item.DealValue = p.Deal
  159. item.DealChange = p.Change1
  160. item.BuyValue = p.BuyIn
  161. item.BuyChange = p.Change2
  162. item.SoldValue = p.SoldOut
  163. item.SoldChange = p.Change3
  164. item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
  165. item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
  166. item.Frequency = "日度"
  167. item.CreateTime = time.Now()
  168. item.ModifyTime = time.Now()
  169. item.DataTime = tradeDate
  170. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  171. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  172. newID, err := models.AddBaseFromTradeIneIndex(item)
  173. if err != nil {
  174. fmt.Println("insert error:", err)
  175. }
  176. fmt.Println("insert new indexID:", newID)
  177. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  178. //更新
  179. err := models.ModifyBaseFromTradeIneIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeIneIndexId)
  180. if err != nil {
  181. fmt.Println("data update err:", err)
  182. }
  183. }
  184. }
  185. }
  186. }
  187. fmt.Println("end")
  188. }