Преглед на файлове

Merge branch 'pool/198' into debug

Roc преди 10 месеца
родител
ревизия
67363b2689
променени са 3 файла, в които са добавени 87 реда и са изтрити 17 реда
  1. 7 17
      models/data_manage/edb_data.go
  2. 58 0
      models/mgo/common.go
  3. 22 0
      utils/common.go

+ 7 - 17
models/data_manage/edb_data.go

@@ -252,25 +252,15 @@ func (m *EdbData) getEdbDataListByMongo(source, subSource int, edbCode, startDat
 	queryConditions := bson.M{
 		"edb_code": edbCode,
 	}
-
-	// 数据开始日期
-	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_hub/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

@@ -1165,3 +1165,25 @@ func GetLikeKeyword(keyword string) string {
 func VerifyFrequency(frequency string) bool {
 	return InArrayByStr([]string{"年度", "半年度", "季度", "月度", "旬度", "周度", "日度"}, frequency)
 }
+
+// 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
+}