You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Console\Commands;
use App\Models\OrderItems;
use Carbon\Carbon;
use Illuminate\Console\Command;
class CheckOrderFee extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'order-items:check-fee
{start_date : 开始日期,格式为 YYYY-MM-DD}
{end_date : 结束日期,格式为 YYYY-MM-DD}
{--project_id= : 只检测指定项目的订单}
{--limit=50 : 表格展示的最大行数}';
/**
* The console command description.
*
* @var string
*/
protected $description = '检测指定日期范围内子订单的已保存管理费与当前算法之间的差异';
/**
* Execute the console command.
*/
public function handle(): int
{
$start = $this->argument('start_date');
$end = $this->argument('end_date');
try {
$startDate = Carbon::createFromFormat('Y-m-d', $start)->startOfDay();
$endDate = Carbon::createFromFormat('Y-m-d', $end)->endOfDay();
} catch (\Exception $exception) {
$this->error('日期格式错误,请使用 YYYY-MM-DD例如 2025-02-01');
return 1;
}
if ($startDate->gt($endDate)) {
$this->error('开始日期不能大于结束日期');
return 1;
}
$projectId = $this->option('project_id');
$limit = (int) $this->option('limit');
$limit = $limit > 0 ? $limit : 50;
$this->info(sprintf(
'检测区间:%s ~ %s%s',
$startDate->toDateString(),
$endDate->toDateString(),
$projectId ? ",项目 ID{$projectId}" : ''
));
$query = OrderItems::query()
->whereBetween('service_date', [$startDate->toDateString(), $endDate->toDateString()])
->with(['order', 'product']);
if ($projectId) {
$query->whereHas('order', function ($q) use ($projectId) {
$q->where('project_id', $projectId);
});
}
$total = 0;
$rowsForTable = [];
$query->chunkById(200, function ($items) use (&$total, &$rowsForTable, $limit) {
foreach ($items as $item) {
/** @var \App\Models\OrderItems $item */
$total++;
$storedFee = (float) $item->fee;
$calcItem = clone $item;
if ($item->relationLoaded('order')) {
$calcItem->setRelation('order', $item->getRelation('order'));
}
if ($item->relationLoaded('product')) {
$calcItem->setRelation('product', $item->getRelation('product'));
}
$calcItem->calculateFee();
$calculatedFee = (float) $calcItem->fee;
if (count($rowsForTable) < $limit) {
$rowsForTable[] = [
'ID' => $item->id,
'订单ID' => $item->order_id,
'日期' => $item->service_date,
'总价' => $item->total,
'原fee' => $storedFee,
'算法fee' => $calculatedFee,
'是否扣款' => $item->paid_at ? '是' : '否',
];
}
}
});
if (empty($rowsForTable)) {
$this->info('该时间段无子订单记录');
} else {
$this->table(
['ID', '订单ID', '日期', '总价', '原fee', '算法fee', '是否扣款'],
$rowsForTable
);
}
$this->info(sprintf('共检测 %d 条子订单。', $total));
return 0;
}
}