Преглед изворни кода

上期所,上期能源结构体修改

ziwen пре 1 година
родитељ
комит
52fdd9bca6
2 измењених фајлова са 135 додато и 52 уклоњено
  1. 51 13
      services/commodity_trade_ine.go
  2. 84 39
      services/commodity_trade_shanghai.go

+ 51 - 13
services/commodity_trade_ine.go

@@ -3,6 +3,7 @@ package services
 import (
 	"encoding/json"
 	"eta/eta_crawler/models"
+	"eta/eta_crawler/services/alarm_msg"
 	"eta/eta_crawler/utils"
 	"fmt"
 	"github.com/mozillazg/go-pinyin"
@@ -52,7 +53,7 @@ func IneIndexCodeGenerator(shortName, indexName, contractCode, suffix string) st
 		//取公司全拼
 		a := pinyin.NewArgs()
 		rows := pinyin.LazyPinyin(shortName, a)
-		for i := 0; i < len(rows); i++ {
+		for i:=0;i<len(rows);i++{
 			strResult += rows[i]
 		}
 	} else {
@@ -73,6 +74,13 @@ func IneIndexCodeGenerator(shortName, indexName, contractCode, suffix string) st
 
 // SyncRankingFromIne 上海能源交易中心持单排名
 func SyncRankingFromIne() {
+	var err error
+	defer func() {
+		if err != nil {
+			msg := "失败提醒" + "SyncRankingFromIne ErrMsg:" + err.Error()
+			go alarm_msg.SendAlarmMsg(msg, 3)
+		}
+	}()
 	n := utils.GetRandInt(10, 120)
 	time.Sleep(time.Duration(n) * time.Second)
 	allCode, err := models.GetIndexCodeFromMapping("Ine")
@@ -96,6 +104,10 @@ func SyncRankingFromIne() {
 			fmt.Println("err:", err)
 		}
 		err = json.Unmarshal(body, &message)
+		if err != nil {
+			fmt.Println("Unmarshal Err:", err)
+			continue
+		}
 		var position = message.Position
 		var tradeDate = message.ReportDate
 		existIndexMap := make(map[string]*models.BaseFromTradeIneIndex)
@@ -124,12 +136,6 @@ func SyncRankingFromIne() {
 				item.DealCode = IneIndexCodeGenerator(item.DealShortName, item.DealName, p.ContractCode, "deal")
 				item.BuyCode = IneIndexCodeGenerator(item.BuyShortName, item.BuyName, p.ContractCode, "buy")
 				item.SoldCode = IneIndexCodeGenerator(item.SoldShortName, item.SoldName, p.ContractCode, "sold")
-				item.DealValue = p.Deal
-				item.DealChange = p.Change1
-				item.BuyValue = p.BuyIn
-				item.BuyChange = p.Change2
-				item.SoldValue = p.SoldOut
-				item.SoldChange = p.Change3
 				item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
 				item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
 				item.Frequency = "日度"
@@ -137,6 +143,25 @@ func SyncRankingFromIne() {
 				item.ModifyTime = time.Now()
 				item.DataTime = tradeDate
 
+				if deal, ok := p.Deal.(int); ok{
+					item.DealValue = deal
+				}
+				if change1, ok := p.Change1.(int); ok{
+					item.DealChange = change1
+				}
+				if buyIn, ok := p.BuyIn.(int); ok{
+					item.BuyValue = buyIn
+				}
+				if change2, ok := p.Change2.(int); ok{
+					item.BuyChange = change2
+				}
+				if soldOut, ok := p.SoldOut.(int); ok{
+					item.SoldValue = soldOut
+				}
+				if change3, ok := p.Change3.(int); ok{
+					item.SoldChange = change3
+				}
+
 				itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
 				if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
 					newID, err := models.AddBaseFromTradeIneIndex(item)
@@ -163,12 +188,6 @@ func SyncRankingFromIne() {
 				item.DealCode = IneIndexCodeGenerator("top20", item.DealName, p.ContractCode, "deal")
 				item.BuyCode = IneIndexCodeGenerator("top20", item.BuyName, p.ContractCode, "buy")
 				item.SoldCode = IneIndexCodeGenerator("top20", item.SoldName, p.ContractCode, "sold")
-				item.DealValue = p.Deal
-				item.DealChange = p.Change1
-				item.BuyValue = p.BuyIn
-				item.BuyChange = p.Change2
-				item.SoldValue = p.SoldOut
-				item.SoldChange = p.Change3
 				item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
 				item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
 				item.Frequency = "日度"
@@ -176,6 +195,25 @@ func SyncRankingFromIne() {
 				item.ModifyTime = time.Now()
 				item.DataTime = tradeDate
 
+				if deal, ok := p.Deal.(int); ok{
+					item.DealValue = deal
+				}
+				if change1, ok := p.Change1.(int); ok{
+					item.DealChange = change1
+				}
+				if buyIn, ok := p.BuyIn.(int); ok{
+					item.BuyValue = buyIn
+				}
+				if change2, ok := p.Change2.(int); ok{
+					item.BuyChange = change2
+				}
+				if soldOut, ok := p.SoldOut.(int); ok{
+					item.SoldValue = soldOut
+				}
+				if change3, ok := p.Change3.(int); ok{
+					item.SoldChange = change3
+				}
+
 				itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
 				if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
 					newID, err := models.AddBaseFromTradeIneIndex(item)

+ 84 - 39
services/commodity_trade_shanghai.go

@@ -3,6 +3,7 @@ package services
 import (
 	"encoding/json"
 	"eta/eta_crawler/models"
+	"eta/eta_crawler/services/alarm_msg"
 	"eta/eta_crawler/utils"
 	"fmt"
 	"github.com/mozillazg/go-pinyin"
@@ -12,22 +13,22 @@ import (
 )
 
 type Position []struct {
-	ContractCode     string `json:"INSTRUMENTID"`
-	ProductSortNo    int    `json:"PRODUCTSORTNO"`
-	Rank             int    `json:"RANK"`
-	ParticipantID1   string `json:"PARTICIPANTID1"`
-	ParticipantName1 string `json:"PARTICIPANTABBR1"`
-	Deal             int    `json:"CJ1"`
-	Change1          int    `json:"CJ1_CHG"`
-	ParticipantID2   string `json:"PARTICIPANTID2"`
-	ParticipantName2 string `json:"PARTICIPANTABBR2"`
-	BuyIn            int    `json:"CJ2"`
-	Change2          int    `json:"CJ2_CHG"`
-	ParticipantID3   string `json:"PARTICIPANTID3"`
-	ParticipantName3 string `json:"PARTICIPANTABBR3"`
-	SoldOut          int    `json:"CJ3"`
-	Change3          int    `json:"CJ3_CHG"`
-	ProductName      string `json:"PRODUCTNAME"`
+	ContractCode     string      `json:"INSTRUMENTID"`
+	ProductSortNo    int         `json:"PRODUCTSORTNO"`
+	Rank             int         `json:"RANK"`
+	ParticipantID1   string      `json:"PARTICIPANTID1"`
+	ParticipantName1 string      `json:"PARTICIPANTABBR1"`
+	Deal             interface{} `json:"CJ1"`
+	Change1          interface{} `json:"CJ1_CHG"`
+	ParticipantID2   string      `json:"PARTICIPANTID2"`
+	ParticipantName2 string      `json:"PARTICIPANTABBR2"`
+	BuyIn            interface{} `json:"CJ2"`
+	Change2          interface{} `json:"CJ2_CHG"`
+	ParticipantID3   string      `json:"PARTICIPANTID3"`
+	ParticipantName3 string      `json:"PARTICIPANTABBR3"`
+	SoldOut          interface{} `json:"CJ3"`
+	Change3          interface{} `json:"CJ3_CHG"`
+	ProductName      string      `json:"PRODUCTNAME"`
 }
 
 type Message struct {
@@ -73,34 +74,50 @@ func shIndexCodeGenerator(shortName, indexName, contractCode, suffix string) str
 
 // SyncRankingFromShangHai 上海商品交易所持单排名
 func SyncRankingFromShangHai() {
+	var err error
+	defer func() {
+		if err != nil {
+			msg := "失败提醒" + "SyncRankingFromShangHai ErrMsg:" + err.Error()
+			fmt.Println("msg:",msg)
+			go alarm_msg.SendAlarmMsg(msg, 3)
+		}
+	}()
 	n := utils.GetRandInt(10, 120)
 	time.Sleep(time.Duration(n) * time.Second)
 	allCode, err := models.GetIndexCodeFromMapping("SH")
 	if err != nil {
 		fmt.Println("select Code err:", err)
+		return
 	}
 	for _, item := range allCode {
 		indexCodeMap[item.IndexName] = item.IndexCode
 	}
 	//获取新的指标信息
-	for i := 6; i >= 0; i-- {
+	for i := 3; i >= 0; i-- {
 		var message Message
 		zzUrl := "http://www.shfe.com.cn/data/dailydata/kx/pm%s.dat"
 		date := time.Now().AddDate(0, 0, -i)
 		dateStr := date.Format(utils.FormatDateUnSpace)
 		zzUrl = fmt.Sprintf(zzUrl, dateStr)
 		fmt.Println(zzUrl)
-		body, err := http.Get(zzUrl)
-		if err != nil {
+		body, e := http.Get(zzUrl)
+		if e != nil {
+			err = e
 			fmt.Println("err:", err)
+			return
 		}
 		err = json.Unmarshal(body, &message)
+		if err != nil {
+			fmt.Println("Unmarshal Err:", err)
+			continue
+		}
 		var position = message.Position
 		var tradeDate = message.ReportDate
 
 		//获取所有指标信息
-		allIndex, err := models.GetBaseFromTradeShangHaiIndexAll(dateStr)
-		if err != nil {
+		allIndex, e := models.GetBaseFromTradeShangHaiIndexAll(dateStr)
+		if e != nil {
+			err = e
 			return
 		}
 
@@ -127,12 +144,6 @@ func SyncRankingFromShangHai() {
 					item.DealCode = shIndexCodeGenerator(item.DealShortName, item.DealName, contractCode, "deal")
 					item.BuyCode = shIndexCodeGenerator(item.BuyShortName, item.BuyName, contractCode, "buy")
 					item.SoldCode = shIndexCodeGenerator(item.SoldShortName, item.SoldName, contractCode, "sold")
-					item.DealValue = p.Deal
-					item.DealChange = p.Change1
-					item.BuyValue = p.BuyIn
-					item.BuyChange = p.Change2
-					item.SoldValue = p.SoldOut
-					item.SoldChange = p.Change3
 					item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
 					item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
 					item.Frequency = "日度"
@@ -140,16 +151,36 @@ func SyncRankingFromShangHai() {
 					item.ModifyTime = time.Now()
 					item.DataTime = tradeDate
 
+					if deal, ok := p.Deal.(int); ok{
+						item.DealValue = deal
+					}
+					if change1, ok := p.Change1.(int); ok{
+						item.DealChange = change1
+					}
+					if buyIn, ok := p.BuyIn.(int); ok{
+						item.BuyValue = buyIn
+					}
+					if change2, ok := p.Change2.(int); ok{
+						item.BuyChange = change2
+					}
+					if soldOut, ok := p.SoldOut.(int); ok{
+						item.SoldValue = soldOut
+					}
+					if change3, ok := p.Change3.(int); ok{
+						item.SoldChange = change3
+					}
+
 					itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
 					if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
-						newID, err := models.AddBaseFromTradeShangHaiIndex(item)
-						if err != nil {
+						newID, e := models.AddBaseFromTradeShangHaiIndex(item)
+						if e != nil {
+							err = e
 							fmt.Println("insert error:", err)
 						}
 						fmt.Println("insert new indexID:", newID)
 					} else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
 						//更新
-						err := models.ModifyBaseFromTradeShangHaiIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeShangHaiIndexId)
+						err = models.ModifyBaseFromTradeShangHaiIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeShangHaiIndexId)
 						if err != nil {
 							fmt.Println("data update err:", err)
 						}
@@ -169,12 +200,6 @@ func SyncRankingFromShangHai() {
 					item.DealCode = shIndexCodeGenerator("top20", item.DealName, contractCode, "deal")
 					item.BuyCode = shIndexCodeGenerator("top20", item.BuyName, contractCode, "buy")
 					item.SoldCode = shIndexCodeGenerator("top20", item.SoldName, contractCode, "sold")
-					item.DealValue = p.Deal
-					item.DealChange = p.Change1
-					item.BuyValue = p.BuyIn
-					item.BuyChange = p.Change2
-					item.SoldValue = p.SoldOut
-					item.SoldChange = p.Change3
 					item.ClassifyName = strings.Replace(p.ProductName, " ", "", -1)
 					item.ClassifyType = strings.Replace(p.ContractCode, " ", "", -1)
 					item.Frequency = "日度"
@@ -182,16 +207,36 @@ func SyncRankingFromShangHai() {
 					item.ModifyTime = time.Now()
 					item.DataTime = tradeDate
 
+					if deal, ok := p.Deal.(int); ok{
+						item.DealValue = deal
+					}
+					if change1, ok := p.Change1.(int); ok{
+						item.DealChange = change1
+					}
+					if buyIn, ok := p.BuyIn.(int); ok{
+						item.BuyValue = buyIn
+					}
+					if change2, ok := p.Change2.(int); ok{
+						item.BuyChange = change2
+					}
+					if soldOut, ok := p.SoldOut.(int); ok{
+						item.SoldValue = soldOut
+					}
+					if change3, ok := p.Change3.(int); ok{
+						item.SoldChange = change3
+					}
+
 					itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
 					if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
-						newID, err := models.AddBaseFromTradeShangHaiIndex(item)
-						if err != nil {
+						newID, e := models.AddBaseFromTradeShangHaiIndex(item)
+						if e != nil {
+							err = e
 							fmt.Println("insert error:", err)
 						}
 						fmt.Println("insert new indexID:", newID)
 					} else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
 						//更新
-						err := models.ModifyBaseFromTradeShangHaiIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeShangHaiIndexId)
+						err = models.ModifyBaseFromTradeShangHaiIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeShangHaiIndexId)
 						if err != nil {
 							fmt.Println("data update err:", err)
 						}