海南旅游SAAS
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.
 
 
 

134 lines
3.7 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;
class LoadSystemCategory extends AbstractTool
{
/**
* @return string
*/
protected $title = '导入系统分类';
# 该属性主要是为了总后台审核成功时自动插入系统分类
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);
}
}
}
}
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
*/
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;
}
}