12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package data
- import (
- "encoding/json"
- "errors"
- "eta/eta_task/models/data_manage"
- "eta/eta_task/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/http"
- "strings"
- "time"
- )
- type EdbDataFromWind struct {
- Close map[string]float64 `json:"CLOSE"`
- Dt map[string]int64 `json:"DT"`
- }
- // 万得
- func SyncEdbDataByWind(edbInfoId int64, edbCode, startDate, endDate string) (err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("GetEdbDataByWind Err:" + err.Error())
- }
- }()
- windUrl := utils.WindServerUrl + `edbInfo/wind?EdbCode=%s&StartDate=%s&EndDate=%s`
- windUrl = fmt.Sprintf(windUrl, edbCode, startDate, endDate)
- utils.FileLog.Info("windUrl:" + windUrl)
- body, err := http.Get(windUrl)
- bodyStr := string(body)
- utils.FileLog.Info("wind result:" + bodyStr)
- if strings.Contains(bodyStr, "html") || strings.Contains(bodyStr, "CWSDService: No data.") {
- return nil
- }
- if err != nil {
- return
- }
- item := new(EdbDataFromWind)
- err = json.Unmarshal(body, &item)
- if err != nil {
- err = errors.New("Err:" + err.Error() + " EdbCode:" + edbCode + "\n" + " BodyStr:" + string(body))
- return
- }
- windList := make([]*data_manage.EdbDataWind, 0)
- for k, v := range item.Dt {
- dataTimestamp := v
- v = v / 1000
- fmt.Println(k, v)
- t := time.Unix(v, 0)
- dateTime := t.Format(utils.FormatDate)
- fmt.Println(dateTime)
- val := item.Close[k]
- count, err := data_manage.GetEdbDataWindCount(edbCode, dateTime)
- if err != nil {
- return err
- }
- if count > 0 {
- err = data_manage.ModifyEdbDataWind(edbInfoId, edbCode, dateTime, val)
- if err != nil {
- return err
- }
- } else {
- item := new(data_manage.EdbDataWind)
- item.EdbCode = edbCode
- item.EdbInfoId = int(edbInfoId)
- item.CreateTime = time.Now()
- item.ModifyTime = time.Now()
- item.DataTime = dateTime
- item.Value = val
- item.Status = 1
- item.DataTimestamp = dataTimestamp
- windList = append(windList, item)
- }
- }
- if len(windList) > 0 {
- err = data_manage.AddEdbDataWind(windList)
- if err != nil {
- return
- }
- }
- return
- }
|