Commit 17c07ff1 authored by hujiebin's avatar hujiebin

匹配条件扣费-粉钻

parent e4dcd5a7
......@@ -4,6 +4,7 @@ import (
"fmt"
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/internal/enum/diamond_e"
"git.hilo.cn/hilo-common/myerr"
"git.hilo.cn/hilo-common/resource/mysql"
"strconv"
"time"
......@@ -35,6 +36,24 @@ type DiamondAccountDetail struct {
diamondAccount *DiamondAccount `gorm:"-"`
}
// 粉钻详情
type DiamondPinkAccountDetail struct {
mysql.Entity
*domain.Model `gorm:"-"`
UserId mysql.ID
DiamondAccountId mysql.ID
OperateId mysql.ID
OperateType diamond_e.OperateType
OriginId mysql.ID
AddReduce mysql.AddReduce
Num mysql.Num
Remark mysql.Str
BefNum mysql.Num
AftNum mysql.Num
OperateIds mysql.Str
diamondAccount *DiamondAccount `gorm:"-"`
}
//账号操作配置
type DiamondOperateSet struct {
mysql.Entity
......@@ -154,3 +173,94 @@ func (diamondAccount *DiamondAccount) addDiamondAccountDetail(operateType diamon
func (diamondAccount *DiamondAccount) AddDiamondAccountDetail(operateType diamond_e.OperateType, refId mysql.ID, diamondNum mysql.Num) (*DiamondAccountDetail, error) {
return diamondAccount.addDiamondAccountDetail(operateType, refId, diamondNum)
}
// 匹配条件扣费-粉钻
func (diamondAccount *DiamondAccount) ChangePinkDiamondAccountDetail(operateType diamond_e.OperateType, originId mysql.ID, diamondNum mysql.Num, operateIds string) (*DiamondPinkAccountDetail, error) {
return diamondAccount.addPinkDiamondAccountDetail(operateType, originId, diamondNum, operateIds)
}
// 粉钻钻石操作记录
func (diamondAccount *DiamondAccount) addPinkDiamondAccountDetail(operateType diamond_e.OperateType, originId mysql.ID,
diamondNum mysql.Num, operateIds string) (*DiamondPinkAccountDetail, error) {
var diamondOperateSet DiamondOperateSet
var err error
if err = diamondAccount.Db.Where(&DiamondOperateSet{
Type: operateType,
Status: mysql.USER,
DiamondType: diamond_e.DiamondPink,
}).First(&diamondOperateSet).Error; err != nil {
return nil, myerr.WrapErr(err)
}
//判断是增加,账号是否被冻结
if diamondAccount.Status == diamond_e.Frozen && diamondOperateSet.AddReduce == mysql.REDUCE {
return nil, fmt.Errorf("bizerr.DiamondAccountFrozen")
}
//无限,检查次数
var count int64
if diamondOperateSet.FrequencyDay == -1 {
if diamondOperateSet.FrequencyNum != -1 {
diamondAccount.Db.Model(&DiamondPinkAccountDetail{}).Where(&DiamondPinkAccountDetail{
UserId: diamondAccount.UserId,
OperateType: operateType,
}).Count(&count)
if count >= int64(diamondOperateSet.FrequencyNum) {
return nil, fmt.Errorf("bizerr.DiamondFrequency")
//return nil, myerr.NewSysError("钻石操作次数多大, userId:" + mysql.IdToStr(diamondAccount.UserId) + " diamondOperateSetId" + mysql.IdToStr(diamondOperateSet.ID))
}
}
} else if diamondOperateSet.FrequencyDay == 1 {
beginTime, err := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Local)
if err != nil {
return nil, myerr.WrapErr(err)
}
//一天的次数
diamondAccount.Db.Model(&DiamondPinkAccountDetail{}).Where(&DiamondPinkAccountDetail{
UserId: diamondAccount.UserId,
OperateType: operateType,
}).Where("created_time >= ? ", beginTime).Count(&count)
if count >= int64(diamondOperateSet.FrequencyNum) {
return nil, fmt.Errorf("bizerr.DiamondFrequency")
}
//终极拦截,利用
diamondAccount.SetCheckUpdateCondition(" EXISTS (SELECT * from (SELECT COUNT(1) as n from diamond_pink_account_detail d where d.user_id = " + strconv.FormatUint(diamondAccount.UserId, 10) + " and d.operate_type = " + strconv.FormatUint(uint64(operateType), 10) + " and d.created_time >= from_unixtime(" + strconv.FormatInt(getZeroTime(time.Now()).Unix(), 10) + ")) t where t.n < " + strconv.Itoa(diamondOperateSet.FrequencyNum) + " )")
}
//-1,代表值无效,由参数给与
var upateDiamondNum mysql.Num
if diamondOperateSet.DiamondNum == -1 {
upateDiamondNum = diamondNum
} else {
upateDiamondNum = mysql.Num(diamondOperateSet.DiamondNum)
}
var afterNum mysql.Num
if diamondOperateSet.AddReduce == mysql.ADD {
afterNum = diamondAccount.PinkDiamondNum + upateDiamondNum
} else if diamondOperateSet.AddReduce == mysql.REDUCE {
if diamondAccount.PinkDiamondNum < upateDiamondNum {
return nil, fmt.Errorf("bizerr.DiamondNoEnough")
}
afterNum = diamondAccount.PinkDiamondNum - upateDiamondNum
} else {
return nil, myerr.NewSysError("AddReduce 值错误:" + mysql.TypeToString(diamondOperateSet.AddReduce))
}
diamondPinkAccountDetail := &DiamondPinkAccountDetail{
Model: diamondAccount.Model,
UserId: diamondAccount.UserId,
DiamondAccountId: diamondAccount.ID,
OperateId: diamondOperateSet.ID,
OperateType: diamondOperateSet.Type,
OriginId: originId,
AddReduce: diamondOperateSet.AddReduce,
Num: upateDiamondNum,
Remark: diamondOperateSet.Name,
BefNum: diamondAccount.PinkDiamondNum,
AftNum: afterNum,
diamondAccount: diamondAccount,
OperateIds: operateIds,
}
return diamondPinkAccountDetail, err
}
......@@ -3,6 +3,7 @@ package diamond_m
import (
"fmt"
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/myerr"
"git.hilo.cn/hilo-common/mylogrus"
"git.hilo.cn/hilo-common/resource/mysql"
"gorm.io/gorm"
......@@ -50,3 +51,44 @@ func (diamondAccountDetail *DiamondAccountDetail) Persistent() error {
}
return nil
}
func (p *DiamondPinkAccountDetail) Persistent() error {
txDiamondAccount := p.Db.Model(p.diamondAccount)
if p.diamondAccount.CheckUpdateCondition() {
txDiamondAccount = txDiamondAccount.Where(p.diamondAccount.GetUpdateCondition())
}
if p.AddReduce == mysql.ADD {
//增加
txDiamondAccount.UpdateColumn("pink_diamond_num", gorm.Expr("pink_diamond_num + ?", p.Num))
} else if p.AddReduce == mysql.REDUCE {
//减少,保证不能扣成负数
txDiamondAccount.Where("pink_diamond_num >= ?", p.Num).UpdateColumn("pink_diamond_num", gorm.Expr("pink_diamond_num - ?", p.Num))
} else {
return myerr.NewSysError("addReduce 枚举错误 value:" + mysql.TypeToString(mysql.Type(p.AddReduce)))
}
if err := txDiamondAccount.Error; err != nil {
return myerr.WrapErr(err)
}
if txDiamondAccount.RowsAffected == 0 {
return myerr.NewWaring("gorm condition update.RowsAffected = 0")
}
//持久化diamondPinkAccountDetail
if err := domain.Persistent(p.Db, p); err != nil {
return myerr.WrapErr(err)
}
//改变diamondAccount值
if p.diamondAccount == nil {
return myerr.NewSysError("持久化错误, 模型:DiamondAccountDetail 中没有diamondAccount, DiamondAccountDetail.Id =" + strconv.Itoa(int(p.ID)))
}
var newDiamondAccount DiamondAccount
if err := p.Db.First(&newDiamondAccount, p.diamondAccount.ID).Error; err != nil {
return myerr.WrapErr(err)
}
if newDiamondAccount.DiamondNum < 0 {
return myerr.NewSysError("diamond_account表中,pink_diamond_num 不能小于0, diamondAccount.id = " + strconv.Itoa(int(newDiamondAccount.ID)))
}
return nil
}
......@@ -30,3 +30,24 @@ func SendDiamond(model *domain.Model, userId mysql.ID, opt diamond_e.OperateType
}
return nil
}
// 下发钻石
func SendDiamondPink(model *domain.Model, userId mysql.ID, opt diamond_e.OperateType, originId mysql.ID, diamondNum mysql.Num, operateIds string,
msgType ...msg_e.MsgUserRecordType) error {
diamondAccount, err := diamond_m.GetDiamondAccountByUserId(model, userId)
if err != nil {
return err
}
diamondSendAccountDetail, err := diamondAccount.ChangePinkDiamondAccountDetail(opt, originId, diamondNum, operateIds)
if err != nil {
return err
}
if err := diamondSendAccountDetail.Persistent(); err != nil {
return err
}
if len(msgType) > 0 {
// 小助手提示获得钻石
msg.SendLittleAssistantMsg(model, userId, msgType[0], fmt.Sprintf("%d", diamondNum), "", "", "", "")
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment