177 lines
10 KiB
PHP
177 lines
10 KiB
PHP
<?php
|
||
namespace app\plugins\vr_ticket\hook;
|
||
|
||
use think\facade\Db;
|
||
use app\plugins\vr_ticket\service\SeatSkuService;
|
||
|
||
class AdminGoodsSaveHandle
|
||
{
|
||
/**
|
||
* 商品保存钩子(同时响应 save_handle 和 save_thing_end 两个时机)
|
||
*/
|
||
public function handle($params = [])
|
||
{
|
||
$hookName = $params['hook_name'] ?? '';
|
||
|
||
// ──────────────────────────────────────────────────────
|
||
// 时机 1:plugins_service_goods_save_handle(事务前,修改待保存数据)
|
||
// - 把 vr_goods_config base64 解码写入 $data
|
||
// - 设置 item_type
|
||
// - 强制 is_exist_many_spec = 1
|
||
// ──────────────────────────────────────────────────────
|
||
if ($hookName === 'plugins_service_goods_save_handle') {
|
||
$postParams = $params['params'] ?? [];
|
||
|
||
if (isset($postParams['vr_is_ticket']) && $postParams['vr_is_ticket'] == 1) {
|
||
$params['data']['item_type'] = 'ticket';
|
||
$params['data']['is_exist_many_spec'] = 1;
|
||
|
||
$base64Config = $postParams['vr_goods_config_base64'] ?? '';
|
||
if (!empty($base64Config)) {
|
||
$jsonStr = base64_decode($base64Config);
|
||
if ($jsonStr !== false) {
|
||
$params['data']['vr_goods_config'] = $jsonStr;
|
||
}
|
||
}
|
||
} else {
|
||
$params['data']['item_type'] = 'normal';
|
||
$params['data']['vr_goods_config'] = '';
|
||
}
|
||
|
||
return ['code' => 0];
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────
|
||
// 时机 2:plugins_service_goods_save_thing_end(事务内,goods 已落表)
|
||
// 关键:此时 GoodsSpecificationsInsert + GoodsSaveBaseUpdate
|
||
// 已经执行完毕(它们处理的是表单原生规格数据)。
|
||
//
|
||
// 对于票务商品,我们需要:
|
||
// a) 删除原生流程产生的所有 spec 数据(表单垃圾)
|
||
// b) 用 BatchGenerate 重新生成 VR 座位级 SKU
|
||
// c) 回写 GoodsSpecType.value(让后台正确展示)
|
||
// d) 重新计算 goods 表的 price/inventory
|
||
// ──────────────────────────────────────────────────────
|
||
if ($hookName === 'plugins_service_goods_save_thing_end') {
|
||
$data = $params['data'] ?? [];
|
||
$goodsId = $params['goods_id'] ?? 0;
|
||
|
||
if ($goodsId > 0 && ($data['item_type'] ?? '') === 'ticket') {
|
||
// 直接从数据库读 vr_goods_config(全量查询,不加 field 限制,避免 ThinkPHP 软删除过滤导致查不到)
|
||
$goodsRow = Db::name('Goods')->find($goodsId);
|
||
$rawConfig = is_array($goodsRow) ? ($goodsRow['vr_goods_config'] ?? '') : '';
|
||
// 如果 DB 里没有( goodsRow 为空或 vr_goods_config 字段为空),fallback 到 params[data]
|
||
if (empty($rawConfig)) {
|
||
$rawConfig = $data['vr_goods_config'] ?? '';
|
||
}
|
||
|
||
// DEBUG: 记录关键值
|
||
$debugPath = defined('RUNTIME_PATH') ? RUNTIME_PATH . 'vr_debug.log' : '/tmp/vr_debug.log';
|
||
$debugInfo = ["[" . date('H:i:s') . "] goodsId=$goodsId, rawConfig_len=" . strlen($rawConfig) . ", rawConfig_preview=" . substr($rawConfig, 0, 200)];
|
||
if (!empty($rawConfig)) {
|
||
$configs = json_decode($rawConfig, true);
|
||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||
$configs = null;
|
||
}
|
||
|
||
if (is_array($configs) && !empty($configs)) {
|
||
// 0) 重建 template_snapshot — 前端不发送 template_snapshot,
|
||
// 当 template_snapshot 为空、或 selected_rooms 有值时,从 DB 重建
|
||
foreach ($configs as $i => &$config) {
|
||
$templateId = intval($config['template_id'] ?? 0);
|
||
$selectedRooms = $config['selected_rooms'] ?? [];
|
||
|
||
// 条件:snapshot 为空,或者前端有 selected_rooms
|
||
if ($templateId > 0 && (!empty($selectedRooms) || empty($config['template_snapshot']) || empty($config['template_snapshot']['rooms']))) {
|
||
$template = Db::name('vr_seat_templates')->find($templateId);
|
||
$seatMap = json_decode($template['seat_map'] ?? '{}', true);
|
||
$allRooms = $seatMap['rooms'] ?? [];
|
||
// DEBUG
|
||
$debugInfo[] = "templateId=$templateId, selectedRooms=" . json_encode($selectedRooms) . ", template_found=" . ($template ? 'YES' : 'NO') . ", seatMap_keys=" . (is_array($seatMap) ? implode(',', array_keys($seatMap)) : 'NOT_ARRAY') . ", allRooms_count=" . count($allRooms) . ", allRooms_ids=" . json_encode(array_column($allRooms, 'id'));
|
||
|
||
// ── v1→v3 兼容迁移 ──
|
||
// v1 旧格式没有 rooms 嵌套,只有 sections+map 扁平结构
|
||
if (empty($allRooms) && !empty($seatMap['sections'])) {
|
||
$v1Sections = $seatMap['sections'] ?? [];
|
||
$v1Map = $seatMap['map'] ?? [];
|
||
$v1Seats = $seatMap['seats'] ?? [];
|
||
$v1RoomId = $selectedRooms[0] ?? 'room_1';
|
||
$allRooms = [[
|
||
'id' => $v1RoomId,
|
||
'name' => $seatMap['venue']['name'] ?? '主馆',
|
||
'sections' => $v1Sections,
|
||
'map' => $v1Map,
|
||
'seats' => $v1Seats,
|
||
]];
|
||
}
|
||
|
||
// 按 selected_rooms 过滤(支持前端标准化的 "room_0" 格式双向兼容)
|
||
$selectedRoomIds = array_column(
|
||
array_filter($allRooms, function ($r) use ($selectedRooms) {
|
||
$rid = $r['id'] ?? '';
|
||
if (in_array($rid, $selectedRooms)) {
|
||
return true;
|
||
}
|
||
// 尝试加/减 "room_" 前缀匹配(PHP 7.x 兼容)
|
||
if (strpos($rid, 'room_') === 0 && in_array(substr($rid, 5), $selectedRooms)) {
|
||
return true;
|
||
}
|
||
if (in_array('room_' . $rid, $selectedRooms)) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}), null
|
||
);
|
||
|
||
$config['template_snapshot'] = [
|
||
'venue' => $seatMap['venue'] ?? [],
|
||
'rooms' => $selectedRoomIds,
|
||
];
|
||
}
|
||
}
|
||
unset($config); // 解除引用,避免后续误改
|
||
|
||
// 写入调试日志
|
||
@file_put_contents($debugPath, implode("\n", $debugInfo) . "\n", FILE_APPEND);
|
||
|
||
// 将填充后的完整 config 写回 goods 表
|
||
Db::name('Goods')->where('id', $goodsId)->update([
|
||
'vr_goods_config' => json_encode($configs, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||
]);
|
||
|
||
// a) 清空原生规格数据 —— 避免列偏移
|
||
Db::name('GoodsSpecType')->where('goods_id', $goodsId)->delete();
|
||
Db::name('GoodsSpecBase')->where('goods_id', $goodsId)->delete();
|
||
Db::name('GoodsSpecValue')->where('goods_id', $goodsId)->delete();
|
||
|
||
// b) 逐模板生成 VR SKU(ensureAndFillVrSpecTypes 在内部调用,type.value 同步写入)
|
||
foreach ($configs as $config) {
|
||
$templateId = intval($config['template_id'] ?? 0);
|
||
$selectedRooms = $config['selected_rooms'] ?? [];
|
||
$selectedSections = $config['selected_sections'] ?? [];
|
||
$sessions = $config['sessions'] ?? [];
|
||
|
||
if ($templateId > 0) {
|
||
$res = SeatSkuService::BatchGenerate(
|
||
$goodsId, $templateId,
|
||
$selectedRooms, $selectedSections, $sessions
|
||
);
|
||
if ($res['code'] !== 0) {
|
||
return $res;
|
||
}
|
||
}
|
||
}
|
||
|
||
// c) 重新计算 goods.price / goods.inventory
|
||
SeatSkuService::refreshGoodsBase($goodsId);
|
||
}
|
||
}
|
||
}
|
||
|
||
return ['code' => 0];
|
||
}
|
||
|
||
return ['code' => 0];
|
||
}
|
||
}
|