Browse Source

fix:修复mongo时间区间查询的bug

Roc 9 months ago
parent
commit
7acf2ede28
5 changed files with 112 additions and 48 deletions
  1. 15 33
      models/data_manage/chart_info.go
  2. 9 8
      models/data_manage/edb_info.go
  3. 8 7
      services/data/edb_data.go
  4. 22 0
      utils/common.go
  5. 58 0
      utils/mgodb/common.go

+ 15 - 33
models/data_manage/chart_info.go

@@ -4,6 +4,7 @@ import (
 	"errors"
 	"eta/eta_api/models/mgo"
 	"eta/eta_api/utils"
+	"eta/eta_api/utils/mgodb"
 	"fmt"
 	"github.com/beego/beego/v2/client/orm"
 	"github.com/rdlucklib/rdluck_tools/paging"
@@ -459,24 +460,15 @@ func getEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate
 		"edb_info_id": edbInfoId,
 	}
 
-	// 数据开始日期
-	if startDate != `` {
-		startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
-		if tmpErr != nil {
-			err = tmpErr
-			return
-		}
-		queryConditions["data_time"] = bson.M{"$gte": startDateTime}
+	// 数据日期
+	dateCondition, err := mgodb.BuildDateCondition(startDate, endDate)
+	if err != nil {
+		return
 	}
-	// 数据结束日期
-	if endDate != "" {
-		endDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
-		if tmpErr != nil {
-			err = tmpErr
-			return
-		}
-		queryConditions["data_time"] = bson.M{"$lte": endDateTime}
+	if len(dateCondition) > 0 {
+		queryConditions["data_time"] = dateCondition
 	}
+
 	// 获取列表数据
 	tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
 	if tmpErr != nil {
@@ -591,25 +583,15 @@ func getEdbDataListMinAndMaxByMongo(source, subSource, edbInfoId int, startDate,
 	queryConditions := bson.M{
 		"edb_info_id": edbInfoId,
 	}
-
-	// 数据开始日期
-	if startDate != `` {
-		startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
-		if tmpErr != nil {
-			err = tmpErr
-			return
-		}
-		queryConditions["data_time"] = bson.M{"$gte": startDateTime}
+	// 日期
+	dateCondition, err := mgodb.BuildDateCondition(startDate, endDate)
+	if err != nil {
+		return
 	}
-	// 数据结束日期
-	if endDate != "" {
-		endDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
-		if tmpErr != nil {
-			err = tmpErr
-			return
-		}
-		queryConditions["data_time"] = bson.M{"$lte": endDateTime}
+	if len(dateCondition) > 0 {
+		queryConditions["data_time"] = dateCondition
 	}
+
 	pipeline := []bson.M{
 		{"$match": queryConditions},
 		{"$group": bson.M{

+ 9 - 8
models/data_manage/edb_info.go

@@ -4,6 +4,7 @@ import (
 	"eta/eta_api/models/mgo"
 	"eta/eta_api/services/alarm_msg"
 	"eta/eta_api/utils"
+	"eta/eta_api/utils/mgodb"
 	"fmt"
 	"github.com/beego/beego/v2/client/orm"
 	"github.com/rdlucklib/rdluck_tools/paging"
@@ -1624,14 +1625,14 @@ func getAllDataByMongo(edbInfoId, source, subSource int, startDataTime string) (
 	queryConditions := bson.M{
 		"edb_info_id": edbInfoId,
 	}
-	// 结束日期
-	if startDataTime != "" {
-		startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDataTime, time.Local)
-		if tmpErr != nil {
-			err = tmpErr
-			return
-		}
-		queryConditions["data_time"] = bson.M{"$gt": startDateTime}
+
+	// 开始日期
+	dateCondition, err := mgodb.BuildDateCondition(startDataTime, "")
+	if err != nil {
+		return
+	}
+	if len(dateCondition) > 0 {
+		queryConditions["data_time"] = dateCondition
 	}
 
 	// 获取列表数据

+ 8 - 7
services/data/edb_data.go

@@ -6,6 +6,7 @@ import (
 	"eta/eta_api/models/data_manage"
 	"eta/eta_api/models/mgo"
 	"eta/eta_api/utils"
+	"eta/eta_api/utils/mgodb"
 	"fmt"
 	"github.com/shopspring/decimal"
 	"go.mongodb.org/mongo-driver/bson"
@@ -480,14 +481,14 @@ func getPageDataByMongo(edbInfoId, source, subSource int, endDataTime string, st
 	queryConditions := bson.M{
 		"edb_info_id": edbInfoId,
 	}
+
 	// 结束日期
-	if endDataTime != "" {
-		endDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, endDataTime, time.Local)
-		if tmpErr != nil {
-			err = tmpErr
-			return
-		}
-		queryConditions["data_time"] = bson.M{"$lte": endDateTime}
+	dateCondition, err := mgodb.BuildDateCondition("", endDataTime)
+	if err != nil {
+		return
+	}
+	if len(dateCondition) > 0 {
+		queryConditions["data_time"] = dateCondition
 	}
 
 	// 获取数据总量

+ 22 - 0
utils/common.go

@@ -2446,3 +2446,25 @@ func MillisecondsToHHMMSS(ms int) string {
 func ByteToMB(byteCount int) float64 {
 	return float64(byteCount) / (1024 * 1024)
 }
+
+// DateConvMysqlConvMongo
+// @Description: 将mysql中的日期比较符转换成mongo中的日期比较符
+// @author: Roc
+// @datetime 2024-05-08 11:03:26
+// @param dateCon string
+func DateConvMysqlConvMongo(dateCon string) string {
+	cond := ""
+	switch dateCon {
+	case "=":
+		cond = "$eq"
+	case "<":
+		cond = "$lt"
+	case "<=":
+		cond = "$lte"
+	case ">":
+		cond = "$gt"
+	case ">=":
+		cond = "$gte"
+	}
+	return cond
+}

+ 58 - 0
utils/mgodb/common.go

@@ -0,0 +1,58 @@
+package mgodb
+
+import (
+	"eta/eta_api/utils"
+	"go.mongodb.org/mongo-driver/bson"
+	"time"
+)
+
+// BuildDateCondition
+// @Description:  构建日期查询条件
+// @author: Roc
+// @datetime 2024-06-03 09:41:19
+// @param start string
+// @param end string
+// @return condition bson.M
+// @return err error
+func BuildDateCondition(start, end string) (condition bson.M, err error) {
+	var startDateTime, endDateTime time.Time
+	if start != "" {
+		// 使用开始日期条件
+		startDateTime, err = time.ParseInLocation(utils.FormatDate, start, time.Local)
+		if err != nil {
+			return
+		}
+	}
+	if end != "" {
+		// 使用结束日期条件
+		endDateTime, err = time.ParseInLocation(utils.FormatDate, end, time.Local)
+		if err != nil {
+			return
+		}
+	}
+
+	return BuildDateTimeCondition(startDateTime, endDateTime)
+
+}
+
+// BuildDateTimeCondition
+// @Description: 构建日期查询条件
+// @author: Roc
+// @datetime 2024-06-03 09:47:32
+// @param startDateTime time.Time
+// @param endDateTime time.Time
+// @return condition bson.M
+// @return err error
+func BuildDateTimeCondition(startDateTime, endDateTime time.Time) (condition bson.M, err error) {
+	if !startDateTime.IsZero() && !endDateTime.IsZero() {
+		condition = bson.M{utils.DateConvMysqlConvMongo(">="): startDateTime, utils.DateConvMysqlConvMongo("<="): endDateTime}
+	} else if !startDateTime.IsZero() {
+		cond := utils.DateConvMysqlConvMongo(">=")
+		condition = bson.M{cond: startDateTime}
+	} else if !endDateTime.IsZero() {
+		cond := utils.DateConvMysqlConvMongo("<=")
+		condition = bson.M{cond: endDateTime}
+	}
+
+	return
+}