0) { $grantedChances[] = [ 'activity_id' => $activity->id, 'activity_name' => $activity->name, 'chances' => $chances ]; } } catch (Exception $e) { // 记录错误但不影响其他活动的处理 trace('抽奖机会分发失败: ' . $e->getMessage(), 'error'); } } return $grantedChances; } /** * 充值完成后检查并分发抽奖机会 * @param array $rechargeInfo 充值信息 * @param int $userId 用户ID * @return array 获得的抽奖机会信息 */ public static function checkAndGrantChanceForRecharge($rechargeInfo, $userId) { $grantedChances = []; // 获取所有正在进行的抽奖活动 $activities = LotteryActivity::getRunningActivities(); foreach ($activities as $activity) { try { $chances = static::processActivityForRecharge($activity, $rechargeInfo, $userId); if ($chances > 0) { $grantedChances[] = [ 'activity_id' => $activity->id, 'activity_name' => $activity->name, 'chances' => $chances ]; } } catch (Exception $e) { // 记录错误但不影响其他活动的处理 trace('抽奖机会分发失败: ' . $e->getMessage(), 'error'); } } return $grantedChances; } /** * 处理订单相关的活动 */ private static function processActivityForOrder($activity, $orderInfo, $userId) { // 检查用户是否符合活动参与资格 if (!static::checkUserQualification($activity, $userId)) { return 0; } // 获取活动的参与条件 $conditions = LotteryCondition::getValidConditions($activity->id); $totalChances = 0; foreach ($conditions as $condition) { // 跳过充值条件 if ($condition->type == LotteryCondition::TYPE_RECHARGE) { continue; } $chances = static::processConditionForOrder($condition, $orderInfo, $userId); $totalChances += $chances; } // 如果获得了抽奖机会,记录到数据库 if ($totalChances > 0) { static::grantChanceToUser($activity->id, $userId, $totalChances, [ 'trigger_type' => 'order', 'order_id' => $orderInfo['id'] ?? 0, 'order_amount' => $orderInfo['total_amount'] ?? 0, 'granted_time' => time() ]); } return $totalChances; } /** * 处理充值相关的活动 */ private static function processActivityForRecharge($activity, $rechargeInfo, $userId) { // 检查用户是否符合活动参与资格 if (!static::checkUserQualification($activity, $userId)) { return 0; } // 获取活动的参与条件 $conditions = LotteryCondition::getValidConditions($activity->id); $totalChances = 0; foreach ($conditions as $condition) { // 只处理充值条件 if ($condition->type != LotteryCondition::TYPE_RECHARGE) { continue; } $chances = static::processConditionForRecharge($condition, $rechargeInfo, $userId); $totalChances += $chances; } // 如果获得了抽奖机会,记录到数据库 if ($totalChances > 0) { static::grantChanceToUser($activity->id, $userId, $totalChances, [ 'trigger_type' => 'recharge', 'recharge_amount' => $rechargeInfo['amount'] ?? 0, 'granted_time' => time() ]); } return $totalChances; } /** * 处理订单条件 */ private static function processConditionForOrder($condition, $orderInfo, $userId) { $chances = 0; switch ($condition->type) { case LotteryCondition::TYPE_GOODS: if ($condition->validateOrder($orderInfo)) { $chances = $condition->getRewardTimes(); } break; case LotteryCondition::TYPE_ORDER_AMOUNT: if ($condition->validateOrder($orderInfo)) { $chances = $condition->getRewardTimes(); // 如果可重复获得,根据金额倍数计算次数 if ($condition->canRepeat()) { $multiple = floor($orderInfo['total_amount'] / $condition->condition_value); $chances *= $multiple; } } break; case LotteryCondition::TYPE_ACCUMULATE: if ($condition->validateAccumulateCondition($userId, $condition->activity_id)) { // 检查是否已经因为累计消费获得过机会 if (!static::hasGrantedForAccumulate($condition->activity_id, $userId)) { $chances = $condition->getRewardTimes(); } } break; } return $chances; } /** * 处理充值条件 */ private static function processConditionForRecharge($condition, $rechargeInfo, $userId) { $chances = 0; if ($condition->validateOrder(['type' => 'recharge', 'amount' => $rechargeInfo['amount']])) { $chances = $condition->getRewardTimes(); // 如果可重复获得,根据金额倍数计算次数 if ($condition->canRepeat()) { $multiple = floor($rechargeInfo['amount'] / $condition->condition_value); $chances *= $multiple; } } return $chances; } /** * 检查用户资格 */ private static function checkUserQualification($activity, $userId) { // 检查用户群体限制 switch ($activity->user_limit_type) { case LotteryActivity::USER_LIMIT_ALL: return true; case LotteryActivity::USER_LIMIT_LEVEL: return static::checkUserLevel($userId, $activity->user_limit_value); case LotteryActivity::USER_LIMIT_TAG: return static::checkUserTag($userId, $activity->user_limit_value); default: return false; } } /** * 检查用户等级 */ private static function checkUserLevel($userId, $limitValue) { if (empty($limitValue)) { return true; } $user = \app\common\model\User::find($userId); return $user && in_array($user->level, (array)$limitValue); } /** * 检查用户标签 */ private static function checkUserTag($userId, $limitValue) { if (empty($limitValue)) { return true; } // 这里需要根据实际的用户标签系统实现 // 暂时返回true return true; } /** * 检查是否已因累计消费获得过机会 */ private static function hasGrantedForAccumulate($activityId, $userId) { $userChance = LotteryUserChance::getUserChance($activityId, $userId); if (!$userChance) { return false; } $getDetail = $userChance->get_detail_data; foreach ($getDetail as $detail) { if (($detail['trigger_type'] ?? '') === 'accumulate') { return true; } } return false; } /** * 给用户分发抽奖机会 */ private static function grantChanceToUser($activityId, $userId, $chances, $detail) { return LotteryUserChance::addChance($activityId, $userId, $chances, $detail); } /** * 手动给用户增加抽奖机会(管理员操作) * @param int $activityId 活动ID * @param int $userId 用户ID * @param int $chances 机会次数 * @param string $reason 原因 * @param int $adminId 管理员ID * @return bool */ public static function manualGrantChance($activityId, $userId, $chances, $reason, $adminId) { $activity = LotteryActivity::find($activityId); if (!$activity) { throw new Exception('活动不存在'); } $detail = [ 'trigger_type' => 'manual', 'reason' => $reason, 'admin_id' => $adminId, 'granted_time' => time() ]; return static::grantChanceToUser($activityId, $userId, $chances, $detail); } /** * 获取用户在指定活动中的抽奖机会详情 * @param int $activityId 活动ID * @param int $userId 用户ID * @return array */ public static function getUserChanceDetail($activityId, $userId) { $userChance = LotteryUserChance::getUserChance($activityId, $userId); if (!$userChance) { return [ 'total_chances' => 0, 'used_chances' => 0, 'remain_chances' => 0, 'get_detail' => [] ]; } return [ 'total_chances' => $userChance->total_chances, 'used_chances' => $userChance->used_chances, 'remain_chances' => $userChance->remain_chances, 'last_get_time' => $userChance->last_get_time, 'last_use_time' => $userChance->last_use_time, 'get_detail' => $userChance->get_detail_data ]; } /** * 批量初始化用户抽奖机会(活动开始时) * @param int $activityId 活动ID * @param array $userIds 用户ID数组 * @param int $initChances 初始机会数 * @return bool */ public static function batchInitChances($activityId, $userIds, $initChances = 1) { $activity = LotteryActivity::find($activityId); if (!$activity) { throw new Exception('活动不存在'); } return LotteryUserChance::batchCreateChances($activityId, $userIds, $initChances); } }