123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package models
- import (
- "eta_gn/eta_index_lib/global"
- "eta_gn/eta_index_lib/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- // BridgeGnIndexDataParams 桥接服务-获取国能指标数据入参
- 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:"结束日期"`
- }
- // BridgeGnIndexAndData 桥接服务-国能指标和数据
- type BridgeGnIndexAndData struct {
- Val float64 `json:"val"`
- DataTime string `json:"data_time"`
- UpdateTime string `json:"update_time"`
- }
- // BridgeGnResultIndexData 桥接服务-获取国能指标数据响应体
- type BridgeGnResultIndexData struct {
- Code int `json:"code" description:"状态码"`
- Msg string `json:"msg" description:"提示信息"`
- Data []BridgeGnIndexAndData `json:"data" description:"返回数据"`
- }
- // RefreshEdbDataFromGn
- // @Description: 刷新国能指标数据
- // @param source
- // @param subSource
- // @param edbInfoId
- // @param tableName
- // @param edbCode
- // @param startDate
- // @param dataList
- // @return err
- 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
- }
|