| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- namespace App\Module\AppGame\Handler\Promotion;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use App\Module\UrsPromotion\Services\UrsReferralService;
- use App\Module\UrsPromotion\Services\UrsProfitService;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\User\Services\UserActivityService;
- use App\Module\Fund\Enums\FUND_TYPE;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Request\RequestPromotionInfo;
- use Uraus\Kku\Response\ResponsePromotionInfo;
- use Uraus\Kku\Common\Reward;
- use Uraus\Kku\Common\RewardCoin;
- use Carbon\Carbon;
- /**
- * 处理推广团队信息请求
- *
- * 获取用户的推广团队统计信息,包括:
- * - 总人数、直推人数、间推人数
- * - 今日新增统计
- * - 团队活跃人数统计
- * - 达人等级信息
- */
- class InfoHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理推广团队信息请求
- *
- * @param RequestPromotionInfo $data 推广团队信息请求数据
- * @return ResponsePromotionInfo 推广团队信息响应
- */
- public function handle(Message $data): Message
- {
- // 创建响应对象
- $response = new ResponsePromotionInfo();
- try {
- // 更新用户活动时间
- $this->updateUserActivityTime();
- // 获取当前农场用户对应的URS用户ID
- $ursUserId = UrsUserMappingService::getUrsUserId($this->user_id);
- if (!$ursUserId) {
- // 用户未进入URS推广系统,返回空数据
- Log::info('用户未进入URS推广系统', [
- 'user_id' => $this->user_id
- ]);
- return $this->setEmptyResponse($response);
- }
- // 获取推广关系统计
- $referralStats = UrsReferralService::getReferralStats($ursUserId);
-
- // 获取今日统计数据
- $todayStats = $this->getTodayStats($ursUserId);
-
- // 获取活跃用户统计
- $activeStats = $this->getActiveStats($ursUserId);
- // 获取收益统计
- $rewardStats = $this->getRewardStats($ursUserId);
- // 获取达人等级信息
- $talentInfo = UrsTalentService::getTalentInfo($ursUserId);
- $starLevel = $talentInfo ? $talentInfo->talentLevel : 0;
- // 设置响应数据
- $response->setTotalCount($referralStats['total_team_count'] ?? 0);
- $response->setDirectCount($referralStats['direct_count'] ?? 0);
- $response->setIndirectCount($referralStats['indirect_count'] ?? 0);
- $response->setDayRecentCount($todayStats['team_new_count'] ?? 0);
- $response->setDayDirectCount($todayStats['direct_new_count'] ?? 0);
- $response->setActiveCount($activeStats['team_active_count'] ?? 0);
- $response->setDirectActiveCount($activeStats['direct_active_count'] ?? 0);
- $response->setStarLevel($starLevel);
- // 设置收益数据
- if ($rewardStats['today_reward']) {
- $response->setTodayReward($rewardStats['today_reward']);
- }
- if ($rewardStats['total_reward']) {
- $response->setTotalReward($rewardStats['total_reward']);
- }
- Log::info('推广团队信息获取成功', [
- 'user_id' => $this->user_id,
- 'urs_user_id' => $ursUserId,
- 'total_count' => $referralStats['total_team_count'] ?? 0,
- 'direct_count' => $referralStats['direct_count'] ?? 0
- ]);
- } catch (\Exception $e) {
- Log::error('获取推广团队信息失败', [
- 'user_id' => $this->user_id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- // 发生错误时返回空数据
- return $this->setEmptyResponse($response);
- }
- return $response;
- }
- /**
- * 设置空响应数据
- *
- * @param ResponsePromotionInfo $response
- * @return ResponsePromotionInfo
- */
- private function setEmptyResponse(ResponsePromotionInfo $response): ResponsePromotionInfo
- {
- $response->setTotalCount(0);
- $response->setDirectCount(0);
- $response->setIndirectCount(0);
- $response->setDayRecentCount(0);
- $response->setDayDirectCount(0);
- $response->setActiveCount(0);
- $response->setDirectActiveCount(0);
-
- return $response;
- }
- /**
- * 获取今日统计数据
- *
- * @param int $ursUserId URS用户ID
- * @return array
- */
- private function getTodayStats(int $ursUserId): array
- {
- try {
- $teamMembers = UrsReferralService::getTeamMembers($ursUserId);
-
- $directNewCount = 0;
- $teamNewCount = 0;
-
- // 统计今日新增的直推用户
- if (!empty($teamMembers[1])) {
- foreach ($teamMembers[1] as $directMember) {
- $farmUserId = UrsUserMappingService::getFarmUserId($directMember);
- if ($farmUserId) {
- // 检查用户映射创建时间是否为今日
- $mapping = UrsUserMappingService::getMappingDetail($directMember);
- if ($mapping && $mapping->mappingTime &&
- Carbon::parse($mapping->mappingTime)->isToday()) {
- $directNewCount++;
- $teamNewCount++;
- }
- }
- }
- }
-
- // 统计今日新增的间推和三推用户
- foreach ([2, 3] as $level) {
- if (!empty($teamMembers[$level])) {
- foreach ($teamMembers[$level] as $member) {
- $farmUserId = UrsUserMappingService::getFarmUserId($member);
- if ($farmUserId) {
- $mapping = UrsUserMappingService::getMappingDetail($member);
- if ($mapping && $mapping->mappingTime &&
- Carbon::parse($mapping->mappingTime)->isToday()) {
- $teamNewCount++;
- }
- }
- }
- }
- }
-
- return [
- 'direct_new_count' => $directNewCount,
- 'team_new_count' => $teamNewCount
- ];
-
- } catch (\Exception $e) {
- Log::error('获取今日统计数据失败', [
- 'urs_user_id' => $ursUserId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'direct_new_count' => 0,
- 'team_new_count' => 0
- ];
- }
- }
- /**
- * 获取活跃用户统计
- *
- * @param int $ursUserId URS用户ID
- * @return array
- */
- private function getActiveStats(int $ursUserId): array
- {
- try {
- $teamMembers = UrsReferralService::getTeamMembers($ursUserId);
- $activeThreshold = Carbon::now()->subHours(24); // 24小时内活跃
- $directActiveCount = 0;
- $teamActiveCount = 0;
- // 统计直推活跃用户
- if (!empty($teamMembers[1])) {
- foreach ($teamMembers[1] as $directMember) {
- $farmUserId = UrsUserMappingService::getFarmUserId($directMember);
- if ($farmUserId) {
- $lastActivity = UserActivityService::getLastActivityTime($farmUserId);
- if ($lastActivity && $lastActivity->gt($activeThreshold)) {
- $directActiveCount++;
- $teamActiveCount++;
- }
- }
- }
- }
- // 统计间推和三推活跃用户
- foreach ([2, 3] as $level) {
- if (!empty($teamMembers[$level])) {
- foreach ($teamMembers[$level] as $member) {
- $farmUserId = UrsUserMappingService::getFarmUserId($member);
- if ($farmUserId) {
- $lastActivity = UserActivityService::getLastActivityTime($farmUserId);
- if ($lastActivity && $lastActivity->gt($activeThreshold)) {
- $teamActiveCount++;
- }
- }
- }
- }
- }
- return [
- 'direct_active_count' => $directActiveCount,
- 'team_active_count' => $teamActiveCount
- ];
- } catch (\Exception $e) {
- Log::error('获取活跃用户统计失败', [
- 'urs_user_id' => $ursUserId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'direct_active_count' => 0,
- 'team_active_count' => 0
- ];
- }
- }
- /**
- * 获取收益统计
- *
- * @param int $ursUserId URS用户ID
- * @return array
- */
- private function getRewardStats(int $ursUserId): array
- {
- try {
- // 获取今日收益统计
- $today = Carbon::today()->format('Y-m-d');
- $tomorrow = Carbon::tomorrow()->format('Y-m-d');
- $todayStats = UrsProfitService::getUserProfitStats($ursUserId, null, $today, $tomorrow);
- // 获取总收益统计
- $totalStats = UrsProfitService::getUserProfitStats($ursUserId);
- // 构建今日收益Reward对象
- $todayReward = null;
- if ($todayStats['total_amount'] > 0) {
- $todayReward = $this->buildRewardFromStats($todayStats);
- }
- // 构建总收益Reward对象
- $totalReward = null;
- if ($totalStats['total_amount'] > 0) {
- $totalReward = $this->buildRewardFromStats($totalStats);
- }
- return [
- 'today_reward' => $todayReward,
- 'total_reward' => $totalReward
- ];
- } catch (\Exception $e) {
- Log::error('获取收益统计失败', [
- 'urs_user_id' => $ursUserId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'today_reward' => null,
- 'total_reward' => null
- ];
- }
- }
- /**
- * 从统计数据构建Reward对象
- *
- * @param array $stats 统计数据
- * @return Reward
- */
- private function buildRewardFromStats(array $stats): Reward
- {
- $reward = new Reward();
- // 创建代币奖励列表
- $coins = [];
- // 推广收益和种植收益都以钻石形式显示
- if ($stats['total_amount'] > 0) {
- $rewardCoin = new RewardCoin();
- $rewardCoin->setType(FUND_TYPE::FUND2->value); // 钻石类型
- $rewardCoin->setQuantity((float)$stats['total_amount']);
- $coins[] = $rewardCoin;
- }
- $reward->setCoins($coins);
- // 其他奖励类型暂时为空
- $reward->setItems([]);
- $reward->setGods([]);
- $reward->setLands([]);
- $reward->setPets([]);
- $reward->setPetPowers([]);
- $reward->setSkins([]);
- return $reward;
- }
- }
|