feat : section

This commit is contained in:
2024-11-07 16:17:57 +08:00
parent 7fd07c55d8
commit b6b2627129
11 changed files with 531 additions and 3 deletions

View File

@@ -0,0 +1,156 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Cache\Redis\Admin\SectionCache;
use App\Exception\AdminException;
use App\Model\AdminSection;
use App\Model\AdminUser;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Admin\SectionTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
class SectionService extends BaseService
{
use SectionTrait;
/**
* 部门注入模型
* @var AdminSection $adminSectionModel
*/
#[Inject]
protected AdminSection $adminSectionModel;
/**
* @var AdminUser
*/
#[Inject]
protected AdminUser $adminUserModel;
/**
* @var SectionCache
*/
#[Inject]
protected SectionCache $sectionCache;
/**
* @return array
*/
public function handle(): array
{
$all = $this->adminSectionModel->get(['id','pid','name','status','remark']);
if (empty($all)) return $this->return->success('success',['list' => []]);
$data = $this->getDbList($all->toArray());
return $this->return->success('success',['list' => $data]);
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function add(): array
{
$model = new AdminSection();
$pid = (int)$this->request->input('pid',0);
if ($pid > 0) $pidInfo = $this->adminSectionModel->getInfoById($pid);
$model->name = $this->request->input('name');
$model->pid = $pid;
$model->city_id = !empty($pidInfo) ? $pidInfo->city_id : $this->request->input('city_id',0);
$model->remark = $this->request->input('remark');
$model->status = $this->request->input('status',1);
if (!$model->save()) throw new AdminException('添加失败');
$this->sectionCache->delList();
return $this->return->success();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$info = $this->adminSectionModel->getInfoById($id);
if (empty($info)) throw new AdminException('数据不存在');
$info->name = $this->request->input('name');
$info->pid = $this->request->input('pid',0);
$info->city_id = $this->request->input('city_id',0);
$info->remark = $this->request->input('remark');
$info->status = $this->request->input('status',1);
if (!$info->save()) throw new AdminException('修改失败');
$this->sectionCache->delList();
return $this->return->success();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function del(): array
{
$id = (int)$this->request->input('id');
$info = $this->adminSectionModel->getInfoById($id);
if (empty($info)) throw new AdminException('数据不存在');
$children = $this->adminSectionModel->getInfoByPid($info->id);
if (empty($children)) throw new AdminException('请先删除下级数据');
$this->adminUserModel->where('section_id',$id)->update(['section_id'=>0]);
$info->delete();
$this->sectionCache->delList();
return $this->return->success();
}
/**
* @return array
*/
public function info(): array
{
$id = (int)$this->request->input('id');
$info = $this->adminSectionModel->getInfoById($id);
if (empty($info)) throw new AdminException('数据不存在');
return $this->return->success('success',$info->toArray());
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function allList(): array
{
return $this->return->success('success',['list' => $this->sectionCache->getList()]);
}
}