62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package common
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
|
|
"91porn-server/common/log"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
// ParseFilter ParseFilter
|
|
func ParseFilter(cond map[string]interface{}) []bson.M {
|
|
ret := []bson.M{}
|
|
for key := range cond {
|
|
if strings.HasSuffix(key, "Start") {
|
|
keyS := strings.TrimSuffix(key, "Start")
|
|
endK := keyS + "End"
|
|
ts, err := time.Parse(time.RFC3339, cond[key].(string))
|
|
if err != nil {
|
|
log.Error("buildFilter解析查询条件出错", log.E(err))
|
|
continue
|
|
}
|
|
te, err := time.Parse(time.RFC3339, cond[endK].(string))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
a := bson.M{
|
|
keyS: bson.M{
|
|
"$gte": ts,
|
|
"$lte": te,
|
|
},
|
|
}
|
|
b := bson.M{
|
|
"$match": a,
|
|
}
|
|
ret = append(ret, b)
|
|
continue
|
|
}
|
|
if strings.HasSuffix(key, "End") {
|
|
continue
|
|
}
|
|
if reflect.TypeOf(cond[key]).Kind() == reflect.Slice {
|
|
mm := bson.M{
|
|
"$match": bson.M{
|
|
key: bson.M{
|
|
"$in": cond[key],
|
|
},
|
|
},
|
|
}
|
|
ret = append(ret, mm)
|
|
continue
|
|
}
|
|
e := bson.M{
|
|
"$match": bson.M{key: cond[key]},
|
|
}
|
|
ret = append(ret, e)
|
|
}
|
|
return ret
|
|
}
|