package data import ( "encoding/json" "eta/eta_task/models/data_manage" "eta/eta_task/utils" "fmt" "github.com/rdlucklib/rdluck_tools/http" "net/url" "strings" "time" ) type EdbDataFromPb struct { Date map[string]int64 `json:"date"` Ticker map[string]string `json:"ticker"` Field map[string]string `json:"field"` Value map[string]float64 `json:"value"` } // 万得 func SyncEdbDataByPb(edbInfoId int64, edbCode, startDate, endDate string) (err error) { defer func() { if err != nil { fmt.Println("SyncEdbDataByPb Err:" + err.Error()) } }() addEdbCode := edbCode edbCode = url.QueryEscape(edbCode) pbUrl := utils.PbServerUrl + `edbInfo/pb?EdbCode=%s&StartDate=%s&EndDate=%s` pbUrl = fmt.Sprintf(pbUrl, edbCode, startDate, endDate) utils.FileLog.Info("pbUrl:" + pbUrl) body, err := http.Get(pbUrl) utils.FileLog.Info("pb result:" + string(body)) if strings.Contains(string(body), "html") { return nil } if err != nil { return } item := new(EdbDataFromPb) err = json.Unmarshal(body, &item) if err != nil { return } pbList := make([]*data_manage.EdbDataPb, 0) if len(item.Date) > 0 { dateMap := item.Date for k, v := range dateMap { dataTimestamp := v v = v / 1000 t := time.Unix(v, 0) dateTime := t.Format(utils.FormatDate) val := item.Value[k] field := item.Field[k] ticker := item.Ticker[k] if field == "PX_LAST" { count, err := data_manage.GetEdbDataPbCount(addEdbCode, dateTime) if err != nil { return err } if count > 0 { err = data_manage.ModifyEdbDataPb(edbInfoId, addEdbCode, dateTime, val) if err != nil { return err } } else { item := new(data_manage.EdbDataPb) item.EdbCode = addEdbCode item.EdbInfoId = int(edbInfoId) item.CreateTime = time.Now() item.ModifyTime = time.Now() item.DataTime = dateTime item.Value = val item.Status = 1 item.Field = field item.Ticker = ticker item.DataTimestamp = dataTimestamp pbList = append(pbList, item) } } } } if len(pbList) > 0 { err = data_manage.AddEdbDataPb(pbList) if err != nil { return } } return }