diamond.go 11.4 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package diamond_m

import (
hujiebin's avatar
hujiebin committed
4
	"fmt"
hujiebin's avatar
hujiebin committed
5
	"git.hilo.cn/hilo-common/domain"
6
	"git.hilo.cn/hilo-common/mylogrus"
hujiebin's avatar
hujiebin committed
7
	"git.hilo.cn/hilo-common/resource/mysql"
8 9
	"git.hilo.cn/hilo-common/utils"
	"gorm.io/gorm"
hujiebin's avatar
hujiebin committed
10
	"hilo-group/_const/enum/diamond_e"
hujiebin's avatar
hujiebin committed
11
	"hilo-group/myerr"
12 13 14
	"hilo-group/myerr/bizerr"
	"strconv"
	"time"
hujiebin's avatar
hujiebin committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
)

type DiamondAccount struct {
	mysql.Entity
	*domain.Model  `gorm:"-"`
	UserId         mysql.ID
	DiamondNum     mysql.Num
	PinkDiamondNum mysql.Num
	Status         diamond_e.StatusAccount
}

//账号详情
type DiamondAccountDetail 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
	diamondAccount   *DiamondAccount `gorm:"-"`
}

hujiebin's avatar
hujiebin committed
43 44 45 46 47
func (DiamondAccountDetail) TableName() string {
	month := time.Now().Format("200601")
	return fmt.Sprintf("diamond_account_detail_%s", month)
}

hujiebin's avatar
hujiebin committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
// 粉钻详情
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
	diamondAccount   *DiamondAccount `gorm:"-"`
}
hujiebin's avatar
hujiebin committed
64 65 66 67 68 69 70 71 72 73 74 75

//通过userId获取帐号
func GetDiamondAccountByUserId(model *domain.Model, userId mysql.ID) (*DiamondAccount, error) {
	var diamondAccount DiamondAccount
	if err := model.Db.WithContext(model).Where(&DiamondAccount{
		UserId: userId,
	}).First(&diamondAccount).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	diamondAccount.Model = model
	return &diamondAccount, nil
}
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

//账号操作配置
type DiamondOperateSet struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	DiamondNum    mysql.NumAll
	FrequencyNum  mysql.NumAll
	FrequencyDay  mysql.NumAll
	DiamondMaxNum mysql.NumAll
	AddReduce     mysql.AddReduce
	Type          diamond_e.OperateType
	Name          mysql.Str
	Status        mysql.UserYesNo
	DiamondType   diamond_e.OperateType
}

//匹配条件扣费
func (diamondAccount *DiamondAccount) ChangeDiamondAccountDetail(operateType diamond_e.OperateType, originId mysql.ID, diamondNum mysql.Num) (*DiamondAccountDetail, error) {
	return diamondAccount.addDiamondAccountDetail(operateType, originId, diamondNum)
}

//钻石操作记录,
func (diamondAccount *DiamondAccount) addDiamondAccountDetail(operateType diamond_e.OperateType, originId mysql.ID, diamondNum mysql.Num) (*DiamondAccountDetail, error) {
	var diamondOperateSet DiamondOperateSet
	var err error
	if err = diamondAccount.Db.Where(&DiamondOperateSet{
		Type:        operateType,
		Status:      mysql.USER,
		DiamondType: 1, // 1:黄钻 2:粉钻
	}).First(&diamondOperateSet).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}

	//判断是增加,账号是否被冻结
	if diamondAccount.Status == diamond_e.Frozen && diamondOperateSet.AddReduce == mysql.REDUCE {
		return nil, bizerr.DiamondAccountFrozen
	}

	//无限,检查次数
	var count int64
	if diamondOperateSet.FrequencyDay == -1 {
		if diamondOperateSet.FrequencyNum != -1 {
hujiebin's avatar
hujiebin committed
118
			diamondAccount.DB().Table(DiamondAccountDetail{}.TableName()).Where(&DiamondAccountDetail{
119 120 121 122 123 124 125 126 127 128 129 130 131 132
				UserId:      diamondAccount.UserId,
				OperateType: operateType,
			}).Count(&count)
			if count >= int64(diamondOperateSet.FrequencyNum) {
				return nil, 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)
		}
		//一天的次数
hujiebin's avatar
hujiebin committed
133
		diamondAccount.DB().Table(DiamondAccountDetail{}.TableName()).Where(&DiamondAccountDetail{
134 135 136 137 138 139 140
			UserId:      diamondAccount.UserId,
			OperateType: operateType,
		}).Where("created_time >= ? ", beginTime).Count(&count)
		if count >= int64(diamondOperateSet.FrequencyNum) {
			return nil, bizerr.DiamondFrequency
		}
		//终极拦截,利用
hujiebin's avatar
hujiebin committed
141
		diamondAccount.SetCheckUpdateCondition(" EXISTS (SELECT * from (SELECT COUNT(1)  as n from " + DiamondAccountDetail{}.TableName() + " 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(utils.GetZeroTime(time.Now()).Unix(), 10) + ")) t where t.n < " + strconv.Itoa(diamondOperateSet.FrequencyNum) + " )")
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	}

	//-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.DiamondNum + upateDiamondNum
	} else if diamondOperateSet.AddReduce == mysql.REDUCE {
		if diamondAccount.DiamondNum < upateDiamondNum {
			return nil, bizerr.DiamondNoEnough
		}
		afterNum = diamondAccount.DiamondNum - upateDiamondNum
	} else {
		return nil, myerr.NewSysError("AddReduce 值错误:" + mysql.TypeToString(mysql.Type(diamondOperateSet.AddReduce)))
	}

	diamondAccountDetail := &DiamondAccountDetail{
		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.DiamondNum,
		AftNum:           afterNum,
		diamondAccount:   diamondAccount,
	}
	return diamondAccountDetail, err
}

func (diamondAccountDetail *DiamondAccountDetail) PersistentNoInTransactional() error {
	//fixme: 这里有点奇怪, diamondAccount持久化动作在diamondAccountDetail持久化之后,RowsAffected 就一定是0
	txDiamondAccount := diamondAccountDetail.Db.Model(diamondAccountDetail.diamondAccount)
	if diamondAccountDetail.diamondAccount.CheckUpdateCondition() {
		txDiamondAccount = txDiamondAccount.Where(diamondAccountDetail.diamondAccount.GetUpdateCondition())
	}
	if diamondAccountDetail.AddReduce == mysql.ADD {
		//增加
		txDiamondAccount.UpdateColumn("diamond_num", gorm.Expr("diamond_num + ?", diamondAccountDetail.Num))
	} else if diamondAccountDetail.AddReduce == mysql.REDUCE {
		//减少,保证不能扣成负数
		txDiamondAccount.Where("diamond_num >= ?", diamondAccountDetail.Num).UpdateColumn("diamond_num", gorm.Expr("diamond_num - ?", diamondAccountDetail.Num))
	} else {
		myerr.NewSysError("addReduce 枚举错误 value:" + mysql.TypeToString(mysql.Type(diamondAccountDetail.AddReduce)))
	}
	if err := txDiamondAccount.Error; err != nil {
		return myerr.WrapErr(err)
	}
	if txDiamondAccount.RowsAffected == 0 {
		diamondAccountDetail.Log.Errorf("gorm condition update.RowsAffected = 0,AddReduce:%v", diamondAccountDetail.AddReduce)
		return myerr.NewWaring("gorm condition update.RowsAffected = 0")
	}

	//持久化diamondAccountDetail
hujiebin's avatar
hujiebin committed
205 206 207 208
	//if err := model.Persistent(diamondAccountDetail.Db, diamondAccountDetail); err != nil {
	//	return myerr.WrapErr(err)
	//}
	if err := diamondAccountDetail.DB().Table(diamondAccountDetail.TableName()).Save(diamondAccountDetail).Error; err != nil {
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		return myerr.WrapErr(err)
	}
	//改变diamondAccount值
	if diamondAccountDetail.diamondAccount == nil {
		return myerr.NewSysError("持久化错误, 模型:DiamondAccountDetail 中没有diamondAccount, DiamondAccountDetail.Id =" + strconv.Itoa(int(diamondAccountDetail.ID)))
	}

	var newDiamondAccount DiamondAccount
	if err := diamondAccountDetail.Db.First(&newDiamondAccount, diamondAccountDetail.diamondAccount.ID).Error; err != nil {
		return myerr.WrapErr(err)
	}

	if newDiamondAccount.DiamondNum < 0 {
		return myerr.NewSysError("diamond_account表中,diamond_num 不能小于0, diamondAccount.id = " + strconv.Itoa(int(newDiamondAccount.ID)))
	}
	return nil
}

//群组支持
func (diamondAccount *DiamondAccount) GroupSupportAdmin(groupSupportAwardId mysql.ID, diamondNum uint32) (*DiamondAccountDetail, error) {
	return diamondAccount.addDiamondAccountDetail(diamond_e.GroupSupportAdmin, groupSupportAwardId, diamondNum)
}

func (diamondAccountDetail *DiamondAccountDetail) Persistent() error {
	//fixme: 这里有点奇怪, diamondAccount持久化动作在diamondAccountDetail持久化之后,RowsAffected 就一定是0
	txDiamondAccount := diamondAccountDetail.Db.Model(diamondAccountDetail.diamondAccount)
	if diamondAccountDetail.diamondAccount.CheckUpdateCondition() {
		txDiamondAccount = txDiamondAccount.Where(diamondAccountDetail.diamondAccount.GetUpdateCondition())
	}
	if diamondAccountDetail.AddReduce == mysql.ADD {
		//增加
		txDiamondAccount.UpdateColumn("diamond_num", gorm.Expr("diamond_num + ?", diamondAccountDetail.Num))
	} else if diamondAccountDetail.AddReduce == mysql.REDUCE {
		//减少,保证不能扣成负数
		txDiamondAccount.Where("diamond_num >= ?", diamondAccountDetail.Num).UpdateColumn("diamond_num", gorm.Expr("diamond_num - ?", diamondAccountDetail.Num))
	} else {
		myerr.NewSysError("addReduce 枚举错误 value:" + mysql.TypeToString(mysql.Type(diamondAccountDetail.AddReduce)))
	}
	if err := txDiamondAccount.Error; err != nil {
		return myerr.WrapErr(err)
	}
	if txDiamondAccount.RowsAffected == 0 {
		mylogrus.MyLog.Errorf("gorm condition update.RowsAffected = 0,AddReduce:%v", diamondAccountDetail.AddReduce)
		return myerr.NewWaring("gorm condition update.RowsAffected = 0")
	}

	//持久化diamondAccountDetail
hujiebin's avatar
hujiebin committed
256 257 258 259
	//if err := model.Persistent(diamondAccountDetail.Db, diamondAccountDetail); err != nil {
	//	return myerr.WrapErr(err)
	//}
	if err := diamondAccountDetail.DB().Table(diamondAccountDetail.TableName()).Save(diamondAccountDetail).Error; err != nil {
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
		return myerr.WrapErr(err)
	}
	//改变diamondAccount值
	if diamondAccountDetail.diamondAccount == nil {
		return myerr.NewSysError("持久化错误, 模型:DiamondAccountDetail 中没有diamondAccount, DiamondAccountDetail.Id =" + strconv.Itoa(int(diamondAccountDetail.ID)))
	}

	var newDiamondAccount DiamondAccount
	if err := diamondAccountDetail.Db.First(&newDiamondAccount, diamondAccountDetail.diamondAccount.ID).Error; err != nil {
		return myerr.WrapErr(err)
	}

	if newDiamondAccount.DiamondNum < 0 {
		return myerr.NewSysError("diamond_account表中,diamond_num 不能小于0, diamondAccount.id = " + strconv.Itoa(int(newDiamondAccount.ID)))
	}
	return nil
}

//群组支持
func (diamondAccount *DiamondAccount) GroupSupportMgr(groupSupportAwardId mysql.ID, diamondNum uint32) (*DiamondAccountDetail, error) {
	return diamondAccount.addDiamondAccountDetail(diamond_e.GroupSupportMgr, groupSupportAwardId, diamondNum)
}
hujiebin's avatar
hujiebin committed
282 283 284 285 286 287 288 289 290 291

//群组IM群发
func (diamondAccount *DiamondAccount) GroupImMass() (*DiamondAccountDetail, error) {
	return diamondAccount.addDiamondAccountDetail(diamond_e.GroupIMMass, 0, 0)
}

//购买自定义群组主题
func (diamondAccount *DiamondAccount) BuyGroupCustomTheme(diamondAccountDetailId mysql.ID) (*DiamondAccountDetail, error) {
	return diamondAccount.addDiamondAccountDetail(diamond_e.GroupCustomTheme, diamondAccountDetailId, 0)
}