|
@@ -1,9 +1,8 @@
|
|
|
package cn.cslg.pas.service.impl;
|
|
|
|
|
|
-import cn.cslg.pas.common.model.dto.StructureAddNewDTO;
|
|
|
-import cn.cslg.pas.common.model.dto.StructureQueryPageDTO;
|
|
|
-import cn.cslg.pas.common.model.dto.StructureUpdateDTO;
|
|
|
-import cn.cslg.pas.common.model.dto.UploadFileDTO;
|
|
|
+import cn.cslg.pas.common.model.dto.*;
|
|
|
+import cn.cslg.pas.common.model.vo.PathStructureNameVO;
|
|
|
+import cn.cslg.pas.common.model.vo.StructurePictureVO;
|
|
|
import cn.cslg.pas.common.model.vo.StructureVO;
|
|
|
import cn.cslg.pas.common.utils.FileUtils;
|
|
|
import cn.cslg.pas.domain.Structure;
|
|
@@ -12,6 +11,8 @@ import cn.cslg.pas.exception.XiaoShiException;
|
|
|
import cn.cslg.pas.mapper.AssoStructurePictureMapper;
|
|
|
import cn.cslg.pas.mapper.StructureMapper;
|
|
|
import cn.cslg.pas.service.IStructureService;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
@@ -19,7 +20,10 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 架构的Service层接口实现类
|
|
@@ -30,7 +34,7 @@ import java.util.List;
|
|
|
@RequiredArgsConstructor
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
-public class StructureServiceImpl implements IStructureService {
|
|
|
+public class StructureServiceImpl extends ServiceImpl<StructureMapper, Structure> implements IStructureService {
|
|
|
private final StructureMapper structureMapper;
|
|
|
private final AssoStructurePictureMapper assoStructurePictureMapper;
|
|
|
private final FileUtils fileUtils;
|
|
@@ -52,11 +56,11 @@ public class StructureServiceImpl implements IStructureService {
|
|
|
|
|
|
Integer parentId = structureAddNewDTO.getParentId();
|
|
|
String structureName = structureAddNewDTO.getStructureName();
|
|
|
- //检查名称是否被占用(检查当前父级下是否有同名架构)
|
|
|
+ //检查名称是否被占用(检查当前架构父级下是否有同名架构)
|
|
|
log.info("检查名称是否被占用(检查当前父级下是否有同名架构)");
|
|
|
- int count = structureMapper.countByparentIdAndStructureName(parentId, structureName);
|
|
|
+ int count = structureMapper.countByparentIdAndStructureName(parentId, structureName, null);
|
|
|
if (count > 0) {
|
|
|
- String message = "新增架构失败,当前父级下已存在【" + structureName + "】";
|
|
|
+ String message = "新增架构失败,当前父级下已存在【" + structureName + "】,请尝试更换名称";
|
|
|
log.info("{}", message);
|
|
|
throw new XiaoShiException(message);
|
|
|
}
|
|
@@ -123,6 +127,130 @@ public class StructureServiceImpl implements IStructureService {
|
|
|
*/
|
|
|
@Override
|
|
|
public void update(StructureUpdateDTO structureUpdateDTO, List<MultipartFile> files) {
|
|
|
+ log.info("开始处理【修改架构】的业务,参数为:{}, {}", structureUpdateDTO, files);
|
|
|
+
|
|
|
+ //检查尝试修改的数据是否存在
|
|
|
+ Integer structureId = structureUpdateDTO.getId();
|
|
|
+ log.info("检查尝试修改的数据是否存在");
|
|
|
+ Structure queryResult = structureMapper.getStandardById(structureId);
|
|
|
+ if (queryResult == null) {
|
|
|
+ String message = "修改架构失败,尝试访问的数据已不存在";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer oldParentId = queryResult.getParentId();
|
|
|
+ String oldPath = queryResult.getPath();
|
|
|
+ Integer newParentId = structureUpdateDTO.getParentId();
|
|
|
+ String newStructureName = structureUpdateDTO.getStructureName();
|
|
|
+ //检查名称是否被占用(检查当前尝试修改的架构父级下是否有同名架构)
|
|
|
+ log.info("检查名称是否被占用(检查当前尝试修改的架构父级下是否有同名架构)");
|
|
|
+ int count = structureMapper.countByparentIdAndStructureName(newParentId, newStructureName, structureId);
|
|
|
+ if (count > 0) {
|
|
|
+ String message = "修改架构失败,当前尝试修改的架构父级下已存在【" + newStructureName + "】,请尝试更换名称";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ //DTO对象赋值给实体类
|
|
|
+ Structure structure = new Structure();
|
|
|
+ BeanUtils.copyProperties(structureUpdateDTO, structure);
|
|
|
+ String parentPath = structureUpdateDTO.getParentPath();
|
|
|
+ if (parentPath == null) {
|
|
|
+ parentPath = "0";
|
|
|
+ }
|
|
|
+
|
|
|
+ //该架构新的path路径
|
|
|
+ String newPath = parentPath + "," + structureId;
|
|
|
+ //如果新路径和原路径不一样,则表示架构更换了父级
|
|
|
+ if (!oldParentId.equals(newParentId)) {
|
|
|
+ structure.setParentId(newParentId);
|
|
|
+ structure.setPath(newPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ //架构表修改数据
|
|
|
+ log.info("架构表修改数据");
|
|
|
+ int rows = structureMapper.update(structure);
|
|
|
+ if (rows != 1) {
|
|
|
+ String message = "修改架构失败,服务器忙请稍后再次尝试!";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果当前修改的架构更换了父级,则要将当前架构下所有子级架构的路径也都更新
|
|
|
+ if (!oldParentId.equals(newParentId)) {
|
|
|
+ //根据当前架构原来的路径查询所有子级架构
|
|
|
+ List<StructureVO> structures = structureMapper.selectByFindInSetPath(oldPath);
|
|
|
+ for (StructureVO n : structures) {
|
|
|
+ String path = n.getPath();
|
|
|
+ //从当前架构(即它们的父级架构)的原来的路径的长度的位置处截取后面路径
|
|
|
+ String behindString = path.substring(oldPath.length());
|
|
|
+ path = newPath + behindString;
|
|
|
+ n.setPath(path);
|
|
|
+ Structure structure2 = new Structure();
|
|
|
+ BeanUtils.copyProperties(n, structure2);
|
|
|
+ log.info("修改当前已更换父级的架构的子级架构路径path");
|
|
|
+ rows = structureMapper.update(structure2);
|
|
|
+ if (rows != 1) {
|
|
|
+ String message = "修改架构失败,修改当前已更换父级的架构的子级架构路径path失败,服务器忙请稍后再次尝试!";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //架构图片关联表删除数据(若有原有图片被删除)
|
|
|
+ //根据架构id查询出所有原有图片
|
|
|
+ log.info("根据架构id查询出所有原有图片");
|
|
|
+ List<StructurePictureVO> structurePictures = assoStructurePictureMapper.selectByStructureId(structureId);
|
|
|
+ ArrayList<Integer> oldPictureIds = new ArrayList<>();
|
|
|
+ for (StructurePictureVO structurePicture : structurePictures) {
|
|
|
+ Integer oldPictureId = structurePicture.getId();
|
|
|
+ oldPictureIds.add(oldPictureId);
|
|
|
+ }
|
|
|
+ //从DTO中获取传过来的原有图片
|
|
|
+ List<StructurePictureUpdateDTO> pictures = structureUpdateDTO.getPictures();
|
|
|
+ ArrayList<Integer> newOldPictureIds = new ArrayList<>();
|
|
|
+ for (StructurePictureUpdateDTO picture : pictures) {
|
|
|
+ Integer newOldPictureId = picture.getId();
|
|
|
+ newOldPictureIds.add(newOldPictureId);
|
|
|
+ }
|
|
|
+ //图片id集合去重,保留下来的即被删除的图片id
|
|
|
+ oldPictureIds.removeAll(newOldPictureIds);
|
|
|
+
|
|
|
+ if (oldPictureIds.size() > 0) {
|
|
|
+ log.info("架构图片关联表删除数据");
|
|
|
+ rows = assoStructurePictureMapper.deleteByIds(oldPictureIds);
|
|
|
+ if (rows != oldPictureIds.size()) {
|
|
|
+ String message = "修改架构失败,架构图片关联表删除数据失败,服务器忙请稍后再次尝试!";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //数据入架构图片关联表(若有新增新图片)
|
|
|
+ if (files != null && files.size() != 0) {
|
|
|
+ ArrayList<AssoStructurePicture> assoStructurePictures = new ArrayList<>();
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ UploadFileDTO fileDTO = fileUtils.uploadFile(file);
|
|
|
+ AssoStructurePicture assoStructurePicture = new AssoStructurePicture()
|
|
|
+ .setStructureId(structureId)
|
|
|
+ .setName(fileDTO.getName())
|
|
|
+ .setSuffix(fileDTO.getExtName())
|
|
|
+ .setUrl(fileDTO.getPath());
|
|
|
+ assoStructurePictures.add(assoStructurePicture);
|
|
|
+ }
|
|
|
+ log.info("数据入架构图片关联表");
|
|
|
+ rows = assoStructurePictureMapper.insertBatch(assoStructurePictures);
|
|
|
+ if (rows != files.size()) {
|
|
|
+ String message = "修改架构失败,数据入架构图片关联表失败,服务器忙请稍后再次尝试!";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("修改架构完成");
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -133,33 +261,137 @@ public class StructureServiceImpl implements IStructureService {
|
|
|
*/
|
|
|
@Override
|
|
|
public StructureVO query(StructureQueryPageDTO structureQueryPageDTO) {
|
|
|
- log.info("开始处理【分页查询架构】的业务,参数为:{}", structureQueryPageDTO);
|
|
|
-
|
|
|
- //取出DTO中structureId,若structureId为null则表示查询的是整个架构树,手动给structureId设为0
|
|
|
- if (structureQueryPageDTO.getStructureId() == null) {
|
|
|
- structureQueryPageDTO.setStructureId(0);
|
|
|
- }
|
|
|
-
|
|
|
- //从DTO中取出产品id、架构id、架构名称
|
|
|
+ log.info("开始处理【查询架构树】的业务,参数为:{}", structureQueryPageDTO);
|
|
|
+ StructureVO structureVO = new StructureVO();
|
|
|
+ //从DTO中取出产品id、架构id
|
|
|
Integer productId = structureQueryPageDTO.getProductId();
|
|
|
Integer structureId = structureQueryPageDTO.getStructureId();
|
|
|
+ if (structureId == null) {
|
|
|
+ structureId = 0;
|
|
|
+ }
|
|
|
String structureName = structureQueryPageDTO.getStructureName();
|
|
|
- StructureVO structureVO = new StructureVO()
|
|
|
- .setProductId(productId)
|
|
|
- .setId(structureId)
|
|
|
- .setStructureName(structureName);
|
|
|
- diGui(structureVO, structureId);
|
|
|
+ LambdaQueryWrapper<Structure> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ if (structureName != null) {
|
|
|
+ wrapper.like(Structure::getStructureName, structureQueryPageDTO.getStructureName());
|
|
|
+ }
|
|
|
+ List<Structure> structures = this.list(wrapper);
|
|
|
+ List<Integer> ids = structures.stream().map(Structure::getId).collect(Collectors.toList());
|
|
|
+ List<String> paths = structures.stream().map(Structure::getPath).collect(Collectors.toList());
|
|
|
+ paths.forEach(
|
|
|
+ item -> {
|
|
|
+ String[] pathStrs = item.split(",");
|
|
|
+ List<String> pathStr = new ArrayList<>(Arrays.asList(pathStrs));
|
|
|
+ pathStr.forEach(
|
|
|
+ tem -> {
|
|
|
+ Integer paId = Integer.parseInt(tem);
|
|
|
+ if (!ids.contains(paId)) {
|
|
|
+ ids.add(paId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+ );
|
|
|
+ if (ids.size() != 0) {
|
|
|
+ diGui(structureVO, structureId, productId, ids);
|
|
|
+ }
|
|
|
return structureVO;
|
|
|
}
|
|
|
|
|
|
- //递归组装架构集合树
|
|
|
- private void diGui(StructureVO structureVO, Integer structureId) {
|
|
|
- List<StructureVO> structureVOs = structureMapper.selectByParentId(structureId);
|
|
|
+ /**
|
|
|
+ * 查询所有架构的路径和路径拼接成的架构名称
|
|
|
+ *
|
|
|
+ * @return 返回查询到的数据
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<PathStructureNameVO> queryPathStructureName(Integer productId) {
|
|
|
+ log.info("开始处理【查询所有架构路径和路径架构名称】的业务,参数为:{}", productId);
|
|
|
+
|
|
|
+ ArrayList<PathStructureNameVO> pathStructureNames = new ArrayList<>();
|
|
|
+
|
|
|
+ log.info("根据产品id查询所有架构数据");
|
|
|
+ List<StructureVO> structures = structureMapper.selectAllByProductId(productId);
|
|
|
+ //map存储所有架构id(key)和架构名称(value)
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
+ for (StructureVO structure : structures) {
|
|
|
+ Integer structureId = structure.getId();
|
|
|
+ String structureStrId = structureId + "";
|
|
|
+ String structureName = structure.getStructureName();
|
|
|
+ map.put(structureStrId, structureName);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (StructureVO structure : structures) {
|
|
|
+ String path = structure.getPath();
|
|
|
+ //path = path.substring(path.indexOf(",") + 1);
|
|
|
+ String[] pathSplit = path.split(",");
|
|
|
+ //String pathStructureName = "";
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ for (int i = 1; i < pathSplit.length; i++) {
|
|
|
+ String structureName = map.get(pathSplit[i]);
|
|
|
+ //pathStructureName = pathStructureName + structureName + "/";
|
|
|
+ builder.append(structureName).append("/");
|
|
|
+ }
|
|
|
+ PathStructureNameVO pathStructureNameVO = new PathStructureNameVO()
|
|
|
+ .setPath(path)
|
|
|
+ .setPathStructureName(builder + "");
|
|
|
+ pathStructureNames.add(pathStructureNameVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ return pathStructureNames;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除架构
|
|
|
+ *
|
|
|
+ * @param id 架构id
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void delete(Integer id) {
|
|
|
+ log.info("开始处理【删除架构】的业务,参数为:{}", id);
|
|
|
+
|
|
|
+ //检查尝试删除的数据是否存在
|
|
|
+ Structure queryResult = structureMapper.getStandardById(id);
|
|
|
+ if (queryResult == null) {
|
|
|
+ String message = "删除架构失败,尝试访问的数据已不存在";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ ArrayList<Integer> structureIds = new ArrayList<>();
|
|
|
+ structureIds.add(id);
|
|
|
+ //根据架构路径查询其所有子级架构
|
|
|
+ String path = queryResult.getPath();
|
|
|
+ log.info("根据架构路径查询其所有子级架构");
|
|
|
+ List<StructureVO> structures = structureMapper.selectByFindInSetPath(path);
|
|
|
+ for (StructureVO structure : structures) {
|
|
|
+ Integer childStructureId = structure.getId();
|
|
|
+ structureIds.add(childStructureId);
|
|
|
+ }
|
|
|
+
|
|
|
+ //架构和专利关联表删除数据
|
|
|
+ log.info("架构和专利关联表删除数据");
|
|
|
+ assoStructurePictureMapper.deleteByIds(structureIds);
|
|
|
+
|
|
|
+ //架构和图片关联表删除数据
|
|
|
+ log.info("架构和图片关联表删除数据");
|
|
|
+ assoStructurePictureMapper.deleteByStructureIds(structureIds);
|
|
|
+
|
|
|
+ //架构表删除数据
|
|
|
+ log.info("架构表删除数据");
|
|
|
+ int rows = structureMapper.deleteByIds(structureIds);
|
|
|
+
|
|
|
+ log.info("删除架构完成");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //递归组装架构树集合
|
|
|
+ //递归组装架构树集合
|
|
|
+ private void diGui(StructureVO structureVO, Integer structureId, Integer productId, List<Integer> ids) {
|
|
|
+ List<StructureVO> structureVOs = structureMapper.selectByParentIdAndProductId(structureId, productId, ids);
|
|
|
if (structureVOs != null) {
|
|
|
structureVO.setChildren(structureVOs);
|
|
|
for (StructureVO n : structureVOs) {
|
|
|
structureId = n.getId();
|
|
|
- diGui(n, structureId);
|
|
|
+ diGui(n, structureId, productId, ids);
|
|
|
}
|
|
|
}
|
|
|
}
|