feat:material material_category

This commit is contained in:
LAPTOP-7SGDREK0\shiweijun
2025-01-21 17:16:37 +08:00
parent 6b38a6b732
commit 9eebcd008f
10 changed files with 645 additions and 1 deletions

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Constants\Admin;
use Hyperf\Constants\AbstractConstants;
use Hyperf\Constants\Annotation\Constants;
#[Constants]
class MaterialCode extends AbstractConstants
{
/**
* 未删除
*/
const int IS_NO_DEL = 1;
/**
* 已删除
*/
const int IS_DEL = 2;
/**
* 启用
*/
const int ENABLE = 1;
/**
* 禁用
*/
const int DISABLE = 2;
}

View File

@@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\MaterialCategoryRequest;
use App\Service\Admin\Material\MaterialCategoryService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: 'admin/material_category')]
#[Middlewares([
JwtAuthMiddleware::class,
])]
class MaterialCategoryController
{
/**
* 添加材料分类信息
* @param MaterialCategoryRequest $request
* @return array
*/
#[RequestMapping(path: "add", methods: "POST")]
#[Scene(scene: "add")]
public function add(MaterialCategoryRequest $request): array
{
return (new MaterialCategoryService)->add();
}
/**
* 修改材料分类信息
* @param MaterialCategoryRequest $request
* @return array
*/
#[RequestMapping(path: "edit", methods: "POST")]
#[Scene(scene: "edit")]
public function edit(MaterialCategoryRequest $request): array
{
return (new MaterialCategoryService())->edit();
}
/**
* 删除材料分类信息
* @param MaterialCategoryRequest $request
* @return array
*/
#[RequestMapping(path: "delete", methods: "GET")]
#[Scene(scene: "delete")]
public function delete(MaterialCategoryRequest $request): array
{
return (new MaterialCategoryService())->delete();
}
/**
* 根据id查询材料种类
* @param MaterialCategoryRequest $request
* @return array
*/
#[RequestMapping(path: "findById", methods: "GET")]
#[Scene(scene: "material_category_info")]
public function findById(MaterialCategoryRequest $request)
{
return (new MaterialCategoryService)->findById();
}
/**
* 材料钟类列表
* @param MaterialCategoryRequest $request
* @return array
*/
#[RequestMapping(path: "list", methods: "GET")]
#[Scene(scene: "list")]
public function getList(MaterialCategoryRequest $request)
{
return (new MaterialCategoryService)->list();
}
}

View File

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\MaterialRequest;
use App\Service\Admin\Material\MaterialService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: 'admin/material')]
#[Middlewares([
JwtAuthMiddleware::class,
])]
class MaterialController
{
/**
* 材料列表
* @param MaterialRequest $request
* @return array
*/
#[RequestMapping(path: "list", methods: "GET")]
#[Scene(scene: "list")]
public function materialList(MaterialRequest $request): array
{
return (new MaterialService())->materialList();
}
/**
* 添加材料信息
* @param MaterialRequest $request
* @return array
*/
#[RequestMapping(path: "add", methods: "POST")]
#[Scene(scene: "add")]
public function add(MaterialRequest $request): array
{
return (new MaterialService)->add();
}
/**
* 删除材料信息
* @param MaterialRequest $request
* @return array
*/
#[RequestMapping(path: "delete", methods: "GET")]
#[Scene(scene: "delete")]
public function delete(MaterialRequest $request): array
{
return (new MaterialService())->delete();
}
/**
* 修改材料信息
* @param MaterialRequest $request
* @return array
*/
#[RequestMapping(path: "edit", methods: "POST")]
#[Scene(scene: "edit")]
public function edit(MaterialRequest $request): array
{
return (new MaterialService())->edit();
}
}

54
app/Model/Material.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace App\Model;
use App\Constants\Admin\MaterialCode;
use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property int $category_id
* @property string $name
* @property string $standard
* @property string $unit
* @property string $bar_code
* @property int $status
* @property int $city_id
* @property int $kitchen_id
* @property int $is_del
* @property string $create_time
* @property string $update_time
*/
class Material extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'material';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'category_id' => 'integer', 'status' => '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('is_del',MaterialCode::IS_NO_DEL)->where('id',$id)->first();
}
}

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Model;
use App\Constants\Admin\MaterialCode;
use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $name
* @property int $parent_id
* @property int $city_id
* @property int $kitchen_id
* @property int $is_del
* @property string $create_time
* @property string $update_time
*/
class MaterialCategory extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'material_category';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'parent_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('is_del',MaterialCode::IS_NO_DEL)->where('id',$id)->first();
}
/**
* @param int $id
* @return \Hyperf\Database\Model\Model|Builder|null
*/
public function getInfoByPId(int $id): \Hyperf\Database\Model\Model|Builder|null
{
return $this->where('is_del',MaterialCode::IS_NO_DEL)->where('parent_id',$id)->first();
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Request\Admin;
use Hyperf\Validation\Request\FormRequest;
class MaterialCategoryRequest 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 [
'query_id' =>'required|integer|exists:material_category,id',
'id' =>'required|integer',
'name' =>'required|string',
'parent_id' =>'sometimes|integer|exists:material_category,id',
'city_id' =>'required|integer|exists:system_city,id',
'kitchen_id' =>'required|integer|exists:kitchen,id',
];
}
protected array $scenes = [
'material_category_info' => ['query_id'],
'add' => ['name', 'parent_id', 'city_id', 'kitchen_id'],
'edit' => ['id','name', 'parent_id', 'city_id', 'kitchen_id'],
'delete' => ['id'],
];
}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Request\Admin;
use Hyperf\Validation\Request\FormRequest;
class MaterialRequest 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',
'id' =>'required|integer',
'category_id' =>'required|integer|exists:material_category,id',
'name' =>'required|string',
'standard' =>'string',
'unit' =>'required|string',
'bar_code' =>'string',
'city_id' =>'required|integer|exists:system_city,id',
'kitchen_id' =>'required|integer|exists:kitchen,id',
];
}
protected array $scenes = [
'list' => ['limit','query_name'],
'add' => ['category_id', 'name', 'standard', 'unit', 'bar_code', 'city_id', 'kitchen_id'],
'edit' => ['id','category_id', 'name', 'standard', 'unit', 'bar_code','status'],
'delete' => ['id'],
];
}

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

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

View File

@@ -303,4 +303,53 @@ id=2&name=冻品仓库&city_id=1&kitchen_id=1
### 仓库删除 ### 仓库删除
GET {{host}}/admin/depot/depot_delete?id=2 GET {{host}}/admin/depot/depot_delete?id=2
Content-Type: application/x-www-form-urlencoded Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}} Authorization: Bearer {{admin_token}}
### 材料种类添加
POST {{host}}/admin/material_category/add
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
name=猪肉&city_id=1&kitchen_id=1
### 材料种类修改
POST {{host}}/admin/material_category/edit
content-type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
id=2&name=猪肉&city_id=1&kitchen_id=1&parent_id=1
### 材料种类删除
GET {{host}}/admin/material_category/delete?id=3
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
### 根据id查询材料种类
GET {{host}}/admin/material_category/findById?query_id=1
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
### 材料列表
GET {{host}}/admin/material/list?limit=10
content-type: application/json
Authorization: Bearer {{admin_token}}
### 材料添加
POST {{host}}/admin/material/add
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
category_id=2&name=冻猪肉&unit=斤&bar_code=1003&city_id=1&kitchen_id=1
### 材料删除
GET {{host}}/admin/material/delete?id=1
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
### 材料修改
POST {{host}}/admin/material/edit
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{admin_token}}
id=1&category_id=2&name=冻猪肉&standard=2斤/包&unit=包&bar_code=1003&status=1