From 01611309cd55ca165d7bdac66948e0acb63f1888 Mon Sep 17 00:00:00 2001 From: weiz Date: Tue, 19 Dec 2023 16:27:17 +0800 Subject: [PATCH] SafetyCheck --- .../safety/SafetyCheckController.php | 108 ++++++++++++ .../lists/safety/SafetyCheckLists.php | 89 ++++++++++ .../logic/safety/SafetyCheckLogic.php | 161 ++++++++++++++++++ .../validate/safety/SafetyCheckValidate.php | 139 +++++++++++++++ app/common/model/safety/SafetyCheck.php | 42 +++++ 5 files changed, 539 insertions(+) create mode 100644 app/adminapi/controller/safety/SafetyCheckController.php create mode 100644 app/adminapi/lists/safety/SafetyCheckLists.php create mode 100644 app/adminapi/logic/safety/SafetyCheckLogic.php create mode 100644 app/adminapi/validate/safety/SafetyCheckValidate.php create mode 100644 app/common/model/safety/SafetyCheck.php diff --git a/app/adminapi/controller/safety/SafetyCheckController.php b/app/adminapi/controller/safety/SafetyCheckController.php new file mode 100644 index 000000000..e1a3dc034 --- /dev/null +++ b/app/adminapi/controller/safety/SafetyCheckController.php @@ -0,0 +1,108 @@ +dataLists(new SafetyCheckLists()); + } + + + /** + * @notes 添加安全检查 + * @return \think\response\Json + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function add() + { + $params = (new SafetyCheckValidate())->post()->goCheck('add'); + $result = SafetyCheckLogic::add($params,$this->adminId); + if (true === $result) { + return $this->success('添加成功', [], 1, 1); + } + return $this->fail(SafetyCheckLogic::getError()); + } + + + /** + * @notes 编辑安全检查 + * @return \think\response\Json + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function edit() + { + $params = (new SafetyCheckValidate())->post()->goCheck('edit'); + $result = SafetyCheckLogic::edit($params,$this->adminId); + if (true === $result) { + return $this->success('编辑成功', [], 1, 1); + } + return $this->fail(SafetyCheckLogic::getError()); + } + + + /** + * @notes 删除安全检查 + * @return \think\response\Json + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function delete() + { + $params = (new SafetyCheckValidate())->post()->goCheck('delete'); + SafetyCheckLogic::delete($params); + return $this->success('删除成功', [], 1, 1); + } + + + /** + * @notes 获取安全检查详情 + * @return \think\response\Json + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function detail() + { + $params = (new SafetyCheckValidate())->goCheck('detail'); + $result = SafetyCheckLogic::detail($params); + return $this->data($result); + } + + +} \ No newline at end of file diff --git a/app/adminapi/lists/safety/SafetyCheckLists.php b/app/adminapi/lists/safety/SafetyCheckLists.php new file mode 100644 index 000000000..6e14768f4 --- /dev/null +++ b/app/adminapi/lists/safety/SafetyCheckLists.php @@ -0,0 +1,89 @@ + ['check_date'], + '%like%' => ['check_name', 'check_user', 'check_company', 'check_area'], + ]; + } + + + /** + * @notes 获取安全检查列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function lists(): array + { + return SafetyCheck::where($this->searchWhere) + ->field(['id', 'org_id', 'dept_id', 'project_id', 'check_name', 'check_date', 'check_user', 'check_company', 'check_area', 'check_item', 'check_result', 'modify_company', 'resp_user', 'risk_level', 'safety_hazard', 'modify_term', 'remark', 'file', 'check_nature', 'quality_hazard', 'sub_table']) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select()->each(function($item){ + $project = Project::field('name,project_code')->where('id',$item['project_id'])->findOrEmpty(); + $org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$item['dept_id'])->findOrEmpty(); + $item['org_name'] = $org['name']; + $item['dept_name'] = $dept['name']; + $item['project_name'] = $project['name']; + $item['project_code'] = $project['project_code']; + return $item; + }) + ->toArray(); + } + + /** + * @notes 获取安全检查数量 + * @return int + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function count(): int + { + return SafetyCheck::where($this->searchWhere)->count(); + } + +} \ No newline at end of file diff --git a/app/adminapi/logic/safety/SafetyCheckLogic.php b/app/adminapi/logic/safety/SafetyCheckLogic.php new file mode 100644 index 000000000..3dd531e46 --- /dev/null +++ b/app/adminapi/logic/safety/SafetyCheckLogic.php @@ -0,0 +1,161 @@ + $params['org_id'], + 'dept_id' => $params['dept_id'], + 'project_id' => $params['project_id'], + 'check_name' => $params['check_name'], + 'check_date' => strtotime($params['check_date']), + 'check_user' => $params['check_user'], + 'check_company' => $params['check_company'], + 'check_area' => $params['check_area'], + 'check_item' => $params['check_item'], + 'check_result' => $params['check_result'], + 'modify_company' => $params['modify_company'], + 'resp_user' => $params['resp_user'], + 'risk_level' => $params['risk_level'], + 'safety_hazard' => $params['safety_hazard'], + 'modify_term' => $params['modify_term'], + 'remark' => $params['remark'], + 'file' => !empty($params['file']) ? $params['file'] : null, + 'check_nature' => $params['check_nature'], + 'quality_hazard' => $params['quality_hazard'], + 'sub_table' => $params['sub_table'], + 'add_user' => $admin_id, + 'update_user' => $admin_id, + ]); + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 编辑安全检查 + * @param array $params + * @return bool + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public static function edit(array $params,$admin_id): bool + { + Db::startTrans(); + try { + SafetyCheck::where('id', $params['id'])->update([ + 'org_id' => $params['org_id'], + 'dept_id' => $params['dept_id'], + 'project_id' => $params['project_id'], + 'check_name' => $params['check_name'], + 'check_date' => strtotime($params['check_date']), + 'check_user' => $params['check_user'], + 'check_company' => $params['check_company'], + 'check_area' => $params['check_area'], + 'check_item' => $params['check_item'], + 'check_result' => $params['check_result'], + 'modify_company' => $params['modify_company'], + 'resp_user' => $params['resp_user'], + 'risk_level' => $params['risk_level'], + 'safety_hazard' => $params['safety_hazard'], + 'modify_term' => $params['modify_term'], + 'remark' => $params['remark'], + 'file' => !empty($params['file']) ? $params['file'] : null, + 'check_nature' => $params['check_nature'], + 'quality_hazard' => $params['quality_hazard'], + 'sub_table' => $params['sub_table'], + 'update_user' => $admin_id, + 'update_time' => time(), + ]); + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 删除安全检查 + * @param array $params + * @return bool + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public static function delete(array $params): bool + { + return SafetyCheck::destroy($params['id']); + } + + + /** + * @notes 获取安全检查详情 + * @param $params + * @return array + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public static function detail($params): array + { + $data = SafetyCheck::field(['id', 'org_id', 'dept_id', 'project_id', 'check_name', 'check_date', 'check_user', 'check_company', 'check_area', 'check_item', 'check_result', 'modify_company', 'resp_user', 'risk_level', 'safety_hazard', 'modify_term', 'remark', 'file', 'check_nature', 'quality_hazard', 'sub_table', 'add_user', 'update_user', 'create_time', 'update_time'])->findOrEmpty($params['id'])->toArray(); + $project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty(); + $org = Orgs::field('name')->where('id',$data['org_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$data['dept_id'])->findOrEmpty(); + $admin = Admin::where('id','in',[$data['add_user'],$data['update_user']])->column('name','id'); + $data['org_name'] = $org['name']; + $data['dept_name'] = $dept['name']; + $data['project_name'] = $project['name']; + $data['project_code'] = $project['project_code']; + $data['add_user_name'] = $admin[$data['add_user']]; + $data['update_user_name'] = $admin[$data['update_user']]; + return $data; + } +} \ No newline at end of file diff --git a/app/adminapi/validate/safety/SafetyCheckValidate.php b/app/adminapi/validate/safety/SafetyCheckValidate.php new file mode 100644 index 000000000..93bc1ddbc --- /dev/null +++ b/app/adminapi/validate/safety/SafetyCheckValidate.php @@ -0,0 +1,139 @@ + 'require', + 'org_id' => 'require|checkOrg', + 'dept_id' => 'require|checkDept', + 'project_id' => 'require|checkProject', + 'check_date' => 'date', + 'file' => 'checkFile' + ]; + + protected $message = [ + 'id.require' => '缺少必要参数', + 'org_id.require' => '请选择组织', + 'dept_id.require' => '请选择部门', + 'project_id.require' => '请选择项目', + 'check_date.date' => '检查日期格式错误', + ]; + + /** + * @notes 添加场景 + * @return SafetyCheckValidate + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function sceneAdd() + { + return $this->remove('id',true); + } + + + /** + * @notes 编辑场景 + * @return SafetyCheckValidate + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function sceneEdit() + {} + + + /** + * @notes 删除场景 + * @return SafetyCheckValidate + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function sceneDelete() + { + return $this->only(['id']); + } + + + /** + * @notes 详情场景 + * @return SafetyCheckValidate + * @author likeadmin + * @date 2023/12/19 16:10 + */ + public function sceneDetail() + { + return $this->only(['id']); + } + + public function checkOrg($value): bool|string + { + $data = Orgs::where('id',$value)->findOrEmpty(); + if($data->isEmpty()){ + return '组织不存在'; + } + return true; + } + + public function checkDept($value,$rule,$data): bool|string + { + $dept = Dept::where('id',$value)->findOrEmpty(); + if($dept->isEmpty()){ + return '部门不存在'; + } + if($dept['org_id'] != $data['org_id']){ + return '部门不属于当前选择的组织'; + } + return true; + } + + public function checkProject($value): bool|string + { + $data = Project::where('id',$value)->findOrEmpty(); + if($data->isEmpty()){ + return '项目不存在'; + } + return true; + } + + public function checkFile($value): bool|string + { + if($value != ''){ + $file = json_decode($value,true); + if(empty($file)){ + return '附件必须是json数组'; + } + } + return true; + } + +} \ No newline at end of file diff --git a/app/common/model/safety/SafetyCheck.php b/app/common/model/safety/SafetyCheck.php new file mode 100644 index 000000000..d0acab1cb --- /dev/null +++ b/app/common/model/safety/SafetyCheck.php @@ -0,0 +1,42 @@ +