package cn.cslg.pas.service.business; import cn.cslg.pas.common.dto.business.GetReferencesDTO; import cn.cslg.pas.common.dto.business.ReferencesDTO; import cn.cslg.pas.common.dto.business.ReferencesUpdateDTO; 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.model.request.OrderDTO; import cn.cslg.pas.common.utils.CacheUtils; import cn.cslg.pas.common.utils.LoginUtils; import cn.cslg.pas.common.vo.business.ReferencesVO; import cn.cslg.pas.domain.business.Project; import cn.cslg.pas.domain.business.References; import cn.cslg.pas.domain.business.ReportProject; import cn.cslg.pas.exception.UnLoginException; import cn.cslg.pas.exception.XiaoShiException; import cn.cslg.pas.mapper.ReferencesMapper; 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.conditions.query.QueryWrapper; 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.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Author xiexiang * @Date 2023/12/19 */ @Slf4j @Service public class ReferencesService extends ServiceImpl { @Autowired private CacheUtils cacheUtils; @Autowired private LoginUtils loginUtils; @Autowired private FileManagerService fileManagerService; @Autowired private PermissionService permissionService; @Autowired private ProjectService projectService; /** * 上传报告文档 * * @param referencesDTO * @return */ public Integer add(ReferencesDTO referencesDTO) { if (referencesDTO != null) { //获取登录人信息 PersonnelVO personnelVO = new PersonnelVO(); personnelVO = cacheUtils.getLoginUser(loginUtils.getId()); References references = new References(); if (StringUtils.isEmpty(referencesDTO.getFileGuid())) { throw new XiaoShiException("附件不得为空"); } BeanUtils.copyProperties(referencesDTO, references); references.setCreateId(personnelVO.getId()); references.insert(); return references.getId(); } else { throw new XiaoShiException("入参为空"); } } /** * 更新 * * @param referencesUpdateDTO * @return */ public Integer update(ReferencesUpdateDTO referencesUpdateDTO) { if (referencesUpdateDTO != null) { Integer id = referencesUpdateDTO.getId(); References references = this.getById(id); BeanUtils.copyProperties(referencesUpdateDTO, references); references.updateById(); return references.getId(); } else { throw new XiaoShiException("入参为空"); } } /** * 删除 * * @param ids */ public void delete(List ids) { if (!ids.isEmpty()) { this.removeBatchByIds(ids); } } /** * 查询 * * @param getReferencesDTO * @return */ public Records query(GetReferencesDTO getReferencesDTO) { //初始化 Integer projectId = getReferencesDTO.getProjectId(); String fileName = getReferencesDTO.getFileName(); Integer pageNum = getReferencesDTO.getCurrent(); Integer pageSize = getReferencesDTO.getSize(); Integer id=getReferencesDTO.getId(); List orderDTOList = getReferencesDTO.getOrderDTOList(); //分页配置 Page page = new Page<>(pageNum, pageSize); QueryWrapper queryWrapper = new QueryWrapper<>(); if(projectId!=null) { queryWrapper.lambda().eq(References::getProjectId, projectId); } if (fileName != null && !fileName.equals("")) { queryWrapper.lambda().like(References::getReferencesName, fileName); } if(id!=null){ queryWrapper.lambda().eq(References::getId, id); } //添加排序逻辑 if (orderDTOList != null && !orderDTOList.isEmpty() && orderDTOList.size() == 1) { OrderDTO orderDTO = orderDTOList.get(0); String orderByField = orderDTO.getOrderBy(); String column = this.getColumns(orderByField); boolean isTrue = orderDTO.getOrderType() == 0 ? true : false; if (orderByField != null && !orderByField.equals("")) { queryWrapper.orderBy(true, isTrue, column); } } else { queryWrapper.orderBy(true, true, "create_time"); } IPage referencesPage = this.page(page, queryWrapper); List references = referencesPage.getRecords(); List referencesVOS = this.loadReferencesVO(references); Records records = new Records(); records.setData(referencesVOS); records.setCurrent((long) pageNum); records.setSize((long) pageSize); records.setTotal(referencesPage.getTotal()); return records; } public List loadReferencesVO(List references) { List referencesVOS = new ArrayList<>(); if (!references.isEmpty()) { List fileGuids = new ArrayList<>(); List createIds = new ArrayList<>(); List projectIds = new ArrayList<>(); references.forEach(item -> { if (item.getProjectId() != null) { projectIds.add(item.getProjectId()); } if (item.getFileGuid() != null) { fileGuids.add(item.getFileGuid()); } if (item.getCreateId() != null) { createIds.add(item.getCreateId()); } }); List systemFiles = new ArrayList<>(); if (!fileGuids.isEmpty()) { try { String res = fileManagerService.getSystemFileFromFMS(fileGuids); systemFiles = JSONObject.parseArray(res, SystemFile.class); } catch (Exception e) { throw new XiaoShiException("查询文件信息错误"); } } List personnels = new ArrayList<>(); if (!createIds.isEmpty()) { try { //查询创建人名称 if (createIds.size() != 0) { String res = permissionService.getPersonnelByIdsFromPCS(createIds); JSONObject jsonObject = JSONObject.parseObject(res); personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class); } } catch (Exception e) { throw new XiaoShiException("查询创建人信息错误"); } } List projects = new ArrayList<>(); if (!projectIds.isEmpty()) { LambdaQueryWrapper queryWrapper1 = new LambdaQueryWrapper<>(); queryWrapper1.in(Project::getId, projectIds); projects = projectService.list(queryWrapper1); } for (References item : references) { ReferencesVO referencesVO = new ReferencesVO(); BeanUtils.copyProperties(item, referencesVO); Project project = projects .stream() .filter(p -> p.getId() != null && p.getId().equals(item.getProjectId())) .findFirst() .orElse(null); if (project != null) { referencesVO.setProjectName(project.getName()); } if (!CollectionUtils.isEmpty(systemFiles)) { SystemFile systemFile = systemFiles .stream() .filter(file -> file.getGuid() != null && file.getGuid().equals(item.getFileGuid())) .findFirst() .orElse(null); if (systemFile != null) { referencesVO.setType(systemFile.getType()); } } Personnel personnel = personnels .stream() .filter(person -> person.getId() != null && person.getId().equals(item.getCreateId())) .findFirst() .orElse(null); if (personnel != null) { referencesVO.setCreateName(personnel.getPersonnelName()); } referencesVOS.add(referencesVO); } } return referencesVOS; } public String getColumns(String column) { Map map = new HashMap<>(); map.put("remark", "remark"); map.put("projectName", "project_id"); map.put("fileGuid", "file_guid"); map.put("referencesName", "references_name"); map.put("createName", "create_id"); map.put("createTime", "create_time"); String reStr = map.get(column); return reStr; } }