feat:supplier

This commit is contained in:
LAPTOP-7SGDREK0\shiweijun
2025-01-22 17:53:00 +08:00
parent d231f343d3
commit 4e11fbaec8
5 changed files with 284 additions and 0 deletions

View 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
View 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();
}
}

View 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'],
];
}

View 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();
}
}