feat:material material_category
This commit is contained in:
116
app/Service/Admin/Material/MaterialCategoryService.php
Normal file
116
app/Service/Admin/Material/MaterialCategoryService.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Material;
|
||||
|
||||
use App\Constants\Admin\MaterialCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\MaterialCategory;
|
||||
use App\Service\Admin\BaseService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class MaterialCategoryService extends BaseService{
|
||||
|
||||
/**
|
||||
* @var MaterialCategory
|
||||
*/
|
||||
#[Inject]
|
||||
protected MaterialCategory $MaterialCategoryModel;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function add(): array
|
||||
{
|
||||
$model = new MaterialCategory();
|
||||
$pid = (int)$this->request->input('parent_id',0);
|
||||
if ($pid > 0){
|
||||
// $pidInfo = $this->MaterialCategoryModel->getInfoById($pid);
|
||||
$model->parent_id = $pid;
|
||||
}
|
||||
|
||||
$model->name = $this->request->input('name');
|
||||
$model->city_id = (int)$this->request->input('city_id',0);
|
||||
$model->kitchen_id = (int)$this->request->input('kitchen_id',0);
|
||||
|
||||
if (!$model->save()) throw new ErrException('添加失败');
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function edit(): array
|
||||
{
|
||||
$id = (int)$this->request->input('id');
|
||||
|
||||
$info = $this->MaterialCategoryModel->getInfoById($id);
|
||||
if (empty($info))
|
||||
throw new ErrException('数据不存在');
|
||||
|
||||
$info->name = $this->request->input('name');
|
||||
|
||||
$pid = (int)$this->request->input('parent_id',0);
|
||||
if ($pid == $id)
|
||||
throw new ErrException('上级数据不能为自身');
|
||||
|
||||
$pidInfo = $this->MaterialCategoryModel->getInfoById($pid);
|
||||
if (empty($pidInfo))
|
||||
throw new ErrException('该上级数据不存在');
|
||||
|
||||
$info->parent_id = $pid;
|
||||
|
||||
$info->city_id = (int)$this->request->input('city_id');
|
||||
$info->kitchen_id = (int)$this->request->input('kitchen_id');
|
||||
|
||||
if (!$info->save()) throw new ErrException('修改失败');
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function delete(): array
|
||||
{
|
||||
$id = (int)$this->request->input('id');
|
||||
|
||||
$info = $this->MaterialCategoryModel->getInfoById($id);
|
||||
if (empty($info)) throw new ErrException('数据不存在');
|
||||
|
||||
$children = $this->MaterialCategoryModel->getInfoByPId($id);
|
||||
if (!empty($children)) throw new ErrException('请先删除下级数据');
|
||||
|
||||
$info->is_del = MaterialCode::IS_DEL;
|
||||
if (!$info->save()) throw new ErrException('删除失败');
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function findById():array
|
||||
{
|
||||
$id = (int)$this->request->input('query_id');
|
||||
$info = $this->MaterialCategoryModel->getInfoById($id)->toArray();
|
||||
if (empty($info)) throw new ErrException('数据不存在');
|
||||
return $this->return->success('success',$info);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
$list = $this->MaterialCategoryModel
|
||||
->where('is_del',MaterialCode::IS_NO_DEL)->get();
|
||||
|
||||
return $this->return->success('success',['list' => $list->toArray()]);
|
||||
}
|
||||
}
|
||||
102
app/Service/Admin/Material/MaterialService.php
Normal file
102
app/Service/Admin/Material/MaterialService.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Material;
|
||||
|
||||
use App\Constants\Admin\MaterialCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\Material;
|
||||
use App\Service\Admin\BaseService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class MaterialService extends BaseService{
|
||||
|
||||
/**
|
||||
* @var Material
|
||||
*/
|
||||
#[Inject]
|
||||
protected Material $MaterialModel;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function materialList(): array
|
||||
{
|
||||
$limit = (int)$this->request->input('limit', 10);
|
||||
$name = $this->request->input('query_name');
|
||||
|
||||
$list = $this
|
||||
->MaterialModel
|
||||
->where('is_del',MaterialCode::IS_NO_DEL)
|
||||
->where('status',MaterialCode::ENABLE)
|
||||
->when(!empty($name), function ($query) use ($name) {
|
||||
$query->where('name', 'like', "$name%");
|
||||
})
|
||||
->paginate($limit)->toArray();
|
||||
|
||||
return $this->return->success('success',$list);
|
||||
}
|
||||
|
||||
public function add(): array
|
||||
{
|
||||
$material = new Material();
|
||||
$material->name = $this->request->input('name');
|
||||
$material->category_id = (int)$this->request->input('category_id');
|
||||
|
||||
$material->standard = $this->request->input('standard');
|
||||
$material->unit = $this->request->input('unit');
|
||||
$material->bar_code = $this->request->input('bar_code');
|
||||
$material->city_id = (int)$this->request->input('city_id',0);
|
||||
$material->kitchen_id = (int)$this->request->input('kitchen_id',0);
|
||||
|
||||
if (!$material->save()) throw new ErrException('添加失败');
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function delete(): array
|
||||
{
|
||||
$id = (int)$this->request->input('id');
|
||||
|
||||
$info = $this->MaterialModel->getInfoById($id);
|
||||
if (empty($info)) throw new ErrException('数据不存在');
|
||||
|
||||
$info->is_del = MaterialCode::IS_DEL;
|
||||
if (!$info->save()) throw new ErrException('删除失败');
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function edit(): array
|
||||
{
|
||||
$id = (int)$this->request->input('id');
|
||||
|
||||
$info = $this->MaterialModel->getInfoById($id);
|
||||
if (empty($info))
|
||||
throw new ErrException('数据不存在');
|
||||
|
||||
$category_id = (int)$this->request->input('category_id');
|
||||
$name = $this->request->input('name');
|
||||
$standard = $this->request->input('standard');
|
||||
$unit = $this->request->input('unit');
|
||||
$bar_code = $this->request->input('bar_code');
|
||||
$status = (int)$this->request->input('status');
|
||||
|
||||
if (!empty($category_id)) $info->category_id = $category_id;
|
||||
if (!empty($name)) $info->name = $name;
|
||||
if (!empty($standard)) $info->standard = $standard;
|
||||
if (!empty($unit)) $info->unit = $unit;
|
||||
if (!empty($bar_code)) $info->bar_code = $bar_code;
|
||||
if (!empty($status)) $info->status = $status;
|
||||
|
||||
if (!$info->save()) throw new ErrException('修改失败');
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user