168 lines
4.7 KiB
PHP
168 lines
4.7 KiB
PHP
<?php
|
|
namespace App\Domains\Category\Services;
|
|
|
|
use App\Core\Service;
|
|
use Illuminate\Validation\Rule;
|
|
use App\Exceptions\NotExistException;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Exceptions\InvalidArgumentException;
|
|
use App\Domains\Category\Repositories\CategoryRepository;
|
|
|
|
class CategoryService extends Service
|
|
{
|
|
public static $types = [
|
|
'test', 'service', 'device'
|
|
];
|
|
|
|
protected $categoryRepository;
|
|
protected $application;
|
|
protected $account;
|
|
protected $type;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(CategoryRepository $categoryRepository)
|
|
{
|
|
$this->categoryRepository = $categoryRepository;
|
|
$this->application = app('dipper')->application;
|
|
$this->account = app('dipper')->initAccount();
|
|
}
|
|
|
|
/**
|
|
* 分类列表
|
|
*
|
|
* @param array $conditions
|
|
* @return void
|
|
*/
|
|
public function index($conditions = [])
|
|
{
|
|
self::verifyType($conditions['type']);
|
|
|
|
$query = $this->categoryRepository->withConditions($conditions)->applyConditions();
|
|
|
|
if ($conditions['limit']) {
|
|
$categories = $query->paginate($conditions['limit']);
|
|
} else {
|
|
$categories = $query->get();
|
|
}
|
|
|
|
$parent_ids = $categories->pluck('parent_id')->unique()->except([0])->toArray();
|
|
|
|
$parents = $this->categoryRepository->select('id', 'name')->withConditions(['ids' => $parent_ids])->get()->keyBy('id');
|
|
|
|
$categories->map(function ($item) use ($parents) {
|
|
$item->parent_name = $parents[$item->parent_id]['name'] ?? '';
|
|
});
|
|
|
|
return $categories;
|
|
}
|
|
|
|
/**
|
|
* Show
|
|
*
|
|
* @param array $conditions
|
|
* @return void
|
|
*/
|
|
public function show($id)
|
|
{
|
|
return $this->categoryRepository->applyConditions()->find($id);
|
|
}
|
|
|
|
/**
|
|
* 添加\编辑分类
|
|
*
|
|
* @param array $attributes
|
|
* @return void
|
|
*/
|
|
public function store(array $attributes)
|
|
{
|
|
$attributes = array_only($attributes, ['id', 'parent_id', 'name', 'type', 'extend', 'status']);
|
|
$attributes['appid'] = $this->application->id;
|
|
$attributes['account_id'] = $this->account->id;
|
|
|
|
$rule = [
|
|
'appid' => ['required'],
|
|
'name' => ['display_length:32', Rule::unique($this->categoryRepository->getTable())->where(function ($query) use ($attributes) {
|
|
return $query->where('appid', $attributes['appid'])->where('type', $attributes['type'])->where('name', $attributes['name']);
|
|
})->ignore($attributes['id'])],
|
|
'type' => [Rule::in(self::$types)],
|
|
'status' => ['in:0,1'],
|
|
];
|
|
|
|
if (!$attributes['id']) {
|
|
$rule['name'][] = 'required';
|
|
}
|
|
|
|
$message = [
|
|
'name.unique' => '分类名已存在',
|
|
'appid.required' => '该账号未分配到应用',
|
|
];
|
|
|
|
Validator::validate($attributes, $rule, $message);
|
|
|
|
if ($attributes['parent_id']) {
|
|
if (!$parent = $this->categoryRepository->ofType($attributes['type'])->find($attributes['parent_id'])) {
|
|
throw new NotExistException('父级分类不存在或已删除');
|
|
}
|
|
}
|
|
|
|
if (!$attributes['id']) {
|
|
$category = $this->categoryRepository->create($attributes, $parent);
|
|
return $category;
|
|
}
|
|
|
|
if (!$category = $this->categoryRepository->ofType($attributes['type'])->find($attributes['id'])) {
|
|
throw new NotExistException('分类不存在或已删除');
|
|
}
|
|
|
|
if ($parent && $category->parent_id !== $parent->id) {
|
|
$category->appendToNode($parent);
|
|
}
|
|
|
|
$this->categoryRepository->setModel($category)->update($attributes);
|
|
|
|
return $category;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @param int|string|array $ids
|
|
* @return bool
|
|
*/
|
|
public function destroy($type, $ids)
|
|
{
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
|
|
$categories = $this->categoryRepository->with('children')->withConditions(['type' => $type, 'ids' => $ids])->get();
|
|
|
|
foreach ($categories as $category) {
|
|
if (count($category->children)) {
|
|
throw new InvalidArgumentException("分类($category)含有子分类, 不能删除");
|
|
}
|
|
}
|
|
|
|
$this->categoryRepository->withConditions($conditions)->destroy($ids);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 验证类型是否合法
|
|
*
|
|
* @param string $type
|
|
* @return void
|
|
*/
|
|
public static function verifyType($type)
|
|
{
|
|
if (!in_array($type, self::$types)) {
|
|
throw new InvalidArgumentException('类型不合法');
|
|
}
|
|
|
|
return $type;
|
|
}
|
|
}
|