You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.6 KiB

<?php
namespace App\Support;
use App\Models\Venue;
/**
* 场馆审核对比:快照(已通过)与待审内容的字段差异。
*/
final class VenueAuditDiff
{
/** @var array<string, string> */
public const FIELD_LABELS = [
'name' => '场馆名称',
'venue_type' => '主题',
'venue_types' => '主题(多选)',
'unit_name' => '所属单位',
'district' => '行政区',
'ticket_type' => '门票类型',
'appointment_type' => '预约类型',
'booking_mode' => '预约模式',
'open_mode' => '开放模式',
'open_time' => '开放时间',
'reservation_notice' => '预约须知',
'ticket_content' => '门票说明',
'booking_method' => '预约方式',
'visit_form' => '参观形式',
'consultation_hours' => '咨询预约时间',
'booking_qr_media' => '预约二维码',
'address' => '场馆地址',
'contact_phone' => '联系电话',
'lat' => '纬度',
'lng' => '经度',
'cover_image' => '封面图',
'gallery_media' => '轮播图与视频',
'detail_html' => '场馆详情',
'live_people_count' => '实时在馆人数',
'sort' => '排序',
'is_active' => '上架',
'is_included_in_stats' => '纳入人数统计',
];
/**
* @return list<array{field: string, label: string, before: string, after: string}>
*/
public static function diff(Venue $venue): array
{
$snapshot = $venue->last_approved_snapshot;
if (! is_array($snapshot) || $snapshot === []) {
return [];
}
$out = [];
foreach (Venue::SNAPSHOT_KEYS as $key) {
$before = self::normalizeValue($snapshot[$key] ?? null);
$after = self::normalizeValue($venue->getAttribute($key));
if ($before === $after) {
continue;
}
$out[] = [
'field' => $key,
'label' => self::FIELD_LABELS[$key] ?? $key,
'before' => $before,
'after' => $after,
];
}
return $out;
}
private static function normalizeValue(mixed $v): string
{
if ($v === null) {
return '';
}
if (is_bool($v)) {
return $v ? '是' : '否';
}
if (is_array($v)) {
if ($v === []) {
return '';
}
$encoded = json_encode($v, JSON_UNESCAPED_UNICODE);
return is_string($encoded) ? $encoded : '';
}
return trim((string) $v);
}
}