feat: 重构商品定价系统,新增步梯搬运费配置与支付台账统计
- 新增商品服务定价配置表,支持多规格服务价格体系 - 新增步梯搬运费配置功能,可按楼层阶梯收费 - 重构支付订单台账,新增核心数据统计卡片 - 优化下单流程,支持定价配置校验与增项计算 - 整合财务配置,移除独立充值配置菜单
这个提交包含在:
@@ -3,6 +3,7 @@
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\ProductPricing;
|
||||
use think\Db;
|
||||
use think\exception\PDOException;
|
||||
use think\exception\ValidateException;
|
||||
@@ -28,6 +29,11 @@ class Service extends Backend
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\Service;
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
if (in_array($this->request->action(), ['index', 'detail', 'pricing_main', 'add', 'edit'], true) && !$this->request->isPost()) {
|
||||
$site = $this->view->config['site'];
|
||||
$site['version'] .= '.service-floor-fees-zero';
|
||||
$this->assignconfig('site', $site);
|
||||
}
|
||||
|
||||
// 获取统一服务字典表(fa_service_type,不绑定单一类目,主服务与增项服务同源)
|
||||
$typesPool = Db::name('service_type')
|
||||
@@ -125,27 +131,30 @@ class Service extends Backend
|
||||
->select();
|
||||
$specMap = [];
|
||||
foreach ($allSpecs as $sp) {
|
||||
$sid = (int)$sp['service_id'];
|
||||
if (!isset($specMap[$sid])) {
|
||||
$specMap[$sid] = [];
|
||||
}
|
||||
$pricesMap = !empty($sp['service_prices']) ? (json_decode($sp['service_prices'], true) ?: []) : [];
|
||||
$spPrice = (float)$sp['base_price'];
|
||||
if (!empty($pricesMap)) {
|
||||
$priceTips = [];
|
||||
foreach ($pricesMap as $pCode => $pVal) {
|
||||
$priceTips[] = $pCode . ':¥' . round($pVal);
|
||||
}
|
||||
$specMap[$sid][] = $sp['name'] . ' (' . implode(',', $priceTips) . ')';
|
||||
} elseif ($spPrice > 0) {
|
||||
$specMap[$sid][] = $sp['name'] . '(¥' . round($spPrice) . ')';
|
||||
} else {
|
||||
$specMap[$sid][] = $sp['name'];
|
||||
}
|
||||
$specMap[$sp['service_id']][] = $sp;
|
||||
}
|
||||
$dictionary = ProductPricing::dictionary();
|
||||
foreach ($rows as &$r) {
|
||||
$sid = (int)$r['id'];
|
||||
$r['specs_list'] = isset($specMap[$sid]) ? $specMap[$sid] : [];
|
||||
$config = ProductPricing::read($r->getData());
|
||||
$r['specs_list'] = array_map(function ($spec) use ($config) {
|
||||
$name = $spec['name'];
|
||||
if ($config) {
|
||||
if (isset($config['spec_configs'][$spec['id']])) {
|
||||
return $name . '(独立配置)';
|
||||
}
|
||||
return isset($config['specs'][$spec['id']]) ? $name . '(已单独设价)' : $name;
|
||||
}
|
||||
$prices = json_decode($spec['service_prices'] ?? '', true) ?: [];
|
||||
if ($prices) {
|
||||
$tips = [];
|
||||
foreach ($prices as $code => $price) {
|
||||
$tips[] = $code . ':¥' . round($price);
|
||||
}
|
||||
return $name . ' (' . implode(',', $tips) . ')';
|
||||
}
|
||||
return $spec['base_price'] > 0 ? $name . '(¥' . round($spec['base_price']) . ')' : $name;
|
||||
}, $specMap[$sid] ?? []);
|
||||
|
||||
// 直接在服务端解析为数组返回,防止前端 bootstrap-table escape:true 将 JSON 双引号转义为 " 导致解析失败
|
||||
$stArr = [];
|
||||
@@ -156,6 +165,20 @@ class Service extends Backend
|
||||
if (!empty($r['addons'])) {
|
||||
$adArr = is_array($r['addons']) ? $r['addons'] : (json_decode($r['addons'], true) ?: []);
|
||||
}
|
||||
if ($config) {
|
||||
$names = [];
|
||||
foreach ($stArr as $type) {
|
||||
$names[$type['code']] = $type['name'];
|
||||
}
|
||||
$addonNames = array_column($adArr, 'name', 'code');
|
||||
$adArr = [];
|
||||
foreach ($config['services'] as $code => $type) {
|
||||
foreach ($type['addons'] as $addon => $cents) {
|
||||
$adArr[] = ['name' => ($names[$code] ?? $code) . '加购' . ($addonNames[$addon] ?? $addon), 'price' => $cents / 100, 'unit' => $dictionary[$addon]['unit'] ?? ''];
|
||||
}
|
||||
}
|
||||
$r->hidden(['pricing_config']);
|
||||
}
|
||||
$r['service_types_list'] = $stArr;
|
||||
$r['addons_list'] = $adArr;
|
||||
}
|
||||
@@ -180,11 +203,13 @@ class Service extends Backend
|
||||
$params[$this->dataLimitField] = $this->auth->id;
|
||||
}
|
||||
|
||||
$params = $this->formatServiceConfig($params);
|
||||
$params = array_diff_key($params, array_flip(['pricing_config', 'service_types', 'addons', 'base_price', 'price_type']));
|
||||
|
||||
$params['status'] = 'hidden';
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
$params = $this->normalizeFloorFees($params);
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
$serviceId = $this->model->id;
|
||||
|
||||
@@ -204,11 +229,34 @@ class Service extends Backend
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$this->view->assign(['floorFee' => '10.00', 'freeFloor' => 2]);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
private function normalizeFloorFees(array $params)
|
||||
{
|
||||
$mode = $this->request->post('floor_fee_mode', '');
|
||||
if ($mode !== 'free' && $mode !== 'paid') {
|
||||
return $params;
|
||||
}
|
||||
$fee = $mode === 'free' ? 0.0 : filter_var($params['floor_fee'] ?? null, FILTER_VALIDATE_FLOAT);
|
||||
if ($fee === false || $fee < 0 || $fee > 99999999.99 || round($fee, 2) !== $fee) {
|
||||
throw new ValidateException('每层费用须为 0 元或正数,且最多保留两位小数');
|
||||
}
|
||||
if ($fee === 0.0) {
|
||||
return array_replace($params, ['floor_fee' => 0, 'free_floor' => 0]);
|
||||
}
|
||||
$freeFloors = filter_var($params['free_floor'] ?? null, FILTER_VALIDATE_INT, [
|
||||
'options' => ['min_range' => 0, 'max_range' => 2147483647],
|
||||
]);
|
||||
if ($freeFloors === false) {
|
||||
throw new ValidateException('免费楼层数须为非负整数');
|
||||
}
|
||||
return array_replace($params, ['floor_fee' => $fee, 'free_floor' => $freeFloors]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 独立弹窗:配置主服务工项及基准价格与规格专属价格
|
||||
* 商品服务与价格使用同一份配置,整体保存。
|
||||
*/
|
||||
public function pricing_main($ids = null)
|
||||
{
|
||||
@@ -220,244 +268,103 @@ class Service extends Backend
|
||||
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
if (!$this->auth->check('service/pricing_addon')) {
|
||||
$this->error('配置服务价格需要主服务和增值服务两项权限');
|
||||
}
|
||||
|
||||
$dictionary = ProductPricing::dictionary();
|
||||
$specQuery = function () use ($row) {
|
||||
return Db::name('service_spec')->where('service_id', $row->id)->where('status', 'normal');
|
||||
};
|
||||
if ($this->request->isPost()) {
|
||||
$serviceTypesRaw = $this->request->post("service_types_raw");
|
||||
$specPricesInput = $this->request->post("spec_prices/a", []);
|
||||
|
||||
$parsedTypes = [];
|
||||
if ($serviceTypesRaw) {
|
||||
$parsedTypes = is_string($serviceTypesRaw) ? (json_decode($serviceTypesRaw, true) ?: []) : $serviceTypesRaw;
|
||||
}
|
||||
if (empty($parsedTypes)) {
|
||||
$this->error('请至少开启 1 项主服务工项');
|
||||
}
|
||||
|
||||
$dictSupportMap = Db::name('service_type')->column('support_fixed', 'code');
|
||||
$hasDefault = false;
|
||||
foreach ($parsedTypes as &$pt) {
|
||||
$code = isset($pt['code']) ? (string)$pt['code'] : '';
|
||||
$supportFixed = isset($dictSupportMap[$code])
|
||||
? (int)$dictSupportMap[$code]
|
||||
: (isset($pt['support_fixed']) ? (int)$pt['support_fixed'] : 1);
|
||||
$price = ($supportFixed === 1 && isset($pt['price'])) ? round((float)$pt['price'], 2) : 0.00;
|
||||
$pt['price'] = $price;
|
||||
$pt['support_fixed'] = $supportFixed;
|
||||
if ($supportFixed === 1 && !empty($pt['is_default']) && !$hasDefault) {
|
||||
$pt['is_default'] = 1;
|
||||
$hasDefault = true;
|
||||
} else {
|
||||
$pt['is_default'] = 0;
|
||||
}
|
||||
}
|
||||
unset($pt);
|
||||
|
||||
if (!$hasDefault && !empty($parsedTypes)) {
|
||||
foreach ($parsedTypes as &$ptDef) {
|
||||
if ((int)$ptDef['support_fixed'] === 1) {
|
||||
$ptDef['is_default'] = 1;
|
||||
$hasDefault = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
unset($ptDef);
|
||||
if (!$hasDefault) {
|
||||
$parsedTypes[0]['is_default'] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$defItem = $parsedTypes[0];
|
||||
foreach ($parsedTypes as $pt) {
|
||||
if (!empty($pt['is_default'])) {
|
||||
$defItem = $pt;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$supportFixed = isset($defItem['support_fixed']) ? (int)$defItem['support_fixed'] : 1;
|
||||
$mainPrice = isset($defItem['price']) ? (float)$defItem['price'] : 0.00;
|
||||
$priceType = ($supportFixed === 0 || $mainPrice <= 0) ? 'quote' : 'fixed';
|
||||
|
||||
$this->token();
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 更新商品主服务配置与基准价格
|
||||
$row->save([
|
||||
'service_types' => json_encode(array_values($parsedTypes), JSON_UNESCAPED_UNICODE),
|
||||
'price_type' => $priceType,
|
||||
'base_price' => $mainPrice,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
|
||||
// 更新各个细分规格的主服务专属定价字典
|
||||
if (is_array($specPricesInput)) {
|
||||
foreach ($specPricesInput as $specId => $pricesMap) {
|
||||
$specId = (int)$specId;
|
||||
if ($specId <= 0) continue;
|
||||
$cleanPrices = [];
|
||||
if (is_string($pricesMap)) {
|
||||
$pricesMap = json_decode($pricesMap, true) ?: [];
|
||||
}
|
||||
if (is_array($pricesMap)) {
|
||||
foreach ($pricesMap as $c => $v) {
|
||||
$c = trim((string)$c);
|
||||
$v = (float)$v;
|
||||
if ($c !== '' && $v > 0) {
|
||||
$cleanPrices[$c] = round($v, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
$servicePricesJson = !empty($cleanPrices) ? json_encode($cleanPrices, JSON_UNESCAPED_UNICODE) : null;
|
||||
$specBasePrice = isset($cleanPrices[$defItem['code']]) ? $cleanPrices[$defItem['code']] : 0.00;
|
||||
|
||||
Db::name('service_spec')
|
||||
->where('id', $specId)
|
||||
->where('service_id', $row->id)
|
||||
->update([
|
||||
'service_prices' => $servicePricesJson,
|
||||
'base_price' => $specBasePrice,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
$current = Db::name('service')->where('id', $row->id)->lock(true)->find();
|
||||
if (!$current) {
|
||||
throw new \InvalidArgumentException('商品已删除');
|
||||
}
|
||||
$previous = ProductPricing::read($current);
|
||||
$input = json_decode((string)$this->request->post('pricing_payload', '', null), true);
|
||||
if (!is_array($input) || !array_key_exists('spec_configs', $input) || (int)($input['revision'] ?? -1) !== (int)($previous['revision'] ?? 0)) {
|
||||
throw new \InvalidArgumentException('配置已变更或格式无效,请重新打开后再保存');
|
||||
}
|
||||
if (!$previous && !empty(json_decode($current['addons'] ?? '', true)) && empty($input['reviewed'])) {
|
||||
throw new \InvalidArgumentException('请先核对旧增值服务的归属');
|
||||
}
|
||||
$specNames = $specQuery()->column('name', 'id');
|
||||
$config = ProductPricing::validate($input, $dictionary, array_map('intval', array_keys($specNames)), $specNames);
|
||||
$config['revision'] = (int)($previous['revision'] ?? 0) + 1;
|
||||
$config['legacy'] = $previous['legacy'] ?? array_intersect_key($current, array_flip([
|
||||
'service_types', 'addons', 'base_price', 'price_type',
|
||||
]));
|
||||
$options = ProductPricing::options($config, $dictionary, 0);
|
||||
$default = $options['services'][$config['default']];
|
||||
$serviceTypes = ProductPricing::publicServices($options);
|
||||
$addons = [];
|
||||
foreach ($serviceTypes as &$service) {
|
||||
foreach ($service['addons'] as $addon) {
|
||||
$addons[$addon['code']] = ['code' => $addon['code'], 'name' => $addon['name']];
|
||||
}
|
||||
unset($service['addons']);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
$this->success('主服务定价配置保存成功');
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$serviceTypes = [];
|
||||
if (!empty($row->service_types)) {
|
||||
$serviceTypes = is_array($row->service_types) ? $row->service_types : json_decode($row->service_types, true);
|
||||
}
|
||||
$specs = Db::name('service_spec')
|
||||
->where('service_id', $row->id)
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc, id asc')
|
||||
->select();
|
||||
foreach ($specs as &$sp) {
|
||||
$sp['prices_map'] = !empty($sp['service_prices']) ? (json_decode($sp['service_prices'], true) ?: []) : [];
|
||||
}
|
||||
unset($sp);
|
||||
|
||||
$catName = Db::name('service_cat')->where('id', $row->cat_id)->value('name') ?: '未归类';
|
||||
$this->view->assign([
|
||||
'row' => $row,
|
||||
'catName' => $catName,
|
||||
'serviceTypes' => $serviceTypes ?: [],
|
||||
'specs' => $specs ?: [],
|
||||
]);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 独立弹窗:配置增值服务工项及基准加价与规格专属加价
|
||||
*/
|
||||
public function pricing_addon($ids = null)
|
||||
{
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
|
||||
if ($this->request->isPost()) {
|
||||
$addonsRaw = $this->request->post("addons_raw");
|
||||
$specAddonPricesInput = $this->request->post("spec_addon_prices/a", []);
|
||||
|
||||
$parsedAddons = [];
|
||||
if ($addonsRaw) {
|
||||
$parsedAddons = is_string($addonsRaw) ? (json_decode($addonsRaw, true) ?: []) : $addonsRaw;
|
||||
}
|
||||
|
||||
$cleanAddons = [];
|
||||
if (is_array($parsedAddons)) {
|
||||
foreach ($parsedAddons as $idx => $ad) {
|
||||
$adName = isset($ad['name']) ? trim((string)$ad['name']) : '';
|
||||
$adPrice = isset($ad['price']) ? round((float)$ad['price'], 2) : 0.00;
|
||||
if ($adName === '' || $adPrice <= 0) continue;
|
||||
$cleanAddons[] = [
|
||||
'id' => isset($ad['id']) ? (int)$ad['id'] : ($idx + 1),
|
||||
'code' => isset($ad['code']) ? (string)$ad['code'] : ('addon_' . ($idx + 1)),
|
||||
'name' => $adName,
|
||||
'unit' => !empty($ad['unit']) ? (string)$ad['unit'] : '份',
|
||||
'price' => $adPrice,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 更新商品增值服务配置
|
||||
$row->save([
|
||||
'addons' => json_encode($cleanAddons, JSON_UNESCAPED_UNICODE),
|
||||
unset($service);
|
||||
Db::name('service')->where('id', $row->id)->update([
|
||||
'pricing_config' => json_encode($config, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
|
||||
'service_types' => json_encode($serviceTypes, JSON_UNESCAPED_UNICODE),
|
||||
'addons' => json_encode(array_values($addons), JSON_UNESCAPED_UNICODE),
|
||||
'base_price' => ($default['cents'] ?? 0) / 100,
|
||||
'price_type' => $default['mode'],
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
|
||||
// 更新各个细分规格的增值服务专属加价字典
|
||||
if (is_array($specAddonPricesInput)) {
|
||||
foreach ($specAddonPricesInput as $specId => $addonMap) {
|
||||
$specId = (int)$specId;
|
||||
if ($specId <= 0) continue;
|
||||
$cleanAddonPrices = [];
|
||||
if (is_string($addonMap)) {
|
||||
$addonMap = json_decode($addonMap, true) ?: [];
|
||||
}
|
||||
if (is_array($addonMap)) {
|
||||
foreach ($addonMap as $c => $v) {
|
||||
$c = trim((string)$c);
|
||||
$v = (float)$v;
|
||||
if ($c !== '' && $v > 0) {
|
||||
$cleanAddonPrices[$c] = round($v, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
$addonPricesJson = !empty($cleanAddonPrices) ? json_encode($cleanAddonPrices, JSON_UNESCAPED_UNICODE) : null;
|
||||
|
||||
Db::name('service_spec')
|
||||
->where('id', $specId)
|
||||
->where('service_id', $row->id)
|
||||
->update([
|
||||
'addon_prices' => $addonPricesJson,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
$this->success('增值服务加价配置保存成功');
|
||||
} catch (Exception $e) {
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
$this->error($e->getMessage(), '', ['__token__' => $this->request->token()]);
|
||||
} catch (\Throwable $e) {
|
||||
Db::rollback();
|
||||
\think\Log::error('[ProductPricing] ' . $e->getMessage());
|
||||
$this->error('服务价格保存失败,请重试', '', ['__token__' => $this->request->token()]);
|
||||
}
|
||||
$this->success('全部配置已保存');
|
||||
}
|
||||
|
||||
$addons = [];
|
||||
if (!empty($row->addons)) {
|
||||
$addons = is_array($row->addons) ? $row->addons : json_decode($row->addons, true);
|
||||
}
|
||||
$specs = Db::name('service_spec')
|
||||
->where('service_id', $row->id)
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc, id asc')
|
||||
->select();
|
||||
foreach ($specs as &$sp) {
|
||||
$sp['addon_prices_map'] = !empty($sp['addon_prices']) ? (json_decode($sp['addon_prices'], true) ?: []) : [];
|
||||
}
|
||||
unset($sp);
|
||||
|
||||
$catName = Db::name('service_cat')->where('id', $row->cat_id)->value('name') ?: '未归类';
|
||||
$specs = $specQuery()->order('weigh desc,id asc')->select();
|
||||
$data = $row->getData();
|
||||
$config = ProductPricing::read($data);
|
||||
$legacy = $config ? [] : (json_decode($data['addons'] ?? '', true) ?: []);
|
||||
$draft = $config ?? ProductPricing::legacyDraft($data, $dictionary, $specs);
|
||||
$previousSpecPrices = !empty($draft['specs']);
|
||||
$draft['spec_configs'] = array_intersect_key(
|
||||
ProductPricing::expandLegacySpecs($draft),
|
||||
array_flip(array_column($specs, 'id'))
|
||||
);
|
||||
unset($draft['legacy'], $draft['specs']);
|
||||
$this->view->assign([
|
||||
'row' => $row,
|
||||
'catName' => $catName,
|
||||
'assignedAddons' => $addons ?: [],
|
||||
'specs' => $specs ?: [],
|
||||
'row' => $row,
|
||||
'pricingData' => json_encode([
|
||||
'draft' => $draft,
|
||||
'dictionary' => $dictionary,
|
||||
'selectedSpecId' => (int)$this->request->get('spec_id', 0),
|
||||
'legacyAddons' => $legacy,
|
||||
'previousSpecPrices' => $previousSpecPrices && !empty($draft['spec_configs']),
|
||||
'legacySpecAddons' => $config ? [] : array_column(array_map(function ($spec) {
|
||||
return ['id' => $spec['id'], 'prices' => json_decode($spec['addon_prices'] ?? '', true) ?: []];
|
||||
}, $specs), 'prices', 'id'),
|
||||
'specs' => array_map(function ($spec) {
|
||||
return ['id' => (int)$spec['id'], 'name' => $spec['name']];
|
||||
}, $specs),
|
||||
], JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT),
|
||||
]);
|
||||
return $this->view->fetch();
|
||||
return $this->view->fetch('pricing_workspace');
|
||||
}
|
||||
|
||||
public function pricing_addon($ids = null)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->error('请在商品的「服务与价格配置」中统一保存');
|
||||
}
|
||||
$this->redirect('service/pricing_main', ['ids' => $ids, 'dialog' => 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -477,11 +384,17 @@ class Service extends Backend
|
||||
$params = $this->request->post("row/a");
|
||||
if ($params) {
|
||||
$params = $this->preExcludeFields($params);
|
||||
$params = $this->formatServiceConfig($params);
|
||||
$params = array_diff_key($params, array_flip(['pricing_config', 'service_types', 'addons', 'base_price', 'price_type']));
|
||||
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
$params = $this->normalizeFloorFees($params);
|
||||
$current = Db::name('service')->where('id', $row->id)->lock(true)->find();
|
||||
if (($params['status'] ?? '') === 'normal' && empty($current['pricing_config'])
|
||||
&& empty(json_decode($current['service_types'] ?? '', true))) {
|
||||
throw new ValidateException('请先完成服务与价格配置,再上架商品');
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
|
||||
// 同步处理商品规格列表
|
||||
@@ -501,45 +414,18 @@ class Service extends Backend
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
|
||||
// 解析当前商品已关联的主服务与增项服务配置
|
||||
$serviceTypes = [];
|
||||
if (!empty($row->service_types)) {
|
||||
$serviceTypes = is_array($row->service_types) ? $row->service_types : json_decode($row->service_types, true);
|
||||
}
|
||||
$addons = [];
|
||||
if (!empty($row->addons)) {
|
||||
$addons = is_array($row->addons) ? $row->addons : json_decode($row->addons, true);
|
||||
}
|
||||
|
||||
// 获取已有商品规格并解析专属主服务与增值服务定价字典
|
||||
$specs = Db::name('service_spec')
|
||||
->where('service_id', $row->id)
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc, id asc')
|
||||
->select();
|
||||
foreach ($specs as &$sp) {
|
||||
$sp['prices_map'] = !empty($sp['service_prices']) ? (json_decode($sp['service_prices'], true) ?: []) : [];
|
||||
$sp['addon_prices_map'] = !empty($sp['addon_prices']) ? (json_decode($sp['addon_prices'], true) ?: []) : [];
|
||||
if (empty($sp['prices_map']) && (float)$sp['base_price'] > 0) {
|
||||
$defCode = 'install';
|
||||
foreach ($serviceTypes as $st) {
|
||||
if (!empty($st['is_default']) && !empty($st['code'])) {
|
||||
$defCode = (string)$st['code'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
$sp['prices_map'][$defCode] = (float)$sp['base_price'];
|
||||
}
|
||||
}
|
||||
unset($sp);
|
||||
|
||||
$catName = Db::name('service_cat')->where('id', $row->cat_id)->value('name') ?: '未归类';
|
||||
|
||||
$this->view->assign("row", $row);
|
||||
$this->view->assign("catName", $catName);
|
||||
$this->view->assign("serviceTypes", $serviceTypes ?: []);
|
||||
$this->view->assign("assignedAddons", $addons ?: []);
|
||||
$this->view->assign("specs", $specs ?: []);
|
||||
$this->view->assign(['floorFee' => $row->floor_fee, 'freeFloor' => $row->free_floor]);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
@@ -552,150 +438,96 @@ class Service extends Backend
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$catName = Db::name('service_cat')->where('id', $row->cat_id)->value('name') ?: '未归类';
|
||||
$serviceTypes = !empty($row->service_types)
|
||||
? (is_array($row->service_types) ? $row->service_types : (json_decode($row->service_types, true) ?: []))
|
||||
: [];
|
||||
$addons = !empty($row->addons)
|
||||
? (is_array($row->addons) ? $row->addons : (json_decode($row->addons, true) ?: []))
|
||||
: [];
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
$product = $row->getData();
|
||||
$dictionary = ProductPricing::dictionary();
|
||||
$specs = Db::name('service_spec')
|
||||
->where('service_id', $row->id)
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc, id asc')
|
||||
->select();
|
||||
foreach ($specs as &$sp) {
|
||||
$sp['prices_map'] = !empty($sp['service_prices']) ? (json_decode($sp['service_prices'], true) ?: []) : [];
|
||||
$sp['addon_prices_map'] = !empty($sp['addon_prices']) ? (json_decode($sp['addon_prices'], true) ?: []) : [];
|
||||
if (empty($sp['prices_map']) && (float)$sp['base_price'] > 0) {
|
||||
$defCode = 'install';
|
||||
foreach ($serviceTypes as $st) {
|
||||
if (!empty($st['is_default']) && !empty($st['code'])) {
|
||||
$defCode = (string)$st['code'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
$sp['prices_map'][$defCode] = (float)$sp['base_price'];
|
||||
$saved = ProductPricing::read($product);
|
||||
$config = $saved ?? ProductPricing::legacyDraft($product, $dictionary, $specs);
|
||||
if (!$saved && $product['price_type'] === 'quote') {
|
||||
foreach ($config['services'] as &$service) {
|
||||
$service['mode'] = 'quote';
|
||||
}
|
||||
unset($service);
|
||||
}
|
||||
$legacyAddons = $saved ? [] : (json_decode($product['addons'] ?? '', true) ?: []);
|
||||
$parent = [
|
||||
'id' => 0,
|
||||
'name' => '商品通用配置',
|
||||
'services' => ProductPricing::publicServices(ProductPricing::options($config, $dictionary, 0)),
|
||||
'legacyAddons' => $this->detailLegacyAddons($legacyAddons, [], $dictionary),
|
||||
];
|
||||
$scopes = [$parent];
|
||||
foreach ($specs as $spec) {
|
||||
$specId = (int)$spec['id'];
|
||||
$addonPrices = $saved ? [] : (json_decode($spec['addon_prices'] ?? '', true) ?: []);
|
||||
$status = isset($config['spec_configs'][$specId]) ? '独立配置'
|
||||
: (!empty($config['specs'][$specId]) || $addonPrices ? '历史规格定价' : '沿用通用');
|
||||
$scopes[] = [
|
||||
'id' => $specId,
|
||||
'name' => $spec['name'],
|
||||
'status' => $status,
|
||||
'description' => $spec['description'],
|
||||
'image' => $this->detailImage($spec['image'] ?? ''),
|
||||
'services' => $status === '沿用通用' ? $parent['services']
|
||||
: ProductPricing::publicServices(ProductPricing::options($config, $dictionary, $specId)),
|
||||
'legacyAddons' => $this->detailLegacyAddons($legacyAddons, $addonPrices, $dictionary),
|
||||
];
|
||||
}
|
||||
unset($sp);
|
||||
|
||||
$orderAgg = Db::name('order')
|
||||
->field('COUNT(*) AS total_cnt, SUM(CASE WHEN pay_status = 1 THEN pay_amount ELSE 0 END) AS total_gmv')
|
||||
->where('service_id', $row->id)
|
||||
->find();
|
||||
|
||||
$this->view->assign([
|
||||
'row' => $row,
|
||||
'catName' => $catName,
|
||||
'serviceTypes' => $serviceTypes,
|
||||
'addons' => $addons,
|
||||
'specs' => $specs,
|
||||
'orderCount' => (int)($orderAgg['total_cnt'] ?? 0),
|
||||
'orderGmv' => number_format((float)($orderAgg['total_gmv'] ?? 0), 2, '.', ''),
|
||||
'row' => $row,
|
||||
'catName' => Db::name('service_cat')->where('id', $row->cat_id)->value('name'),
|
||||
'cover' => $this->detailImage($product['image'] ?? ''),
|
||||
'description' => $product['description'] ?? $product['desc'] ?? '',
|
||||
'canEditPricing' => $this->auth->check('service/pricing_main') && $this->auth->check('service/pricing_addon'),
|
||||
'detailData' => json_encode([
|
||||
'scopes' => $scopes,
|
||||
'selectedSpecId' => (int)$this->request->get('spec_id', 0),
|
||||
], JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT),
|
||||
]);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化商品关联的主服务与增项服务 JSON,并自动提取默认主服务价格用于底层索引兼容
|
||||
*/
|
||||
protected function formatServiceConfig($params)
|
||||
private function detailLegacyAddons(array $addons, array $specPrices, array $dictionary)
|
||||
{
|
||||
$parsedTypes = [];
|
||||
if (isset($params['service_types_raw'])) {
|
||||
if (is_string($params['service_types_raw'])) {
|
||||
$parsedTypes = json_decode($params['service_types_raw'], true) ?: [];
|
||||
} else {
|
||||
$parsedTypes = is_array($params['service_types_raw']) ? $params['service_types_raw'] : [];
|
||||
$names = array_column($dictionary, null, 'name');
|
||||
$result = [];
|
||||
foreach ($addons as $addon) {
|
||||
$code = $addon['code'] ?? '';
|
||||
$name = $addon['name'] ?? '';
|
||||
$type = $dictionary[$code] ?? $names[$name] ?? null;
|
||||
if (!$type) {
|
||||
continue;
|
||||
}
|
||||
unset($params['service_types_raw']);
|
||||
|
||||
if (empty($parsedTypes)) {
|
||||
throw new ValidateException('请至少开启 1 项主服务');
|
||||
$price = (float)($specPrices[$name] ?? $specPrices[$code] ?? 0);
|
||||
if ($price <= 0) {
|
||||
$price = (float)($addon['price'] ?? 0);
|
||||
}
|
||||
|
||||
$dictSupportMap = Db::name('service_type')->column('support_fixed', 'code');
|
||||
$hasDefault = false;
|
||||
foreach ($parsedTypes as &$pt) {
|
||||
$code = isset($pt['code']) ? (string)$pt['code'] : '';
|
||||
$supportFixed = isset($dictSupportMap[$code])
|
||||
? (int)$dictSupportMap[$code]
|
||||
: (isset($pt['support_fixed']) ? (int)$pt['support_fixed'] : 1);
|
||||
$price = ($supportFixed === 1 && isset($pt['price'])) ? round((float)$pt['price'], 2) : 0.00;
|
||||
$pt['price'] = $price;
|
||||
$pt['support_fixed'] = $supportFixed;
|
||||
if ($supportFixed === 1 && !empty($pt['is_default']) && !$hasDefault) {
|
||||
$pt['is_default'] = 1;
|
||||
$hasDefault = true;
|
||||
} else {
|
||||
$pt['is_default'] = 0;
|
||||
}
|
||||
}
|
||||
unset($pt);
|
||||
|
||||
if (!$hasDefault && !empty($parsedTypes)) {
|
||||
foreach ($parsedTypes as &$ptDef) {
|
||||
if ((int)$ptDef['support_fixed'] === 1) {
|
||||
$ptDef['is_default'] = 1;
|
||||
$hasDefault = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
unset($ptDef);
|
||||
if (!$hasDefault) {
|
||||
$parsedTypes[0]['is_default'] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$params['service_types'] = json_encode(array_values($parsedTypes), JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
if (isset($params['addons_raw'])) {
|
||||
$parsedAddons = is_string($params['addons_raw'])
|
||||
? (json_decode($params['addons_raw'], true) ?: [])
|
||||
: (is_array($params['addons_raw']) ? $params['addons_raw'] : []);
|
||||
unset($params['addons_raw']);
|
||||
$cleanAddons = [];
|
||||
foreach ($parsedAddons as $idx => $ad) {
|
||||
$adName = isset($ad['name']) ? trim((string)$ad['name']) : '';
|
||||
$adPrice = isset($ad['price']) ? round((float)$ad['price'], 2) : 0.00;
|
||||
if ($adName === '' || $adPrice <= 0) {
|
||||
continue;
|
||||
}
|
||||
$cleanAddons[] = [
|
||||
'id' => isset($ad['id']) ? (int)$ad['id'] : ($idx + 1),
|
||||
'code' => isset($ad['code']) ? (string)$ad['code'] : ('addon_' . ($idx + 1)),
|
||||
'name' => $adName,
|
||||
'unit' => !empty($ad['unit']) ? (string)$ad['unit'] : '份',
|
||||
'price' => $adPrice,
|
||||
];
|
||||
}
|
||||
$params['addons'] = json_encode($cleanAddons, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
// 自动从关联的主服务(优先默认首选主服务)同步底层 base_price 与 price_type
|
||||
if (!empty($parsedTypes)) {
|
||||
$defItem = $parsedTypes[0];
|
||||
foreach ($parsedTypes as $pt) {
|
||||
if (!empty($pt['is_default'])) {
|
||||
$defItem = $pt;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$supportFixed = isset($defItem['support_fixed']) ? (int)$defItem['support_fixed'] : 1;
|
||||
$mainPrice = isset($defItem['price']) ? (float)$defItem['price'] : 0.00;
|
||||
// 若该工项本身不支持一口价(需现场勘验)或价格为0,则自动切换为师傅报价模式;否则为标准一口价
|
||||
if ($supportFixed === 0 || $mainPrice <= 0) {
|
||||
$params['price_type'] = 'quote';
|
||||
$params['base_price'] = 0.00;
|
||||
} else {
|
||||
$params['price_type'] = 'fixed';
|
||||
$params['base_price'] = $mainPrice;
|
||||
if ($price > 0) {
|
||||
$result[] = ['name' => $type['name'], 'price' => $price, 'unit' => $addon['unit'] ?? $type['unit']];
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $params;
|
||||
private function detailImage($path)
|
||||
{
|
||||
if (!$path) {
|
||||
return '';
|
||||
}
|
||||
if (strpos($path, '/assets/') === 0 || strpos($path, '/icon/') === 0) {
|
||||
return $path;
|
||||
}
|
||||
$url = cdnurl($path, true);
|
||||
return preg_match('~^(?:https?:)?//~i', $url) ? $url : '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -705,16 +537,7 @@ class Service extends Backend
|
||||
{
|
||||
if (empty($serviceId)) return;
|
||||
$now = time();
|
||||
$svcRow = Db::name('service')->field('cat_id,service_types')->where('id', $serviceId)->find();
|
||||
$catId = (int)($svcRow['cat_id'] ?? 0);
|
||||
$svcTypes = !empty($svcRow['service_types']) ? (json_decode($svcRow['service_types'], true) ?: []) : [];
|
||||
$defaultCode = 'install';
|
||||
foreach ($svcTypes as $st) {
|
||||
if (!empty($st['is_default']) && !empty($st['code'])) {
|
||||
$defaultCode = (string)$st['code'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
$catId = (int)Db::name('service')->where('id', $serviceId)->value('cat_id');
|
||||
|
||||
$keepIds = [];
|
||||
if (!empty($specsInput) && is_array($specsInput)) {
|
||||
@@ -725,60 +548,12 @@ class Service extends Backend
|
||||
$specDesc = isset($item['description']) ? trim($item['description']) : '';
|
||||
$specImage = isset($item['image']) ? trim((string)$item['image']) : '';
|
||||
|
||||
// 解析当前规格的工项专属定价字典 (JSON / 关联数组)
|
||||
$cleanPricesMap = [];
|
||||
if (isset($item['service_prices'])) {
|
||||
$rawPrices = is_array($item['service_prices'])
|
||||
? $item['service_prices']
|
||||
: (json_decode($item['service_prices'], true) ?: []);
|
||||
foreach ($rawPrices as $pCode => $pVal) {
|
||||
$pCode = trim((string)$pCode);
|
||||
$pNum = (float)$pVal;
|
||||
if ($pCode !== '' && $pNum > 0) {
|
||||
$cleanPricesMap[$pCode] = round($pNum, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// base_price 保持向下兼容:优先取默认首选主服务的专属价,未设则取传入的 base_price,均无则存 0.00
|
||||
$specPrice = 0.00;
|
||||
if (isset($cleanPricesMap[$defaultCode])) {
|
||||
$specPrice = $cleanPricesMap[$defaultCode];
|
||||
} elseif (isset($item['base_price']) && is_numeric($item['base_price']) && (float)$item['base_price'] > 0) {
|
||||
$specPrice = round((float)$item['base_price'], 2);
|
||||
if (!isset($cleanPricesMap[$defaultCode])) {
|
||||
$cleanPricesMap[$defaultCode] = $specPrice;
|
||||
}
|
||||
} elseif (!empty($cleanPricesMap)) {
|
||||
$specPrice = reset($cleanPricesMap);
|
||||
}
|
||||
|
||||
$pricesJson = !empty($cleanPricesMap) ? json_encode($cleanPricesMap, JSON_UNESCAPED_UNICODE) : null;
|
||||
$specId = isset($item['id']) ? (int)$item['id'] : 0;
|
||||
$specId = (int)($item['id'] ?? 0);
|
||||
$weigh = max(0, 100 - $idx * 5);
|
||||
$idx++;
|
||||
|
||||
// 解析当前规格的增值服务专属加价字典 (JSON / 关联数组)
|
||||
$cleanAddonPricesMap = [];
|
||||
if (isset($item['addon_prices'])) {
|
||||
$rawAddonPrices = is_array($item['addon_prices'])
|
||||
? $item['addon_prices']
|
||||
: (json_decode($item['addon_prices'], true) ?: []);
|
||||
foreach ($rawAddonPrices as $aCode => $aVal) {
|
||||
$aCode = trim((string)$aCode);
|
||||
$aNum = (float)$aVal;
|
||||
if ($aCode !== '' && $aNum > 0) {
|
||||
$cleanAddonPricesMap[$aCode] = round($aNum, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
$addonPricesJson = !empty($cleanAddonPricesMap) ? json_encode($cleanAddonPricesMap, JSON_UNESCAPED_UNICODE) : null;
|
||||
|
||||
$saveData = [
|
||||
'name' => $name,
|
||||
'base_price' => $specPrice,
|
||||
'service_prices' => $pricesJson,
|
||||
'addon_prices' => $addonPricesJson,
|
||||
'description' => $specDesc,
|
||||
'weigh' => $weigh,
|
||||
'updatetime' => $now,
|
||||
|
||||
在新工单中引用
屏蔽一个用户