|
|
@@ -19,20 +19,79 @@ use Illuminate\Support\Facades\Log;
|
|
|
class UrsProfitLogic
|
|
|
{
|
|
|
/**
|
|
|
- * 计算并分发URS推广收益
|
|
|
- *
|
|
|
+ * 计算并分发URS推广收益(按人头奖励)
|
|
|
+ *
|
|
|
+ * @param int $userId 新注册用户ID
|
|
|
+ * @param string $sourceType 收益来源类型
|
|
|
+ * @param int $sourceId 收益来源ID
|
|
|
+ * @return array 分成记录
|
|
|
+ */
|
|
|
+ public function distributePromotionReward(
|
|
|
+ int $userId,
|
|
|
+ string $sourceType,
|
|
|
+ int $sourceId
|
|
|
+ ): array {
|
|
|
+ $profits = [];
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 获取用户的推荐关系链(三代)
|
|
|
+ $referralChain = $this->getUserReferralChain($userId);
|
|
|
+
|
|
|
+ if (empty($referralChain)) {
|
|
|
+ Log::info("用户 {$userId} 无推荐关系,无需分成");
|
|
|
+ return $profits;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取达人等级配置
|
|
|
+ $talentConfigs = $this->getTalentConfigs();
|
|
|
+
|
|
|
+ // 为每一级推荐人发放奖励
|
|
|
+ foreach ($referralChain as $level => $referrerId) {
|
|
|
+ $profit = $this->calculatePromotionReward(
|
|
|
+ $referrerId,
|
|
|
+ $userId,
|
|
|
+ $sourceType,
|
|
|
+ $sourceId,
|
|
|
+ $level,
|
|
|
+ $talentConfigs
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($profit) {
|
|
|
+ $profits[] = $profit;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Log::info("用户 {$userId} 推广收益分发完成", [
|
|
|
+ 'source_type' => $sourceType,
|
|
|
+ 'source_id' => $sourceId,
|
|
|
+ 'profits_count' => count($profits)
|
|
|
+ ]);
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error("URS推广收益分发失败", [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'source_type' => $sourceType,
|
|
|
+ 'source_id' => $sourceId,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $profits;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算并分发URS种植收益(按比例分成)
|
|
|
+ *
|
|
|
* @param int $userId 产生收益的用户ID
|
|
|
* @param string $sourceType 收益来源类型
|
|
|
* @param int $sourceId 收益来源ID
|
|
|
- * @param UrsProfitType $profitType 收益类型
|
|
|
* @param string $originalAmount 原始收益金额
|
|
|
* @return array 分成记录
|
|
|
*/
|
|
|
- public function distributeProfit(
|
|
|
- int $userId,
|
|
|
- string $sourceType,
|
|
|
- int $sourceId,
|
|
|
- UrsProfitType $profitType,
|
|
|
+ public function distributePlantingReward(
|
|
|
+ int $userId,
|
|
|
+ string $sourceType,
|
|
|
+ int $sourceId,
|
|
|
string $originalAmount
|
|
|
): array {
|
|
|
$profits = [];
|
|
|
@@ -51,36 +110,33 @@ class UrsProfitLogic
|
|
|
|
|
|
// 为每一级推荐人计算分成
|
|
|
foreach ($referralChain as $level => $referrerId) {
|
|
|
- $profit = $this->calculateLevelProfit(
|
|
|
+ $profit = $this->calculatePlantingReward(
|
|
|
$referrerId,
|
|
|
$userId,
|
|
|
$sourceType,
|
|
|
$sourceId,
|
|
|
- $profitType,
|
|
|
$level,
|
|
|
$originalAmount,
|
|
|
$talentConfigs
|
|
|
);
|
|
|
-
|
|
|
+
|
|
|
if ($profit) {
|
|
|
$profits[] = $profit;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- Log::info("用户 {$userId} 收益分成完成", [
|
|
|
+
|
|
|
+ Log::info("用户 {$userId} 种植收益分成完成", [
|
|
|
'source_type' => $sourceType,
|
|
|
'source_id' => $sourceId,
|
|
|
- 'profit_type' => $profitType->value,
|
|
|
'original_amount' => $originalAmount,
|
|
|
'profits_count' => count($profits)
|
|
|
]);
|
|
|
-
|
|
|
+
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error("URS收益分成失败", [
|
|
|
+ Log::error("URS种植收益分成失败", [
|
|
|
'user_id' => $userId,
|
|
|
'source_type' => $sourceType,
|
|
|
'source_id' => $sourceId,
|
|
|
- 'profit_type' => $profitType->value,
|
|
|
'error' => $e->getMessage()
|
|
|
]);
|
|
|
}
|
|
|
@@ -136,24 +192,92 @@ class UrsProfitLogic
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 计算单级推荐人的分成收益
|
|
|
- *
|
|
|
+ * 计算推广收益奖励(按人头)
|
|
|
+ *
|
|
|
+ * @param int $referrerId 推荐人ID
|
|
|
+ * @param int $memberId 新注册用户ID
|
|
|
+ * @param string $sourceType 收益来源类型
|
|
|
+ * @param int $sourceId 收益来源ID
|
|
|
+ * @param int $relationLevel 推荐层级
|
|
|
+ * @param array $talentConfigs 达人等级配置
|
|
|
+ * @return UrsProfit|null
|
|
|
+ */
|
|
|
+ private function calculatePromotionReward(
|
|
|
+ int $referrerId,
|
|
|
+ int $memberId,
|
|
|
+ string $sourceType,
|
|
|
+ int $sourceId,
|
|
|
+ int $relationLevel,
|
|
|
+ array $talentConfigs
|
|
|
+ ): ?UrsProfit {
|
|
|
+ // 获取推荐人的达人等级
|
|
|
+ $talent = UrsUserTalent::where('user_id', $referrerId)->first();
|
|
|
+ $talentLevel = $talent ? $talent->talent_level : 0;
|
|
|
+
|
|
|
+ // 获取对应等级的配置
|
|
|
+ $config = $talentConfigs[$talentLevel] ?? null;
|
|
|
+ if (!$config) {
|
|
|
+ Log::warning("推荐人 {$referrerId} 达人等级 {$talentLevel} 配置不存在");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取奖励组ID
|
|
|
+ $rewardGroupId = $this->getPromotionRewardGroupId($config, $relationLevel);
|
|
|
+ if (!$rewardGroupId) {
|
|
|
+ Log::debug("推荐人 {$referrerId} 等级 {$talentLevel} 层级 {$relationLevel} 无奖励组配置");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: 这里需要根据奖励组ID获取具体的奖励金额
|
|
|
+ // 暂时使用固定金额作为示例
|
|
|
+ $rewardAmount = $this->getRewardAmountByGroupId($rewardGroupId);
|
|
|
+
|
|
|
+ // 创建收益记录
|
|
|
+ $profit = UrsProfit::create([
|
|
|
+ 'user_id' => $referrerId,
|
|
|
+ 'promotion_member_id' => $memberId,
|
|
|
+ 'source_id' => $sourceId,
|
|
|
+ 'source_type' => $sourceType,
|
|
|
+ 'profit_type' => UrsProfitType::PROMOTION_REWARD->value,
|
|
|
+ 'relation_level' => $relationLevel,
|
|
|
+ 'original_amount' => '0', // 推广收益无原始金额概念
|
|
|
+ 'profit_amount' => $rewardAmount,
|
|
|
+ 'profit_rate' => 0, // 推广收益无比例概念
|
|
|
+ 'reward_group_id' => $rewardGroupId,
|
|
|
+ 'talent_level' => $talentLevel,
|
|
|
+ 'status' => UrsProfit::STATUS_NORMAL,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ Log::info("URS推广收益记录创建", [
|
|
|
+ 'profit_id' => $profit->id,
|
|
|
+ 'referrer_id' => $referrerId,
|
|
|
+ 'member_id' => $memberId,
|
|
|
+ 'relation_level' => $relationLevel,
|
|
|
+ 'talent_level' => $talentLevel,
|
|
|
+ 'reward_group_id' => $rewardGroupId,
|
|
|
+ 'reward_amount' => $rewardAmount
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return $profit;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算种植收益分成(按比例)
|
|
|
+ *
|
|
|
* @param int $referrerId 推荐人ID
|
|
|
* @param int $memberId 团队成员ID
|
|
|
* @param string $sourceType 收益来源类型
|
|
|
* @param int $sourceId 收益来源ID
|
|
|
- * @param UrsProfitType $profitType 收益类型
|
|
|
* @param int $relationLevel 推荐层级
|
|
|
* @param string $originalAmount 原始收益金额
|
|
|
* @param array $talentConfigs 达人等级配置
|
|
|
* @return UrsProfit|null
|
|
|
*/
|
|
|
- private function calculateLevelProfit(
|
|
|
+ private function calculatePlantingReward(
|
|
|
int $referrerId,
|
|
|
int $memberId,
|
|
|
string $sourceType,
|
|
|
int $sourceId,
|
|
|
- UrsProfitType $profitType,
|
|
|
int $relationLevel,
|
|
|
string $originalAmount,
|
|
|
array $talentConfigs
|
|
|
@@ -161,40 +285,41 @@ class UrsProfitLogic
|
|
|
// 获取推荐人的达人等级
|
|
|
$talent = UrsUserTalent::where('user_id', $referrerId)->first();
|
|
|
$talentLevel = $talent ? $talent->talent_level : 0;
|
|
|
-
|
|
|
+
|
|
|
// 获取对应等级的配置
|
|
|
$config = $talentConfigs[$talentLevel] ?? null;
|
|
|
if (!$config) {
|
|
|
Log::warning("推荐人 {$referrerId} 达人等级 {$talentLevel} 配置不存在");
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 获取分成比例
|
|
|
- $profitRate = $this->getProfitRate($config, $profitType, $relationLevel);
|
|
|
+ $profitRate = $this->getPlantingRewardRate($config, $relationLevel);
|
|
|
if ($profitRate <= 0) {
|
|
|
Log::debug("推荐人 {$referrerId} 等级 {$talentLevel} 层级 {$relationLevel} 分成比例为0");
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 计算分成金额
|
|
|
$profitAmount = bcmul($originalAmount, (string)$profitRate, 10);
|
|
|
-
|
|
|
+
|
|
|
// 创建收益记录
|
|
|
$profit = UrsProfit::create([
|
|
|
'user_id' => $referrerId,
|
|
|
'promotion_member_id' => $memberId,
|
|
|
'source_id' => $sourceId,
|
|
|
'source_type' => $sourceType,
|
|
|
- 'profit_type' => $profitType->value,
|
|
|
+ 'profit_type' => UrsProfitType::PLANTING_REWARD->value,
|
|
|
'relation_level' => $relationLevel,
|
|
|
'original_amount' => $originalAmount,
|
|
|
'profit_amount' => $profitAmount,
|
|
|
'profit_rate' => $profitRate,
|
|
|
+ 'reward_group_id' => null, // 种植收益不使用奖励组
|
|
|
'talent_level' => $talentLevel,
|
|
|
'status' => UrsProfit::STATUS_NORMAL,
|
|
|
]);
|
|
|
-
|
|
|
- Log::info("URS收益分成记录创建", [
|
|
|
+
|
|
|
+ Log::info("URS种植收益记录创建", [
|
|
|
'profit_id' => $profit->id,
|
|
|
'referrer_id' => $referrerId,
|
|
|
'member_id' => $memberId,
|
|
|
@@ -203,32 +328,77 @@ class UrsProfitLogic
|
|
|
'profit_rate' => $profitRate,
|
|
|
'profit_amount' => $profitAmount
|
|
|
]);
|
|
|
-
|
|
|
+
|
|
|
return $profit;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
- * 获取分成比例
|
|
|
- *
|
|
|
+ * 获取推广收益奖励组ID
|
|
|
+ *
|
|
|
+ * @param array $config 达人等级配置
|
|
|
+ * @param int $relationLevel 推荐层级
|
|
|
+ * @return int|null
|
|
|
+ */
|
|
|
+ private function getPromotionRewardGroupId(array $config, int $relationLevel): ?int
|
|
|
+ {
|
|
|
+ $groups = $config['promotion_reward_groups'] ?? [];
|
|
|
+ if (is_string($groups)) {
|
|
|
+ $groups = json_decode($groups, true) ?? [];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $groups[$relationLevel] ?? null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取种植收益分成比例
|
|
|
+ *
|
|
|
* @param array $config 达人等级配置
|
|
|
- * @param UrsProfitType $profitType 收益类型
|
|
|
* @param int $relationLevel 推荐层级
|
|
|
* @return float
|
|
|
*/
|
|
|
- private function getProfitRate(array $config, UrsProfitType $profitType, int $relationLevel): float
|
|
|
+ private function getPlantingRewardRate(array $config, int $relationLevel): float
|
|
|
{
|
|
|
- $ratesField = match($profitType) {
|
|
|
- UrsProfitType::PROMOTION_REWARD => 'promotion_reward_rates',
|
|
|
- UrsProfitType::PLANTING_REWARD => 'planting_reward_rates',
|
|
|
- };
|
|
|
-
|
|
|
- $rates = $config[$ratesField] ?? [];
|
|
|
+ $rates = $config['planting_reward_rates'] ?? [];
|
|
|
if (is_string($rates)) {
|
|
|
$rates = json_decode($rates, true) ?? [];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return (float)($rates[$relationLevel] ?? 0);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据奖励组ID获取奖励金额
|
|
|
+ *
|
|
|
+ * TODO: 这里需要集成奖励组系统,暂时使用固定金额
|
|
|
+ *
|
|
|
+ * @param int $rewardGroupId 奖励组ID
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ private function getRewardAmountByGroupId(int $rewardGroupId): string
|
|
|
+ {
|
|
|
+ // 暂时使用固定金额映射,实际应该从奖励组系统获取
|
|
|
+ $rewardAmounts = [
|
|
|
+ 1001 => '50.0000000000', // 初级达人直推奖励
|
|
|
+ 1002 => '30.0000000000', // 初级达人间推奖励
|
|
|
+ 1003 => '10.0000000000', // 初级达人三推奖励
|
|
|
+ 1004 => '80.0000000000', // 中级达人直推奖励
|
|
|
+ 1005 => '50.0000000000', // 中级达人间推奖励
|
|
|
+ 1006 => '20.0000000000', // 中级达人三推奖励
|
|
|
+ 1007 => '120.0000000000', // 高级达人直推奖励
|
|
|
+ 1008 => '80.0000000000', // 高级达人间推奖励
|
|
|
+ 1009 => '40.0000000000', // 高级达人三推奖励
|
|
|
+ 1010 => '200.0000000000', // 资深达人直推奖励
|
|
|
+ 1011 => '120.0000000000', // 资深达人间推奖励
|
|
|
+ 1012 => '60.0000000000', // 资深达人三推奖励
|
|
|
+ 1013 => '300.0000000000', // 顶级达人直推奖励
|
|
|
+ 1014 => '200.0000000000', // 顶级达人间推奖励
|
|
|
+ 1015 => '100.0000000000', // 顶级达人三推奖励
|
|
|
+ ];
|
|
|
+
|
|
|
+ return $rewardAmounts[$rewardGroupId] ?? '0.0000000000';
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 获取用户的收益统计
|