123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <?php
- namespace addons\shopro\service\commission;
- use addons\shopro\service\commission\Config;
- use app\admin\model\shopro\user\User as UserModel;
- use app\admin\model\shopro\commission\Agent as AgentModel;
- use app\admin\model\shopro\commission\Level as LevelModel;
- use app\admin\model\shopro\commission\Log as LogModel;
- use app\admin\model\shopro\Share as ShareModel;
- class Agent
- {
- public $user;
- public $agent;
- public $config;
- public $parentUserId;
- public $nextAgentTeam;
- public $nextUserTeam;
-
- public function __construct($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);
- }
- $this->config = new Config();
- }
-
- public function getAgentStatus($autoCreate = false)
- {
- if (empty($this->agent)) {
-
- if ($autoCreate) {
- return $this->createNewAgent();
- }
- return NULL;
- }
- return $this->agent->status;
- }
-
- public function isAgentAvaliable()
- {
- $status = $this->getAgentStatus();
- if (in_array($status, [AgentModel::AGENT_STATUS_NORMAL, AgentModel::AGENT_STATUS_FREEZE])) {
- return true;
- }
- return false;
- }
-
- public function getAgentLevel()
- {
- if (empty($this->agent)) {
- return 0;
- }
- if (empty($this->agent->level_info)) {
- return 1;
- }
- return $this->agent->level_info->level;
- }
-
- public function getAgentUpgradeLock()
- {
- if (empty($this->agent)) {
- return true;
- }
- if ($this->agent->upgrade_lock == AgentModel::UPGRADE_LOCK_OPEN) {
- return true;
- }
- return false;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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 runAgentUpgradePlan($upgrade = true)
- {
- if ($this->isAgentAvaliable()) {
-
- $nextUserTeam = $this->getNextUserTeam();
- $nextAgentTeam = $this->getNextAgentTeam();
-
- $this->agent->child_user_count_1 = count($nextUserTeam);
-
- $this->agent->child_user_count_2 = array_sum(array_column($nextAgentTeam, 'child_user_count_1'));
-
- $this->agent->child_user_count_all = $this->agent->child_user_count_1 + array_sum(array_column($nextAgentTeam, 'child_user_count_all'));
-
- $this->agent->child_agent_count_1 = count($nextAgentTeam);
-
- $this->agent->child_agent_count_2 = array_sum(array_column($nextAgentTeam, 'child_agent_count_1'));
-
- $this->agent->child_agent_count_all = $this->agent->child_agent_count_1 + array_sum(array_column($nextAgentTeam, 'child_agent_count_all'));
-
- $this->agent->child_order_money_2 = array_sum(array_column($nextAgentTeam, 'child_order_money_1')) + array_sum(array_column($nextAgentTeam, 'child_order_money_0'));
-
- $this->agent->child_order_money_all = $this->agent->child_order_money_0 + $this->agent->child_order_money_1 + array_sum(array_column($nextAgentTeam, 'child_order_money_all'));
-
- $this->agent->child_order_count_2 = array_sum(array_column($nextAgentTeam, 'child_order_count_1')) + array_sum(array_column($nextAgentTeam, 'child_order_count_0'));
-
- $this->agent->child_order_count_all = $this->agent->child_order_count_0 + $this->agent->child_order_count_1 + array_sum(array_column($nextAgentTeam, 'child_order_count_all'));
-
- $child_agent_level_1 = array_count_values(array_column($nextAgentTeam, 'level'));
- ksort($child_agent_level_1);
- $this->agent->child_agent_level_1 = $child_agent_level_1;
-
- $child_agent_level_all = $this->childAgentLevelCount(array_column($nextAgentTeam, 'child_agent_level_all'), $this->agent->child_agent_level_1);
- ksort($child_agent_level_all);
- $this->agent->child_agent_level_all = $child_agent_level_all;
- $this->agent->save();
-
- if (!$this->getAgentUpgradeLock() && $upgrade) {
- $canUpgradeLevel = $this->checkAgentUpgradeLevel();
- 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();
- }
- }
- \think\Log::info('统计分销商业绩[ID=' . $this->user->id . '&nickname=' . $this->user->nickname . '] ---Ended');
- }
- $parentUserId = $this->getParentUserId();
- if ($parentUserId) {
- $this->createAsyncAgentUpgrade($parentUserId);
- }
- }
-
- private function childAgentLevelCount($childAgentLevelArray, $childAgentLevel1Array)
- {
- $childAgentLevelCount = [];
- foreach ($childAgentLevelArray as &$agentLevel) {
- if (!empty($agentLevel)) {
- $agentLevel = json_decode($agentLevel, true);
- array_walk($agentLevel, function ($count, $level) use (&$childAgentLevelCount) {
- if (isset($childAgentLevelCount[$level])) {
- $childAgentLevelCount[$level] += $count;
- } else {
- $childAgentLevelCount[$level] = $count;
- }
- });
- }
- }
- array_walk($childAgentLevel1Array, function ($count, $level) use (&$childAgentLevelCount) {
- if (isset($childAgentLevelCount[$level])) {
- $childAgentLevelCount[$level] += $count;
- } else {
- $childAgentLevelCount[$level] = $count;
- }
- });
- return $childAgentLevelCount;
- }
-
- 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 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;
- }
-
- private function getNextAgentLevel()
- {
- $nextAgentLevel = [];
- $agentLevel = $this->getAgentLevel();
- if ($agentLevel) {
- $nextAgentLevel = LevelModel::where('level', '>', $agentLevel)->order('level asc')->select();
- }
- return $nextAgentLevel;
- }
-
- private function checkAgentUpgradeLevel()
- {
- $nextAgentLevel = $this->getNextAgentLevel();
- if (count($nextAgentLevel)) {
- foreach ($nextAgentLevel as $level) {
- $checkLevel[$level->level] = $this->isMatchUpgradeLevelRule($level);
-
- 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)
- {
- foreach ($level->upgrade_rules as $name => $value) {
- $match[$name] = false;
- switch ($name) {
- case 'total_consume':
- $match[$name] = $this->user->$name >= $value;
- break;
- case 'child_user_count_all':
- case 'child_user_count_1':
- case 'child_user_count_2':
- case 'child_order_money_0':
- case 'child_order_money_1':
- case 'child_order_money_2':
- case 'child_order_money_all':
- case 'child_order_count_0':
- case 'child_order_count_1':
- case 'child_order_count_2':
- case 'child_order_count_all':
- case 'child_agent_count_1':
- case 'child_agent_count_2':
- case 'child_agent_count_all':
- $match[$name] = $this->agent->$name >= $value;
- break;
- case 'child_agent_level_1':
- case 'child_agent_level_all':
- $match[$name] = true;
- if (count($value) > 0) {
- if (empty($this->agent->$name)) {
- $match[$name] = false;
- } else {
- foreach ($value as $k => $row) {
- if (!isset(($this->agent->$name)[$row['level']]) || ($this->agent->$name)[$row['level']] < $row['count']) {
- $match[$name] = false;
- break;
- }
- }
- }
- }
- break;
- }
-
- if (!$level->upgrade_type && $match[$name]) {
- return true;
- break;
- }
-
- if ($level->upgrade_type && !$match[$name]) {
- return false;
- break;
- }
- }
-
- return boolval($level->upgrade_type);
- }
- }
|