123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package com.wtkj.service.impl;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.wtkj.dto.UploadFileDTO;
- import com.wtkj.dto.UploadInnerFileDTO;
- import com.wtkj.entity.FileAndFolder;
- import com.wtkj.entity.FileAndFolderHistory;
- import com.wtkj.entity.InnerFileAndFolder;
- import com.wtkj.service.ICommonService;
- import com.wtkj.service.IFileAndFolderHistoryService;
- import com.wtkj.service.IFileAndFolderService;
- import com.wtkj.service.IInnerFileAndFolderService;
- import com.wutong.file.feign.IFileClient;
- import com.wutong.file.vo.FileVO;
- import lombok.AllArgsConstructor;
- import org.springblade.core.log.exception.ServiceException;
- import org.springblade.core.secure.utils.AuthUtil;
- import org.springblade.core.tool.api.R;
- import org.springblade.core.tool.utils.Func;
- import org.springblade.core.tool.utils.StringUtil;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.CollectionUtils;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Objects;
- import static com.wtkj.config.MagicValue.TWO;
- /**
- * @author Blizzard
- * @create at 2023-09-14 16:07
- * @describe
- */
- @Service
- @AllArgsConstructor
- public class CommonServiceImpl implements ICommonService {
- private final IFileClient fileClient;
- private final IFileAndFolderService fileAndFolderService;
- private final IFileAndFolderHistoryService historyService;
- private final IInnerFileAndFolderService innerFileAndFolderService;
- @Override
- public boolean uploadFile(List<UploadFileDTO> files) {
- boolean flag = false;
- List<FileAndFolder> fileList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(files)) {
- Long projectId = files.get(0).getProjectId();
- Long parentId = files.get(0).getParentId();
- Long stageId = files.get(0).getStageId();
- files.forEach(file -> {
- Long fileId = file.getBladeFileId();
- R<FileVO> rpc = fileClient.findById(fileId);
- if (rpc.isSuccess()) {
- FileAndFolder newFile = new FileAndFolder();
- newFile.setProjectId(projectId);
- newFile.setStageId(stageId);
- newFile.setParentId(parentId);
- newFile.setBladeFileId(file.getBladeFileId());
- newFile.setType(1);
- newFile.setTitle(rpc.getData().getOriginalFileName());
- if (parentId != null && parentId > 0L) {
- FileAndFolder folder = fileAndFolderService.getById(parentId);
- if (folder != null) {
- newFile.setIsPrivate(folder.getIsPrivate());
- newFile.setPrivateUser(folder.getPrivateUser());
- }
- }
- fileList.add(newFile);
- }
- });
- flag = fileAndFolderService.saveBatch(fileList);
- }
- return flag;
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public boolean coverFile(List<UploadFileDTO> files) {
- boolean flag = false;
- List<FileAndFolder> fileList = new ArrayList<>();
- List<FileAndFolderHistory> histories = new ArrayList<>();
- if (!CollectionUtils.isEmpty(files)) {
- Long projectId = files.get(0).getProjectId();
- Long parentId = files.get(0).getParentId();
- Long stageId = files.get(0).getStageId();
- files.forEach(file -> {
- Long fileId = file.getBladeFileId();
- R<FileVO> rpc = fileClient.findById(fileId);
- if (rpc.isSuccess()) {
- //是否存在同名文件
- LambdaQueryWrapper<FileAndFolder> lqw = Wrappers.lambdaQuery();
- lqw.eq(FileAndFolder::getParentId, parentId);
- lqw.eq(FileAndFolder::getTitle, rpc.getData().getOriginalFileName());
- FileAndFolder old = fileAndFolderService.getOne(lqw);
- if (old != null) {
- //存在同名文件
- //此处不删除旧的同名文件,直接修改值 ---> 再保存历史记录信息 修改已存在的的数据 更新url 文件大小等信息
- //先根据旧的fileId判断是否存在历史记录
- List<FileAndFolderHistory> hisList = historyService.getByLatestId(old.getBladeFileId());
- if (!CollectionUtils.isEmpty(hisList)) {
- histories.forEach(h -> {
- h.setLatestId(fileId);
- histories.add(h);
- });
- }
- //历史文件和新上传文件不是同一个的时候才保存
- if (!fileId.equals(old.getBladeFileId())) {
- FileAndFolderHistory his = new FileAndFolderHistory();
- his.setLatestId(fileId);
- his.setHistoryId(old.getBladeFileId());
- hisList.add(his);
- }
- old.setBladeFileId(fileId);
- } else {
- FileAndFolder newFile = new FileAndFolder();
- newFile.setProjectId(projectId);
- newFile.setStageId(stageId);
- newFile.setParentId(parentId);
- newFile.setBladeFileId(file.getBladeFileId());
- newFile.setType(1);
- newFile.setTitle(rpc.getData().getOriginalFileName());
- if (parentId != null && parentId > 0L) {
- FileAndFolder folder = fileAndFolderService.getById(parentId);
- if (folder != null) {
- newFile.setIsPrivate(folder.getIsPrivate());
- newFile.setPrivateUser(folder.getPrivateUser());
- }
- }
- fileList.add(newFile);
- }
- }
- });
- if (!CollectionUtils.isEmpty(histories)) {
- historyService.saveOrUpdateBatch(histories);
- }
- flag = fileAndFolderService.saveOrUpdateBatch(fileList);
- }
- return flag;
- }
- @Override
- public boolean remove(List<Long> ids) {
- boolean flag = false;
- if (!CollectionUtils.isEmpty(ids)) {
- ids.forEach(id -> {
- FileAndFolder byId = fileAndFolderService.getById(id);
- //如果是文件夹
- if (Objects.equals(byId.getType(), TWO)) {
- List<FileAndFolder> children = fileAndFolderService.getChildren(null, id);
- if (!CollectionUtils.isEmpty(children)) {
- throw new ServiceException("删除文件夹失败:请先删除下级");
- }
- }
- });
- flag = fileAndFolderService.removeByIds(ids);
- }
- return flag;
- }
- @Override
- public boolean move(String ids, Long stageId, Long parentId) {
- boolean flag = false;
- if (StringUtil.isNotBlank(ids)) {
- List<FileAndFolder> list = new ArrayList<>();
- List<Long> idList = Func.toLongList(ids);
- idList.forEach(id -> {
- FileAndFolder byId = fileAndFolderService.getById(id);
- if (byId != null) {
- byId.setStageId(stageId);
- byId.setParentId(parentId);
- list.add(byId);
- }
- });
- flag = fileAndFolderService.updateBatchById(list);
- }
- return flag;
- }
- @Override
- public boolean delete(List<Long> ids) {
- boolean flag = false;
- Long deptId = Func.firstLong(AuthUtil.getDeptId());
- if (!CollectionUtils.isEmpty(ids)) {
- ids.forEach(id -> {
- InnerFileAndFolder byId = innerFileAndFolderService.getById(id);
- //如果是文件夹
- if (byId != null && Objects.equals(byId.getType(), TWO)) {
- if (!deptId.equals(byId.getDeptId())) {
- throw new ServiceException("非你部门下文件,禁止删除");
- }
- List<InnerFileAndFolder> children = innerFileAndFolderService.getChildren(byId.getDeptId(), id);
- if (!CollectionUtils.isEmpty(children)) {
- throw new ServiceException("删除文件夹失败:请先删除下级");
- }
- }
- });
- flag = innerFileAndFolderService.removeByIds(ids);
- }
- return flag;
- }
- @Override
- public boolean uploadInnerFile(List<UploadInnerFileDTO> files) {
- boolean flag = false;
- List<InnerFileAndFolder> fileList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(files)) {
- Long parentId = files.get(0).getParentId();
- Long deptId = files.get(0).getDeptId();
- files.forEach(file -> {
- Long fileId = file.getBladeFileId();
- R<FileVO> rpc = fileClient.findById(fileId);
- if (rpc.isSuccess()) {
- InnerFileAndFolder newFile = new InnerFileAndFolder();
- newFile.setDeptId(deptId);
- newFile.setParentId(parentId);
- newFile.setBladeFileId(file.getBladeFileId());
- newFile.setType(1);
- newFile.setTitle(rpc.getData().getOriginalFileName());
- fileList.add(newFile);
- }
- });
- flag = innerFileAndFolderService.saveBatch(fileList);
- }
- return flag;
- }
- }
|