fix(commission): 修复分销商升级规则中下级分销商等级未向上兼容累计统计导致无法晋升的问题
这个提交包含在:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* 测试分销商升级向上兼容累计规则及目标账号升级验证
|
||||
*/
|
||||
define('APP_PATH', __DIR__ . '/../application/');
|
||||
define('ROOT_PATH', dirname(realpath(APP_PATH)) . '/');
|
||||
require_once __DIR__ . '/../thinkphp/base.php';
|
||||
require_once __DIR__ . '/../thinkphp/helper.php';
|
||||
|
||||
if (is_file(ROOT_PATH . '.env')) {
|
||||
$env = parse_ini_file(ROOT_PATH . '.env', true);
|
||||
foreach ($env as $key => $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;
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户