diff --git a/application/admin/controller/kpi/Template.php b/application/admin/controller/kpi/Template.php index 8aeaa40..5dd3c29 100644 --- a/application/admin/controller/kpi/Template.php +++ b/application/admin/controller/kpi/Template.php @@ -73,8 +73,9 @@ class Template extends Backend $result = $this->model->allowField(true)->save($params); - - $templateId = $result; + if($result!==false) { + $templateId = $this->model->id; //新插入数据的ID + } $templateItem = []; $kpiItems = json_decode($params['kpiitem'], true); @@ -89,7 +90,7 @@ class Template extends Backend } Db::name('kpi_template_item') - ->insert($templateItem); + ->insertAll($templateItem); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { @@ -102,4 +103,121 @@ class Template extends Backend $this->success(); } + + 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 (false === $this->request->isPost()) { + + $kpiitem = Db::name('kpi_template_item') + ->join('kpi_item', 'item_id = kpi_item.id') + ->where('template_id', $row->id) + ->select(); + + $assignKpiitem = array_map(function ($item) { + return [ + 'id' => (string)$item['id'], + 'rate' => (string)$item['rate'], + ]; + }, $kpiitem); + $assignKpiitem = json_encode($assignKpiitem); + + $this->view->assign('assignkpiitem', $assignKpiitem); + $this->view->assign('row', $row); + return $this->view->fetch(); + } + $params = $this->request->post('row/a'); + if (empty($params)) { + $this->error(__('Parameter %s can not be empty', '')); + } + $params = $this->preExcludeFields($params); + $result = false; + Db::startTrans(); + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; + $row->validateFailException()->validate($validate); + } + + $params['update_time'] = date('Y-m-d H:i:s'); + $result = $row->allowField(true)->save($params); + + $templateId = $row->id; + + $templateItem = []; + $kpiItems = json_decode($params['kpiitem'], true); + + foreach ($kpiItems as $kpiItem) { + $templateItem[] = [ + 'admin_id' => $this->auth->id, + 'template_id' => $templateId, + 'item_id' => $kpiItem['id'], + 'rate' => $kpiItem['rate'], + ]; + } + + Db::name('kpi_template_item') + ->where('template_id', $templateId) + ->delete(); + + Db::name('kpi_template_item') + ->insertAll($templateItem); + + Db::commit(); + } catch (ValidateException| \think\exception\PDOException |Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if (false === $result) { + $this->error(__('No rows were updated')); + } + $this->success(); + } + + + public function del($ids = null) + { + if (false === $this->request->isPost()) { + $this->error(__("Invalid parameters")); + } + $ids = $ids ?: $this->request->post("ids"); + if (empty($ids)) { + $this->error(__('Parameter %s can not be empty', 'ids')); + } + $pk = $this->model->getPk(); + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + $list = $this->model->where($pk, 'in', $ids)->select(); + + $count = 0; + Db::startTrans(); + try { + foreach ($list as $item) { + Db::name('kpi_template_item') + ->where('template_id', $item->id) + ->delete(); + $count += $item->delete(); + } + Db::commit(); + } catch (\think\exception\PDOException |Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if ($count) { + $this->success(); + } + $this->error(__('No rows were deleted')); + } + } diff --git a/application/admin/view/kpi/template/add.html b/application/admin/view/kpi/template/add.html index 96afbbc..fe4c5bf 100644 --- a/application/admin/view/kpi/template/add.html +++ b/application/admin/view/kpi/template/add.html @@ -12,21 +12,9 @@ -
| {:__('指标')} | {:__('比例')} | @@ -38,7 +26,7 @@