123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package data
- import (
- "encoding/json"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/http"
- "hongze/hz_crm_api/models/data_manage"
- "hongze/hz_crm_api/utils"
- "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 GetEdbDataByPb(edbCode, startDate, endDate string) (searchItem *data_manage.EdbInfoSearch, err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("GetEdbDataByPb Err:%s", err.Error())
- }
- }()
- edbCode = url.QueryEscape(edbCode)
- bpUrl := utils.Hz_Data_PB_Url + `edbInfo/pb?EdbCode=%s&StartDate=%s&EndDate=%s`
- bpUrl = fmt.Sprintf(bpUrl, edbCode, startDate, endDate)
- utils.FileLog.Info("bpUrl:%s", bpUrl)
- body, err := http.Get(bpUrl)
- if err != nil {
- utils.FileLog.Info("GetEdbDataByPb Err:%s", err.Error())
- return
- }
- utils.FileLog.Info("GetEdbDataByPb result:%s", string(body))
- item := new(EdbDataFromPb)
- err = json.Unmarshal(body, &item)
- if err != nil {
- utils.FileLog.Info("GetEdbDataByPb Unmarshal Err:%s", err.Error())
- return
- }
- edbCode, _ = url.QueryUnescape(edbCode)
- searchItem = new(data_manage.EdbInfoSearch)
- searchItem.EdbCode = edbCode
- if len(item.Date) > 0 {
- var isAdd bool
- dateMap := item.Date
- addSql := ` INSERT INTO edb_data_pb(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,ticker,field,data_timestamp) values `
- nowStr := time.Now().Format(utils.FormatDateTime)
- for k, v := range dateMap {
- timeStr := fmt.Sprintf("%d", 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" {
- addSql += "("
- addSql += "0," + "'" + edbCode + "'" + "," + "'" + dateTime + "'" + "," + utils.SubFloatToString(val, 20) + "," + "'" + nowStr + "'" +
- "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + ticker + "'" + "," + "'" + field + "'" + "," + "'" + timeStr + "'"
- addSql += "),"
- isAdd = true
- }
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- err = data_manage.AddEdbDataPbBySql(addSql)
- if err != nil {
- utils.FileLogData.Info("GetEdbDataByPb AddEdbDataPbBySql Err:%s", err.Error())
- return searchItem, err
- }
- }
- size := utils.EDB_DATA_LIMIT
- dataList, err := data_manage.GetEdbDataPbByCode(edbCode, size)
- if err != nil {
- fmt.Println("GetEdbDataWindByCode Err", err.Error())
- return searchItem, err
- }
- minDate, maxDate, err := data_manage.GetEdbDataPbMaxOrMinDate(edbCode)
- if err != nil {
- return searchItem, err
- }
- searchItem.DataList = dataList
- searchItem.StartDate = minDate
- searchItem.EndDate = maxDate
- }
- if searchItem.DataList == nil {
- searchItem.DataList = make([]*data_manage.EdbInfoSearchData, 0)
- }
- return
- }
- func RefreshEdbDataByPb(edbInfoId int, edbCode, startDate, endDate string) (err error) {
- refreshEdbCode := edbCode
- edbCode = url.QueryEscape(edbCode)
- bpUrl := utils.Hz_Data_PB_Url + `edbInfo/pb?EdbCode=%s&StartDate=%s&EndDate=%s`
- bpUrl = fmt.Sprintf(bpUrl, edbCode, startDate, endDate)
- utils.FileLog.Info("bpUrl+" + bpUrl)
- body, err := http.Get(bpUrl)
- fmt.Println("RefreshEdbDataByPb body:")
- fmt.Println(string(body))
- utils.FileLog.Info("RefreshEdbDataByPb:" + string(body))
- if err != nil {
- return
- }
- item := new(EdbDataFromPb)
- err = json.Unmarshal(body, &item)
- if err != nil {
- return
- }
- if len(item.Date) > 0 {
- dateMap := item.Date
- pbList := make([]*data_manage.EdbDataPb, 0)
- for k, v := range dateMap {
- timestamp := 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.GetEdbDataPbByCodeAndDate(refreshEdbCode, dateTime)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return err
- }
- fmt.Println("field:", field)
- if count <= 0 {
- item := new(data_manage.EdbDataPb)
- item.EdbCode, _ = url.QueryUnescape(edbCode)
- item.EdbInfoId = 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 = timestamp
- pbList = append(pbList, item)
- } else {
- err = data_manage.ModifyEdbDataPb(edbInfoId, dateTime, val)
- if err != nil {
- return err
- }
- }
- }
- }
- if len(pbList) > 0 {
- err = data_manage.AddEdbDataPb(pbList)
- if err != nil {
- fmt.Println("AddEdbDataPb Err", err.Error())
- return
- }
- }
- }
- return
- }
|