|
|
@ -43,26 +43,30 @@ class ResourceDirService extends Service |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param $resource 资源对象 |
|
|
* @param $resource 资源对象 |
|
|
* @param $resource_dir_id 移入/移出的文件夹id |
|
|
|
|
|
|
|
|
* @param $resource_dir_id 移入/移出的资源文件夹id |
|
|
* @param $resource_dir_id method + 移入 - 移除 |
|
|
* @param $resource_dir_id method + 移入 - 移除 |
|
|
* @return array |
|
|
* @return array |
|
|
* 将一普通资源移入或者移出文件夹 文件夹修改资源数量 和 资源大小 |
|
|
|
|
|
|
|
|
* 将一资源(普通或者文件夹资源均可)移入或者移出文件夹 文件夹修改资源数量 和 资源大小 |
|
|
*/ |
|
|
*/ |
|
|
public function changeDirSizeAndCount($resource, $resource_dir_id, $method = '+') |
|
|
|
|
|
|
|
|
public function changeDirSizeAndCount($resource, $target_resource_dir_id, $method = '+') |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
//资源还不属于文件夹
|
|
|
//资源还不属于文件夹
|
|
|
if ($resource_dir_id == 0) { |
|
|
|
|
|
|
|
|
if ($target_resource_dir_id == 0) { |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
$resource_dir = ResourceDir::find($resource_dir_id); |
|
|
|
|
|
|
|
|
//获取资源的大小
|
|
|
|
|
|
$resource_info = (new ResourceService())->getResourceSizeAndCount($resource, true); |
|
|
|
|
|
|
|
|
|
|
|
//目标文件夹
|
|
|
|
|
|
$target_resource_dir = ResourceDir::find($target_resource_dir_id); |
|
|
|
|
|
|
|
|
if ($method === '+') { |
|
|
if ($method === '+') { |
|
|
ResourceDir::whereIn('id', $resource_dir['id_path'])->inc('resource_size', $resource->file_size) |
|
|
|
|
|
->inc('resource_count', 1) |
|
|
|
|
|
|
|
|
ResourceDir::whereIn('id', $target_resource_dir['id_path'])->inc('resource_size', $resource_info['size']) |
|
|
|
|
|
->inc('resource_count', $resource_info['count']) |
|
|
->update(); |
|
|
->update(); |
|
|
} elseif ($method === '-') { |
|
|
} elseif ($method === '-') { |
|
|
ResourceDir::whereIn('id', $resource_dir['id_path'])->dec('resource_size', $resource->file_size) |
|
|
|
|
|
->dec('resource_count', 1) |
|
|
|
|
|
|
|
|
ResourceDir::whereIn('id', $target_resource_dir['id_path'])->dec('resource_size', $resource_info['size']) |
|
|
|
|
|
->dec('resource_count', $resource_info['count']) |
|
|
->update(); |
|
|
->update(); |
|
|
} |
|
|
} |
|
|
return dataReturn($this->sucCode, $this->addMsg); |
|
|
return dataReturn($this->sucCode, $this->addMsg); |
|
|
@ -80,4 +84,63 @@ class ResourceDirService extends Service |
|
|
// 反转路径数组,使顶级文件夹在前
|
|
|
// 反转路径数组,使顶级文件夹在前
|
|
|
return implode(',', array_reverse($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); |
|
|
|
|
|
} |
|
|
|
|
|
return dataReturn($this->sucCode, $this->moveMsg); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 递归更新文件夹的 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']); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |