CaiWuXiTong/app/Console/Commands/RemindDropshipFreightUpload.php

75 lines
3.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use App\Http\Controllers\Business\DropshipFreightController;
use App\Models\Common\Company;
use App\Support\DropshipFreight\DropshipUploadReminder;
use Carbon\CarbonImmutable;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Throwable;
/** 每月 1–14 日自动提醒还没上传上月代发运费账单的厂家(14 日截止,逾期本月货款和运费不予结算)。 */
class RemindDropshipFreightUpload extends Command
{
protected $signature = 'dropship-freight:remind-upload
{--date= : 按哪天算,格式 Y-m-d,默认今天(北京时间)}
{--force : 不看开关、不看当天是否已发、不限 1–14 日}
{--dry-run : 只列出会提醒哪些厂家,不发}';
protected $description = '每月 1–14 日提醒还没上传上月代发运费账单的代发厂家';
public function handle(DropshipUploadReminder $reminder): int
{
$date = trim((string) $this->option('date'));
if ($date !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) !== 1) {
$this->error('日期格式无效,应为 Y-m-d。');
return self::FAILURE;
}
$today = $date !== '' ? CarbonImmutable::createFromFormat('Y-m-d', $date, DropshipUploadReminder::TIMEZONE)->startOfDay() : DropshipUploadReminder::today();
$force = (bool) $this->option('force');
if ($today->day > DropshipUploadReminder::DEADLINE_DAY && ! $force) {
$this->line($today->format('Y-m-d') . ':只在每月 1–' . DropshipUploadReminder::DEADLINE_DAY . ' 日提醒。');
return self::SUCCESS;
}
$month = DropshipUploadReminder::billMonth($today);
$failed = false;
foreach (Company::enabled()->orderBy('id')->get() as $company) {
$company->makeCurrent();
$label = "#{$company->id} {$company->name} {$month}";
if (! DropshipUploadReminder::enabled() && ! $force) {
$this->line($label . ':自动提醒已关闭');
continue;
}
$portal = route('business.supplier-portal.home', ['company_id' => $company->id]);
if ($this->option('dry-run')) {
$plan = $reminder->targets($month);
$this->line($label . ':会提醒 ' . count($plan['targets']) . ' 家:' . implode('、', array_map(static fn ($f): string => (string) $f->name, $plan['targets']))
. ';已上传 ' . count($plan['done']) . ' 家;没有代发发货 ' . count($plan['no_shipment']) . ' 家;没配机器人 ' . count($plan['no_robot']) . ' 家'
. ($plan['no_robot'] !== [] ? ':' . implode('、', $plan['no_robot']) : '') . ';入口 ' . $portal);
continue;
}
$lock = sprintf('dropship-upload-reminder:%d:%s:%s', $company->id, $month, $today->format('Y-m-d'));
if (! $force && ! Cache::add($lock, true, now()->addDays(2))) {
$this->line($label . ':今天已经提醒过');
continue;
}
try {
$result = $reminder->remind($month, $portal, null, $today);
$this->info($label . ':' . DropshipFreightController::reminderSummary($month, $result));
$failed = $failed || $result['failed'] !== [];
} catch (Throwable $error) {
Cache::forget($lock);
report($error);
$this->error($label . ':自动提醒失败:' . $error->getMessage());
$failed = true;
}
}
return $failed ? self::FAILURE : self::SUCCESS;
}
}