InfoHandler.php 11 KB

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