You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.5 KiB
94 lines
2.5 KiB
<?php
|
|
|
|
namespace App\AdminAgent\Actions\Tree;
|
|
|
|
use App\Models\Category;
|
|
use Dcat\Admin\Actions\Response;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Traits\HasPermissions;
|
|
use Dcat\Admin\Tree\AbstractTool;
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
|
|
class LoadSystemCategory extends AbstractTool
|
|
{
|
|
/**
|
|
* @return string
|
|
*/
|
|
protected $title = '导入系统分类';
|
|
|
|
/**
|
|
* Handle the action request.
|
|
*
|
|
* @param Request $request
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function handle(Request $request)
|
|
{
|
|
$system_cate = Category::where('agent_id', 0)->orderBy('pid')->get();
|
|
$system_name = $system_cate->pluck('name')->toArray();
|
|
|
|
$own_cate = Category::where('agent_id', Admin::user()->id)->get();
|
|
$own_name = $own_cate->pluck('name')->toArray();
|
|
|
|
$no_name = array_diff($system_name, $own_name); //找到系统有,但自己没有的分类名
|
|
$max_id = Category::where('agent_id', Admin::user()->id)->max('id');
|
|
|
|
Category::insert(array_map(function ($name) {
|
|
return [
|
|
'agent_id' => Admin::user()->id,
|
|
'name' => $name,
|
|
'pid' => 0,
|
|
'sort' => 255,
|
|
];
|
|
}, $no_name));
|
|
|
|
$own_cate = Category::where('agent_id', Admin::user()->id)->orderBy('id')->get(); //获取所有分类,包含新插入的分类
|
|
$system_pid = $system_cate->pluck('pid', 'name')->toArray(); //获取原来系统的PID
|
|
$own_id = $own_cate->pluck('id', 'name')->toArray(); //获取自己的PID,不考虑name重复的情况
|
|
|
|
foreach ($own_cate as $cate) {
|
|
//不是新插入的分类不处理
|
|
if ($cate->id <= $max_id) continue;
|
|
|
|
//如果原来系统PID不是0,则获取父类的名称,并在自己的分类中获取对应名称ID,再设置父分类
|
|
$sys_pid = $system_pid[$cate['name']];
|
|
if ($sys_pid != 0) {
|
|
$sys_pid_name = $system_cate->find($sys_pid)['name'];
|
|
|
|
$cate->pid = $own_id[$sys_pid_name];
|
|
$cate->save();
|
|
}
|
|
}
|
|
|
|
return $this->response()->success('操作成功!')->refresh();
|
|
}
|
|
|
|
/**
|
|
* @return string|void
|
|
*/
|
|
protected function href()
|
|
{
|
|
// return admin_url('auth/users');
|
|
}
|
|
|
|
/**
|
|
* @return string|array|void
|
|
*/
|
|
public function confirm()
|
|
{
|
|
return ['操作确认', '确定要导入系统分类吗?'];
|
|
}
|
|
|
|
/**
|
|
* @param Model|Authenticatable|HasPermissions|null $user
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function authorize($user): bool
|
|
{
|
|
return true;
|
|
}
|
|
}
|