From 4e11fbaec8d7a108c6008990d8a5d9f57131fbce Mon Sep 17 00:00:00 2001 From: "LAPTOP-7SGDREK0\\shiweijun" <411582373@qq.com> Date: Wed, 22 Jan 2025 17:53:00 +0800 Subject: [PATCH] feat:supplier --- app/Controller/Admin/SupplierController.php | 66 +++++++++++ app/Model/Supplier.php | 50 +++++++++ app/Request/Admin/SupplierRequest.php | 39 +++++++ .../Admin/Material/SupplierService.php | 105 ++++++++++++++++++ sync/http/admin/auth.http | 24 ++++ 5 files changed, 284 insertions(+) create mode 100644 app/Controller/Admin/SupplierController.php create mode 100644 app/Model/Supplier.php create mode 100644 app/Request/Admin/SupplierRequest.php create mode 100644 app/Service/Admin/Material/SupplierService.php diff --git a/app/Controller/Admin/SupplierController.php b/app/Controller/Admin/SupplierController.php new file mode 100644 index 0000000..d7241e8 --- /dev/null +++ b/app/Controller/Admin/SupplierController.php @@ -0,0 +1,66 @@ +list(); + } + + /** + * 添加供应商 + * @param SupplierRequest $request + * @return array + */ + #[RequestMapping(path: "add", methods: "POST")] + #[Scene(scene: "add")] + public function add(SupplierRequest $request): array + { + return (new SupplierService)->add(); + } + + /** + * 修改供应商 + * @return array + */ + #[RequestMapping(path: "edit", methods: "POST")] + #[Scene(scene: "edit")] + public function edit(SupplierRequest $request): array + { + return (new SupplierService)->edit(); + } + + /** + * 删除供应商 + * @return array + */ + #[RequestMapping(path: "delete", methods: "GET")] + #[Scene(scene: "delete")] + public function delete(SupplierRequest $request): array + { + return (new SupplierService)->delete(); + } +} diff --git a/app/Model/Supplier.php b/app/Model/Supplier.php new file mode 100644 index 0000000..1395a06 --- /dev/null +++ b/app/Model/Supplier.php @@ -0,0 +1,50 @@ + '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('id',$id)->where('is_del',1)->first(); + } +} diff --git a/app/Request/Admin/SupplierRequest.php b/app/Request/Admin/SupplierRequest.php new file mode 100644 index 0000000..6103cab --- /dev/null +++ b/app/Request/Admin/SupplierRequest.php @@ -0,0 +1,39 @@ + 'required|integer', + 'query_name' => 'sometimes|string', + 'mobile' => 'digits:11', + 'city_id' => 'required|integer|exists:system_city,id', + 'kitchen_id' => 'required|integer|exists:kitchen,id', + ]; + } + + protected array $scenes = [ + 'list' => ['limit','query_name'], + 'add' => ['name','mobile','address','city_id','kitchen_id'], + 'edit' => ['id','name','mobile','address'], + 'delete' => ['id'], + ]; +} diff --git a/app/Service/Admin/Material/SupplierService.php b/app/Service/Admin/Material/SupplierService.php new file mode 100644 index 0000000..dcc0bcd --- /dev/null +++ b/app/Service/Admin/Material/SupplierService.php @@ -0,0 +1,105 @@ +request->input('limit', 10); + $name = (int)$this->request->input('query_name'); + + $list = $this->SupplierModel + ->leftJoin('system_city','system_city.id','supplier.city_id') + ->leftJoin('kitchen','kitchen.id','supplier.kitchen_id') + ->where('supplier.is_del',1) + ->when(!empty($name), function ($query) use ($name) { + $query->where('supplier.name', 'like', "$name%"); + }) + ->paginate($limit,['supplier.*','system_city.title as city_name','kitchen.name as kitchen_name']) + ->toArray(); + + return $this->return->success('success',$list); + + } + + /** + * @return array + */ + public function add():array + { + $name = $this->request->input('name'); + $mobile = $this->request->input('mobile'); + $address = $this->request->input('address'); + $city_id = (int)$this->request->input('city_id'); + $kitchen_id = (int)$this->request->input('kitchen_id'); + + $supplier = new Supplier(); + $supplier->name = $name; + $supplier->mobile = $mobile; + $supplier->address = $address; + $supplier->city_id = $city_id; + $supplier->kitchen_id = $kitchen_id; + + if (!$supplier->save()) throw new ErrException('供应商添加失败'); + + return $this->return->success(); + } + + public function edit():array + { + $id = (int)$this->request->input('id'); + $name = $this->request->input('name'); + $mobile = $this->request->input('mobile'); + $address = $this->request->input('address'); + + $info = $this->SupplierModel->getInfoById($id); + if (empty($info)) throw new ErrException('数据不存在'); + + if(!empty($name)) $info->name = $name; + if(!empty($mobile)) $info->mobile = $mobile; + if(!empty($address)) $info->address = $address; + + if (!$info->save()) throw new ErrException('供应商修改失败'); + + return $this->return->success(); + } + + /** + * @return array + */ + public function delete(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->SupplierModel->getInfoById($id); + if (empty($info)) throw new ErrException('供应商不存在'); + + $info->is_del = 2; + + 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 21c7c81..1314783 100644 --- a/sync/http/admin/auth.http +++ b/sync/http/admin/auth.http @@ -353,3 +353,27 @@ Authorization: Bearer {{admin_token}} id=1&category_id=2&name=冻猪肉&standard=2斤/包&unit=包&bar_code=1003&status=1 +### 供应商列表 +GET {{host}}/admin/supplier/list?limit=10 +content-type: application/json +Authorization: Bearer {{admin_token}} + +### 供应商添加 +POST {{host}}/admin/supplier/add +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +name=1号&mobile=15864359431&address=深圳市南山区&city_id=1&kitchen_id=1 + +### 供应商修改 +POST {{host}}/admin/supplier/edit +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +id=1&name=1号供应商&mobile=15864359431&address=广东省深圳市南山区 + +### 供应商删除 +GET {{host}}/admin/supplier/delete?id=1 +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} +