feat:supplier
This commit is contained in:
66
app/Controller/Admin/SupplierController.php
Normal file
66
app/Controller/Admin/SupplierController.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Middleware\Admin\JwtAuthMiddleware;
|
||||
use App\Request\Admin\SupplierRequest;
|
||||
use App\Service\Admin\Material\SupplierService;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middlewares;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use Hyperf\Validation\Annotation\Scene;
|
||||
|
||||
#[Controller(prefix: 'admin/supplier')]
|
||||
#[Middlewares([
|
||||
JwtAuthMiddleware::class,
|
||||
])]
|
||||
class SupplierController
|
||||
{
|
||||
/**
|
||||
* 供应商列表
|
||||
* @param SupplierRequest $request
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "list", methods: "GET")]
|
||||
#[Scene(scene: "list")]
|
||||
public function list(SupplierRequest $request): array
|
||||
{
|
||||
return (new SupplierService())->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();
|
||||
}
|
||||
}
|
||||
50
app/Model/Supplier.php
Normal file
50
app/Model/Supplier.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\Database\Model\Builder;
|
||||
use Hyperf\DbConnection\Model\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $mobile
|
||||
* @property string $address
|
||||
* @property int $city_id
|
||||
* @property int $kitchen_id
|
||||
* @property int $is_del
|
||||
* @property string $create_time
|
||||
* @property string $update_time
|
||||
*/
|
||||
class Supplier extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'supplier';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['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('id',$id)->where('is_del',1)->first();
|
||||
}
|
||||
}
|
||||
39
app/Request/Admin/SupplierRequest.php
Normal file
39
app/Request/Admin/SupplierRequest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Request\Admin;
|
||||
|
||||
use Hyperf\Validation\Request\FormRequest;
|
||||
|
||||
class SupplierRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'limit' => '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'],
|
||||
];
|
||||
}
|
||||
105
app/Service/Admin/Material/SupplierService.php
Normal file
105
app/Service/Admin/Material/SupplierService.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Material;
|
||||
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\Supplier;
|
||||
use App\Service\Admin\BaseService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class SupplierService extends BaseService{
|
||||
|
||||
/**
|
||||
* @var Supplier
|
||||
*/
|
||||
#[Inject]
|
||||
protected Supplier $SupplierModel;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function list():array
|
||||
{
|
||||
$limit = (int)$this->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();
|
||||
}
|
||||
}
|
||||
@@ -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}}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user