Roc 2 年之前
父節點
當前提交
6d4c7b1e8c

+ 2 - 2
controllers/future_good/future_good_edb_info.go

@@ -70,7 +70,7 @@ func (this *FutureGoodEdbInfoController) Add() {
 
 		windUrl := utils.Hz_Wind_Data_Url_LIST[0].Url
 
-		dataItem, errCode, err := services.GetFutureGoodDataFromWindUrl(windUrl, req.EdbCode, utils.BASE_START_DATE, endDate)
+		dataItem, errCode, err := services.GetFutureGoodDataFromThs( req.EdbCode, utils.BASE_START_DATE, endDate,0)
 		//dataItem, errCode, err := services.GetEdbDataFromWind(req.EdbCode, utils.BASE_START_DATE, endDate)
 		if errCode == 421 { //指标超限
 			br.Ret = 421
@@ -163,7 +163,7 @@ func (this *FutureGoodEdbInfoController) Refresh() {
 	}
 	endDate := endDateTime.Format(utils.FormatDate)
 
-	dataItem, errCode, err := services.GetFutureGoodDataFromWindUrl(futureGoodEdbInfo.ServerUrl, req.FutureGoodEdbCode, req.StartDate, endDate)
+	dataItem, errCode, err := services.GetFutureGoodDataFromThs(req.FutureGoodEdbCode, req.StartDate, endDate,0)
 	if errCode == 421 { //指标超限
 		br.Ret = 421
 	}

+ 4 - 4
models/future_good/future_good_edb_data.go

@@ -114,7 +114,7 @@ type RefreshFutureEdbEdbInfoReq struct {
 }
 
 // RefreshFutureGoodEdbDataFromWind 刷新wind期货指标数据
-func RefreshFutureGoodEdbDataFromWind(futureGoodEdbInfoId int, edbCode, startDate string, item *services.FutureGoodDataFromWind) (err error) {
+func RefreshFutureGoodEdbDataFromWind(futureGoodEdbInfoId int, edbCode, startDate string, item *services.FutureGoodDataFromThs) (err error) {
 	o := orm.NewOrm()
 	to, err := o.Begin()
 	if err != nil {
@@ -170,9 +170,9 @@ func RefreshFutureGoodEdbDataFromWind(futureGoodEdbInfoId int, edbCode, startDat
 		low := utils.SubFloatToString(item.Low[k], 20)
 		closeVal := utils.SubFloatToString(item.Close[k], 20)
 		volume := utils.SubFloatToString(item.Volume[k], 20)
-		amt := utils.SubFloatToString(item.Amt[k], 20)
-		oi := utils.SubFloatToString(item.Oi[k], 20)
-		settle := utils.SubFloatToString(item.Settle[k], 20)
+		amt := utils.SubFloatToString(item.Amount[k], 20)
+		oi := utils.SubFloatToString(item.Ccl[k], 20)
+		settle := utils.SubFloatToString(item.Settlement[k], 20)
 
 		if findItem, ok := existMap[dateTime]; !ok {
 			if _, existOk := addMap[dateTime]; !existOk {

+ 190 - 0
services/base_from_ths.go

@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"github.com/rdlucklib/rdluck_tools/http"
 	"github.com/shopspring/decimal"
+	"hongze/hongze_edb_lib/services/alarm_msg"
 	"hongze/hongze_edb_lib/utils"
 	"reflect"
 )
@@ -124,3 +125,192 @@ func getEdbDataFromThs(edbCode, startDate, endDate string, num int) (item *EdbDa
 	}
 	return item, nil
 }
+
+// FutureGoodDataFromThsInterface 同花顺商品数据类型转为interface
+type FutureGoodDataFromThsInterface struct {
+	Errmsg    string      `json:"errmsg"`
+	Errorcode int64       `json:"errorcode"`
+	DataVol   int64       `json:"dataVol"`
+	Perf      interface{} `json:"perf"`
+	Tables    []struct {
+		ThsCode string   `json:"thscode"`
+		Time    []string `json:"time"`
+		Table   struct {
+			Time           []string  `json:"time"`
+			LastClose      []float64 `json:"lastclose"`
+			Open           []float64 `json:"open"`
+			High           []float64 `json:"high"`
+			Low            []float64 `json:"low"`
+			Close          []float64 `json:"close"`
+			AvgPrice       []float64 `json:"avgprice"`
+			Change         []float64 `json:"change"`
+			ChangePer      []float64 `json:"changeper"`
+			Volume         []float64 `json:"volume"`
+			Amount         []float64 `json:"amount"`
+			Hsl            []float64 `json:"hsl"`
+			LastSettlement []float64 `json:"lastsettlement"`
+			Settlement     []float64 `json:"settlement"`
+			ZdSettlement   []float64 `json:"zdsettlement"`
+			ZdfSettlement  []float64 `json:"zdfsettlement"`
+			Ccl            []float64 `json:"ccl"`
+			Ccbd           []float64 `json:"ccbd"`
+			Zf             []float64 `json:"zf"`
+			Zjlx           []float64 `json:"zjlx"`
+			Zjcd           []float64 `json:"zjcd"`
+		} `json:"table"`
+	} `json:"tables"`
+}
+
+// FutureGoodDataFromThs 同花顺期货数据
+type FutureGoodDataFromThs struct {
+	DataVol   int64                  `json:"dataVol"`
+	Errmsg    string                 `json:"errmsg"`
+	Errorcode int64                  `json:"errorcode"`
+	Perf      interface{}            `json:"perf"`
+	Tables    []FutureGoodDataTables `json:"tables"`
+}
+
+// FutureGoodDataTables 同花顺表格数据
+type FutureGoodDataTables struct {
+	//TradeCode    []string  `json:"id"`
+	Time       []string  `json:"time"`
+	Open       []float64 `json:"open"`
+	High       []float64 `json:"high"`
+	Low        []float64 `json:"low"`
+	Close      []float64 `json:"close"`
+	Volume     []float64 `json:"volume"`
+	Amount     []float64 `json:"amount"`
+	Ccl        []float64 `json:"ccl"`
+	Settlement []float64 `json:"settlement"`
+}
+
+// GetFutureGoodDataFromThs 通过url获取wind的商品数据
+func GetFutureGoodDataFromThs(edbCode, startDate, endDate string, num int) (item *FutureGoodDataFromThs, errorCode int, err error) {
+	thsUrl := utils.Hz_Wind_Data_Url + `edbInfo/ths/future_good?EdbCode=%s&StartDate=%s&EndDate=%s`
+	thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
+	utils.FileLog.Info("thsUrl:" + thsUrl)
+	body, err := http.Get(thsUrl)
+	utils.FileLog.Info("ths result:" + string(body))
+	if err != nil {
+		err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
+		return
+	}
+	if string(body) == "null" {
+		err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
+		return
+	}
+
+	tmpItems := new(FutureGoodDataFromThsInterface)
+	err = json.Unmarshal(body, &tmpItems)
+	if err != nil {
+		err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
+		return
+	}
+	if tmpItems.Errorcode != 0 {
+
+		//session has expired,please re-login after using the system
+		//如果是同花顺登录session失效了,那么就重新请求获取数据
+		if tmpItems.Errorcode == -1020 && num == 0 {
+			return GetFutureGoodDataFromThs(edbCode, startDate, endDate, 1)
+		}
+
+		err = errors.New(string(body))
+		return
+	}
+
+	// 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
+	tablesList := make([]FutureGoodDataTables, 0)
+	for _, table := range tmpItems.Tables {
+		tableTimeList := make([]string, 0)
+		tableValueList := make([]float64, 0)
+		tableOpenList := make([]float64, 0)
+
+
+		Open       []float64 `json:"open"`
+		High       []float64 `json:"high"`
+		Low        []float64 `json:"low"`
+		Close      []float64 `json:"close"`
+		Volume     []float64 `json:"volume"`
+		Amount     []float64 `json:"amount"`
+		Ccl        []float64 `json:"ccl"`
+		Settlement []float64 `json:"settlement"`
+
+		for _, tableId := range table.ID {
+			tableIdList = append(tableIdList, tableId)
+		}
+		for _, tableTime := range table.Time {
+			tableTimeList = append(tableTimeList, tableTime)
+		}
+
+		//指标数据
+		for _, tmpValue := range table.Value {
+			var tableValue float64
+			if reflect.TypeOf(tmpValue).Kind() == reflect.Float64 {
+				tableValue = reflect.ValueOf(tmpValue).Float()
+			} else if reflect.TypeOf(tmpValue).Kind() == reflect.String {
+				tmpTableValue, tmpErr := decimal.NewFromString(reflect.ValueOf(tmpValue).String())
+				if tmpErr != nil {
+					err = tmpErr
+					return
+				}
+				tableValue, _ = tmpTableValue.Truncate(4).Float64()
+			} else {
+				err = errors.New("错误的数据类型" + reflect.TypeOf(tmpValue).String())
+				return
+			}
+			tableValueList = append(tableValueList, tableValue)
+		}
+		tmpTable := FutureGoodDataTables{
+			ID:    tableIdList,
+			Time:  tableTimeList,
+			Value: tableValueList,
+		}
+		tablesList = append(tablesList, tmpTable)
+	}
+	item = &FutureGoodDataFromThs{
+		DataVol:   tmpItems.DataVol,
+		Errmsg:    tmpItems.Errmsg,
+		Errorcode: tmpItems.Errorcode,
+		Perf:      tmpItems.Perf,
+		Tables:    tablesList,
+	}
+	return item, nil
+
+	//if err != nil {
+	//	return
+	//}
+	//item = new(FutureGoodDataFromWind)
+	//err = json.Unmarshal(body, &item)
+	//
+	////异常的话,需要邮件通知
+	//if len(item.ErrorCode) > 0 {
+	//	if item.ErrorCode["0"] != 0 {
+	//		if item.ErrorCode["0"] == -40522017 {
+	//			//{
+	//			//DT: {
+	//			//0: 1654646400000
+	//			//},
+	//			//CLOSE: {
+	//			//0: "CEDBService:: quota exceeded."
+	//			//},
+	//			//ErrorCode: {
+	//			//0: -40522017
+	//			//}
+	//			//}
+	//			// 设置服务器已超限
+	//			errorCode = 421
+	//			go utils.SendEmail("wind数据接口超限", "地址:"+requestWindUrl, utils.EmailSendToUsers)
+	//			return
+	//		} else if item.ErrorCode["0"] == -40520005 {
+	//			//.ErrorCode=-40520005
+	//			//.Data=[No Python API Authority
+	//			err = errors.New("No Python API Authority")
+	//			go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind商品数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
+	//			return
+	//		} else {
+	//			go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind商品数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
+	//		}
+	//	}
+	//}
+	//return
+}

+ 0 - 65
services/base_from_wind.go

@@ -132,71 +132,6 @@ func GetEdbDataFromWindUrl(windUrl, edbCode, startDate, endDate string) (item *E
 	return
 }
 
-// FutureGoodDataFromWind wind期货数据
-type FutureGoodDataFromWind struct {
-	ErrorCode map[string]int64   `json:"ErrorCode"`
-	Dt        map[string]int64   `json:"DT"`
-	TradeCode map[string]string  `json:"TRADE_CODE"`
-	Close     map[string]float64 `json:"CLOSE"`
-	Open      map[string]float64 `json:"OPEN"`
-	High      map[string]float64 `json:"HIGH"`
-	Low       map[string]float64 `json:"LOW"`
-	Volume    map[string]float64 `json:"VOLUME"`
-	Amt       map[string]float64 `json:"AMT"`
-	Oi        map[string]float64 `json:"OI"`
-	Settle    map[string]float64 `json:"SETTLE"`
-	ErrMsg    string
-}
-
-// GetFutureGoodDataFromWindUrl 通过url获取wind的商品数据
-func GetFutureGoodDataFromWindUrl(windUrl, edbCode, startDate, endDate string) (item *FutureGoodDataFromWind, errorCode int, err error) {
-	requestWindUrl := windUrl + `edbInfo/wind/future_good?FutureGoodEdbCode=%s&StartDate=%s&EndDate=%s`
-	requestWindUrl = fmt.Sprintf(requestWindUrl, edbCode, startDate, endDate)
-	utils.FileLog.Info(fmt.Sprintf("windUrl:%s", requestWindUrl))
-	body, err := http.Get(requestWindUrl)
-	fmt.Println("GetFutureGoodDataFromWindUrl body:")
-	fmt.Println(string(body))
-	utils.FileLog.Info(fmt.Sprint("wind result:", string(body)))
-
-	if err != nil {
-		return
-	}
-	item = new(FutureGoodDataFromWind)
-	err = json.Unmarshal(body, &item)
-
-	//异常的话,需要邮件通知
-	if len(item.ErrorCode) > 0 {
-		if item.ErrorCode["0"] != 0 {
-			if item.ErrorCode["0"] == -40522017 {
-				//{
-				//DT: {
-				//0: 1654646400000
-				//},
-				//CLOSE: {
-				//0: "CEDBService:: quota exceeded."
-				//},
-				//ErrorCode: {
-				//0: -40522017
-				//}
-				//}
-				// 设置服务器已超限
-				errorCode = 421
-				go utils.SendEmail("wind数据接口超限", "地址:"+requestWindUrl, utils.EmailSendToUsers)
-				return
-			} else if item.ErrorCode["0"] == -40520005 {
-				//.ErrorCode=-40520005
-				//.Data=[No Python API Authority
-				err = errors.New("No Python API Authority")
-				go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind商品数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
-				return
-			} else {
-				go alarm_msg.SendAlarmMsg(fmt.Sprintf("wind商品数据服务异常,edbCode:%s,ErrorCode:%d,result:%s", edbCode, item.ErrorCode["0"], string(body)), 3)
-			}
-		}
-	}
-	return
-}
-
 // GetWindUrl 获取wind的url
 func GetWindUrl(edbCode string) (windUrl string, err error) {
 	defer func() {