commodity_trade_ine.go 8.2 KB

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