|
@@ -0,0 +1,442 @@
|
|
|
+package org.springblade.business.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.util.ReflectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import org.springblade.business.entity.*;
|
|
|
+import org.springblade.business.service.StandardFileService;
|
|
|
+import org.springblade.business.dto.PrivateStandardDTO;
|
|
|
+import org.springblade.business.mapper.StandardInfoJoinMapper;
|
|
|
+import org.springblade.business.mapper.StandardInfoMapper;
|
|
|
+import org.springblade.business.mapper.StandardInfoPrivateJoinMapper;
|
|
|
+import org.springblade.business.service.PrivateStandardService;
|
|
|
+import org.springblade.business.mapper.PrivateStandardMapper;
|
|
|
+import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springblade.core.oss.model.BladeFile;
|
|
|
+import org.springblade.core.secure.BladeUser;
|
|
|
+import org.springblade.core.secure.utils.SecureUtil;
|
|
|
+import org.springblade.core.tool.utils.BeanUtil;
|
|
|
+import org.springblade.resource.feign.NewIOSSClient;
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.URL;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author LHB
|
|
|
+ * @description 针对表【u_wbs_private_standard(规范文件夹及规范文件表)】的数据库操作Service实现
|
|
|
+ * @createDate 2025-06-10 10:48:37
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMapper, PrivateStandard>
|
|
|
+ implements PrivateStandardService {
|
|
|
+ @Resource
|
|
|
+ private StandardInfoMapper standardInfoMapper;
|
|
|
+ @Resource
|
|
|
+ private StandardInfoJoinMapper standardInfoJoinMapper;
|
|
|
+ @Resource
|
|
|
+ private StandardInfoPrivateJoinMapper standardInfoPrivateJoinMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private StandardFileService standardFileService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对象存储构建类
|
|
|
+ */
|
|
|
+ @Resource
|
|
|
+ private NewIOSSClient newIOSSClient;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean insert(PrivateStandardDTO uWbsPrivateStandard) {
|
|
|
+ BladeUser user = SecureUtil.getUser();
|
|
|
+ uWbsPrivateStandard.setCreateUser(user.getUserId());
|
|
|
+ Boolean isUploadFile = false;
|
|
|
+ if (uWbsPrivateStandard.getType() == 2 && uWbsPrivateStandard.getFiles().length > 0) {
|
|
|
+ isUploadFile = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ //先上传文件,上传成功在执行添加
|
|
|
+ if (isUploadFile) {
|
|
|
+ List<StandardFile> standardFiles = new ArrayList<>();
|
|
|
+ MultipartFile[] files = uWbsPrivateStandard.getFiles();
|
|
|
+
|
|
|
+ try {
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+
|
|
|
+ StandardFile standardFile = new StandardFile();
|
|
|
+ standardFile.setId(SnowFlakeUtil.getId());
|
|
|
+ standardFile.setStandardId(uWbsPrivateStandard.getId());
|
|
|
+ standardFile.setCreateUser(user.getUserId());
|
|
|
+
|
|
|
+
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ standardFile.setFileName(originalFilename);
|
|
|
+
|
|
|
+
|
|
|
+ originalFilename = "standard/" + uWbsPrivateStandard.getId() + "|" + originalFilename;
|
|
|
+//
|
|
|
+// ReflectUtil.setFieldValue(file,"filename",originalFilename);
|
|
|
+
|
|
|
+ MockMultipartFile multipartFile = new MockMultipartFile("file", originalFilename, "application/pdf", file.getInputStream());
|
|
|
+ //Oss上传 传特殊文件名 在oss中做特殊路径处理
|
|
|
+ BladeFile bladeFile = newIOSSClient.uploadFileByInputStream(multipartFile);
|
|
|
+
|
|
|
+ standardFile.setStandardFileUrl(bladeFile.getLink());
|
|
|
+ standardFiles.add(standardFile);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ //删除之前上传的文件
|
|
|
+ for (StandardFile standardFile : standardFiles) {
|
|
|
+ String pdfName = standardFile.getStandardFileUrl().split("upload")[1];
|
|
|
+ this.newIOSSClient.removeFile("upload" + pdfName);
|
|
|
+ }
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new ServiceException("文件上传失败!请检查Oss");
|
|
|
+ }
|
|
|
+ standardFileService.saveBatch(standardFiles);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (uWbsPrivateStandard.getType() == 2) {
|
|
|
+ //修改之前的规则为过期
|
|
|
+ baseMapper.updateStatus(uWbsPrivateStandard.getParentId());
|
|
|
+ }
|
|
|
+ return baseMapper.insert(uWbsPrivateStandard) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean update(List<PrivateStandardDTO> privateStandards) {
|
|
|
+ BladeUser user = SecureUtil.getUser();
|
|
|
+
|
|
|
+ List<PrivateStandard> privateStandards1 = new ArrayList<>();
|
|
|
+
|
|
|
+ for (PrivateStandardDTO privateStandard : privateStandards) {
|
|
|
+ PrivateStandard privateStandard1 = BeanUtil.copyProperties(privateStandard, PrivateStandard.class);
|
|
|
+ privateStandards1.add(privateStandard1);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.updateBatchById(privateStandards1);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean delete(Long id) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ //获取当前用户
|
|
|
+ BladeUser user = SecureUtil.getUser();
|
|
|
+
|
|
|
+ //删除文件
|
|
|
+ List<StandardFile> list = standardFileService.list(Wrappers.<StandardFile>lambdaQuery()
|
|
|
+ .eq(StandardFile::getStandardId, id));
|
|
|
+ for (StandardFile standardFile : list) {
|
|
|
+ deleteFile(standardFile.getId());
|
|
|
+ }
|
|
|
+ //更新西悉尼
|
|
|
+ int update = baseMapper.update(null, Wrappers.<PrivateStandard>lambdaUpdate()
|
|
|
+ .set(PrivateStandard::getIsDeleted, 1)
|
|
|
+ .set(PrivateStandard::getUpdateUser, user.getUserId())
|
|
|
+ .eq(PrivateStandard::getId, id)
|
|
|
+ .or()
|
|
|
+ .eq(PrivateStandard::getParentId, id)
|
|
|
+ );
|
|
|
+
|
|
|
+ int update1 = standardInfoMapper.update(null, Wrappers.<StandardInfo>lambdaUpdate()
|
|
|
+ .set(StandardInfo::getIsDeleted, 1)
|
|
|
+ .set(StandardInfo::getUpdateUser, user.getUserId())
|
|
|
+ .eq(StandardInfo::getStandardId, id));
|
|
|
+ //TODO 还要删除关联信息
|
|
|
+ standardInfoJoinMapper.updateJoin(user.getUserId(), 1, id);
|
|
|
+ //TODo 还要删除与表单的关联信息
|
|
|
+ standardInfoPrivateJoinMapper.updateJoin(user.getUserId(), 1, id);
|
|
|
+
|
|
|
+ return update > 0;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new ServiceException("删除失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean deleteFile(Long id) {
|
|
|
+ StandardFile byId = standardFileService.getById(id);
|
|
|
+ if (byId != null) {
|
|
|
+ try {
|
|
|
+ String pdfName = byId.getStandardFileUrl().split("upload")[1];
|
|
|
+ this.newIOSSClient.removeFile("upload" + pdfName);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new ServiceException("文件删除失败,Oss异常");
|
|
|
+ }
|
|
|
+ standardFileService.update(Wrappers.<StandardFile>lambdaUpdate()
|
|
|
+ .set(StandardFile::getIsDeleted, 1)
|
|
|
+ .set(StandardFile::getUpdateUser, SecureUtil.getUser().getUserId())
|
|
|
+ .eq(StandardFile::getId, id));
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean updateTypeByTwo(List<PrivateStandardDTO> data,
|
|
|
+ List<Long> delIds,
|
|
|
+ List<Long> delFileIds,
|
|
|
+ MultipartFile[] allFiles) {
|
|
|
+ BladeUser user = SecureUtil.getUser();
|
|
|
+ //先删除文件
|
|
|
+ try {
|
|
|
+ if (CollectionUtils.isNotEmpty(delFileIds)) {
|
|
|
+ List<StandardFile> standardFiles = standardFileService.listByIds(delFileIds);
|
|
|
+ if (CollectionUtils.isNotEmpty(standardFiles)) {
|
|
|
+ for (StandardFile standardFile : standardFiles) {
|
|
|
+ this.deleteFile(standardFile.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //删除规范文件
|
|
|
+ if (CollectionUtils.isNotEmpty(delIds)) {
|
|
|
+ for (Long delId : delIds) {
|
|
|
+ delete(delId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new ServiceException("Oss删除文件失败");
|
|
|
+ }
|
|
|
+ //文件集合
|
|
|
+ List<StandardFile> standardFiles = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ //空文件曲中
|
|
|
+ List<MultipartFile> files = new ArrayList<>();
|
|
|
+ for (MultipartFile allFile : allFiles) {
|
|
|
+ if (allFile != null) {
|
|
|
+ files.add(allFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 文件索引计数器
|
|
|
+ int fileIndex = 0;
|
|
|
+ for (PrivateStandardDTO dto : data) {
|
|
|
+ // 获取当前对象需要的文件数量
|
|
|
+ int fileCount = dto.getFilesCount();
|
|
|
+ if (files != null && files.size() > 0) {
|
|
|
+ if (fileCount > 0 && fileIndex < files.size()) {
|
|
|
+ dto.setFile(files.get(fileIndex++));
|
|
|
+ }
|
|
|
+ //先上传文件,上传成功在执行添加
|
|
|
+ if (dto.getFile() != null) {
|
|
|
+ //再去查询当前规范文件是否在数据库中存在未删除的文件
|
|
|
+ List<StandardFile> list = standardFileService.list(Wrappers.<StandardFile>lambdaQuery()
|
|
|
+ .eq(StandardFile::getStandardId, dto.getId())
|
|
|
+ .eq(StandardFile::getIsDeleted, 0));
|
|
|
+ if (CollectionUtils.isNotEmpty(list)) {
|
|
|
+ //删除文件
|
|
|
+ for (StandardFile standardFile : list) {
|
|
|
+ String pdfName = standardFile.getStandardFileUrl().split("upload")[1];
|
|
|
+ this.newIOSSClient.removeFile("upload" + pdfName);
|
|
|
+ standardFile.setIsDeleted(1);
|
|
|
+ }
|
|
|
+ standardFileService.saveOrUpdateBatch(list);
|
|
|
+ }
|
|
|
+ MultipartFile file = dto.getFile();
|
|
|
+ StandardFile standardFile = new StandardFile();
|
|
|
+ standardFile.setId(SnowFlakeUtil.getId());
|
|
|
+ standardFile.setStandardId(dto.getId());
|
|
|
+ standardFile.setCreateUser(user.getUserId());
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ standardFile.setFileName(originalFilename);
|
|
|
+ originalFilename = "standard/" + dto.getId() + "|" + originalFilename;
|
|
|
+ MockMultipartFile multipartFile = new MockMultipartFile("file", originalFilename, "application/pdf", file.getInputStream());
|
|
|
+
|
|
|
+ //Oss上传 传特殊文件名 在oss中做特殊路径处理
|
|
|
+ BladeFile bladeFile = newIOSSClient.uploadFileByInputStream(multipartFile);
|
|
|
+ if (bladeFile == null) {
|
|
|
+ throw new ServiceException("Oss异常");
|
|
|
+ }
|
|
|
+ standardFile.setStandardFileUrl(bladeFile.getLink());
|
|
|
+ //添加新文件
|
|
|
+ standardFileService.save(standardFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //创建数据
|
|
|
+ PrivateStandard privateStandard = BeanUtil.copyProperties(dto, PrivateStandard.class);
|
|
|
+ //修改
|
|
|
+ baseMapper.updateById(privateStandard);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ //删除之前上传的文件
|
|
|
+ if (CollectionUtils.isNotEmpty(standardFiles)) {
|
|
|
+ for (StandardFile standardFile : standardFiles) {
|
|
|
+ String pdfName = standardFile.getStandardFileUrl().split("upload")[1];
|
|
|
+ this.newIOSSClient.removeFile("upload" + pdfName);
|
|
|
+ }
|
|
|
+ throw new ServiceException("Oss异常");
|
|
|
+ }
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new ServiceException("更新失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Long standardUpdate(Long id) {
|
|
|
+ BladeUser user = SecureUtil.getUser();
|
|
|
+ //获取当前规范文件信息
|
|
|
+ PrivateStandard privateStandard = baseMapper.selectById(id);
|
|
|
+ if(privateStandard == null){
|
|
|
+ throw new ServiceException("未查询到规范文件");
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取信息
|
|
|
+ List<StandardInfo> standardInfos = standardInfoMapper.selectList(Wrappers.<StandardInfo>lambdaQuery()
|
|
|
+ .eq(StandardInfo::getStandardId, id)
|
|
|
+ .eq(StandardInfo::getIsDeleted, 0));
|
|
|
+
|
|
|
+ List<Long> infoIds = null;
|
|
|
+ if (CollectionUtils.isNotEmpty(standardInfos)) {
|
|
|
+ infoIds = standardInfos.stream().filter(f -> f.getType() == 2).map(StandardInfo::getId).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ List<StandardInfoJoin> standardInfoJoins = null;
|
|
|
+ List<StandardInfoPrivateJoin> standardInfoPrivateJoins = null;
|
|
|
+ if (CollectionUtils.isNotEmpty(infoIds)) {
|
|
|
+ //获取绑定信息
|
|
|
+ standardInfoJoins = standardInfoJoinMapper.selectList(Wrappers.<StandardInfoJoin>lambdaQuery()
|
|
|
+ .in(StandardInfoJoin::getStandardInfoLeftId, infoIds));
|
|
|
+
|
|
|
+ //获取绑定表单信息
|
|
|
+ standardInfoPrivateJoins = standardInfoPrivateJoinMapper.selectList(Wrappers.<StandardInfoPrivateJoin>lambdaQuery()
|
|
|
+ .in(StandardInfoPrivateJoin::getStandardInfoId, infoIds));
|
|
|
+ }
|
|
|
+ //获取文件信息
|
|
|
+ List<StandardFile> standardFiles = standardFileService.list(Wrappers.<StandardFile>lambdaQuery()
|
|
|
+ .eq(StandardFile::getStandardId, id)
|
|
|
+ .eq(StandardFile::getIsDeleted, 0));
|
|
|
+
|
|
|
+
|
|
|
+ //复制数据
|
|
|
+
|
|
|
+ //新规范文件id
|
|
|
+ Long newId = SnowFlakeUtil.getId();
|
|
|
+
|
|
|
+ privateStandard.setId(newId);
|
|
|
+ if (privateStandard.getStatus() == 2) {
|
|
|
+ privateStandard.setName(privateStandard.getName().replace("-已过期", ""));
|
|
|
+ privateStandard.setStatus(1);
|
|
|
+ }
|
|
|
+ privateStandard.setCreateTime(LocalDateTime.now());
|
|
|
+ privateStandard.setCreateUser(user.getUserId());
|
|
|
+
|
|
|
+ try {
|
|
|
+ //修改之前的规则为过期
|
|
|
+ baseMapper.updateStatus(privateStandard.getParentId());
|
|
|
+
|
|
|
+ baseMapper.insert(privateStandard);
|
|
|
+
|
|
|
+ if(CollectionUtils.isNotEmpty(standardInfos)){
|
|
|
+ //旧id与新id的映射关系
|
|
|
+ Map<Long, Long> map = new HashMap<>();
|
|
|
+ standardInfos.forEach(f -> {
|
|
|
+ Long newInfoId = SnowFlakeUtil.getId();
|
|
|
+ map.put(f.getId(), newInfoId);
|
|
|
+ });
|
|
|
+ standardInfos.forEach(f -> {
|
|
|
+ f.setId(map.get(f.getId()));
|
|
|
+ f.setParentId(map.get(f.getParentId()) == null ? 0 : map.get(f.getParentId()));
|
|
|
+ f.setStandardId(newId);
|
|
|
+ f.setCreateTime(DateTime.now());
|
|
|
+ f.setCreateUser(user.getUserId());
|
|
|
+ standardInfoMapper.insert(f);
|
|
|
+ });
|
|
|
+ if(CollectionUtils.isNotEmpty(standardInfoJoins)){
|
|
|
+ standardInfoJoins.forEach(f -> {
|
|
|
+ f.setId(SnowFlakeUtil.getId());
|
|
|
+ f.setStandardInfoLeftId(map.get(f.getStandardInfoLeftId()));
|
|
|
+ f.setStandardInfoRightId(map.get(f.getStandardInfoRightId()));
|
|
|
+ f.setCreateTime(DateTime.now());
|
|
|
+ f.setCreateUser(user.getUserId());
|
|
|
+ standardInfoJoinMapper.insert(f);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if(CollectionUtils.isNotEmpty(standardInfoPrivateJoins)){
|
|
|
+ standardInfoPrivateJoins.forEach(f -> {
|
|
|
+ f.setId(SnowFlakeUtil.getId());
|
|
|
+ f.setStandardInfoId(map.get(f.getStandardInfoId()));
|
|
|
+ f.setCreateTime(DateTime.now());
|
|
|
+ f.setCreateUser(user.getUserId());
|
|
|
+ standardInfoPrivateJoinMapper.insert(f);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new ServiceException("复制失败");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if(CollectionUtils.isNotEmpty(standardFiles)){
|
|
|
+ //文件集合
|
|
|
+ List<String> urls = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ for (StandardFile f : standardFiles) {
|
|
|
+ f.setId(SnowFlakeUtil.getId());
|
|
|
+ f.setStandardId(newId);
|
|
|
+ f.setCreateTime(DateTime.now());
|
|
|
+ f.setCreateUser(user.getUserId());
|
|
|
+ if (StringUtils.isNotEmpty(f.getStandardFileUrl())) {
|
|
|
+ URL urlFile = new URL(f.getStandardFileUrl());
|
|
|
+ InputStream inputStream = urlFile.openStream();
|
|
|
+ String originalFilename = "standard/" + f.getId() + "|" + f.getFileName();
|
|
|
+ MockMultipartFile multipartFile = new MockMultipartFile("file", originalFilename, "application/pdf", inputStream);
|
|
|
+ //Oss上传 传特殊文件名 在oss中做特殊路径处理
|
|
|
+ BladeFile bladeFile = newIOSSClient.uploadFileByInputStream(multipartFile);
|
|
|
+ if (bladeFile == null) {
|
|
|
+ throw new ServiceException("Oss异常");
|
|
|
+ }
|
|
|
+ f.setStandardFileUrl(bladeFile.getLink());
|
|
|
+ urls.add(bladeFile.getLink());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ for (String url : urls) {
|
|
|
+ String pdfName = url.split("upload")[1];
|
|
|
+ this.newIOSSClient.removeFile("upload" + pdfName);
|
|
|
+ }
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new ServiceException("Oss异常");
|
|
|
+ }
|
|
|
+ standardFileService.saveBatch(standardFiles);
|
|
|
+ }
|
|
|
+ return newId;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|