101 lines
3.6 KiB
PHP
101 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/8/9
|
|
* Time: 11:51
|
|
*/
|
|
|
|
namespace common\modules\card;
|
|
|
|
|
|
use common\modules\card\vo\Card;
|
|
use common\modules\dailytask\DailyTaskUtil;
|
|
use common\modules\safe\SafeUtil;
|
|
use common\modules\systemlog\SystemLogUtil;
|
|
use common\modules\vip\VipUtil;
|
|
use common\services\GameConfigServices;
|
|
use common\services\ResultStatusServices;
|
|
use common\services\StatusException;
|
|
use common\utils\CommUtil;
|
|
use common\utils\MoneyUtil;
|
|
use common\utils\RedisKeyUtil;
|
|
|
|
/**
|
|
* 周卡,月卡
|
|
* Class
|
|
* @package common\modules\levelgift
|
|
*/
|
|
class CardUtil
|
|
{
|
|
|
|
const GET_GIFT = 1;//获取奖励
|
|
const NOT_GET_GIFT = 0;//没有获取奖励
|
|
const GET_GIFT_LIST = [
|
|
self::GET_GIFT,
|
|
self::NOT_GET_GIFT,
|
|
];
|
|
|
|
public static function buyCard($userID, $itemID)
|
|
{
|
|
// return SafeUtil::dbOperate(function ($userID, $itemID) {
|
|
return SafeUtil::redisLockOperate(function ($userID, $itemID) {
|
|
|
|
$card = Card::findByUserIDAndItemID($userID, $itemID);
|
|
if (!empty($card) && $card->count > 0) throw new StatusException(ResultStatusServices::RS_ITEM_CAN_NOT_BY_MORE);
|
|
|
|
if ($card == null) {
|
|
$card = new Card();
|
|
$card->user_id = $userID;
|
|
$card->item_id = $itemID;
|
|
}
|
|
|
|
$config = GameConfigServices::getParamByPath('card.' . $itemID);
|
|
if (empty($config)) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
$card->count = intval($config['count']);
|
|
$card->last_get_gift_time = 0;
|
|
|
|
MoneyUtil::parseItem($config['fixed_reward_id'], 1, true, $userID, SystemLogUtil::FROM_TYPE_CARD_FIX_GIFT, ['config' => $config]);
|
|
|
|
if (!$card->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
|
|
|
|
VipUtil::addVipExp($userID, intval($config['rmb']) * 10);
|
|
|
|
}, [$userID, $itemID], RedisKeyUtil::lockCard($userID, $itemID));
|
|
// }, [$userID, $itemID]);
|
|
}
|
|
|
|
public static function getGift($userID, $itemID)
|
|
{
|
|
SafeUtil::dbOperate(function ($userID, $itemID) {
|
|
SafeUtil::redisLockOperate(function ($userID, $itemID) {
|
|
|
|
$card = Card::findByUserIDAndItemID($userID, $itemID);
|
|
if (empty($card) || $card->count <= 0) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
if (CommUtil::dayStr($card->last_get_gift_time) == CommUtil::dayStr(time()))
|
|
throw new StatusException(ResultStatusServices::RS_HAD_GET);
|
|
|
|
$config = GameConfigServices::getParamByPath('card.' . $itemID);
|
|
if (empty($config)) throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
// if ($card->is_get_fix_gift == CardUtil::NOT_GET_GIFT) {
|
|
// MoneyUtil::parseItem($config['fixed_reward_id'], 1, true, $userID, SystemLogUtil::FROM_TYPE_CARD_FIX_GIFT, ['config' => $config]);
|
|
// }
|
|
|
|
MoneyUtil::parseItem($config['diamonds_day_id'], 1, true, $userID, SystemLogUtil::FROM_TYPE_CARD_DAY_GIFT, ['config' => $config]);
|
|
|
|
$card->last_get_gift_time = time();
|
|
$card->count--;
|
|
|
|
if (!$card->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
|
|
|
|
//任务记录
|
|
DailyTaskUtil::addTaskCount($userID, DailyTaskUtil::TYPE_GET_DAY_OR_WEEK_CARD_GIFT, null, 1);
|
|
|
|
}, [$userID, $itemID], RedisKeyUtil::lockCard($userID, $itemID));
|
|
}, [$userID, $itemID]);
|
|
}
|
|
|
|
} |