77 行
2.5 KiB
PHP
77 行
2.5 KiB
PHP
<?php
|
||
|
||
namespace app\admin\command;
|
||
|
||
use app\common\library\CouponService;
|
||
use app\common\library\OrderClose;
|
||
use think\console\Command;
|
||
use think\console\Input;
|
||
use think\console\Output;
|
||
use think\Db;
|
||
|
||
/**
|
||
* 定时维护任务(建议 crontab 每 5 分钟执行一次)
|
||
*
|
||
* php think fc:settle
|
||
*
|
||
* 职责:
|
||
* 1. 佣金到期结算:status=0 且 settle_due<=now → status=1(T+N 结算周期)
|
||
* 2. 关闭超时未支付订单(24 小时)并退回优惠券
|
||
* 3. 同步「转账中」的提现单(回调缺失兜底)
|
||
*/
|
||
class FcSettle extends Command
|
||
{
|
||
protected function configure()
|
||
{
|
||
$this->setName('fc:settle')
|
||
->setDescription('分销佣金到期结算 / 关闭超时订单 / 同步转账状态');
|
||
}
|
||
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
$now = time();
|
||
|
||
/* 1. 佣金到期结算 */
|
||
$settled = Db::name('fc_commission')
|
||
->where('status', 0)
|
||
->where('settle_due', '<=', $now)
|
||
->update([
|
||
'status' => 1,
|
||
'settletime' => $now,
|
||
]);
|
||
$output->writeln('==> 佣金结算:' . $settled . ' 笔');
|
||
|
||
/* 2. 关闭超时未支付订单(统一走 OrderClose 规范化清理) */
|
||
$timeout = OrderClose::getTimeoutSeconds();
|
||
$expired = Db::name('fc_order')
|
||
->where('status', 'pending')
|
||
->where('createtime', '<', $now - $timeout)
|
||
->select();
|
||
|
||
$closed = 0;
|
||
foreach ($expired as $order) {
|
||
if (OrderClose::close($order, 'timeout_auto')) {
|
||
$closed++;
|
||
}
|
||
}
|
||
$output->writeln('==> 关闭超时订单:' . $closed . ' 笔(超时阈值:' . round($timeout / 60) . ' 分钟)');
|
||
|
||
/* 3. 同步转账中的提现单(最多 20 笔) */
|
||
$synced = 0;
|
||
if (class_exists('\app\common\library\WxTransfer') && \app\common\library\WxTransfer::isReady()) {
|
||
$rows = Db::name('fc_withdraw')->where('status', 'paying')->limit(20)->select();
|
||
foreach ($rows as $row) {
|
||
try {
|
||
\app\common\library\WxTransfer::sync((int)$row['id']);
|
||
$synced++;
|
||
} catch (\Throwable $e) {
|
||
// 单笔失败不影响其它
|
||
}
|
||
}
|
||
}
|
||
$output->writeln('==> 同步转账状态:' . $synced . ' 笔');
|
||
|
||
$output->writeln('==> 完成');
|
||
}
|
||
}
|