123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038 |
- <?php
- namespace app\common\Service\lottery;
- use app\common\model\lottery\LotteryActivity;
- use app\common\model\lottery\LotteryCondition;
- use app\common\model\lottery\LotteryUserChance;
- use app\common\model\lottery\LotteryDrawRecord;
- use app\common\model\User;
- use app\common\model\Order;
- use app\common\Enum\LotteryEnum;
- use think\Exception;
- use think\Db;
- /**
- * 抽奖机会服务类
- * 处理用户获得抽奖机会的逻辑
- *
- * 主要功能:
- * - 订单/充值后自动分发抽奖机会
- * - 条件验证和机会计算
- * - 用户机会管理
- * - 批量操作和统计
- */
- class LotteryChanceService
- {
- // ============ 常量定义 ============
-
- /** @var int 默认批量处理数量 */
- const DEFAULT_BATCH_SIZE = 100;
-
- /** @var int 最大批量处理数量 */
- const MAX_BATCH_SIZE = 1000;
-
- /** @var string 触发类型:订单 */
- const TRIGGER_TYPE_ORDER = 'order';
-
- /** @var string 触发类型:充值 */
- const TRIGGER_TYPE_RECHARGE = 'recharge';
-
- /** @var string 触发类型:累计消费 */
- const TRIGGER_TYPE_ACCUMULATE = 'accumulate';
-
- /** @var string 触发类型:手动 */
- const TRIGGER_TYPE_MANUAL = 'manual';
- // ============ 主要业务方法 ============
- /**
- * 订单完成后检查并分发抽奖机会
- *
- * @param array $orderInfo 订单信息 ['id', 'total_amount', 'goods' => [['goods_id']]]
- * @param int $userId 用户ID
- * @return array 获得的抽奖机会信息
- * @throws Exception
- */
- public static function checkAndGrantChanceForOrder($orderInfo, $userId)
- {
- if (empty($orderInfo) || empty($userId)) {
- throw new Exception('订单信息或用户ID不能为空');
- }
- $grantedChances = [];
-
- try {
- // 获取所有正在进行的抽奖活动
- $activities = LotteryService::getRunningActivities();
-
- foreach ($activities as $activity) {
- try {
- $chances = static::processActivityForOrder($activity, $orderInfo, $userId);
- if ($chances > 0) {
- $grantedChances[] = [
- 'activity_id' => $activity->id,
- 'activity_name' => $activity->name,
- 'chances' => $chances,
- 'granted_time' => time()
- ];
- }
- } catch (Exception $e) {
- // 记录错误但不影响其他活动的处理
- trace("抽奖机会分发失败 - 活动ID: {$activity->id}, 用户ID: {$userId}, 错误: " . $e->getMessage(), 'error');
- }
- }
- } catch (Exception $e) {
- trace("订单抽奖机会分发失败 - 用户ID: {$userId}, 错误: " . $e->getMessage(), 'error');
- throw $e;
- }
-
- return $grantedChances;
- }
- /**
- * 充值完成后检查并分发抽奖机会
- *
- * @param array $rechargeInfo 充值信息 ['amount', 'type' => 'recharge']
- * @param int $userId 用户ID
- * @return array 获得的抽奖机会信息
- * @throws Exception
- */
- public static function checkAndGrantChanceForRecharge($rechargeInfo, $userId)
- {
- if (empty($rechargeInfo) || empty($userId)) {
- throw new Exception('充值信息或用户ID不能为空');
- }
- $grantedChances = [];
-
- try {
- // 获取所有正在进行的抽奖活动
- $activities = LotteryService::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,
- 'granted_time' => time()
- ];
- }
- } catch (Exception $e) {
- // 记录错误但不影响其他活动的处理
- trace("充值抽奖机会分发失败 - 活动ID: {$activity->id}, 用户ID: {$userId}, 错误: " . $e->getMessage(), 'error');
- }
- }
- } catch (Exception $e) {
- trace("充值抽奖机会分发失败 - 用户ID: {$userId}, 错误: " . $e->getMessage(), 'error');
- throw $e;
- }
-
- return $grantedChances;
- }
- /**
- * 手动给用户增加抽奖机会(管理员操作)
- *
- * @param int $activityId 活动ID
- * @param int $userId 用户ID
- * @param int $chances 机会次数
- * @param string $reason 原因
- * @param int $adminId 管理员ID
- * @return bool
- * @throws Exception
- */
- public static function manualGrantChance($activityId, $userId, $chances, $reason = '', $adminId = 0)
- {
- if ($chances <= 0) {
- throw new Exception('抽奖机会数量必须大于0');
- }
- $activity = LotteryActivity::find($activityId);
- if (!$activity) {
- throw new Exception('活动不存在');
- }
- $user = User::find($userId);
- if (!$user) {
- throw new Exception('用户不存在');
- }
- $detail = [
- 'trigger_type' => static::TRIGGER_TYPE_MANUAL,
- 'reason' => $reason,
- 'admin_id' => $adminId,
- 'granted_time' => time()
- ];
- return static::grantChanceToUser($activityId, $userId, $chances, $detail);
- }
- /**
- * 批量初始化用户抽奖机会(活动开始时)
- *
- * @param int $activityId 活动ID
- * @param array $userIds 用户ID数组
- * @param int $initChances 初始机会数
- * @param int $batchSize 批量处理大小
- * @return array 处理结果
- * @throws Exception
- */
- public static function batchInitChances($activityId, $userIds, $initChances = 1, $batchSize = null)
- {
- if (empty($userIds)) {
- throw new Exception('用户ID列表不能为空');
- }
- $activity = LotteryActivity::find($activityId);
- if (!$activity) {
- throw new Exception('活动不存在');
- }
- $batchSize = $batchSize ?: static::DEFAULT_BATCH_SIZE;
- $batchSize = min($batchSize, static::MAX_BATCH_SIZE);
- $result = [
- 'total_users' => count($userIds),
- 'success_count' => 0,
- 'failed_count' => 0,
- 'errors' => []
- ];
- // 分批处理
- $batches = array_chunk($userIds, $batchSize);
-
- foreach ($batches as $batch) {
- try {
- $success = static::batchCreateChances($activityId, $batch, $initChances);
- if ($success) {
- $result['success_count'] += count($batch);
- } else {
- $result['failed_count'] += count($batch);
- $result['errors'][] = "批次处理失败: " . implode(',', $batch);
- }
- } catch (Exception $e) {
- $result['failed_count'] += count($batch);
- $result['errors'][] = "批次处理异常: " . $e->getMessage();
- }
- }
- return $result;
- }
- // ============ 私有处理方法 ============
- /**
- * 处理订单相关的活动
- *
- * @param LotteryActivity $activity 活动对象
- * @param array $orderInfo 订单信息
- * @param int $userId 用户ID
- * @return int 获得的抽奖机会数量
- */
- private static function processActivityForOrder($activity, $orderInfo, $userId)
- {
- // 检查用户是否符合活动参与资格
- if (!static::checkUserQualification($activity, $userId)) {
- return 0;
- }
- // 获取活动的参与条件
- $conditions = static::getValidConditions($activity->id);
- $totalChances = 0;
- foreach ($conditions as $condition) {
- // 跳过充值条件
- if ($condition->type == LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
- continue;
- }
- $chances = static::processConditionForOrder($condition, $orderInfo, $userId);
- $totalChances += $chances;
- }
- // 如果获得了抽奖机会,记录到数据库
- if ($totalChances > 0) {
- static::grantChanceToUser($activity->id, $userId, $totalChances, [
- 'trigger_type' => static::TRIGGER_TYPE_ORDER,
- 'order_id' => $orderInfo['id'] ?? 0,
- 'order_amount' => $orderInfo['total_amount'] ?? 0,
- 'granted_time' => time()
- ]);
- }
- return $totalChances;
- }
- /**
- * 处理充值相关的活动
- *
- * @param LotteryActivity $activity 活动对象
- * @param array $rechargeInfo 充值信息
- * @param int $userId 用户ID
- * @return int 获得的抽奖机会数量
- */
- private static function processActivityForRecharge($activity, $rechargeInfo, $userId)
- {
- // 检查用户是否符合活动参与资格
- if (!static::checkUserQualification($activity, $userId)) {
- return 0;
- }
- // 获取活动的参与条件
- $conditions = static::getValidConditions($activity->id);
- $totalChances = 0;
- foreach ($conditions as $condition) {
- // 只处理充值条件
- if ($condition->type != LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
- continue;
- }
- $chances = static::processConditionForRecharge($condition, $rechargeInfo, $userId);
- $totalChances += $chances;
- }
- // 如果获得了抽奖机会,记录到数据库
- if ($totalChances > 0) {
- static::grantChanceToUser($activity->id, $userId, $totalChances, [
- 'trigger_type' => static::TRIGGER_TYPE_RECHARGE,
- 'recharge_amount' => $rechargeInfo['amount'] ?? 0,
- 'granted_time' => time()
- ]);
- }
- return $totalChances;
- }
- /**
- * 处理订单条件
- *
- * @param LotteryCondition $condition 条件对象
- * @param array $orderInfo 订单信息
- * @param int $userId 用户ID
- * @return int 获得的抽奖机会数量
- */
- private static function processConditionForOrder($condition, $orderInfo, $userId)
- {
- $chances = 0;
- switch ($condition->type) {
- case LotteryEnum::CONDITION_TYPE_BUY_GOODS:
- if (static::validateGoodsCondition($condition, $orderInfo)) {
- $chances = static::getRewardTimes($condition);
- }
- break;
- case LotteryEnum::CONDITION_TYPE_ORDER_AMOUNT:
- if (static::validateOrderAmountCondition($condition, $orderInfo)) {
- $chances = static::getRewardTimes($condition);
-
- // 如果可重复获得,根据金额倍数计算次数
- if (static::canRepeat($condition)) {
- $multiple = floor($orderInfo['total_amount'] / $condition->condition_value);
- $chances *= $multiple;
- }
- }
- break;
- case LotteryEnum::CONDITION_TYPE_TOTAL_AMOUNT:
- if (static::validateAccumulateCondition($condition, $userId, $condition->activity_id)) {
- // 检查是否已经因为累计消费获得过机会
- if (!static::hasGrantedForAccumulate($condition->activity_id, $userId)) {
- $chances = static::getRewardTimes($condition);
- }
- }
- break;
- }
- return $chances;
- }
- /**
- * 处理充值条件
- *
- * @param LotteryCondition $condition 条件对象
- * @param array $rechargeInfo 充值信息
- * @param int $userId 用户ID
- * @return int 获得的抽奖机会数量
- */
- private static function processConditionForRecharge($condition, $rechargeInfo, $userId)
- {
- $chances = 0;
-
- if (static::validateRechargeCondition($condition, ['type' => 'recharge', 'amount' => $rechargeInfo['amount']])) {
- $chances = static::getRewardTimes($condition);
-
- // 如果可重复获得,根据金额倍数计算次数
- if (static::canRepeat($condition)) {
- $multiple = floor($rechargeInfo['amount'] / $condition->condition_value);
- $chances *= $multiple;
- }
- }
- return $chances;
- }
- // ============ 用户资格检查方法 ============
- /**
- * 检查用户资格
- *
- * @param LotteryActivity $activity 活动对象
- * @param int $userId 用户ID
- * @return bool
- */
- private static function checkUserQualification($activity, $userId)
- {
- // 检查用户群体限制
- switch ($activity->user_limit_type) {
- case LotteryEnum::USER_LIMIT_ALL:
- return true;
- case LotteryEnum::USER_LIMIT_LEVEL:
- return static::checkUserLevel($userId, $activity->user_limit_value);
- case LotteryEnum::USER_LIMIT_TAG:
- return static::checkUserTag($userId, $activity->user_limit_value);
- default:
- return false;
- }
- }
- /**
- * 检查用户等级
- *
- * @param int $userId 用户ID
- * @param mixed $limitValue 限制值
- * @return bool
- */
- private static function checkUserLevel($userId, $limitValue)
- {
- if (empty($limitValue)) {
- return true;
- }
-
- $user = User::find($userId);
- if (!$user) {
- return false;
- }
-
- $limitLevels = is_array($limitValue) ? $limitValue : [$limitValue];
- return in_array($user->level, $limitLevels);
- }
- /**
- * 检查用户标签
- *
- * @param int $userId 用户ID
- * @param mixed $limitValue 限制值
- * @return bool
- */
- private static function checkUserTag($userId, $limitValue)
- {
- if (empty($limitValue)) {
- return true;
- }
-
- // TODO: 根据实际的用户标签系统实现
- // 这里需要根据具体的用户标签表结构来实现
- // 暂时返回true,避免影响现有功能
-
- return true;
- }
- /**
- * 检查是否已因累计消费获得过机会
- *
- * @param int $activityId 活动ID
- * @param int $userId 用户ID
- * @return bool
- */
- private static function hasGrantedForAccumulate($activityId, $userId)
- {
- $userChance = static::getUserChance($activityId, $userId);
- if (!$userChance) {
- return false;
- }
-
- $getDetail = $userChance->get_detail_data;
- foreach ($getDetail as $detail) {
- if (($detail['trigger_type'] ?? '') === static::TRIGGER_TYPE_ACCUMULATE) {
- return true;
- }
- }
-
- return false;
- }
- // ============ 条件验证方法 ============
- /**
- * 验证订单是否满足条件
- *
- * @param LotteryCondition $condition 条件对象
- * @param array $orderInfo 订单信息
- * @return bool
- */
- public static function validateOrder(LotteryCondition $condition, $orderInfo)
- {
- switch ($condition->type) {
- case LotteryEnum::CONDITION_TYPE_BUY_GOODS:
- return static::validateGoodsCondition($condition, $orderInfo);
- case LotteryEnum::CONDITION_TYPE_ORDER_AMOUNT:
- return static::validateOrderAmountCondition($condition, $orderInfo);
- case LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT:
- return static::validateRechargeCondition($condition, $orderInfo);
- default:
- return false;
- }
- }
- /**
- * 验证商品条件
- *
- * @param LotteryCondition $condition 条件对象
- * @param array $orderInfo 订单信息
- * @return bool
- */
- public static function validateGoodsCondition(LotteryCondition $condition, $orderInfo)
- {
- if (empty($orderInfo['goods']) || empty($condition->goods_ids_list)) {
- return false;
- }
- $orderGoodsIds = array_column($orderInfo['goods'], 'goods_id');
- $conditionGoodsIds = $condition->goods_ids_list;
- $intersection = array_intersect($orderGoodsIds, $conditionGoodsIds);
- if ($condition->goods_rule == LotteryEnum::GOODS_RULE_INCLUDE) {
- // 指定商品参与:订单中必须包含指定商品
- return !empty($intersection);
- } else {
- // 指定商品不可参与:订单中不能包含指定商品
- return empty($intersection);
- }
- }
- /**
- * 验证订单金额条件
- *
- * @param LotteryCondition $condition 条件对象
- * @param array $orderInfo 订单信息
- * @return bool
- */
- public static function validateOrderAmountCondition(LotteryCondition $condition, $orderInfo)
- {
- $orderAmount = $orderInfo['total_amount'] ?? 0;
- return $orderAmount >= $condition->condition_value;
- }
- /**
- * 验证充值条件
- *
- * @param LotteryCondition $condition 条件对象
- * @param array $orderInfo 订单信息
- * @return bool
- */
- public static function validateRechargeCondition(LotteryCondition $condition, $orderInfo)
- {
- if (($orderInfo['type'] ?? '') !== 'recharge') {
- return false;
- }
-
- $rechargeAmount = $orderInfo['amount'] ?? 0;
- return $rechargeAmount >= $condition->condition_value;
- }
- /**
- * 验证累计消费条件
- *
- * @param LotteryCondition $condition 条件对象
- * @param int $userId 用户ID
- * @param int $activityId 活动ID
- * @return bool
- */
- public static function validateAccumulateCondition(LotteryCondition $condition, $userId, $activityId)
- {
- $activity = LotteryActivity::find($activityId);
- if (!$activity) {
- return false;
- }
- // 计算活动期间用户累计消费
- $totalAmount = Order::where('user_id', $userId)
- ->where('status', 'paid')
- ->where('createtime', '>=', $activity->start_time)
- ->where('createtime', '<=', $activity->end_time)
- ->sum('total_amount');
- return $totalAmount >= $condition->condition_value;
- }
- // ============ 条件管理方法 ============
- /**
- * 获取活动的有效条件
- *
- * @param int $activityId 活动ID
- * @return array
- */
- public static function getValidConditions($activityId)
- {
- return LotteryCondition::where('activity_id', $activityId)
- ->where('status', 1)
- ->order('id', 'asc')
- ->select();
- }
- /**
- * 检查条件是否可重复获得奖励
- *
- * @param LotteryCondition $condition 条件对象
- * @return bool
- */
- public static function canRepeat(LotteryCondition $condition)
- {
- return $condition->is_repeatable == 1;
- }
- /**
- * 获取奖励次数
- *
- * @param LotteryCondition $condition 条件对象
- * @return int
- */
- public static function getRewardTimes(LotteryCondition $condition)
- {
- return $condition->reward_times ?: 1;
- }
- /**
- * 批量验证订单条件
- *
- * @param int $activityId 活动ID
- * @param array $orderInfo 订单信息
- * @param int $userId 用户ID
- * @return array 满足的条件列表
- */
- public static function batchValidateConditions($activityId, $orderInfo, $userId)
- {
- $conditions = static::getValidConditions($activityId);
- $validConditions = [];
- foreach ($conditions as $condition) {
- if (static::validateOrder($condition, $orderInfo)) {
- $validConditions[] = [
- 'condition_id' => $condition->id,
- 'condition_name' => $condition->name,
- 'condition_type' => $condition->type,
- 'condition_type_text' => $condition->type_text,
- 'reward_times' => static::getRewardTimes($condition),
- 'can_repeat' => static::canRepeat($condition)
- ];
- }
- }
- return $validConditions;
- }
- /**
- * 获取条件统计信息
- *
- * @param int $activityId 活动ID
- * @return array
- */
- public static function getConditionStatistics($activityId)
- {
- $conditions = static::getValidConditions($activityId);
- $statistics = [
- 'total_conditions' => count($conditions),
- 'goods_conditions' => 0,
- 'amount_conditions' => 0,
- 'recharge_conditions' => 0,
- 'accumulate_conditions' => 0
- ];
- foreach ($conditions as $condition) {
- switch ($condition->type) {
- case LotteryEnum::CONDITION_TYPE_BUY_GOODS:
- $statistics['goods_conditions']++;
- break;
- case LotteryEnum::CONDITION_TYPE_ORDER_AMOUNT:
- $statistics['amount_conditions']++;
- break;
- case LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT:
- $statistics['recharge_conditions']++;
- break;
- case LotteryEnum::CONDITION_TYPE_TOTAL_AMOUNT:
- $statistics['accumulate_conditions']++;
- break;
- }
- }
- return $statistics;
- }
- // ============ 用户机会管理方法 ============
- /**
- * 获取用户抽奖机会
- *
- * @param int $activityId 活动ID
- * @param int $userId 用户ID
- * @return LotteryUserChance|null
- */
- public static function getUserChance($activityId, $userId)
- {
- return LotteryUserChance::where('activity_id', $activityId)
- ->where('user_id', $userId)
- ->find();
- }
- /**
- * 获取用户在指定活动中的抽奖机会详情
- *
- * @param int $activityId 活动ID
- * @param int $userId 用户ID
- * @return array
- */
- public static function getUserChanceDetail($activityId, $userId)
- {
- $userChance = static::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 int $userId 用户ID
- * @param int $chances 机会次数
- * @param array $detail 获得详情
- * @return LotteryUserChance|bool
- */
- private static function grantChanceToUser($activityId, $userId, $chances, $detail)
- {
- return static::addChance($activityId, $userId, $chances, $detail);
- }
- /**
- * 增加抽奖机会
- *
- * @param int $activityId 活动ID
- * @param int $userId 用户ID
- * @param int $times 机会次数
- * @param array $detail 获得详情
- * @return LotteryUserChance|bool
- */
- public static function addChance($activityId, $userId, $times = 1, $detail = [])
- {
- if ($times <= 0) {
- return false;
- }
- $chance = static::getUserChance($activityId, $userId);
-
- if (!$chance) {
- // 创建新记录
- $data = [
- 'activity_id' => $activityId,
- 'user_id' => $userId,
- 'total_chances' => $times,
- 'used_chances' => 0,
- 'remain_chances' => $times,
- 'last_get_time' => time(),
- 'get_detail' => json_encode([$detail])
- ];
- return LotteryUserChance::create($data);
- } else {
- // 更新现有记录
- $chance->total_chances += $times;
- $chance->remain_chances += $times;
- $chance->last_get_time = time();
-
- // 更新获得详情
- $getDetail = $chance->get_detail_data;
- $getDetail[] = $detail;
- $chance->get_detail = json_encode($getDetail);
-
- return $chance->save() ? $chance : false;
- }
- }
- /**
- * 使用抽奖机会
- *
- * @param LotteryUserChance $userChance 用户机会对象
- * @param int $times 使用次数
- * @return bool
- */
- public static function useChance(LotteryUserChance $userChance, $times = 1)
- {
- if ($userChance->remain_chances < $times) {
- return false;
- }
-
- $userChance->used_chances += $times;
- $userChance->remain_chances -= $times;
- $userChance->last_use_time = time();
-
- return $userChance->save();
- }
- /**
- * 检查是否有剩余机会
- *
- * @param LotteryUserChance $userChance 用户机会对象
- * @param int $times 需要的机会次数
- * @return bool
- */
- public static function hasChance(LotteryUserChance $userChance, $times = 1)
- {
- return $userChance->remain_chances >= $times;
- }
- /**
- * 重置抽奖机会(用于测试或特殊情况)
- *
- * @param LotteryUserChance $userChance 用户机会对象
- * @return bool
- */
- public static function resetChance(LotteryUserChance $userChance)
- {
- $userChance->used_chances = 0;
- $userChance->remain_chances = $userChance->total_chances;
- return $userChance->save();
- }
- // ============ 统计和查询方法 ============
- /**
- * 获取活动总参与人数
- *
- * @param int $activityId 活动ID
- * @return int
- */
- public static function getActivityParticipants($activityId)
- {
- return LotteryUserChance::where('activity_id', $activityId)->count();
- }
- /**
- * 获取用户在多个活动中的机会统计
- *
- * @param int $userId 用户ID
- * @param array $activityIds 活动ID数组
- * @return array
- */
- public static function getUserChancesStats($userId, $activityIds = [])
- {
- $query = LotteryUserChance::where('user_id', $userId);
-
- if (!empty($activityIds)) {
- $query->where('activity_id', 'in', $activityIds);
- }
-
- return $query->field([
- 'activity_id',
- 'total_chances',
- 'used_chances',
- 'remain_chances'
- ])
- ->select();
- }
- /**
- * 获取用户在所有活动中的机会概览
- *
- * @param int $userId 用户ID
- * @return array
- */
- public static function getUserAllChancesOverview($userId)
- {
- $chances = LotteryUserChance::where('user_id', $userId)
- ->with(['activity'])
- ->select();
- $overview = [
- 'total_activities' => count($chances),
- 'total_chances' => 0,
- 'total_used' => 0,
- 'total_remain' => 0,
- 'activities' => []
- ];
- foreach ($chances as $chance) {
- $overview['total_chances'] += $chance->total_chances;
- $overview['total_used'] += $chance->used_chances;
- $overview['total_remain'] += $chance->remain_chances;
- $overview['activities'][] = [
- 'activity_id' => $chance->activity_id,
- 'activity_name' => $chance->activity->name ?? '',
- 'total_chances' => $chance->total_chances,
- 'used_chances' => $chance->used_chances,
- 'remain_chances' => $chance->remain_chances,
- 'last_get_time' => $chance->last_get_time,
- 'last_use_time' => $chance->last_use_time
- ];
- }
- return $overview;
- }
- /**
- * 检查用户是否可以参与活动
- *
- * @param int $activityId 活动ID
- * @param int $userId 用户ID
- * @return bool
- */
- public static function canUserParticipate($activityId, $userId)
- {
- $userChance = static::getUserChance($activityId, $userId);
- return $userChance && static::hasChance($userChance, 1);
- }
- /**
- * 获取活动参与用户列表
- *
- * @param int $activityId 活动ID
- * @param int $page 页码
- * @param int $limit 每页数量
- * @return array
- */
- public static function getActivityParticipantsList($activityId, $page = 1, $limit = 20)
- {
- return LotteryUserChance::where('activity_id', $activityId)
- ->with(['user'])
- ->order('createtime', 'desc')
- ->page($page, $limit)
- ->select();
- }
- /**
- * 获取活动参与统计
- *
- * @param int $activityId 活动ID
- * @return array
- */
- public static function getActivityParticipationStats($activityId)
- {
- $stats = LotteryUserChance::where('activity_id', $activityId)
- ->field([
- 'COUNT(*) as total_participants',
- 'SUM(total_chances) as total_chances_granted',
- 'SUM(used_chances) as total_chances_used',
- 'SUM(remain_chances) as total_chances_remain'
- ])
- ->find();
- return [
- 'total_participants' => $stats['total_participants'] ?? 0,
- 'total_chances_granted' => $stats['total_chances_granted'] ?? 0,
- 'total_chances_used' => $stats['total_chances_used'] ?? 0,
- 'total_chances_remain' => $stats['total_chances_remain'] ?? 0,
- 'usage_rate' => $stats['total_chances_granted'] > 0 ?
- round(($stats['total_chances_used'] / $stats['total_chances_granted']) * 100, 2) : 0
- ];
- }
- // ============ 批量操作方法 ============
- /**
- * 批量创建用户机会(用于活动启动时)
- *
- * @param int $activityId 活动ID
- * @param array $userIds 用户ID数组
- * @param int $times 机会次数
- * @return bool
- */
- public static function batchCreateChances($activityId, $userIds, $times = 1)
- {
- if (empty($userIds)) {
- return false;
- }
- $data = [];
- $now = time();
-
- foreach ($userIds as $userId) {
- $data[] = [
- 'activity_id' => $activityId,
- 'user_id' => $userId,
- 'total_chances' => $times,
- 'used_chances' => 0,
- 'remain_chances' => $times,
- 'last_get_time' => $now,
- 'createtime' => $now,
- 'updatetime' => $now
- ];
- }
-
- return LotteryUserChance::insertAll($data);
- }
- /**
- * 批量检查用户资格
- *
- * @param int $activityId 活动ID
- * @param array $userIds 用户ID数组
- * @return array 符合条件的用户ID数组
- */
- public static function batchCheckUserQualification($activityId, $userIds)
- {
- $activity = LotteryActivity::find($activityId);
- if (!$activity) {
- return [];
- }
- $qualifiedUsers = [];
-
- foreach ($userIds as $userId) {
- if (static::checkUserQualification($activity, $userId)) {
- $qualifiedUsers[] = $userId;
- }
- }
- return $qualifiedUsers;
- }
- /**
- * 批量验证条件
- *
- * @param int $activityId 活动ID
- * @param array $orderInfo 订单信息
- * @param array $userIds 用户ID数组
- * @return array 每个用户满足的条件
- */
- public static function batchValidateConditionsForUsers($activityId, $orderInfo, $userIds)
- {
- $results = [];
-
- foreach ($userIds as $userId) {
- $results[$userId] = static::batchValidateConditions($activityId, $orderInfo, $userId);
- }
-
- return $results;
- }
- }
|