|
|
|
@ -8,209 +8,210 @@ use app\model\ResourceDir; |
|
|
|
|
|
|
|
class ResourceDirService extends Service |
|
|
|
{ |
|
|
|
/** |
|
|
|
* @param $param 前端提交的参数 |
|
|
|
* @return array |
|
|
|
* 添加文件夹 |
|
|
|
*/ |
|
|
|
public function addResourceDir($param): array |
|
|
|
{ |
|
|
|
try { |
|
|
|
$resource_dir = ResourceDir::create($param); |
|
|
|
//更新id_path
|
|
|
|
$resource_dir->id_path = $this->getResourceDirIdPath($resource_dir->id); |
|
|
|
$resource_dir->save(); |
|
|
|
} catch (\Exception $e) { |
|
|
|
throw new ModelException($e->getMessage()); |
|
|
|
/** |
|
|
|
* @param $param 前端提交的参数 |
|
|
|
* @return array |
|
|
|
* 添加文件夹 |
|
|
|
*/ |
|
|
|
public function addResourceDir($param): array |
|
|
|
{ |
|
|
|
try { |
|
|
|
$resource_dir = ResourceDir::create($param); |
|
|
|
//更新id_path
|
|
|
|
$resource_dir->id_path = $this->getResourceDirIdPath($resource_dir->id); |
|
|
|
$resource_dir->save(); |
|
|
|
} catch (\Exception $e) { |
|
|
|
throw new ModelException($e->getMessage()); |
|
|
|
} |
|
|
|
return dataReturn($this->sucCode, $this->addMsg, $resource_dir->id); |
|
|
|
} |
|
|
|
return dataReturn($this->sucCode, $this->addMsg, $resource_dir->id); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $resource_dir_id 文件夹id |
|
|
|
* @return array |
|
|
|
* 获取一个文件夹下的所有文件夹 和资源 |
|
|
|
*/ |
|
|
|
public function getResourceDirAndResource($resource_dir_id) |
|
|
|
{ |
|
|
|
$resource_list = ResourceDir::where('parent_id', $resource_dir_id) |
|
|
|
->order('type', 'desc') //文件夹放在上面
|
|
|
|
->order('create_time', 'desc') |
|
|
|
->select(); |
|
|
|
return $resource_list; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $resource 资源对象 |
|
|
|
* @param $resource_dir_id 移入/移出的资源文件夹id |
|
|
|
* @param $resource_dir_id method + 移入 - 移除 |
|
|
|
* @return array |
|
|
|
* 将一资源(普通或者文件夹资源均可)移入或者移出文件夹 文件夹修改资源数量 和 资源大小 |
|
|
|
*/ |
|
|
|
public function changeDirSizeAndCount($resource, $target_resource_dir_id, $method = '+') |
|
|
|
{ |
|
|
|
//资源还不属于文件夹
|
|
|
|
if ($target_resource_dir_id == 0) { |
|
|
|
return true; |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $resource_dir_id 文件夹id |
|
|
|
* @return array |
|
|
|
* 获取一个文件夹下的所有文件夹 和资源 |
|
|
|
*/ |
|
|
|
public function getResourceDirAndResource($resource_dir_id) |
|
|
|
{ |
|
|
|
$resource_list = ResourceDir::where('parent_id', $resource_dir_id) |
|
|
|
->order('type', 'desc') //文件夹放在上面
|
|
|
|
->order('create_time', 'desc') |
|
|
|
->select(); |
|
|
|
return $resource_list; |
|
|
|
} |
|
|
|
//获取资源的大小
|
|
|
|
$resource_info = (new ResourceService())->getResourceSizeAndCount($resource, true); |
|
|
|
|
|
|
|
//目标文件夹
|
|
|
|
$target_resource_dir = ResourceDir::find($target_resource_dir_id); |
|
|
|
|
|
|
|
if ($method === '+') { |
|
|
|
ResourceDir::whereIn('id', $target_resource_dir['id_path'])->inc('resource_size', $resource_info['size']) |
|
|
|
->inc('resource_count', $resource_info['count']) |
|
|
|
->update(); |
|
|
|
} elseif ($method === '-') { |
|
|
|
ResourceDir::whereIn('id', $target_resource_dir['id_path'])->dec('resource_size', $resource_info['size']) |
|
|
|
->dec('resource_count', $resource_info['count']) |
|
|
|
->update(); |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $resource 资源对象 |
|
|
|
* @param $resource_dir_id 移入/移出的资源文件夹id |
|
|
|
* @param $resource_dir_id method + 移入 - 移除 |
|
|
|
* @return array |
|
|
|
* 将一资源(普通或者文件夹资源均可)移入或者移出文件夹 文件夹修改资源数量 和 资源大小 |
|
|
|
*/ |
|
|
|
public function changeDirSizeAndCount($resource, $target_resource_dir_id, $method = '+') |
|
|
|
{ |
|
|
|
//资源还不属于文件夹
|
|
|
|
if ($target_resource_dir_id == 0) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
//获取资源的大小
|
|
|
|
$resource_info = (new ResourceService())->getResourceSizeAndCount($resource, true); |
|
|
|
|
|
|
|
//目标文件夹
|
|
|
|
$target_resource_dir = ResourceDir::find($target_resource_dir_id); |
|
|
|
|
|
|
|
if ($method === '+') { |
|
|
|
ResourceDir::whereIn('id', $target_resource_dir['id_path'])->inc('resource_size', $resource_info['size']) |
|
|
|
->inc('resource_count', $resource_info['count']) |
|
|
|
->update(); |
|
|
|
} elseif ($method === '-') { |
|
|
|
ResourceDir::whereIn('id', $target_resource_dir['id_path'])->dec('resource_size', $resource_info['size']) |
|
|
|
->dec('resource_count', $resource_info['count']) |
|
|
|
->update(); |
|
|
|
} |
|
|
|
return dataReturn($this->sucCode, $this->addMsg); |
|
|
|
} |
|
|
|
return dataReturn($this->sucCode, $this->addMsg); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $resource_dir_id 文件夹id |
|
|
|
* 获取文件夹 id_path(从顶级文件夹到自己的路径) |
|
|
|
*/ |
|
|
|
public function getResourceDirIdPath($resource_dir_id) |
|
|
|
{ |
|
|
|
$path = []; |
|
|
|
// 调用递归函数
|
|
|
|
findParent(new ResourceDir(), $resource_dir_id, $path); |
|
|
|
// 反转路径数组,使顶级文件夹在前
|
|
|
|
return implode(',', array_reverse($path)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param $ids 需要移动的文件夹资源ids |
|
|
|
* @param $to_id 文件夹资源id |
|
|
|
* @return array |
|
|
|
* 移动操作 |
|
|
|
*/ |
|
|
|
public function moveTo($ids, $to_id) |
|
|
|
{ |
|
|
|
|
|
|
|
foreach ($ids as $resource_id) { |
|
|
|
$resource = Resource::find($resource_id); |
|
|
|
|
|
|
|
if ($resource->parent_id == $to_id) { |
|
|
|
//移动到同文件夹 无需操作
|
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
//1 更新原文件夹 以及所有父文件夹 大小和数量
|
|
|
|
(new ResourceDirService())->changeDirSizeAndCount($resource, $resource->parent_id, '-'); |
|
|
|
|
|
|
|
//2 更新 新的文件夹 以及所有父文件夹 大小和数量
|
|
|
|
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+'); |
|
|
|
|
|
|
|
//3 更新当前文件夹 parent_id
|
|
|
|
$resource->parent_id = $to_id; |
|
|
|
$resource->save(); |
|
|
|
|
|
|
|
//4 递归修改当前文件夹 子文件夹id_path
|
|
|
|
$this->updateChildParentIds($resource_id); |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $resource_dir_id 文件夹id |
|
|
|
* 获取文件夹 id_path(从顶级文件夹到自己的路径) |
|
|
|
*/ |
|
|
|
public function getResourceDirIdPath($resource_dir_id) |
|
|
|
{ |
|
|
|
$path = []; |
|
|
|
// 调用递归函数
|
|
|
|
findParent(new ResourceDir(), $resource_dir_id, $path); |
|
|
|
// 反转路径数组,使顶级文件夹在前
|
|
|
|
return implode(',', array_reverse($path)); |
|
|
|
} |
|
|
|
return dataReturn($this->sucCode, $this->moveMsg); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param $ids 需要复制的文件夹资源ids |
|
|
|
* @param $to_id 文件夹资源id |
|
|
|
* @return array |
|
|
|
* 复制操作 |
|
|
|
*/ |
|
|
|
public function copyTo($ids, $to_id) |
|
|
|
{ |
|
|
|
|
|
|
|
// 更新 新的文件夹资源 以及所有父文件夹资源 大小和数量
|
|
|
|
foreach ($ids as $resource_id) { |
|
|
|
$resource = Resource::find($resource_id); |
|
|
|
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+'); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param $ids 需要移动的文件夹资源ids |
|
|
|
* @param $to_id 文件夹资源id |
|
|
|
* @return array |
|
|
|
* 移动操作 |
|
|
|
*/ |
|
|
|
public function moveTo($ids, $to_id) |
|
|
|
{ |
|
|
|
|
|
|
|
foreach ($ids as $resource_id) { |
|
|
|
$resource = Resource::find($resource_id); |
|
|
|
|
|
|
|
if ($resource->parent_id == $to_id) { |
|
|
|
//移动到同文件夹 无需操作
|
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
//1 更新原文件夹 以及所有父文件夹 大小和数量
|
|
|
|
(new ResourceDirService())->changeDirSizeAndCount($resource, $resource->parent_id, '-'); |
|
|
|
|
|
|
|
//2 更新 新的文件夹 以及所有父文件夹 大小和数量
|
|
|
|
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+'); |
|
|
|
|
|
|
|
//3 更新当前文件夹 parent_id
|
|
|
|
$resource->parent_id = $to_id; |
|
|
|
$resource->save(); |
|
|
|
|
|
|
|
//4 递归修改当前文件夹 子文件夹id_path
|
|
|
|
$this->updateChildParentIds($resource_id); |
|
|
|
} |
|
|
|
return dataReturn($this->sucCode, $this->moveMsg); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//递归复制
|
|
|
|
$this->copyFolderRecursively($ids, $to_id); |
|
|
|
return dataReturn($this->sucCode, $this->copyMsg); |
|
|
|
} |
|
|
|
/** |
|
|
|
* @param $ids 需要复制的普通资源ids |
|
|
|
* @param $to_id 文件夹资源id |
|
|
|
* @return array |
|
|
|
* 递归复制资源文件夹及其内容 |
|
|
|
*/ |
|
|
|
public function copyFolderRecursively($ids, $to_id) |
|
|
|
{ |
|
|
|
|
|
|
|
foreach ($ids as $resource_id) { |
|
|
|
//获取源文件 和子文件
|
|
|
|
$resource = Resource::find($resource_id); |
|
|
|
$children = Resource::with(['files', 'accredit_files'])->where('parent_id', $resource->id)->select(); |
|
|
|
|
|
|
|
// 复制源文件夹
|
|
|
|
$new_resource_dir = $this->copyResourceDir($resource->toArray(), $to_id); |
|
|
|
$new_resource_dir_id = $new_resource_dir['data']; |
|
|
|
// 复制子文件和子文件夹
|
|
|
|
|
|
|
|
if ($children) { |
|
|
|
foreach ($children as $child) { |
|
|
|
if ($child['type'] == '2') { |
|
|
|
// 递归复制子文件夹
|
|
|
|
$this->copyFolderRecursively([$child['id']], $new_resource_dir_id); |
|
|
|
} else { |
|
|
|
// 复制文件
|
|
|
|
(new ResourceService())->copyResource($child->toArray(), $new_resource_dir_id); |
|
|
|
} |
|
|
|
/** |
|
|
|
* @param $ids 需要复制的文件夹资源ids |
|
|
|
* @param $to_id 文件夹资源id |
|
|
|
* @return array |
|
|
|
* 复制操作 |
|
|
|
*/ |
|
|
|
public function copyTo($ids, $to_id) |
|
|
|
{ |
|
|
|
|
|
|
|
// 更新 新的文件夹资源 以及所有父文件夹资源 大小和数量
|
|
|
|
foreach ($ids as $resource_id) { |
|
|
|
$resource = Resource::find($resource_id); |
|
|
|
(new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//递归复制
|
|
|
|
$this->copyFolderRecursively($ids, $to_id); |
|
|
|
return dataReturn($this->sucCode, $this->copyMsg); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $ids 需要复制的普通资源ids |
|
|
|
* @param $to_id 文件夹资源id |
|
|
|
* @return array |
|
|
|
* 递归复制资源文件夹及其内容 |
|
|
|
*/ |
|
|
|
public function copyFolderRecursively($ids, $to_id) |
|
|
|
{ |
|
|
|
|
|
|
|
foreach ($ids as $resource_id) { |
|
|
|
//获取源文件 和子文件
|
|
|
|
$resource = Resource::find($resource_id); |
|
|
|
$children = Resource::with(['files', 'accredit_files'])->where('parent_id', $resource->id)->select(); |
|
|
|
|
|
|
|
// 复制源文件夹
|
|
|
|
$new_resource_dir = $this->copyResourceDir($resource->toArray(), $to_id); |
|
|
|
$new_resource_dir_id = $new_resource_dir['data']; |
|
|
|
// 复制子文件和子文件夹
|
|
|
|
|
|
|
|
if ($children) { |
|
|
|
foreach ($children as $child) { |
|
|
|
if ($child['type'] == '2') { |
|
|
|
// 递归复制子文件夹
|
|
|
|
$this->copyFolderRecursively([$child['id']], $new_resource_dir_id); |
|
|
|
} else { |
|
|
|
// 复制文件
|
|
|
|
(new ResourceService())->copyResource($child->toArray(), $new_resource_dir_id); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param $resource 资源数组(因为前台创建是数组 保持一致) |
|
|
|
* @param $to_id 复制到的文件夹id |
|
|
|
* @return array |
|
|
|
* 复制一个文件夹资源数据 |
|
|
|
*/ |
|
|
|
public function copyResourceDir($resource, $to_id) |
|
|
|
{ |
|
|
|
unset($resource['id']); |
|
|
|
$resource['create_time'] = date('Y-m-d H:i:s'); |
|
|
|
$resource['update_time'] = date('Y-m-d H:i:s'); |
|
|
|
$resource['parent_id'] = $to_id; |
|
|
|
|
|
|
|
//重新创建资源
|
|
|
|
return $this->addResourceDir($resource); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 递归更新文件夹的 id_path |
|
|
|
* |
|
|
|
* @param int $folderId 要更新的文件夹 ID |
|
|
|
* @param int $newParentId 新的父文件夹 ID |
|
|
|
* @return void |
|
|
|
*/ |
|
|
|
protected function updateChildParentIds($resource_id) |
|
|
|
{ |
|
|
|
// 更新当前文件夹的 parent_id id_path
|
|
|
|
$id_path = $this->getResourceDirIdPath($resource_id); |
|
|
|
|
|
|
|
ResourceDir::where('id', $resource_id)->update(['id_path' => $id_path]); |
|
|
|
// 获取所有子文件和子文件夹
|
|
|
|
$children = ResourceDir::where('parent_id', $resource_id)->select(); |
|
|
|
|
|
|
|
foreach ($children as $child) { |
|
|
|
// 递归更新子文件夹的 parent_id
|
|
|
|
if ($child['type'] == '2') { |
|
|
|
$this->updateChildParentIds($child['id']); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param $resource 资源数组(因为前台创建是数组 保持一致) |
|
|
|
* @param $to_id 复制到的文件夹id |
|
|
|
* @return array |
|
|
|
* 复制一个文件夹资源数据 |
|
|
|
*/ |
|
|
|
public function copyResourceDir($resource, $to_id) |
|
|
|
{ |
|
|
|
unset($resource['id']); |
|
|
|
$resource['create_time'] = date('Y-m-d H:i:s'); |
|
|
|
$resource['update_time'] = date('Y-m-d H:i:s'); |
|
|
|
$resource['parent_id'] = $to_id; |
|
|
|
|
|
|
|
//重新创建资源
|
|
|
|
return $this->addResourceDir($resource); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 递归更新文件夹的 id_path |
|
|
|
* |
|
|
|
* @param int $folderId 要更新的文件夹 ID |
|
|
|
* @param int $newParentId 新的父文件夹 ID |
|
|
|
* @return void |
|
|
|
*/ |
|
|
|
protected function updateChildParentIds($resource_id) |
|
|
|
{ |
|
|
|
// 更新当前文件夹的 parent_id id_path
|
|
|
|
$id_path = $this->getResourceDirIdPath($resource_id); |
|
|
|
|
|
|
|
ResourceDir::where('id', $resource_id)->update(['id_path' => $id_path]); |
|
|
|
// 获取所有子文件和子文件夹
|
|
|
|
$children = ResourceDir::where('parent_id', $resource_id)->select(); |
|
|
|
|
|
|
|
foreach ($children as $child) { |
|
|
|
// 递归更新子文件夹的 parent_id
|
|
|
|
if ($child['type'] == '2') { |
|
|
|
$this->updateChildParentIds($child['id']); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |