CaiWuXiTong/app/Business/JstOrderLabelPlantingRules.php
qiuxiang b53555b74c 修复「是否种菜订单」映射读取为空:补 deleted_at 列并改用表查询
根因:映射表模型继承的基类带 SoftDeletes,而新建的 jst_order_label_planting_rules 没有 deleted_at 列,
查询直接报错(被服务兜底成空映射),页面全部显示「否」。
- 新增迁移 2026_10_07_130000 补 deleted_at(建表迁移也同步补上,供全新安装)
- 服务改用 DB 表查询,避开基类软删除/公司作用域与模型缓存,导入后立即失效重读
2026-10-07 11:19:53 +08:00

70 lines
2.2 KiB
PHP
Raw 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\Business;
use App\Models\Business\JstOrderLabelPlantingRule;
use Illuminate\Support\Facades\DB;
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 (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 [];
}
});
}
/** 单个标签的判断结果:命中映射表取映射值(默认是),未命中为否。 */
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}";
}
}