85 行
3.6 KiB
PHP
85 行
3.6 KiB
PHP
<?php
|
||
|
||
namespace addons\shopro\console;
|
||
|
||
use think\console\Command;
|
||
use think\console\Input;
|
||
use think\console\Output;
|
||
use think\console\input\Option;
|
||
use addons\shopro\service\partner\WalletMigrationService;
|
||
|
||
/**
|
||
* 历史收益全量结转至钱包余额控制台命令
|
||
*/
|
||
class PartnerTransferCommission extends Command
|
||
{
|
||
protected function configure()
|
||
{
|
||
$this->setName('shopro:partner:transfer-commission')
|
||
->addOption('dry-run', 'd', Option::VALUE_NONE, '只读试算预览,不真实扣减与增加资金')
|
||
->addOption('force', 'f', Option::VALUE_NONE, '强制执行,不提示二次确认')
|
||
->setDescription('将所有会员账户已有佣金收益全量结转至钱包余额');
|
||
}
|
||
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
$dryRun = (bool) $input->getOption('dry-run');
|
||
$force = (bool) $input->getOption('force');
|
||
|
||
$output->writeln("========================================================");
|
||
$output->writeln(" 恒联数通 · 历史收益全量结转至钱包余额工具");
|
||
$output->writeln("========================================================");
|
||
|
||
$service = new WalletMigrationService();
|
||
$preview = $service->preview();
|
||
|
||
$output->writeln("待处理会员总数: {$preview['total_users']} 人");
|
||
$output->writeln("待结转收益总金额: ¥{$preview['total_commission']} 元");
|
||
|
||
if ($preview['total_users'] === 0) {
|
||
$output->writeln("\n当前无任何待结转佣金余额,无需处理。");
|
||
return 0;
|
||
}
|
||
|
||
$output->writeln("\n部分待结转会员抽样(前 10 名):");
|
||
foreach (array_slice($preview['samples'], 0, 10) as $i => $s) {
|
||
$idx = $i + 1;
|
||
$output->writeln(" [{$idx}] 会员ID: {$s['id']} | 手机: {$s['mobile']} | 当前佣金: ¥{$s['current_commission']} -> 钱包余额: ¥{$s['current_money']} (结转后预计: ¥{$s['target_money']})");
|
||
}
|
||
|
||
if ($dryRun) {
|
||
$output->writeln("\n【DRY-RUN 试算模式已结束】未对数据库发生任何资金变动。");
|
||
return 0;
|
||
}
|
||
|
||
if (!$force) {
|
||
$output->writeln("\n【安全警告】即将对全量 {$preview['total_users']} 名会员执行结转,将 ¥{$preview['total_commission']} 元佣金划入钱包余额!");
|
||
$output->writeln("若确认执行,请增加参数 --force 或 -f,例如:");
|
||
$output->writeln("php think shopro:partner:transfer-commission --force");
|
||
return 1;
|
||
}
|
||
|
||
$output->writeln("\n>>> 开始执行资金划转...");
|
||
$startTime = microtime(true);
|
||
|
||
$res = $service->transferAll(false, function ($current, $total, $userId, $amount, $err) use ($output) {
|
||
if ($err) {
|
||
$output->writeln(" [{$current}/{$total}] 会员 {$userId} 结转失败: {$err}");
|
||
} else {
|
||
$output->writeln(" [{$current}/{$total}] 会员 {$userId} 成功结转 ¥" . number_format($amount, 2, '.', '') . " 元至钱包余额");
|
||
}
|
||
});
|
||
|
||
$cost = round(microtime(true) - $startTime, 2);
|
||
|
||
$output->writeln("\n========================================================");
|
||
$output->writeln("结转完成!耗时: {$cost} 秒");
|
||
$output->writeln("成功处理: {$res['success_count']} 人");
|
||
$output->writeln("失败人数: {$res['fail_count']} 人");
|
||
$output->writeln("累计结转资金: ¥{$res['transferred_amount']} 元");
|
||
$output->writeln("========================================================");
|
||
|
||
return $res['fail_count'] > 0 ? 1 : 0;
|
||
}
|
||
}
|