From 9eebcd008fabba19c18228b0eac2c8d0885f1a25 Mon Sep 17 00:00:00 2001 From: "LAPTOP-7SGDREK0\\shiweijun" <411582373@qq.com> Date: Tue, 21 Jan 2025 17:16:37 +0800 Subject: [PATCH] feat:material material_category --- app/Constants/Admin/MaterialCode.php | 29 +++++ .../Admin/MaterialCategoryController.php | 81 ++++++++++++ app/Controller/Admin/MaterialController.php | 69 +++++++++++ app/Model/Material.php | 54 ++++++++ app/Model/MaterialCategory.php | 59 +++++++++ app/Request/Admin/MaterialCategoryRequest.php | 41 +++++++ app/Request/Admin/MaterialRequest.php | 44 +++++++ .../Material/MaterialCategoryService.php | 116 ++++++++++++++++++ .../Admin/Material/MaterialService.php | 102 +++++++++++++++ sync/http/admin/auth.http | 51 +++++++- 10 files changed, 645 insertions(+), 1 deletion(-) create mode 100644 app/Constants/Admin/MaterialCode.php create mode 100644 app/Controller/Admin/MaterialCategoryController.php create mode 100644 app/Controller/Admin/MaterialController.php create mode 100644 app/Model/Material.php create mode 100644 app/Model/MaterialCategory.php create mode 100644 app/Request/Admin/MaterialCategoryRequest.php create mode 100644 app/Request/Admin/MaterialRequest.php create mode 100644 app/Service/Admin/Material/MaterialCategoryService.php create mode 100644 app/Service/Admin/Material/MaterialService.php diff --git a/app/Constants/Admin/MaterialCode.php b/app/Constants/Admin/MaterialCode.php new file mode 100644 index 0000000..6e590be --- /dev/null +++ b/app/Constants/Admin/MaterialCode.php @@ -0,0 +1,29 @@ +add(); + } + + /** + * 修改材料分类信息 + * @param MaterialCategoryRequest $request + * @return array + */ + #[RequestMapping(path: "edit", methods: "POST")] + #[Scene(scene: "edit")] + public function edit(MaterialCategoryRequest $request): array + { + return (new MaterialCategoryService())->edit(); + } + + /** + * 删除材料分类信息 + * @param MaterialCategoryRequest $request + * @return array + */ + #[RequestMapping(path: "delete", methods: "GET")] + #[Scene(scene: "delete")] + public function delete(MaterialCategoryRequest $request): array + { + return (new MaterialCategoryService())->delete(); + } + + /** + * 根据id查询材料种类 + * @param MaterialCategoryRequest $request + * @return array + */ + #[RequestMapping(path: "findById", methods: "GET")] + #[Scene(scene: "material_category_info")] + public function findById(MaterialCategoryRequest $request) + { + return (new MaterialCategoryService)->findById(); + } + + /** + * 材料钟类列表 + * @param MaterialCategoryRequest $request + * @return array + */ + #[RequestMapping(path: "list", methods: "GET")] + #[Scene(scene: "list")] + public function getList(MaterialCategoryRequest $request) + { + return (new MaterialCategoryService)->list(); + } +} diff --git a/app/Controller/Admin/MaterialController.php b/app/Controller/Admin/MaterialController.php new file mode 100644 index 0000000..1296fa5 --- /dev/null +++ b/app/Controller/Admin/MaterialController.php @@ -0,0 +1,69 @@ +materialList(); + } + + /** + * 添加材料信息 + * @param MaterialRequest $request + * @return array + */ + #[RequestMapping(path: "add", methods: "POST")] + #[Scene(scene: "add")] + public function add(MaterialRequest $request): array + { + return (new MaterialService)->add(); + } + + /** + * 删除材料信息 + * @param MaterialRequest $request + * @return array + */ + #[RequestMapping(path: "delete", methods: "GET")] + #[Scene(scene: "delete")] + public function delete(MaterialRequest $request): array + { + return (new MaterialService())->delete(); + } + + /** + * 修改材料信息 + * @param MaterialRequest $request + * @return array + */ + #[RequestMapping(path: "edit", methods: "POST")] + #[Scene(scene: "edit")] + public function edit(MaterialRequest $request): array + { + return (new MaterialService())->edit(); + } + +} diff --git a/app/Model/Material.php b/app/Model/Material.php new file mode 100644 index 0000000..4de7a8c --- /dev/null +++ b/app/Model/Material.php @@ -0,0 +1,54 @@ + 'integer', 'category_id' => 'integer', 'status' => 'integer', 'city_id' => 'integer', 'kitchen_id' => 'integer', 'is_del' => 'integer']; + + const CREATED_AT = 'create_time'; + + const UPDATED_AT = 'update_time'; + + /** + * @param int $id + * @return \Hyperf\Database\Model\Model|Builder|null + */ + public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('is_del',MaterialCode::IS_NO_DEL)->where('id',$id)->first(); + } +} diff --git a/app/Model/MaterialCategory.php b/app/Model/MaterialCategory.php new file mode 100644 index 0000000..d66f725 --- /dev/null +++ b/app/Model/MaterialCategory.php @@ -0,0 +1,59 @@ + 'integer', 'parent_id' => 'integer', 'city_id' => 'integer', 'kitchen_id' => 'integer', 'is_del' => 'integer']; + + const CREATED_AT = 'create_time'; + + const UPDATED_AT = 'update_time'; + + /** + * @param int $id + * @return \Hyperf\Database\Model\Model|Builder|null + */ + public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('is_del',MaterialCode::IS_NO_DEL)->where('id',$id)->first(); + } + + /** + * @param int $id + * @return \Hyperf\Database\Model\Model|Builder|null + */ + public function getInfoByPId(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('is_del',MaterialCode::IS_NO_DEL)->where('parent_id',$id)->first(); + } +} diff --git a/app/Request/Admin/MaterialCategoryRequest.php b/app/Request/Admin/MaterialCategoryRequest.php new file mode 100644 index 0000000..f4d441a --- /dev/null +++ b/app/Request/Admin/MaterialCategoryRequest.php @@ -0,0 +1,41 @@ +'required|integer|exists:material_category,id', + 'id' =>'required|integer', + 'name' =>'required|string', + 'parent_id' =>'sometimes|integer|exists:material_category,id', + 'city_id' =>'required|integer|exists:system_city,id', + 'kitchen_id' =>'required|integer|exists:kitchen,id', + + ]; + } + + protected array $scenes = [ + 'material_category_info' => ['query_id'], + 'add' => ['name', 'parent_id', 'city_id', 'kitchen_id'], + 'edit' => ['id','name', 'parent_id', 'city_id', 'kitchen_id'], + 'delete' => ['id'], + ]; +} diff --git a/app/Request/Admin/MaterialRequest.php b/app/Request/Admin/MaterialRequest.php new file mode 100644 index 0000000..75d284c --- /dev/null +++ b/app/Request/Admin/MaterialRequest.php @@ -0,0 +1,44 @@ + 'required|integer', + 'query_name' =>'sometimes|string', + 'id' =>'required|integer', + 'category_id' =>'required|integer|exists:material_category,id', + 'name' =>'required|string', + 'standard' =>'string', + 'unit' =>'required|string', + 'bar_code' =>'string', + 'city_id' =>'required|integer|exists:system_city,id', + 'kitchen_id' =>'required|integer|exists:kitchen,id', + ]; + } + + protected array $scenes = [ + 'list' => ['limit','query_name'], + 'add' => ['category_id', 'name', 'standard', 'unit', 'bar_code', 'city_id', 'kitchen_id'], + 'edit' => ['id','category_id', 'name', 'standard', 'unit', 'bar_code','status'], + 'delete' => ['id'], + ]; +} diff --git a/app/Service/Admin/Material/MaterialCategoryService.php b/app/Service/Admin/Material/MaterialCategoryService.php new file mode 100644 index 0000000..c18b684 --- /dev/null +++ b/app/Service/Admin/Material/MaterialCategoryService.php @@ -0,0 +1,116 @@ +request->input('parent_id',0); + if ($pid > 0){ +// $pidInfo = $this->MaterialCategoryModel->getInfoById($pid); + $model->parent_id = $pid; + } + + $model->name = $this->request->input('name'); + $model->city_id = (int)$this->request->input('city_id',0); + $model->kitchen_id = (int)$this->request->input('kitchen_id',0); + + if (!$model->save()) throw new ErrException('添加失败'); + + return $this->return->success(); + } + + /** + * @return array + */ + public function edit(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->MaterialCategoryModel->getInfoById($id); + if (empty($info)) + throw new ErrException('数据不存在'); + + $info->name = $this->request->input('name'); + + $pid = (int)$this->request->input('parent_id',0); + if ($pid == $id) + throw new ErrException('上级数据不能为自身'); + + $pidInfo = $this->MaterialCategoryModel->getInfoById($pid); + if (empty($pidInfo)) + throw new ErrException('该上级数据不存在'); + + $info->parent_id = $pid; + + $info->city_id = (int)$this->request->input('city_id'); + $info->kitchen_id = (int)$this->request->input('kitchen_id'); + + if (!$info->save()) throw new ErrException('修改失败'); + + return $this->return->success(); + } + + public function delete(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->MaterialCategoryModel->getInfoById($id); + if (empty($info)) throw new ErrException('数据不存在'); + + $children = $this->MaterialCategoryModel->getInfoByPId($id); + if (!empty($children)) throw new ErrException('请先删除下级数据'); + + $info->is_del = MaterialCode::IS_DEL; + if (!$info->save()) throw new ErrException('删除失败'); + + return $this->return->success(); + } + + /** + * @return array + */ + public function findById():array + { + $id = (int)$this->request->input('query_id'); + $info = $this->MaterialCategoryModel->getInfoById($id)->toArray(); + if (empty($info)) throw new ErrException('数据不存在'); + return $this->return->success('success',$info); + + } + + /** + * @return array + */ + public function list(): array + { + $list = $this->MaterialCategoryModel + ->where('is_del',MaterialCode::IS_NO_DEL)->get(); + + return $this->return->success('success',['list' => $list->toArray()]); + } +} \ No newline at end of file diff --git a/app/Service/Admin/Material/MaterialService.php b/app/Service/Admin/Material/MaterialService.php new file mode 100644 index 0000000..4e2ecba --- /dev/null +++ b/app/Service/Admin/Material/MaterialService.php @@ -0,0 +1,102 @@ +request->input('limit', 10); + $name = $this->request->input('query_name'); + + $list = $this + ->MaterialModel + ->where('is_del',MaterialCode::IS_NO_DEL) + ->where('status',MaterialCode::ENABLE) + ->when(!empty($name), function ($query) use ($name) { + $query->where('name', 'like', "$name%"); + }) + ->paginate($limit)->toArray(); + + return $this->return->success('success',$list); + } + + public function add(): array + { + $material = new Material(); + $material->name = $this->request->input('name'); + $material->category_id = (int)$this->request->input('category_id'); + + $material->standard = $this->request->input('standard'); + $material->unit = $this->request->input('unit'); + $material->bar_code = $this->request->input('bar_code'); + $material->city_id = (int)$this->request->input('city_id',0); + $material->kitchen_id = (int)$this->request->input('kitchen_id',0); + + if (!$material->save()) throw new ErrException('添加失败'); + + return $this->return->success(); + } + + public function delete(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->MaterialModel->getInfoById($id); + if (empty($info)) throw new ErrException('数据不存在'); + + $info->is_del = MaterialCode::IS_DEL; + if (!$info->save()) throw new ErrException('删除失败'); + + return $this->return->success(); + } + + /** + * @return array + */ + public function edit(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->MaterialModel->getInfoById($id); + if (empty($info)) + throw new ErrException('数据不存在'); + + $category_id = (int)$this->request->input('category_id'); + $name = $this->request->input('name'); + $standard = $this->request->input('standard'); + $unit = $this->request->input('unit'); + $bar_code = $this->request->input('bar_code'); + $status = (int)$this->request->input('status'); + + if (!empty($category_id)) $info->category_id = $category_id; + if (!empty($name)) $info->name = $name; + if (!empty($standard)) $info->standard = $standard; + if (!empty($unit)) $info->unit = $unit; + if (!empty($bar_code)) $info->bar_code = $bar_code; + if (!empty($status)) $info->status = $status; + + if (!$info->save()) throw new ErrException('修改失败'); + + return $this->return->success(); + } +} \ No newline at end of file diff --git a/sync/http/admin/auth.http b/sync/http/admin/auth.http index 65c30c1..21c7c81 100644 --- a/sync/http/admin/auth.http +++ b/sync/http/admin/auth.http @@ -303,4 +303,53 @@ id=2&name=冻品仓库&city_id=1&kitchen_id=1 ### 仓库删除 GET {{host}}/admin/depot/depot_delete?id=2 Content-Type: application/x-www-form-urlencoded -Authorization: Bearer {{admin_token}} \ No newline at end of file +Authorization: Bearer {{admin_token}} + +### 材料种类添加 +POST {{host}}/admin/material_category/add +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +name=猪肉&city_id=1&kitchen_id=1 + +### 材料种类修改 +POST {{host}}/admin/material_category/edit +content-type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +id=2&name=猪肉&city_id=1&kitchen_id=1&parent_id=1 + +### 材料种类删除 +GET {{host}}/admin/material_category/delete?id=3 +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +### 根据id查询材料种类 +GET {{host}}/admin/material_category/findById?query_id=1 +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +### 材料列表 +GET {{host}}/admin/material/list?limit=10 +content-type: application/json +Authorization: Bearer {{admin_token}} + +### 材料添加 +POST {{host}}/admin/material/add +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +category_id=2&name=冻猪肉&unit=斤&bar_code=1003&city_id=1&kitchen_id=1 + +### 材料删除 +GET {{host}}/admin/material/delete?id=1 +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +### 材料修改 +POST {{host}}/admin/material/edit +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +id=1&category_id=2&name=冻猪肉&standard=2斤/包&unit=包&bar_code=1003&status=1 +