level.go 11.9 KB
Newer Older
1 2 3 4 5 6
package cp_m

import (
	"fmt"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
7
	"git.hilo.cn/hilo-common/rpc"
hujiebin's avatar
hujiebin committed
8
	"git.hilo.cn/hilo-common/txop/headwear_tx"
9
	"gorm.io/gorm"
hujiebin's avatar
hujiebin committed
10
	"gorm.io/gorm/clause"
11
	"hilo-user/_const/enum/cp_e"
hujiebin's avatar
hujiebin committed
12 13
	"hilo-user/_const/enum/gift_e"
	"hilo-user/domain/model/user_m"
14 15 16 17 18 19 20
	"hilo-user/myerr"
	"time"
)

// cp等级
type CpLevel struct {
	mysql.Entity
hujiebin's avatar
hujiebin committed
21 22 23 24 25 26
	CpId     mysql.ID
	UserId1  mysql.ID
	UserId2  mysql.ID
	Points   mysql.Num
	Level    cp_e.CpLevel
	ExpireAt time.Time
27 28 29 30 31
}

// cp等级积分明细
type CpLevelDetail struct {
	mysql.Entity
hujiebin's avatar
hujiebin committed
32 33 34 35 36 37 38 39
	CpId      mysql.ID
	UserId1   mysql.ID
	UserId2   mysql.ID
	AddReduce mysql.AddReduce
	Num       mysql.Num
	BefNum    mysql.Num
	AftNum    mysql.Num
	Remark    string
40 41
}

hujiebin's avatar
hujiebin committed
42 43 44 45 46 47 48 49 50
// 获取cp等级
func GetCpLevel(model *domain.Model, cpId mysql.ID) CpLevel {
	var level CpLevel
	if err := model.DB().Model(CpLevel{}).Where("cp_id = ?", cpId).First(&level).Error; err != nil {
		model.Log.Errorf("GetCpLevel fail:%v", err)
	}
	return level
}

51 52 53 54 55 56 57 58 59 60 61 62 63
// 批量获取cp等级
func MGetCpLevel(model *domain.Model, cpIds []mysql.ID) map[mysql.ID]CpLevel {
	var res = make(map[mysql.ID]CpLevel)
	var level []CpLevel
	if err := model.DB().Model(CpLevel{}).Where("cp_id in ?", cpIds).Find(&level).Error; err != nil {
		model.Log.Errorf("MGetCpLevel fail:%v", err)
	}
	for i, v := range level {
		res[v.CpId] = level[i]
	}
	return res
}

64 65 66 67 68
// 添加cp等级积分增减明细
func AddCpLevelDetail(model *domain.Model, detail CpLevelDetail) error {
	return model.DB().Create(&detail).Error
}

hujiebin's avatar
hujiebin committed
69
// 获取cpRelation
hujiebin's avatar
hujiebin committed
70 71
func GetCpRelation(model *domain.Model, userId mysql.ID) (cpRelation CpRelation, exits bool) {
	if err := model.DB().Model(CpRelation{}).Where("user_id1 = ? or user_id2 = ?", userId, userId).First(&cpRelation).Error; err != nil {
hujiebin's avatar
hujiebin committed
72 73 74 75 76 77 78 79 80 81 82
		if err != gorm.ErrRecordNotFound {
			model.Log.Errorf("GetCpRelation fail:%v", err)
			return
		}
	} else {
		exits = true
	}
	return
}

// 获取cpRelation pair
hujiebin's avatar
hujiebin committed
83 84
func GetCpRelationPair(model *domain.Model, userId1, userId2 mysql.ID) (cpRelation CpRelation, exits bool) {
	if err := model.DB().Model(CpRelation{}).Where("user_id1 = ? AND user_id2 = ?", userId1, userId2).First(&cpRelation).Error; err != nil {
hujiebin's avatar
hujiebin committed
85 86 87 88 89
		if err != gorm.ErrRecordNotFound {
			model.Log.Errorf("GetCpRelation fail:%v", err)
			return
		} else {
			// gorm.ErrRecordNotFound
hujiebin's avatar
hujiebin committed
90
			if err := model.DB().Model(CpRelation{}).Where("user_id1 = ? AND user_id2 = ?", userId2, userId1).First(&cpRelation).Error; err != nil {
hujiebin's avatar
hujiebin committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104
				if err != gorm.ErrRecordNotFound {
					model.Log.Errorf("GetCpRelation fail:%v", err)
					return
				}
			} else {
				exits = true
			}
		}
	} else {
		exits = true
	}
	return
}

hujiebin's avatar
hujiebin committed
105
// 获取cpRelation pair
hujiebin's avatar
hujiebin committed
106 107
func MGetCpRelation(model *domain.Model, userIds []mysql.ID) (cpRelation []CpRelation) {
	if err := model.DB().Model(CpRelation{}).Where("user_id1 in ? or user_id2 in ?", userIds, userIds).Find(&cpRelation).Error; err != nil {
hujiebin's avatar
hujiebin committed
108 109 110 111 112
		model.Log.Errorf("GetCpRelation fail:%v", err)
	}
	return
}

hujiebin's avatar
hujiebin committed
113 114 115
// 获取是否申请解绑中
func GetApplyToUnbind(model *domain.Model, userId, cpUserId mysql.ID) bool {
	var total int64
hujiebin's avatar
hujiebin committed
116
	if err := model.DB().Model(CpCancel{}).Where("user_id = ? AND rec_user_id = ? AND status = 1", userId, cpUserId).Count(&total).Error; err != nil {
hujiebin's avatar
hujiebin committed
117 118 119 120 121
		model.Log.Errorf("GetApplyToUnbind fail:%v", err)
	}
	return total > 0
}

hujiebin's avatar
hujiebin committed
122 123 124 125 126 127 128 129 130 131
// 初始化cpLevel
func InitCpLevel(model *domain.Model, cpId, userId1, userId2 mysql.ID) error {
	return model.DB().Model(CpLevel{}).Clauses(clause.OnConflict{DoNothing: true}).Create(&CpLevel{
		CpId:     cpId,
		UserId1:  userId1,
		UserId2:  userId2,
		ExpireAt: time.Now().AddDate(0, 1, 0),
	}).Error
}

132
// 增加cp等级积分
hujiebin's avatar
hujiebin committed
133
// 此函数并发不安全,利用mysql事件串行执行保证顺序
134 135 136 137 138 139
// 送礼1钻石=1点数
// condition
//	 1.记录不存在,首充加points计算level增加90天有效期
//	 2.记录存在
//		 2.1 在有效期内,直接加points后判断新level,升级需要更新有效期
//		 2.2 不有效期内,算首充,重置points后判断新level,升级需要更新有效期
hujiebin's avatar
hujiebin committed
140
func AddCpLevelPoints(model *domain.Model, cpRelation CpRelation, points mysql.Num, sceneType gift_e.GiftOperateSceneType) (err error) {
141 142 143 144
	start := time.Now()
	defer func() {
		model.Log.Infof("AddCpLevelPoints cost:%v,err:%v", time.Now().Sub(start), err)
	}()
hujiebin's avatar
hujiebin committed
145
	var oldLevel cp_e.CpLevel
146 147
	var cpLevel CpLevel
	var cpLevelDetails []CpLevelDetail
hujiebin's avatar
hujiebin committed
148
	if err := model.DB().Model(CpLevel{}).Where("cp_id = ?", cpRelation.Id).First(&cpLevel).Error; err != nil {
149 150 151 152 153
		if err != gorm.ErrRecordNotFound {
			return myerr.WrapErr(err)
		}
		// 明细
		cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
hujiebin's avatar
hujiebin committed
154
			CpId:      cpRelation.Id,
hujiebin's avatar
hujiebin committed
155 156
			UserId1:   cpRelation.UserId1,
			UserId2:   cpRelation.UserId2,
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
			AddReduce: mysql.ADD,
			Num:       points,
			BefNum:    0,
			AftNum:    points,
			Remark:    fmt.Sprintf("send %d gift diamonds", points),
		})
		//  1.记录不存在,首充加points计算level增加90天有效期
		var level cp_e.CpLevel
		for l := cp_e.CpLevelMax; l >= cp_e.CpLevel0; l-- {
			if cp_e.CpLevelPoints[l] <= points {
				level = l
				break
			}
		}
		if level > 0 {
			points = points - cp_e.CpLevelPoints[level] // 减去用于已用于升级的积分
			// 明细
			cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
hujiebin's avatar
hujiebin committed
175
				CpId:      cpRelation.Id,
hujiebin's avatar
hujiebin committed
176 177
				UserId1:   cpRelation.UserId1,
				UserId2:   cpRelation.UserId2,
178 179 180 181 182 183 184 185
				AddReduce: mysql.REDUCE,
				Num:       cp_e.CpLevelPoints[level],
				BefNum:    cp_e.CpLevelPoints[level] + points,
				AftNum:    points,
				Remark:    fmt.Sprintf("Become LEVEL%d", level),
			})
		}
		cpLevel = CpLevel{
hujiebin's avatar
hujiebin committed
186
			CpId:     cpRelation.Id,
hujiebin's avatar
hujiebin committed
187 188 189 190 191
			UserId1:  cpRelation.UserId1,
			UserId2:  cpRelation.UserId2,
			Points:   points,
			Level:    level,
			ExpireAt: time.Now().AddDate(0, 0, cp_e.EffectDays),
192 193 194 195 196 197 198 199
		}
	} else {
		// 2.记录存在
		// 2.1 在有效期内,直接加points后判断新level,升级需要更新有效期
		if cpLevel.ExpireAt.After(time.Now()) {
			cpLevel.Points += points
			// 明细
			cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
hujiebin's avatar
hujiebin committed
200
				CpId:      cpRelation.Id,
hujiebin's avatar
hujiebin committed
201 202
				UserId1:   cpRelation.UserId1,
				UserId2:   cpRelation.UserId2,
203 204 205 206 207 208
				AddReduce: mysql.ADD,
				Num:       points,
				BefNum:    cpLevel.Points - points,
				AftNum:    cpLevel.Points,
				Remark:    fmt.Sprintf("send %d gift diamonds", points),
			})
hujiebin's avatar
hujiebin committed
209
			oldLevel = cpLevel.Level
210 211 212 213 214 215 216 217 218 219 220 221 222 223
			levelPoint := cp_e.CpLevelPoints[oldLevel] // 已经用于升级的积分
			for level := cp_e.CpLevelMax; level > oldLevel; level-- {
				if cp_e.CpLevelPoints[level] <= cpLevel.Points+levelPoint {
					cpLevel.Level = level
					break
				}
			}
			// 升级
			if oldLevel != cpLevel.Level {
				// 减去已用于升级的积分
				cpLevel.Points = cpLevel.Points - (cp_e.CpLevelPoints[cpLevel.Level] - cp_e.CpLevelPoints[oldLevel])
				cpLevel.ExpireAt = time.Now().AddDate(0, 0, cp_e.EffectDays)
				// 明细
				cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
hujiebin's avatar
hujiebin committed
224
					CpId:      cpRelation.Id,
hujiebin's avatar
hujiebin committed
225 226
					UserId1:   cpRelation.UserId1,
					UserId2:   cpRelation.UserId2,
227 228 229 230 231 232 233 234 235 236 237 238 239
					AddReduce: mysql.REDUCE,
					Num:       cp_e.CpLevelPoints[cpLevel.Level] - cp_e.CpLevelPoints[oldLevel],
					BefNum:    cpLevel.Points + cp_e.CpLevelPoints[cpLevel.Level] - cp_e.CpLevelPoints[oldLevel],
					AftNum:    cpLevel.Points,
					Remark:    fmt.Sprintf("Become LEVEL%d", cpLevel.Level),
				})
			}
		} else {
			// 2.2 不有效期内,算首充,重置points后判断新level,更新有效期30天
			oldPoints := cpLevel.Points
			cpLevel.Points = points
			// 明细
			cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
hujiebin's avatar
hujiebin committed
240
				CpId:      cpRelation.Id,
hujiebin's avatar
hujiebin committed
241 242
				UserId1:   cpRelation.UserId1,
				UserId2:   cpRelation.UserId2,
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
				AddReduce: mysql.ADD,
				Num:       points,
				BefNum:    oldPoints,
				AftNum:    cpLevel.Points,
				Remark:    fmt.Sprintf("send %d gift diamonds", points),
			})
			for level := cp_e.CpLevelMax; level >= cp_e.CpLevel0; level-- {
				if cp_e.CpLevelPoints[level] <= cpLevel.Points {
					cpLevel.Level = level
					break
				}
			}
			if cpLevel.Level > 0 {
				cpLevel.Points -= cp_e.CpLevelPoints[cpLevel.Level] // 减去已用于升级的积分
				// 明细
				cpLevelDetails = append(cpLevelDetails, CpLevelDetail{
hujiebin's avatar
hujiebin committed
259
					CpId:      cpRelation.Id,
hujiebin's avatar
hujiebin committed
260 261
					UserId1:   cpRelation.UserId1,
					UserId2:   cpRelation.UserId2,
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
					AddReduce: mysql.REDUCE,
					Num:       cp_e.CpLevelPoints[cpLevel.Level],
					BefNum:    cpLevel.Points + cp_e.CpLevelPoints[cpLevel.Level],
					AftNum:    cpLevel.Points,
					Remark:    fmt.Sprintf("Become SVIP%d", cpLevel.Level),
				})
			}
			cpLevel.ExpireAt = time.Now().AddDate(0, 0, cp_e.EffectDays)
		}
	}
	// 顺序增加明细
	for _, detail := range cpLevelDetails {
		if err := AddCpLevelDetail(model, detail); err != nil {
			return myerr.WrapErr(err)
		}
	}
hujiebin's avatar
hujiebin committed
278 279
	// 赠送cp头像头饰
	if oldLevel < 3 && cpLevel.Level >= 3 {
hujiebin's avatar
hujiebin committed
280
		if err := headwear_tx.SendHeadwear(model, cpRelation.UserId1, cp_e.CpHeadwearId, 30); err != nil {
hujiebin's avatar
hujiebin committed
281 282
			return err
		}
hujiebin's avatar
hujiebin committed
283
		if err := headwear_tx.SendHeadwear(model, cpRelation.UserId2, cp_e.CpHeadwearId, 30); err != nil {
hujiebin's avatar
hujiebin committed
284
			return err
hujiebin's avatar
hujiebin committed
285
		}
hujiebin's avatar
hujiebin committed
286
	}
hujiebin's avatar
hujiebin committed
287 288 289 290 291 292 293 294 295 296
	// 群组中送礼升级
	if oldLevel != cpLevel.Level && sceneType == gift_e.GroupSceneType {
		go func() {
			userId1, userId2 := cpRelation.UserId1, cpRelation.UserId2
			model := domain.CreateModelContext(model.MyContext)
			users, err := user_m.GetUserMapByIds(model, []mysql.ID{userId1, userId2})
			if err != nil {
				model.Log.Errorf("")
				return
			}
hujiebin's avatar
hujiebin committed
297 298 299 300 301 302 303 304 305
			levelMsgIdMap := map[cp_e.CpLevel]uint{
				cp_e.CpLevel1: 252,
				cp_e.CpLevel2: 253,
				cp_e.CpLevel3: 254,
				cp_e.CpLevel4: 255,
				cp_e.CpLevel5: 256,
			}
			content := fmt.Sprintf(GetTranslate(286, users[userId1].Language), GetTranslate(levelMsgIdMap[cpLevel.Level], users[userId1].Language))
			if err := rpc.SendCpUpgrade(users[userId1].Nick, users[userId2].Nick, users[userId1].Avatar, users[userId2].Avatar, uint32(cpLevel.Level), content); err != nil {
hujiebin's avatar
hujiebin committed
306 307 308 309
				model.Log.Errorf("SendCpUpgrade fail:%v", err)
			}
		}()
	}
310 311 312 313
	return cpLevel.Persistence(model)
}

// 清理过期svip积分
hujiebin's avatar
hujiebin committed
314
// 降级保级: 积分清零,svip去到大于0的等级,有效期30天
315
// svip0:积分清零,有效期保持过期
hujiebin's avatar
hujiebin committed
316
func ClearExpireCpPoints(model *domain.Model) error {
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	var cpLevels []*CpLevel
	// 过期 + (积分 or level) 大于0
	if err := model.DB().Model(CpLevel{}).Where("expire_at < ? AND (points > 0 or level > 0) ", time.Now()).Find(&cpLevels).Error; err != nil {
		return myerr.WrapErr(err)
	}
	for _, cpLevel := range cpLevels {
		var doubleCheck CpLevel
		if err := model.DB().Model(CpLevel{}).Where("id = ?", cpLevel.ID).First(&doubleCheck).Error; err != nil {
			model.Log.Errorf("double check fail:%v", err)
			continue
		}
		if doubleCheck.ExpireAt.After(time.Now()) {
			continue
		}
		oldPoints := cpLevel.Points
		cpLevel.Level, cpLevel.Points = cp_e.CpLevel0, 0 // 清零
		var newLevel cp_e.CpLevel
		// 0级不刷新30天有效期
		for level := cp_e.CpLevelMax; level > cp_e.CpLevel0; level-- {
			if cp_e.CpLevelPoints[level] <= oldPoints {
				newLevel = level
				break
			}
		}
		// 降级/保级刷新30天有效期
		if newLevel > 0 {
			cpLevel.Level = newLevel
			cpLevel.ExpireAt = time.Now().AddDate(0, 0, cp_e.EffectDays)
			// 明细
			if err := AddCpLevelDetail(model, CpLevelDetail{
hujiebin's avatar
hujiebin committed
347 348 349 350 351 352 353 354
				CpId:      cpLevel.CpId,
				UserId1:   cpLevel.UserId1,
				UserId2:   cpLevel.UserId2,
				AddReduce: mysql.REDUCE,
				Num:       cp_e.CpLevelPoints[newLevel],
				BefNum:    oldPoints,
				AftNum:    0,
				Remark:    fmt.Sprintf("Become LEVEL%d", newLevel),
355 356 357 358 359 360 361 362 363
			}); err != nil {
				model.Log.Errorf("AddCpLevelDetail fail:%v", err)
			}
		}
		if err := cpLevel.Persistence(model); err != nil {
			model.Log.Errorf("cpLevel persistence fail:%v", err)
		}
		// 明细
		if err := AddCpLevelDetail(model, CpLevelDetail{
hujiebin's avatar
hujiebin committed
364 365 366 367 368 369 370 371
			CpId:      cpLevel.CpId,
			UserId1:   cpLevel.UserId1,
			UserId2:   cpLevel.UserId2,
			AddReduce: mysql.SET,
			Num:       oldPoints - cp_e.CpLevelPoints[newLevel],
			BefNum:    oldPoints - cp_e.CpLevelPoints[newLevel],
			AftNum:    0,
			Remark:    fmt.Sprintf("Expired clear"),
372 373 374 375 376 377
		}); err != nil {
			model.Log.Errorf("AddCpLevelDetail fail:%v", err)
		}
	}
	return nil
}