| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace App\Module\Promotion\Services;
- use App\Module\Promotion\Enums\PROFIT_SOURCE_TYPE;
- use App\Module\Promotion\Logics\ReferralLogic;
- use App\Module\Promotion\Logics\TalentLogic;
- use App\Module\Promotion\Logics\PromotionProfitLogic;
- use Illuminate\Support\Facades\Log;
- /**
- * 团队收益服务类
- *
- * 对外提供团队收益相关的服务,包括记录收益、获取收益记录、
- * 统计收益等功能。该类对外提供服务,内部调用逻辑层实现。
- */
- class PromotionProfitService
- {
- /**
- * 记录农场收获收益
- *
- * @param int $userId 产生收益的用户ID
- * @param int $sourceId 收益来源ID(如收获记录ID)
- * @param int $itemId 物品ID
- * @param int $amount 收益数量
- * @return bool
- */
- public static function recordFarmHarvestProfit(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::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模块的物品出售'
- ]
- ];
- }
- }
|