|
|
|
@ -9,7 +9,6 @@ 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 |
|
|
|
{ |
|
|
|
@ -18,56 +17,94 @@ class LoadSystemCategory extends AbstractTool |
|
|
|
*/ |
|
|
|
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(); |
|
|
|
|
|
|
|
$name2publish_type = $system_cate->pluck('publish_type', '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) use ($name2publish_type) { |
|
|
|
return [ |
|
|
|
'agent_id' => Admin::user()->id, |
|
|
|
'name' => $name, |
|
|
|
'pid' => 0, |
|
|
|
'sort' => 255, |
|
|
|
'publish_type' => $name2publish_type[$name], |
|
|
|
]; |
|
|
|
}, $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(); |
|
|
|
# 该属性主要是为了总后台审核成功时自动插入系统分类
|
|
|
|
private int $agent_id = 0; |
|
|
|
|
|
|
|
public function __construct($title = null, $agent_id = 0) |
|
|
|
{ |
|
|
|
parent::__construct($title); |
|
|
|
$this->agent_id = $agent_id ?: Admin::user()->id; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Handle the action request. |
|
|
|
* |
|
|
|
* @return Response |
|
|
|
*/ |
|
|
|
public function handle() |
|
|
|
{ |
|
|
|
$sys_cate = Category::where('agent_id', 0)->orderBy('sort')->get(['id', 'pid', 'name', 'sort', 'publish_type'])->toArray(); |
|
|
|
$sys_cate = $this->node_merge($sys_cate); |
|
|
|
|
|
|
|
$self_cate = Category::where('agent_id', $this->agent_id)->orderBy('sort')->get(['id', 'pid', 'name', 'sort', 'publish_type'])->toArray(); |
|
|
|
$self_cate = $this->node_merge($self_cate); |
|
|
|
|
|
|
|
$this->insert_cate($sys_cate, $self_cate); |
|
|
|
return $this->response()->success('导入系统分类成功!')->refresh(); |
|
|
|
} |
|
|
|
|
|
|
|
private function insert_cate($sys_cate, $self_cate, $pid = 0) |
|
|
|
{ |
|
|
|
$agent_id = $this->agent_id; |
|
|
|
|
|
|
|
foreach ($sys_cate as $cate) { |
|
|
|
$insert_data = $cate; |
|
|
|
|
|
|
|
$insert_data['agent_id'] = $agent_id; |
|
|
|
$insert_data['pid'] = $pid; |
|
|
|
unset($insert_data['id'], $insert_data['child']); |
|
|
|
|
|
|
|
# 如果原来已经有的(通过name和publish_type判断是否已经存在),不再重复插入,只获取pid值
|
|
|
|
$exists_cate = array_filter($self_cate, fn($a) => $a['name'] == $cate['name'] && $a['publish_type'] == $cate['publish_type']); |
|
|
|
if ($exists_cate) { |
|
|
|
$pid2 = current($exists_cate)['id']; |
|
|
|
} else { |
|
|
|
$pid2 = Category::insertGetId($insert_data); |
|
|
|
} |
|
|
|
|
|
|
|
if (!empty($cate['child']) && is_array($cate['child'])) { |
|
|
|
# 如果有子栏目含有下级栏目,则一个个插入
|
|
|
|
if (array_filter($cate['child'], fn($a) => !empty($a['child']))) { |
|
|
|
$this->insert_cate($cate['child'], current($exists_cate)['child'] ?? [], $pid2); |
|
|
|
} |
|
|
|
# 否则直接整个child插入
|
|
|
|
else { |
|
|
|
$insert_data = array_map(function ($a) use ($agent_id, $pid2) { |
|
|
|
$a['agent_id'] = $agent_id; |
|
|
|
$a['pid'] = $pid2; |
|
|
|
|
|
|
|
unset($a['id'], $a['child']); |
|
|
|
return $a; |
|
|
|
}, $cate['child']); |
|
|
|
|
|
|
|
# 如果已经存在,则不再重复插入
|
|
|
|
$child = !empty(current($exists_cate)['child']) ? current($exists_cate)['child'] : []; |
|
|
|
if (!empty($child)) { |
|
|
|
foreach ($insert_data as $k => &$cate2) { |
|
|
|
if (array_filter($child, fn($a) => $a['name'] == $cate2['name'] && $a['publish_type'] == $cate2['publish_type'])) { |
|
|
|
unset($insert_data[$k]); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Category::insert($insert_data); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return $this->response()->success('操作成功!')->refresh(); |
|
|
|
} |
|
|
|
private function node_merge(array $arr, int $pid = 0, string $pidKey = 'pid', string $childrenKey = 'child', string $idKey = 'id'): array |
|
|
|
{ |
|
|
|
$rs = []; |
|
|
|
$arr = array_column($arr, null, $idKey); |
|
|
|
foreach ($arr as $k => $v) { |
|
|
|
if ($v[$pidKey] === $pid) { |
|
|
|
$rs[] = &$arr[$k]; |
|
|
|
} else { |
|
|
|
$arr[$v[$pidKey]][$childrenKey][] = &$arr[$k]; |
|
|
|
} |
|
|
|
} |
|
|
|
return $rs; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @return string|void |
|
|
|
|