|
@@ -5,9 +5,10 @@ import (
|
|
|
"eta/eta_forum_hub/models/mgodb"
|
|
|
"eta/eta_forum_hub/utils"
|
|
|
"fmt"
|
|
|
- "github.com/shopspring/decimal"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
+
|
|
|
+ "github.com/shopspring/decimal"
|
|
|
)
|
|
|
|
|
|
func BatchAddOrUpdateEdbData(req []*models.AddEdbDataReq) (err error) {
|
|
@@ -28,6 +29,10 @@ func BatchAddOrUpdateEdbData(req []*models.AddEdbDataReq) (err error) {
|
|
|
}
|
|
|
|
|
|
func AddOrUpdateEdbData(edbCode string, dataList []*models.EdbDataBase) (err error) {
|
|
|
+ // 指标数据为空则不更新
|
|
|
+ if len(dataList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
addList := make([]interface{}, 0)
|
|
|
existList, err := mgodb.GetEdbDataBaseByEdbCode(edbCode)
|
|
|
if err != nil {
|
|
@@ -59,8 +64,8 @@ func AddOrUpdateEdbData(edbCode string, dataList []*models.EdbDataBase) (err err
|
|
|
DataTime: dataTime,
|
|
|
Value: val,
|
|
|
Status: v.Status,
|
|
|
- CreateTime: time.Now(),
|
|
|
- ModifyTime: time.Now(),
|
|
|
+ CreateTime: v.CreateTime,
|
|
|
+ ModifyTime: v.ModifyTime,
|
|
|
DataTimestamp: v.DataTimestamp,
|
|
|
}
|
|
|
addList = append(addList, tmp)
|
|
@@ -75,7 +80,7 @@ func AddOrUpdateEdbData(edbCode string, dataList []*models.EdbDataBase) (err err
|
|
|
} else {
|
|
|
delete(deleteMap, v.DataTime)
|
|
|
if val != oldObj.Value {
|
|
|
- err = mgodb.ModifyValueEdbDataValue(oldObj.EdbDataId, val)
|
|
|
+ err = mgodb.ModifyValueEdbDataValue(oldObj.EdbDataId, val, v.ModifyTime)
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("更新指标数据出错 error, %v", err)
|
|
|
return
|
|
@@ -97,7 +102,7 @@ func AddOrUpdateEdbData(edbCode string, dataList []*models.EdbDataBase) (err err
|
|
|
}
|
|
|
removeNum := len(removeDateList)
|
|
|
if removeNum > 0 {
|
|
|
- err = mgodb.DeleteEdbInfoDataByEdbInfoIdAndDate(edbCode, removeDateList)
|
|
|
+ err = mgodb.DeleteEdbInfoDataByEdbCodeAndDateList(edbCode, removeDateList)
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("删除多余日期失败,error, %v", err)
|
|
|
return
|
|
@@ -113,6 +118,10 @@ func AddOrUpdateEdbData(edbCode string, dataList []*models.EdbDataBase) (err err
|
|
|
}
|
|
|
|
|
|
func AddOrUpdateEdbDataCalculate(edbCode string, dataList []*models.EdbDataBase) (err error) {
|
|
|
+ // 指标数据为空则不更新
|
|
|
+ if len(dataList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
addList := make([]interface{}, 0)
|
|
|
existList, err := mgodb.GetEdbDataCalculateByEdbCode(edbCode)
|
|
|
if err != nil {
|
|
@@ -144,8 +153,8 @@ func AddOrUpdateEdbDataCalculate(edbCode string, dataList []*models.EdbDataBase)
|
|
|
DataTime: dataTime,
|
|
|
Value: val,
|
|
|
Status: v.Status,
|
|
|
- CreateTime: time.Now(),
|
|
|
- ModifyTime: time.Now(),
|
|
|
+ CreateTime: v.CreateTime,
|
|
|
+ ModifyTime: v.ModifyTime,
|
|
|
DataTimestamp: v.DataTimestamp,
|
|
|
}
|
|
|
addList = append(addList, tmp)
|
|
@@ -160,7 +169,7 @@ func AddOrUpdateEdbDataCalculate(edbCode string, dataList []*models.EdbDataBase)
|
|
|
} else {
|
|
|
delete(deleteMap, v.DataTime)
|
|
|
if val != oldObj.Value {
|
|
|
- err = mgodb.ModifyValueEdbCalculateDataValue(oldObj.EdbDataId, val)
|
|
|
+ err = mgodb.ModifyValueEdbCalculateDataValue(oldObj.EdbDataId, val, v.ModifyTime)
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("更新指标数据出错 error, %v", err)
|
|
|
return
|
|
@@ -182,7 +191,7 @@ func AddOrUpdateEdbDataCalculate(edbCode string, dataList []*models.EdbDataBase)
|
|
|
}
|
|
|
removeNum := len(removeDateList)
|
|
|
if removeNum > 0 {
|
|
|
- err = mgodb.DeleteEdbCalculateDataByEdbInfoIdAndDate(edbCode, removeDateList)
|
|
|
+ err = mgodb.DeleteEdbCalculateDataByEdbCodeAndDateList(edbCode, removeDateList)
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("删除多余日期失败,error, %v", err)
|
|
|
return
|
|
@@ -361,3 +370,195 @@ func GetEdbDataTbzForSeason(frequency string, tmpDataList []*models.EdbDataList,
|
|
|
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// AddOrUpdateEdbDataWithOpType 根据操作类型,更新指标数据
|
|
|
+func AddOrUpdateEdbDataWithOpType(edbInfoId int, dataList []*models.EdbDataBaseWithOpType, minDataTime string, maxDataTime string) (err error) {
|
|
|
+ // 指标数据为空则不更新
|
|
|
+ if len(dataList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ addList := make([]interface{}, 0)
|
|
|
+ minDataTimeT, err := time.ParseInLocation(utils.FormatDate, minDataTime, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("日期格式错误 error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ maxDataTimeT, err := time.ParseInLocation(utils.FormatDate, maxDataTime, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("日期格式错误 error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ existList, err := mgodb.GetEdbDataList(edbInfoId, minDataTimeT, maxDataTimeT)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("查询指标数据出错 error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ existMap := make(map[string]*mgodb.EdbDataBase, len(existList))
|
|
|
+ removeDateList := make([]time.Time, 0)
|
|
|
+ for _, exist := range existList {
|
|
|
+ tmp := exist.DataTime.Format(utils.FormatDate)
|
|
|
+ existMap[tmp] = exist
|
|
|
+ }
|
|
|
+ for _, v := range dataList {
|
|
|
+ dataTime, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("时间格式化出错 error, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ val, e := strconv.ParseFloat(v.Value, 64)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("指标数据格式化出错 error, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if oldObj, ok := existMap[v.DataTime]; !ok {
|
|
|
+ if v.OpType == "insert" || v.OpType == "update" {
|
|
|
+ tmp := &mgodb.EdbDataBase{
|
|
|
+ EdbInfoId: v.EdbInfoId,
|
|
|
+ EdbCode: v.EdbCode,
|
|
|
+ DataTime: dataTime,
|
|
|
+ Value: val,
|
|
|
+ Status: v.Status,
|
|
|
+ CreateTime: v.CreateTime,
|
|
|
+ ModifyTime: v.ModifyTime,
|
|
|
+ DataTimestamp: v.DataTimestamp,
|
|
|
+ }
|
|
|
+ addList = append(addList, tmp)
|
|
|
+ if len(addList) >500 {
|
|
|
+ err = mgodb.InsertEdbDataBatch(addList)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("批量新增指标数据失败 error, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ addList = make([]interface{}, 0)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if v.OpType != "delete" {
|
|
|
+ if v.ModifyTime.After(oldObj.ModifyTime) {
|
|
|
+ err = mgodb.ModifyValueEdbDataValue(oldObj.EdbDataId, val, v.ModifyTime)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("更新指标数据出错 error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else if v.OpType == "delete" {
|
|
|
+ removeDateList = append(removeDateList, dataTime)
|
|
|
+ // 删除数据
|
|
|
+ // err = mgodb.DeleteEdbInfoDataByEdbInfoIdAndDate(v.EdbCode, v.DataTime)
|
|
|
+ // if err != nil {
|
|
|
+ // err = fmt.Errorf("删除指标数据出错 error, %v", err)
|
|
|
+ // return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //遍历deletemap,找出需要删除的日期
|
|
|
+
|
|
|
+ removeNum := len(removeDateList)
|
|
|
+ if removeNum > 0 {
|
|
|
+ err = mgodb.DeleteEdbInfoDataByEdbInfoIdAndDateList(edbInfoId, removeDateList)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("删除多余日期失败,error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if len(addList) > 0 {
|
|
|
+ err = mgodb.InsertEdbDataBatch(addList)
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func AddOrUpdateEdbDataCalculateWithOpType(edbInfoId int, dataList []*models.EdbDataBaseWithOpType, minDataTime string, maxDataTime string) (err error) {
|
|
|
+ // 指标数据为空则不更新
|
|
|
+ if len(dataList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ addList := make([]interface{}, 0)
|
|
|
+ minDataTimeT, err := time.ParseInLocation(utils.FormatDate, minDataTime, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("日期格式错误 error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ maxDataTimeT, err := time.ParseInLocation(utils.FormatDate, maxDataTime, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("日期格式错误 error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ existList, err := mgodb.GetEdbCalculateDataList(edbInfoId, minDataTimeT, maxDataTimeT)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("查询指标数据出错 error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ existMap := make(map[string]*mgodb.EdbDataBase, len(existList))
|
|
|
+ removeDateList := make([]time.Time, 0)
|
|
|
+ for _, exist := range existList {
|
|
|
+ tmp := exist.DataTime.Format(utils.FormatDate)
|
|
|
+ existMap[tmp] = exist
|
|
|
+ }
|
|
|
+ for _, v := range dataList {
|
|
|
+ dataTime, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("时间格式化出错 error, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ val, e := strconv.ParseFloat(v.Value, 64)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("指标数据格式化出错 error, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if oldObj, ok := existMap[v.DataTime]; !ok {
|
|
|
+ if v.OpType == "insert" || v.OpType == "update" {
|
|
|
+ tmp := &mgodb.EdbDataBase{
|
|
|
+ EdbInfoId: v.EdbInfoId,
|
|
|
+ EdbCode: v.EdbCode,
|
|
|
+ DataTime: dataTime,
|
|
|
+ Value: val,
|
|
|
+ Status: v.Status,
|
|
|
+ CreateTime: v.CreateTime,
|
|
|
+ ModifyTime: v.ModifyTime,
|
|
|
+ DataTimestamp: v.DataTimestamp,
|
|
|
+ }
|
|
|
+ addList = append(addList, tmp)
|
|
|
+ if len(addList) >500 {
|
|
|
+ err = mgodb.InsertEdbCalculateDataBatch(addList)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("批量新增指标数据失败 error, %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ addList = make([]interface{}, 0)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if v.OpType != "delete" {
|
|
|
+ if v.ModifyTime.After(oldObj.ModifyTime) {
|
|
|
+ err = mgodb.ModifyValueEdbCalculateDataValue(oldObj.EdbDataId, val, v.ModifyTime)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("更新指标数据出错 error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if v.OpType == "delete" {
|
|
|
+ removeDateList = append(removeDateList, dataTime)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //遍历deleteMap,找出需要删除的日期
|
|
|
+ removeNum := len(removeDateList)
|
|
|
+ if removeNum > 0 {
|
|
|
+ err = mgodb.DeleteEdbCalculateDataByEdbInfoIdAndDateList(edbInfoId, removeDateList)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("删除多余日期失败,error, %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(addList) > 0 {
|
|
|
+ err = mgodb.InsertEdbCalculateDataBatch(addList)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|