package cn.cslg.pas.service.business; import cn.cslg.pas.common.dto.business.OtherPatentInfoDTO; import cn.cslg.pas.common.dto.business.OtherReferencesDTO; import cn.cslg.pas.common.model.cronModel.Personnel; import cn.cslg.pas.common.model.cronModel.PersonnelVO; import cn.cslg.pas.common.model.cronModel.Records; 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.utils.StringUtils; import cn.cslg.pas.common.vo.business.OtherReferencesVO; import cn.cslg.pas.domain.business.AssoOtherPatentInfoFile; import cn.cslg.pas.domain.business.OtherReferences; import cn.cslg.pas.domain.business.ReviewHistory; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.mapper.OtherReferencesMapper; 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.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.stereotype.Service; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * 其他参考资料 * @Author xiexiang * @Date 2024/1/17 */ @Slf4j @Service public class OtherReferencesService extends ServiceImpl { @Autowired private LoginUtils loginUtils; @Autowired private CacheUtils cacheUtils; @Autowired private PermissionService permissionService; @Autowired private AssoOtherPatentInfoFileService assoOtherPatentInfoFileService; @Autowired private FileManagerService fileManagerService; /** * 新增or更新其他参考资料 * @param otherReferencesDTO * @return */ public Integer saveOrUpdate(OtherReferencesDTO otherReferencesDTO){ if (otherReferencesDTO == null) { throw new XiaoShiException("入参为空"); } Integer id = otherReferencesDTO.getId(); List fileGuids = otherReferencesDTO.getFileGuids(); OtherReferences otherReferences = new OtherReferences(); Integer type = 1; if (id != null) { //update otherReferences = this.getById(id); BeanUtils.copyProperties(otherReferencesDTO, otherReferences); otherReferences.updateById(); //先删除 后添加 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AssoOtherPatentInfoFile::getOtherPatentInfoId, id) .eq(AssoOtherPatentInfoFile::getType, type); assoOtherPatentInfoFileService.remove(queryWrapper); if (fileGuids != null && !fileGuids.isEmpty()) { List fileIds = this.addFile(otherReferences.getId(), fileGuids, type); } } else { BeanUtils.copyProperties(otherReferencesDTO, otherReferences); PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new XiaoShiException("未查询到当前登陆人"); } otherReferences.setCreateId(personnelVO.getId()); otherReferences.setTenantId(personnelVO.getTenantId()); otherReferences.insert(); if (fileGuids != null && !fileGuids.isEmpty()) { //先删除 后添加 List fileIds = this.addFile(otherReferences.getId(), fileGuids, type); } } return otherReferences.getId(); } /** * 新增文件与其他参考资料关联 * @param id * @param fileGuids * @param type * @return */ public List addFile(Integer id, List fileGuids, Integer type){ List ids = new ArrayList<>(); if (!fileGuids.isEmpty()) { List assoOtherPatentInfoFiles = new ArrayList<>(); fileGuids.forEach(item -> { AssoOtherPatentInfoFile assoOtherPatentInfoFile = new AssoOtherPatentInfoFile(); assoOtherPatentInfoFile.setOtherPatentInfoId(id); assoOtherPatentInfoFile.setType(type); assoOtherPatentInfoFile.setFileGuid(item); assoOtherPatentInfoFiles.add(assoOtherPatentInfoFile); }); assoOtherPatentInfoFileService.saveBatch(assoOtherPatentInfoFiles); ids = assoOtherPatentInfoFiles.stream().map(AssoOtherPatentInfoFile::getId).collect(Collectors.toList()); } return ids; } /** * 分页查询 * @param otherPatentInfoDTO * @return * @throws IOException */ public Records getOtherReferences(OtherPatentInfoDTO otherPatentInfoDTO) throws IOException { List otherReferencesVOS = new ArrayList<>(); String patentNo = otherPatentInfoDTO.getPatentNo(); if (patentNo == null || StringUtils.isEmpty(patentNo)) { throw new XiaoShiException("入参为空"); } Integer current = otherPatentInfoDTO.getCurrent(); Integer size = otherPatentInfoDTO.getSize(); Page page = new Page<>(current, size); PersonnelVO personnelVO = new PersonnelVO(); try { personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); } catch (Exception e) { throw new XiaoShiException("未查询到当前登陆人"); } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(OtherReferences::getPatentNo, patentNo) .eq(OtherReferences::getTenantId, personnelVO.getTenantId()); IPage otherReferencesPage = this.page(page, queryWrapper); List otherReferencesList = otherReferencesPage.getRecords(); long total = otherReferencesPage.getTotal(); if (!otherReferencesList.isEmpty()) { otherReferencesList.forEach(item -> { OtherReferencesVO otherReferencesVO = new OtherReferencesVO(); BeanUtils.copyProperties(item, otherReferencesVO); otherReferencesVOS.add(otherReferencesVO); }); this.loadOtherReferencesVOS(otherReferencesVOS); } Records records = new Records(); records.setTotal(total); records.setCurrent((long)current); records.setSize((long) size); records.setData(otherReferencesVOS); return records; } /** * 装载查询其他参考资料 * @param otherReferencesVOS * @throws IOException */ public void loadOtherReferencesVOS(List otherReferencesVOS) throws IOException { List createIds = new ArrayList<>(); otherReferencesVOS.forEach(item -> { if (item.getCreateId() != null) { createIds.add(item.getCreateId()); } }); List personnels = new ArrayList<>(); //查询发起人名称 if (createIds.size() != 0) { String res = permissionService.getPersonnelByIdsFromPCS(createIds); JSONObject jsonObject = JSONObject.parseObject(res); personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class); } //装载信息 for (OtherReferencesVO otherReferencesVO : otherReferencesVOS) { //装载人员信息 Personnel personnel = personnels.stream().filter(item -> item.getId().equals(otherReferencesVO.getCreateId())).findFirst().orElse(null); if (personnel != null) { otherReferencesVO.setCreateName(personnel.getPersonnelName()); } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(AssoOtherPatentInfoFile::getOtherPatentInfoId, otherReferencesVO.getId()) .eq(AssoOtherPatentInfoFile::getType, 1); List assoOtherPatentInfoFiles = assoOtherPatentInfoFileService.list(queryWrapper); if (!assoOtherPatentInfoFiles.isEmpty()) { List fileGuids = assoOtherPatentInfoFiles.stream().map(AssoOtherPatentInfoFile::getFileGuid).collect(Collectors.toList()); otherReferencesVO.setFileGuids(fileGuids); List systemFiles = new ArrayList<>(); if (fileGuids.size() != 0) { String res = fileManagerService.getSystemFileFromFMS(fileGuids); systemFiles = JSONObject.parseArray(res, SystemFile.class); } if (!systemFiles.isEmpty()) { otherReferencesVO.setSystemFileList(systemFiles); } } } } /** * 删除其他参考资料 * @param ids * @return */ public List deleteOtherReferences(List ids) { if (!ids.isEmpty()) { //先删除附件 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(AssoOtherPatentInfoFile::getOtherPatentInfoId, ids) .eq(AssoOtherPatentInfoFile::getType, 1); assoOtherPatentInfoFileService.remove(queryWrapper); this.removeBatchByIds(ids); } return ids; } }