2026-05-29 19:54:56 +08:00

178 lines
6.8 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: zn
* Date: 2017/8/9
* Time: 11:51
*/
namespace common\modules\vip;
use common\modules\safe\SafeUtil;
use common\modules\systemlog\SystemLogUtil;
use common\modules\vip\vo\Vip;
use common\services\GameConfigServices;
use common\services\ResultStatusServices;
use common\services\StatusException;
use common\utils\CommUtil;
use common\utils\MoneyUtil;
use common\utils\RedisKeyUtil;
/**
* vip
* Class
* @package common\modules\levelgift
*/
class VipUtil
{
const GET_GIFT = 1;//获取奖励
const NOT_GET_GIFT = 0;//没有获取奖励
const GET_GIFT_LIST = [
self::GET_GIFT,
self::NOT_GET_GIFT,
];
public static function addVipExp($userID, $exp)
{
return SafeUtil::dbOperate(function ($userID, $exp) {
return SafeUtil::redisLockOperate(function ($userID, $exp) {
$exp = (int)$exp;
if ($exp <= 0) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$vip = Vip::findByUserID($userID);
$vip->exp += $exp;
while (true) {
if ($vip->level == GameConfigServices::getMaxVipLevel()) {
if (!$vip->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
return $vip;
}
$config = GameConfigServices::getParamByPath('vip_list.' . ($vip->level + 1));
if (empty($config)) throw new StatusException(ResultStatusServices::RS_NOT_FIND_IN_CONFIG);
$needExp = (int)$config['recharge_amount'];
if ($vip->exp >= $needExp) {
//升级
$vip->level++;
$vip->exp = $vip->exp - $needExp;
if ($vip->exp < 0) $vip->exp = 0;
//清除领取记录
$vip->is_get_fix_gift = VipUtil::NOT_GET_GIFT;
$vip->is_get_week_gift = VipUtil::NOT_GET_GIFT;
$vip->last_get_week_gift_time = 0;
} else {
break;
}
}
if (!$vip->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
return $vip;
}, [$userID, $exp], RedisKeyUtil::lockVip($userID));
}, [$userID, $exp]);
}
public static function getGift($userID)
{
SafeUtil::dbOperate(function ($userID) {
SafeUtil::redisLockOperate(function ($userID) {
$vip = Vip::findByUserID($userID);
$change = false;
//固定礼包
if ($vip->is_get_fix_gift == VipUtil::NOT_GET_GIFT && $vip->level > 0) {
$config = GameConfigServices::getParamByPath('vip_list.' . $vip->level);
if (empty($config)) throw new StatusException(ResultStatusServices::RS_NOT_FIND_IN_CONFIG);
MoneyUtil::parseItem($config['fixed_reward_id'], 1, true, $userID, SystemLogUtil::FROM_TYPE_VIP_FIX_GIFT, ['config' => $config]);
$vip->is_get_fix_gift = VipUtil::GET_GIFT;
$change = true;
}
//周礼包
if (CommUtil::getWeek(time()) != CommUtil::getWeek($vip->last_get_week_gift_time))
$vip->is_get_week_gift = VipUtil::NOT_GET_GIFT;
if ($vip->is_get_week_gift == VipUtil::NOT_GET_GIFT && $vip->level > 0) {
$config = GameConfigServices::getParamByPath('vip_list.' . $vip->level);
if (empty($config)) throw new StatusException(ResultStatusServices::RS_NOT_FIND_IN_CONFIG);
//领取物品
for ($i = 1; $i <= 3; $i++) {
if (!isset($config['cycle_bonus_id' . $i])) continue;
MoneyUtil::parseItem($config['cycle_bonus_id' . $i], 1, true, $userID, SystemLogUtil::FROM_TYPE_VIP_WEEK_GIFT, ['config' => $config]);
}
$vip->is_get_week_gift = VipUtil::GET_GIFT;
$vip->last_get_week_gift_time = time();
$change = true;
}
if ($change && !$vip->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}, [$userID], RedisKeyUtil::lockVip($userID));
}, [$userID]);
}
public static function getFixGift($userID)
{
SafeUtil::dbOperate(function ($userID) {
SafeUtil::redisLockOperate(function ($userID) {
$vip = Vip::findByUserID($userID);
if ($vip->level == 0 || $vip->is_get_fix_gift == VipUtil::GET_GIFT) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$config = GameConfigServices::getParamByPath('vip_list.' . $vip->level);
if (empty($config)) throw new StatusException(ResultStatusServices::RS_NOT_FIND_IN_CONFIG);
MoneyUtil::parseItem($config['fixed_reward_id'], 1, true, $userID, SystemLogUtil::FROM_TYPE_VIP_FIX_GIFT, ['config' => $config]);
$vip->is_get_fix_gift = VipUtil::GET_GIFT;
if (!$vip->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}, [$userID], RedisKeyUtil::lockVip($userID));
}, [$userID]);
}
public static function getWeekGift($userID)
{
SafeUtil::dbOperate(function ($userID) {
SafeUtil::redisLockOperate(function ($userID) {
$vip = Vip::findByUserID($userID);
if ($vip->level == 0) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
if (CommUtil::getWeek(time()) != CommUtil::getWeek($vip->last_get_week_gift_time))
$vip->is_get_week_gift = VipUtil::NOT_GET_GIFT;
if ($vip->is_get_week_gift == VipUtil::GET_GIFT) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$config = GameConfigServices::getParamByPath('vip_list.' . $vip->level);
if (empty($config)) throw new StatusException(ResultStatusServices::RS_NOT_FIND_IN_CONFIG);
//领取物品
for ($i = 1; $i <= 3; $i++) {
if (!isset($config['cycle_bonus_id' . $i])) continue;
MoneyUtil::parseItem($config['cycle_bonus_id' . $i], 1, true, $userID, SystemLogUtil::FROM_TYPE_VIP_WEEK_GIFT, ['config' => $config]);
}
$vip->is_get_week_gift = VipUtil::GET_GIFT;
$vip->last_get_week_gift_time = time();
if (!$vip->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}, [$userID], RedisKeyUtil::lockVip($userID));
}, [$userID]);
}
}