Browse Source

Merge remote-tracking branch 'origin/custom'

Roc 9 months ago
parent
commit
ad148906da
3 changed files with 87 additions and 16 deletions
  1. 7 16
      models/chart.go
  2. 58 0
      models/mgo/common.go
  3. 22 0
      utils/common.go

+ 7 - 16
models/chart.go

@@ -209,24 +209,15 @@ func getEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate
 	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 := mgo.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 {

+ 58 - 0
models/mgo/common.go

@@ -0,0 +1,58 @@
+package mgo
+
+import (
+	"eta/eta_chart_lib/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
+}

+ 22 - 0
utils/common.go

@@ -1142,3 +1142,25 @@ func GetFrequencyEn(frequency string) (frequencyEn string) {
 	}
 	return
 }
+
+// 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
+}