|
|
|
@ -97,6 +97,12 @@ class CommissionScoreLog |
|
|
|
|
$company_level_price = $this->getGoodPriceByLevel($order['goods_id'], self::LEVEL_6)[0]; |
|
|
|
|
|
|
|
|
|
\think\Log::info('上级分销商ID=' . $parentUserId . '] ---start'.'---高级分销商---'.$level5.'---循环次数---'.$currentCommissionLevel); |
|
|
|
|
|
|
|
|
|
//优惠券不反佣金 |
|
|
|
|
if($order['ext']['coupon_discount_fee'] > 0) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//1.非用户角色下单立即反积分 并且给公司账户返积分 |
|
|
|
|
if ($initLevel > self::LEVEL_1 && $currentCommissionLevel == 1) { |
|
|
|
|
$amount = bcsub($order['goods_price'], $current_level_price, 2); |
|
|
|
@ -108,6 +114,18 @@ class CommissionScoreLog |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
//2.新人大礼包商品不用计算返积分 直接给用户升级 |
|
|
|
|
$is_new_user = $this->getGoodPriceByLevel($order['goods_id'], $initLevel)[1]; |
|
|
|
|
if ($is_new_user == 1 && $initLevel == self::LEVEL_1) { |
|
|
|
|
$currentAgent_user = AgentModel::where('user_id', $this->user->id)->find(); |
|
|
|
|
$currentAgent_user->level = self::LEVEL_2; |
|
|
|
|
$currentAgent_user->save(); |
|
|
|
|
//更新用户标识 |
|
|
|
|
$this->user->is_buy_new_goods = 1; |
|
|
|
|
$this->user->save(); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 执行下一级返现任务 |
|
|
|
|
if ($parentUserId) { |
|
|
|
|
$parentAgent = new Agent($parentUserId); |
|
|
|
@ -116,18 +134,7 @@ class CommissionScoreLog |
|
|
|
|
\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]; |
|
|
|
|
|
|
|
|
|
//2.新人大礼包商品不用计算返积分 直接给用户升级 |
|
|
|
|
if ($is_new_user == 1 && $initLevel == self::LEVEL_1) { |
|
|
|
|
$currentAgent_user = AgentModel::where('user_id', $this->user->id)->find(); |
|
|
|
|
$currentAgent_user->level = self::LEVEL_2; |
|
|
|
|
$currentAgent_user->save(); |
|
|
|
|
//更新用户标识 |
|
|
|
|
$this->user->is_buy_new_goods = 1; |
|
|
|
|
$this->user->save(); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//只有上级分销等级 比初始高才算佣金 |
|
|
|
@ -199,28 +206,34 @@ class CommissionScoreLog |
|
|
|
|
/* |
|
|
|
|
* 执行分销用户升级计划 |
|
|
|
|
*/ |
|
|
|
|
public function runAgentUpgradePlan($order, $currentCommissionLevel = 1) { |
|
|
|
|
public function runAgentUpgradePlan() { |
|
|
|
|
$currentAgent = new Agent($this->user); |
|
|
|
|
\think\Log::info('当前分销商数据222=' . json_encode($currentAgent)); |
|
|
|
|
if (!empty($currentAgent)) { |
|
|
|
|
$this->calcAgentUpgradePlan($currentAgent, $currentCommissionLevel = 1); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function calcAgentUpgradePlan($currentAgent,$currentCommissionLevel) { |
|
|
|
|
//默认只算到10级 |
|
|
|
|
if ($currentCommissionLevel >= self::CALC_LEVEL) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$canUpgradeLevel = $this->checkAgentUpgradeLevel($this->agent, $order); |
|
|
|
|
$canUpgradeLevel = $this->checkAgentUpgradeLevel($currentAgent); |
|
|
|
|
\think\Log::info('当前分销商升级信息=' . $canUpgradeLevel . '] ---start'.'---循环次数---'.$currentCommissionLevel); |
|
|
|
|
if ($canUpgradeLevel) { |
|
|
|
|
// if ($this->config->isUpgradeCheck()) { |
|
|
|
|
// $this->agent->level_status = $canUpgradeLevel; |
|
|
|
|
// } else { |
|
|
|
|
$this->agent->level = $canUpgradeLevel; |
|
|
|
|
LogModel::add($this->user->id, 'agent', ['type' => 'level', 'level' => LevelModel::find($canUpgradeLevel)]); |
|
|
|
|
// } |
|
|
|
|
$this->agent->level = $canUpgradeLevel; |
|
|
|
|
LogModel::add($this->user->id, 'agent', ['type' => 'level', 'level' => LevelModel::find($canUpgradeLevel)]); |
|
|
|
|
$this->agent->save(); |
|
|
|
|
} |
|
|
|
|
$parentUserId = $this->getParentUserId(); |
|
|
|
|
\think\Log::info('上级分销商ID=' . $parentUserId . '] ---start'.'---分销商订单---'.json_encode($order).'---循环次数---'.$currentCommissionLevel); |
|
|
|
|
$parentUserId = $currentAgent->getParentUserId(); |
|
|
|
|
if ($parentUserId) { |
|
|
|
|
$this->createAsyncAgentUpgradeNew($parentUserId, $order); |
|
|
|
|
$currentCommissionLevel ++; |
|
|
|
|
$parentAgent = new Agent($parentUserId); |
|
|
|
|
$this->calcAgentUpgradePlan($parentAgent, $currentCommissionLevel); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -256,17 +269,20 @@ class CommissionScoreLog |
|
|
|
|
/** |
|
|
|
|
* 比对当前分销商条件是否满足升级规则 |
|
|
|
|
*/ |
|
|
|
|
private function checkAgentUpgradeLevel($agent, $order) |
|
|
|
|
private function checkAgentUpgradeLevel($agent) |
|
|
|
|
{ |
|
|
|
|
$nextAgentLevel = $this->getNextAgentLevel($agent); |
|
|
|
|
|
|
|
|
|
\think\Log::info('当前分销商升级信息=' . json_encode($nextAgentLevel, true)); |
|
|
|
|
if (count($nextAgentLevel)) { |
|
|
|
|
foreach ($nextAgentLevel as $level) { |
|
|
|
|
$checkLevel[$level->level] = $this->isMatchUpgradeLevelRule($level, $order); |
|
|
|
|
// 不允许越级升级 |
|
|
|
|
$checkLevel[$level->level] = $this->isMatchUpgradeLevelRule($level, $agent); |
|
|
|
|
\think\Log::info('当前等级是否可以升级=' . json_encode($checkLevel, true)); |
|
|
|
|
// 用户不允许越级升级 |
|
|
|
|
|
|
|
|
|
// if (!$this->config->isUpgradeJump()) break; |
|
|
|
|
} |
|
|
|
|
$checkLevel = array_reverse($checkLevel, true); |
|
|
|
|
\think\Log::info('当前等级是否可以升级反转过的=' . json_encode($checkLevel, true)); |
|
|
|
|
$canUpgradeLevel = array_search(true, $checkLevel); |
|
|
|
|
if ($canUpgradeLevel) { |
|
|
|
|
return $canUpgradeLevel; |
|
|
|
@ -275,9 +291,11 @@ class CommissionScoreLog |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function isMatchUpgradeLevelRule($level, $order) { |
|
|
|
|
public function isMatchUpgradeLevelRule($level, $agent) { |
|
|
|
|
$is_level = false; |
|
|
|
|
if (($order->order_amount + $this->getNextTeamTotal($this->user->id)) > $level->total_consume) { |
|
|
|
|
\think\Log::info('当前用户消费总金额=' .($agent->user->total_consume + $this->getNextTeamTotal($agent->user->id)) ); |
|
|
|
|
\think\Log::info('当前等级升级'.$level->level.'金额条件=' .$level->total_consume); |
|
|
|
|
if (($agent->user->total_consume + $this->getNextTeamTotal($agent->user->id)) > $level->total_consume) { |
|
|
|
|
$is_level = true; |
|
|
|
|
} |
|
|
|
|
return $is_level; |
|
|
|
@ -289,9 +307,7 @@ class CommissionScoreLog |
|
|
|
|
public function getNextTeamTotal($user_id) { |
|
|
|
|
$nextUserIds = []; |
|
|
|
|
$this->getNextTeamUserIds($this->getParent($user_id), $nextUserIds); |
|
|
|
|
return Order::whereIn('user_id', $nextUserIds) |
|
|
|
|
->where('status', Order::STATUS_PAID) |
|
|
|
|
->sum('order_amount'); |
|
|
|
|
return User::whereIn('id', $nextUserIds)->sum('total_consume'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|