feat:dish

This commit is contained in:
LAPTOP-7SGDREK0\shiweijun
2025-02-06 15:30:15 +08:00
parent c0b032d706
commit c0dffe3dc7
5 changed files with 228 additions and 8 deletions

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App\Controller\Api;
use App\Middleware\Api\JwtAuthMiddleware;
use App\Request\Api\DishRequest;
use App\Service\Api\Material\DishService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: 'api/dish')]
#[Middlewares([
JwtAuthMiddleware::class,
])]
class DishController
{
/**
* 菜品添加
* @param DishRequest $request
* @return array
*/
#[RequestMapping(path: "add", methods: "POST")]
#[Scene(scene: "add")]
public function dishAdd(dishRequest $request): array
{
return (new DishService())->add();
}
/**
* 菜品修改
* @param DishRequest $request
* @return array
*/
#[RequestMapping(path: "edit", methods: "POST")]
#[Scene(scene: "edit")]
public function dishEdit(dishRequest $request): array
{
return (new DishService())->edit();
}
/**
* 菜品列表
* @param DishRequest $request
* @return array
*/
#[RequestMapping(path: "list", methods: "GET")]
#[Scene(scene: "list")]
public function dishList(dishRequest $request): array
{
return (new DishService())->list();
}
}

View File

@@ -17,7 +17,6 @@ use Hyperf\DbConnection\Model\Model;
* @property string $side_dish
* @property string $flavor
* @property int $cycle_id
* @property int $status
* @property int $city_id
* @property int $kitchen_id
* @property int $chef_id
@@ -40,7 +39,7 @@ class Dish extends Model
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'pre_quantity' => 'integer', 'cycle_id' => 'integer', 'status' => 'integer', 'city_id' => 'integer','kitchen_id' => 'integer', 'chef_id' => 'integer', 'is_del' => 'integer'];
protected array $casts = ['id' => 'integer', 'pre_quantity' => 'integer', 'cycle_id' => 'integer', 'city_id' => 'integer','kitchen_id' => 'integer', 'chef_id' => 'integer', 'is_del' => 'integer'];
const string CREATED_AT = 'create_time';

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Request\Api;
use Hyperf\Validation\Request\FormRequest;
class DishRequest 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',
'dish_name' => 'sometimes|string',
'cycle_id' => 'sometimes|integer|exists:cycle,id',
'city_id' => 'sometimes|integer|exists:system_city,id',
'kitchen_id' => 'sometimes|integer|exists:kitchen,id',
];
}
protected array $scenes = [
'add' => ['dish_name','profile','pre_quantity','price','side_dish','flavor','cycle_id','city_id','kitchen_id'],
'edit' => ['id','dish_name','profile','pre_quantity','price','side_dish','flavor'],
'list' =>['limit']
];
}

View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace App\Service\Api\Material;
use App\Constants\Common\DishCode;
use App\Exception\ErrException;
use App\Model\Dish;
use App\Service\Api\BaseService;
use Hyperf\Di\Annotation\Inject;
class DishService extends BaseService
{
/**
* @var Dish
*/
#[Inject]
protected Dish $DishModel;
public function handle()
{
}
/**
* @return array
*/
public function add(): array
{
$dish_name = $this->request->input('dish_name');
$profile = $this->request->input('profile');
$pre_quantity = (int)$this->request->input('pre_quantity');
$price = (double)$this->request->input('price');
$side_dish = $this->request->input('side_dish');
$flavor = $this->request->input('flavor');
$cycle_id = (int)$this->request->input('cycle_id');
$city_id = (int)$this->request->input('city_id');
$kitchen_id = (int)$this->request->input('kitchen_id');
$chef_id = $this->userId;
$dish = new Dish();
$dish ->dish = $dish_name;
$dish ->profile = $profile;
$dish ->pre_quantity = $pre_quantity;
$dish ->price = $price;
$dish ->side_dish = $side_dish;
$dish ->flavor = $flavor;
$dish ->cycle_id = $cycle_id;
$dish ->city_id = $city_id;
$dish ->kitchen_id = $kitchen_id;
$dish ->chef_id = $chef_id;
if (!$dish->save()) throw new ErrException('菜品添加失败');
return $this->return->success('success');
}
public function edit(): array
{
$dish_id = (int)$this->request->input('id');
$dish_name = $this->request->input('dish_name');
$profile = $this->request->input('profile');
$pre_quantity = (int)$this->request->input('pre_quantity');
$price = (double)$this->request->input('price');
$side_dish = $this->request->input('side_dish');
$flavor = $this->request->input('flavor');
$info = $this->DishModel->getInfoById($dish_id);
if (empty($info)) throw new ErrException('数据不存在');
if (!empty($dish_name)) {
$info->dish = $dish_name;
}
if (!empty($profile)) {
$info->profile = $profile;
}
if (!empty($pre_quantity)) {
$info->pre_quantity = $pre_quantity;
}
if (!empty($price)) {
$info->price = $price;
}
if (!empty($side_dish)) {
$info->side_dish = $side_dish;
}
if (!empty($flavor)) {
$info->flavor = $flavor;
}
if (!$info->save()) throw new ErrException('菜品修改失败');
return $this->return->success();
}
public function list(): array
{
$limit = (int)$this->request->input('limit');
$chef_id = $this->userId;
$list = $this->DishModel
->where('chef_id', $chef_id)
->paginate($limit)->toArray();
return $this->return->success('success',$list);
}
}