|
@@ -0,0 +1,237 @@
|
|
|
+package cn.cslg.pas.service.business;
|
|
|
+
|
|
|
+import cn.cslg.pas.common.dto.business.ProductMessageDTO;
|
|
|
+import cn.cslg.pas.common.model.cronModel.Personnel;
|
|
|
+import cn.cslg.pas.common.model.cronModel.PersonnelVO;
|
|
|
+import cn.cslg.pas.common.model.cronModel.SystemFile;
|
|
|
+import cn.cslg.pas.common.utils.CacheUtils;
|
|
|
+import cn.cslg.pas.common.utils.LoginUtils;
|
|
|
+import cn.cslg.pas.common.vo.business.ProductMessageVO;
|
|
|
+import cn.cslg.pas.domain.business.AssoProductMessageFile;
|
|
|
+import cn.cslg.pas.domain.business.ProductMessage;
|
|
|
+import cn.cslg.pas.exception.UnLoginException;
|
|
|
+import cn.cslg.pas.exception.XiaoShiException;
|
|
|
+import cn.cslg.pas.mapper.ProductMessageMapper;
|
|
|
+import cn.cslg.pas.service.common.FileManagerService;
|
|
|
+import cn.cslg.pas.service.permissions.PermissionService;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 标的产品信息的Service
|
|
|
+ * @Author xiexiang
|
|
|
+ * @Date 2023/12/8
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class ProductMessageService extends ServiceImpl<ProductMessageMapper, ProductMessage> {
|
|
|
+ @Autowired
|
|
|
+ private CacheUtils cacheUtils;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private LoginUtils loginUtils;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AssoProductMessageFileService assoProductMessageFileService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileManagerService fileManagerService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PermissionService permissionService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑or保存标的产品信息
|
|
|
+ * @param productMessageDTO
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Integer addOrUpdateProductMessage(ProductMessageDTO productMessageDTO){
|
|
|
+ //判空
|
|
|
+ if (productMessageDTO == null) {
|
|
|
+ throw new XiaoShiException("入参不能为空");
|
|
|
+ }
|
|
|
+ if (productMessageDTO.getProjectId() == null) {
|
|
|
+ throw new XiaoShiException("报告id不能为空!");
|
|
|
+ }
|
|
|
+ //获取登录人信息
|
|
|
+ PersonnelVO personnelVO = new PersonnelVO();
|
|
|
+ try {
|
|
|
+ personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new UnLoginException("未登录");
|
|
|
+ }
|
|
|
+ //判断产品id是否传入
|
|
|
+ if (productMessageDTO.getId() != null) {
|
|
|
+ //传入了id,执行更新操作
|
|
|
+ //根据id查询产品信息
|
|
|
+ ProductMessage productMessage = this.getById(productMessageDTO.getId());
|
|
|
+ BeanUtils.copyProperties(productMessageDTO, productMessage);
|
|
|
+ productMessage.updateById();
|
|
|
+ Integer productMessageId = productMessage.getId();
|
|
|
+ //如果文件集合不为空,则需要更新产品与文件关联表
|
|
|
+ if (productMessageDTO.getFileGuids() != null && !productMessageDTO.getFileGuids().isEmpty()) {
|
|
|
+ List<String> newFileGuids = productMessageDTO.getFileGuids();
|
|
|
+ //更新产品与文件关联表
|
|
|
+ this.saveAssoProductMessageFile(productMessageId, newFileGuids);
|
|
|
+ }
|
|
|
+ return productMessageId;
|
|
|
+ } else {
|
|
|
+ //未传入id,执行新增操作
|
|
|
+ ProductMessage productMessage = new ProductMessage();
|
|
|
+ BeanUtils.copyProperties(productMessageDTO, productMessage);
|
|
|
+ productMessage.setCreateId(personnelVO.getId());
|
|
|
+ productMessage.insert();
|
|
|
+ Integer productMessageId = productMessage.getId();
|
|
|
+ if (productMessageId != null) {
|
|
|
+ //如果文件集合不为空,需要插入产品与文件关联表
|
|
|
+ List<String> fileGuids = productMessageDTO.getFileGuids();
|
|
|
+ if (fileGuids != null && !fileGuids.isEmpty()) {
|
|
|
+ this.addAssoProductMessageFile(productMessageId, fileGuids);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new XiaoShiException("产品信息插入失败");
|
|
|
+ }
|
|
|
+ return productMessage.getId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新产品与文件关联
|
|
|
+ * @param productMessageId
|
|
|
+ * @param newFileGuids
|
|
|
+ */
|
|
|
+ public void saveAssoProductMessageFile(Integer productMessageId, List<String> newFileGuids){
|
|
|
+ List<String> oldFileGuids = new ArrayList<>();
|
|
|
+ //根据标的产品id查询出关联的文件集合
|
|
|
+ LambdaQueryWrapper<AssoProductMessageFile> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(AssoProductMessageFile::getProductMessageId, productMessageId);
|
|
|
+ List<AssoProductMessageFile> assoProductMessageFiles = assoProductMessageFileService.list(queryWrapper);
|
|
|
+ if (!assoProductMessageFiles.isEmpty()) {
|
|
|
+ oldFileGuids = assoProductMessageFiles.stream().map(AssoProductMessageFile::getFileGuid).collect(Collectors.toList());
|
|
|
+ //新旧文件集合作差,多余的新增,没有的删除;
|
|
|
+ List<String> filesToAdd = new ArrayList<>(newFileGuids);
|
|
|
+ List<String> filesToRemove = new ArrayList<>(oldFileGuids);
|
|
|
+ //需要新增的文件
|
|
|
+ filesToAdd.removeAll(oldFileGuids);
|
|
|
+ //需要删除的文件
|
|
|
+ filesToRemove.removeAll(newFileGuids);
|
|
|
+ //先删除
|
|
|
+ LambdaQueryWrapper<AssoProductMessageFile> deleteWrapper = new LambdaQueryWrapper<>();
|
|
|
+ deleteWrapper.in(AssoProductMessageFile::getFileGuid, filesToRemove);
|
|
|
+ assoProductMessageFileService.remove(deleteWrapper);
|
|
|
+ //后新增
|
|
|
+ List<AssoProductMessageFile> newAssoFile = new ArrayList<>();
|
|
|
+ filesToAdd.forEach(item -> {
|
|
|
+ AssoProductMessageFile assoProductMessageFile = new AssoProductMessageFile();
|
|
|
+ assoProductMessageFile.setFileGuid(item);
|
|
|
+ assoProductMessageFile.setProductMessageId(productMessageId);
|
|
|
+ newAssoFile.add(assoProductMessageFile);
|
|
|
+ });
|
|
|
+ assoProductMessageFileService.saveBatch(newAssoFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增产品与文件关联
|
|
|
+ * @param productMessageId
|
|
|
+ * @param fileGuids
|
|
|
+ */
|
|
|
+ public void addAssoProductMessageFile(Integer productMessageId, List<String> fileGuids){
|
|
|
+ List<AssoProductMessageFile> assoProductMessageFiles = new ArrayList<>();
|
|
|
+ fileGuids.forEach(item -> {
|
|
|
+ AssoProductMessageFile assoProductMessageFile = new AssoProductMessageFile();
|
|
|
+ assoProductMessageFile.setProductMessageId(productMessageId);
|
|
|
+ assoProductMessageFile.setFileGuid(item);
|
|
|
+ assoProductMessageFiles.add(assoProductMessageFile);
|
|
|
+ });
|
|
|
+ //批量保存产品与文件关联信息
|
|
|
+ assoProductMessageFileService.saveBatch(assoProductMessageFiles);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据projectId查询标的产品信息
|
|
|
+ * @param projectId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ProductMessageVO getProductMessage(Integer projectId) {
|
|
|
+ //新建返回VO
|
|
|
+ ProductMessageVO productMessageVO = new ProductMessageVO();
|
|
|
+ //根据projectId查询产品信息
|
|
|
+ LambdaQueryWrapper<ProductMessage> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(ProductMessage::getProjectId, projectId);
|
|
|
+ ProductMessage productMessage = this.getOne(queryWrapper, false);
|
|
|
+ if (productMessage != null) {
|
|
|
+ //复制给返回VO
|
|
|
+ BeanUtils.copyProperties(productMessage, productMessageVO);
|
|
|
+ //查询文件信息
|
|
|
+ List<String> fileGuids = this.getFileGuids(productMessageVO.getId());
|
|
|
+ if (!fileGuids.isEmpty()) {
|
|
|
+ List<SystemFile> systemFiles = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ String res = fileManagerService.getSystemFileFromFMS(fileGuids);
|
|
|
+ systemFiles = JSONObject.parseArray(res, SystemFile.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new XiaoShiException("查询文件详细信息错误" + e);
|
|
|
+ }
|
|
|
+ productMessageVO.setSystemFileList(systemFiles);
|
|
|
+ }
|
|
|
+ this.loadProductMessageVO(productMessageVO);
|
|
|
+ } else {
|
|
|
+ throw new XiaoShiException("未查询到产品");
|
|
|
+ }
|
|
|
+ return productMessageVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据产品id查询关联文件Guids
|
|
|
+ * @param productMessageId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<String> getFileGuids(Integer productMessageId){
|
|
|
+ LambdaQueryWrapper<AssoProductMessageFile> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(AssoProductMessageFile::getProductMessageId, productMessageId);
|
|
|
+ List<AssoProductMessageFile> assoProductMessageFiles = assoProductMessageFileService.list(queryWrapper);
|
|
|
+ List<String> fileGuids = new ArrayList<>();
|
|
|
+ if (!assoProductMessageFiles.isEmpty()) {
|
|
|
+ fileGuids = assoProductMessageFiles.stream().map(AssoProductMessageFile::getFileGuid).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return fileGuids;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 装载返回VO
|
|
|
+ * @param productMessageVO
|
|
|
+ */
|
|
|
+ public void loadProductMessageVO(ProductMessageVO productMessageVO){
|
|
|
+ List<String> createIds = new ArrayList<>();
|
|
|
+ createIds.add(productMessageVO.getCreateId());
|
|
|
+ List<Personnel> personnels = new ArrayList<>();
|
|
|
+ //查询创建人名称
|
|
|
+ if (createIds.size() != 0) {
|
|
|
+ try {
|
|
|
+ String res = permissionService.getPersonnelByIdsFromPCS(createIds);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new XiaoShiException("查询创建人名称错误");
|
|
|
+ }
|
|
|
+ //装载人员信息
|
|
|
+ Personnel personnel = personnels.stream().filter(item -> item.getId().equals(productMessageVO.getCreateId())).findFirst().orElse(null);
|
|
|
+ if (personnel != null) {
|
|
|
+ productMessageVO.setCreateName(personnel.getPersonnelName());
|
|
|
+ } else {
|
|
|
+ throw new XiaoShiException("未获取到当前登陆人信息");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|