ReferencesService.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.dto.business.GetReferencesDTO;
  3. import cn.cslg.pas.common.dto.business.ReferencesDTO;
  4. import cn.cslg.pas.common.dto.business.ReferencesUpdateDTO;
  5. import cn.cslg.pas.common.model.cronModel.Personnel;
  6. import cn.cslg.pas.common.model.cronModel.PersonnelVO;
  7. import cn.cslg.pas.common.model.cronModel.Records;
  8. import cn.cslg.pas.common.model.cronModel.SystemFile;
  9. import cn.cslg.pas.common.model.request.OrderDTO;
  10. import cn.cslg.pas.common.utils.CacheUtils;
  11. import cn.cslg.pas.common.utils.LoginUtils;
  12. import cn.cslg.pas.common.vo.business.ReferencesVO;
  13. import cn.cslg.pas.domain.business.Project;
  14. import cn.cslg.pas.domain.business.References;
  15. import cn.cslg.pas.domain.business.ReportProject;
  16. import cn.cslg.pas.exception.UnLoginException;
  17. import cn.cslg.pas.exception.XiaoShiException;
  18. import cn.cslg.pas.mapper.ReferencesMapper;
  19. import cn.cslg.pas.service.common.FileManagerService;
  20. import cn.cslg.pas.service.permissions.PermissionService;
  21. import com.alibaba.fastjson.JSONObject;
  22. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  23. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  24. import com.baomidou.mybatisplus.core.metadata.IPage;
  25. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  26. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  27. import lombok.extern.slf4j.Slf4j;
  28. import org.apache.commons.lang3.StringUtils;
  29. import org.springframework.beans.BeanUtils;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.stereotype.Service;
  32. import org.springframework.util.CollectionUtils;
  33. import java.util.ArrayList;
  34. import java.util.HashMap;
  35. import java.util.List;
  36. import java.util.Map;
  37. /**
  38. * @Author xiexiang
  39. * @Date 2023/12/19
  40. */
  41. @Slf4j
  42. @Service
  43. public class ReferencesService extends ServiceImpl<ReferencesMapper, References> {
  44. @Autowired
  45. private CacheUtils cacheUtils;
  46. @Autowired
  47. private LoginUtils loginUtils;
  48. @Autowired
  49. private FileManagerService fileManagerService;
  50. @Autowired
  51. private PermissionService permissionService;
  52. @Autowired
  53. private ProjectService projectService;
  54. /**
  55. * 上传报告文档
  56. * @param referencesDTO
  57. * @return
  58. */
  59. public Integer add(ReferencesDTO referencesDTO){
  60. if (referencesDTO != null) {
  61. //获取登录人信息
  62. PersonnelVO personnelVO = new PersonnelVO();
  63. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  64. References references = new References();
  65. if (StringUtils.isEmpty(referencesDTO.getFileGuid())) {
  66. throw new XiaoShiException("附件不得为空");
  67. }
  68. BeanUtils.copyProperties(referencesDTO, references);
  69. references.setCreateId(personnelVO.getId());
  70. references.insert();
  71. return references.getId();
  72. } else {
  73. throw new XiaoShiException("入参为空");
  74. }
  75. }
  76. /**
  77. * 更新
  78. * @param referencesUpdateDTO
  79. * @return
  80. */
  81. public Integer update(ReferencesUpdateDTO referencesUpdateDTO){
  82. if (referencesUpdateDTO != null) {
  83. Integer id = referencesUpdateDTO.getId();
  84. References references = this.getById(id);
  85. BeanUtils.copyProperties(referencesUpdateDTO, references);
  86. references.updateById();
  87. return references.getId();
  88. } else {
  89. throw new XiaoShiException("入参为空");
  90. }
  91. }
  92. /**
  93. * 删除
  94. * @param ids
  95. */
  96. public void delete(List<Integer> ids){
  97. if (!ids.isEmpty()) {
  98. this.removeBatchByIds(ids);
  99. }
  100. }
  101. /**
  102. * 查询
  103. * @param getReferencesDTO
  104. * @return
  105. */
  106. public Records query(GetReferencesDTO getReferencesDTO) {
  107. //初始化
  108. Integer projectId = getReferencesDTO.getProjectId();
  109. String fileName = getReferencesDTO.getFileName();
  110. Integer pageNum = getReferencesDTO.getCurrent();
  111. Integer pageSize = getReferencesDTO.getSize();
  112. List<OrderDTO> orderDTOList = getReferencesDTO.getOrderDTOList();
  113. //分页配置
  114. Page<References> page = new Page<>(pageNum, pageSize);
  115. QueryWrapper<References> queryWrapper = new QueryWrapper<>();
  116. queryWrapper.lambda().eq(References::getProjectId, projectId);
  117. if (fileName != null && !fileName.equals("")) {
  118. queryWrapper.lambda().like(References::getReferencesName, fileName);
  119. }
  120. //添加排序逻辑
  121. if (!orderDTOList.isEmpty() && orderDTOList.size() == 1) {
  122. OrderDTO orderDTO = orderDTOList.get(0);
  123. String orderByField = orderDTO.getOrderBy();
  124. String column = this.getColumns(orderByField);
  125. boolean isTrue = orderDTO.getOrderType() == 0 ? true : false;
  126. if (orderByField != null && !orderByField.equals("")) {
  127. queryWrapper.orderBy(true, isTrue, column);
  128. }
  129. } else {
  130. queryWrapper.orderBy(true, true, "create_time");
  131. }
  132. IPage<References> referencesPage = this.page(page, queryWrapper);
  133. List<References> references = referencesPage.getRecords();
  134. List<ReferencesVO> referencesVOS = this.loadReferencesVO(references);
  135. Records records = new Records();
  136. records.setData(referencesVOS);
  137. records.setCurrent((long)pageNum);
  138. records.setSize((long)pageSize);
  139. records.setTotal(referencesPage.getTotal());
  140. return records;
  141. }
  142. public List<ReferencesVO> loadReferencesVO(List<References> references){
  143. List<ReferencesVO> referencesVOS = new ArrayList<>();
  144. if (!references.isEmpty()) {
  145. List<String> fileGuids = new ArrayList<>();
  146. List<String> createIds = new ArrayList<>();
  147. List<Integer> projectIds = new ArrayList<>();
  148. references.forEach(item -> {
  149. if (item.getProjectId() != null) {
  150. projectIds.add(item.getProjectId());
  151. }
  152. if (item.getFileGuid() != null) {
  153. fileGuids.add(item.getFileGuid());
  154. }
  155. if (item.getCreateId() != null) {
  156. createIds.add(item.getCreateId());
  157. }
  158. });
  159. if (!fileGuids.isEmpty()) {
  160. List<Personnel> personnels = new ArrayList<>();
  161. List<SystemFile> systemFiles = new ArrayList<>();
  162. List<Project> projects = new ArrayList<>();
  163. try {
  164. //查询创建人名称
  165. if (createIds.size() != 0) {
  166. String res = permissionService.getPersonnelByIdsFromPCS(createIds);
  167. JSONObject jsonObject = JSONObject.parseObject(res);
  168. personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class);
  169. }
  170. } catch (Exception e) {
  171. throw new XiaoShiException("查询创建人信息错误");
  172. }
  173. try {
  174. String res = fileManagerService.getSystemFileFromFMS(fileGuids);
  175. systemFiles = JSONObject.parseArray(res, SystemFile.class);
  176. } catch (Exception e) {
  177. throw new XiaoShiException("查询文件信息错误");
  178. }
  179. LambdaQueryWrapper<Project> queryWrapper1 = new LambdaQueryWrapper<>();
  180. queryWrapper1.in(Project::getId, projectIds);
  181. projects = projectService.list(queryWrapper1);
  182. for (References item : references) {
  183. ReferencesVO referencesVO = new ReferencesVO();
  184. BeanUtils.copyProperties(item, referencesVO);
  185. Project project = projects
  186. .stream()
  187. .filter(p -> p.getId() != null && p.getId().equals(item.getProjectId()))
  188. .findFirst()
  189. .orElse(null);
  190. if (project !=null) {
  191. referencesVO.setProjectName(project.getName());
  192. }
  193. if (!CollectionUtils.isEmpty(systemFiles)) {
  194. SystemFile systemFile = systemFiles
  195. .stream()
  196. .filter(file -> file.getGuid() != null && file.getGuid().equals(item.getFileGuid()))
  197. .findFirst()
  198. .orElse(null);
  199. if (systemFile != null) {
  200. referencesVO.setType(systemFile.getType());
  201. }
  202. }
  203. Personnel personnel = personnels
  204. .stream()
  205. .filter(person -> person.getId() != null && person.getId().equals(item.getCreateId()))
  206. .findFirst()
  207. .orElse(null);
  208. if (personnel != null) {
  209. referencesVO.setCreateName(personnel.getPersonnelName());
  210. }
  211. referencesVOS.add(referencesVO);
  212. }
  213. }
  214. }
  215. return referencesVOS;
  216. }
  217. public String getColumns(String column){
  218. Map<String, String> map = new HashMap<>();
  219. map.put("remark","remark");
  220. map.put("projectName","project_id");
  221. map.put("fileGuid","file_guid");
  222. map.put("referencesName","references_name");
  223. map.put("createName","create_id");
  224. map.put("createTime","create_time");
  225. String reStr = map.get(column);
  226. return reStr;
  227. }
  228. }