package data import ( "encoding/json" "fmt" "github.com/rdlucklib/rdluck_tools/http" "hongze/hz_crm_api/models/data_manage" "hongze/hz_crm_api/utils" "reflect" "strings" "time" ) type EdbDataFromLt struct { Close map[int64]interface{} `json:"CLOSE"` } // GetEdbDataByLt 获取路透社指标 func GetEdbDataByLt(edbCode, startDate, endDate string) (searchItem *data_manage.EdbInfoSearch, err error) { // 获取路透社数据 ltDataList, err := QueryEdbDataByLt(edbCode, startDate, endDate) if err != nil { return } searchItem = new(data_manage.EdbInfoSearch) searchItem.EdbCode = edbCode if len(ltDataList) > 0 { var isAdd bool addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values ` nowStr := time.Now().Format(utils.FormatDateTime) dataList := make([]*data_manage.EdbInfoSearchData, 0) for timestampInt, edbValue := range ltDataList { dataTime := time.Unix(timestampInt/1000, 0) //校验数据类型对不对 valType := reflect.TypeOf(edbValue) if valType == nil { continue } if valType.String() != "float64" { continue } sValue := edbValue.(float64) eDate := dataTime.Format(utils.FormatDate) if err != nil { return nil, err } timestamp := dataTime.UnixNano() / 1e6 timeStr := fmt.Sprintf("%d", timestamp) addSql += "(" addSql += "0," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + utils.SubFloatToString(sValue, 20) + "," + "'" + nowStr + "'" + "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'" addSql += ")," isAdd = true } if isAdd { addSql = strings.TrimRight(addSql, ",") err = data_manage.AddEdbDataLtBySql(addSql) if err != nil { utils.FileLogData.Info("AddEdbDataLtBySql Err:%s", err.Error()) return } } size := utils.EDB_DATA_LIMIT dataList, err := data_manage.GetEdbDataAllByEdbCode(edbCode, utils.DATA_SOURCE_LT, size) if err != nil { utils.FileLogData.Info("GetEdbDataLtByCode Err:%s", err.Error()) return searchItem, err } edbInfoMaxAndMinInfo, err := data_manage.GetEdbInfoMaxAndMinInfo(utils.DATA_SOURCE_LT, edbCode) if err != nil { return searchItem, err } searchItem.DataList = dataList searchItem.StartDate = edbInfoMaxAndMinInfo.MinDate searchItem.EndDate = edbInfoMaxAndMinInfo.MaxDate } if searchItem.DataList == nil { searchItem.DataList = make([]*data_manage.EdbInfoSearchData, 0) } return } //func init() { // fmt.Println("aa") // searchInfo, err := GetEdbDataByLt("OMA-1Mdubsw-A", "2021-01-01", "2021-12-28") // if err != nil { // fmt.Println("err:", err) // } else { // fmt.Println(searchInfo) // } //} // RefreshEdbDataByLt 刷新路透社指标数据 func RefreshEdbDataByLt(edbInfoId int, edbCode, startDate, endDate string) (err error) { // 获取路透社数据 //ltDataList,err := QueryEdbDataByLt(edbCode, startDate, endDate) //if err != nil{ // return //} //if item.Errorcode != 0 { // err = errors.New(string(body)) // return //} //if len(item.Tables) > 0 { // table := item.Tables[0] // dataLen := len(table.Time) // addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values ` // nowStr := time.Now().Format(utils.FormatDateTime) // var isAdd bool // for i := 0; i < dataLen; i++ { // eDate := table.Time[i] // sValue := table.Value[i] // // count, err := data_manage.GetEdbDataLtByCodeAndDate(edbCode, eDate) // if err != nil && err.Error() != utils.ErrNoRow() { // return err // } // if count <= 0 { // dataTime, err := time.Parse(utils.FormatDate, eDate) // if err != nil { // // } // timestamp := dataTime.UnixNano() / 1e6 // timeStr := fmt.Sprintf("%d", timestamp) // // addSql += "(" // addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + utils.SubFloatToString(sValue, 30) + "," + "'" + nowStr + "'" + // "," + "'" + nowStr + "'" + "," + "1" // addSql += "," + "'" + timeStr + "'" // addSql += ")," // isAdd = true // } else { // err = data_manage.ModifyEdbDataLt(int64(edbInfoId), eDate, sValue) // if err != nil { // return err // } // } // } // // if isAdd { // addSql = strings.TrimRight(addSql, ",") // err = data_manage.AddEdbDataLtBySql(addSql) // if err != nil { // fmt.Println("AddEdbDataLt Err", err.Error()) // return // } // } //} return } // QueryEdbDataByLt 获取路透社数据 func QueryEdbDataByLt(edbCode, startDate, endDate string) (dataList map[int64]interface{}, err error) { dataList = make(map[int64]interface{}) ltUrl := utils.Hz_Data_LT_Url + `edbInfo/ek?EdbCode=%s&StartDate=%s&EndDate=%s` ltUrl = fmt.Sprintf(ltUrl, edbCode, startDate, endDate) utils.FileLogData.Info("ltUrl:%s", ltUrl) body, err := http.Get(ltUrl) utils.FileLogData.Info("lt result:%s", string(body)) if err != nil { return } //fmt.Println(string(body)) item := new(EdbDataFromLt) err = json.Unmarshal(body, &item) if err != nil { return } dataList = item.Close return }