94 lines
1.8 KiB
Go
94 lines
1.8 KiB
Go
package wdchannmod
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/pageopt"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type WithdrawChannelSlice []WithdrawChannel
|
|
|
|
// 加权随机
|
|
func (w WithdrawChannelSlice) WeightedRandom() WithdrawChannel {
|
|
if len(w) == 0 {
|
|
return WithdrawChannel{}
|
|
}
|
|
var indexs []int
|
|
for i, v := range w {
|
|
for j := 0; j < v.Weight; j++ {
|
|
indexs = append(indexs, i)
|
|
}
|
|
}
|
|
i := rand.Intn(len(indexs))
|
|
if i < 0 {
|
|
return WithdrawChannel{}
|
|
}
|
|
return w[indexs[i]]
|
|
}
|
|
|
|
func (w WithdrawChannelSlice) ToPayTypeList() []string {
|
|
payTypeMap := make(map[string]byte)
|
|
for _, v := range w {
|
|
payTypeMap[v.PayType] = 1
|
|
}
|
|
payTypeList := make([]string, len(payTypeMap))
|
|
i := 0
|
|
for p := range payTypeMap {
|
|
payTypeList[i] = p
|
|
i++
|
|
}
|
|
return payTypeList
|
|
}
|
|
|
|
type Matcher = pageopt.Matcher
|
|
|
|
// ActiveMatch
|
|
type ActiveMatch struct {
|
|
Active *bool
|
|
}
|
|
|
|
func (d *ActiveMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("active", d.Active)
|
|
}
|
|
|
|
// PayTypeMatch
|
|
type PayTypeMatch struct {
|
|
PayType *string
|
|
}
|
|
|
|
func (d *PayTypeMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("payType", d.PayType)
|
|
}
|
|
|
|
type Sort = bson.D
|
|
|
|
func List(sort Sort, skip, limit *int64, matchers ...Matcher) (WithdrawChannelSlice, error) {
|
|
filter := pageopt.MergeM(matchers)
|
|
opt := &options.FindOptions{}
|
|
if len(sort) != 0 {
|
|
opt.SetSort(sort)
|
|
}
|
|
if skip != nil {
|
|
opt.SetSkip(*skip)
|
|
}
|
|
if limit != nil {
|
|
opt.SetLimit(*limit)
|
|
}
|
|
list := WithdrawChannelSlice{}
|
|
if err := coll(nil).Find(&list, filter, opt); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "List", table, "Find", err),
|
|
log.Any("sort", sort),
|
|
log.Any("skip", skip),
|
|
log.Any("limit", limit),
|
|
log.Any("matchers", matchers),
|
|
)
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|