|
|
|
@ -2,33 +2,669 @@ |
|
|
|
|
|
|
|
|
|
namespace addons\shopro\service; |
|
|
|
|
|
|
|
|
|
use addons\shopro\service\commission\Agent; |
|
|
|
|
use addons\shopro\service\order\OrderOper; |
|
|
|
|
use addons\shopro\service\pay\PayOper; |
|
|
|
|
use app\admin\model\shopro\commission\Agent as AgentModel; |
|
|
|
|
use app\admin\model\shopro\commission\Log as LogModel; |
|
|
|
|
use app\admin\model\shopro\goods\Goods; |
|
|
|
|
use app\admin\model\shopro\order\Order; |
|
|
|
|
use app\admin\model\shopro\Share as ShareModel; |
|
|
|
|
use app\admin\model\shopro\user\User as UserModel; |
|
|
|
|
use app\admin\model\User; |
|
|
|
|
use app\admin\model\shopro\commission\Level as LevelModel; |
|
|
|
|
use app\admin\model\shopro\user\WalletLog as WalletLogModel; |
|
|
|
|
use think\Db; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommissionScoreLog |
|
|
|
|
{ |
|
|
|
|
public $user = null; |
|
|
|
|
public $user; // 商城用户 |
|
|
|
|
public $agent; // 分销商 |
|
|
|
|
public $config; // 分销设置 |
|
|
|
|
public $goods; //商品信息 |
|
|
|
|
public $parentUserId; //上级ID |
|
|
|
|
public $nextAgentTeam; //下级团队 |
|
|
|
|
public $nextUserTeam; //下级用户 |
|
|
|
|
|
|
|
|
|
const CALC_LEVEL = 10; //默认计算层级 |
|
|
|
|
|
|
|
|
|
const LEVEL_1 = 1; |
|
|
|
|
const LEVEL_2 = 2;//会员 |
|
|
|
|
const LEVEL_3 = 3;//初级代理 |
|
|
|
|
const LEVEL_4 = 4;//中级代理 |
|
|
|
|
const LEVEL_5 = 5;//高级代理 |
|
|
|
|
|
|
|
|
|
public function __construct($user = null) |
|
|
|
|
{ |
|
|
|
|
$this->user = is_numeric($user) ? User::get($user) : $user; |
|
|
|
|
$this->user = $this->user ?: auth_user(); |
|
|
|
|
if (is_numeric($user)) { |
|
|
|
|
$this->user = UserModel::get($user); |
|
|
|
|
} else { |
|
|
|
|
$this->user = UserModel::get($user->id); |
|
|
|
|
} |
|
|
|
|
if (!empty($this->user->id)) { |
|
|
|
|
$this->agent = AgentModel::with(['level_info'])->find($this->user->id); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 执行分销用户积分结算计划 |
|
|
|
|
* @param object $order 分销订单 |
|
|
|
|
* @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)) { |
|
|
|
|
// 获取用户分销等级 |
|
|
|
|
$agentLevel = $this->getAgentLevel($currentAgent); |
|
|
|
|
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); |
|
|
|
|
// } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
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++; |
|
|
|
|
// 超出分销层级 |
|
|
|
|
if (self::CALC_LEVEL < $currentCommissionLevel) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
$parentUserId = $currentAgent->getParentUserId(); |
|
|
|
|
// 执行下一级分销任务 |
|
|
|
|
if ($parentUserId) { |
|
|
|
|
$parentAgent = new Agent($parentUserId); |
|
|
|
|
$parentLevel = $this->getAgentLevel($parentAgent); |
|
|
|
|
$parent_level_price = $this->getGoodPriceByLevel($order->goods_id, $parentLevel); |
|
|
|
|
$crrentLevel = $this->getAgentLevel($currentAgent); |
|
|
|
|
//只有上级分销等级 比自己高才算佣金 |
|
|
|
|
if ($parentLevel > $initLevel) { |
|
|
|
|
//1.会员不参与反佣 |
|
|
|
|
if ($parentLevel == self::LEVEL_2) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
//2.向上反佣金 |
|
|
|
|
$amount = bcsub($order->pay_fee, $parent_level_price, 2); |
|
|
|
|
if ($parentLevel == self::LEVEL_5 && $crrentLevel == self::LEVEL_5) {//用户的上上级是高级 算团队2% |
|
|
|
|
$amount = bcmul($amount, '0.02', 2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//算积分 |
|
|
|
|
Wallet::change($parentAgent->user->id, 'score', $amount, 'team_order', [], '团队下单'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$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', [], '团队下单'); |
|
|
|
|
|
|
|
|
|
$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; |
|
|
|
|
} |
|
|
|
|
$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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function level3BackPlan($order, $currentAgent = null, $currentCommissionLevel = 1) { |
|
|
|
|
//高级不参与反佣金 |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 执行分销用户升级计划 |
|
|
|
|
*/ |
|
|
|
|
public function runAgentUpgradePlan($order, $currentCommissionLevel = 1) { |
|
|
|
|
//默认只算到10级 |
|
|
|
|
if ($currentCommissionLevel >= self::CALC_LEVEL) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$canUpgradeLevel = $this->checkAgentUpgradeLevel($this->agent, $order); |
|
|
|
|
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->save(); |
|
|
|
|
} |
|
|
|
|
$parentUserId = $this->getParentUserId(); |
|
|
|
|
if ($parentUserId) { |
|
|
|
|
$this->createAsyncAgentUpgradeNew($parentUserId, $order); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建[分销商升级&统计业绩]异步队列任务 |
|
|
|
|
* 为了防止计算量大而引起阻塞,使用异步递归 |
|
|
|
|
*/ |
|
|
|
|
public function createAsyncAgentUpgradeNew($order,$user_id = 0) |
|
|
|
|
{ |
|
|
|
|
if ($user_id === 0) { |
|
|
|
|
$user_id = $this->user->id; |
|
|
|
|
} |
|
|
|
|
\think\Queue::push('\addons\shopro\job\Commission@createAsyncAgentUpgradeNew', [ |
|
|
|
|
'user_id' => $user_id ,'order' => $order |
|
|
|
|
], 'shopro'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 计算佣金异步队列任务 |
|
|
|
|
* @param $order |
|
|
|
|
* @param $user_id |
|
|
|
|
* @return void |
|
|
|
|
*/ |
|
|
|
|
public function createAsyncAgentScoreSettlePlan($order,$user_id = 0) { |
|
|
|
|
if ($user_id === 0) { |
|
|
|
|
$user_id = $this->user->id; |
|
|
|
|
} |
|
|
|
|
\think\Queue::push('\addons\shopro\job\Commission@createAsyncAgentScoreSettlePlan', [ |
|
|
|
|
'user_id' => $user_id ,'order' => $order |
|
|
|
|
], 'shopro'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 计算用户积分 |
|
|
|
|
* 比对当前分销商条件是否满足升级规则 |
|
|
|
|
*/ |
|
|
|
|
private function checkAgentUpgradeLevel($agent, $order) |
|
|
|
|
{ |
|
|
|
|
$nextAgentLevel = $this->getNextAgentLevel($agent); |
|
|
|
|
|
|
|
|
|
if (count($nextAgentLevel)) { |
|
|
|
|
foreach ($nextAgentLevel as $level) { |
|
|
|
|
$checkLevel[$level->level] = $this->isMatchUpgradeLevelRule($level, $order); |
|
|
|
|
// 不允许越级升级 |
|
|
|
|
// if (!$this->config->isUpgradeJump()) break; |
|
|
|
|
} |
|
|
|
|
$checkLevel = array_reverse($checkLevel, true); |
|
|
|
|
$canUpgradeLevel = array_search(true, $checkLevel); |
|
|
|
|
if ($canUpgradeLevel) { |
|
|
|
|
return $canUpgradeLevel; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function isMatchUpgradeLevelRule($level, $order) { |
|
|
|
|
$is_level = false; |
|
|
|
|
if (($order->order_amount + $this->getNextTeamTotal($this->user->id)) > $level->total_consume) { |
|
|
|
|
$is_level = true; |
|
|
|
|
} |
|
|
|
|
return $is_level; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 查询用户消费和下级团队消费总额 |
|
|
|
|
*/ |
|
|
|
|
public function getNextTeamTotal($user_id) { |
|
|
|
|
$nextUserIds = $this->getNextTeamUserIds($this->getParent($user_id)); |
|
|
|
|
return Order::whereIn('user_id', $nextUserIds) |
|
|
|
|
->where('status', Order::STATUS_PAID) |
|
|
|
|
->sum('order_amount'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取所有下级团队人数 |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public function addLog() { |
|
|
|
|
public function getNextTeamUserIds($user) { |
|
|
|
|
static $nextUserIds = []; |
|
|
|
|
foreach ($user as $item) { |
|
|
|
|
$nextUserIds[] = $item['id']; |
|
|
|
|
$nexUserData = $this->getParent($item['id']); |
|
|
|
|
if (is_array($nexUserData)) { |
|
|
|
|
$this->getNextTeamUserIds($nexUserData); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return $nextUserIds; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function getParent($pid) { |
|
|
|
|
return UserModel::where('parent_user_id', $pid)->select(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取可升级的分销商等级 |
|
|
|
|
*/ |
|
|
|
|
private function getNextAgentLevel($agent) |
|
|
|
|
{ |
|
|
|
|
$nextAgentLevel = []; |
|
|
|
|
$agentLevel = $this->getAgentLevel($agent); |
|
|
|
|
if ($agentLevel) { |
|
|
|
|
$nextAgentLevel = LevelModel::where('level', '>', $agentLevel)->order('level asc')->select(); |
|
|
|
|
} |
|
|
|
|
return $nextAgentLevel; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取当前等级对应商品价格 |
|
|
|
|
*/ |
|
|
|
|
public function getGoodPriceByLevel($goods_id, $level) { |
|
|
|
|
$goods_info = Goods::get($goods_id); |
|
|
|
|
$name = 'leve'.$level.'_price'; |
|
|
|
|
return $goods_info[$name] ?? 00; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取当前分销商等级 |
|
|
|
|
*/ |
|
|
|
|
public function getAgentLevel($currentAgent) |
|
|
|
|
{ |
|
|
|
|
if (empty($currentAgent->agent)) { |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
if (empty($currentAgent->agent->level_info)) { |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
return $currentAgent->agent->level_info->level; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取下级直推团队用户 |
|
|
|
|
*/ |
|
|
|
|
public function getNextUserTeam() |
|
|
|
|
{ |
|
|
|
|
if (!$this->isAgentAvaliable()) { |
|
|
|
|
return []; |
|
|
|
|
} |
|
|
|
|
if (empty($this->nextUserTeam)) { |
|
|
|
|
$this->nextUserTeam = UserModel::where(['parent_user_id' => $this->user->id, 'status' => 'normal'])->column('id'); |
|
|
|
|
} |
|
|
|
|
return $this->nextUserTeam; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取下级分销商团队 |
|
|
|
|
*/ |
|
|
|
|
public function getNextAgentTeam() |
|
|
|
|
{ |
|
|
|
|
if (!$this->isAgentAvaliable()) { |
|
|
|
|
return []; |
|
|
|
|
} |
|
|
|
|
if (empty($this->nextAgentTeam)) { |
|
|
|
|
$this->nextAgentTeam = AgentModel::hasWhere('user', function ($query) { |
|
|
|
|
return $query->where('parent_user_id', $this->user->id); |
|
|
|
|
})->column('user_id, Agent.level, child_user_count_1, child_user_count_all,child_agent_count_1, child_agent_count_all, child_order_money_0, child_order_money_1, child_order_money_all, child_order_count_0, child_order_count_1, child_order_count_all, child_agent_level_1, child_agent_level_all'); |
|
|
|
|
} |
|
|
|
|
return $this->nextAgentTeam; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取分销商可参与状态 正常和冻结都可正常浏览并统计业绩 |
|
|
|
|
*/ |
|
|
|
|
public function isAgentAvaliable() |
|
|
|
|
{ |
|
|
|
|
$status = $this->getAgentStatus(); |
|
|
|
|
if (in_array($status, [AgentModel::AGENT_STATUS_NORMAL, AgentModel::AGENT_STATUS_FREEZE])) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取分销商实时状态 |
|
|
|
|
*/ |
|
|
|
|
public function getAgentStatus($autoCreate = false) |
|
|
|
|
{ |
|
|
|
|
if (empty($this->agent)) { |
|
|
|
|
// 自动创建分销商 |
|
|
|
|
if ($autoCreate) { |
|
|
|
|
return $this->createNewAgent(); |
|
|
|
|
} |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
return $this->agent->status; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建分销商 |
|
|
|
|
*/ |
|
|
|
|
public function createNewAgent($event = '', $applyInfo = []) |
|
|
|
|
{ |
|
|
|
|
// 已经是分销商 |
|
|
|
|
if (!empty($this->agent)) { |
|
|
|
|
return $this->getAgentStatus(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$agentStatus = AgentModel::AGENT_STATUS_NULL; |
|
|
|
|
|
|
|
|
|
$condition = $this->config->getBecomeAgentEvent(); |
|
|
|
|
|
|
|
|
|
$check = false; // 是否满足条件 |
|
|
|
|
$needAgentApplyForm = $this->config->isAgentApplyForm(); |
|
|
|
|
|
|
|
|
|
if ($event !== '' && $condition['type'] !== $event) { |
|
|
|
|
return $agentStatus; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch ($condition['type']) { |
|
|
|
|
case 'apply': // 直接自助申请 |
|
|
|
|
$check = true; |
|
|
|
|
$needAgentApplyForm = true; |
|
|
|
|
break; |
|
|
|
|
case 'goods': // 需购买指定产品 |
|
|
|
|
$isBuy = \app\admin\model\shopro\order\Order::hasWhere('items', function ($query) use ($condition) { |
|
|
|
|
return $query->where('goods_id', 'in', $condition['value'])->where('refund_status', 0); |
|
|
|
|
})->where('Order.user_id', $this->user->id)->paid()->find(); |
|
|
|
|
if ($isBuy) $check = true; |
|
|
|
|
break; |
|
|
|
|
case 'consume': // 消费累计 |
|
|
|
|
if ($this->user->total_consume >= $condition['value']) { |
|
|
|
|
$check = true; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case 'user': // 新会员注册 |
|
|
|
|
$check = true; |
|
|
|
|
$needAgentApplyForm = false; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 可以成为分销商 检查系统设置 |
|
|
|
|
if ($check) { |
|
|
|
|
// 需后台审核 |
|
|
|
|
if ($this->config->isAgentCheck()) { |
|
|
|
|
$agentStatus = AgentModel::AGENT_STATUS_PENDING; |
|
|
|
|
} else { |
|
|
|
|
$agentStatus = AgentModel::AGENT_STATUS_NORMAL; |
|
|
|
|
} |
|
|
|
|
// 需要提交资料 |
|
|
|
|
if ($needAgentApplyForm && empty($applyInfo)) { |
|
|
|
|
$agentStatus = AgentModel::AGENT_STATUS_NEEDINFO; // 需要主动提交资料,暂时不加分销商信息 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 可以直接添加分销商信息 |
|
|
|
|
if ($agentStatus === AgentModel::AGENT_STATUS_NORMAL || $agentStatus === AgentModel::AGENT_STATUS_PENDING) { |
|
|
|
|
AgentModel::create([ |
|
|
|
|
'user_id' => $this->user->id, |
|
|
|
|
'level' => 1, // 默认分销商等级 |
|
|
|
|
'status' => $agentStatus, |
|
|
|
|
'apply_info' => $applyInfo, |
|
|
|
|
'apply_num' => 1, |
|
|
|
|
'become_time' => time() |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
// 绑定上级推荐人 |
|
|
|
|
if ($this->user->parent_user_id === NULL) { |
|
|
|
|
if ($this->bindUserRelation('agent') && $agentStatus !== AgentModel::AGENT_STATUS_NORMAL) { |
|
|
|
|
$this->createAsyncAgentUpgrade($this->user->id); // 防止真正成为分销商时重新触发升级任务造成冗余 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// 绑定为平台直推 |
|
|
|
|
if ($this->user->parent_user_id === NULL) { |
|
|
|
|
$this->user->parent_user_id = 0; |
|
|
|
|
$this->user->save(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$this->agent = AgentModel::with(['level_info'])->find($this->user->id); |
|
|
|
|
|
|
|
|
|
// 添加分销商状态记录 |
|
|
|
|
LogModel::add($this->user->id, 'agent', ['type' => 'status', 'value' => $agentStatus]); |
|
|
|
|
|
|
|
|
|
// 统计分销层级单链业绩 |
|
|
|
|
if ($agentStatus === AgentModel::AGENT_STATUS_NORMAL) { |
|
|
|
|
$this->createAsyncAgentUpgrade($this->user->id); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return $agentStatus; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取用户等级 |
|
|
|
|
* 绑定用户关系 |
|
|
|
|
* |
|
|
|
|
* @param string $event 事件标识(share=点击分享链接, pay=首次支付, agent=成为子分销商) |
|
|
|
|
* @param int $bindAgentId 可指定需绑定的分销商用户ID 默认从分享记录中去查 |
|
|
|
|
*/ |
|
|
|
|
public function getUserLevel() { |
|
|
|
|
echo 11; |
|
|
|
|
public function bindUserRelation($event, $bindAgentId = NULL) |
|
|
|
|
{ |
|
|
|
|
$bindCheck = false; // 默认不绑定 |
|
|
|
|
|
|
|
|
|
// 该用户已经有上级 |
|
|
|
|
if ($this->user->parent_user_id !== NULL) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 不满足绑定下级事件 |
|
|
|
|
if ($this->config->getInviteLockEvent() !== $event) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch ($this->config->getInviteLockEvent()) { |
|
|
|
|
case 'share': |
|
|
|
|
$bindCheck = true; |
|
|
|
|
break; |
|
|
|
|
case 'pay': |
|
|
|
|
if ($this->user->total_consume > 0) { |
|
|
|
|
$bindCheck = true; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case 'agent': |
|
|
|
|
$bindCheck = true; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!$bindCheck) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($bindAgentId === NULL) { |
|
|
|
|
$bindAgentId = $this->getParentUserId(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!$bindAgentId) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$bindAgent = new Agent($bindAgentId); |
|
|
|
|
|
|
|
|
|
if (!$bindAgent->isAgentAvaliable()) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 允许绑定用户 |
|
|
|
|
$this->user->parent_user_id = $bindAgent->user->id; |
|
|
|
|
$this->user->save(); |
|
|
|
|
|
|
|
|
|
// 添加推广记录 |
|
|
|
|
LogModel::add($bindAgent->user->id, 'share', ['user' => $this->user]); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建[分销商升级&统计业绩]异步队列任务 |
|
|
|
|
* 为了防止计算量大而引起阻塞,使用异步递归 |
|
|
|
|
*/ |
|
|
|
|
public function createAsyncAgentUpgrade($user_id = 0) |
|
|
|
|
{ |
|
|
|
|
if ($user_id === 0) { |
|
|
|
|
$user_id = $this->user->id; |
|
|
|
|
} |
|
|
|
|
\think\Queue::push('\addons\shopro\job\Commission@agentUpgrade', [ |
|
|
|
|
'user_id' => $user_id |
|
|
|
|
], 'shopro'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 实时获取上级推荐人 |
|
|
|
|
*/ |
|
|
|
|
public function getParentUserId() |
|
|
|
|
{ |
|
|
|
|
if (empty($this->parentUserId)) { |
|
|
|
|
|
|
|
|
|
$this->parentUserId = 0; |
|
|
|
|
|
|
|
|
|
$parent_user_id = $this->user->parent_user_id; |
|
|
|
|
// 未直接绑定分销商,从分享记录查找最近的分销商 |
|
|
|
|
if ($parent_user_id === NULL) { |
|
|
|
|
$shareLog = ShareModel::hasWhere( |
|
|
|
|
'agent', |
|
|
|
|
function ($query) { |
|
|
|
|
return $query->where('status', 'in', [AgentModel::AGENT_STATUS_NORMAL, AgentModel::AGENT_STATUS_FREEZE]); |
|
|
|
|
} |
|
|
|
|
)->where('Share.user_id', $this->user->id)->order('id desc')->find(); |
|
|
|
|
|
|
|
|
|
if ($shareLog) { |
|
|
|
|
$parent_user_id = $shareLog['share_id']; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// 再次检查上级分销商是否可用 |
|
|
|
|
if ($parent_user_id > 0) { |
|
|
|
|
$parentUser = UserModel::where('id', $parent_user_id)->find(); |
|
|
|
|
$parentAgent = AgentModel::avaliable()->where(['user_id' => $parent_user_id])->find(); |
|
|
|
|
if ($parentUser && $parentAgent) { |
|
|
|
|
$this->parentUserId = $parentAgent->user_id; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $this->parentUserId; |
|
|
|
|
} |
|
|
|
|
} |