- 新增 invoice_title、showcase、wallet_log 模型及对应语言包 - 新增后台控制器、视图及搜索字段配置 - 修复服务类目删除保护逻辑 - 优化后台登录页面文案及样式 - 新增前端角色切换逻辑及页面拦截 - 新增 SVG 图标及样式变量 - 修复接口权限及下拉框默认值问题
133 行
5.5 KiB
PHP
133 行
5.5 KiB
PHP
<?php
|
||
|
||
namespace app\admin\command;
|
||
|
||
use app\common\library\OrderStatus;
|
||
use think\console\Command;
|
||
use think\console\Input;
|
||
use think\console\Output;
|
||
use think\Db;
|
||
|
||
/**
|
||
* 自动核销完工超过48小时未确认的工单守护任务
|
||
* 运行方式:php think order:autoverify
|
||
*/
|
||
class OrderAutoverify extends Command
|
||
{
|
||
protected function configure()
|
||
{
|
||
$this->setName('order:autoverify')
|
||
->setDescription('自动核销完工超过48小时未确认的工单');
|
||
}
|
||
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
$threshold = time() - (48 * 3600);
|
||
$orders = Db::name('order')
|
||
->where('status', OrderStatus::PENDING_VERIFY)
|
||
->where('finish_time', '>', 0)
|
||
->where('finish_time', '<=', $threshold)
|
||
->select();
|
||
|
||
$count = count($orders);
|
||
$output->writeln("[OrderAutoverify] 扫描到 {$count} 笔超48小时待核销工单");
|
||
|
||
$success = 0;
|
||
foreach ($orders as $order) {
|
||
$orderId = (int)$order['id'];
|
||
$now = time();
|
||
|
||
Db::startTrans();
|
||
try {
|
||
$lockedOrder = Db::name('order')->where('id', $orderId)->lock(true)->find();
|
||
if (!$lockedOrder || (int)$lockedOrder['status'] !== OrderStatus::PENDING_VERIFY || (int)$lockedOrder['settle_status'] === 1) {
|
||
Db::rollback();
|
||
continue;
|
||
}
|
||
|
||
$masterId = (int)$lockedOrder['master_id'];
|
||
$payAmount = (float)$lockedOrder['pay_amount'];
|
||
$rate = 10.00;
|
||
$commission = round($payAmount * $rate / 100, 2);
|
||
$settleAmount = round($payAmount - $commission, 2);
|
||
|
||
// 1. 更新订单为已完成
|
||
Db::name('order')->where('id', $orderId)->update([
|
||
'status' => OrderStatus::COMPLETED,
|
||
'verify_time' => $now,
|
||
'settle_status' => 1,
|
||
'settle_amount' => $settleAmount,
|
||
'updatetime' => $now,
|
||
]);
|
||
|
||
// 2. 插入订单日志(严格对齐 fa_order_log 真实列名)
|
||
Db::name('order_log')->insert([
|
||
'order_id' => $orderId,
|
||
'status' => OrderStatus::COMPLETED,
|
||
'remark' => '完工满48小时客户未确认,系统自动验收结算',
|
||
'operator_type' => 'system',
|
||
'operator_id' => 0,
|
||
'createtime' => $now,
|
||
]);
|
||
|
||
// 3. 写入结算表
|
||
Db::name('settle')->insert([
|
||
'order_id' => $orderId,
|
||
'master_id' => $masterId,
|
||
'order_amount' => $payAmount,
|
||
'commission_rate' => $rate,
|
||
'commission' => $commission,
|
||
'settle_amount' => $settleAmount,
|
||
'status' => 'settled',
|
||
'settle_time' => $now,
|
||
'createtime' => $now,
|
||
]);
|
||
|
||
// 4. 师傅钱包入账(行级悲观锁)
|
||
if ($masterId > 0) {
|
||
$master = Db::name('master')->where('id', $masterId)->lock(true)->find();
|
||
if ($master) {
|
||
$newMoney = round((float)$master['money'] + $settleAmount, 2);
|
||
Db::name('master')->where('id', $masterId)->update([
|
||
'money' => $newMoney,
|
||
'order_count' => (int)$master['order_count'] + 1,
|
||
'updatetime' => $now,
|
||
]);
|
||
Db::name('wallet_log')->insert([
|
||
'user_type' => 'master',
|
||
'user_id' => $masterId,
|
||
'type' => 'settle',
|
||
'amount' => $settleAmount,
|
||
'balance' => $newMoney,
|
||
'order_id' => $orderId,
|
||
'remark' => '订单超时自动核销结算入账',
|
||
'createtime' => $now,
|
||
]);
|
||
|
||
// 通知师傅(严格对齐 fa_message 真实列名,receive_id 传 fa_master.id)
|
||
Db::name('message')->insert([
|
||
'receive_type' => 'master',
|
||
'receive_id' => (int)$master['id'],
|
||
'type' => 'order',
|
||
'title' => '订单自动核销结算入账',
|
||
'content' => "订单 {$lockedOrder['order_no']} 完工满48小时已自动验收,收入 ¥{$settleAmount}",
|
||
'order_id' => $orderId,
|
||
'is_read' => 0,
|
||
'createtime' => $now,
|
||
]);
|
||
}
|
||
}
|
||
|
||
Db::commit();
|
||
$success++;
|
||
$output->writeln(" [OK] 订单 #{$orderId} ({$lockedOrder['order_no']}) 自动核销完成,结算 ¥{$settleAmount}");
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
$output->writeln(" [FAIL] 订单 #{$orderId} 自动核销异常: " . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
$output->writeln("[OrderAutoverify] 执行结束,成功自动核销 {$success} 笔");
|
||
}
|
||
}
|