@@ -0,0 +1,73 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"91porn-server/common/timeutil"
|
||||
"91porn-server/models/commod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// StandQuery 客户端传过来的时间条件信息
|
||||
type StandQuery struct {
|
||||
StartTime *string `form:"startTime" json:"startTime,omitempty" ` // 开始时间
|
||||
EndTime *string `form:"endTime" json:"endTime,omitempty"` // 结束时间
|
||||
TimeString *string `form:"timeString" json:"timeString,omitempty"` // 时间
|
||||
DistrictCode *string `form:"districtCode" json:"districtCode" bson:"districtCode"` // 商区码
|
||||
|
||||
commod.Page
|
||||
}
|
||||
|
||||
// StandQueryMap 通用条件查询管道pipe组装
|
||||
func StandQueryMap(s StandQuery, obj interface{}) (cond bson.M, opts *options.FindOptions) {
|
||||
stdq := commod.StdQuery{
|
||||
Page: &commod.PageBy{Num: s.PageNumber, Size: s.PageSize},
|
||||
}
|
||||
opts = commod.ConvertToListQuery(stdq)
|
||||
opts.SetSort(bson.D{{Key: "_id", Value: -1}})
|
||||
m, _ := JSONStruct2Map(obj)
|
||||
date := make(map[string]interface{})
|
||||
if s.EndTime != nil {
|
||||
date["$lte"] = timeutil.StrTimeToTime(*s.EndTime)
|
||||
}
|
||||
if s.StartTime != nil {
|
||||
date["$gte"] = timeutil.StrTimeToTime(*s.StartTime)
|
||||
}
|
||||
if len(date) > 0 {
|
||||
if s.TimeString != nil {
|
||||
m[*s.TimeString] = date
|
||||
} else {
|
||||
m["createdAt"] = date
|
||||
}
|
||||
}
|
||||
if v, ok := m["_id"]; ok {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
m["_id"], _ = primitive.ObjectIDFromHex(str)
|
||||
}
|
||||
if s.DistrictCode != nil {
|
||||
m["districtCode"] = s.DistrictCode
|
||||
}
|
||||
cond = m
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
// Deduplication 切片去重
|
||||
func Deduplication(src []primitive.ObjectID) []primitive.ObjectID {
|
||||
if len(src) == 0 {
|
||||
return src
|
||||
}
|
||||
dst := []primitive.ObjectID{}
|
||||
m := make(map[primitive.ObjectID]int)
|
||||
for _, s := range src {
|
||||
m[s] = 1
|
||||
}
|
||||
for k := range m {
|
||||
dst = append(dst, k)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
Reference in New Issue
Block a user