Skip to content

Commit

Permalink
feat: 文件管理增加资源统计,统计总存储量、各类型文件存储占用
Browse files Browse the repository at this point in the history
  • Loading branch information
jskils authored and Charles7c committed Apr 30, 2024
1 parent faa56d1 commit 15c966f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@

package top.continew.admin.system.mapper;

import org.apache.ibatis.annotations.Select;
import top.continew.admin.system.model.entity.FileDO;
import top.continew.admin.system.model.resp.FileStatisticsResp;
import top.continew.starter.data.mybatis.plus.base.BaseMapper;

import java.util.List;

/**
* 文件 Mapper
*
* @author Charles7c
* @since 2023/12/23 10:38
*/
public interface FileMapper extends BaseMapper<FileDO> {


/**
* 查询文件资源统计
*
* @return 文件资源统计结果
*/
@Select("SELECT type,COUNT(1) number,SUM(size) size FROM sys_file GROUP BY type")
List<FileStatisticsResp> statistics();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package top.continew.admin.system.model.resp;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import top.continew.admin.system.enums.FileTypeEnum;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
* 文件资源统计
*
* @author Kils
* @since 2024/04/30 14:30
*/
@Data
@Schema(description = "文件资源统计")
public class FileStatisticsResp implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

/**
* 文件类型
*/
@Schema(description = "文件类型", example = "")
private FileTypeEnum type;

/**
* 大小(字节)
*/
@Schema(description = "大小(字节)", example = "4096")
private Long size;

/**
* 数量
*/
@Schema(description = "数量", example = "1000")
private Long number;

/**
* 分类数据
*/
@Schema(description = "分类数据")
private List<FileStatisticsResp> data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import top.continew.admin.system.model.query.FileQuery;
import top.continew.admin.system.model.req.FileReq;
import top.continew.admin.system.model.resp.FileResp;
import top.continew.admin.system.model.resp.FileStatisticsResp;
import top.continew.starter.data.mybatis.plus.service.IService;
import top.continew.starter.extension.crud.service.BaseService;

Expand Down Expand Up @@ -61,4 +62,11 @@ default FileInfo upload(MultipartFile file) {
* @return 文件数量
*/
Long countByStorageIds(List<Long> storageIds);

/**
* 查询文件资源统计
*
* @return 资源统计结果
*/
FileStatisticsResp statistics();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

package top.continew.admin.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.unit.DataSizeUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -34,6 +39,7 @@
import top.continew.admin.system.model.query.FileQuery;
import top.continew.admin.system.model.req.FileReq;
import top.continew.admin.system.model.resp.FileResp;
import top.continew.admin.system.model.resp.FileStatisticsResp;
import top.continew.admin.system.service.FileService;
import top.continew.admin.system.service.StorageService;
import top.continew.starter.core.constant.StringConstants;
Expand Down Expand Up @@ -113,6 +119,19 @@ public Long countByStorageIds(List<Long> storageIds) {
return baseMapper.lambdaQuery().in(FileDO::getStorageId, storageIds).count();
}

@Override
public FileStatisticsResp statistics() {
FileStatisticsResp resp = new FileStatisticsResp();
List<FileStatisticsResp> statisticsList = baseMapper.statistics();
if (CollUtil.isEmpty(statisticsList)) {
return resp;
}
resp.setData(statisticsList);
resp.setSize(statisticsList.stream().mapToLong(FileStatisticsResp::getSize).sum());
resp.setNumber(statisticsList.stream().mapToLong(FileStatisticsResp::getNumber).sum());
return resp;
}

@Override
protected void fill(Object obj) {
super.fill(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
*/

package top.continew.admin.webapi.system;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import top.continew.admin.system.model.query.FileQuery;
import top.continew.admin.system.model.req.FileReq;
import top.continew.admin.system.model.resp.FileResp;
import top.continew.admin.system.model.resp.FileStatisticsResp;
import top.continew.admin.system.service.FileService;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.log.core.annotation.Log;
import top.continew.starter.web.model.R;

/**
* 文件管理 API
Expand All @@ -36,6 +38,16 @@
*/
@Tag(name = "文件管理 API")
@RestController
@RequiredArgsConstructor
@CrudRequestMapping(value = "/system/file", api = {Api.PAGE, Api.UPDATE, Api.DELETE})
public class FileController extends BaseController<FileService, FileResp, FileResp, FileQuery, FileReq> {

private final FileService fileService;

@Log(ignore = true)
@Operation(summary = "查询文件资源统计", description = "查询文件资源统计")
@GetMapping("/statistics")
public R<FileStatisticsResp> statistics() {
return R.ok(fileService.statistics());
}
}

0 comments on commit 15c966f

Please sign in to comment.