123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package models
- import (
- "eta_gn/eta_index_lib/global"
- "eta_gn/eta_index_lib/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- type BridgeGnIndexDataParams struct {
- IndexCode string `json:"index_code" form:"index_code" description:"指标编码"`
- StartDate string `json:"start_date" form:"start_date" description:"开始日期"`
- EndDate string `json:"end_date" form:"end_date" description:"结束日期"`
- }
- type BridgeGnIndexAndData struct {
- Val float64 `json:"val"`
- DataTime string `json:"data_time"`
- UpdateTime string `json:"update_time"`
- }
- type BridgeGnResultIndexData struct {
- Code int `json:"code" description:"状态码"`
- Msg string `json:"msg" description:"提示信息"`
- Data []BridgeGnIndexAndData `json:"data" description:"返回数据"`
- }
- func RefreshEdbDataFromGn(source, subSource, edbInfoId int, tableName, edbCode, startDate string, dataList []BridgeGnIndexAndData) (err error) {
- if source <= 0 {
- err = fmt.Errorf("指标来源有误")
- return
- }
- if edbInfoId <= 0 {
- err = fmt.Errorf("指标ID有误")
- return
- }
- if tableName == "" {
- err = fmt.Errorf("数据表名为空")
- return
- }
- var realDataMaxDate, edbDataInsertConfigDate time.Time
- var edbDataInsertConfig *EdbDataInsertConfig
- var isFindConfigDateRealData bool
- {
- conf, e := GetEdbDataInsertConfigByEdbId(edbInfoId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- return
- }
- edbDataInsertConfig = conf
- if edbDataInsertConfig != nil {
- edbDataInsertConfigDate = edbDataInsertConfig.Date
- }
- }
- cond := ` AND edb_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, edbInfoId)
- var startDateTime time.Time
- if startDate != "" {
- cond += ` AND data_time >= ?`
- pars = append(pars, startDate)
- startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- }
- existList, e := GetEdbDataByCondition(source, subSource, cond, pars)
- if e != nil {
- err = fmt.Errorf("获取指标已有数据失败, Err: %s", e.Error())
- return
- }
- existMap := make(map[string]*EdbInfoSearchData)
- for _, v := range existList {
- existMap[v.DataTime] = v
- }
- hasNew := false
- strEdbInfoId := strconv.Itoa(edbInfoId)
- addExists := make(map[string]bool)
- sqlInsert := fmt.Sprintf(`INSERT INTO %s(edb_info_id, edb_code, data_time, value, create_time, modify_time, data_timestamp) VALUES `, tableName)
- for _, v := range dataList {
- currDataTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- val := utils.SubFloatToString(v.Val, 30)
- stamp := fmt.Sprint(currDataTime.UnixMilli())
- dataTime := currDataTime.Format(utils.FormatDate)
- if !startDateTime.IsZero() && currDataTime.Before(startDateTime) {
- t, e := GetEdbDataByDate(source, subSource, edbCode, dataTime)
- if e == nil && t != nil {
- existMap[t.DataTime] = t
- }
- }
- {
- if realDataMaxDate.IsZero() || currDataTime.After(realDataMaxDate) {
- realDataMaxDate = currDataTime
- }
- if edbDataInsertConfigDate.IsZero() || currDataTime.Equal(edbDataInsertConfigDate) {
- isFindConfigDateRealData = true
- }
- }
- exist, ok := existMap[dataTime]
- if !ok {
- if _, o := addExists[dataTime]; !o {
- hasNew = true
- sqlInsert += GetAddSql(strEdbInfoId, edbCode, dataTime, stamp, val)
- addExists[dataTime] = true
- }
- continue
- }
- if exist != nil && utils.SubFloatToString(exist.Value, 30) != val {
- if e = ModifyEdbDataById(source, subSource, exist.EdbDataId, val); e != nil {
- err = fmt.Errorf("modify edb data err: %s", e.Error())
- return
- }
- }
- }
- HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
- if !hasNew {
- return
- }
- sqlInsert = strings.TrimRight(sqlInsert, ",")
- e = global.DEFAULT_DmSQL.Exec(sqlInsert).Error
- if e != nil {
- err = fmt.Errorf("insert edb data err: %s", e.Error())
- return
- }
- return
- }
|