|
|
|
@ -35,6 +35,8 @@ class CommissionScoreLog |
|
|
|
|
const LEVEL_4 = 4;//中级代理 |
|
|
|
|
const LEVEL_5 = 5;//高级代理 |
|
|
|
|
|
|
|
|
|
const LEVEL_6 = 6;//公司账户 |
|
|
|
|
|
|
|
|
|
public function __construct($user = null) |
|
|
|
|
{ |
|
|
|
|
if (is_numeric($user)) { |
|
|
|
@ -53,221 +55,137 @@ class CommissionScoreLog |
|
|
|
|
* @param object $currentAgent 当前待分佣的分销商 默认自购开始算 |
|
|
|
|
* @param int $currentCommissionLevel 当前分佣层级 一级(自购)开始算 |
|
|
|
|
*/ |
|
|
|
|
public function agentScoreSettlePlan($order, $currentAgent = null, $currentCommissionLevel = 1) { |
|
|
|
|
// if ($currentCommissionLevel >= self::CALC_LEVEL) { |
|
|
|
|
// return true; |
|
|
|
|
// } |
|
|
|
|
// 当前层级为1且分销订单是自购订单 则当前分销商为购买人自己 |
|
|
|
|
if ($currentCommissionLevel === 1) { |
|
|
|
|
$currentAgent = new Agent($order->user_id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($currentAgent && !empty($currentAgent->user)) { |
|
|
|
|
public function agentScoreSettlePlan($order, $currentCommissionLevel = 1) |
|
|
|
|
{ |
|
|
|
|
\think\Log::info('当前分销商订单数据=' . json_encode($order)); |
|
|
|
|
$currentAgent = new Agent($this->user); |
|
|
|
|
\think\Log::info('当前分销商数据222=' . json_encode($currentAgent)); |
|
|
|
|
if (!empty($currentAgent)) { |
|
|
|
|
// 获取用户分销等级 |
|
|
|
|
$agentLevel = $this->getAgentLevel($currentAgent); |
|
|
|
|
$agentLevel = $currentAgent->getAgentLevel(); |
|
|
|
|
\think\Log::info('当前用户分销等级=' . json_encode($agentLevel)); |
|
|
|
|
if ($agentLevel) { |
|
|
|
|
//普通用户 |
|
|
|
|
// if ($agentLevel == self::LEVEL_1) { |
|
|
|
|
$this->userBackScorePlan($order, $currentAgent, $currentCommissionLevel, $agentLevel); |
|
|
|
|
// } |
|
|
|
|
// //会员 |
|
|
|
|
// if ($agentLevel == self::LEVEL_2) { |
|
|
|
|
// $this->memberBackPlan($order, $currentAgent, $currentCommissionLevel, $agentLevel); |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// //初级代理 |
|
|
|
|
// if ($agentLevel == self::LEVEL_3 ) { |
|
|
|
|
// $this->level1BackPlan($order, $currentAgent, $currentCommissionLevel, $agentLevel); |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// //中级代理 |
|
|
|
|
// if ($agentLevel == self::LEVEL_4) { |
|
|
|
|
// $this->level2BackPlan($order, $currentAgent, $currentCommissionLevel, $agentLevel); |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// //高级代理 |
|
|
|
|
// if ($agentLevel == self::LEVEL_5) { |
|
|
|
|
// return $this->level3BackPlan($order, $currentAgent, $currentCommissionLevel, $agentLevel); |
|
|
|
|
// } |
|
|
|
|
$this->userBackScorePlan($order, $currentAgent, $currentCommissionLevel, $agentLevel, $level5 = 0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
// 递归执行下一次 |
|
|
|
|
// $currentCommissionLevel++; |
|
|
|
|
// // 超出分销层级 |
|
|
|
|
// if (self::CALC_LEVEL < $currentCommissionLevel) { |
|
|
|
|
// return true; |
|
|
|
|
// } |
|
|
|
|
// $parentUserId = $currentAgent->getParentUserId(); |
|
|
|
|
// // 执行下一级分销任务 |
|
|
|
|
// if ($parentUserId) { |
|
|
|
|
// $parentAgent = new Agent($parentUserId); |
|
|
|
|
// $this->agentScoreSettlePlan($order, $parentAgent, $currentCommissionLevel); |
|
|
|
|
// } else { |
|
|
|
|
// return true; |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 用户返积分计划 |
|
|
|
|
*/ |
|
|
|
|
public function userBackScorePlan($order, $currentAgent, $currentCommissionLevel, $initLevel) { |
|
|
|
|
// 递归执行下一次 |
|
|
|
|
$currentCommissionLevel++; |
|
|
|
|
public function userBackScorePlan($order, $currentAgent, $currentCommissionLevel, $initLevel, $level5) { |
|
|
|
|
\think\Log::info('当前分销商等级=' . $initLevel . '] ---start'.'---高级分销商---'.$level5); |
|
|
|
|
|
|
|
|
|
\think\Log::info('当前分销商订单信息=' . json_encode($order) . '] ---start'.'---高级分销商---'.$level5.'---循环次数---'.$currentCommissionLevel); |
|
|
|
|
|
|
|
|
|
// 超出分销层级 |
|
|
|
|
if (self::CALC_LEVEL < $currentCommissionLevel) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
$parentUserId = $currentAgent->getParentUserId(); |
|
|
|
|
// 执行下一级分销任务 |
|
|
|
|
$currentLevel = $this->getAgentLevel($currentAgent); |
|
|
|
|
$current_level_price = $this->getGoodPriceByLevel($order['goods_id'], $currentLevel)[0]; |
|
|
|
|
$company_level_price = $this->getGoodPriceByLevel($order['goods_id'], self::LEVEL_6)[0]; |
|
|
|
|
|
|
|
|
|
\think\Log::info('上级分销商ID=' . $parentUserId . '] ---start'.'---高级分销商---'.$level5.'---循环次数---'.$currentCommissionLevel); |
|
|
|
|
//非用户角色下单立即反积分 |
|
|
|
|
if ($initLevel > self::LEVEL_1 && $currentCommissionLevel == 1) { |
|
|
|
|
$amount = bcsub($order['goods_price'], $current_level_price, 2); |
|
|
|
|
//算积分 |
|
|
|
|
Wallet::change($currentAgent->user->id, 'score', $amount, 'back_score', [], '下单返现'); |
|
|
|
|
$this->calcCompany($current_level_price, $company_level_price); |
|
|
|
|
} |
|
|
|
|
// 执行下一级返现任务 |
|
|
|
|
if ($parentUserId) { |
|
|
|
|
$parentAgent = new Agent($parentUserId); |
|
|
|
|
$parentLevel = $this->getAgentLevel($parentAgent); |
|
|
|
|
$parent_level_price = $this->getGoodPriceByLevel($order->goods_id, $parentLevel); |
|
|
|
|
$current_level_price = $this->getGoodPriceByLevel($order->goods_id, $currentAgent); |
|
|
|
|
$crrentLevel = $this->getAgentLevel($currentAgent); |
|
|
|
|
//只有上级分销等级 比自己高才算佣金 |
|
|
|
|
|
|
|
|
|
\think\Log::info('父用户等级'.$parentLevel); |
|
|
|
|
\think\Log::info('当前用户等级'.$currentLevel); |
|
|
|
|
$parent_level_price = $this->getGoodPriceByLevel($order['goods_id'], $parentLevel)[0]; |
|
|
|
|
$is_new_user = $this->getGoodPriceByLevel($order['goods_id'], $parentLevel)[1]; |
|
|
|
|
|
|
|
|
|
//新人大礼包商品不用计算返积分 给用户升级 |
|
|
|
|
if ($is_new_user == 1 && $initLevel == self::LEVEL_1) { |
|
|
|
|
// $currentAgent->level = self::LEVEL_2; |
|
|
|
|
// $currentAgent->save(); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//只有上级分销等级 比初始高才算佣金 |
|
|
|
|
if ($parentLevel > $initLevel) { |
|
|
|
|
//1.会员不参与反佣 |
|
|
|
|
if ($parentLevel == self::LEVEL_2) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//2.向上反佣金 |
|
|
|
|
if ($parentLevel > $crrentLevel) { |
|
|
|
|
|
|
|
|
|
//3.向上反佣金 |
|
|
|
|
if ($parentLevel > $currentLevel) { |
|
|
|
|
$amount = bcsub($current_level_price, $parent_level_price, 2); |
|
|
|
|
\think\Log::info('上级商品价格'.$parent_level_price); |
|
|
|
|
\think\Log::info('当前商品价格'.$current_level_price); |
|
|
|
|
if (!empty($amount) && $amount> 0) { |
|
|
|
|
$this->calcCompany($parent_level_price, $company_level_price); |
|
|
|
|
//算积分 |
|
|
|
|
Wallet::change($parentAgent->user->id, 'score', $amount, 'team_order', [], '团队下单'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//3.用户的上上级是高级 算团队2% |
|
|
|
|
if ($parentLevel == self::LEVEL_5 && $crrentLevel == self::LEVEL_5) { |
|
|
|
|
$amount = bcmul($order->pay_fee, '0.02', 2); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//4.用户的上上级是高级 算团队2% |
|
|
|
|
if ($parentLevel == self::LEVEL_5 && $currentLevel == self::LEVEL_5 && $level5<2) { |
|
|
|
|
|
|
|
|
|
if (!empty($amount) && $amount> 0) { |
|
|
|
|
if ($level5 == 0) { |
|
|
|
|
$amount = bcmul($order['goods_price'], '0.02', 2); |
|
|
|
|
$this->calcCompany($parent_level_price, $company_level_price, $amount); |
|
|
|
|
//算积分 |
|
|
|
|
Wallet::change($parentAgent->user->id, 'score', $amount, 'team_order', [], '团队下单'); |
|
|
|
|
} |
|
|
|
|
$level5++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$this->userBackScorePlan($order, $parentAgent, $currentCommissionLevel, $initLevel); |
|
|
|
|
} else { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 会员返佣计划 |
|
|
|
|
*/ |
|
|
|
|
public function memberBackPlan($order, $currentAgent = null, $currentCommissionLevel = 1) { |
|
|
|
|
// 递归执行下一次 |
|
|
|
|
$currentCommissionLevel++; |
|
|
|
|
// 超出分销层级 |
|
|
|
|
if (self::CALC_LEVEL < $currentCommissionLevel) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
$parentUserId = $currentAgent->getParentUserId(); |
|
|
|
|
// 执行下一级分销任务 |
|
|
|
|
if ($parentUserId) { |
|
|
|
|
$parentAgent = new Agent($parentUserId); |
|
|
|
|
$parentLevel = $this->getAgentLevel($parentAgent); |
|
|
|
|
$level_price = $this->getGoodPriceByLevel($order->goods_id, $parentLevel); |
|
|
|
|
$amount = bcsub($level_price, $order->pay_fee, 2); |
|
|
|
|
//算积分 |
|
|
|
|
Wallet::change($parentAgent->user->id, 'score', $amount, 'team_order', [], '团队下单'); |
|
|
|
|
|
|
|
|
|
$this->memberBackPlan($order, $parentAgent, $currentCommissionLevel); |
|
|
|
|
} else { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 初级代理返佣金 |
|
|
|
|
* @param $order |
|
|
|
|
* @param $currentAgent |
|
|
|
|
* @param $currentCommissionLevel |
|
|
|
|
*/ |
|
|
|
|
public function level1BackPlan($order, $currentAgent = null, $currentCommissionLevel = 1) { |
|
|
|
|
// 递归执行下一次 |
|
|
|
|
$currentCommissionLevel++; |
|
|
|
|
// 超出分销层级 |
|
|
|
|
if (self::CALC_LEVEL < $currentCommissionLevel) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
$parentUserId = $currentAgent->getParentUserId(); |
|
|
|
|
// 执行下一级分销任务 |
|
|
|
|
if ($parentUserId) { |
|
|
|
|
$parentAgent = new Agent($parentUserId); |
|
|
|
|
$parentLevel = $this->getAgentLevel($parentAgent); |
|
|
|
|
$level_price = $this->getGoodPriceByLevel($order->goods_id, $parentLevel); |
|
|
|
|
$amount = bcsub($level_price, $order->goods_amount, 2); |
|
|
|
|
//算积分 |
|
|
|
|
Wallet::change($parentAgent->user->id, 'score', $amount, 'team_order', [], '团队下单'); |
|
|
|
|
// 递归执行下一次 |
|
|
|
|
$currentCommissionLevel++; |
|
|
|
|
if ($parentAgent) { |
|
|
|
|
$this->userBackScorePlan($order, $parentAgent, $currentCommissionLevel, $initLevel, $level5); |
|
|
|
|
} else { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$this->level1BackPlan($order, $parentAgent, $currentCommissionLevel); |
|
|
|
|
} else { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 中级代理返佣金 |
|
|
|
|
* @param $order |
|
|
|
|
* @param $currentAgent |
|
|
|
|
* @param $currentCommissionLevel |
|
|
|
|
* 公司账户算佣金 |
|
|
|
|
*/ |
|
|
|
|
public function level2BackPlan($order, $currentAgent = null, $currentCommissionLevel = 1) { |
|
|
|
|
// 递归执行下一次 |
|
|
|
|
$currentCommissionLevel++; |
|
|
|
|
// 超出分销层级 |
|
|
|
|
if (self::CALC_LEVEL < $currentCommissionLevel) { |
|
|
|
|
return true; |
|
|
|
|
public function calcCompany($currentPrice, $companyPrice, $amountRate = 0) { |
|
|
|
|
$amount = bcsub($currentPrice, $companyPrice, 2); |
|
|
|
|
//同级奖励佣金 算差额 |
|
|
|
|
if ($amountRate > 0) { |
|
|
|
|
$amount = bcsub($amount, $amountRate, 2); |
|
|
|
|
} |
|
|
|
|
$parentUserId = $currentAgent->getParentUserId(); |
|
|
|
|
// 执行下一级分销任务 |
|
|
|
|
if ($parentUserId) { |
|
|
|
|
$parentAgent = new Agent($parentUserId); |
|
|
|
|
$parentLevel = $this->getAgentLevel($parentAgent); |
|
|
|
|
$level_price = $this->getGoodPriceByLevel($order->goods_id, $parentLevel); |
|
|
|
|
$amount = bcsub($level_price, $order->goods_amount, 2); |
|
|
|
|
//算积分 |
|
|
|
|
Wallet::change($parentAgent->user->id, 'score', $amount, 'team_order', [], '团队下单'); |
|
|
|
|
|
|
|
|
|
$this->level2BackPlan($order, $parentAgent, $currentCommissionLevel); |
|
|
|
|
} else { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
Wallet::change(2, 'score', $amount, 'team_order', [], '用户下单'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function level3BackPlan($order, $currentAgent = null, $currentCommissionLevel = 1) { |
|
|
|
|
//高级不参与反佣金 |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
/* |
|
|
|
|
* 执行分销用户升级计划 |
|
|
|
|
*/ |
|
|
|
|
public function runAgentUpgradePlan($order, $currentCommissionLevel = 1) { |
|
|
|
|
//默认只算到10级 |
|
|
|
|
if ($currentCommissionLevel >= self::CALC_LEVEL) { |
|
|
|
|
return true; |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$canUpgradeLevel = $this->checkAgentUpgradeLevel($this->agent, $order); |
|
|
|
|
\think\Log::info('当前分销商升级信息=' . $canUpgradeLevel . '] ---start'.'---循环次数---'.$currentCommissionLevel); |
|
|
|
|
if ($canUpgradeLevel) { |
|
|
|
|
// if ($this->config->isUpgradeCheck()) { |
|
|
|
|
// $this->agent->level_status = $canUpgradeLevel; |
|
|
|
@ -278,10 +196,11 @@ class CommissionScoreLog |
|
|
|
|
$this->agent->save(); |
|
|
|
|
} |
|
|
|
|
$parentUserId = $this->getParentUserId(); |
|
|
|
|
\think\Log::info('上级分销商ID=' . $parentUserId . '] ---start'.'---分销商订单---'.json_encode($order).'---循环次数---'.$currentCommissionLevel); |
|
|
|
|
if ($parentUserId) { |
|
|
|
|
$this->createAsyncAgentUpgradeNew($parentUserId, $order); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -290,7 +209,7 @@ class CommissionScoreLog |
|
|
|
|
* 创建[分销商升级&统计业绩]异步队列任务 |
|
|
|
|
* 为了防止计算量大而引起阻塞,使用异步递归 |
|
|
|
|
*/ |
|
|
|
|
public function createAsyncAgentUpgradeNew($order,$user_id = 0) |
|
|
|
|
public function createAsyncAgentUpgradeNew($user_id, $order) |
|
|
|
|
{ |
|
|
|
|
if ($user_id === 0) { |
|
|
|
|
$user_id = $this->user->id; |
|
|
|
@ -349,7 +268,8 @@ class CommissionScoreLog |
|
|
|
|
* 查询用户消费和下级团队消费总额 |
|
|
|
|
*/ |
|
|
|
|
public function getNextTeamTotal($user_id) { |
|
|
|
|
$nextUserIds = $this->getNextTeamUserIds($this->getParent($user_id)); |
|
|
|
|
$nextUserIds = []; |
|
|
|
|
$this->getNextTeamUserIds($this->getParent($user_id), $nextUserIds); |
|
|
|
|
return Order::whereIn('user_id', $nextUserIds) |
|
|
|
|
->where('status', Order::STATUS_PAID) |
|
|
|
|
->sum('order_amount'); |
|
|
|
@ -359,13 +279,12 @@ class CommissionScoreLog |
|
|
|
|
* 获取所有下级团队人数 |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public function getNextTeamUserIds($user) { |
|
|
|
|
static $nextUserIds = []; |
|
|
|
|
public function getNextTeamUserIds($user, &$nextUserIds) { |
|
|
|
|
foreach ($user as $item) { |
|
|
|
|
$nextUserIds[] = $item['id']; |
|
|
|
|
$nexUserData = $this->getParent($item['id']); |
|
|
|
|
if (is_array($nexUserData)) { |
|
|
|
|
$this->getNextTeamUserIds($nexUserData); |
|
|
|
|
$this->getNextTeamUserIds($nexUserData, $nextUserIds); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return $nextUserIds; |
|
|
|
@ -393,8 +312,28 @@ class CommissionScoreLog |
|
|
|
|
*/ |
|
|
|
|
public function getGoodPriceByLevel($goods_id, $level) { |
|
|
|
|
$goods_info = Goods::get($goods_id); |
|
|
|
|
$name = 'leve'.$level.'_price'; |
|
|
|
|
return $goods_info[$name] ?? 00; |
|
|
|
|
$level_price = 0; |
|
|
|
|
if ($level == self::LEVEL_1) { |
|
|
|
|
$level_price = $goods_info['price'][0]; |
|
|
|
|
} |
|
|
|
|
if ($level == self::LEVEL_2) { |
|
|
|
|
$level_price = $goods_info['user_price']; |
|
|
|
|
} |
|
|
|
|
if ($level == self::LEVEL_3) { |
|
|
|
|
$level_price = $goods_info['level1_price']; |
|
|
|
|
} |
|
|
|
|
if ($level == self::LEVEL_4) { |
|
|
|
|
$level_price = $goods_info['level2_price']; |
|
|
|
|
} |
|
|
|
|
if ($level == self::LEVEL_5) { |
|
|
|
|
$level_price = $goods_info['level3_price']; |
|
|
|
|
} |
|
|
|
|
if ($level == self::LEVEL_6) { |
|
|
|
|
$level_price = $goods_info['company_price']; |
|
|
|
|
} |
|
|
|
|
\think\Log::info('当前商品价格=' .json_encode($goods_info)); |
|
|
|
|
|
|
|
|
|
return [$level_price, $goods_info['is_new_user']]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|