|  | @ -23,11 +23,14 @@ class ResourceService extends Service | 
		
	
		
			
				|  |  |             $resource = Resource::create($param); |  |  |             $resource = Resource::create($param); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             //存在授权文件 插入素材表
 |  |  |             //存在授权文件 插入素材表
 | 
		
	
		
			
				|  |  |             if ($param['copyright_type'] == 2 && isset($param['accredit_files'])) { |  |  |  | 
		
	
		
			
				|  |  |  |  |  |             if (isset($param['copyright_type']) && $param['copyright_type'] == 2 && isset($param['accredit_files'])) { | 
		
	
		
			
				|  |  |                 (new FileService())->addAccreditFile($resource, $param['accredit_files']); |  |  |                 (new FileService())->addAccreditFile($resource, $param['accredit_files']); | 
		
	
		
			
				|  |  |             } |  |  |             } | 
		
	
		
			
				|  |  |             //插入素材表
 |  |  |             //插入素材表
 | 
		
	
		
			
				|  |  |             (new FileService())->addFile($resource, $param['files'], $param['file_input_mode']); |  |  |  | 
		
	
		
			
				|  |  |  |  |  |             if (isset($param['files']) && isset($param['file_input_mode'])) { | 
		
	
		
			
				|  |  |  |  |  |                 (new FileService())->addFile($resource, $param['files'], $param['file_input_mode']); | 
		
	
		
			
				|  |  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |             //添加资源的后置操作
 |  |  |             //添加资源的后置操作
 | 
		
	
		
			
				|  |  |             $this->afterAddResource($resource); |  |  |             $this->afterAddResource($resource); | 
		
	
		
			
				|  |  |         } catch (\Exception $e) { |  |  |         } catch (\Exception $e) { | 
		
	
	
		
			
				|  | @ -49,14 +52,16 @@ class ResourceService extends Service | 
		
	
		
			
				|  |  |         $resource->file_size = $file_size_and_count['file_size']; |  |  |         $resource->file_size = $file_size_and_count['file_size']; | 
		
	
		
			
				|  |  |         $resource->file_count = $file_size_and_count['file_count']; |  |  |         $resource->file_count = $file_size_and_count['file_count']; | 
		
	
		
			
				|  |  |         $resource->audit_status = (new ProcessService())->getAuditStatusByProcess('resource_upload'); |  |  |         $resource->audit_status = (new ProcessService())->getAuditStatusByProcess('resource_upload'); | 
		
	
		
			
				|  |  |         $resource->save(); |  |  |  | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |         //如果资源状态是待审核 添加审核记录
 |  |  |         //如果资源状态是待审核 添加审核记录
 | 
		
	
		
			
				|  |  |         if ($resource->audit_status == 1) { |  |  |         if ($resource->audit_status == 1) { | 
		
	
		
			
				|  |  |             (new AuditResourceService())->addOneAudit($resource, 1); |  |  |             (new AuditResourceService())->addOneAudit($resource, 1); | 
		
	
		
			
				|  |  |         } |  |  |         } | 
		
	
		
			
				|  |  | 
 |  |  |  | 
		
	
		
			
				|  |  | 
 |  |  |  | 
		
	
		
			
				|  |  |  |  |  |         if ($resource->type == 2) { | 
		
	
		
			
				|  |  |  |  |  |             $resource->id_path = $this->getResourceDirIdPath($resource->id); | 
		
	
		
			
				|  |  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |  |         $resource->save(); | 
		
	
		
			
				|  |  |     } |  |  |     } | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     /** |  |  |     /** | 
		
	
	
		
			
				|  | @ -75,14 +80,17 @@ class ResourceService extends Service | 
		
	
		
			
				|  |  |             } |  |  |             } | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             //1 更新原文件夹 以及所有父文件夹 大小和数量
 |  |  |             //1 更新原文件夹 以及所有父文件夹 大小和数量
 | 
		
	
		
			
				|  |  |             (new ResourceDirService())->changeDirSizeAndCount($resource, $resource->parent_id, '-'); |  |  |  | 
		
	
		
			
				|  |  |  |  |  |             $this->changeDirSizeAndCount($resource, $resource->parent_id, '-'); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             //2 更新 新的文件夹 以及所有父文件夹 大小和数量
 |  |  |             //2 更新 新的文件夹 以及所有父文件夹 大小和数量
 | 
		
	
		
			
				|  |  |             (new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+'); |  |  |  | 
		
	
		
			
				|  |  |  |  |  |             $this->changeDirSizeAndCount($resource, $to_id, '+'); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             //3 更新parent_id
 |  |  |             //3 更新parent_id
 | 
		
	
		
			
				|  |  |             $resource->parent_id = $to_id; |  |  |             $resource->parent_id = $to_id; | 
		
	
		
			
				|  |  |             $resource->save(); |  |  |             $resource->save(); | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |             //4 更新id path
 | 
		
	
		
			
				|  |  |  |  |  |             $this->updateChildParentIds($resource_id); | 
		
	
		
			
				|  |  |         } |  |  |         } | 
		
	
		
			
				|  |  |         return dataReturn($this->sucCode, $this->moveMsg); |  |  |         return dataReturn($this->sucCode, $this->moveMsg); | 
		
	
		
			
				|  |  |     } |  |  |     } | 
		
	
	
		
			
				|  | @ -96,17 +104,96 @@ class ResourceService extends Service | 
		
	
		
			
				|  |  |      */ |  |  |      */ | 
		
	
		
			
				|  |  |     public function copyTo($ids, $to_id) |  |  |     public function copyTo($ids, $to_id) | 
		
	
		
			
				|  |  |     { |  |  |     { | 
		
	
		
			
				|  |  |  |  |  |         // 更新 新的文件夹资源 以及所有父文件夹资源 大小和数量
 | 
		
	
		
			
				|  |  |  |  |  |         foreach ($ids as $resource_id) { | 
		
	
		
			
				|  |  |  |  |  |             $resource = Resource::find($resource_id); | 
		
	
		
			
				|  |  |  |  |  |             $this->changeDirSizeAndCount($resource, $to_id, '+'); | 
		
	
		
			
				|  |  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |         //递归复制
 | 
		
	
		
			
				|  |  |  |  |  |         $this->copyResrouceRecursively($ids, $to_id); | 
		
	
		
			
				|  |  |  |  |  |         return dataReturn($this->sucCode, $this->copyMsg); | 
		
	
		
			
				|  |  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |     public function copyResrouceRecursively($ids, $to_id) | 
		
	
		
			
				|  |  |  |  |  |     { | 
		
	
		
			
				|  |  |         foreach ($ids as $resource_id) { |  |  |         foreach ($ids as $resource_id) { | 
		
	
		
			
				|  |  |             //复制需要 需要复制里面所有的素材
 |  |  |  | 
		
	
		
			
				|  |  |  |  |  |             //获取源文件 和子文件
 | 
		
	
		
			
				|  |  |             $resource = Resource::with(['files', 'accredit_files'])->find($resource_id); |  |  |             $resource = Resource::with(['files', 'accredit_files'])->find($resource_id); | 
		
	
		
			
				|  |  |  |  |  |             if ($resource->type == 1) { | 
		
	
		
			
				|  |  |  |  |  |                 //说明要复制的是个文件资源 直接复制
 | 
		
	
		
			
				|  |  |  |  |  |                 $this->copyResource($resource->toArray(), $to_id); | 
		
	
		
			
				|  |  |  |  |  |             } elseif ($resource->type == 2) { | 
		
	
		
			
				|  |  |  |  |  |                 $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->copyResrouceRecursively([$child['id']], $new_resource_dir_id); | 
		
	
		
			
				|  |  |  |  |  |                         } else { | 
		
	
		
			
				|  |  |  |  |  |                             // 复制文件
 | 
		
	
		
			
				|  |  |  |  |  |                             $this->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); | 
		
	
		
			
				|  |  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |     /** | 
		
	
		
			
				|  |  |  |  |  |      * @param int $folderId 要更新的文件夹 ID | 
		
	
		
			
				|  |  |  |  |  |      * @param int $newParentId 新的父文件夹 ID | 
		
	
		
			
				|  |  |  |  |  |      * @return void | 
		
	
		
			
				|  |  |  |  |  |      * 递归更新文件夹的 id_path | 
		
	
		
			
				|  |  |  |  |  |      */ | 
		
	
		
			
				|  |  |  |  |  |     public function updateChildParentIds($resource_id) | 
		
	
		
			
				|  |  |  |  |  |     { | 
		
	
		
			
				|  |  |  |  |  |         // 更新当前文件夹的 parent_id id_path
 | 
		
	
		
			
				|  |  |  |  |  |         $id_path = $this->getResourceDirIdPath($resource_id); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             //1.创建一个新普通资源
 |  |  |  | 
		
	
		
			
				|  |  |             $this->copyResource($resource->toArray(), $to_id); |  |  |  | 
		
	
		
			
				|  |  |  |  |  |         Resource::where('id', $resource_id)->update(['id_path' => $id_path]); | 
		
	
		
			
				|  |  |  |  |  |         // 获取所有子文件和子文件夹
 | 
		
	
		
			
				|  |  |  |  |  |         $children = Resource::where('parent_id', $resource_id)->select(); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             //2 更新 新的文件夹资源 以及所有父文件夹资源 大小和数量
 |  |  |  | 
		
	
		
			
				|  |  |             (new ResourceDirService())->changeDirSizeAndCount($resource, $to_id, '+'); |  |  |  | 
		
	
		
			
				|  |  |  |  |  |         foreach ($children as $child) { | 
		
	
		
			
				|  |  |  |  |  |             // 递归更新子文件夹的 parent_id
 | 
		
	
		
			
				|  |  |  |  |  |             if ($child['type'] == '2') { | 
		
	
		
			
				|  |  |  |  |  |                 $this->updateChildParentIds($child['id']); | 
		
	
		
			
				|  |  |  |  |  |             } | 
		
	
		
			
				|  |  |         } |  |  |         } | 
		
	
		
			
				|  |  |         return dataReturn($this->sucCode, $this->copyMsg); |  |  |  | 
		
	
		
			
				|  |  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |     /** | 
		
	
		
			
				|  |  |  |  |  |      * @param $resource_dir_id 文件夹id | 
		
	
		
			
				|  |  |  |  |  |      * 获取文件夹 id_path(从顶级文件夹到自己的路径) | 
		
	
		
			
				|  |  |  |  |  |      */ | 
		
	
		
			
				|  |  |  |  |  |     public function getResourceDirIdPath($resource_dir_id) | 
		
	
		
			
				|  |  |  |  |  |     { | 
		
	
		
			
				|  |  |  |  |  |         $path = []; | 
		
	
		
			
				|  |  |  |  |  |         // 调用递归函数
 | 
		
	
		
			
				|  |  |  |  |  |         findParent(new Resource(), $resource_dir_id, $path); | 
		
	
		
			
				|  |  |  |  |  |         // 反转路径数组,使顶级文件夹在前
 | 
		
	
		
			
				|  |  |  |  |  |         return implode(',', array_reverse($path)); | 
		
	
		
			
				|  |  |     } |  |  |     } | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     /** |  |  |     /** | 
		
	
	
		
			
				|  | @ -250,4 +337,35 @@ class ResourceService extends Service | 
		
	
		
			
				|  |  |         } |  |  |         } | 
		
	
		
			
				|  |  |         return $url; |  |  |         return $url; | 
		
	
		
			
				|  |  |     } |  |  |     } | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |     /** | 
		
	
		
			
				|  |  |  |  |  |      * @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 = $this->getResourceSizeAndCount($resource, true); | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |         //目标文件夹
 | 
		
	
		
			
				|  |  |  |  |  |         $target_resource_dir = Resource::find($target_resource_dir_id); | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |         if ($method === '+') { | 
		
	
		
			
				|  |  |  |  |  |             Resource::whereIn('id', $target_resource_dir['id_path'])->inc('resource_size', $resource_info['size']) | 
		
	
		
			
				|  |  |  |  |  |                 ->inc('resource_count', $resource_info['count']) | 
		
	
		
			
				|  |  |  |  |  |                 ->update(); | 
		
	
		
			
				|  |  |  |  |  |         } elseif ($method === '-') { | 
		
	
		
			
				|  |  |  |  |  |             Resource::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); | 
		
	
		
			
				|  |  |  |  |  |     } | 
		
	
		
			
				|  |  | } |  |  | } |