InfoHandler.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace App\Module\AppGame\Handler\Promotion;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  5. use App\Module\UrsPromotion\Services\UrsReferralService;
  6. use App\Module\UrsPromotion\Services\UrsProfitService;
  7. use App\Module\UrsPromotion\Services\UrsTalentService;
  8. use App\Module\User\Services\UserActivityService;
  9. use App\Module\Fund\Enums\FUND_TYPE;
  10. use Google\Protobuf\Internal\Message;
  11. use Illuminate\Support\Facades\Log;
  12. use Uraus\Kku\Request\RequestPromotionInfo;
  13. use Uraus\Kku\Response\ResponsePromotionInfo;
  14. use Uraus\Kku\Common\Reward;
  15. use Uraus\Kku\Common\RewardCoin;
  16. use Carbon\Carbon;
  17. /**
  18. * 处理推广团队信息请求
  19. *
  20. * 获取用户的推广团队统计信息,包括:
  21. * - 总人数、直推人数、间推人数
  22. * - 今日新增统计
  23. * - 团队活跃人数统计
  24. * - 达人等级信息
  25. */
  26. class InfoHandler extends BaseHandler
  27. {
  28. /**
  29. * 是否需要登录
  30. * @var bool
  31. */
  32. protected bool $need_login = true;
  33. /**
  34. * 处理推广团队信息请求
  35. *
  36. * @param RequestPromotionInfo $data 推广团队信息请求数据
  37. * @return ResponsePromotionInfo 推广团队信息响应
  38. */
  39. public function handle(Message $data): Message
  40. {
  41. // 创建响应对象
  42. $response = new ResponsePromotionInfo();
  43. try {
  44. // 更新用户活动时间
  45. $this->updateUserActivityTime();
  46. // 获取当前农场用户对应的URS用户ID
  47. $ursUserId = UrsUserMappingService::getUrsUserId($this->user_id);
  48. if (!$ursUserId) {
  49. // 用户未进入URS推广系统,返回空数据
  50. Log::info('用户未进入URS推广系统', [
  51. 'user_id' => $this->user_id
  52. ]);
  53. return $this->setEmptyResponse($response);
  54. }
  55. // 获取推广关系统计
  56. $referralStats = UrsReferralService::getReferralStats($ursUserId);
  57. // 获取今日统计数据
  58. $todayStats = $this->getTodayStats($ursUserId);
  59. // 获取活跃用户统计
  60. $activeStats = $this->getActiveStats($ursUserId);
  61. // 获取收益统计
  62. $rewardStats = $this->getRewardStats($ursUserId);
  63. // 获取达人等级信息
  64. $talentInfo = UrsTalentService::getTalentInfo($ursUserId);
  65. $starLevel = $talentInfo ? $talentInfo->talentLevel : 0;
  66. // 设置响应数据
  67. $response->setTotalCount($referralStats['total_team_count'] ?? 0);
  68. $response->setDirectCount($referralStats['direct_count'] ?? 0);
  69. $response->setIndirectCount($referralStats['indirect_count'] ?? 0);
  70. $response->setDayRecentCount($todayStats['team_new_count'] ?? 0);
  71. $response->setDayDirectCount($todayStats['direct_new_count'] ?? 0);
  72. $response->setActiveCount($activeStats['team_active_count'] ?? 0);
  73. $response->setDirectActiveCount($activeStats['direct_active_count'] ?? 0);
  74. $response->setStarLevel($starLevel);
  75. // 设置收益数据
  76. if ($rewardStats['today_reward']) {
  77. $response->setTodayReward($rewardStats['today_reward']);
  78. }
  79. if ($rewardStats['total_reward']) {
  80. $response->setTotalReward($rewardStats['total_reward']);
  81. }
  82. Log::info('推广团队信息获取成功', [
  83. 'user_id' => $this->user_id,
  84. 'urs_user_id' => $ursUserId,
  85. 'total_count' => $referralStats['total_team_count'] ?? 0,
  86. 'direct_count' => $referralStats['direct_count'] ?? 0
  87. ]);
  88. } catch (\Exception $e) {
  89. Log::error('获取推广团队信息失败', [
  90. 'user_id' => $this->user_id,
  91. 'error' => $e->getMessage(),
  92. 'trace' => $e->getTraceAsString()
  93. ]);
  94. // 发生错误时返回空数据
  95. return $this->setEmptyResponse($response);
  96. }
  97. return $response;
  98. }
  99. /**
  100. * 设置空响应数据
  101. *
  102. * @param ResponsePromotionInfo $response
  103. * @return ResponsePromotionInfo
  104. */
  105. private function setEmptyResponse(ResponsePromotionInfo $response): ResponsePromotionInfo
  106. {
  107. $response->setTotalCount(0);
  108. $response->setDirectCount(0);
  109. $response->setIndirectCount(0);
  110. $response->setDayRecentCount(0);
  111. $response->setDayDirectCount(0);
  112. $response->setActiveCount(0);
  113. $response->setDirectActiveCount(0);
  114. return $response;
  115. }
  116. /**
  117. * 获取今日统计数据
  118. *
  119. * @param int $ursUserId URS用户ID
  120. * @return array
  121. */
  122. private function getTodayStats(int $ursUserId): array
  123. {
  124. try {
  125. $teamMembers = UrsReferralService::getTeamMembers($ursUserId);
  126. $directNewCount = 0;
  127. $teamNewCount = 0;
  128. // 统计今日新增的直推用户
  129. if (!empty($teamMembers[1])) {
  130. foreach ($teamMembers[1] as $directMember) {
  131. $farmUserId = UrsUserMappingService::getFarmUserId($directMember);
  132. if ($farmUserId) {
  133. // 检查用户映射创建时间是否为今日
  134. $mapping = UrsUserMappingService::getMappingDetail($directMember);
  135. if ($mapping && $mapping->mappingTime &&
  136. Carbon::parse($mapping->mappingTime)->isToday()) {
  137. $directNewCount++;
  138. $teamNewCount++;
  139. }
  140. }
  141. }
  142. }
  143. // 统计今日新增的间推和三推用户
  144. foreach ([2, 3] as $level) {
  145. if (!empty($teamMembers[$level])) {
  146. foreach ($teamMembers[$level] as $member) {
  147. $farmUserId = UrsUserMappingService::getFarmUserId($member);
  148. if ($farmUserId) {
  149. $mapping = UrsUserMappingService::getMappingDetail($member);
  150. if ($mapping && $mapping->mappingTime &&
  151. Carbon::parse($mapping->mappingTime)->isToday()) {
  152. $teamNewCount++;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. return [
  159. 'direct_new_count' => $directNewCount,
  160. 'team_new_count' => $teamNewCount
  161. ];
  162. } catch (\Exception $e) {
  163. Log::error('获取今日统计数据失败', [
  164. 'urs_user_id' => $ursUserId,
  165. 'error' => $e->getMessage()
  166. ]);
  167. return [
  168. 'direct_new_count' => 0,
  169. 'team_new_count' => 0
  170. ];
  171. }
  172. }
  173. /**
  174. * 获取活跃用户统计
  175. *
  176. * @param int $ursUserId URS用户ID
  177. * @return array
  178. */
  179. private function getActiveStats(int $ursUserId): array
  180. {
  181. try {
  182. $teamMembers = UrsReferralService::getTeamMembers($ursUserId);
  183. $activeThreshold = Carbon::now()->subHours(24); // 24小时内活跃
  184. $directActiveCount = 0;
  185. $teamActiveCount = 0;
  186. // 统计直推活跃用户
  187. if (!empty($teamMembers[1])) {
  188. foreach ($teamMembers[1] as $directMember) {
  189. $farmUserId = UrsUserMappingService::getFarmUserId($directMember);
  190. if ($farmUserId) {
  191. $lastActivity = UserActivityService::getLastActivityTime($farmUserId);
  192. if ($lastActivity && $lastActivity->gt($activeThreshold)) {
  193. $directActiveCount++;
  194. $teamActiveCount++;
  195. }
  196. }
  197. }
  198. }
  199. // 统计间推和三推活跃用户
  200. foreach ([2, 3] as $level) {
  201. if (!empty($teamMembers[$level])) {
  202. foreach ($teamMembers[$level] as $member) {
  203. $farmUserId = UrsUserMappingService::getFarmUserId($member);
  204. if ($farmUserId) {
  205. $lastActivity = UserActivityService::getLastActivityTime($farmUserId);
  206. if ($lastActivity && $lastActivity->gt($activeThreshold)) {
  207. $teamActiveCount++;
  208. }
  209. }
  210. }
  211. }
  212. }
  213. return [
  214. 'direct_active_count' => $directActiveCount,
  215. 'team_active_count' => $teamActiveCount
  216. ];
  217. } catch (\Exception $e) {
  218. Log::error('获取活跃用户统计失败', [
  219. 'urs_user_id' => $ursUserId,
  220. 'error' => $e->getMessage()
  221. ]);
  222. return [
  223. 'direct_active_count' => 0,
  224. 'team_active_count' => 0
  225. ];
  226. }
  227. }
  228. /**
  229. * 获取收益统计
  230. *
  231. * @param int $ursUserId URS用户ID
  232. * @return array
  233. */
  234. private function getRewardStats(int $ursUserId): array
  235. {
  236. try {
  237. // 获取今日收益统计
  238. $today = Carbon::today()->format('Y-m-d');
  239. $tomorrow = Carbon::tomorrow()->format('Y-m-d');
  240. $todayStats = UrsProfitService::getUserProfitStats($ursUserId, null, $today, $tomorrow);
  241. // 获取总收益统计
  242. $totalStats = UrsProfitService::getUserProfitStats($ursUserId);
  243. // 构建今日收益Reward对象
  244. $todayReward = null;
  245. if ($todayStats['total_amount'] > 0) {
  246. $todayReward = $this->buildRewardFromStats($todayStats);
  247. }
  248. // 构建总收益Reward对象
  249. $totalReward = null;
  250. if ($totalStats['total_amount'] > 0) {
  251. $totalReward = $this->buildRewardFromStats($totalStats);
  252. }
  253. return [
  254. 'today_reward' => $todayReward,
  255. 'total_reward' => $totalReward
  256. ];
  257. } catch (\Exception $e) {
  258. Log::error('获取收益统计失败', [
  259. 'urs_user_id' => $ursUserId,
  260. 'error' => $e->getMessage()
  261. ]);
  262. return [
  263. 'today_reward' => null,
  264. 'total_reward' => null
  265. ];
  266. }
  267. }
  268. /**
  269. * 从统计数据构建Reward对象
  270. *
  271. * @param array $stats 统计数据
  272. * @return Reward
  273. */
  274. private function buildRewardFromStats(array $stats): Reward
  275. {
  276. $reward = new Reward();
  277. // 创建代币奖励列表
  278. $coins = [];
  279. // 推广收益和种植收益都以钻石形式显示
  280. if ($stats['total_amount'] > 0) {
  281. $rewardCoin = new RewardCoin();
  282. $rewardCoin->setType(FUND_TYPE::FUND2->value); // 钻石类型
  283. $rewardCoin->setQuantity((float)$stats['total_amount']);
  284. $coins[] = $rewardCoin;
  285. }
  286. $reward->setCoins($coins);
  287. // 其他奖励类型暂时为空
  288. $reward->setItems([]);
  289. $reward->setGods([]);
  290. $reward->setLands([]);
  291. $reward->setPets([]);
  292. $reward->setPetPowers([]);
  293. $reward->setSkins([]);
  294. return $reward;
  295. }
  296. }