mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
fix: 修复super管理员获取菜单时因companyId为null抛异常的问题
- 将getCurrentCompanyId()调用延迟到switch分支内部 - super角色不需要公司上下文,直接获取所有菜单 - admin/user角色按需获取公司ID
This commit is contained in:
-103
@@ -1,103 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.MediaFileFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.UpdateFileDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaFileVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.service.MediaFileService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件资源池管理控制器
|
||||
*/
|
||||
@Tag(name = "媒体-文件管理", description = "文件的增删改查接口")
|
||||
@RestController
|
||||
@RequestMapping("/media/file")
|
||||
public class MediaFileController {
|
||||
|
||||
private final MediaFileService mediaFileService;
|
||||
|
||||
public MediaFileController(MediaFileService mediaFileService) {
|
||||
this.mediaFileService = mediaFileService;
|
||||
}
|
||||
|
||||
@Operation(summary = "上传文件", description = "上传单个文件")
|
||||
@PostMapping("/upload")
|
||||
public ApiResponse<MediaFileVO> uploadFile(
|
||||
@RequestParam("file") MultipartFile file) {
|
||||
|
||||
// 直接调用Service处理文件上传逻辑
|
||||
MediaFileVO mediaFileVO = mediaFileService.uploadFile(file);
|
||||
return ApiResponse.success(mediaFileVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "批量上传文件", description = "批量上传多个文件")
|
||||
@PostMapping("/batch-upload")
|
||||
public ApiResponse<List<MediaFileVO>> batchUploadFiles(
|
||||
@RequestParam("files") List<MultipartFile> files) {
|
||||
|
||||
if (files == null || files.isEmpty()) {
|
||||
return ApiResponse.fail("请选择要上传的文件");
|
||||
}
|
||||
|
||||
// 直接调用Service处理批量文件上传逻辑
|
||||
List<MediaFileVO> mediaFileVOList = mediaFileService.batchUploadFiles(files);
|
||||
return ApiResponse.success(mediaFileVOList);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新文件信息", description = "更新文件的元数据信息")
|
||||
@PutMapping("/update/{id}")
|
||||
public ApiResponse<MediaFileVO> updateFile(
|
||||
@Parameter(description = "文件ID") @PathVariable Long id,
|
||||
@Valid @RequestBody UpdateFileDTO updateFileDTO) {
|
||||
MediaFileVO mediaFileVO = mediaFileService.updateFile(id, updateFileDTO);
|
||||
return ApiResponse.success(mediaFileVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "删除文件", description = "删除指定文件")
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public ApiResponse<Void> deleteFile(
|
||||
@Parameter(description = "文件ID") @PathVariable Long id) {
|
||||
mediaFileService.deleteFile(id);
|
||||
return ApiResponse.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "根据哈希查询文件", description = "根据文件哈希值查询文件信息")
|
||||
@GetMapping("/hash/{fileHash}")
|
||||
public ApiResponse<MediaFileVO> getFileByHash(
|
||||
@Parameter(description = "文件哈希值") @PathVariable String fileHash) {
|
||||
MediaFileVO mediaFileVO = mediaFileService.getFileByHash(fileHash);
|
||||
if (mediaFileVO == null) {
|
||||
return ApiResponse.fail("文件不存在");
|
||||
}
|
||||
return ApiResponse.success(mediaFileVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "批量根据哈希查询文件", description = "根据文件哈希值列表批量查询文件信息")
|
||||
@PostMapping("/batch-hash")
|
||||
public ApiResponse<List<MediaFileVO>> getFilesByHashes(
|
||||
@RequestBody List<String> fileHashes) {
|
||||
if (fileHashes == null || fileHashes.isEmpty()) {
|
||||
return ApiResponse.fail("哈希列表不能为空");
|
||||
}
|
||||
List<MediaFileVO> mediaFileVOList = mediaFileService.getFilesByHashes(fileHashes);
|
||||
return ApiResponse.success(mediaFileVOList);
|
||||
}
|
||||
|
||||
@Operation(summary = "分页获取文件列表", description = "分页获取文件列表,支持条件过滤")
|
||||
@PostMapping("/page")
|
||||
public ApiResponse<PageResultVO<MediaFileVO>> getFilePage(
|
||||
@Valid @RequestBody PageQueryDTO<MediaFileFilterDTO> pageQueryDTO) {
|
||||
PageResultVO<MediaFileVO> result = mediaFileService.getFilePage(pageQueryDTO);
|
||||
return ApiResponse.success(result);
|
||||
}
|
||||
}
|
||||
-116
@@ -1,116 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.MediaImageFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.UpdateImageDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaImageVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.service.MediaImageService;
|
||||
import top.xyquan.ecomdmbbackend.utils.FileUploadUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 图片资源池管理控制器
|
||||
*/
|
||||
@Tag(name = "媒体-图片管理", description = "图片的增删改查接口")
|
||||
@RestController
|
||||
@RequestMapping("/media/image")
|
||||
public class MediaImageController {
|
||||
|
||||
private final MediaImageService mediaImageService;
|
||||
|
||||
public MediaImageController(MediaImageService mediaImageService) {
|
||||
this.mediaImageService = mediaImageService;
|
||||
}
|
||||
|
||||
@Operation(summary = "上传图片", description = "上传单张图片文件")
|
||||
@PostMapping("/upload")
|
||||
public ApiResponse<MediaImageVO> uploadImage(
|
||||
@RequestParam("file") MultipartFile file) {
|
||||
|
||||
// 检查是否为图片文件
|
||||
if (!FileUploadUtils.isImageFile(file.getOriginalFilename())) {
|
||||
return ApiResponse.fail("只能上传图片文件");
|
||||
}
|
||||
|
||||
// 直接调用Service处理图片上传逻辑
|
||||
MediaImageVO mediaImageVO = mediaImageService.uploadImage(file);
|
||||
return ApiResponse.success(mediaImageVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "批量上传图片", description = "批量上传多张图片文件")
|
||||
@PostMapping("/batch-upload")
|
||||
public ApiResponse<List<MediaImageVO>> batchUploadImages(
|
||||
@RequestParam("files") List<MultipartFile> files) {
|
||||
|
||||
if (files == null || files.isEmpty()) {
|
||||
return ApiResponse.fail("请选择要上传的图片文件");
|
||||
}
|
||||
|
||||
// 检查所有文件是否为图片
|
||||
for (MultipartFile file : files) {
|
||||
if (!FileUploadUtils.isImageFile(file.getOriginalFilename())) {
|
||||
return ApiResponse.fail("只能上传图片文件");
|
||||
}
|
||||
}
|
||||
|
||||
// 直接调用Service处理批量图片上传逻辑
|
||||
List<MediaImageVO> mediaImageVOList = mediaImageService.batchUploadImages(files);
|
||||
return ApiResponse.success(mediaImageVOList);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新图片信息", description = "更新图片的元数据信息")
|
||||
@PutMapping("/update/{id}")
|
||||
public ApiResponse<MediaImageVO> updateImage(
|
||||
@Parameter(description = "图片ID") @PathVariable Long id,
|
||||
@Valid @RequestBody UpdateImageDTO updateImageDTO) {
|
||||
MediaImageVO mediaImageVO = mediaImageService.updateImage(id, updateImageDTO);
|
||||
return ApiResponse.success(mediaImageVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "删除图片", description = "删除指定图片")
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public ApiResponse<Void> deleteImage(
|
||||
@Parameter(description = "图片ID") @PathVariable Long id) {
|
||||
mediaImageService.deleteImage(id);
|
||||
return ApiResponse.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "根据哈希查询图片", description = "根据文件哈希值查询图片信息")
|
||||
@GetMapping("/hash/{fileHash}")
|
||||
public ApiResponse<MediaImageVO> getImageByHash(
|
||||
@Parameter(description = "文件哈希值") @PathVariable String fileHash) {
|
||||
MediaImageVO mediaImageVO = mediaImageService.getImageByHash(fileHash);
|
||||
if (mediaImageVO == null) {
|
||||
return ApiResponse.fail("图片不存在");
|
||||
}
|
||||
return ApiResponse.success(mediaImageVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "批量根据哈希查询图片", description = "根据文件哈希值列表批量查询图片信息")
|
||||
@PostMapping("/batch-hash")
|
||||
public ApiResponse<List<MediaImageVO>> getImagesByHashes(
|
||||
@RequestBody List<String> fileHashes) {
|
||||
if (fileHashes == null || fileHashes.isEmpty()) {
|
||||
return ApiResponse.fail("哈希列表不能为空");
|
||||
}
|
||||
List<MediaImageVO> mediaImageVOList = mediaImageService.getImagesByHashes(fileHashes);
|
||||
return ApiResponse.success(mediaImageVOList);
|
||||
}
|
||||
|
||||
@Operation(summary = "分页获取图片列表", description = "分页获取图片列表,支持条件过滤")
|
||||
@PostMapping("/page")
|
||||
public ApiResponse<PageResultVO<MediaImageVO>> getImagePage(
|
||||
@Valid @RequestBody PageQueryDTO<MediaImageFilterDTO> pageQueryDTO) {
|
||||
PageResultVO<MediaImageVO> result = mediaImageService.getImagePage(pageQueryDTO);
|
||||
return ApiResponse.success(result);
|
||||
}
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.po.MediaFilePO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件资源池Mapper
|
||||
*/
|
||||
@Mapper
|
||||
public interface MediaFileMapper extends BaseMapper<MediaFilePO> {
|
||||
|
||||
/**
|
||||
* 根据文件哈希查询文件信息(返回第一个匹配的记录)
|
||||
*
|
||||
* @param fileHash 文件哈希
|
||||
* @return 文件信息
|
||||
*/
|
||||
MediaFilePO selectByFileHash(@Param("fileHash") String fileHash);
|
||||
|
||||
/**
|
||||
* 根据文件哈希列表批量查询文件信息
|
||||
*
|
||||
* @param fileHashes 文件哈希列表
|
||||
* @return 文件信息列表
|
||||
*/
|
||||
List<MediaFilePO> selectByFileHashes(@Param("fileHashes") List<String> fileHashes);
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.po.MediaImagePO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 图片资源池Mapper
|
||||
*/
|
||||
@Mapper
|
||||
public interface MediaImageMapper extends BaseMapper<MediaImagePO> {
|
||||
|
||||
/**
|
||||
* 根据文件哈希查询图片信息(返回第一个匹配的记录)
|
||||
*
|
||||
* @param fileHash 文件哈希
|
||||
* @return 图片信息
|
||||
*/
|
||||
MediaImagePO selectByFileHash(@Param("fileHash") String fileHash);
|
||||
|
||||
/**
|
||||
* 根据文件哈希列表批量查询图片信息
|
||||
*
|
||||
* @param fileHashes 文件哈希列表
|
||||
* @return 图片信息列表
|
||||
*/
|
||||
List<MediaImagePO> selectByFileHashes(@Param("fileHashes") List<String> fileHashes);
|
||||
}
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 文件DTO
|
||||
*/
|
||||
@Data
|
||||
public class MediaFileDTO {
|
||||
|
||||
/**
|
||||
* 文件ID(更新时使用)
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 文件URL
|
||||
*/
|
||||
@NotBlank(message = "文件URL不能为空")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
@NotBlank(message = "文件名不能为空")
|
||||
private String filename;
|
||||
|
||||
/**
|
||||
* 文件格式,如 pdf/cad/xlsx/doc
|
||||
*/
|
||||
private String fileFormat;
|
||||
|
||||
/**
|
||||
* 文件哈希值,用于去重
|
||||
*/
|
||||
private String fileHash;
|
||||
|
||||
/**
|
||||
* 标签,逗号分隔
|
||||
*/
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 文件描述
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 文件过滤条件DTO
|
||||
*/
|
||||
@Data
|
||||
public class MediaFileFilterDTO {
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String filename;
|
||||
|
||||
/**
|
||||
* 文件格式
|
||||
*/
|
||||
private String fileFormat;
|
||||
|
||||
/**
|
||||
* 标签
|
||||
*/
|
||||
private String tags;
|
||||
}
|
||||
-52
@@ -1,52 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 图片DTO
|
||||
*/
|
||||
@Data
|
||||
public class MediaImageDTO {
|
||||
|
||||
/**
|
||||
* 图片ID(更新时使用)
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 图片URL
|
||||
*/
|
||||
@NotBlank(message = "图片URL不能为空")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 原始文件名
|
||||
*/
|
||||
private String filename;
|
||||
|
||||
/**
|
||||
* 图片分类 (image,avatar,logo)
|
||||
*/
|
||||
private String category;
|
||||
|
||||
/**
|
||||
* 图片格式,如 jpg/png/gif/webp
|
||||
*/
|
||||
private String imageFormat;
|
||||
|
||||
/**
|
||||
* 文件哈希值,用于去重
|
||||
*/
|
||||
private String fileHash;
|
||||
|
||||
/**
|
||||
* 标签,逗号分隔
|
||||
*/
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 图片说明
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 图片过滤条件DTO
|
||||
*/
|
||||
@Data
|
||||
public class MediaImageFilterDTO {
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String filename;
|
||||
|
||||
/**
|
||||
* 图片分类
|
||||
*/
|
||||
private String category;
|
||||
|
||||
/**
|
||||
* 图片格式
|
||||
*/
|
||||
private String imageFormat;
|
||||
|
||||
/**
|
||||
* 标签
|
||||
*/
|
||||
private String tags;
|
||||
}
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 更新文件信息DTO
|
||||
*/
|
||||
@Data
|
||||
public class UpdateFileDTO {
|
||||
|
||||
/**
|
||||
* 标签,逗号分隔
|
||||
*/
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 文件描述
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 更新图片信息DTO
|
||||
*/
|
||||
@Data
|
||||
public class UpdateImageDTO {
|
||||
|
||||
/**
|
||||
* 标签,逗号分隔
|
||||
*/
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 图片说明
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
-100
@@ -1,100 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 文件资源池实体
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("media_file")
|
||||
public class MediaFilePO {
|
||||
|
||||
/**
|
||||
* 文件ID
|
||||
*/
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 公司ID
|
||||
*/
|
||||
@TableField("company_id")
|
||||
private Long companyId;
|
||||
|
||||
/**
|
||||
* 文件URL
|
||||
*/
|
||||
@TableField("url")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
@TableField("filename")
|
||||
private String filename;
|
||||
|
||||
/**
|
||||
* 文件格式,如 pdf/cad/xlsx/doc
|
||||
*/
|
||||
@TableField("file_format")
|
||||
private String fileFormat;
|
||||
|
||||
/**
|
||||
* 文件哈希值,用于去重
|
||||
*/
|
||||
@TableField("file_hash")
|
||||
private String fileHash;
|
||||
|
||||
/**
|
||||
* 标签,逗号分隔
|
||||
*/
|
||||
@TableField("tags")
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 文件描述
|
||||
*/
|
||||
@TableField("remark")
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 上传人ID
|
||||
*/
|
||||
@TableField("upload_by")
|
||||
private Long uploadBy;
|
||||
|
||||
/**
|
||||
* 上传时间
|
||||
*/
|
||||
@TableField(value = "upload_time", fill = FieldFill.INSERT)
|
||||
private LocalDateTime uploadTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
/**
|
||||
* 更新人ID
|
||||
*/
|
||||
@TableField("update_by")
|
||||
private Long updateBy;
|
||||
|
||||
/**
|
||||
* 删除时间
|
||||
*/
|
||||
@TableField("delete_time")
|
||||
private LocalDateTime deleteTime;
|
||||
|
||||
/**
|
||||
* 删除人ID
|
||||
*/
|
||||
@TableField("delete_by")
|
||||
private Long deleteBy;
|
||||
}
|
||||
-106
@@ -1,106 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 图片资源池实体
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("media_image")
|
||||
public class MediaImagePO {
|
||||
|
||||
/**
|
||||
* 图片ID
|
||||
*/
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 公司ID
|
||||
*/
|
||||
@TableField("company_id")
|
||||
private Long companyId;
|
||||
|
||||
/**
|
||||
* 图片URL
|
||||
*/
|
||||
@TableField("url")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 原始文件名
|
||||
*/
|
||||
@TableField("filename")
|
||||
private String filename;
|
||||
|
||||
/**
|
||||
* 图片分类 (image,avatar,logo)
|
||||
*/
|
||||
@TableField("category")
|
||||
private String category;
|
||||
|
||||
/**
|
||||
* 图片格式,如 jpg/png/gif/webp
|
||||
*/
|
||||
@TableField("image_format")
|
||||
private String imageFormat;
|
||||
|
||||
/**
|
||||
* 文件哈希值,用于去重
|
||||
*/
|
||||
@TableField("file_hash")
|
||||
private String fileHash;
|
||||
|
||||
/**
|
||||
* 标签,逗号分隔
|
||||
*/
|
||||
@TableField("tags")
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 图片说明
|
||||
*/
|
||||
@TableField("remark")
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 上传人ID
|
||||
*/
|
||||
@TableField("upload_by")
|
||||
private Long uploadBy;
|
||||
|
||||
/**
|
||||
* 上传时间
|
||||
*/
|
||||
@TableField(value = "upload_time", fill = FieldFill.INSERT)
|
||||
private LocalDateTime uploadTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
/**
|
||||
* 更新人ID
|
||||
*/
|
||||
@TableField("update_by")
|
||||
private Long updateBy;
|
||||
|
||||
/**
|
||||
* 删除时间
|
||||
*/
|
||||
@TableField("delete_time")
|
||||
private LocalDateTime deleteTime;
|
||||
|
||||
/**
|
||||
* 删除人ID
|
||||
*/
|
||||
@TableField("delete_by")
|
||||
private Long deleteBy;
|
||||
}
|
||||
-62
@@ -1,62 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 文件VO
|
||||
*/
|
||||
@Data
|
||||
public class MediaFileVO {
|
||||
|
||||
/**
|
||||
* 文件ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 公司ID
|
||||
*/
|
||||
private Long companyId;
|
||||
|
||||
/**
|
||||
* 文件URL
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String filename;
|
||||
|
||||
/**
|
||||
* 文件格式,如 pdf/cad/xlsx/doc
|
||||
*/
|
||||
private String fileFormat;
|
||||
|
||||
/**
|
||||
* 文件哈希值,用于去重
|
||||
*/
|
||||
private String fileHash;
|
||||
|
||||
/**
|
||||
* 标签,逗号分隔
|
||||
*/
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 文件描述
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 上传人ID
|
||||
*/
|
||||
private Long uploadBy;
|
||||
|
||||
/**
|
||||
* 上传时间
|
||||
*/
|
||||
private LocalDateTime uploadTime;
|
||||
}
|
||||
-67
@@ -1,67 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 图片VO
|
||||
*/
|
||||
@Data
|
||||
public class MediaImageVO {
|
||||
|
||||
/**
|
||||
* 图片ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 公司ID
|
||||
*/
|
||||
private Long companyId;
|
||||
|
||||
/**
|
||||
* 图片URL
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 原始文件名
|
||||
*/
|
||||
private String filename;
|
||||
|
||||
/**
|
||||
* 图片分类 (image,avatar,logo)
|
||||
*/
|
||||
private String category;
|
||||
|
||||
/**
|
||||
* 图片格式,如 jpg/png/gif/webp
|
||||
*/
|
||||
private String imageFormat;
|
||||
|
||||
/**
|
||||
* 文件哈希值,用于去重
|
||||
*/
|
||||
private String fileHash;
|
||||
|
||||
/**
|
||||
* 标签,逗号分隔
|
||||
*/
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 图片说明
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 上传人ID
|
||||
*/
|
||||
private Long uploadBy;
|
||||
|
||||
/**
|
||||
* 上传时间
|
||||
*/
|
||||
private LocalDateTime uploadTime;
|
||||
}
|
||||
-51
@@ -1,51 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.MediaFileFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.UpdateFileDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaFileVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件资源池服务接口
|
||||
*/
|
||||
public interface MediaFileService {
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
MediaFileVO uploadFile(MultipartFile file);
|
||||
|
||||
/**
|
||||
* 批量上传文件
|
||||
*/
|
||||
List<MediaFileVO> batchUploadFiles(List<MultipartFile> files);
|
||||
|
||||
/**
|
||||
* 更新文件信息
|
||||
*/
|
||||
MediaFileVO updateFile(Long id, UpdateFileDTO updateFileDTO);
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*/
|
||||
void deleteFile(Long id);
|
||||
|
||||
/**
|
||||
* 根据文件哈希查询文件信息
|
||||
*/
|
||||
MediaFileVO getFileByHash(String fileHash);
|
||||
|
||||
/**
|
||||
* 根据文件哈希列表批量查询文件信息
|
||||
*/
|
||||
List<MediaFileVO> getFilesByHashes(List<String> fileHashes);
|
||||
|
||||
/**
|
||||
* 分页获取文件列表
|
||||
*/
|
||||
PageResultVO<MediaFileVO> getFilePage(PageQueryDTO<MediaFileFilterDTO> pageQueryDTO);
|
||||
}
|
||||
-66
@@ -1,66 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.MediaImageFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.UpdateImageDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaImageVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 图片资源池服务接口
|
||||
*/
|
||||
public interface MediaImageService {
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
*/
|
||||
MediaImageVO uploadImage(MultipartFile file);
|
||||
|
||||
/**
|
||||
* 批量上传图片
|
||||
*/
|
||||
List<MediaImageVO> batchUploadImages(List<MultipartFile> files);
|
||||
|
||||
/**
|
||||
* 更新图片信息
|
||||
*/
|
||||
MediaImageVO updateImage(Long id, UpdateImageDTO updateImageDTO);
|
||||
|
||||
/**
|
||||
* 删除图片
|
||||
*/
|
||||
void deleteImage(Long id);
|
||||
|
||||
/**
|
||||
* 根据ID查询图片信息
|
||||
*/
|
||||
MediaImageVO getImageById(Long id);
|
||||
|
||||
/**
|
||||
* 根据文件哈希查询图片信息
|
||||
*/
|
||||
MediaImageVO getImageByHash(String fileHash);
|
||||
|
||||
/**
|
||||
* 根据文件哈希列表批量查询图片信息
|
||||
*/
|
||||
List<MediaImageVO> getImagesByHashes(List<String> fileHashes);
|
||||
|
||||
/**
|
||||
* 分页获取图片列表
|
||||
*/
|
||||
PageResultVO<MediaImageVO> getImagePage(PageQueryDTO<MediaImageFilterDTO> pageQueryDTO);
|
||||
|
||||
/**
|
||||
* 根据文件哈希和分类查询图片信息
|
||||
*/
|
||||
MediaImageVO getImageByHashAndType(String fileHash, String category);
|
||||
|
||||
/**
|
||||
* 上传指定分类的图片
|
||||
*/
|
||||
MediaImageVO uploadImageWithType(MultipartFile file, String category);
|
||||
}
|
||||
-285
@@ -1,285 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.mapper.MediaFileMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.UpdateFileDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.po.MediaFilePO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaFileVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.service.MediaFileService;
|
||||
import top.xyquan.ecomdmbbackend.utils.FileUploadUtils;
|
||||
import top.xyquan.ecomdmbbackend.utils.FileHashUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.MediaFileFilterDTO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 文件资源池服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class MediaFileServiceImpl extends ServiceImpl<MediaFileMapper, MediaFilePO>
|
||||
implements MediaFileService {
|
||||
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public MediaFileServiceImpl(CurrentUserContext currentUserContext) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param file 文件
|
||||
* @return 文件VO
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public MediaFileVO uploadFile(MultipartFile file) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 计算文件哈希
|
||||
String fileHash = FileHashUtils.calculateFileHash(file);
|
||||
|
||||
// 通过文件后缀判断文件格式
|
||||
String fileFormat = FileUploadUtils.getFileExtension(file.getOriginalFilename()).toLowerCase();
|
||||
|
||||
// 检查当前公司是否已有相同哈希的文件且文件存在
|
||||
List<MediaFilePO> existingFiles = baseMapper.selectByFileHashes(List.of(fileHash));
|
||||
MediaFilePO existingFileWithValidFile = existingFiles.stream()
|
||||
.filter(f -> companyId.equals(f.getCompanyId()) && FileHashUtils.fileExists(f.getUrl()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
if (existingFileWithValidFile != null) {
|
||||
// 当前公司已有相同文件且有效,直接返回现有记录
|
||||
return convertToVO(existingFileWithValidFile);
|
||||
}
|
||||
|
||||
// 检查是否有任何公司已有相同哈希的文件且文件存在(用于文件共享)
|
||||
MediaFilePO sharedFileWithValidFile = existingFiles.stream()
|
||||
.filter(f -> FileHashUtils.fileExists(f.getUrl()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
String fileUrl;
|
||||
if (sharedFileWithValidFile != null) {
|
||||
// 其他公司已有相同文件且有效,共享文件URL
|
||||
fileUrl = sharedFileWithValidFile.getUrl();
|
||||
} else {
|
||||
// 没有任何公司有相同文件,保存新文件
|
||||
fileUrl = FileUploadUtils.uploadFile(file);
|
||||
}
|
||||
|
||||
// 创建新记录(即使是相同文件,不同公司也要创建新记录)
|
||||
MediaFilePO mediaFile = new MediaFilePO();
|
||||
mediaFile.setCompanyId(companyId);
|
||||
mediaFile.setUrl(fileUrl);
|
||||
mediaFile.setFilename(file.getOriginalFilename());
|
||||
mediaFile.setFileFormat(fileFormat);
|
||||
mediaFile.setFileHash(fileHash);
|
||||
mediaFile.setUploadBy(currentUserContext.getCurrentUserId());
|
||||
|
||||
// 保存文件记录
|
||||
this.save(mediaFile);
|
||||
|
||||
// 转换为VO并返回
|
||||
return convertToVO(mediaFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量上传文件
|
||||
* @param files 文件列表
|
||||
* @return 文件VO列表
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public List<MediaFileVO> batchUploadFiles(List<MultipartFile> files) {
|
||||
if (files == null || files.isEmpty()) {
|
||||
throw new BusinessException("文件列表不能为空");
|
||||
}
|
||||
|
||||
// 批量处理文件上传
|
||||
List<MediaFileVO> resultList = new ArrayList<>();
|
||||
for (MultipartFile file : files) {
|
||||
resultList.add(uploadFile(file));
|
||||
}
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文件
|
||||
* @param id 文件ID
|
||||
* @param updateFileDTO 更新文件DTO
|
||||
* @return 文件VO
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public MediaFileVO updateFile(Long id, UpdateFileDTO updateFileDTO) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 检查文件是否存在且属于当前公司
|
||||
MediaFilePO existingFile = this.getById(id);
|
||||
if (existingFile == null) {
|
||||
throw new BusinessException("文件不存在");
|
||||
}
|
||||
|
||||
// 验证文件是否属于当前公司
|
||||
if (!companyId.equals(existingFile.getCompanyId())) {
|
||||
throw new BusinessException("无权操作此文件");
|
||||
}
|
||||
|
||||
// 更新文件信息(不更新文件格式字段,格式在创建时确定后不可修改)
|
||||
MediaFilePO mediaFile = new MediaFilePO();
|
||||
mediaFile.setId(id);
|
||||
mediaFile.setTags(updateFileDTO.getTags());
|
||||
mediaFile.setRemark(updateFileDTO.getRemark());
|
||||
mediaFile.setUpdateBy(currentUserContext.getCurrentUserId()); // 记录更新人
|
||||
|
||||
// 更新文件记录
|
||||
this.updateById(mediaFile);
|
||||
|
||||
// 重新查询更新后的记录
|
||||
MediaFilePO updatedFile = this.getById(id);
|
||||
return convertToVO(updatedFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param id 文件ID
|
||||
*/
|
||||
@Override
|
||||
public void deleteFile(Long id) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 检查文件是否存在且属于当前公司
|
||||
MediaFilePO mediaFile = this.getById(id);
|
||||
if (mediaFile == null) {
|
||||
throw new BusinessException("文件不存在");
|
||||
}
|
||||
|
||||
// 验证文件是否属于当前公司
|
||||
if (!companyId.equals(mediaFile.getCompanyId())) {
|
||||
throw new BusinessException("无权操作此文件");
|
||||
}
|
||||
|
||||
// 删除物理文件
|
||||
FileUploadUtils.deleteFile(mediaFile.getUrl());
|
||||
|
||||
// 删除文件记录
|
||||
this.removeById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件哈希获取文件
|
||||
* @param fileHash 文件哈希
|
||||
* @return 文件VO
|
||||
*/
|
||||
@Override
|
||||
public MediaFileVO getFileByHash(String fileHash) {
|
||||
MediaFilePO mediaFile = baseMapper.selectByFileHash(fileHash);
|
||||
if (mediaFile == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 检查文件是否真的存在
|
||||
if (!FileHashUtils.fileExists(mediaFile.getUrl())) {
|
||||
// 文件不存在,返回null但不删除记录,等待重新上传时更新
|
||||
return null;
|
||||
}
|
||||
|
||||
return convertToVO(mediaFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件哈希列表获取文件
|
||||
* @param fileHashes 文件哈希列表
|
||||
* @return 文件VO列表
|
||||
*/
|
||||
@Override
|
||||
public List<MediaFileVO> getFilesByHashes(List<String> fileHashes) {
|
||||
if (fileHashes == null || fileHashes.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
List<MediaFilePO> mediaFiles = baseMapper.selectByFileHashes(fileHashes);
|
||||
|
||||
// 过滤掉文件不存在的记录
|
||||
List<MediaFileVO> resultList = mediaFiles.stream()
|
||||
.filter(mediaFile -> FileHashUtils.fileExists(mediaFile.getUrl()))
|
||||
.map(this::convertToVO)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件分页列表
|
||||
* @param pageQueryDTO 分页查询DTO
|
||||
* @return 分页结果VO
|
||||
*/
|
||||
@Override
|
||||
public PageResultVO<MediaFileVO> getFilePage(PageQueryDTO<MediaFileFilterDTO> pageQueryDTO) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 创建分页对象
|
||||
Page<MediaFilePO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||
|
||||
// 构建查询条件
|
||||
LambdaQueryWrapper<MediaFilePO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(MediaFilePO::getCompanyId, companyId)
|
||||
.isNull(MediaFilePO::getDeleteTime);
|
||||
|
||||
// 添加过滤条件
|
||||
MediaFileFilterDTO filter = pageQueryDTO.getFilters();
|
||||
if (filter != null) {
|
||||
if (filter.getFilename() != null && !filter.getFilename().trim().isEmpty()) {
|
||||
wrapper.like(MediaFilePO::getFilename, filter.getFilename());
|
||||
}
|
||||
if (filter.getFileFormat() != null && !filter.getFileFormat().trim().isEmpty()) {
|
||||
wrapper.eq(MediaFilePO::getFileFormat, filter.getFileFormat());
|
||||
}
|
||||
if (filter.getTags() != null && !filter.getTags().trim().isEmpty()) {
|
||||
wrapper.like(MediaFilePO::getTags, filter.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
// 排序
|
||||
wrapper.orderByDesc(MediaFilePO::getUploadTime);
|
||||
|
||||
// 执行分页查询
|
||||
IPage<MediaFilePO> pageResult = this.page(page, wrapper);
|
||||
|
||||
// 转换为VO
|
||||
List<MediaFileVO> voList = pageResult.getRecords().stream()
|
||||
.map(this::convertToVO)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 构建分页结果
|
||||
return PageResultVO.create(voList, pageResult.getTotal(), pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为VO
|
||||
*/
|
||||
private MediaFileVO convertToVO(MediaFilePO mediaFile) {
|
||||
MediaFileVO mediaFileVO = new MediaFileVO();
|
||||
BeanUtils.copyProperties(mediaFile, mediaFileVO);
|
||||
return mediaFileVO;
|
||||
}
|
||||
}
|
||||
-380
@@ -1,380 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.media.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.mapper.MediaImageMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.UpdateImageDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.po.MediaImagePO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaImageVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.service.MediaImageService;
|
||||
import top.xyquan.ecomdmbbackend.utils.FileUploadUtils;
|
||||
import top.xyquan.ecomdmbbackend.utils.FileHashUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.dto.MediaImageFilterDTO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 图片资源池服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class MediaImageServiceImpl extends ServiceImpl<MediaImageMapper, MediaImagePO>
|
||||
implements MediaImageService {
|
||||
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public MediaImageServiceImpl(CurrentUserContext currentUserContext) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
* @param file 图片文件
|
||||
* @return 图片VO
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public MediaImageVO uploadImage(MultipartFile file) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 计算文件哈希
|
||||
String fileHash = FileHashUtils.calculateFileHash(file);
|
||||
|
||||
// 设置图片分类默认值
|
||||
String category = "image";
|
||||
|
||||
// 通过文件后缀判断图片格式
|
||||
String imageFormat = FileUploadUtils.getFileExtension(file.getOriginalFilename()).toLowerCase();
|
||||
|
||||
// 检查当前公司是否已有相同哈希的图片且文件存在
|
||||
List<MediaImagePO> existingImages = baseMapper.selectByFileHashes(List.of(fileHash));
|
||||
MediaImagePO existingImageWithValidFile = existingImages.stream()
|
||||
.filter(img -> companyId.equals(img.getCompanyId()) && FileHashUtils.fileExists(img.getUrl()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
if (existingImageWithValidFile != null) {
|
||||
// 当前公司已有相同图片且有效,直接返回现有记录
|
||||
return convertToVO(existingImageWithValidFile);
|
||||
}
|
||||
|
||||
// 检查是否有任何公司已有相同哈希的图片且文件存在(用于图片共享)
|
||||
MediaImagePO sharedImageWithValidFile = existingImages.stream()
|
||||
.filter(img -> FileHashUtils.fileExists(img.getUrl()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
String fileUrl;
|
||||
if (sharedImageWithValidFile != null) {
|
||||
// 其他公司已有相同图片且有效,共享图片URL
|
||||
fileUrl = sharedImageWithValidFile.getUrl();
|
||||
} else {
|
||||
// 没有任何公司有相同图片,保存新图片
|
||||
fileUrl = FileUploadUtils.uploadImage(file);
|
||||
}
|
||||
|
||||
// 创建新记录(即使是相同图片,不同公司也要创建新记录)
|
||||
MediaImagePO mediaImage = new MediaImagePO();
|
||||
mediaImage.setCompanyId(companyId);
|
||||
mediaImage.setUrl(fileUrl);
|
||||
mediaImage.setFilename(file.getOriginalFilename());
|
||||
mediaImage.setCategory(category);
|
||||
mediaImage.setImageFormat(imageFormat);
|
||||
mediaImage.setFileHash(fileHash);
|
||||
mediaImage.setUploadBy(currentUserContext.getCurrentUserId());
|
||||
|
||||
// 保存图片记录
|
||||
this.save(mediaImage);
|
||||
|
||||
// 转换为VO并返回
|
||||
return convertToVO(mediaImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量上传图片
|
||||
* @param files 图片文件列表
|
||||
* @return 图片VO列表
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public List<MediaImageVO> batchUploadImages(List<MultipartFile> files) {
|
||||
if (files == null || files.isEmpty()) {
|
||||
throw new BusinessException("图片列表不能为空");
|
||||
}
|
||||
|
||||
// 批量处理图片上传
|
||||
List<MediaImageVO> resultList = new ArrayList<>();
|
||||
for (MultipartFile file : files) {
|
||||
resultList.add(uploadImage(file));
|
||||
}
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新图片
|
||||
* @param id 图片ID
|
||||
* @param updateImageDTO 更新图片DTO
|
||||
* @return 图片VO
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public MediaImageVO updateImage(Long id, UpdateImageDTO updateImageDTO) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 检查图片是否存在且属于当前公司
|
||||
MediaImagePO existingImage = this.getById(id);
|
||||
if (existingImage == null) {
|
||||
throw new BusinessException("图片不存在");
|
||||
}
|
||||
|
||||
// 验证图片是否属于当前公司
|
||||
if (!companyId.equals(existingImage.getCompanyId())) {
|
||||
throw new BusinessException("无权操作此图片");
|
||||
}
|
||||
|
||||
// 更新图片信息(不更新分类和格式字段,这些字段在创建时确定后不可修改)
|
||||
MediaImagePO mediaImage = new MediaImagePO();
|
||||
mediaImage.setId(id);
|
||||
mediaImage.setTags(updateImageDTO.getTags());
|
||||
mediaImage.setRemark(updateImageDTO.getRemark());
|
||||
mediaImage.setUpdateBy(currentUserContext.getCurrentUserId()); // 记录更新人
|
||||
|
||||
// 更新图片记录
|
||||
this.updateById(mediaImage);
|
||||
|
||||
// 重新查询更新后的记录
|
||||
MediaImagePO updatedImage = this.getById(id);
|
||||
return convertToVO(updatedImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID查询图片
|
||||
* @param id 图片ID
|
||||
* @return 图片VO
|
||||
*/
|
||||
@Override
|
||||
public MediaImageVO getImageById(Long id) {
|
||||
MediaImagePO mediaImage = this.getById(id);
|
||||
if (mediaImage == null) {
|
||||
return null;
|
||||
}
|
||||
return convertToVO(mediaImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除图片
|
||||
* @param id 图片ID
|
||||
*/
|
||||
@Override
|
||||
public void deleteImage(Long id) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 检查图片是否存在且属于当前公司
|
||||
MediaImagePO mediaImage = this.getById(id);
|
||||
if (mediaImage == null) {
|
||||
throw new BusinessException("图片不存在");
|
||||
}
|
||||
|
||||
// 验证图片是否属于当前公司
|
||||
if (!companyId.equals(mediaImage.getCompanyId())) {
|
||||
throw new BusinessException("无权操作此图片");
|
||||
}
|
||||
|
||||
// 删除物理文件
|
||||
FileUploadUtils.deleteFile(mediaImage.getUrl());
|
||||
|
||||
// 删除图片记录
|
||||
this.removeById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件哈希查询图片
|
||||
* @param fileHash 文件哈希
|
||||
* @return 图片VO
|
||||
*/
|
||||
@Override
|
||||
public MediaImageVO getImageByHash(String fileHash) {
|
||||
MediaImagePO mediaImage = baseMapper.selectByFileHash(fileHash);
|
||||
if (mediaImage == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 检查文件是否真的存在
|
||||
if (!FileHashUtils.fileExists(mediaImage.getUrl())) {
|
||||
// 文件不存在,返回null但不删除记录,等待重新上传时更新
|
||||
return null;
|
||||
}
|
||||
|
||||
return convertToVO(mediaImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件哈希列表查询图片
|
||||
* @param fileHashes 文件哈希列表
|
||||
* @return 图片VO列表
|
||||
*/
|
||||
@Override
|
||||
public List<MediaImageVO> getImagesByHashes(List<String> fileHashes) {
|
||||
if (fileHashes == null || fileHashes.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
List<MediaImagePO> mediaImages = baseMapper.selectByFileHashes(fileHashes);
|
||||
|
||||
// 过滤掉文件不存在的记录
|
||||
List<MediaImageVO> resultList = mediaImages.stream()
|
||||
.filter(mediaImage -> FileHashUtils.fileExists(mediaImage.getUrl()))
|
||||
.map(this::convertToVO)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片分页列表
|
||||
* @param pageQueryDTO 分页查询DTO
|
||||
* @return 分页结果VO
|
||||
*/
|
||||
@Override
|
||||
public PageResultVO<MediaImageVO> getImagePage(PageQueryDTO<MediaImageFilterDTO> pageQueryDTO) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 创建分页对象
|
||||
Page<MediaImagePO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||
|
||||
// 构建查询条件
|
||||
LambdaQueryWrapper<MediaImagePO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(MediaImagePO::getCompanyId, companyId)
|
||||
.isNull(MediaImagePO::getDeleteTime);
|
||||
|
||||
// 添加过滤条件
|
||||
MediaImageFilterDTO filter = pageQueryDTO.getFilters();
|
||||
if (filter != null) {
|
||||
if (filter.getFilename() != null && !filter.getFilename().trim().isEmpty()) {
|
||||
wrapper.like(MediaImagePO::getFilename, filter.getFilename());
|
||||
}
|
||||
if (filter.getCategory() != null && !filter.getCategory().trim().isEmpty()) {
|
||||
wrapper.eq(MediaImagePO::getCategory, filter.getCategory());
|
||||
}
|
||||
if (filter.getImageFormat() != null && !filter.getImageFormat().trim().isEmpty()) {
|
||||
wrapper.eq(MediaImagePO::getImageFormat, filter.getImageFormat());
|
||||
}
|
||||
if (filter.getTags() != null && !filter.getTags().trim().isEmpty()) {
|
||||
wrapper.like(MediaImagePO::getTags, filter.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
// 排序
|
||||
wrapper.orderByDesc(MediaImagePO::getUploadTime);
|
||||
|
||||
// 执行分页查询
|
||||
IPage<MediaImagePO> pageResult = this.page(page, wrapper);
|
||||
|
||||
// 转换为VO
|
||||
List<MediaImageVO> voList = pageResult.getRecords().stream()
|
||||
.map(this::convertToVO)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 构建分页结果
|
||||
return PageResultVO.create(voList, pageResult.getTotal(), pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件哈希和分类查询图片
|
||||
* @param fileHash 文件哈希
|
||||
* @param type 分类
|
||||
* @return 图片VO
|
||||
*/
|
||||
@Override
|
||||
public MediaImageVO getImageByHashAndType(String fileHash, String type) {
|
||||
if (fileHash == null || fileHash.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<MediaImagePO> mediaImages = baseMapper.selectByFileHashes(List.of(fileHash));
|
||||
|
||||
// 查找指定分类且文件存在的图片
|
||||
MediaImagePO mediaImage = mediaImages.stream()
|
||||
.filter(img -> type.equals(img.getCategory()) && FileHashUtils.fileExists(img.getUrl()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
return mediaImage != null ? convertToVO(mediaImage) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传指定分类的图片
|
||||
* @param file 图片文件
|
||||
* @param category 分类
|
||||
* @return 图片VO
|
||||
*/
|
||||
@Override
|
||||
public MediaImageVO uploadImageWithType(MultipartFile file, String category) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 计算文件哈希
|
||||
String fileHash = FileHashUtils.calculateFileHash(file);
|
||||
|
||||
// 通过文件后缀判断图片格式
|
||||
String imageFormat = FileUploadUtils.getFileExtension(file.getOriginalFilename()).toLowerCase();
|
||||
|
||||
// 检查是否已有相同哈希和分类的图片(支持跨公司共享)
|
||||
MediaImageVO existingImage = getImageByHashAndType(fileHash, category);
|
||||
if (existingImage != null) {
|
||||
// 如果已存在,直接返回现有记录
|
||||
return existingImage;
|
||||
}
|
||||
|
||||
// 上传文件到对应目录
|
||||
String fileUrl;
|
||||
if ("avatar".equals(category)) {
|
||||
fileUrl = FileUploadUtils.uploadAvatar(file);
|
||||
} else if ("logo".equals(category)) {
|
||||
fileUrl = FileUploadUtils.uploadLogo(file);
|
||||
} else {
|
||||
fileUrl = FileUploadUtils.uploadImage(file);
|
||||
}
|
||||
|
||||
// 创建新记录
|
||||
MediaImagePO mediaImage = new MediaImagePO();
|
||||
mediaImage.setCompanyId(companyId);
|
||||
mediaImage.setUrl(fileUrl);
|
||||
mediaImage.setFilename(file.getOriginalFilename());
|
||||
mediaImage.setCategory(category);
|
||||
mediaImage.setImageFormat(imageFormat);
|
||||
mediaImage.setFileHash(fileHash);
|
||||
mediaImage.setUploadBy(currentUserContext.getCurrentUserId());
|
||||
|
||||
// 保存图片记录
|
||||
this.save(mediaImage);
|
||||
|
||||
// 转换为VO并返回
|
||||
return convertToVO(mediaImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为VO
|
||||
*/
|
||||
private MediaImageVO convertToVO(MediaImagePO mediaImage) {
|
||||
MediaImageVO mediaImageVO = new MediaImageVO();
|
||||
BeanUtils.copyProperties(mediaImage, mediaImageVO);
|
||||
return mediaImageVO;
|
||||
}
|
||||
}
|
||||
+11
-17
@@ -17,8 +17,8 @@ import top.xyquan.ecomdmbbackend.modules.org.company.model.dto.CompanyFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.company.model.po.CompanyPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.company.model.vo.CompanyVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.company.service.CompanyService;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.service.MediaImageService;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaImageVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.file.service.SysFileService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileUploadVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.promotion.service.PromotionFeeTypeService;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.companymenu.service.CompanyMenuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.department.service.DepartmentService;
|
||||
@@ -54,7 +54,7 @@ import java.util.stream.Collectors;
|
||||
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyPO> implements CompanyService {
|
||||
|
||||
private final CurrentUserContext currentUserContext;
|
||||
private final MediaImageService mediaImageService;
|
||||
private final SysFileService sysFileService;
|
||||
private final PromotionFeeTypeService promotionFeeTypeService;
|
||||
private final CompanyMenuService companyMenuService;
|
||||
private final DepartmentService departmentService;
|
||||
@@ -64,14 +64,14 @@ public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyPO> im
|
||||
private final CompanyMemberService companyMemberService;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
public CompanyServiceImpl(@Lazy CurrentUserContext currentUserContext, MediaImageService mediaImageService,
|
||||
public CompanyServiceImpl(@Lazy CurrentUserContext currentUserContext, SysFileService sysFileService,
|
||||
PromotionFeeTypeService promotionFeeTypeService,
|
||||
CompanyMenuService companyMenuService, DepartmentService departmentService,
|
||||
PositionService positionService, MenuService menuService,
|
||||
@Lazy UserService userService, @Lazy CompanyMemberService companyMemberService,
|
||||
PasswordEncoder passwordEncoder) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
this.mediaImageService = mediaImageService;
|
||||
this.sysFileService = sysFileService;
|
||||
this.promotionFeeTypeService = promotionFeeTypeService;
|
||||
this.companyMenuService = companyMenuService;
|
||||
this.departmentService = departmentService;
|
||||
@@ -103,11 +103,8 @@ public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyPO> im
|
||||
|
||||
// 处理logo图片ID
|
||||
if (createCompanyDTO.getLogoImageId() != null) {
|
||||
MediaImageVO logoImage = mediaImageService.getImageById(createCompanyDTO.getLogoImageId());
|
||||
if (logoImage != null) {
|
||||
company.setLogoImageId(createCompanyDTO.getLogoImageId());
|
||||
company.setLogo(logoImage.getUrl());
|
||||
}
|
||||
company.setLogoImageId(createCompanyDTO.getLogoImageId());
|
||||
company.setLogo(sysFileService.getFileAccessUrl(createCompanyDTO.getLogoImageId()));
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
@@ -192,11 +189,8 @@ public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyPO> im
|
||||
|
||||
// 处理logo图片ID
|
||||
if (updateCompanyDTO.getLogoImageId() != null) {
|
||||
MediaImageVO logoImage = mediaImageService.getImageById(updateCompanyDTO.getLogoImageId());
|
||||
if (logoImage != null) {
|
||||
company.setLogoImageId(updateCompanyDTO.getLogoImageId());
|
||||
company.setLogo(logoImage.getUrl());
|
||||
}
|
||||
company.setLogoImageId(updateCompanyDTO.getLogoImageId());
|
||||
company.setLogo(sysFileService.getFileAccessUrl(updateCompanyDTO.getLogoImageId()));
|
||||
}
|
||||
|
||||
// 设置更新人ID
|
||||
@@ -284,8 +278,8 @@ public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyPO> im
|
||||
}
|
||||
|
||||
// 上传Logo到媒体模块(支持哈希校验和共享)
|
||||
MediaImageVO mediaImageVO = mediaImageService.uploadImageWithType(file, "logo");
|
||||
String logoUrl = mediaImageVO.getUrl();
|
||||
List<SysFileUploadVO> uploaded = sysFileService.batchUploadFiles(java.util.List.of(file), "logo", company.getId());
|
||||
String logoUrl = uploaded.isEmpty() ? null : sysFileService.getFileAccessUrl(uploaded.get(0).getId());
|
||||
|
||||
// 更新公司Logo信息
|
||||
company.setLogo(logoUrl);
|
||||
|
||||
+10
@@ -35,6 +35,16 @@ public interface SysFileService {
|
||||
*/
|
||||
List<SysFileUploadVO> batchUploadFiles(List<MultipartFile> files, String category);
|
||||
|
||||
/**
|
||||
* 批量上传文件(指定所属公司)
|
||||
*
|
||||
* @param files 上传的文件列表
|
||||
* @param category 文件分类
|
||||
* @param companyId 所属公司ID(可为空)
|
||||
* @return 上传结果
|
||||
*/
|
||||
List<SysFileUploadVO> batchUploadFiles(List<MultipartFile> files, String category, Long companyId);
|
||||
|
||||
/**
|
||||
* 批量获取URL签名(用于前端上传到OSS)
|
||||
*
|
||||
|
||||
+61
-131
@@ -54,7 +54,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
|
||||
throw new BusinessException("文件列表不能为空");
|
||||
}
|
||||
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
List<SysFileUploadVO> result = new ArrayList<>();
|
||||
|
||||
@@ -88,7 +88,6 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
|
||||
// 检查是否有其他公司已有相同哈希的文件
|
||||
LambdaQueryWrapper<SysFilePO> otherCompanyWrapper = new LambdaQueryWrapper<>();
|
||||
otherCompanyWrapper.eq(SysFilePO::getFileHash, fileHash)
|
||||
.ne(SysFilePO::getCompanyId, companyId)
|
||||
.isNull(SysFilePO::getDeleteTime)
|
||||
.last("LIMIT 1");
|
||||
SysFilePO otherCompanyFile = this.getOne(otherCompanyWrapper);
|
||||
@@ -167,130 +166,61 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
|
||||
@Override
|
||||
@Transactional
|
||||
public List<SysFileUploadVO> batchUploadFiles(List<MultipartFile> files, String category) {
|
||||
if (files == null || files.isEmpty()) {
|
||||
throw new BusinessException("文件列表不能为空");
|
||||
}
|
||||
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
List<SysFileUploadVO> result = new ArrayList<>();
|
||||
|
||||
for (MultipartFile file : files) {
|
||||
if (file.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 计算文件哈希
|
||||
String fileHash = FileHashUtils.calculateFileHash(file);
|
||||
|
||||
// 检查当前公司是否已有相同哈希的文件
|
||||
LambdaQueryWrapper<SysFilePO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(SysFilePO::getCompanyId, companyId)
|
||||
.eq(SysFilePO::getFileHash, fileHash)
|
||||
.isNull(SysFilePO::getDeleteTime)
|
||||
.last("LIMIT 1");
|
||||
SysFilePO existingFile = this.getOne(wrapper);
|
||||
|
||||
if (existingFile != null) {
|
||||
// 当前公司已有相同文件,先验证文件是否真的存在可访问
|
||||
if (verifyFileExists(existingFile)) {
|
||||
// 如果文件已存在但category不同,更新category
|
||||
if (category != null && !category.equals(existingFile.getCategory())) {
|
||||
existingFile.setCategory(category);
|
||||
this.updateById(existingFile);
|
||||
}
|
||||
result.add(convertToUploadVO(existingFile));
|
||||
continue;
|
||||
} else {
|
||||
// 文件不存在,删除无效记录,继续处理
|
||||
this.removeById(existingFile.getId());
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否有其他公司已有相同哈希的文件
|
||||
LambdaQueryWrapper<SysFilePO> otherCompanyWrapper = new LambdaQueryWrapper<>();
|
||||
otherCompanyWrapper.eq(SysFilePO::getFileHash, fileHash)
|
||||
.ne(SysFilePO::getCompanyId, companyId)
|
||||
.isNull(SysFilePO::getDeleteTime)
|
||||
.last("LIMIT 1");
|
||||
SysFilePO otherCompanyFile = this.getOne(otherCompanyWrapper);
|
||||
|
||||
String storagePath = null;
|
||||
String storageType = "local"; // 上传接口始终使用本地存储
|
||||
|
||||
if (otherCompanyFile != null) {
|
||||
// 其他公司已有相同文件,先验证文件是否真的存在可访问
|
||||
if (verifyFileExists(otherCompanyFile)) {
|
||||
// 文件存在,复制存储路径和类型
|
||||
storagePath = otherCompanyFile.getStoragePath();
|
||||
storageType = otherCompanyFile.getStorageType();
|
||||
|
||||
// 如果是本地存储,需要重新上传到当前公司的存储路径
|
||||
if ("local".equals(storageType)) {
|
||||
String relativePath = generateStoragePath(file.getOriginalFilename(), fileHash);
|
||||
storagePath = "./" + relativePath;
|
||||
saveFileToLocal(storagePath, file);
|
||||
}
|
||||
// 如果是OSS存储,直接使用相同的存储路径(OSS文件可以跨公司共享)
|
||||
|
||||
// 创建当前公司的文件记录
|
||||
SysFilePO sysFile = new SysFilePO();
|
||||
BeanUtils.copyProperties(otherCompanyFile, sysFile);
|
||||
sysFile.setId(null);
|
||||
sysFile.setCompanyId(companyId);
|
||||
sysFile.setStoragePath(storagePath);
|
||||
sysFile.setStorageType(storageType);
|
||||
sysFile.setFilename(file.getOriginalFilename());
|
||||
sysFile.setFileFormat(getFileExtension(file.getOriginalFilename()).toLowerCase());
|
||||
sysFile.setMimeType(file.getContentType());
|
||||
sysFile.setFileSize(file.getSize());
|
||||
sysFile.setFileHash(fileHash);
|
||||
sysFile.setCategory(category); // 设置文件分类
|
||||
sysFile.setUploadBy(userId);
|
||||
sysFile.setUploadTime(LocalDateTime.now());
|
||||
sysFile.setUpdateTime(null);
|
||||
sysFile.setUpdateBy(null);
|
||||
sysFile.setDeleteTime(null);
|
||||
sysFile.setDeleteBy(null);
|
||||
|
||||
this.save(sysFile);
|
||||
result.add(convertToUploadVO(sysFile));
|
||||
} else {
|
||||
// 文件不存在,删除无效记录,继续处理(当作新文件处理)
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有找到有效的其他公司文件,或者文件不存在,则作为新文件处理
|
||||
if (storagePath == null) {
|
||||
// 没有任何公司有相同文件,生成新的存储路径并上传(仅本地存储)
|
||||
String relativePath = generateStoragePath(file.getOriginalFilename(), fileHash);
|
||||
storagePath = "./" + relativePath;
|
||||
saveFileToLocal(storagePath, file);
|
||||
|
||||
// 创建新记录
|
||||
SysFilePO sysFile = new SysFilePO();
|
||||
sysFile.setCompanyId(companyId);
|
||||
sysFile.setStorageType(storageType);
|
||||
sysFile.setStoragePath(storagePath);
|
||||
sysFile.setFilename(file.getOriginalFilename());
|
||||
sysFile.setFileFormat(getFileExtension(file.getOriginalFilename()).toLowerCase());
|
||||
sysFile.setMimeType(file.getContentType());
|
||||
sysFile.setFileSize(file.getSize());
|
||||
sysFile.setFileHash(fileHash);
|
||||
sysFile.setCategory(category); // 设置文件分类
|
||||
sysFile.setUploadBy(userId);
|
||||
|
||||
this.save(sysFile);
|
||||
result.add(convertToUploadVO(sysFile));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return batchUploadFiles(files, category, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public List<SysFileUploadVO> batchUploadFiles(List<MultipartFile> files, String category, Long overrideCompanyId) {
|
||||
if (files == null || files.isEmpty()) {
|
||||
throw new BusinessException("文件列表不能为空");
|
||||
}
|
||||
Long companyId = overrideCompanyId != null ? overrideCompanyId : (currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null);
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
List<SysFileUploadVO> result = new ArrayList<>();
|
||||
for (MultipartFile file : files) {
|
||||
if (file.isEmpty()) { continue; }
|
||||
String fileHash = FileHashUtils.calculateFileHash(file);
|
||||
// 查询当前公司是否已有相同哈希的文件
|
||||
LambdaQueryWrapper<SysFilePO> wrapper = new LambdaQueryWrapper<>();
|
||||
if (companyId != null) {
|
||||
wrapper.eq(SysFilePO::getCompanyId, companyId);
|
||||
} else {
|
||||
wrapper.isNull(SysFilePO::getCompanyId);
|
||||
}
|
||||
wrapper.eq(SysFilePO::getFileHash, fileHash)
|
||||
.isNull(SysFilePO::getDeleteTime)
|
||||
.last("LIMIT 1");
|
||||
SysFilePO existingFile = this.getOne(wrapper);
|
||||
if (existingFile != null && verifyFileExists(existingFile)) {
|
||||
result.add(convertToUploadVO(existingFile));
|
||||
continue;
|
||||
}
|
||||
String storageType = "local";
|
||||
String storagePath = "./" + generateStoragePath(file.getOriginalFilename(), fileHash);
|
||||
saveFileToLocal(storagePath, file);
|
||||
SysFilePO sysFile = new SysFilePO();
|
||||
sysFile.setCompanyId(companyId);
|
||||
sysFile.setStorageType(storageType);
|
||||
sysFile.setStoragePath(storagePath);
|
||||
sysFile.setFilename(file.getOriginalFilename());
|
||||
sysFile.setFileFormat(getFileExtension(file.getOriginalFilename()).toLowerCase());
|
||||
sysFile.setMimeType(file.getContentType());
|
||||
sysFile.setFileSize(file.getSize());
|
||||
sysFile.setFileHash(fileHash);
|
||||
sysFile.setCategory(category);
|
||||
sysFile.setUploadBy(userId);
|
||||
sysFile.setUploadTime(LocalDateTime.now());
|
||||
this.save(sysFile);
|
||||
result.add(convertToUploadVO(sysFile));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public BatchSignUrlVO batchSignUrl(BatchSignUrlDTO dto) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
|
||||
List<BatchSignUrlVO.FileSignInfo> fileSignInfos = new ArrayList<>();
|
||||
|
||||
for (BatchSignUrlDTO.FileItem item : dto.getFiles()) {
|
||||
@@ -402,7 +332,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
|
||||
throw new BusinessException("文件列表不能为空");
|
||||
}
|
||||
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
List<SysFileVO> result = new ArrayList<>();
|
||||
|
||||
@@ -555,7 +485,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
|
||||
throw new BusinessException("文件ID不能为空");
|
||||
}
|
||||
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
|
||||
|
||||
// 查询文件记录
|
||||
SysFilePO sysFile = this.getById(fileId);
|
||||
@@ -563,8 +493,8 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
|
||||
throw new BusinessException("文件不存在");
|
||||
}
|
||||
|
||||
// 验证文件是否属于当前公司
|
||||
if (!sysFile.getCompanyId().equals(companyId)) {
|
||||
// 验证文件是否属于当前公司(company_id为null的文件如用户头像等,不做公司校验)
|
||||
if (sysFile.getCompanyId() != null && !sysFile.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("无权访问该文件");
|
||||
}
|
||||
|
||||
@@ -603,7 +533,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
|
||||
@Override
|
||||
public PageResultVO<SysFileVO> getFilePage(PageQueryDTO<SysFileFilterDTO> pageQueryDTO) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
|
||||
|
||||
// 创建分页对象
|
||||
Page<SysFilePO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||
@@ -662,9 +592,9 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
|
||||
|
||||
@Override
|
||||
public void deleteFile(Long id) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
|
||||
SysFilePO file = this.getById(id);
|
||||
if (file == null || !file.getCompanyId().equals(companyId)) {
|
||||
if (file == null || (file.getCompanyId() != null && !file.getCompanyId().equals(companyId))) {
|
||||
throw new BusinessException("文件不存在");
|
||||
}
|
||||
// 删除数据库记录(物理文件由OSS生命周期或定时任务清理)
|
||||
@@ -673,9 +603,9 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFilePO> im
|
||||
|
||||
@Override
|
||||
public void updateFile(Long id, SysFileUpdateDTO dto) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null;
|
||||
SysFilePO file = this.getById(id);
|
||||
if (file == null || !file.getCompanyId().equals(companyId)) {
|
||||
if (file == null || (file.getCompanyId() != null && !file.getCompanyId().equals(companyId))) {
|
||||
throw new BusinessException("文件不存在");
|
||||
}
|
||||
// 更新元信息
|
||||
|
||||
+5
-5
@@ -71,18 +71,17 @@ public class UserMenuServiceImpl implements UserMenuService {
|
||||
|
||||
// 获取用户角色
|
||||
String userRole = user.getRole();
|
||||
// 获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
List<MenuVO> menus;
|
||||
|
||||
switch (userRole) {
|
||||
case "super":
|
||||
// 超级管理员获取所有系统菜单
|
||||
// 超级管理员获取所有系统菜单,不需要公司上下文
|
||||
menus = menuService.getAllMenus();
|
||||
break;
|
||||
case "admin":
|
||||
// 公司管理员获取公司所有菜单
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
menus = List.of();
|
||||
} else {
|
||||
@@ -91,12 +90,13 @@ public class UserMenuServiceImpl implements UserMenuService {
|
||||
break;
|
||||
case "user":
|
||||
// 普通用户,根据公司成员角色判断
|
||||
CompanyMemberPO member = companyMemberService.getByUserIdAndCompanyId(userId, companyId);
|
||||
Long companyIdForUser = currentUserContext.getCurrentCompanyId();
|
||||
CompanyMemberPO member = companyMemberService.getByUserIdAndCompanyId(userId, companyIdForUser);
|
||||
if (member == null) {
|
||||
menus = List.of();
|
||||
} else if (member.getRole() == CompanyMemberRole.owner) {
|
||||
// owner 角色获取公司所有菜单
|
||||
menus = companyMenuService.getCompanyMenusForRoute(companyId);
|
||||
menus = companyMenuService.getCompanyMenusForRoute(companyIdForUser);
|
||||
} else {
|
||||
// member 角色根据职位获取菜单
|
||||
if (member.getPositionId() == null) {
|
||||
|
||||
+9
-12
@@ -22,8 +22,8 @@ import top.xyquan.ecomdmbbackend.modules.org.company.model.po.CompanyPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.auth.model.vo.LoginVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.user.model.vo.UserVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.user.service.UserService;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.service.MediaImageService;
|
||||
import top.xyquan.ecomdmbbackend.modules.media.model.vo.MediaImageVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.file.service.SysFileService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileUploadVO;
|
||||
import top.xyquan.ecomdmbbackend.utils.JwtUtils;
|
||||
import top.xyquan.ecomdmbbackend.utils.SecurityUtils;
|
||||
import top.xyquan.ecomdmbbackend.utils.AccessTokenPayload;
|
||||
@@ -42,18 +42,18 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final JwtUtils jwtUtils;
|
||||
private final MediaImageService mediaImageService;
|
||||
private final SysFileService sysFileService;
|
||||
private final top.xyquan.ecomdmbbackend.modules.org.companymember.service.CompanyMemberService companyMemberService;
|
||||
private final top.xyquan.ecomdmbbackend.modules.org.departmentmember.service.DepartmentMemberService departmentMemberService;
|
||||
private final top.xyquan.ecomdmbbackend.modules.org.company.service.CompanyService companyService;
|
||||
|
||||
public UserServiceImpl(PasswordEncoder passwordEncoder, JwtUtils jwtUtils, MediaImageService mediaImageService,
|
||||
public UserServiceImpl(PasswordEncoder passwordEncoder, JwtUtils jwtUtils, SysFileService sysFileService,
|
||||
@Lazy top.xyquan.ecomdmbbackend.modules.org.companymember.service.CompanyMemberService companyMemberService,
|
||||
top.xyquan.ecomdmbbackend.modules.org.departmentmember.service.DepartmentMemberService departmentMemberService,
|
||||
top.xyquan.ecomdmbbackend.modules.org.company.service.CompanyService companyService) {
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.jwtUtils = jwtUtils;
|
||||
this.mediaImageService = mediaImageService;
|
||||
this.sysFileService = sysFileService;
|
||||
this.companyMemberService = companyMemberService;
|
||||
this.departmentMemberService = departmentMemberService;
|
||||
this.companyService = companyService;
|
||||
@@ -218,11 +218,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
|
||||
// 处理头像图片ID
|
||||
if (updateUserInfoDTO.getAvatarImageId() != null) {
|
||||
MediaImageVO avatarImage = mediaImageService.getImageById(updateUserInfoDTO.getAvatarImageId());
|
||||
if (avatarImage != null) {
|
||||
user.setAvatarImageId(updateUserInfoDTO.getAvatarImageId());
|
||||
user.setAvatar(avatarImage.getUrl());
|
||||
}
|
||||
user.setAvatarImageId(updateUserInfoDTO.getAvatarImageId());
|
||||
user.setAvatar(sysFileService.getFileAccessUrl(updateUserInfoDTO.getAvatarImageId()));
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(updateUserInfoDTO.getPhone())) {
|
||||
@@ -293,8 +290,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
}
|
||||
|
||||
// 上传头像到媒体模块(支持哈希校验和共享)
|
||||
MediaImageVO mediaImageVO = mediaImageService.uploadImageWithType(file, "avatar");
|
||||
String avatarUrl = mediaImageVO.getUrl();
|
||||
List<SysFileUploadVO> uploaded = sysFileService.batchUploadFiles(java.util.List.of(file), "avatar");
|
||||
String avatarUrl = uploaded.isEmpty() ? null : sysFileService.getFileAccessUrl(uploaded.get(0).getId());
|
||||
|
||||
// 更新用户头像信息
|
||||
user.setAvatar(avatarUrl);
|
||||
|
||||
Reference in New Issue
Block a user