From 511a8cb485af41cf62f7097c3f526be7f61600ff Mon Sep 17 00:00:00 2001 From: chen Date: Tue, 29 Sep 2026 16:07:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(commission):=20=E4=BF=AE=E5=A4=8D=E5=88=86?= =?UTF-8?q?=E9=94=80=E5=95=86=E5=8D=87=E7=BA=A7=E8=A7=84=E5=88=99=E4=B8=AD?= =?UTF-8?q?=E4=B8=8B=E7=BA=A7=E5=88=86=E9=94=80=E5=95=86=E7=AD=89=E7=BA=A7?= =?UTF-8?q?=E6=9C=AA=E5=90=91=E4=B8=8A=E5=85=BC=E5=AE=B9=E7=B4=AF=E8=AE=A1?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E6=99=8B?= =?UTF-8?q?=E5=8D=87=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/shopro/service/commission/Agent.php | 48 +++++++-- tests/test_agent_upgrade_cumulative.php | 116 +++++++++++++++++++++ 2 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 tests/test_agent_upgrade_cumulative.php diff --git a/addons/shopro/service/commission/Agent.php b/addons/shopro/service/commission/Agent.php index 469e642..8f619af 100755 --- a/addons/shopro/service/commission/Agent.php +++ b/addons/shopro/service/commission/Agent.php @@ -337,13 +337,32 @@ class Agent // 团队用户人数 = 一级用户人数 + 一级用户的团队用户人数 $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); + // 一级有效分销商与等级统计 (优化:等级 1 品酒师必须有自购计奖商品 child_order_count_0 >= 1 才算有效品酒师) + $effectiveNextLevels = []; + $effectiveAgentCount1 = 0; + foreach ($nextAgentTeam as $agMember) { + $agLv = intval($agMember['level'] ?? 1); + $selfBuyOrderCount = intval($agMember['child_order_count_0'] ?? 0); + + if ($agLv === 1) { + if ($selfBuyOrderCount >= 1) { + $effectiveNextLevels[] = 1; + $effectiveAgentCount1++; + } + // 未自购计奖商品者属于普通游客,不计入品酒师,亦不计入一级分销商人数 + } elseif ($agLv >= 2) { + $effectiveNextLevels[] = $agLv; + $effectiveAgentCount1++; + } + } + + // 一级有效分销商人数 + $this->agent->child_agent_count_1 = $effectiveAgentCount1; // 二级分销商人数 = 一级分销商的一级分销商人数 $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')); // 二级分销订单金额 = 一级分销商的一级分销订单金额 + 一级分销商的自购订单金额 @@ -358,8 +377,8 @@ class Agent // 团队分销订单数量 = 自购分销订单数量 + 一级分销订单数量 + 一级所有分销商的团队分销订单总数量 $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')); + // 一级分销商等级统计 (仅统计自购了计奖商品的有效品酒师及创客) + $child_agent_level_1 = array_count_values($effectiveNextLevels); ksort($child_agent_level_1); $this->agent->child_agent_level_1 = $child_agent_level_1; @@ -520,11 +539,26 @@ class Agent case 'child_agent_level_all': // 团队分销商等级统计 $match[$name] = true; if (count($value) > 0) { - if (empty($this->agent->$name)) { + $currentLevelDist = $this->agent->$name; + if (is_string($currentLevelDist)) { + $currentLevelDist = json_decode($currentLevelDist, true) ?: []; + } + $currentLevelDist = (array)$currentLevelDist; + + if (empty($currentLevelDist)) { $match[$name] = false; } else { foreach ($value as $k => $row) { - if (!isset(($this->agent->$name)[$row['level']]) || ($this->agent->$name)[$row['level']] < $row['count']) { + $targetLevel = intval($row['level'] ?? 0); + $targetCount = intval($row['count'] ?? 0); + $actualCount = 0; + // 向上兼容累计:当下级达到或超过目标等级时,均符合该等级的人数考核要求 + foreach ($currentLevelDist as $curLv => $cnt) { + if (intval($curLv) >= $targetLevel) { + $actualCount += intval($cnt); + } + } + if ($actualCount < $targetCount) { $match[$name] = false; break; } diff --git a/tests/test_agent_upgrade_cumulative.php b/tests/test_agent_upgrade_cumulative.php new file mode 100644 index 0000000..7ba05a3 --- /dev/null +++ b/tests/test_agent_upgrade_cumulative.php @@ -0,0 +1,116 @@ + $val) { + $name = 'PHP_' . strtoupper($key); + if (is_array($val)) { + foreach ($val as $k => $v) { + $item = $name . '_' . strtoupper($k); + putenv("$item=$v"); + } + } else { + putenv("$name=$val"); + } + } +} + +\think\Config::set(\think\Config::load(APP_PATH . 'config.php')); +\think\Config::set(include APP_PATH . 'database.php', 'database'); +foreach (glob(APP_PATH . 'extra/*.php') as $extraFile) { + $extraName = basename($extraFile, '.php'); + \think\Config::set($extraName, include $extraFile); +} +\think\Loader::addNamespace('app', APP_PATH); +\think\Loader::addNamespace('addons', ROOT_PATH . 'addons/'); +require_once __DIR__ . '/../application/common.php'; +require_once __DIR__ . '/../addons/shopro/helper/helper.php'; + +$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; +@ini_set('session.use_cookies', '0'); +\think\Session::init(['auto_start' => true]); +$auth = \app\admin\library\Auth::instance(); +$admin = \app\admin\model\Admin::get(1); +if ($admin) { + $admin->loginip = '127.0.0.1'; + $admin->token = \fast\Random::uuid(); + $admin->save(); + \think\Session::set('admin', $admin->toArray()); + \think\Session::set('admin.safecode', $auth->getEncryptSafecode($admin)); + $ref = new \ReflectionProperty($auth, 'logined'); + $ref->setAccessible(true); + $ref->setValue($auth, true); +} + +use addons\shopro\service\commission\Agent as AgentService; +use app\admin\model\shopro\commission\Agent as AgentModel; +use app\admin\model\shopro\commission\Level as LevelModel; + +echo "=== 开始分销商升级规则验证 ===" . PHP_EOL; + +// 1. 获取目标测试账号(User ID 8) +$userId = 8; +$agent = AgentModel::where('user_id', $userId)->find(); +if (!$agent) { + die("未找到用户 ID {$userId} 的分销商记录\n"); +} + +echo "当前账号 ID: {$userId}, 等级: {$agent->level}" . PHP_EOL; +echo "child_user_count_1: {$agent->child_user_count_1}" . PHP_EOL; +echo "child_order_count_all: {$agent->child_order_count_all}" . PHP_EOL; +echo "child_agent_count_1: {$agent->child_agent_count_1}" . PHP_EOL; +echo "child_agent_level_1: " . json_encode($agent->child_agent_level_1, JSON_UNESCAPED_UNICODE) . PHP_EOL; + +// 2. 模拟匹配测试各等级升级规则 +$agentService = new AgentService($userId); + +$levels = LevelModel::order('level asc')->select(); +foreach ($levels as $lvl) { + if ($lvl->level <= 1) continue; + $matched = $agentService->isMatchUpgradeLevelRule($lvl); + echo "等级 [{$lvl->level}] {$lvl->name} 匹配结果: " . ($matched ? "【符合】" : "【不符合】") . PHP_EOL; +} + +// 3. 执行升级计划 +echo "执行升级计划 runAgentUpgradePlan(true)..." . PHP_EOL; +$agentService->runAgentUpgradePlan(true); + +// 4. 重新查询分销商最新等级 +$agentUpdated = AgentModel::where('user_id', $userId)->find(); +echo "升级后账号 ID: {$userId}, 最新等级: {$agentUpdated->level}" . PHP_EOL; +echo "升级后 child_agent_level_1: " . json_encode($agentUpdated->child_agent_level_1, JSON_UNESCAPED_UNICODE) . PHP_EOL; + +// 5. 校验结果 +if ($agentUpdated->level > 1) { + echo ">>> SUCCESS: 账号已成功自动晋升!最新等级为: {$agentUpdated->level} <<<" . PHP_EOL; +} else { + echo ">>> FAILED: 账号仍未能晋升! <<<" . PHP_EOL; +} + +// 6. 验证后台详情 API 返回数据 +echo PHP_EOL . "=== 验证后台详情接口 (Agent::detail) 返回数据 ===" . PHP_EOL; +$request = \think\Request::instance(); +$request->get(['id' => $userId]); +$request->header(['accept' => 'application/json']); +$controller = new \app\admin\controller\shopro\commission\Agent($request); + +try { + $controller->detail($userId); +} catch (\think\exception\HttpResponseException $e) { + $resp = $e->getResponse(); + $detailData = $resp->getData()['data'] ?? []; + $radar = $detailData['operational_stats']['upgrade_radar']; + + echo "当前身份等级: " . $detailData['level'] . " (" . $radar['current_level_name'] . ")" . PHP_EOL; + echo "下一目标等级: " . $radar['next_level'] . " (" . $radar['next_level_name'] . ")" . PHP_EOL; + echo "直推品酒师指标: " . $radar['current_direct_agents'] . " / " . $radar['target_direct_agents'] . " (达标: " . ($radar['direct_agent_reached'] ? "是" : "否") . ")" . PHP_EOL; + echo "团队订单指标: " . $radar['current_team_orders'] . " / " . $radar['target_team_orders'] . " (缺: " . $radar['team_order_gap'] . " 单, 达标: " . ($radar['team_order_reached'] ? "是" : "否") . ")" . PHP_EOL; + echo "全部条件达标: " . ($radar['all_reached'] ? "已全部达标" : "尚未全部达标") . PHP_EOL; +}