commodity_trade_shanghai.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_crawler/models"
  5. "eta/eta_crawler/services/alarm_msg"
  6. "eta/eta_crawler/utils"
  7. "fmt"
  8. "github.com/mozillazg/go-pinyin"
  9. "github.com/rdlucklib/rdluck_tools/http"
  10. "strings"
  11. "time"
  12. )
  13. type Position []struct {
  14. ContractCode string `json:"INSTRUMENTID"`
  15. ProductSortNo int `json:"PRODUCTSORTNO"`
  16. Rank int `json:"RANK"`
  17. ParticipantID1 string `json:"PARTICIPANTID1"`
  18. ParticipantName1 string `json:"PARTICIPANTABBR1"`
  19. Deal interface{} `json:"CJ1"`
  20. Change1 interface{} `json:"CJ1_CHG"`
  21. ParticipantID2 string `json:"PARTICIPANTID2"`
  22. ParticipantName2 string `json:"PARTICIPANTABBR2"`
  23. BuyIn interface{} `json:"CJ2"`
  24. Change2 interface{} `json:"CJ2_CHG"`
  25. ParticipantID3 string `json:"PARTICIPANTID3"`
  26. ParticipantName3 string `json:"PARTICIPANTABBR3"`
  27. SoldOut interface{} `json:"CJ3"`
  28. Change3 interface{} `json:"CJ3_CHG"`
  29. ProductName string `json:"PRODUCTNAME"`
  30. }
  31. type Message struct {
  32. Position Position `json:"o_cursor"`
  33. Length string `json:"showlength"`
  34. Code int `json:"o_code"`
  35. Msg string `json:"o_msg"`
  36. ReportDate string `json:"report_date"`
  37. UpdateDate string `json:"update_date"`
  38. PrintDate string `json:"print_date"`
  39. }
  40. var indexCode string
  41. var indexCodeMap = make(map[string]string)
  42. func shIndexCodeGenerator(shortName, indexName, contractCode, suffix string) string {
  43. if shortName == "" {
  44. return ""
  45. }
  46. strResult := ""
  47. if shortName != "top20" {
  48. //取公司全拼
  49. a := pinyin.NewArgs()
  50. rows := pinyin.LazyPinyin(shortName, a)
  51. for i := 0; i < len(rows); i++ {
  52. strResult += rows[i]
  53. }
  54. } else {
  55. strResult = "top20"
  56. }
  57. indexCode, _ := indexCodeMap[indexName]
  58. if indexCode == "" {
  59. indexCode = strResult + contractCode + suffix
  60. indexCode = strings.Replace(indexCode, " ", "", -1)
  61. indexCodeMap[indexName] = indexCode
  62. err := models.AddBaseFromTradeMapping(indexName, indexCode, "SH")
  63. if err != nil {
  64. fmt.Println("add Code err:", err)
  65. }
  66. }
  67. return strings.Replace(indexCode, " ", "", -1)
  68. }
  69. // SyncRankingFromShangHai 上海商品交易所持单排名
  70. func SyncRankingFromShangHai() {
  71. var err error
  72. defer func() {
  73. if err != nil {
  74. msg := "失败提醒" + "SyncRankingFromShangHai ErrMsg:" + err.Error()
  75. fmt.Println("msg:",msg)
  76. go alarm_msg.SendAlarmMsg(msg, 3)
  77. }
  78. }()
  79. n := utils.GetRandInt(10, 120)
  80. time.Sleep(time.Duration(n) * time.Second)
  81. allCode, err := models.GetIndexCodeFromMapping("SH")
  82. if err != nil {
  83. fmt.Println("select Code err:", err)
  84. return
  85. }
  86. for _, item := range allCode {
  87. indexCodeMap[item.IndexName] = item.IndexCode
  88. }
  89. //获取新的指标信息
  90. for i := 10; i >= 0; i-- {
  91. var message Message
  92. zzUrl := "http://www.shfe.com.cn/data/dailydata/kx/pm%s.dat"
  93. date := time.Now().AddDate(0, 0, -i)
  94. dateStr := date.Format(utils.FormatDateUnSpace)
  95. zzUrl = fmt.Sprintf(zzUrl, dateStr)
  96. fmt.Println(zzUrl)
  97. body, e := http.Get(zzUrl)
  98. if e != nil {
  99. err = e
  100. fmt.Println("err:", err)
  101. return
  102. }
  103. err = json.Unmarshal(body, &message)
  104. if err != nil {
  105. fmt.Println("Unmarshal Err:", err)
  106. continue
  107. }
  108. var position = message.Position
  109. var tradeDate = message.ReportDate
  110. //获取所有指标信息
  111. allIndex, e := models.GetBaseFromTradeShangHaiIndexAll(dateStr)
  112. if e != nil {
  113. err = e
  114. return
  115. }
  116. existIndexMap := make(map[string]*models.BaseFromTradeShanghaiIndex)
  117. for _, v := range allIndex {
  118. indexKey := v.DealName + v.BuyName + v.SoldName
  119. existIndexMap[indexKey] = v
  120. }
  121. var itemVerifyCode int
  122. //处理指标
  123. for _, p := range position {
  124. var item = new(models.BaseFromTradeShanghaiIndex)
  125. if p.Rank > 0 && p.Rank < 40 && p.ParticipantName1 != "" {
  126. if strings.Replace(p.ProductName, " ", "", -1) != "20号胶" && strings.Replace(p.ProductName, " ", "", -1) != "低硫燃料油" {
  127. contractCode := strings.Replace(p.ContractCode, " ", "", -1)
  128. //成交量
  129. item.Rank = p.Rank
  130. item.DealShortName = strings.Replace(p.ParticipantName1, " ", "", -1)
  131. item.BuyShortName = strings.Replace(p.ParticipantName2, " ", "", -1)
  132. item.SoldShortName = strings.Replace(p.ParticipantName3, " ", "", -1)
  133. item.DealName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName1+"_"+p.ContractCode+"_成交量(手)"), " ", "", -1)
  134. item.BuyName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName2+"_"+p.ContractCode+"_持买单量(手)"), " ", "", -1)
  135. item.SoldName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName3+"_"+p.ContractCode+"_持卖单量(手)"), " ", "", -1)
  136. item.DealCode = shIndexCodeGenerator(item.DealShortName, item.DealName, contractCode, "deal")
  137. item.BuyCode = shIndexCodeGenerator(item.BuyShortName, item.BuyName, contractCode, "buy")
  138. item.SoldCode = shIndexCodeGenerator(item.SoldShortName, item.SoldName, contractCode, "sold")
  139. item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
  140. item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
  141. item.Frequency = "日度"
  142. item.CreateTime = time.Now()
  143. item.ModifyTime = time.Now()
  144. item.DataTime = tradeDate
  145. if deal, ok := p.Deal.(float64); ok{
  146. item.DealValue = int(deal)
  147. }
  148. if change1, ok := p.Change1.(float64); ok{
  149. item.DealChange = int(change1)
  150. }
  151. if buyIn, ok := p.BuyIn.(float64); ok{
  152. item.BuyValue = int(buyIn)
  153. }
  154. if change2, ok := p.Change2.(float64); ok{
  155. item.BuyChange = int(change2)
  156. }
  157. if soldOut, ok := p.SoldOut.(float64); ok{
  158. item.SoldValue = int(soldOut)
  159. }
  160. if change3, ok := p.Change3.(float64); ok{
  161. item.SoldChange = int(change3)
  162. }
  163. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  164. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  165. newID, e := models.AddBaseFromTradeShangHaiIndex(item)
  166. if e != nil {
  167. err = e
  168. fmt.Println("insert error:", err)
  169. }
  170. fmt.Println("insert new indexID:", newID)
  171. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  172. //更新
  173. err = models.ModifyBaseFromTradeShangHaiIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeShangHaiIndexId)
  174. if err != nil {
  175. fmt.Println("data update err:", err)
  176. }
  177. }
  178. }
  179. } else if p.Rank == 999 {
  180. if strings.Replace(p.ProductName, " ", "", -1) != "20号胶" && strings.Replace(p.ProductName, " ", "", -1) != "低硫燃料油" {
  181. contractCode := strings.Replace(p.ContractCode, " ", "", -1)
  182. //Top 20
  183. item.Rank = p.Rank
  184. item.DealShortName = strings.Replace(p.ParticipantName1, " ", "", -1)
  185. item.BuyShortName = strings.Replace(p.ParticipantName2, " ", "", -1)
  186. item.SoldShortName = strings.Replace(p.ParticipantName3, " ", "", -1)
  187. item.DealName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.ContractCode+"_成交量(手)"), " ", "", -1)
  188. item.BuyName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.ContractCode+"_持买单量(手)"), " ", "", -1)
  189. item.SoldName = strings.Replace(fmt.Sprintf("%s", "top20_"+p.ContractCode+"_持卖单量(手)"), " ", "", -1)
  190. item.DealCode = shIndexCodeGenerator("top20", item.DealName, contractCode, "deal")
  191. item.BuyCode = shIndexCodeGenerator("top20", item.BuyName, contractCode, "buy")
  192. item.SoldCode = shIndexCodeGenerator("top20", item.SoldName, contractCode, "sold")
  193. item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
  194. item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
  195. item.Frequency = "日度"
  196. item.CreateTime = time.Now()
  197. item.ModifyTime = time.Now()
  198. item.DataTime = tradeDate
  199. if deal, ok := p.Deal.(float64); ok{
  200. item.DealValue = int(deal)
  201. }
  202. if change1, ok := p.Change1.(float64); ok{
  203. item.DealChange = int(change1)
  204. }
  205. if buyIn, ok := p.BuyIn.(float64); ok{
  206. item.BuyValue = int(buyIn)
  207. }
  208. if change2, ok := p.Change2.(float64); ok{
  209. item.BuyChange = int(change2)
  210. }
  211. if soldOut, ok := p.SoldOut.(float64); ok{
  212. item.SoldValue = int(soldOut)
  213. }
  214. if change3, ok := p.Change3.(float64); ok{
  215. item.SoldChange = int(change3)
  216. }
  217. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  218. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  219. newID, e := models.AddBaseFromTradeShangHaiIndex(item)
  220. if e != nil {
  221. err = e
  222. fmt.Println("insert error:", err)
  223. }
  224. fmt.Println("insert new indexID:", newID)
  225. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  226. //更新
  227. err = models.ModifyBaseFromTradeShangHaiIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeShangHaiIndexId)
  228. if err != nil {
  229. fmt.Println("data update err:", err)
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. fmt.Println("end")
  237. }