hy-farm/muchang/common/modules/levelgift/LevelGiftUtil.php
2026-05-29 19:54:56 +08:00

61 lines
2.1 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: zn
* Date: 2017/8/9
* Time: 11:51
*/
namespace common\modules\levelgift;
use common\models\User;
use common\modules\levelgift\vo\LevelGift;
use common\modules\safe\SafeUtil;
use common\modules\systemlog\SystemLogUtil;
use common\services\GameConfigServices;
use common\services\ResultStatusServices;
use common\services\StatusException;
use common\utils\MoneyUtil;
use common\utils\RedisKeyUtil;
/**
* 等级礼包
* Class LevelGiftUtil
* @package common\modules\levelgift
*/
class LevelGiftUtil
{
public static function getLevelGift($userID, $giftID)
{
if (empty($userID) || empty($giftID))
throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
SafeUtil::dbOperate(function ($userID, $giftID) {
SafeUtil::redisLockOperate(function ($userID, $giftID) {
$itemConfig = GameConfigServices::getParamByPath('player_level_gift.' . $giftID);
if (empty($itemConfig)) throw new StatusException(ResultStatusServices::RS_NOT_FIND_IN_CONFIG);
if (!empty(LevelGift::findByUserIDAndGiftID($userID, $giftID)))
throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
$user = User::findById($userID);
if (((int)$itemConfig['player_level']) > $user->level)
throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
//领取物品
for ($i = 1; $i <= 4; $i++) {
if (!isset($itemConfig['fruit_id' . $i])) continue;
MoneyUtil::parseItem($itemConfig['fruit_id' . $i], 1, true, $userID, SystemLogUtil::FROM_TYPE_PLAYER_LEVEL_GIFT, ['config' => $itemConfig]);
}
$levelGift = new LevelGift();
$levelGift->user_id = $userID;
$levelGift->gift_id = $giftID;
if (!$levelGift->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}, [$userID, $giftID], RedisKeyUtil::lockPlayerLevelGift($userID));
}, [$userID, $giftID]);
}
}