75 行
2.0 KiB
PHP
75 行
2.0 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro\service\partner;
|
|
|
|
use addons\shopro\service\commission\Agent as AgentService;
|
|
use app\admin\model\shopro\commission\Agent as AgentModel;
|
|
|
|
/**
|
|
* 从买家向上走关系链
|
|
*/
|
|
class ChainService
|
|
{
|
|
/**
|
|
* @param int $buyerId
|
|
* @param bool $includeSelf 是否把买家自己放进链头(酒道馆自购)
|
|
* @return array
|
|
*/
|
|
public function collect($buyerId, $includeSelf = false)
|
|
{
|
|
$nodes = [];
|
|
$max = PartnerConfig::chainMaxDepth();
|
|
$visited = [];
|
|
|
|
if ($includeSelf) {
|
|
$self = $this->node($buyerId);
|
|
if ($self) {
|
|
$nodes[] = $self;
|
|
$visited[$buyerId] = true;
|
|
}
|
|
}
|
|
|
|
$currentId = $buyerId;
|
|
for ($i = 0; $i < $max; $i++) {
|
|
$agent = new AgentService($currentId);
|
|
$parentId = $agent->getParentUserId();
|
|
if (!$parentId || isset($visited[$parentId])) {
|
|
break;
|
|
}
|
|
$node = $this->node($parentId);
|
|
if (!$node) {
|
|
break;
|
|
}
|
|
$nodes[] = $node;
|
|
$visited[$parentId] = true;
|
|
$currentId = $parentId;
|
|
}
|
|
|
|
return $nodes;
|
|
}
|
|
|
|
public function node($userId)
|
|
{
|
|
if (!$userId) {
|
|
return null;
|
|
}
|
|
$agent = AgentModel::where('user_id', $userId)
|
|
->where('status', 'in', ['normal', 'freeze'])
|
|
->find();
|
|
if (!$agent) {
|
|
return null;
|
|
}
|
|
return [
|
|
'user_id' => intval($agent->user_id),
|
|
'level' => intval($agent->level),
|
|
'is_venue' => intval($agent->is_venue),
|
|
'is_county' => intval($agent->is_county),
|
|
'county_code' => (string) $agent->county_code,
|
|
'last_self_order_time' => $agent->last_self_order_time ? intval($agent->last_self_order_time) : 0,
|
|
'direct_order_seq' => intval($agent->direct_order_seq),
|
|
'status' => $agent->status,
|
|
'agent' => $agent,
|
|
];
|
|
}
|
|
}
|