|
@@ -3,13 +3,14 @@ package models
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
+ "eta/eta_index_lib/models/mgo"
|
|
|
"eta/eta_index_lib/utils"
|
|
|
"fmt"
|
|
|
- "strconv"
|
|
|
- "time"
|
|
|
-
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
|
"github.com/shopspring/decimal"
|
|
|
+ "go.mongodb.org/mongo-driver/bson"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type EdbInfo struct {
|
|
@@ -182,20 +183,77 @@ func (edbInfo *EdbInfo) Update(cols []string) (err error) {
|
|
|
|
|
|
// EdbInfoSearchData
|
|
|
type EdbInfoSearchData struct {
|
|
|
- EdbDataId int `description:"数据ID"`
|
|
|
- DataTime string `description:"数据日期"`
|
|
|
- Value float64 `description:"数据"`
|
|
|
- EdbCode string `description:"指标编码"`
|
|
|
+ EdbDataId int `description:"数据ID"`
|
|
|
+ EdbInfoId int `description:"指标ID"`
|
|
|
+ DataTime string `description:"数据日期"`
|
|
|
+ Value float64 `description:"数据"`
|
|
|
+ EdbCode string `description:"指标编码"`
|
|
|
+ DataTimestamp int64 `description:"时间戳"`
|
|
|
}
|
|
|
|
|
|
-// GetEdbDataListAll 获取指标数据列表 order:1升序,其余值为降序
|
|
|
-func GetEdbDataListAll(condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchData, err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
+type FindEdbDataListAllCond struct {
|
|
|
+ EdbInfoId int
|
|
|
+ StartDataTime string
|
|
|
+ StartDataTimeCond string
|
|
|
+ EndDataTime string
|
|
|
+ EndDataTimeCond string
|
|
|
+}
|
|
|
+
|
|
|
+// GetEdbDataListAll
|
|
|
+// @Description: 获取指标数据列表 order:1升序,其余值为降序
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-05-08 15:34:01
|
|
|
+// @param source int
|
|
|
+// @param subSource int
|
|
|
+// @param findEdbDataListAllCond FindEdbDataListAllCond
|
|
|
+// @param order int order:1升序,其余值为降序
|
|
|
+// @return item []*EdbInfoSearchData
|
|
|
+// @return err error
|
|
|
+func GetEdbDataListAll(source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (item []*EdbInfoSearchData, err error) {
|
|
|
+ if source == utils.DATA_SOURCE_BUSINESS {
|
|
|
+ return GetEdbDataListAllByMongo(source, subSource, findEdbDataListAllCond, order)
|
|
|
+ }
|
|
|
+
|
|
|
+ return GetEdbDataListAllByMysql(source, subSource, findEdbDataListAllCond, order)
|
|
|
+}
|
|
|
+
|
|
|
+// GetEdbDataListAllByMysql
|
|
|
+// @Description: 从mysql数据库中获取指标数据列表 order:1升序,其余值为降序
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-05-08 15:32:55
|
|
|
+// @param source int
|
|
|
+// @param subSource int
|
|
|
+// @param findEdbDataListAllCond FindEdbDataListAllCond
|
|
|
+// @param order int
|
|
|
+// @return item []*EdbInfoSearchData
|
|
|
+// @return err error
|
|
|
+func GetEdbDataListAllByMysql(source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (item []*EdbInfoSearchData, err error) {
|
|
|
+ if findEdbDataListAllCond.EdbInfoId <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ to := orm.NewOrm()
|
|
|
+
|
|
|
sql := ``
|
|
|
tableName := GetEdbDataTableName(source, subSource)
|
|
|
sql = ` SELECT * FROM %s WHERE 1=1 `
|
|
|
sql = fmt.Sprintf(sql, tableName)
|
|
|
|
|
|
+ var condition string
|
|
|
+ var pars []interface{}
|
|
|
+ condition += " AND edb_info_id=? "
|
|
|
+ pars = append(pars, findEdbDataListAllCond.EdbInfoId)
|
|
|
+
|
|
|
+ // 开始日期
|
|
|
+ if findEdbDataListAllCond.StartDataTime != "" && findEdbDataListAllCond.StartDataTimeCond != `` {
|
|
|
+ condition += fmt.Sprintf(" AND data_time %s ? ", findEdbDataListAllCond.StartDataTimeCond)
|
|
|
+ pars = append(pars, findEdbDataListAllCond.StartDataTime)
|
|
|
+ }
|
|
|
+ // 结束日期
|
|
|
+ if findEdbDataListAllCond.EndDataTime != "" && findEdbDataListAllCond.EndDataTimeCond != `` {
|
|
|
+ condition += fmt.Sprintf(" AND data_time %s ? ", findEdbDataListAllCond.EndDataTimeCond)
|
|
|
+ pars = append(pars, findEdbDataListAllCond.EndDataTime)
|
|
|
+ }
|
|
|
+
|
|
|
if condition != "" {
|
|
|
sql += condition
|
|
|
}
|
|
@@ -204,17 +262,65 @@ func GetEdbDataListAll(condition string, pars []interface{}, source, subSource,
|
|
|
} else {
|
|
|
sql += ` ORDER BY data_time DESC `
|
|
|
}
|
|
|
- _, err = o.Raw(sql, pars).QueryRows(&item)
|
|
|
+ _, err = to.Raw(sql, pars).QueryRows(&item)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// GetEdbDataListAllByTo 根据事务链接获取指标数据列表 order:1升序,其余值为降序
|
|
|
-func GetEdbDataListAllByTo(to orm.TxOrmer, condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchData, err error) {
|
|
|
+// GetEdbDataListAllByTo
|
|
|
+// @Description: 根据事务链接获取指标数据列表 order:1升序,其余值为降序
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-05-08 15:34:06
|
|
|
+// @param to orm.TxOrmer
|
|
|
+// @param source int
|
|
|
+// @param subSource int
|
|
|
+// @param findEdbDataListAllCond FindEdbDataListAllCond
|
|
|
+// @param order int
|
|
|
+// @return item []*EdbInfoSearchData
|
|
|
+// @return err error
|
|
|
+func GetEdbDataListAllByTo(to orm.TxOrmer, source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (item []*EdbInfoSearchData, err error) {
|
|
|
+ if source == utils.DATA_SOURCE_BUSINESS {
|
|
|
+ return GetEdbDataListAllByMongo(source, subSource, findEdbDataListAllCond, order)
|
|
|
+ }
|
|
|
+
|
|
|
+ return GetEdbDataListAllByMysqlTo(to, source, subSource, findEdbDataListAllCond, order)
|
|
|
+}
|
|
|
+
|
|
|
+// GetEdbDataListAllByMysqlTo
|
|
|
+// @Description: 根据事务链接获取指标数据列表 order:1升序,其余值为降序(Mysql)
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-05-08 15:34:13
|
|
|
+// @param to orm.TxOrmer
|
|
|
+// @param source int
|
|
|
+// @param subSource int
|
|
|
+// @param findEdbDataListAllCond FindEdbDataListAllCond
|
|
|
+// @param order int
|
|
|
+// @return item []*EdbInfoSearchData
|
|
|
+// @return err error
|
|
|
+func GetEdbDataListAllByMysqlTo(to orm.TxOrmer, source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (item []*EdbInfoSearchData, err error) {
|
|
|
+ if findEdbDataListAllCond.EdbInfoId <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
sql := ``
|
|
|
tableName := GetEdbDataTableName(source, subSource)
|
|
|
sql = ` SELECT * FROM %s WHERE 1=1 `
|
|
|
sql = fmt.Sprintf(sql, tableName)
|
|
|
|
|
|
+ var condition string
|
|
|
+ var pars []interface{}
|
|
|
+ condition += " AND edb_info_id=? "
|
|
|
+ pars = append(pars, findEdbDataListAllCond.EdbInfoId)
|
|
|
+
|
|
|
+ // 开始日期
|
|
|
+ if findEdbDataListAllCond.StartDataTime != "" && findEdbDataListAllCond.StartDataTimeCond != `` {
|
|
|
+ condition += fmt.Sprintf(" AND data_time %s ? ", findEdbDataListAllCond.StartDataTimeCond)
|
|
|
+ pars = append(pars, findEdbDataListAllCond.StartDataTime)
|
|
|
+ }
|
|
|
+ // 结束日期
|
|
|
+ if findEdbDataListAllCond.EndDataTime != "" && findEdbDataListAllCond.EndDataTimeCond != `` {
|
|
|
+ condition += fmt.Sprintf(" AND data_time %s ? ", findEdbDataListAllCond.EndDataTimeCond)
|
|
|
+ pars = append(pars, findEdbDataListAllCond.EndDataTime)
|
|
|
+ }
|
|
|
+
|
|
|
if condition != "" {
|
|
|
sql += condition
|
|
|
}
|
|
@@ -227,6 +333,80 @@ func GetEdbDataListAllByTo(to orm.TxOrmer, condition string, pars []interface{},
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// GetEdbDataListAllByMongo
|
|
|
+// @Description: 根据事务链接获取指标数据列表 order:1升序,其余值为降序(Mongo)
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-05-08 15:34:24
|
|
|
+// @param source int
|
|
|
+// @param subSource int
|
|
|
+// @param findEdbDataListAllCond FindEdbDataListAllCond
|
|
|
+// @param order int
|
|
|
+// @return dataList []*EdbInfoSearchData
|
|
|
+// @return err error
|
|
|
+func GetEdbDataListAllByMongo(source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (dataList []*EdbInfoSearchData, err error) {
|
|
|
+ dataList = make([]*EdbInfoSearchData, 0)
|
|
|
+ if findEdbDataListAllCond.EdbInfoId <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ mogDataObj := mgo.EdbDataBusiness{}
|
|
|
+ // 构建查询条件
|
|
|
+ queryConditions := bson.M{
|
|
|
+ "edb_info_id": findEdbDataListAllCond.EdbInfoId,
|
|
|
+ }
|
|
|
+ // 开始日期
|
|
|
+ if findEdbDataListAllCond.StartDataTime != "" {
|
|
|
+ startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, findEdbDataListAllCond.StartDataTime, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 日期比较符
|
|
|
+ cond := utils.DateConvMysqlConvMongo(findEdbDataListAllCond.StartDataTimeCond)
|
|
|
+ if cond != `` {
|
|
|
+ queryConditions["data_time"] = bson.M{cond: startDateTime}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 结束日期
|
|
|
+ if findEdbDataListAllCond.EndDataTime != "" {
|
|
|
+ endDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, findEdbDataListAllCond.EndDataTime, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 日期比较符
|
|
|
+ cond := utils.DateConvMysqlConvMongo(findEdbDataListAllCond.EndDataTimeCond)
|
|
|
+ if cond != `` {
|
|
|
+ queryConditions["data_time"] = bson.M{cond: endDateTime}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ sortList := make([]string, 0)
|
|
|
+ if order == 1 {
|
|
|
+ sortList = append(sortList, "-data_time")
|
|
|
+ } else {
|
|
|
+ sortList = append(sortList, "data_time")
|
|
|
+ }
|
|
|
+ // 获取列表数据
|
|
|
+ tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, sortList)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for k, v := range tmpDataList {
|
|
|
+ dataList = append(dataList, &EdbInfoSearchData{
|
|
|
+ EdbDataId: k + 1,
|
|
|
+ EdbInfoId: v.EdbInfoId,
|
|
|
+ EdbCode: v.EdbCode,
|
|
|
+ DataTime: v.DataTime.Format(utils.FormatDate),
|
|
|
+ Value: v.Value,
|
|
|
+ DataTimestamp: v.DataTimestamp,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
// EdbInfoMaxAndMinInfo 指标最新数据记录结构体
|
|
|
type EdbInfoMaxAndMinInfo struct {
|
|
|
MinDate string `description:"最小日期" bson:"min_date"`
|
|
@@ -906,29 +1086,29 @@ func GetPredictDataListByPredictEdbInfo(edbInfo *EdbInfo, order int, startDate s
|
|
|
|
|
|
// GetPredictDataListByPredictEdbConfList 根据预测指标信息获取预测指标的数据,order:1升序,其余值为降序
|
|
|
func GetPredictDataListByPredictEdbConfList(edbInfo, sourceEdbInfoItem *EdbInfo, predictEdbConfList []*PredictEdbConfAndData, order int, startDate string) (dataList []*EdbInfoSearchData, err error, errMsg string) {
|
|
|
-
|
|
|
allDataList := make([]*EdbInfoSearchData, 0)
|
|
|
//获取指标数据(实际已生成)
|
|
|
- var condition string
|
|
|
- var pars []interface{}
|
|
|
- condition += " AND edb_info_id=? "
|
|
|
- pars = append(pars, sourceEdbInfoItem.EdbInfoId)
|
|
|
- if startDate != "" {
|
|
|
- condition += " AND data_time>=? "
|
|
|
- pars = append(pars, startDate)
|
|
|
- }
|
|
|
- tmpDataList, err := GetEdbDataListAll(condition, pars, sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, 1)
|
|
|
+ tmpDataList, err := GetEdbDataListAll(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, FindEdbDataListAllCond{
|
|
|
+ EdbInfoId: sourceEdbInfoItem.EdbInfoId,
|
|
|
+ StartDataTime: startDate,
|
|
|
+ StartDataTimeCond: ">=",
|
|
|
+ }, 1)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
- // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
|
|
|
+
|
|
|
+ // 默认认为上一个查询是把所有的数据查出来了
|
|
|
+ allDataList = tmpDataList
|
|
|
+
|
|
|
+ // 如果条件中有选择了日期,那么上次查询实际并不是把所有的数据查出来了
|
|
|
+ // 那么需要把日期给过滤掉,然后筛选所有的数据,用于未来指标的生成
|
|
|
if startDate != `` {
|
|
|
- allDataList, err = GetEdbDataListAll(" AND edb_info_id=? ", []interface{}{sourceEdbInfoItem.EdbInfoId}, sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, 1)
|
|
|
+ allDataList, err = GetEdbDataListAll(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, FindEdbDataListAllCond{
|
|
|
+ EdbInfoId: sourceEdbInfoItem.EdbInfoId,
|
|
|
+ }, 1)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
- } else {
|
|
|
- allDataList = tmpDataList
|
|
|
}
|
|
|
|
|
|
// 获取预测指标未来的数据
|
|
@@ -982,11 +1162,9 @@ func GetPredictEdbDataListAll(edbInfo *EdbInfo, order int) (items []*EdbInfoSear
|
|
|
if edbInfo.Source == utils.DATA_SOURCE_PREDICT { //普通的预测指标是没有入库数据的,直接往配置里面获取
|
|
|
items, _, err, _ = GetPredictDataListByPredictEdbInfo(edbInfo, 1, "")
|
|
|
} else {
|
|
|
- var condition string
|
|
|
- var pars []interface{}
|
|
|
- condition += " AND edb_info_id=? "
|
|
|
- pars = append(pars, edbInfo.EdbInfoId)
|
|
|
- items, err = GetEdbDataListAll(condition, pars, edbInfo.Source, edbInfo.SubSource, order)
|
|
|
+ items, err = GetEdbDataListAll(edbInfo.Source, edbInfo.SubSource, FindEdbDataListAllCond{
|
|
|
+ EdbInfoId: edbInfo.EdbInfoId,
|
|
|
+ }, order)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
@@ -996,15 +1174,11 @@ func GetPredictEdbDataListAllByStartDate(edbInfo *EdbInfo, order int, startDate
|
|
|
if edbInfo.Source == utils.DATA_SOURCE_PREDICT { //普通的预测指标是没有入库数据的,直接往配置里面获取
|
|
|
items, _, err, _ = GetPredictDataListByPredictEdbInfo(edbInfo, order, startDate)
|
|
|
} else {
|
|
|
- var condition string
|
|
|
- var pars []interface{}
|
|
|
- condition += " AND edb_info_id=? "
|
|
|
- pars = append(pars, edbInfo.EdbInfoId)
|
|
|
- if startDate != "" {
|
|
|
- condition += " AND data_time>=? "
|
|
|
- pars = append(pars, startDate)
|
|
|
- }
|
|
|
- items, err = GetEdbDataListAll(condition, pars, edbInfo.Source, edbInfo.SubSource, order)
|
|
|
+ items, err = GetEdbDataListAll(edbInfo.Source, edbInfo.SubSource, FindEdbDataListAllCond{
|
|
|
+ EdbInfoId: edbInfo.EdbInfoId,
|
|
|
+ StartDataTime: startDate,
|
|
|
+ StartDataTimeCond: ">=",
|
|
|
+ }, order)
|
|
|
}
|
|
|
return
|
|
|
}
|