- 新表 jst_order_label_planting_rules(公司 + 标签 → 是/否)+ 模型 + 缓存服务 JstOrderLabelPlantingRules - 新增导入命令 jst-order-labels:import-planting-rules,读取财务「判断种菜单.xlsx」(表头:标签 / 是否种菜订单),按 --company 导入并清缓存 - 页面:下单时间后面新增「是否种菜订单」列,逐行用(公司 + 标签)匹配映射表,命中为是、未命中为否 - 导出:CSV 同步新增该列(页面/导出同口径) - 本地:build-frontend.ps1 通过;6 个改动/新增 PHP 文件 php -l 通过
68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
||
|
||
namespace App\Business;
|
||
|
||
use App\Models\Business\JstOrderLabelPlantingRule;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Throwable;
|
||
|
||
/**
|
||
* 聚水潭订单标签明细的「是否种菜订单」判断:按(公司 + 标签)匹配映射表。
|
||
*
|
||
* 映射表由财务维护(判断种菜单.xlsx)并按公司缓存,页面与导出共用同一份结果;
|
||
* 映射表缺失或未导入时不报错,统一按「否」展示。
|
||
*/
|
||
class JstOrderLabelPlantingRules
|
||
{
|
||
private const TTL_HOURS = 24;
|
||
|
||
/** @return array<string,string> [标签 => 是/否] */
|
||
public function map(int $companyId): array
|
||
{
|
||
return Cache::remember($this->key($companyId), now()->addHours(self::TTL_HOURS), function () use ($companyId): array {
|
||
try {
|
||
if (! Schema::hasTable('jst_order_label_planting_rules')) {
|
||
return [];
|
||
}
|
||
|
||
$map = [];
|
||
foreach (JstOrderLabelPlantingRule::query()
|
||
->where('company_id', $companyId)
|
||
->get(['label', 'is_planting']) as $row) {
|
||
$label = trim((string) $row->label);
|
||
if ($label === '') {
|
||
continue;
|
||
}
|
||
$map[$label] = trim((string) $row->is_planting) === '否' ? '否' : '是';
|
||
}
|
||
|
||
return $map;
|
||
} catch (Throwable) {
|
||
return [];
|
||
}
|
||
});
|
||
}
|
||
|
||
/** 单个标签的判断结果:命中映射表取映射值(默认是),未命中为否。 */
|
||
public function flag(int $companyId, ?string $label): string
|
||
{
|
||
$key = trim((string) $label);
|
||
if ($key === '') {
|
||
return '否';
|
||
}
|
||
|
||
return $this->map($companyId)[$key] ?? '否';
|
||
}
|
||
|
||
public function forget(int $companyId): void
|
||
{
|
||
Cache::forget($this->key($companyId));
|
||
}
|
||
|
||
private function key(int $companyId): string
|
||
{
|
||
return "jst-order-labels:planting-rules:v1:{$companyId}";
|
||
}
|
||
}
|