package services import ( "encoding/json" "fmt" "hongze/hongze_data_crawler/models" "hongze/hongze_data_crawler/utils" "rdluck_tools/http" "strconv" "strings" "time" ) 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"` } type message struct { Position Position `json:"o_cursor"` Length string `json:"showlength"` Code int `json:"o_code"` Msg string `json:"o_msg"` ReportDate string `json:"report_date"` UpdateDate string `json:"update_date"` PrintDate string `json:"print_date"` } var ineIndexCode string var ineIndexCodeMap = make(map[string]string) func IneIndexCodeGenerator(indexName,suffix string) string { ineIndexCode,_ := ineIndexCodeMap[indexName] if ineIndexCode == "" { ineIndexCode = fmt.Sprintf("INE%s", time.Now().Format(utils.FormatDateTimeUnSpace)+strconv.Itoa(utils.GetRandInt(1, 100))+suffix) ineIndexCodeMap[indexName] = ineIndexCode err := models.AddBaseFromTradeMapping(indexName, ineIndexCode, "INE") if err != nil { fmt.Println("add Code err:", err) } } return ineIndexCode } // SyncRankingFromIne 上海能源交易中心持单排名 func SyncRankingFromIne() { allCode, err := models.GetIndexCodeFromMapping("Ine") if err != nil { fmt.Println("select Code err:", err) } for _, item := range allCode { ineIndexCodeMap[item.IndexName] = item.IndexCode } //获取新的指标信息 for i := 180; i > 0; i-- { var message Message var item = new(models.BaseFromTradeIneIndex) zzUrl := "http://www.ine.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 { fmt.Println("err:", err) } err = json.Unmarshal(body, &message) var position = message.Position var tradeDate = message.ReportDate existIndexMap := make(map[string]*models.BaseFromTradeIneIndex) //获取所有指标信息 allIndex, err := models.GetBaseFromTradeIneIndexAll(dateStr) if err != nil { fmt.Println("select err:", err) } for _, v := range allIndex { indexKey := v.DealName + v.BuyName + v.SoldName + tradeDate existIndexMap[indexKey] = v ineIndexCodeMap[v.BuyName] = v.BuyCode ineIndexCodeMap[v.SoldName] = v.SoldCode ineIndexCodeMap[v.DealName] = v.DealCode } var itemVerifyCode int //处理指标 for _, p := range position { if p.Rank > 0 && p.Rank < 40 && p.ParticipantName1 != "" { //成交量 item.Rank = p.Rank item.DealShortName = p.ParticipantName1 item.BuyShortName = p.ParticipantName2 item.SoldShortName = p.ParticipantName3 item.DealName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName1+"_成交量"+"_"+p.ProductName+"_"+p.ContractCode), " ", "", -1) item.BuyName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName2+"_持买单量"+"_"+p.ProductName+"_"+p.ContractCode), " ", "", -1) item.SoldName = strings.Replace(fmt.Sprintf("%s", p.ParticipantName3+"_持卖单量"+"_"+p.ProductName+"_"+p.ContractCode), " ", "", -1) item.DealCode = IneIndexCodeGenerator(item.DealName, "deal") item.BuyCode = IneIndexCodeGenerator(item.BuyName, "buy") item.SoldCode = IneIndexCodeGenerator(item.SoldName, "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 = "日度" item.CreateTime = time.Now() item.ModifyTime = time.Now() item.DataTime = tradeDate itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName+tradeDate]; !ok { newID, err := models.AddBaseFromTradeIneIndex(item) if err != nil { fmt.Println("insert error:", err) } fmt.Println("insert new indexID:", newID) } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) { //更新 err := models.ModifyBaseFromTradeIneIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeIneIndexId) if err != nil { fmt.Println("data update err:", err) } } } } } fmt.Println("end") }