145 lines
5.1 KiB
PHP
145 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Admin\Material;
|
|
|
|
use App\Constants\Common\MaterialCode;
|
|
use App\Exception\ErrException;
|
|
use App\Model\Material;
|
|
use App\Model\MaterialStock;
|
|
use App\Service\Admin\BaseService;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class MaterialService extends BaseService{
|
|
|
|
/**
|
|
* @var Material
|
|
*/
|
|
#[Inject]
|
|
protected Material $MaterialModel;
|
|
|
|
#[Inject]
|
|
protected MaterialStock $MaterialStockModel;
|
|
|
|
public function handle()
|
|
{
|
|
|
|
}
|
|
|
|
public function materialList(): array
|
|
{
|
|
$limit = (int)$this->request->input('limit', 10);
|
|
$name = $this->request->input('query_name');
|
|
$kitchenId = $this->request->input('query_kitchen_id');
|
|
|
|
$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%");
|
|
})
|
|
->when(!empty($kitchenId), function ($query) use ($kitchenId) {
|
|
$query->where('kitchen_id', $kitchenId);
|
|
})
|
|
->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();
|
|
}
|
|
|
|
public function materialStockList(): array
|
|
{
|
|
$limit = (int)$this->request->input('limit', 10);
|
|
$name = $this->request->input('query_name');
|
|
$materialId = $this->request->input('query_materialId');
|
|
$depotId = (int)$this->request->input('query_depotId');
|
|
$supplierId = (int)$this->request->input('query_supplierId');
|
|
$kitchenId = (int)$this->request->input('query_kitchenId');
|
|
$list = $this->MaterialStockModel
|
|
->leftJoin('material', 'material_stock.material_id', '=', 'material.id')
|
|
->leftJoin('supplier', 'material_stock.supplier_id', '=', 'supplier.id')
|
|
->leftJoin('depot', 'material_stock.depot_id', '=', 'depot.id')
|
|
->where('material_stock.is_del',MaterialCode::IS_NO_DEL)
|
|
->when(!empty($name), function ($query) use ($name) {
|
|
$query->where('material.name', 'like', "$name%");
|
|
})
|
|
->when(!empty($materialId), function ($query) use ($materialId) {
|
|
$query->where('material_stock.material_id', $materialId);
|
|
})
|
|
->when(!empty($depotId), function ($query) use ($depotId) {
|
|
$query->where('material_stock.depot_id', $depotId);
|
|
})
|
|
->when(!empty($supplierId), function ($query) use ($supplierId) {
|
|
$query->where('material_stock.supplier_id', $supplierId);
|
|
})
|
|
->when(!empty($kitchenId), function ($query) use ($kitchenId) {
|
|
$query->where('material.kitchen_id', $kitchenId);
|
|
})
|
|
->paginate($limit,['material_stock.*','material.name as material_name','supplier.name as supplier_name','depot.name as depot_name'])
|
|
->toArray();
|
|
|
|
return $this->return->success('success',$list);
|
|
}
|
|
|
|
} |