123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- package data_manage
- import (
- "encoding/json"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/http"
- "github.com/beego/beego/v2/client/orm"
- "hongze/hongze_task/utils"
- "reflect"
- "strconv"
- "strings"
- "time"
- )
- type EdbDataLt struct {
- EdbDataId int `orm:"column(edb_data_id);pk"`
- EdbInfoId int
- EdbCode string
- DataTime string
- Value float64
- Status int
- CreateTime time.Time
- ModifyTime time.Time
- DataTimestamp int64
- }
- // AddEdbDataLtBySql 执行添加数据的sql
- func AddEdbDataLtBySql(sqlStr string) (err error) {
- o := orm.NewOrmUsingDB("data")
- _, err = o.Raw(sqlStr).Exec()
- return
- }
- // ModifyEdbDataLt 修改路透社数据
- func ModifyEdbDataLt(edbInfoId int64, dataTime string, value float64) (err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` UPDATE edb_data_lt SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
- _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
- return
- }
- func GetEdbDataLtMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_lt WHERE edb_code=? `
- err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
- return
- }
- func GetEdbDataLtByCodeAndDate(edbCode string, startDate string) (count int, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT COUNT(1) AS count FROM edb_data_lt WHERE edb_code=? AND data_time=? `
- err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
- return
- }
- func GetEdbDataLtByCode(edbCode string, size int) (items []*EdbInfoSearchData, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_data_lt WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
- _, err = o.Raw(sql, edbCode, size).QueryRows(&items)
- return
- }
- //刷新所有数据
- func RefreshAllEdbDataByLt(edbInfoId, source int, edbCode, startDate, endDate string) (err error) {
- // 获取路透社数据
- ltDataList, err := QueryEdbDataByLt(edbCode, startDate, endDate)
- if err != nil {
- return
- }
- o := orm.NewOrmUsingDB("data")
- tx,err:=o.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- //获取指标所有数据
- dataList := make([]*EdbDataBase, 0)
- dataTableName := GetEdbDataTableName(source)
- sql := `SELECT * FROM %s WHERE edb_info_id=? `
- sql = fmt.Sprintf(sql, dataTableName)
- _, err = o.Raw(sql, edbInfoId).QueryRows(&dataList)
- if err != nil {
- return err
- }
- dataMap := make(map[string]string)
- for _, v := range dataList {
- dataMap[v.DataTime] = v.Value
- }
- edbInfoIdStr := strconv.Itoa(edbInfoId)
- addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
- var isAdd bool
- 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 err
- }
- saveValue := utils.SubFloatToString(sValue, 30)
- if existVal, ok := dataMap[eDate]; !ok {
- timestamp := dataTime.UnixNano() / 1e6
- timeStr := fmt.Sprintf("%d", timestamp)
- addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
- isAdd = true
- } else {
- if existVal != saveValue {
- sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
- sql = fmt.Sprintf(sql, dataTableName)
- _, err = tx.Raw(sql, sValue, edbInfoId, eDate).Exec()
- if err != nil {
- return err
- }
- }
- }
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = tx.Raw(addSql).Exec()
- if err != nil {
- fmt.Println("RefreshAllEdbDataByLt add Err", err.Error())
- return
- }
- }
- return
- }
- type EdbDataFromLt struct {
- Close map[int64]interface{} `json:"CLOSE"`
- }
- // 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.FileLog.Info("ltUrl:%s", ltUrl)
- body, err := http.Get(ltUrl)
- utils.FileLog.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
- }
|