common.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package mgo
  2. import (
  3. "eta_gn/eta_index_lib/utils"
  4. "go.mongodb.org/mongo-driver/bson"
  5. "time"
  6. )
  7. // BuildDateCondition
  8. // @Description: 构建日期查询条件
  9. // @author: Roc
  10. // @datetime 2024-06-03 09:41:19
  11. // @param start string
  12. // @param end string
  13. // @return condition bson.M
  14. // @return err error
  15. func BuildDateCondition(start, end string) (condition bson.M, err error) {
  16. var startDateTime, endDateTime time.Time
  17. if start != "" {
  18. // 使用开始日期条件
  19. startDateTime, err = time.ParseInLocation(utils.FormatDate, start, time.Local)
  20. if err != nil {
  21. return
  22. }
  23. }
  24. if end != "" {
  25. // 使用结束日期条件
  26. endDateTime, err = time.ParseInLocation(utils.FormatDate, end, time.Local)
  27. if err != nil {
  28. return
  29. }
  30. }
  31. return BuildDateTimeCondition(startDateTime, endDateTime)
  32. }
  33. // BuildDateTimeCondition
  34. // @Description: 构建日期查询条件
  35. // @author: Roc
  36. // @datetime 2024-06-03 09:47:32
  37. // @param startDateTime time.Time
  38. // @param endDateTime time.Time
  39. // @return condition bson.M
  40. // @return err error
  41. func BuildDateTimeCondition(startDateTime, endDateTime time.Time) (condition bson.M, err error) {
  42. if !startDateTime.IsZero() && !endDateTime.IsZero() {
  43. condition = bson.M{utils.DateConvMysqlConvMongo(">="): startDateTime, utils.DateConvMysqlConvMongo("<="): endDateTime}
  44. } else if !startDateTime.IsZero() {
  45. cond := utils.DateConvMysqlConvMongo(">=")
  46. condition = bson.M{cond: startDateTime}
  47. } else if !endDateTime.IsZero() {
  48. cond := utils.DateConvMysqlConvMongo("<=")
  49. condition = bson.M{cond: endDateTime}
  50. }
  51. return
  52. }