增加菜单迁移脚本
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
// app/service/ResourceService.php
|
||||
|
||||
namespace app\common\utils;
|
||||
|
||||
use support\Db;
|
||||
|
||||
class MigrationResources
|
||||
{
|
||||
/**
|
||||
* 获取完整的资源树结构(不区分系统)
|
||||
*
|
||||
* @param array $conditions 筛选条件
|
||||
* @return array
|
||||
*/
|
||||
public function getFullResourceTree($table = 'resources', array $conditions = []): array
|
||||
{
|
||||
// 查询所有资源数据
|
||||
$query = Db::table($table)->where($conditions);
|
||||
|
||||
// 按排序字段排序
|
||||
$resources = $query->orderBy('sort')->orderBy('id')->get();
|
||||
// var_dump($resources->count());
|
||||
// 转换为数组
|
||||
$resourceList = [];
|
||||
foreach ($resources as $resource) {
|
||||
$resourceList[] = (array) $resource;
|
||||
}
|
||||
|
||||
// 构建树状结构
|
||||
return $this->buildTree($resourceList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建树状结构
|
||||
*
|
||||
* @param array $resources 资源列表
|
||||
* @param int $parentId 父级ID
|
||||
* @return array
|
||||
*/
|
||||
private function buildTree(array $resources, int $parentId = 0): array
|
||||
{
|
||||
$tree = [];
|
||||
|
||||
foreach ($resources as $resource) {
|
||||
if ($resource['parent_id'] == $parentId) {
|
||||
// 递归获取子节点
|
||||
$children = $this->buildTree($resources, $resource['id']);
|
||||
|
||||
// 如果有子节点,则添加到children字段
|
||||
if (!empty($children)) {
|
||||
$resource['children'] = $children;
|
||||
}
|
||||
|
||||
$tree[] = $resource;
|
||||
}
|
||||
}
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用引用方式构建树(性能优化版)
|
||||
*
|
||||
* @param array $conditions 筛选条件
|
||||
* @return array
|
||||
*/
|
||||
public function getFullResourceTreeOptimized($table = 'resources', array $conditions = []): array
|
||||
{
|
||||
// 查询所有资源数据
|
||||
$query = Db::table($table)->where($conditions);
|
||||
|
||||
// 按排序字段排序
|
||||
$resources = $query->orderBy('sort')->orderBy('id')->get();
|
||||
|
||||
// 构建ID映射和引用关系
|
||||
$resourceMap = [];
|
||||
$tree = [];
|
||||
|
||||
// 初始化所有节点
|
||||
foreach ($resources as $resource) {
|
||||
$resourceArray = (array) $resource;
|
||||
$resourceMap[$resourceArray['id']] = $resourceArray;
|
||||
}
|
||||
|
||||
// 构建父子关系
|
||||
foreach ($resourceMap as $id => &$node) {
|
||||
if ($node['parent_id'] == 0) {
|
||||
// 根节点
|
||||
$tree[] = &$node;
|
||||
} else {
|
||||
// 子节点
|
||||
if (isset($resourceMap[$node['parent_id']])) {
|
||||
if (!isset($resourceMap[$node['parent_id']]['children'])) {
|
||||
$resourceMap[$node['parent_id']]['children'] = [];
|
||||
}
|
||||
$resourceMap[$node['parent_id']]['children'][] = &$node;
|
||||
} else {
|
||||
// 父节点不存在,作为根节点处理
|
||||
$tree[] = &$node;
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($node);
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取扁平化的资源列表
|
||||
*
|
||||
* @param array $conditions 筛选条件
|
||||
* @return array
|
||||
*/
|
||||
public function getResourceList($table = 'resources', array $conditions = []): array
|
||||
{
|
||||
$query = Db::table($table)->where($conditions);
|
||||
|
||||
// 按排序字段排序
|
||||
$resources = $query->orderBy('sort')->orderBy('id')->get();
|
||||
|
||||
// 转换为数组
|
||||
$resourceList = [];
|
||||
foreach ($resources as $resource) {
|
||||
$resourceList[] = (array) $resource;
|
||||
}
|
||||
|
||||
return $resourceList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 递归插入资源树
|
||||
*/
|
||||
private function insertResourceTree($table, $tree, $parentId, $keys = [])
|
||||
{
|
||||
$keys = ['system_id','name','desc','type','code','parent_id','url','icon','open_type','link',
|
||||
'is_hide','is_hide_tab','no_auth','is_home','instance','keep_alive','sort','status'];
|
||||
foreach ($tree as $item) {
|
||||
// 保存子节点
|
||||
$children = $item['children'] ?? [];
|
||||
|
||||
// 过滤字段
|
||||
$filteredItem = [];
|
||||
foreach ($item as $k => $v) {
|
||||
if (in_array($k, $keys)) {
|
||||
$filteredItem[$k] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
// 设置parent_id
|
||||
$filteredItem['parent_id'] = $parentId;
|
||||
|
||||
// 插入并获取新ID
|
||||
$newId = Db::table($table)->insertGetId($filteredItem);
|
||||
echo "插入节点: {$filteredItem['name']}, 新ID: $newId\n";
|
||||
|
||||
// 递归处理子节点
|
||||
if (!empty($children)) {
|
||||
$this->insertResourceTree($table, $children, $newId, $keys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function do(string $from = 'resources_20250916_bak', string $to = 'resources_yuanshi', array $where = [])
|
||||
{
|
||||
try {
|
||||
// 开启事务
|
||||
Db::beginTransaction();
|
||||
|
||||
// 查询迁移资源的树状结果集
|
||||
$list = $this->getFullResourceTreeOptimized($from, $where);
|
||||
|
||||
// 入库字段
|
||||
$keys = ['system_id','name','desc','type','code','parent_id','url','icon','open_type','link',
|
||||
'is_hide','is_hide_tab','no_auth','is_home','instance','keep_alive','sort','status'];
|
||||
|
||||
// 使用递归处理树结构
|
||||
$this->insertResourceTree($to, $list, 0, $keys);
|
||||
|
||||
// 提交事务
|
||||
Db::commit();
|
||||
|
||||
echo "数据插入成功";
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// 回滚事务
|
||||
Db::rollBack();
|
||||
|
||||
echo "数据插入失败: " . $e->getMessage() . " 在文件 " . $e->getFile() . " 第 " . $e->getLine() . " 行";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace app\controller;
|
||||
|
||||
use app\common\model\Resource;
|
||||
use app\common\service\SysDictService;
|
||||
use app\common\service\UploadService;
|
||||
use app\common\utils\scaffold\Generate;
|
||||
@@ -97,71 +98,58 @@ class IndexController
|
||||
}
|
||||
|
||||
|
||||
public function test2(Request $request)
|
||||
{
|
||||
$where[] = ['id', '>', 141];
|
||||
$list = (new \app\common\utils\MigrationResources())->getFullResourceTreeOptimized('resources', $where);
|
||||
|
||||
|
||||
$keys = ['system_id','name','desc','type','code','parent_id','url','icon','open_type','link',
|
||||
'is_hide','is_hide_tab','no_auth','is_home','instance','keep_alive','sort','status'];
|
||||
foreach ($list as $key=>$item) {
|
||||
|
||||
|
||||
$temp = [];
|
||||
|
||||
foreach ($item as $k=>$v) {
|
||||
|
||||
if (isset($item['children']) && count($item['children']) > 0) {
|
||||
$temp = $item['children'];
|
||||
}
|
||||
|
||||
if (!in_array($k, $keys)) {
|
||||
unset($item[$k]);
|
||||
}
|
||||
|
||||
$id = Db::table('resources')->insertGetId($item);
|
||||
|
||||
if ($temp) {
|
||||
foreach ($temp as $kc=>$vc) {
|
||||
|
||||
foreach ($vc as $kk => $vv) {
|
||||
if (!in_array($kk, $keys)) {
|
||||
unset($vc[$kk]);
|
||||
}
|
||||
}
|
||||
|
||||
$vc['parent_id'] = $id;
|
||||
|
||||
Db::table('resources')->insert($vc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function test(Request $request)
|
||||
{
|
||||
// 菜单迁移
|
||||
$where[] = ['id', '>', 141];
|
||||
$list = (new \app\common\utils\MigrationResources())->do('resources_20250916_bak', 'resources_yuanshi', $where);
|
||||
|
||||
|
||||
// $flowType = 'stock_orders';
|
||||
// $bizType = 'stock_orders';
|
||||
// $bizId = 32;
|
||||
// $formData = [];
|
||||
// $initiatorId = 74;
|
||||
|
||||
// $appro = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
// // $sp = $appro->start($flowType, $bizType, $bizId, $formData, $initiatorId);
|
||||
|
||||
// $sp = $appro->approve(11, 1, 'reject');
|
||||
// var_dump($sp);
|
||||
|
||||
// return json([
|
||||
// 'code' => 0,
|
||||
// 'msg' => 'success'
|
||||
// ]);
|
||||
|
||||
// $param = [
|
||||
// 'path' => public_path().'/pdf/BH20250908014.pdf',
|
||||
// 'scene' => 'stock_orders',
|
||||
// 'category' => 'pdf',
|
||||
// 'original_name' => 'BH20250908014.pdf'
|
||||
// ];
|
||||
|
||||
// $up = new UploadService();
|
||||
// $res = $up->uploadLocalFile($param);
|
||||
// var_dump($res);
|
||||
$param = $request->all();
|
||||
try {
|
||||
// $service = new \app\crm\service\StockOrdersService();
|
||||
// $result = $service->createdPdfFile($param['id']);
|
||||
// return json($result);
|
||||
|
||||
// \Webman\RedisQueue\Redis::send('stock-order-pdf', ['id'=>82]);
|
||||
|
||||
|
||||
// $approvalType = array_flip(\app\common\service\SysDictService::pluckKeyValueByCode('approval_type')->toArray());
|
||||
// $result = $approvalType['stock_orders'];
|
||||
// var_dump($result);
|
||||
|
||||
|
||||
// $list = \app\common\model\User::where('status', 1)->where('email', '!=', '')->pluck('email')->toArray();
|
||||
|
||||
// var_dump($list);
|
||||
|
||||
$approvalType = array_flip(\app\common\service\SysDictService::pluckKeyValueByCode('approval_type')->toArray());
|
||||
|
||||
$title = "【审批】备货单:BH20250911001";
|
||||
$flowType = $approvalType['stock_orders'];
|
||||
$content = "【审批】你有一条审批 待审批:类型 {$flowType},业务单号 BH20250911001";
|
||||
|
||||
noticePush([
|
||||
'title' => $title,
|
||||
'content' => $content,
|
||||
'recipient' => [1,74,78],
|
||||
'email' => 1
|
||||
]);
|
||||
} catch (\Throwable $th) {
|
||||
//throw $th;
|
||||
var_dump($th->getMessage().$th->getLine().$th->getFile());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user