文件
xionggeshop/addons/shopro/refresh_commission_team_stats.php

76 行
2.1 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
/**
* 刷新分销团队统计(child_user_count* / child_agent_count*)
*
* 用法(在项目根目录):
* php addons/shopro/refresh_commission_team_stats.php
* php addons/shopro/refresh_commission_team_stats.php 910006
*
* 说明:Shopro 默认把统计任务投递到 shopro 队列;若守护进程未监听该队列,
* 绑定/支付后团队人数可能不更新。本脚本可手动全量或指定用户刷新。
*/
define('APP_PATH', __DIR__ . '/../../application/');
require __DIR__ . '/../../thinkphp/base.php';
\think\App::initCommon();
use think\Db;
use addons\shopro\library\commission\Agent;
$targetUserId = isset($argv[1]) ? intval($argv[1]) : 0;
function getUserDepth($userId)
{
$depth = 0;
$visited = [];
while ($userId > 0 && !isset($visited[$userId])) {
$visited[$userId] = true;
$parentId = intval(Db::name('user')->where('id', $userId)->value('parent_user_id'));
if ($parentId <= 0) {
break;
}
$depth++;
$userId = $parentId;
if ($depth > 50) {
break;
}
}
return $depth;
}
function refreshUserStats($userId)
{
$agent = new Agent($userId);
if (!$agent->user) {
return false;
}
$agent->runUpgradeAgentPlan(false);
return true;
}
if ($targetUserId > 0) {
if (!refreshUserStats($targetUserId)) {
echo "用户 {$targetUserId} 不存在\n";
exit(1);
}
$user = Db::name('user')->where('id', $targetUserId)->field('child_user_count,child_user_count_1,child_user_count_2')->find();
echo "已刷新用户 {$targetUserId}:团队 {$user['child_user_count']},直推 {$user['child_user_count_1']},间推 {$user['child_user_count_2']}\n";
exit(0);
}
$userIds = Db::name('user')->column('id');
usort($userIds, function ($a, $b) {
return getUserDepth($b) <=> getUserDepth($a);
});
$ok = 0;
foreach ($userIds as $userId) {
if (refreshUserStats($userId)) {
$ok++;
}
}
echo "团队统计刷新完成,共处理 {$ok} 个用户\n";
echo "请确认守护进程命令包含:php think queue:listen --queue shopro\n";