|
|
@@ -5,12 +5,16 @@ 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 App\Module\Game\Enums\REWARD_SOURCE_TYPE;
|
|
|
+use App\Module\Game\Models\GameRewardLog;
|
|
|
+use App\Module\Fund\Models\FundLogModel;
|
|
|
+use App\Module\GameItems\Models\ItemTransactionLog;
|
|
|
use Google\Protobuf\Internal\Message;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
use Uraus\Kku\Request\RequestPromotionInfo;
|
|
|
use Uraus\Kku\Response\ResponsePromotionInfo;
|
|
|
use Uraus\Kku\Common\Reward;
|
|
|
@@ -268,13 +272,23 @@ class InfoHandler extends BaseHandler
|
|
|
private function getRewardStats(int $ursUserId): array
|
|
|
{
|
|
|
try {
|
|
|
+ // 获取对应的农场用户ID
|
|
|
+ $farmUserId = UrsUserMappingService::getFarmUserId($ursUserId);
|
|
|
+ if (!$farmUserId) {
|
|
|
+ Log::info('URS用户未映射到农场用户', ['urs_user_id' => $ursUserId]);
|
|
|
+ return [
|
|
|
+ 'today_reward' => null,
|
|
|
+ 'total_reward' => null
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
// 获取今日收益统计
|
|
|
$today = Carbon::today()->format('Y-m-d');
|
|
|
$tomorrow = Carbon::tomorrow()->format('Y-m-d');
|
|
|
- $todayStats = UrsProfitService::getUserProfitStats($ursUserId, null, $today, $tomorrow);
|
|
|
+ $todayStats = $this->getRewardStatsFromLogs($farmUserId, $today, $tomorrow);
|
|
|
|
|
|
// 获取总收益统计
|
|
|
- $totalStats = UrsProfitService::getUserProfitStats($ursUserId);
|
|
|
+ $totalStats = $this->getRewardStatsFromLogs($farmUserId);
|
|
|
|
|
|
// 构建今日收益Reward对象
|
|
|
$todayReward = null;
|
|
|
@@ -306,6 +320,146 @@ class InfoHandler extends BaseHandler
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 从物品模块和资金模块的日志中获取收益统计
|
|
|
+ *
|
|
|
+ * @param int $farmUserId 农场用户ID
|
|
|
+ * @param string|null $startDate 开始日期
|
|
|
+ * @param string|null $endDate 结束日期
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function getRewardStatsFromLogs(int $farmUserId, ?string $startDate = null, ?string $endDate = null): array
|
|
|
+ {
|
|
|
+ $stats = [
|
|
|
+ 'total_amount' => 0,
|
|
|
+ 'total_count' => 0,
|
|
|
+ 'by_source_type' => []
|
|
|
+ ];
|
|
|
+
|
|
|
+ try {
|
|
|
+ // URS推广相关的收益来源类型
|
|
|
+ $ursPromotionSourceTypes = [
|
|
|
+ REWARD_SOURCE_TYPE::URSPROMOTION_HAVEST->value,
|
|
|
+ REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value,
|
|
|
+ REWARD_SOURCE_TYPE::URSPROMOTION_REWARD->value,
|
|
|
+ REWARD_SOURCE_TYPE::URSPROMOTION_REGISTER->value,
|
|
|
+ REWARD_SOURCE_TYPE::URSPROMOTION_LEVEL->value,
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 1. 从奖励日志表中统计物品奖励
|
|
|
+ $rewardQuery = GameRewardLog::where('user_id', $farmUserId)
|
|
|
+ ->whereIn('source_type', $ursPromotionSourceTypes);
|
|
|
+
|
|
|
+ if ($startDate) {
|
|
|
+ $rewardQuery->where('created_at', '>=', $startDate);
|
|
|
+ }
|
|
|
+ if ($endDate) {
|
|
|
+ $rewardQuery->where('created_at', '<=', $endDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ $rewardLogs = $rewardQuery->get();
|
|
|
+
|
|
|
+ foreach ($rewardLogs as $log) {
|
|
|
+ $stats['total_count']++;
|
|
|
+
|
|
|
+ // 解析奖励物品,计算等价钻石价值
|
|
|
+ $rewardItems = is_array($log->reward_items) ? $log->reward_items : (json_decode($log->reward_items, true) ?? []);
|
|
|
+ $itemValue = $this->calculateItemsValue($rewardItems);
|
|
|
+ $stats['total_amount'] += $itemValue;
|
|
|
+
|
|
|
+ // 按来源类型统计
|
|
|
+ $sourceType = $log->source_type;
|
|
|
+ if (!isset($stats['by_source_type'][$sourceType])) {
|
|
|
+ $stats['by_source_type'][$sourceType] = ['amount' => 0, 'count' => 0];
|
|
|
+ }
|
|
|
+ $stats['by_source_type'][$sourceType]['amount'] += $itemValue;
|
|
|
+ $stats['by_source_type'][$sourceType]['count']++;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 从资金日志表中统计钻石奖励
|
|
|
+ $fundQuery = FundLogModel::where('user_id', $farmUserId)
|
|
|
+ ->where('fund_id', FUND_TYPE::FUND2->value) // 钻石类型
|
|
|
+ ->where('amount', '>', 0) // 只统计收入
|
|
|
+ ->where('remark', 'like', '%推广%'); // 包含推广关键词的备注
|
|
|
+
|
|
|
+ if ($startDate) {
|
|
|
+ $fundQuery->where('create_time', '>=', strtotime($startDate));
|
|
|
+ }
|
|
|
+ if ($endDate) {
|
|
|
+ $fundQuery->where('create_time', '<=', strtotime($endDate));
|
|
|
+ }
|
|
|
+
|
|
|
+ $fundLogs = $fundQuery->get();
|
|
|
+
|
|
|
+ foreach ($fundLogs as $log) {
|
|
|
+ $stats['total_count']++;
|
|
|
+ $stats['total_amount'] += (float)$log->amount;
|
|
|
+
|
|
|
+ // 按备注内容推断来源类型
|
|
|
+ $sourceType = $this->inferSourceTypeFromRemark($log->remark);
|
|
|
+ if (!isset($stats['by_source_type'][$sourceType])) {
|
|
|
+ $stats['by_source_type'][$sourceType] = ['amount' => 0, 'count' => 0];
|
|
|
+ }
|
|
|
+ $stats['by_source_type'][$sourceType]['amount'] += (float)$log->amount;
|
|
|
+ $stats['by_source_type'][$sourceType]['count']++;
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('从日志获取收益统计失败', [
|
|
|
+ 'farm_user_id' => $farmUserId,
|
|
|
+ 'start_date' => $startDate,
|
|
|
+ 'end_date' => $endDate,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $stats;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算物品的等价钻石价值
|
|
|
+ *
|
|
|
+ * @param array $rewardItems 奖励物品数组
|
|
|
+ * @return float
|
|
|
+ */
|
|
|
+ private function calculateItemsValue(array $rewardItems): float
|
|
|
+ {
|
|
|
+ $totalValue = 0;
|
|
|
+
|
|
|
+ foreach ($rewardItems as $item) {
|
|
|
+ // 简单的物品价值计算,可以根据实际需求调整
|
|
|
+ // 这里假设每个物品价值1钻石,实际应该查询物品配置表
|
|
|
+ $quantity = $item['quantity'] ?? 1;
|
|
|
+ $totalValue += $quantity * 1; // 每个物品按1钻石计算
|
|
|
+ }
|
|
|
+
|
|
|
+ return $totalValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从备注推断收益来源类型
|
|
|
+ *
|
|
|
+ * @param string $remark 备注内容
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ private function inferSourceTypeFromRemark(string $remark): string
|
|
|
+ {
|
|
|
+ if (strpos($remark, '收获') !== false) {
|
|
|
+ return REWARD_SOURCE_TYPE::URSPROMOTION_HAVEST->value;
|
|
|
+ }
|
|
|
+ if (strpos($remark, '补发') !== false) {
|
|
|
+ return REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value;
|
|
|
+ }
|
|
|
+ if (strpos($remark, '注册') !== false) {
|
|
|
+ return REWARD_SOURCE_TYPE::URSPROMOTION_REGISTER->value;
|
|
|
+ }
|
|
|
+ if (strpos($remark, '等级') !== false) {
|
|
|
+ return REWARD_SOURCE_TYPE::URSPROMOTION_LEVEL->value;
|
|
|
+ }
|
|
|
+
|
|
|
+ return REWARD_SOURCE_TYPE::URSPROMOTION_REWARD->value; // 默认推广奖励
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 从统计数据构建Reward对象
|
|
|
*
|