样机小程序
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\traits\crud\CrudLogicTrait;
|
||||
use app\common\model\UploadedFiles;
|
||||
|
||||
class UploadedFilesLogic
|
||||
{
|
||||
use CrudLogicTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = UploadedFiles::class;
|
||||
|
||||
/**
|
||||
* 根据分区获取文件列表
|
||||
*
|
||||
* @param string $partition
|
||||
* @param int $limit
|
||||
* @param int $page
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFilesByPartition(string $partition, int $limit = 15, int $page = 1)
|
||||
{
|
||||
$config = [
|
||||
'model' => $this->modelClass,
|
||||
'where' => ['partition' => $partition],
|
||||
'order' => ['created_at' => 'desc', 'id' => 'desc']
|
||||
];
|
||||
|
||||
return $this->logicList($config, $limit, $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类获取文件列表
|
||||
*
|
||||
* @param string $category
|
||||
* @param int $limit
|
||||
* @param int $page
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFilesByCategory(string $category, int $limit = 15, int $page = 1)
|
||||
{
|
||||
$config = [
|
||||
'model' => $this->modelClass,
|
||||
'where' => ['category' => $category],
|
||||
'order' => ['created_at' => 'desc', 'id' => 'desc']
|
||||
];
|
||||
|
||||
return $this->logicList($config, $limit, $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件类型获取文件列表
|
||||
*
|
||||
* @param string $fileType
|
||||
* @param int $limit
|
||||
* @param int $page
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFilesByType(string $fileType, int $limit = 15, int $page = 1)
|
||||
{
|
||||
$config = [
|
||||
'model' => $this->modelClass,
|
||||
'where' => ['file_type' => $fileType],
|
||||
'order' => ['created_at' => 'desc', 'id' => 'desc']
|
||||
];
|
||||
|
||||
return $this->logicList($config, $limit, $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索文件
|
||||
*
|
||||
* @param string $keyword
|
||||
* @param int $limit
|
||||
* @param int $page
|
||||
* @return mixed
|
||||
*/
|
||||
public function searchFiles(string $keyword, int $limit = 15, int $page = 1)
|
||||
{
|
||||
$config = [
|
||||
'model' => $this->modelClass,
|
||||
'where' => [
|
||||
'or' => [
|
||||
['file_name', 'like', '%' . $keyword . '%'],
|
||||
['original_name', 'like', '%' . $keyword . '%'],
|
||||
['category', 'like', '%' . $keyword . '%']
|
||||
]
|
||||
],
|
||||
'order' => ['created_at' => 'desc', 'id' => 'desc']
|
||||
];
|
||||
|
||||
return $this->logicList($config, $limit, $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件统计信息
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFileStatistics(): array
|
||||
{
|
||||
$model = new $this->modelClass();
|
||||
|
||||
$totalFiles = $model->count();
|
||||
$totalSize = $model->sum('file_size');
|
||||
|
||||
$partitionStats = $model->selectRaw('partition, COUNT(*) as count, SUM(file_size) as size')
|
||||
->groupBy('partition')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
$categoryStats = $model->selectRaw('category, COUNT(*) as count, SUM(file_size) as size')
|
||||
->groupBy('category')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
$typeStats = $model->selectRaw('file_type, COUNT(*) as count, SUM(file_size) as size')
|
||||
->groupBy('file_type')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
return [
|
||||
'total_files' => $totalFiles,
|
||||
'total_size' => $totalSize,
|
||||
'partition_stats' => $partitionStats,
|
||||
'category_stats' => $categoryStats,
|
||||
'type_stats' => $typeStats
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理过期文件
|
||||
*
|
||||
* @param int $days 保留天数
|
||||
* @return int 清理的文件数量
|
||||
*/
|
||||
public function cleanExpiredFiles(int $days = 30): int
|
||||
{
|
||||
$expiredTime = time() - ($days * 24 * 60 * 60);
|
||||
|
||||
$config = [
|
||||
'model' => $this->modelClass,
|
||||
'where' => [
|
||||
['created_at', '<', $expiredTime]
|
||||
]
|
||||
];
|
||||
|
||||
$files = $this->logicListAll($config);
|
||||
$count = 0;
|
||||
|
||||
foreach ($files as $file) {
|
||||
// 删除物理文件
|
||||
$fullPath = public_path() . $file->file_path;
|
||||
if (file_exists($fullPath)) {
|
||||
unlink($fullPath);
|
||||
}
|
||||
|
||||
// 删除数据库记录
|
||||
$this->logicDeleteBy(['where' => ['id' => $file->id]]);
|
||||
$count++;
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user