130 lines
3.8 KiB
PHP
130 lines
3.8 KiB
PHP
<?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\Model\MaterialApplication;
|
|
use App\Service\Api\BaseService;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class DishService extends BaseService
|
|
{
|
|
|
|
/**
|
|
* @var Dish
|
|
*/
|
|
#[Inject]
|
|
protected Dish $DishModel;
|
|
|
|
#[Inject]
|
|
protected MaterialApplication $MaterialApplication;
|
|
|
|
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 delete(): array
|
|
{
|
|
$id = (int)$this->request->input('id');
|
|
$info = $this->DishModel->getInfoById($id);
|
|
$application = $this->MaterialApplication
|
|
->where('dish_id', $id)
|
|
->where('is_del', DishCode::IS_NO_DEL)
|
|
->first();
|
|
if (!empty($application)) throw new ErrException('该菜品存在申请材料');
|
|
|
|
$info->is_del = DishCode::IS_DELETE;
|
|
|
|
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
|
|
->leftJoin('cycle','dish.cycle_id','=','cycle.id')
|
|
->where('chef_id', $chef_id)
|
|
->orderBy('cycle.dates','desc')
|
|
->paginate($limit,['dish.*','cycle.dates'])->toArray();
|
|
|
|
return $this->return->success('success',$list);
|
|
}
|
|
|
|
} |