185 lines
5.4 KiB
Go
185 lines
5.4 KiB
Go
package proxyincomemod
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/timeutil"
|
|
"91porn-server/models"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
const table = models.ProxyIncomeLog
|
|
|
|
// IncomeLog 推广收益记录表
|
|
type StandReq struct {
|
|
UID uint64 `form:"uid" json:"uid,omitempty" bson:"uid"` // 用户UID
|
|
}
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
// initIndex 索引设置
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "originUID", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
// InsertIncomeLog 插入记录
|
|
func InsertIncomeLog(t *db.MongoTool, p *ProxyIncomeLog) error {
|
|
p.CreatedAt = time.Now()
|
|
if _, err := coll(nil).InsertOne(p); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertIncomeLog", table, "InsertOne", err), log.Any("p", p))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FindIncomeOfMonth 查询每月收益
|
|
func FindIncomeOfMonth(uid uint64) (totalMoney int64, totalPerfomance int64, err error) {
|
|
firstDay, lastDay := timeutil.MonthStartEndTime(time.Now())
|
|
pipelines := []bson.M{
|
|
{"$match": bson.M{"uid": uid, "createdAt": bson.M{"$gte": firstDay, "$lt": lastDay}}},
|
|
{"$group": bson.M{"_id": nil, "totalMoney": bson.M{"$sum": "$money"}, "totalPerformance": bson.M{"$sum": "$performance"}}},
|
|
{"$project": bson.M{"totalMoney": 1, "totalPerformance": 1}},
|
|
}
|
|
type res struct {
|
|
TotalMoney int64 `json:"totalMoney"`
|
|
TotalPerformance int64 `json:"totalPerformance"`
|
|
}
|
|
data := make([]res, 0)
|
|
if err = coll(nil).Aggregate(&data, pipelines); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindIncomeOfMonth", table, "Aggregate", err), log.Any("uid", uid))
|
|
return
|
|
}
|
|
if len(data) > 0 {
|
|
totalMoney = data[0].TotalMoney
|
|
totalPerfomance = data[0].TotalPerformance
|
|
}
|
|
return
|
|
}
|
|
|
|
// FindIncomeLogs 查询收益详情
|
|
func FindIncomeLogs(uid uint64, pageNumber int, pageSize int) (total int64, incomeLogs []*ProxyIncomeLog, hasNext bool, err error) {
|
|
incomeLogs = make([]*ProxyIncomeLog, 0)
|
|
total, err = coll(nil).Count(bson.M{"uid": uid})
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindIncomeLogs", table, "Count", err), log.Any("uid", uid))
|
|
return
|
|
}
|
|
skip := int64((pageNumber - 1) * pageSize)
|
|
limit := int64(pageSize + 1)
|
|
opts := options.FindOptions{
|
|
Skip: &skip,
|
|
Limit: &limit,
|
|
Sort: bson.D{{Key: "createdAt", Value: -1}},
|
|
}
|
|
if err = coll(nil).Find(&incomeLogs, bson.M{"uid": uid}, &opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindIncomeLogs", table, "Aggregate", err), log.Any("uid", uid))
|
|
return
|
|
}
|
|
if len(incomeLogs) > pageSize {
|
|
hasNext = true
|
|
incomeLogs = incomeLogs[:pageSize]
|
|
}
|
|
return
|
|
}
|
|
|
|
// FindIncomeLogs 查询收益详情
|
|
func FindList(cond bson.M, opts *options.FindOptions) (total int64, incomeLogs []*ProxyIncomeLog, err error) {
|
|
incomeLogs = make([]*ProxyIncomeLog, 0)
|
|
total, err = coll(nil).Count(cond)
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindList", table, "Count", err), log.Any("cond", cond))
|
|
return
|
|
}
|
|
if err = coll(nil).Find(&incomeLogs, cond, opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindList", table, "Find", err), log.Any("cond", cond))
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetTotalIncome 计算总收益
|
|
func GetTotalIncome(uid uint64, begin *string, end *string) (int64, error) {
|
|
cond := bson.M{"uid": uid}
|
|
if begin != nil && end != nil {
|
|
cond["createdAt"] = bson.M{"$gte": timeutil.StrTimeToTime(*begin), "$lte": timeutil.StrTimeToTime(*end)}
|
|
}
|
|
pipelines := []bson.M{
|
|
{"$match": cond},
|
|
{"$group": bson.M{"_id": nil, "totalMoney": bson.M{"$sum": "$money"}}},
|
|
}
|
|
type I struct {
|
|
TotalMoney int64 `json:"totalMoney" bson:"totalMoney"`
|
|
}
|
|
res := []I{}
|
|
if err := coll(nil).Aggregate(&res, pipelines); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetTotalIncome", table, "Aggregate", err),
|
|
log.Any("uid", uid),
|
|
log.Any("begin", begin),
|
|
log.Any("end", end),
|
|
)
|
|
return 0, err
|
|
}
|
|
var tMoney int64 = 0
|
|
if len(res) > 0 {
|
|
tMoney = res[0].TotalMoney
|
|
}
|
|
return tMoney, nil
|
|
}
|
|
|
|
// FindAllIncome 查询总收益
|
|
func FindAllIncome(uid uint64) (lv1 int64, lv2 int64, lv3 int64, lv4 int64, total int64, err error) {
|
|
pipelines := []bson.M{
|
|
{"$match": bson.M{"uid": uid}},
|
|
{"$group": bson.M{"_id": "$agentLevel", "totalMoney": bson.M{"$sum": "$money"}}},
|
|
}
|
|
type I struct {
|
|
ID int64 `json:"id" bson:"_id"`
|
|
TotalMoney int64 `json:"totalMoney"`
|
|
}
|
|
res := make([]I, 0)
|
|
if err = coll(nil).Aggregate(&res, pipelines); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindAllIncome", table, "Aggregate", err), log.Any("uid", uid))
|
|
return
|
|
}
|
|
for _, v := range res {
|
|
switch v.ID {
|
|
case 1:
|
|
lv1 = v.TotalMoney
|
|
total += lv1
|
|
case 2:
|
|
lv2 = v.TotalMoney
|
|
total += lv2
|
|
case 3:
|
|
lv3 = v.TotalMoney
|
|
total += lv3
|
|
case 4:
|
|
lv4 = v.TotalMoney
|
|
total += lv4
|
|
}
|
|
}
|
|
return
|
|
}
|