|
@@ -1,16 +1,14 @@
|
|
|
package models
|
|
|
|
|
|
import (
|
|
|
- "errors"
|
|
|
"eta/eta_index_lib/models/mgo"
|
|
|
"eta/eta_index_lib/utils"
|
|
|
"fmt"
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
|
- "github.com/qiniu/qmgo"
|
|
|
"github.com/shopspring/decimal"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
- "go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
"reflect"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -105,7 +103,7 @@ func (obj Business) Add(params AddBaseParams, businessIndexItem *BaseFromBusines
|
|
|
edbInfo.EdbInfoId = int(newEdbInfoId)
|
|
|
|
|
|
// 更新数据
|
|
|
- err = obj.refresh(edbInfo, "")
|
|
|
+ err = obj.refresh(to, edbInfo, "")
|
|
|
|
|
|
return
|
|
|
}
|
|
@@ -155,7 +153,7 @@ func (obj Business) Edit(params EditBaseParams, businessIndexItem *BaseFromBusin
|
|
|
}
|
|
|
|
|
|
//计算数据
|
|
|
- err = obj.refresh(edbInfo, "")
|
|
|
+ err = obj.refresh(to, edbInfo, "")
|
|
|
|
|
|
return
|
|
|
}
|
|
@@ -176,7 +174,7 @@ func (obj Business) Refresh(params RefreshBaseParams) (err error, errMsg string)
|
|
|
}()
|
|
|
|
|
|
// 计算数据
|
|
|
- err = obj.refresh(params.EdbInfo, params.StartDate)
|
|
|
+ err = obj.refresh(to, params.EdbInfo, params.StartDate)
|
|
|
|
|
|
return
|
|
|
}
|
|
@@ -196,7 +194,17 @@ func (obj Business) GetEdbType() int {
|
|
|
return utils.DEFAULT_EDB_TYPE
|
|
|
}
|
|
|
|
|
|
-func (obj Business) refresh(edbInfo *EdbInfo, startDate string) (err error) {
|
|
|
+func (obj Business) refresh(to orm.TxOrmer, edbInfo *EdbInfo, startDate string) (err error) {
|
|
|
+ if utils.UseMongo {
|
|
|
+ return obj.refreshByMongo(edbInfo, startDate)
|
|
|
+ } else {
|
|
|
+ return obj.refreshByMysql(to, edbInfo, startDate)
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func (obj Business) refreshByMongo(edbInfo *EdbInfo, startDate string) (err error) {
|
|
|
// 真实数据的最大日期 , 插入规则配置的日期
|
|
|
var realDataMaxDate, edbDataInsertConfigDate time.Time
|
|
|
var edbDataInsertConfig *EdbDataInsertConfig
|
|
@@ -212,7 +220,7 @@ func (obj Business) refresh(edbInfo *EdbInfo, startDate string) (err error) {
|
|
|
}
|
|
|
|
|
|
//获取已存在的所有数据
|
|
|
- baseDataList, err := obj.getBaseBusinessData(edbInfo, startDate)
|
|
|
+ baseDataList, err := obj.getBaseBusinessDataByMongo(edbInfo, startDate)
|
|
|
|
|
|
//获取指标所有数据
|
|
|
existDataList := make([]*mgo.EdbDataBusiness, 0)
|
|
@@ -354,8 +362,144 @@ func (obj Business) refresh(edbInfo *EdbInfo, startDate string) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func (obj Business) refreshByMysql(to orm.TxOrmer, edbInfo *EdbInfo, startDate string) (err error) {
|
|
|
+ dataTableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
|
|
|
+ edbInfoIdStr := fmt.Sprint(edbInfo.EdbInfoId)
|
|
|
+
|
|
|
+ // 真实数据的最大日期 , 插入规则配置的日期
|
|
|
+ var realDataMaxDate, edbDataInsertConfigDate time.Time
|
|
|
+ var edbDataInsertConfig *EdbDataInsertConfig
|
|
|
+ var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
|
|
|
+ {
|
|
|
+ edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfo.EdbInfoId)
|
|
|
+ if err != nil && err.Error() != utils.ErrNoRow() {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if edbDataInsertConfig != nil {
|
|
|
+ edbDataInsertConfigDate = edbDataInsertConfig.Date
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取已存在的所有数据
|
|
|
+ baseDataList, err := obj.getBaseBusinessDataByMysql(edbInfo, startDate)
|
|
|
+
|
|
|
+ //获取指标所有数据
|
|
|
+ var existCondition string
|
|
|
+ var existPars []interface{}
|
|
|
+
|
|
|
+ existCondition += " AND edb_info_id=? "
|
|
|
+ existPars = append(existPars, edbInfo.EdbInfoId)
|
|
|
+ if startDate != "" {
|
|
|
+ existCondition += " AND data_time>=? "
|
|
|
+ existPars = append(existPars, startDate)
|
|
|
+ }
|
|
|
+
|
|
|
+ existList, err := GetEdbDataByCondition(edbInfo.Source, edbInfo.SubSource, existCondition, existPars)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(obj.GetSourceName() + ",refreshByMysql err;getEdbDataBusinessList Err:" + err.Error())
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ existDataMap := make(map[string]*EdbInfoSearchData)
|
|
|
+ removeDataTimeMap := make(map[string]bool) //需要移除的日期数据
|
|
|
+ for _, v := range existList {
|
|
|
+ existDataMap[v.DataTime] = v
|
|
|
+ removeDataTimeMap[v.DataTime] = true
|
|
|
+ }
|
|
|
+ needAddDateMap := make(map[time.Time]int)
|
|
|
+
|
|
|
+ // 待添加的数据集
|
|
|
+ addSql := ` INSERT INTO ` + dataTableName + ` (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
|
|
|
+ var isAdd bool
|
|
|
+ // 待更新的数据集
|
|
|
+ updateDataList := make([]*EdbInfoSearchData, 0)
|
|
|
+
|
|
|
+ for _, tmpData := range baseDataList {
|
|
|
+ currDate := tmpData.DataTime
|
|
|
+ currDateStr := currDate.Format(utils.FormatDate)
|
|
|
+
|
|
|
+ // 当前的实际值
|
|
|
+ saveValue := decimal.NewFromFloat(tmpData.Value).Round(4).String()
|
|
|
+
|
|
|
+ // 下面代码主要目的是处理掉手动插入的数据判断
|
|
|
+ {
|
|
|
+ if realDataMaxDate.IsZero() || currDate.After(realDataMaxDate) {
|
|
|
+ realDataMaxDate = currDate
|
|
|
+ }
|
|
|
+ if edbDataInsertConfigDate.IsZero() || currDate.Equal(edbDataInsertConfigDate) {
|
|
|
+ isFindConfigDateRealData = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ existData, ok := existDataMap[currDateStr]
|
|
|
+ // 如果库中已经存在该数据的话,那么就进行值的变更操作
|
|
|
+ if ok {
|
|
|
+ // 已经入到指标库的值
|
|
|
+ existValStr := decimal.NewFromFloat(existData.Value).Round(4).String()
|
|
|
+
|
|
|
+ //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该日期
|
|
|
+ delete(removeDataTimeMap, currDateStr)
|
|
|
+ if existValStr != saveValue {
|
|
|
+ existData.Value = tmpData.Value
|
|
|
+ updateDataList = append(updateDataList, existData)
|
|
|
+ }
|
|
|
+
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 库中不存在该日期的数据
|
|
|
+ timestamp := currDate.UnixNano() / 1e6
|
|
|
+ needAddDateMap[currDate] = 1
|
|
|
+ addSql += GetAddSql(edbInfoIdStr, edbInfo.EdbCode, currDateStr, fmt.Sprint(timestamp), saveValue)
|
|
|
+ isAdd = true
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除已经不存在的指标数据(由于该指标当日的数据删除了)
|
|
|
+ {
|
|
|
+ removeDateList := make([]string, 0)
|
|
|
+ for dateTime := range removeDataTimeMap {
|
|
|
+ removeDateList = append(removeDateList, dateTime)
|
|
|
+ }
|
|
|
+ removeNum := len(removeDateList)
|
|
|
+ if removeNum > 0 {
|
|
|
+ //如果拼接指标变更了,那么需要删除所有的指标数据
|
|
|
+ sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (`+utils.GetOrmInReplace(removeNum)+`) `, dataTableName)
|
|
|
+
|
|
|
+ _, err = to.Raw(sql, edbInfo.EdbInfoId, removeDateList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("删除自有数据的明细数据失败,Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if isAdd {
|
|
|
+ addSql = strings.TrimRight(addSql, ",")
|
|
|
+ _, err = to.Raw(addSql).Exec()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("RefreshAllCalculate add Err", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 修改历史数据
|
|
|
+ if len(updateDataList) > 0 {
|
|
|
+ for _, v := range updateDataList {
|
|
|
+ err = ModifyEdbDataById(edbInfo.Source, edbInfo.SubSource, v.EdbDataId, fmt.Sprint(v.Value))
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(obj.GetSourceName() + ",refreshByMysql:Err:" + err.Error())
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理手工数据补充的配置
|
|
|
+ HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, existDataMap, isFindConfigDateRealData)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
// GetEdbInfoMaxAndMinInfo 获取指标的最新数据记录信息
|
|
|
-func (obj Business) GetEdbInfoMaxAndMinInfo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
|
|
|
+func (obj Business) getEdbInfoMaxAndMinInfoByMongo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
|
|
|
mogDataObj := new(mgo.EdbDataBusiness)
|
|
|
pipeline := []bson.M{
|
|
|
{"$match": bson.M{"edb_code": edbCode}},
|
|
@@ -407,18 +551,17 @@ func (obj Business) GetEdbInfoMaxAndMinInfo(edbCode string) (item *EdbInfoMaxAnd
|
|
|
// @param edbInfo *EdbInfo
|
|
|
// @return err error
|
|
|
func (obj Business) UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo *EdbInfo) (err error) {
|
|
|
- edbInfoMaxAndMinInfo, err := obj.GetEdbInfoMaxAndMinInfo(edbInfo.EdbCode)
|
|
|
- // 如果有错误,且错误信息是取不到文档,那么就不修改了
|
|
|
- if err != nil && !errors.Is(err, qmgo.ErrNoSuchDocuments) {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- // 如果正常获取到了,那就去修改指标的最大最小值
|
|
|
- if err == nil && edbInfoMaxAndMinInfo != nil {
|
|
|
- err = ModifyEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, edbInfoMaxAndMinInfo)
|
|
|
+ if utils.UseMongo {
|
|
|
+ edbInfoMaxAndMinInfo, tmpErr := obj.getEdbInfoMaxAndMinInfoByMongo(edbInfo.EdbCode)
|
|
|
+ // 如果正常获取到了,那就去修改指标的最大最小值
|
|
|
+ if tmpErr == nil && edbInfoMaxAndMinInfo != nil {
|
|
|
+ err = ModifyEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, edbInfoMaxAndMinInfo)
|
|
|
+ } else {
|
|
|
+ // 清空的目的是为了避免异常返回
|
|
|
+ err = nil
|
|
|
+ }
|
|
|
} else {
|
|
|
- // 清空的目的是为了避免异常返回
|
|
|
- err = nil
|
|
|
+ err, _ = UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
|
|
|
}
|
|
|
|
|
|
return
|
|
@@ -427,10 +570,10 @@ func (obj Business) UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo *EdbInfo) (err err
|
|
|
// EdbInfoMgoData
|
|
|
// @Description: mgo里面的数据
|
|
|
type EdbInfoMgoData struct {
|
|
|
- EdbDataId primitive.ObjectID `description:"数据ID"`
|
|
|
- DataTime time.Time `description:"数据日期"`
|
|
|
- Value float64 `description:"数据"`
|
|
|
- EdbCode string `description:"指标编码"`
|
|
|
+ //EdbDataId primitive.ObjectID `description:"数据ID"`
|
|
|
+ DataTime time.Time `description:"数据日期"`
|
|
|
+ Value float64 `description:"数据"`
|
|
|
+ EdbCode string `description:"指标编码"`
|
|
|
}
|
|
|
|
|
|
// getBaseBusinessData
|
|
@@ -443,6 +586,19 @@ type EdbInfoMgoData struct {
|
|
|
// @return newDataList []EdbInfoSearchData
|
|
|
// @return err error
|
|
|
func (obj Business) getBaseBusinessData(edbInfo *EdbInfo, startDate string) (newDataList []EdbInfoMgoData, err error) {
|
|
|
+ return obj.getBaseBusinessDataByMongo(edbInfo, startDate)
|
|
|
+}
|
|
|
+
|
|
|
+// getBaseBusinessDataByMongo
|
|
|
+// @Description: 从mongo中获取基础的明细数据
|
|
|
+// @author: Roc
|
|
|
+// @receiver obj
|
|
|
+// @datetime 2024-07-02 10:12:02
|
|
|
+// @param edbInfo *EdbInfo
|
|
|
+// @param startDate string
|
|
|
+// @return newDataList []EdbInfoMgoData
|
|
|
+// @return err error
|
|
|
+func (obj Business) getBaseBusinessDataByMongo(edbInfo *EdbInfo, startDate string) (newDataList []EdbInfoMgoData, err error) {
|
|
|
newDataList = make([]EdbInfoMgoData, 0)
|
|
|
|
|
|
// 获取数据源的指标数据
|
|
@@ -471,10 +627,53 @@ func (obj Business) getBaseBusinessData(edbInfo *EdbInfo, startDate string) (new
|
|
|
|
|
|
for _, v := range baseDataList {
|
|
|
newDataList = append(newDataList, EdbInfoMgoData{
|
|
|
- EdbDataId: v.ID,
|
|
|
- DataTime: v.DataTime,
|
|
|
- Value: v.Value,
|
|
|
- EdbCode: v.IndexCode,
|
|
|
+ //EdbDataId: v.ID,
|
|
|
+ DataTime: v.DataTime,
|
|
|
+ Value: v.Value,
|
|
|
+ EdbCode: v.IndexCode,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// getBaseBusinessDataByMysql
|
|
|
+// @Description: 从mysql中获取基础的明细数据
|
|
|
+// @author: Roc
|
|
|
+// @receiver obj
|
|
|
+// @datetime 2024-07-02 10:12:16
|
|
|
+// @param edbInfo *EdbInfo
|
|
|
+// @param startDate string
|
|
|
+// @return newDataList []EdbInfoMgoData
|
|
|
+// @return err error
|
|
|
+func (obj Business) getBaseBusinessDataByMysql(edbInfo *EdbInfo, startDate string) (newDataList []EdbInfoMgoData, err error) {
|
|
|
+ newDataList = make([]EdbInfoMgoData, 0)
|
|
|
+ // 获取数据源的指标数据
|
|
|
+ baseBusinessDataObj := new(BaseFromBusinessData)
|
|
|
+
|
|
|
+ // 构建查询条件
|
|
|
+ var condition []string
|
|
|
+ var pars []interface{}
|
|
|
+ condition = append(condition, "index_code = ? ")
|
|
|
+ pars = append(pars, edbInfo.EdbCode)
|
|
|
+
|
|
|
+ if startDate != `` {
|
|
|
+ condition = append(condition, " data_time >= ? ")
|
|
|
+ pars = append(pars, startDate)
|
|
|
+ }
|
|
|
+
|
|
|
+ baseDataList, err := baseBusinessDataObj.GetAllDataList(condition, pars, " data_time ASC ")
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("getBaseBusinessData Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range baseDataList {
|
|
|
+ newDataList = append(newDataList, EdbInfoMgoData{
|
|
|
+ //EdbDataId: v.BusinessDataId,
|
|
|
+ DataTime: v.DataTime,
|
|
|
+ Value: v.Value,
|
|
|
+ EdbCode: v.IndexCode,
|
|
|
})
|
|
|
}
|
|
|
|