114 lines
2.9 KiB
PHP
114 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Admin\Depot;
|
|
|
|
use App\Constants\Admin\DepotCode;
|
|
use App\Exception\ErrException;
|
|
use App\Model\Depot;
|
|
use App\Service\Admin\BaseService;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class DepotService extends BaseService{
|
|
|
|
/**
|
|
* @var Depot
|
|
*/
|
|
#[Inject]
|
|
protected Depot $DepotModel;
|
|
|
|
public function handle()
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function depotList():array
|
|
{
|
|
$limit = (int)$this->request->input('limit', 10);
|
|
$id = (int)$this->request->input('query_id');
|
|
$kitchenId = (int)$this->request->input('query_kitchen_id');
|
|
|
|
$list = $this->DepotModel
|
|
->where('is_del',DepotCode::IS_NO_DEL)
|
|
->when($id,function ($query) use ($id) {
|
|
$query->where('id',$id);
|
|
})
|
|
->when($kitchenId,function ($query) use ($kitchenId) {
|
|
$query->where('kitchen_id',$kitchenId);
|
|
})
|
|
->paginate($limit)->toArray();
|
|
|
|
return $this->return->success('success',$list);
|
|
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function add():array
|
|
{
|
|
$name = $this->request->input('name');
|
|
$kitchen_id = (int)$this->request->input('kitchen_id');
|
|
$info = $this->DepotModel->getInfoByName($name,$kitchen_id);
|
|
if (!empty($info)) throw new ErrException('仓库已存在');
|
|
|
|
$depot = new Depot();
|
|
$depot->name = $name;
|
|
$depot->city_id = $this->request->input('city_id');
|
|
$depot->kitchen_id = $kitchen_id;
|
|
|
|
if (!$depot->save()) throw new ErrException('仓库添加失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function edit(): array
|
|
{
|
|
$id = (int)$this->request->input('id');
|
|
$depotName = $this->request->input('name');
|
|
$kitchen_id = (int)$this->request->input('kitchen_id');
|
|
|
|
$info = $this->DepotModel->getInfoById($id);
|
|
if (empty($info)) throw new ErrException('数据不存在');
|
|
|
|
$name = $this->DepotModel->getInfoByName($depotName,$kitchen_id);
|
|
if (!empty($name)){
|
|
if ($name->id != $info->id && $info->kitchen_id == $kitchen_id)
|
|
throw new ErrException('仓库已存在');
|
|
}
|
|
|
|
$info->name = $depotName;
|
|
$info->city_id = (int)$this->request->input('city_id');
|
|
$info->kitchen_id = $kitchen_id;
|
|
|
|
if (!$info->save()) throw new ErrException('仓库修改失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function delete(): array
|
|
{
|
|
$id = (int)$this->request->input('id');
|
|
|
|
$info = $this->DepotModel->getInfoById($id);
|
|
if (empty($info)) throw new ErrException('仓库不存在');
|
|
|
|
$info->is_del = DepotCode::IS_DEL;
|
|
|
|
if (!$info->save()) throw new ErrException('删除失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
|
|
} |