diff --git a/app/adminapi/controller/safety/SafetyStandardController.php b/app/adminapi/controller/safety/SafetyStandardController.php new file mode 100644 index 000000000..a92795938 --- /dev/null +++ b/app/adminapi/controller/safety/SafetyStandardController.php @@ -0,0 +1,108 @@ +dataLists(new SafetyStandardLists()); + } + + + /** + * @notes 添加安全规范 + * @return \think\response\Json + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public function add() + { + $params = (new SafetyStandardValidate())->post()->goCheck('add'); + $result = SafetyStandardLogic::add($params,$this->adminId); + if (true === $result) { + return $this->success('添加成功', [], 1, 1); + } + return $this->fail(SafetyStandardLogic::getError()); + } + + + /** + * @notes 编辑安全规范 + * @return \think\response\Json + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public function edit() + { + $params = (new SafetyStandardValidate())->post()->goCheck('edit'); + $result = SafetyStandardLogic::edit($params,$this->adminId); + if (true === $result) { + return $this->success('编辑成功', [], 1, 1); + } + return $this->fail(SafetyStandardLogic::getError()); + } + + + /** + * @notes 删除安全规范 + * @return \think\response\Json + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public function delete() + { + $params = (new SafetyStandardValidate())->post()->goCheck('delete'); + SafetyStandardLogic::delete($params); + return $this->success('删除成功', [], 1, 1); + } + + + /** + * @notes 获取安全规范详情 + * @return \think\response\Json + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public function detail() + { + $params = (new SafetyStandardValidate())->goCheck('detail'); + $result = SafetyStandardLogic::detail($params); + return $this->data($result); + } + + +} \ No newline at end of file diff --git a/app/adminapi/lists/safety/SafetyStandardLists.php b/app/adminapi/lists/safety/SafetyStandardLists.php new file mode 100644 index 000000000..294ea5fed --- /dev/null +++ b/app/adminapi/lists/safety/SafetyStandardLists.php @@ -0,0 +1,84 @@ + ['type', 'name', 'publish_dep'], + ]; + } + + + /** + * @notes 获取安全规范列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public function lists(): array + { + return SafetyStandard::where($this->searchWhere) + ->field(['id', 'org_id', 'dept_id', 'type', 'name', 'publish_dep', 'content', 'file']) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select()->each(function($item){ + $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']; + return $item; + }) + ->toArray(); + } + + /** + * @notes 获取安全规范数量 + * @return int + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public function count(): int + { + return SafetyStandard::where($this->searchWhere)->count(); + } + +} \ No newline at end of file diff --git a/app/adminapi/logic/safety/SafetyStandardLogic.php b/app/adminapi/logic/safety/SafetyStandardLogic.php new file mode 100644 index 000000000..531b502cd --- /dev/null +++ b/app/adminapi/logic/safety/SafetyStandardLogic.php @@ -0,0 +1,131 @@ + $params['org_id'], + 'dept_id' => $params['dept_id'], + 'type' => $params['type'], + 'name' => $params['name'], + 'publish_dep' => $params['publish_dep'], + 'content' => $params['content'], + 'file' => !empty($params['file']) ? $params['file'] : null, + '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 10:21 + */ + public static function edit(array $params,$admin_id): bool + { + Db::startTrans(); + try { + SafetyStandard::where('id', $params['id'])->update([ + 'org_id' => $params['org_id'], + 'dept_id' => $params['dept_id'], + 'type' => $params['type'], + 'name' => $params['name'], + 'publish_dep' => $params['publish_dep'], + 'content' => $params['content'], + 'file' => !empty($params['file']) ? $params['file'] : null, + '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 10:21 + */ + public static function delete(array $params): bool + { + return SafetyStandard::destroy($params['id']); + } + + + /** + * @notes 获取安全规范详情 + * @param $params + * @return array + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public static function detail($params): array + { + $data = SafetyStandard::field('id,org_id,dept_id,type,name,publish_dep,content,file,add_user,update_user,create_time,update_time')->findOrEmpty($params['id'])->toArray(); + $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['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/SafetyStandardValidate.php b/app/adminapi/validate/safety/SafetyStandardValidate.php new file mode 100644 index 000000000..7090a3958 --- /dev/null +++ b/app/adminapi/validate/safety/SafetyStandardValidate.php @@ -0,0 +1,146 @@ + 'require', + 'org_id' => 'require|checkOrg', + 'dept_id' => 'require|checkDept', + 'name' => 'require', + 'file' => 'checkFile' + ]; + + protected $message = [ + 'id.require' => '缺少必要参数', + 'org_id.require' => '请选择组织', + 'dept_id.require' => '请选择部门', + 'name.require' => '请填写规范名称', + ]; + + + /** + * 参数描述 + * @var string[] + */ + protected $field = [ + 'id' => 'id', + 'org_id' => '组织id', + 'dept_id' => '部门id', + 'type' => '规范类别', + 'name' => '规范名称', + 'publish_dep' => '发布部门', + 'content' => '规范内容', + 'file' => '附件', + ]; + + + /** + * @notes 添加场景 + * @return SafetyStandardValidate + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public function sceneAdd() + { + return $this->only(['org_id','dept_id','type','name','publish_dep','content','file']); + } + + + /** + * @notes 编辑场景 + * @return SafetyStandardValidate + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public function sceneEdit() + { + return $this->only(['id','org_id','dept_id','type','name','publish_dep','content','file']); + } + + + /** + * @notes 删除场景 + * @return SafetyStandardValidate + * @author likeadmin + * @date 2023/12/19 10:21 + */ + public function sceneDelete() + { + return $this->only(['id']); + } + + + /** + * @notes 详情场景 + * @return SafetyStandardValidate + * @author likeadmin + * @date 2023/12/19 10:21 + */ + 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 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/SafetyStandard.php b/app/common/model/safety/SafetyStandard.php new file mode 100644 index 000000000..49959ac40 --- /dev/null +++ b/app/common/model/safety/SafetyStandard.php @@ -0,0 +1,37 @@ +