197 lines
5.4 KiB
Go
Executable File
197 lines
5.4 KiB
Go
Executable File
package signrecordser
|
||
|
||
import (
|
||
"91porn-server/app/appg"
|
||
"91porn-server/common/db"
|
||
"91porn-server/common/log"
|
||
"91porn-server/common/stderr"
|
||
"91porn-server/models/cache/signrecorddata"
|
||
"91porn-server/models/commod"
|
||
"91porn-server/models/v/productmod"
|
||
"91porn-server/models/v/signrecordmod"
|
||
"91porn-server/models/v/txnmod"
|
||
"91porn-server/models/v/walletmod"
|
||
"errors"
|
||
"fmt"
|
||
"math"
|
||
"time"
|
||
|
||
"go.mongodb.org/mongo-driver/mongo/options"
|
||
|
||
"go.mongodb.org/mongo-driver/bson"
|
||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||
)
|
||
|
||
type AppQueryListReq struct {
|
||
commod.Page
|
||
}
|
||
|
||
type AppListRes struct {
|
||
Total int64 `json:"total"`
|
||
HasNext bool `json:"hasNext"`
|
||
List []signrecorddata.AppSimpleData `json:"list"`
|
||
}
|
||
|
||
// GetList 获取列表
|
||
func (p *AppQueryListReq) GetList() AppListRes {
|
||
var res AppListRes
|
||
var err error
|
||
filter := bson.M{}
|
||
|
||
sort := bson.D{{"_id", -1}}
|
||
// 获取列表
|
||
var data []signrecordmod.SignRecord
|
||
data, res.Total, res.HasNext, err = signrecorddata.GetListFromCache(filter, int64(p.Skip()), int64(p.Limit()), sort)
|
||
if err != nil {
|
||
log.Error("获取打卡记录表列表数据错误", log.Any("Params", *p), log.E(err))
|
||
return res
|
||
}
|
||
res.List = signrecorddata.FormatAppSimpleDataList(data)
|
||
|
||
return res
|
||
}
|
||
|
||
type AppQueryInfoReq struct {
|
||
ID string `json:"id" form:"id"` // id
|
||
}
|
||
type AppQueryInfoRes = signrecorddata.AppSimpleData
|
||
|
||
// GetInfo 获取详情
|
||
func (p *AppQueryInfoReq) GetInfo() (res AppQueryInfoRes, err error) {
|
||
oid, err := primitive.ObjectIDFromHex(p.ID)
|
||
if err != nil {
|
||
return res, err
|
||
}
|
||
|
||
var item signrecordmod.SignRecord
|
||
item, err = signrecorddata.GetInfoFromCache(oid)
|
||
if err != nil {
|
||
log.Error("获取打卡记录表详情数据错误", log.Any("ID", p.ID), log.E(err))
|
||
return
|
||
}
|
||
|
||
res = signrecorddata.FormatAppSimpleData(item)
|
||
return
|
||
}
|
||
|
||
type AppReSignReq struct {
|
||
PID string `json:"pid" form:"pid"` // ID
|
||
}
|
||
|
||
// ReSign 补签
|
||
func (param *AppReSignReq) ReSign(uid uint64) (code stderr.Code, err error) {
|
||
var isReturn bool
|
||
pid, _ := primitive.ObjectIDFromHex(param.PID)
|
||
|
||
filter := bson.M{"_id": pid}
|
||
signRecord, err := signrecordmod.GetInfoByCond(filter)
|
||
if err != nil {
|
||
return stderr.Failure, err
|
||
}
|
||
|
||
if signRecord == nil || signRecord.ID.IsZero() {
|
||
return stderr.Failure, errors.New("signRecord is null")
|
||
}
|
||
|
||
if signRecord.CurrentSignDays+signRecord.ForgetSignDays > signRecord.TotalDays {
|
||
return stderr.Failure, errors.New("reSign days is excess days")
|
||
}
|
||
|
||
if signRecord.ForgetSignDays <= 0 {
|
||
return stderr.Failure, errors.New("reSign no forgetSignDays days")
|
||
}
|
||
|
||
product, err := productmod.FindOne(nil, bson.M{"_id": signRecord.PID}, options.FindOne())
|
||
if err != nil {
|
||
return stderr.ErrDbQueryError, err
|
||
}
|
||
if product == nil || product.ID.IsZero() {
|
||
return stderr.Failure, errors.New("product is null")
|
||
}
|
||
|
||
// 修改补签次数
|
||
wallet, err := walletmod.GetWallet(uid)
|
||
if err != nil {
|
||
return stderr.Failure, err
|
||
}
|
||
|
||
if wallet == nil || wallet.ID.IsZero() {
|
||
return stderr.InsufficientBalance, errors.New("account balance is not enough")
|
||
}
|
||
|
||
// 10/20/40/80/160
|
||
var price uint64
|
||
for p := 0; p < int(signRecord.ForgetSignDays+signRecord.RenewalSignDays); p++ {
|
||
if p < int(signRecord.RenewalSignDays) {
|
||
continue
|
||
}
|
||
price += uint64(math.Pow(2, float64(p)) * 10)
|
||
}
|
||
if int64(price) > wallet.Amount {
|
||
return stderr.InsufficientBalance, errors.New("account balance is not enough")
|
||
}
|
||
|
||
if signRecord.CurrentSignDays+signRecord.ForgetSignDays == signRecord.TotalDays {
|
||
isReturn = true
|
||
}
|
||
newRecord := []signrecordmod.Record{}
|
||
cond := bson.M{"_id": signRecord.ID}
|
||
update := bson.M{
|
||
"$inc": bson.M{"renewalSignDays": signRecord.ForgetSignDays, "currentSignDays": signRecord.ForgetSignDays, "forgetSignDays": -signRecord.ForgetSignDays},
|
||
"$set": bson.M{"updateTime": time.Now(), "recordTime": newRecord},
|
||
}
|
||
if isReturn {
|
||
update["$set"] = bson.M{"updateTime": time.Now(), "recordTime": newRecord, "hasReturn": true}
|
||
}
|
||
if err = appg.VideoDB.Trans(func(tool *db.MongoTool) error {
|
||
_, err := signrecordmod.UpdateDataByCond(tool, cond, update)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
// 扣除钱包余额
|
||
wall, err := walletmod.DebitAmount(tool, int64(price), uid)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 补签打卡扣除金币
|
||
txnLogs := []txnmod.TransactionLog{
|
||
{
|
||
TransNo: primitive.NewObjectID(),
|
||
UID: uid,
|
||
Amount: int64(-price),
|
||
ActualAmount: -float64(price),
|
||
TranType: txnmod.ReSignDebitAmount.Key(),
|
||
TranTypeInt: int64(txnmod.ReSignDebitAmount),
|
||
Desc: fmt.Sprintf("%v:%v个", txnmod.ReSignDebitAmount.Key(), int64(price)),
|
||
RealAmount: wall.RealAmount(),
|
||
},
|
||
}
|
||
if isReturn {
|
||
amount := *product.DiscountedPriceIos
|
||
wallAct, err := walletmod.DebitAmount(tool, -amount, uid)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
tl := txnmod.TransactionLog{
|
||
TransNo: primitive.NewObjectID(),
|
||
UID: uid,
|
||
Amount: amount,
|
||
ActualAmount: float64(amount),
|
||
TranType: txnmod.SuccessSignReturnAmount.Key(),
|
||
TranTypeInt: int64(txnmod.SuccessSignReturnAmount),
|
||
Desc: fmt.Sprintf("%v:%v个", txnmod.SuccessSignReturnAmount.Key(), amount),
|
||
RealAmount: wallAct.RealAmount(),
|
||
}
|
||
txnLogs = append(txnLogs, tl)
|
||
}
|
||
return txnmod.InsertManyTransactionLog(tool, txnLogs)
|
||
}); err != nil {
|
||
log.Error(fmt.Sprintf("uid:%v, ReSign Trans err:%v", uid, err))
|
||
return stderr.Failure, err
|
||
}
|
||
|
||
return stderr.Success, nil
|
||
}
|