model = new \app\admin\model\Service; $this->view->assign("priceTypeList", $this->model->getPriceTypeList()); $this->view->assign("statusList", $this->model->getStatusList()); // 获取全部可用的公共增项服务列表 $addonsPool = Db::name('service_addon') ->where('status', 'normal') ->order('weigh desc, id asc') ->select(); $this->view->assign("addonsPool", $addonsPool); } public function index() { $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->with(['cat']) ->where($where) ->order($sort, $order) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 添加服务商品 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } // 处理工项类型与增项配置 $params = $this->formatServiceConfig($params); $result = false; Db::startTrans(); try { $result = $this->model->allowField(true)->save($params); $serviceId = $this->model->id; // 处理规格列表 $this->syncSpecs($serviceId, $this->request->post('specs/a', [])); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were inserted')); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑服务商品(工项价格、规格价格、增项服务绑定) */ public function edit($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()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); // 处理工项类型与增项配置 $params = $this->formatServiceConfig($params); $result = false; Db::startTrans(); try { $result = $row->allowField(true)->save($params); // 同步处理规格列表 $this->syncSpecs($row->id, $this->request->post('specs/a', [])); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were updated')); } } $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(); $this->view->assign("row", $row); $this->view->assign("serviceTypes", $serviceTypes ?: []); $this->view->assign("assignedAddons", $addons ?: []); $this->view->assign("specs", $specs ?: []); return $this->view->fetch(); } /** * 格式化工项与增项输入数据 */ protected function formatServiceConfig($params) { // 工项类型 JSON 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); 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']); } return $params; } /** * 同步商品规格表 */ protected function syncSpecs($serviceId, $specsInput) { if (empty($serviceId)) return; $now = time(); $keepIds = []; if (!empty($specsInput) && is_array($specsInput)) { 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; if ($specId > 0) { Db::name('service_spec')->where('id', $specId)->where('service_id', $serviceId)->update([ 'name' => $name, 'base_price' => $specPrice, 'description' => $specDesc, 'updatetime' => $now, ]); $keepIds[] = $specId; } else { $newId = Db::name('service_spec')->insertGetId([ 'service_id' => $serviceId, 'name' => $name, 'base_price' => $specPrice, 'description' => $specDesc, 'weigh' => 0, 'status' => 'normal', 'createtime' => $now, 'updatetime' => $now, ]); $keepIds[] = $newId; } } } // 将未在列表中的历史规格软下架或删除 $delQuery = Db::name('service_spec')->where('service_id', $serviceId); if (!empty($keepIds)) { $delQuery->whereNotIn('id', $keepIds); } $delQuery->delete(); } }