@@ -0,0 +1,190 @@
|
||||
package rchgchanmod
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/app/appg"
|
||||
"91porn-server/common/constant/redisconst"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/redis"
|
||||
"91porn-server/models"
|
||||
"91porn-server/web/webg"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
const table = models.PayChannel
|
||||
|
||||
var rchgChansCach = "rchgChansCach:%s"
|
||||
|
||||
// initIndex 设置index
|
||||
func initIndex() {
|
||||
coll := coll(nil)
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "cid", Value: 1}, {Key: "payType", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "active", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "updatedAt", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InsertOne 插入一条数据
|
||||
func InsertPayChannel(p *PayChannel) error {
|
||||
defer redisCachDel(rchgChansCach)
|
||||
if _, err := coll(nil).InsertOne(p); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertPayChannel", table, "InsertOne", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除数据
|
||||
func DeletePayChannel(ids []string) (int64, error) {
|
||||
defer redisCachDel(rchgChansCach)
|
||||
objIDs := make([]primitive.ObjectID, len(ids))
|
||||
for i, v := range ids {
|
||||
objIDs[i], _ = primitive.ObjectIDFromHex(v)
|
||||
}
|
||||
result, err := coll(nil).DeleteMany(bson.M{"_id": bson.M{"$in": objIDs}})
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeletePayChannel", table, "DeleteMany", err))
|
||||
return 0, err
|
||||
}
|
||||
return result.DeletedCount, err
|
||||
}
|
||||
|
||||
// UpdateAmountTypesByCID
|
||||
func UpdateAmountTypesByCID(cid string, amountTypes []int64) (int64, error) {
|
||||
defer redisCachDel(rchgChansCach)
|
||||
result, err := coll(nil).UpdateOne(bson.M{"cid": cid}, bson.M{"$set": bson.M{"amountTypes": amountTypes, "updatedAt": time.Now()}})
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdatePayChannelBaseOnCID", table, "UpdateOne", err),
|
||||
log.Any("cid", cid),
|
||||
log.Any("amountTypes", amountTypes),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
|
||||
// Update
|
||||
func UpdatePayChannel(id string, set EditInfo) (int64, error) {
|
||||
defer redisCachDel(rchgChansCach)
|
||||
set.UpdatedAt = time.Now()
|
||||
objID, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdatePayChannel", table, "ObjectIDFromHex", err),
|
||||
log.Any("id", id),
|
||||
log.Any("set", set),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
result, err := coll(nil).UpdateOne(bson.M{"_id": objID}, bson.M{"$set": set})
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdatePayChannel", table, "UpdateOne", err),
|
||||
log.Any("id", id),
|
||||
log.Any("set", set),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
|
||||
// GetPayChannels 条件获取支付渠道列表
|
||||
func GetPayChannels() ([]*PayChannel, error) {
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.D{{Key: "active", Value: -1}, {Key: "updatedAt", Value: -1}})
|
||||
var back []*PayChannel
|
||||
if err := coll(nil).Find(&back, bson.M{}, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetPayChannels", table, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
|
||||
// GetPayChannelsByMoney 条件获取支付渠道列表
|
||||
func GetPayChannelsByMoney(money int64, payType string) ([]*PayChannel, error) {
|
||||
var back []*PayChannel
|
||||
if err := coll(nil).Aggregate(&back, []bson.M{
|
||||
{"$match": bson.M{"payType": payType}},
|
||||
{"$match": bson.M{"maxMoney": bson.M{"$gte": money}}},
|
||||
{"$match": bson.M{"minMoney": bson.M{"$lte": money}}},
|
||||
{"$match": bson.M{"active": true}},
|
||||
}); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetPayChannelsByMoney", table, "Aggregate", err))
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
|
||||
// GetPayChannelsByMoney 条件获取支付渠道列表
|
||||
func GetActiveTruePayChannels(mode string) ([]PayChannel, error) {
|
||||
var back []PayChannel
|
||||
redisKey := redisconst.DataCachKey(table, fmt.Sprintf(rchgChansCach, mode))
|
||||
redisc := getRedis()
|
||||
if redisc == nil || !redisc.Exists(redisKey) {
|
||||
modeSet := getModeSet(mode)
|
||||
if err := coll(nil).Find(&back, bson.M{"active": true, "model_sup": bson.M{"$bitsAnySet": modeSet}}); err != nil {
|
||||
log.ZapLog.Warn("recharge GetPayChannel Find fail", log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
if redisc == nil {
|
||||
return back, nil
|
||||
}
|
||||
jsonBytes, err := json.Marshal(back)
|
||||
if err != nil {
|
||||
return back, err
|
||||
}
|
||||
_ = redisc.Set(redisKey, string(jsonBytes), redisconst.DataCachExpire)
|
||||
return back, nil
|
||||
}
|
||||
str, err := redisc.Get(redisKey)
|
||||
if err != nil {
|
||||
return back, err
|
||||
}
|
||||
if str == nil {
|
||||
return back, errors.New("redis key is null")
|
||||
}
|
||||
return back, json.Unmarshal([]byte(*str), &back)
|
||||
}
|
||||
|
||||
func redisCachDel(m string) {
|
||||
redisKey := redisconst.DataCachKey(table, m)
|
||||
_, _ = getRedis().Del(redisKey)
|
||||
}
|
||||
|
||||
func getRedis() *redis.Client {
|
||||
if appg.Redis != nil {
|
||||
return appg.Redis
|
||||
}
|
||||
if webg.Redis != nil {
|
||||
return webg.Redis
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user