diff --git a/app/controller/ResourceController.php b/app/controller/ResourceController.php index 1e8feca..83e0408 100644 --- a/app/controller/ResourceController.php +++ b/app/controller/ResourceController.php @@ -142,8 +142,19 @@ class ResourceController /** * 资源 复制到 */ - public function copyTo() + public function copyTo(Resource $resource, Request $request) { + Db::startTrans(); + try { + // 复制资源 + $res = $resource->copyTo($request->param('ids'), $request->param('to_id')); + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + return jsonReturn(-5, $e->getMessage()); + } + + return json($res); } /** diff --git a/app/model/File.php b/app/model/File.php index 9fbd1a5..4bd3c32 100644 --- a/app/model/File.php +++ b/app/model/File.php @@ -6,21 +6,38 @@ use app\exception\ModelException; class File extends Model { + /** * @param $resource 资源对象 - * @param $files 素材数组 - * @param $file_input_mode 素材录入方式 - * 添加资源 素材关联表 + * @param $accredit_files 授权素材数组 + * 添加 授权素材 + */ + public function addAccreditFile($resource, $accredit_files) + { + foreach ($accredit_files as $accredit_file) { + $accredit_file['type'] = 2; + $accredit_file_array[] = $accredit_file; + } + // 资源素材关联 系统会自动插入 + $resource->accreditFiles()->saveAll($accredit_file_array); + } + + /** + * @param $resource 资源对象 + * @param $files 素材 + * 添加 素材 */ public function addFile($resource, $files, $file_input_mode) { foreach ($files as $file) { + $file['type'] = 1; $file['file_input_mode'] = $file_input_mode; $file_array[] = $file; } // 资源素材关联 系统会自动插入 $resource->files()->saveAll($file_array); } + /** * @param $files 素材数组 * 获得一组素材的大小 diff --git a/app/model/Model.php b/app/model/Model.php index b090112..42a10dd 100644 --- a/app/model/Model.php +++ b/app/model/Model.php @@ -18,6 +18,7 @@ class Model extends BaseModel { // msg public $moveMsg; + public $copyMsg; public $getMsg; public $addMsg; public $updateMsg; @@ -50,6 +51,7 @@ class Model extends BaseModel $this->listCacheKey = $this->className . '_cache_list'; $this->detailCacheKey = $this->className . '_cache_detail'; $this->moveMsg = lang('移动成功'); + $this->copyMsg = lang('复制成功'); $this->getMsg = lang('获取成功'); $this->addMsg = lang('添加成功'); $this->updateMsg = lang('修改成功'); diff --git a/app/model/Resource.php b/app/model/Resource.php index d61976d..9c71870 100644 --- a/app/model/Resource.php +++ b/app/model/Resource.php @@ -12,22 +12,32 @@ class Resource extends Model } /** - * 资源 素材 多对多 + * 资源 普通素材 一对多 */ public function files() { - return $this->belongsToMany(File::class, ResourceFile::class, 'file_id', 'resource_id'); + return $this->hasMany(File::class, 'resource_id', 'id')->where('type', 1); } /** - * 资源 授权文件 一对多 + * 资源 授权素材 一对多 */ public function accreditFiles() { - return $this->hasMany(AccreditFile::class, 'resource_id', 'id'); + return $this->hasMany(File::class, 'resource_id', 'id')->where('type', 2); } /** + * 资源 所有素材 + */ + public function allFiles() + { + return $this->hasMany(File::class, 'resource_id', 'id'); + } + + + + /** * @param $param 前端提交的参数 * @return array * 添加资源 @@ -37,9 +47,10 @@ class Resource extends Model try { //插入资源表 $resource = self::create($param); + //存在授权文件 插入授权文件表 if ($param['copyright_type'] == 2 && isset($param['accredit_files'])) { - $resource->accreditFiles()->saveAll($param['accredit_files']); + (new File())->addAccreditFile($resource, $param['accredit_files']); } //插入素材表 (new File())->addFile($resource, $param['files'], $param['file_input_mode']); @@ -49,6 +60,7 @@ class Resource extends Model } catch (\Exception $e) { throw new ModelException($e->getMessage()); } + return dataReturn($this->sucCode, $this->addMsg, $resource->id); } @@ -81,6 +93,7 @@ class Resource extends Model //移动到同文件夹 无需操作 continue; } + //1 更新原文件夹 以及所有父文件夹 大小和数量 (new ResourceDir())->changeDirSizeAndCount($resource, $resource->parent_id, '-'); @@ -93,6 +106,62 @@ class Resource extends Model } 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 = $this::with(['files', 'accredit_files'])->find($resource_id); + + //1.创建一个新资源 + $this->copyResource($resource->toArray(), $to_id); + + //2 更新 新的文件夹 以及所有父文件夹 大小和数量 + (new ResourceDir())->changeDirSizeAndCount($resource, $to_id, '+'); + } + return dataReturn($this->sucCode, $this->copyMsg); + } + + /** + * @param $resource 资源数组(因为前台创建是数组 保持一致) + * @param $to_id 复制到的文件夹id + * @return array + * 复制一个资源数据 + */ + public function copyResource($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; + + //关联表去掉关联字段 + if (isset($resource['files'])) { + foreach ($resource['files'] as $key => $file) { + unset($resource['files'][$key]['id']); + unset($resource['files'][$key]['resource_id']); //去掉原来的关联关系 + + } + } + + if (isset($resource['accredit_files'])) { + foreach ($resource['accredit_files'] as $key => $file) { + unset($resource['accredit_files'][$key]['id']); + unset($resource['accredit_files'][$key]['resource_id']); //去掉原来的关联关系 + } + } + + //重新创建资源 + return $this->addResource($resource); + } /** * @param $resource 资源对象 * @return array @@ -103,7 +172,9 @@ class Resource extends Model $size = 0; $count = 0; foreach ($resource->files as $file) { - $size += $file['size']; + if (isset($file['size'])) { + $size += $file['size']; + } $count++; } return [ diff --git a/app/model/ResourceDir.php b/app/model/ResourceDir.php index a171f54..cbed422 100644 --- a/app/model/ResourceDir.php +++ b/app/model/ResourceDir.php @@ -43,7 +43,7 @@ class ResourceDir extends Model } /** - * @param $resource 资源 + * @param $resource 资源对象 * @param $resource_dir_id 移入/移出的文件夹id * @param $resource_dir_id method + 移入 - 移除 * @return array @@ -51,6 +51,11 @@ class ResourceDir extends Model */ public function changeDirSizeAndCount($resource, $resource_dir_id, $method = '+') { + + //资源还不属于文件夹 + if ($resource_dir_id == 0) { + return true; + } $resource_dir = $this::find($resource_dir_id); if ($method === '+') { $this::whereIn('id', $resource_dir['id_path'])->inc('resource_size', $resource->file_size) @@ -61,6 +66,7 @@ class ResourceDir extends Model ->dec('resource_count', 1) ->update(); } + return dataReturn($this->sucCode, $this->addMsg); } /** diff --git a/route/app.php b/route/app.php index b471a3c..3d7d5f2 100644 --- a/route/app.php +++ b/route/app.php @@ -23,6 +23,7 @@ Route::group('resource', function () { Route::get('/share/:id', 'share'); Route::post('/rename', 'rename'); Route::post('/move_to', 'moveTo'); + Route::post('/copy_to', 'copyTo'); })->prefix('resource/'); Route::group('resourceDir', function () {