calculateAndRecordProfit( $userId, PROFIT_SOURCE_TYPE::FARM_HARVEST, $sourceId, $itemId, $amount ); } catch (\Exception $e) { Log::error("记录农场收获收益失败: " . $e->getMessage()); return false; } } /** * 记录任务完成收益 * * @param int $userId 产生收益的用户ID * @param int $sourceId 收益来源ID(如任务记录ID) * @param int $itemId 物品ID * @param int $amount 收益数量 * @return bool */ public static function recordTaskCompleteProfit(int $userId, int $sourceId, int $itemId, int $amount): bool { try { $referralLogic = new ReferralLogic(); $talentLogic = new TalentLogic($referralLogic); $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic); return $promotionProfitLogic->calculateAndRecordProfit( $userId, PROFIT_SOURCE_TYPE::TASK_COMPLETE, $sourceId, $itemId, $amount ); } catch (\Exception $e) { Log::error("记录任务完成收益失败: " . $e->getMessage()); return false; } } /** * 记录物品出售收益 * * @param int $userId 产生收益的用户ID * @param int $sourceId 收益来源ID(如出售记录ID) * @param int $itemId 物品ID * @param int $amount 收益数量 * @return bool */ public static function recordItemSellProfit(int $userId, int $sourceId, int $itemId, int $amount): bool { try { $referralLogic = new ReferralLogic(); $talentLogic = new TalentLogic($referralLogic); $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic); return $promotionProfitLogic->calculateAndRecordProfit( $userId, PROFIT_SOURCE_TYPE::ITEM_SELL, $sourceId, $itemId, $amount ); } catch (\Exception $e) { Log::error("记录物品出售收益失败: " . $e->getMessage()); return false; } } /** * 获取用户的收益记录 * * @param int $userId 用户ID * @param string|null $sourceType 收益来源类型 * @param int $page 页码 * @param int $pageSize 每页数量 * @return array */ public static function getUserProfits(int $userId, ?string $sourceType = null, int $page = 1, int $pageSize = 20): array { try { $referralLogic = new ReferralLogic(); $talentLogic = new TalentLogic($referralLogic); $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic); return $promotionProfitLogic->getUserProfits($userId, $sourceType, $page, $pageSize); } catch (\Exception $e) { Log::error("获取用户收益记录失败: " . $e->getMessage()); return [ 'total' => 0, 'page' => $page, 'page_size' => $pageSize, 'total_pages' => 0, 'profits' => [] ]; } } /** * 统计用户的收益 * * @param int $userId 用户ID * @param string|null $sourceType 收益来源类型 * @param int|null $itemId 物品ID * @return array */ public static function sumUserProfits(int $userId, ?string $sourceType = null, ?int $itemId = null): array { try { $referralLogic = new ReferralLogic(); $talentLogic = new TalentLogic($referralLogic); $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic); return $promotionProfitLogic->sumUserProfits($userId, $sourceType, $itemId); } catch (\Exception $e) { Log::error("统计用户收益失败: " . $e->getMessage()); return [ 'direct_profit' => 0, 'indirect_profit' => 0, 'total_profit' => 0 ]; } } /** * 获取收益分成规则 * * @param string $sourceType 收益来源类型 * @return array|null */ public static function getProfitRule(string $sourceType): ?array { try { $referralLogic = new ReferralLogic(); $talentLogic = new TalentLogic($referralLogic); $promotionProfitLogic = new PromotionProfitLogic($referralLogic, $talentLogic); $rule = $promotionProfitLogic->getProfitRule($sourceType); if (!$rule) { return null; } return [ 'source_type' => $rule->source_type, 'direct_profit_rate' => $rule->direct_profit_rate, 'max_indirect_level' => $rule->max_indirect_level, 'status' => $rule->status, 'rules' => is_array($rule->rules) ? $rule->rules : json_decode($rule->rules, true) ]; } catch (\Exception $e) { Log::error("获取收益分成规则失败: " . $e->getMessage()); return null; } } /** * 获取所有收益来源类型 * * @return array */ public static function getAllProfitSourceTypes(): array { return [ [ 'type' => PROFIT_SOURCE_TYPE::FARM_HARVEST, 'name' => '农场收获', 'description' => '来自Farm模块的作物收获' ], [ 'type' => PROFIT_SOURCE_TYPE::TASK_COMPLETE, 'name' => '任务完成', 'description' => '来自Task模块的任务奖励' ], [ 'type' => PROFIT_SOURCE_TYPE::ITEM_SELL, 'name' => '物品出售', 'description' => '来自GameItems模块的物品出售' ] ]; } }