mirror of
https://github.com/kuaifan/dootask.git
synced 2026-02-28 21:20:31 +00:00
perf: 支持搜索共享文件
This commit is contained in:
parent
ed9b93c22f
commit
0a99ecdd9b
@ -93,8 +93,7 @@ class FileController extends AbstractController
|
||||
->join('file_users', 'files.id', '=', 'file_users.file_id')
|
||||
->where('files.userid', '!=', $user->userid)
|
||||
->where(function ($query) use ($user) {
|
||||
$query->where('file_users.userid', 0);
|
||||
$query->orWhere('file_users.userid', $user->userid);
|
||||
$query->whereIn('file_users.userid', [0, $user->userid]);
|
||||
})
|
||||
->groupBy('files.id')
|
||||
->take(100)
|
||||
@ -175,11 +174,36 @@ class FileController extends AbstractController
|
||||
if (empty($key)) {
|
||||
return Base::retError('请输入关键词');
|
||||
}
|
||||
//
|
||||
// 搜索自己的
|
||||
$builder = File::whereUserid($user->userid)->where("name", "like", "%{$key}%");
|
||||
$list = $builder->take(50)->get();
|
||||
$array = $builder->take(50)->get()->toArray();
|
||||
// 搜索共享的
|
||||
$take = 50 - count($array);
|
||||
if ($take > 0) {
|
||||
$list = File::where("name", "like", "%{$key}%")
|
||||
->whereIn('pshare', function ($queryA) use ($user) {
|
||||
$queryA->select('files.id')
|
||||
->from('files')
|
||||
->join('file_users', 'files.id', '=', 'file_users.file_id')
|
||||
->where('files.userid', '!=', $user->userid)
|
||||
->where(function ($queryB) use ($user) {
|
||||
$queryB->whereIn('file_users.userid', [0, $user->userid]);
|
||||
});
|
||||
})
|
||||
->take($take)
|
||||
->get();
|
||||
if ($list->isNotEmpty()) {
|
||||
foreach ($list as $file) {
|
||||
$temp = $file->toArray();
|
||||
if ($file->pshare === $file->id) {
|
||||
$temp['pid'] = 0;
|
||||
}
|
||||
$array[] = $temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
return Base::retSuccess('success', $list);
|
||||
return Base::retSuccess('success', $array);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -274,7 +298,7 @@ class FileController extends AbstractController
|
||||
'created_id' => $user->userid,
|
||||
]);
|
||||
$file->handleDuplicateName();
|
||||
$file->saveBeforePids();
|
||||
$file->saveBeforePP();
|
||||
//
|
||||
$data = File::find($file->id);
|
||||
$data->pushMsg('add', $data);
|
||||
@ -328,7 +352,7 @@ class FileController extends AbstractController
|
||||
$data = AbstractModel::transaction(function() use ($file) {
|
||||
$content = FileContent::select(['content', 'text', 'size'])->whereFid($file->cid)->orderByDesc('id')->first();
|
||||
$file->size = $content?->size ?: 0;
|
||||
$file->saveBeforePids();
|
||||
$file->saveBeforePP();
|
||||
if ($content) {
|
||||
$content = $content->toArray();
|
||||
$content['fid'] = $file->id;
|
||||
@ -407,7 +431,7 @@ class FileController extends AbstractController
|
||||
//
|
||||
$file->pid = $pid;
|
||||
$file->handleDuplicateName();
|
||||
$file->saveBeforePids();
|
||||
$file->saveBeforePP();
|
||||
$files[] = $file;
|
||||
}
|
||||
});
|
||||
@ -716,7 +740,7 @@ class FileController extends AbstractController
|
||||
'created_id' => $user->userid,
|
||||
]);
|
||||
$dirRow->handleDuplicateName();
|
||||
if ($dirRow->saveBeforePids()) {
|
||||
if ($dirRow->saveBeforePP()) {
|
||||
$addItem[] = File::find($dirRow->id);
|
||||
}
|
||||
}
|
||||
@ -786,7 +810,7 @@ class FileController extends AbstractController
|
||||
// 开始创建
|
||||
return AbstractModel::transaction(function () use ($addItem, $webkitRelativePath, $type, $user, $data, $file) {
|
||||
$file->size = $data['size'] * 1024;
|
||||
$file->saveBeforePids();
|
||||
$file->saveBeforePP();
|
||||
//
|
||||
$data = Base::uploadMove($data, "uploads/file/" . $file->type . "/" . date("Ym") . "/" . $file->id . "/");
|
||||
$content = [
|
||||
|
||||
@ -13,8 +13,8 @@ use Request;
|
||||
* App\Models\File
|
||||
*
|
||||
* @property int $id
|
||||
* @property string|null $pids 上级ID递归
|
||||
* @property int|null $pid 上级ID
|
||||
* @property string|null $pids 上级ID递归
|
||||
* @property int|null $cid 复制ID
|
||||
* @property string|null $name 名称
|
||||
* @property string|null $type 类型
|
||||
@ -22,6 +22,7 @@ use Request;
|
||||
* @property int|null $size 大小(B)
|
||||
* @property int|null $userid 拥有者ID
|
||||
* @property int|null $share 是否共享
|
||||
* @property int|null $pshare 所属分享ID
|
||||
* @property int|null $created_id 创建者
|
||||
* @property \Illuminate\Support\Carbon|null $created_at
|
||||
* @property \Illuminate\Support\Carbon|null $updated_at
|
||||
@ -39,6 +40,7 @@ use Request;
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|File whereName($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|File wherePid($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|File wherePids($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|File wherePshare($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|File whereShare($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|File whereSize($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|File whereType($value)
|
||||
@ -183,6 +185,7 @@ class File extends AbstractModel
|
||||
AbstractModel::transaction(function () use ($share) {
|
||||
$this->share = $share;
|
||||
$this->save();
|
||||
File::where("pids", "like", "%,{$this->id},%")->update(['pshare' => $share ? $this->id : 0]);
|
||||
if ($share === 0) {
|
||||
FileUser::deleteFileAll($this->id, $this->userid);
|
||||
}
|
||||
@ -223,16 +226,25 @@ class File extends AbstractModel
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存前更新pids
|
||||
* 保存前更新pids/pshare
|
||||
* @return bool
|
||||
*/
|
||||
public function saveBeforePids()
|
||||
public function saveBeforePP()
|
||||
{
|
||||
$pid = $this->pid;
|
||||
$pshare = $this->share ? $this->id : 0;
|
||||
$array = [];
|
||||
while ($pid > 0) {
|
||||
$array[] = $pid;
|
||||
$pid = intval(self::whereId($pid)->value('pid'));
|
||||
$file = self::select(['id', 'pid', 'share'])->find($pid);
|
||||
if ($file) {
|
||||
$pid = $file->pid;
|
||||
if ($file->share) {
|
||||
$pshare = $file->id;
|
||||
}
|
||||
} else {
|
||||
$pid = 0;
|
||||
}
|
||||
}
|
||||
$opids = $this->pids;
|
||||
if ($array) {
|
||||
@ -241,6 +253,7 @@ class File extends AbstractModel
|
||||
} else {
|
||||
$this->pids = '';
|
||||
}
|
||||
$this->pshare = $pshare;
|
||||
if (!$this->save()) {
|
||||
return false;
|
||||
}
|
||||
@ -249,7 +262,7 @@ class File extends AbstractModel
|
||||
self::wherePid($this->id)->chunkById(100, function ($lists) {
|
||||
/** @var self $item */
|
||||
foreach ($lists as $item) {
|
||||
$item->saveBeforePids();
|
||||
$item->saveBeforePP();
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -494,7 +507,7 @@ class File extends AbstractModel
|
||||
'created_id' => 0,
|
||||
]);
|
||||
$file->handleDuplicateName();
|
||||
$file->saveBeforePids();
|
||||
$file->saveBeforePP();
|
||||
|
||||
// 移交文件
|
||||
self::whereUserid($originalUserid)->chunkById(100, function($list) use ($file, $newUserid) {
|
||||
@ -504,7 +517,7 @@ class File extends AbstractModel
|
||||
$item->pid = $file->id;
|
||||
}
|
||||
$item->userid = $newUserid;
|
||||
$item->saveBeforePids();
|
||||
$item->saveBeforePP();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ class AddFilesPids extends Migration
|
||||
\App\Models\File::where('pid', '>', 0)->chunkById(100, function ($lists) {
|
||||
/** @var \App\Models\File $item */
|
||||
foreach ($lists as $item) {
|
||||
$item->saveBeforePids();
|
||||
$item->saveBeforePP();
|
||||
}
|
||||
});
|
||||
\App\Models\File::whereShare(0)->chunkById(100, function ($lists) {
|
||||
|
||||
46
database/migrations/2022_07_15_165114_add_files_pshare.php
Normal file
46
database/migrations/2022_07_15_165114_add_files_pshare.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddFilesPshare extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$isAdd = false;
|
||||
Schema::table('files', function (Blueprint $table) use (&$isAdd) {
|
||||
if (!Schema::hasColumn('files', 'pshare')) {
|
||||
$isAdd = true;
|
||||
$table->bigInteger('pshare')->nullable()->default(0)->after('share')->comment('所属分享ID');
|
||||
}
|
||||
});
|
||||
if ($isAdd) {
|
||||
\App\Models\File::whereShare(1)->chunkById(100, function ($lists) {
|
||||
/** @var \App\Models\File $item */
|
||||
foreach ($lists as $item) {
|
||||
\App\Models\File::where("pids", "like", "%,{$item->id},%")->update(['pshare' => $item->id]);
|
||||
$item->pshare = $item->id;
|
||||
$item->save();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$table->dropColumn("pshare");
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1410,11 +1410,11 @@ export default {
|
||||
},
|
||||
|
||||
onSearchChange() {
|
||||
clearTimeout(this.searchTimeout);
|
||||
this.searchTimeout && clearTimeout(this.searchTimeout);
|
||||
if (this.searchKey.trim() != '') {
|
||||
this.searchTimeout = setTimeout(() => {
|
||||
this.loadIng++;
|
||||
this.$store.dispatch("searchFiles", this.searchKey).then(() => {
|
||||
this.$store.dispatch("searchFiles", this.searchKey.trim()).then(() => {
|
||||
this.loadIng--;
|
||||
}).catch(() => {
|
||||
this.loadIng--;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user