vr-shopxo-plugin/shopxo/app/plugins/vr_ticket/hook/AdminGoodsSaveHandle.php

100 lines
4.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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'] ?? '';
// ──────────────────────────────────────────────────────
// 时机 1plugins_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];
}
// ──────────────────────────────────────────────────────
// 时机 2plugins_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') {
$rawConfig = $data['vr_goods_config'] ?? '';
if (!empty($rawConfig)) {
$configs = json_decode($rawConfig, true);
if (is_array($configs) && !empty($configs)) {
// 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 SKUensureAndFillVrSpecTypes 在内部调用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];
}
}