From de05423733743c8d7eb92a29b90fab12cd611402 Mon Sep 17 00:00:00 2001 From: zhuyu Date: Wed, 14 May 2025 11:15:03 +0800 Subject: [PATCH] feature: kpi item --- application/admin/controller/kpi/Item.php | 103 ++++++++++++++++++++++ application/admin/view/kpi/item/add.html | 12 --- application/admin/view/kpi/item/edit.html | 12 --- 3 files changed, 103 insertions(+), 24 deletions(-) diff --git a/application/admin/controller/kpi/Item.php b/application/admin/controller/kpi/Item.php index 4cddfc2..72bce84 100644 --- a/application/admin/controller/kpi/Item.php +++ b/application/admin/controller/kpi/Item.php @@ -3,6 +3,11 @@ namespace app\admin\controller\kpi; use app\common\controller\Backend; +use Exception; +use think\Db; +use think\exception\DbException; +use think\exception\PDOException; +use think\exception\ValidateException; /** * kpi指标 @@ -43,4 +48,102 @@ class Item extends Backend */ + /** + * 添加 + * + * @return string + * @throws \think\Exception + */ + public function add() + { + if (false === $this->request->isPost()) { + 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); + + if ($this->dataLimit && $this->dataLimitFieldAutoFill) { + $params[$this->dataLimitField] = $this->auth->id; + } + $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 . '.add' : $name) : $this->modelValidate; + $this->model->validateFailException()->validate($validate); + } + + $params['create_time'] = date('Y-m-d H:i:s'); + $params['update_time'] = date('Y-m-d H:i:s'); + + $result = $this->model->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if ($result === false) { + $this->error(__('No rows were inserted')); + } + $this->success(); + } + + /** + * 编辑 + * + * @param $ids + * @return string + * @throws DbException + * @throws \think\Exception + */ + 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()) { + $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); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if (false === $result) { + $this->error(__('No rows were updated')); + } + $this->success(); + } + + } diff --git a/application/admin/view/kpi/item/add.html b/application/admin/view/kpi/item/add.html index f8f7c1f..e6e575d 100644 --- a/application/admin/view/kpi/item/add.html +++ b/application/admin/view/kpi/item/add.html @@ -42,18 +42,6 @@ -
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-