93 lines
3.1 KiB
PHP
93 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace App\Business;
|
||
|
||
use App\Models\Business\JstOrderLabelPlantingRule;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Throwable;
|
||
|
||
/**
|
||
* 聚水潭订单标签明细的「是否种菜订单」判断:
|
||
* - 按(公司 + 标签)匹配财务维护的映射表;
|
||
* - 订单货号含信誉订单虚拟条码(X00006)的直接判为「是」。
|
||
*
|
||
* 映射表由财务维护(判断种菜单.xlsx)并按公司缓存,页面与导出共用同一份结果;
|
||
* 映射表缺失或未导入时不报错,统一按「否」展示。
|
||
*/
|
||
class JstOrderLabelPlantingRules
|
||
{
|
||
private const TTL_HOURS = 24;
|
||
|
||
/** 该货号(信誉订单虚拟条码)的订单直接判为种菜订单。 */
|
||
public const PLANTING_ITEM_CODE = 'X00006';
|
||
|
||
/** @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 (DB::table('jst_order_label_planting_rules')
|
||
->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 [];
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 单条(标签 + 货号)的判断结果:标签命中映射表取其值;
|
||
* 货号含 X00006 的一律为「是」,其余未命中为「否」。
|
||
*/
|
||
public function flag(int $companyId, ?string $label, ?string $itemCode = null): string
|
||
{
|
||
$key = trim((string) $label);
|
||
$flag = $key === '' ? '否' : ($this->map($companyId)[$key] ?? '否');
|
||
|
||
if ($flag === '否' && $this->hasPlantingItemCode($itemCode)) {
|
||
return '是';
|
||
}
|
||
|
||
return $flag;
|
||
}
|
||
|
||
/** 货号(可能多个,逗号连接)里是否含信誉订单虚拟条码。 */
|
||
private function hasPlantingItemCode(?string $itemCode): bool
|
||
{
|
||
foreach (explode(',', (string) $itemCode) as $code) {
|
||
if (mb_strtoupper(trim($code)) === self::PLANTING_ITEM_CODE) {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
public function forget(int $companyId): void
|
||
{
|
||
Cache::forget($this->key($companyId));
|
||
app(PlantingOrderLabelLookup::class)->forget($companyId);
|
||
}
|
||
|
||
private function key(int $companyId): string
|
||
{
|
||
return "jst-order-labels:planting-rules:v1:{$companyId}";
|
||
}
|
||
}
|