CaiWuXiTong/app/Console/Commands/WarmReportCaches.php

86 lines
3.5 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Modules\EcommerceFinance\Repositories\SmallPaymentWorkOrderMirrorRepository;
use Modules\EcommerceFinance\Repositories\StoreReceivingAlipayBillMirrorRepository;
use Modules\EcommerceFinance\Repositories\ThirdPartyWarehouseStorageMirrorRepository;
use Throwable;
class WarmReportCaches extends Command
{
protected $signature = 'business-mirror:warm-report-caches';
protected $description = '预热默认进销存、淘系支付宝与小额打款报表,避免镜像切换后的首批请求重复扫表';
public function handle(
ThirdPartyWarehouseStorageMirrorRepository $inventory,
StoreReceivingAlipayBillMirrorRepository $alipay,
SmallPaymentWorkOrderMirrorRepository $smallPayments,
): int {
$failed = false;
try {
$month = now()->startOfMonth()->subMonth()->format('Y-m');
$result = $inventory->paginateMonthlyInventoryReport([
'month' => $month,
'product_code' => '',
'warehouse_provider' => '',
'category' => '',
], 1, 25);
$this->info(sprintf('进销存 %s 首页已预热,共 %d 行。', $month, $result['total']));
} catch (Throwable $exception) {
$failed = true;
Log::error('默认进销存报表缓存预热失败。', ['exception' => $exception::class]);
$this->error('默认进销存报表缓存预热失败。');
}
try {
$result = $alipay->paginate([], 'occurred_at', 'desc', 1, 10);
$this->info(sprintf('淘系支付宝首页已预热,共 %d 行。', $result['total']));
$options = $alipay->filterOptionsForChannel(
'alipayCSV',
StoreReceivingAlipayBillMirrorRepository::DATASET,
true,
);
$this->info(sprintf(
'淘系支付宝筛选选项已预热,共 %d 个店铺、%d 个账单项目。',
count($options['shops']),
count($options['bill_items']),
));
} catch (Throwable $exception) {
$failed = true;
Log::error('淘系支付宝报表缓存预热失败。', ['exception' => $exception::class]);
$this->error('淘系支付宝报表缓存预热失败。');
}
try {
$result = $smallPayments->paginate([
'from' => now()->subDays(29)->format('Y-m-d') . 'T00:00:00',
'to' => now()->format('Y-m-d') . 'T23:59:59',
'work_order_no' => '',
'shop_name' => '',
'transaction_no' => '',
'online_order_no' => '',
'status' => '',
'payment_reason' => '',
], 1, 25);
$options = $smallPayments->filterOptions();
$this->info(sprintf(
'小额打款首页已预热,共 %d 行、%d 个店铺、%d 个打款原因。',
$result['total'],
count($options['shops']),
count($options['payment_reasons']),
));
} catch (Throwable $exception) {
$failed = true;
Log::error('小额打款报表缓存预热失败。', ['exception' => $exception::class]);
$this->error('小额打款报表缓存预热失败。');
}
return $failed ? self::FAILURE : self::SUCCESS;
}
}