feat: 重构后台服务字典与商品独立定价体系及前后端交互优化
这个提交包含在:
@@ -9,9 +9,9 @@ use think\exception\ValidateException;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* 服务项目与商品管理
|
||||
* 商品管理(商品仅定义施工标的物与细分规格,服务价格统一配置在关联的主服务与增项服务上)
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
* @icon fa fa-cubes
|
||||
*/
|
||||
class Service extends Backend
|
||||
{
|
||||
@@ -29,21 +29,23 @@ class Service extends Backend
|
||||
$this->view->assign("priceTypeList", $this->model->getPriceTypeList());
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
|
||||
// 获取全部可用的公共增项服务列表并关联所属品类
|
||||
$addonsPool = Db::name('service_addon')
|
||||
->alias('a')
|
||||
->leftJoin('service_cat c', 'a.cat_id = c.id')
|
||||
->field('a.*, COALESCE(c.name, "全平台通用") as cat_name')
|
||||
->where('a.status', 'normal')
|
||||
->order('a.cat_id asc, a.weigh desc, a.id asc')
|
||||
// 获取统一服务字典表(fa_service_type,不绑定单一类目,主服务与增项服务同源)
|
||||
$typesPool = Db::name('service_type')
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc, id asc')
|
||||
->select();
|
||||
$this->view->assign("addonsPool", $addonsPool);
|
||||
$this->view->assign("typesPool", $typesPool);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if (false === $this->request->isAjax()) {
|
||||
$cats = Db::name('service_cat')
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc, id asc')
|
||||
->select();
|
||||
$this->view->assign('catList', $cats);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
if ($this->request->request('keyField')) {
|
||||
@@ -55,12 +57,53 @@ class Service extends Backend
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
$result = ['total' => $list->total(), 'rows' => $list->items()];
|
||||
|
||||
$rows = $list->items();
|
||||
if (!empty($rows)) {
|
||||
$serviceIds = [];
|
||||
foreach ($rows as $r) {
|
||||
$serviceIds[] = (int)$r['id'];
|
||||
}
|
||||
// 一次性批量查询当前页商品的细分规格,避免 N+1 查询
|
||||
$allSpecs = Db::name('service_spec')
|
||||
->field('id,service_id,name,description')
|
||||
->where('service_id', 'in', $serviceIds)
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc, id asc')
|
||||
->select();
|
||||
$specMap = [];
|
||||
foreach ($allSpecs as $sp) {
|
||||
$sid = (int)$sp['service_id'];
|
||||
if (!isset($specMap[$sid])) {
|
||||
$specMap[$sid] = [];
|
||||
}
|
||||
$specMap[$sid][] = $sp['name'];
|
||||
}
|
||||
foreach ($rows as &$r) {
|
||||
$sid = (int)$r['id'];
|
||||
$r['specs_list'] = isset($specMap[$sid]) ? $specMap[$sid] : [];
|
||||
|
||||
// 直接在服务端解析为数组返回,防止前端 bootstrap-table escape:true 将 JSON 双引号转义为 " 导致解析失败
|
||||
$stArr = [];
|
||||
if (!empty($r['service_types'])) {
|
||||
$stArr = is_array($r['service_types']) ? $r['service_types'] : (json_decode($r['service_types'], true) ?: []);
|
||||
}
|
||||
$adArr = [];
|
||||
if (!empty($r['addons'])) {
|
||||
$adArr = is_array($r['addons']) ? $r['addons'] : (json_decode($r['addons'], true) ?: []);
|
||||
}
|
||||
$r['service_types_list'] = $stArr;
|
||||
$r['addons_list'] = $adArr;
|
||||
}
|
||||
unset($r);
|
||||
}
|
||||
|
||||
$result = ['total' => $list->total(), 'rows' => $rows];
|
||||
return json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加服务商品
|
||||
* 添加商品并配置关联服务定价
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
@@ -73,7 +116,6 @@ class Service extends Backend
|
||||
$params[$this->dataLimitField] = $this->auth->id;
|
||||
}
|
||||
|
||||
// 处理工项类型与增项配置
|
||||
$params = $this->formatServiceConfig($params);
|
||||
|
||||
$result = false;
|
||||
@@ -82,8 +124,8 @@ class Service extends Backend
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
$serviceId = $this->model->id;
|
||||
|
||||
// 处理规格列表
|
||||
$this->syncSpecs($serviceId, $this->request->post('specs/a', []));
|
||||
// 处理商品规格列表
|
||||
$this->syncSpecs($serviceId, $this->request->post('specs/a', []), isset($params['base_price']) ? (float)$params['base_price'] : 0);
|
||||
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
@@ -102,7 +144,7 @@ class Service extends Backend
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑服务商品(工项价格、规格价格、增项服务绑定)
|
||||
* 编辑商品与关联服务定价
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
@@ -118,8 +160,6 @@ class Service extends Backend
|
||||
$params = $this->request->post("row/a");
|
||||
if ($params) {
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
// 处理工项类型与增项配置
|
||||
$params = $this->formatServiceConfig($params);
|
||||
|
||||
$result = false;
|
||||
@@ -127,8 +167,8 @@ class Service extends Backend
|
||||
try {
|
||||
$result = $row->allowField(true)->save($params);
|
||||
|
||||
// 同步处理规格列表
|
||||
$this->syncSpecs($row->id, $this->request->post('specs/a', []));
|
||||
// 同步处理商品规格列表
|
||||
$this->syncSpecs($row->id, $this->request->post('specs/a', []), isset($params['base_price']) ? (float)$params['base_price'] : (float)$row->base_price);
|
||||
|
||||
Db::commit();
|
||||
} catch (ValidateException|PDOException|Exception $e) {
|
||||
@@ -144,7 +184,7 @@ 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);
|
||||
@@ -154,14 +194,17 @@ class Service extends Backend
|
||||
$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();
|
||||
|
||||
$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 ?: []);
|
||||
@@ -169,44 +212,68 @@ class Service extends Backend
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化工项与增项输入数据
|
||||
* 格式化商品关联的主服务与增项服务 JSON,并自动提取默认主服务价格用于底层索引兼容
|
||||
*/
|
||||
protected function formatServiceConfig($params)
|
||||
{
|
||||
// 工项类型 JSON
|
||||
$parsedTypes = [];
|
||||
if (isset($params['service_types_raw'])) {
|
||||
$params['service_types'] = is_string($params['service_types_raw']) ? $params['service_types_raw'] : json_encode($params['service_types_raw'], JSON_UNESCAPED_UNICODE);
|
||||
if (is_string($params['service_types_raw'])) {
|
||||
$parsedTypes = json_decode($params['service_types_raw'], true) ?: [];
|
||||
$params['service_types'] = $params['service_types_raw'];
|
||||
} else {
|
||||
$parsedTypes = is_array($params['service_types_raw']) ? $params['service_types_raw'] : [];
|
||||
$params['service_types'] = json_encode($parsedTypes, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
unset($params['service_types_raw']);
|
||||
}
|
||||
// 增项服务 JSON
|
||||
|
||||
if (isset($params['addons_raw'])) {
|
||||
$params['addons'] = is_string($params['addons_raw']) ? $params['addons_raw'] : json_encode($params['addons_raw'], JSON_UNESCAPED_UNICODE);
|
||||
unset($params['addons_raw']);
|
||||
}
|
||||
|
||||
// 自动从关联的主服务(优先默认主服务或“安装”)同步底层 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;
|
||||
$params['price_type'] = $supportFixed === 1 ? 'fixed' : 'quote';
|
||||
$params['base_price'] = $supportFixed === 1 ? (float)$defItem['price'] : 0.00;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步商品规格表
|
||||
* 同步商品规格型号表(规格不设独立商品价格,仅标识具体施工标的物型号)
|
||||
*/
|
||||
protected function syncSpecs($serviceId, $specsInput)
|
||||
protected function syncSpecs($serviceId, $specsInput, $defaultBasePrice = 0.00)
|
||||
{
|
||||
if (empty($serviceId)) return;
|
||||
$now = time();
|
||||
$keepIds = [];
|
||||
if (!empty($specsInput) && is_array($specsInput)) {
|
||||
$idx = 0;
|
||||
foreach ($specsInput as $item) {
|
||||
$name = trim(isset($item['name']) ? $item['name'] : '');
|
||||
if ($name === '') continue;
|
||||
$specPrice = isset($item['base_price']) ? (float)$item['base_price'] : 0.00;
|
||||
$specDesc = isset($item['description']) ? trim($item['description']) : '';
|
||||
$specId = isset($item['id']) ? (int)$item['id'] : 0;
|
||||
$weigh = max(0, 100 - $idx * 5);
|
||||
$idx++;
|
||||
|
||||
if ($specId > 0) {
|
||||
Db::name('service_spec')->where('id', $specId)->where('service_id', $serviceId)->update([
|
||||
'name' => $name,
|
||||
'base_price' => $specPrice,
|
||||
'base_price' => $defaultBasePrice,
|
||||
'description' => $specDesc,
|
||||
'weigh' => $weigh,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
$keepIds[] = $specId;
|
||||
@@ -214,9 +281,9 @@ class Service extends Backend
|
||||
$newId = Db::name('service_spec')->insertGetId([
|
||||
'service_id' => $serviceId,
|
||||
'name' => $name,
|
||||
'base_price' => $specPrice,
|
||||
'base_price' => $defaultBasePrice,
|
||||
'description' => $specDesc,
|
||||
'weigh' => 0,
|
||||
'weigh' => $weigh,
|
||||
'status' => 'normal',
|
||||
'createtime' => $now,
|
||||
'updatetime' => $now,
|
||||
@@ -225,7 +292,6 @@ class Service extends Backend
|
||||
}
|
||||
}
|
||||
}
|
||||
// 将未在列表中的历史规格软下架或删除
|
||||
$delQuery = Db::name('service_spec')->where('service_id', $serviceId);
|
||||
if (!empty($keepIds)) {
|
||||
$delQuery->whereNotIn('id', $keepIds);
|
||||
|
||||
在新工单中引用
屏蔽一个用户