package mgo 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 }