ReferencesService.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. *
  57. * @param referencesDTO
  58. * @return
  59. */
  60. public Integer add(ReferencesDTO referencesDTO) {
  61. if (referencesDTO != null) {
  62. //获取登录人信息
  63. PersonnelVO personnelVO = new PersonnelVO();
  64. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  65. References references = new References();
  66. if (StringUtils.isEmpty(referencesDTO.getFileGuid())) {
  67. throw new XiaoShiException("附件不得为空");
  68. }
  69. BeanUtils.copyProperties(referencesDTO, references);
  70. references.setCreateId(personnelVO.getId());
  71. references.insert();
  72. return references.getId();
  73. } else {
  74. throw new XiaoShiException("入参为空");
  75. }
  76. }
  77. /**
  78. * 更新
  79. *
  80. * @param referencesUpdateDTO
  81. * @return
  82. */
  83. public Integer update(ReferencesUpdateDTO referencesUpdateDTO) {
  84. if (referencesUpdateDTO != null) {
  85. Integer id = referencesUpdateDTO.getId();
  86. References references = this.getById(id);
  87. BeanUtils.copyProperties(referencesUpdateDTO, references);
  88. references.updateById();
  89. return references.getId();
  90. } else {
  91. throw new XiaoShiException("入参为空");
  92. }
  93. }
  94. /**
  95. * 删除
  96. *
  97. * @param ids
  98. */
  99. public void delete(List<Integer> ids) {
  100. if (!ids.isEmpty()) {
  101. this.removeBatchByIds(ids);
  102. }
  103. }
  104. /**
  105. * 查询
  106. *
  107. * @param getReferencesDTO
  108. * @return
  109. */
  110. public Records query(GetReferencesDTO getReferencesDTO) {
  111. //初始化
  112. Integer projectId = getReferencesDTO.getProjectId();
  113. String fileName = getReferencesDTO.getFileName();
  114. Integer pageNum = getReferencesDTO.getCurrent();
  115. Integer pageSize = getReferencesDTO.getSize();
  116. Integer id=getReferencesDTO.getId();
  117. List<OrderDTO> orderDTOList = getReferencesDTO.getOrderDTOList();
  118. //分页配置
  119. Page<References> page = new Page<>(pageNum, pageSize);
  120. QueryWrapper<References> queryWrapper = new QueryWrapper<>();
  121. if(projectId!=null) {
  122. queryWrapper.lambda().eq(References::getProjectId, projectId);
  123. }
  124. if (fileName != null && !fileName.equals("")) {
  125. queryWrapper.lambda().like(References::getReferencesName, fileName);
  126. }
  127. if(id!=null){
  128. queryWrapper.lambda().eq(References::getId, id);
  129. }
  130. //添加排序逻辑
  131. if (orderDTOList != null && !orderDTOList.isEmpty() && orderDTOList.size() == 1) {
  132. OrderDTO orderDTO = orderDTOList.get(0);
  133. String orderByField = orderDTO.getOrderBy();
  134. String column = this.getColumns(orderByField);
  135. boolean isTrue = orderDTO.getOrderType() == 0 ? true : false;
  136. if (orderByField != null && !orderByField.equals("")) {
  137. queryWrapper.orderBy(true, isTrue, column);
  138. }
  139. } else {
  140. queryWrapper.orderBy(true, true, "create_time");
  141. }
  142. IPage<References> referencesPage = this.page(page, queryWrapper);
  143. List<References> references = referencesPage.getRecords();
  144. List<ReferencesVO> referencesVOS = this.loadReferencesVO(references);
  145. Records records = new Records();
  146. records.setData(referencesVOS);
  147. records.setCurrent((long) pageNum);
  148. records.setSize((long) pageSize);
  149. records.setTotal(referencesPage.getTotal());
  150. return records;
  151. }
  152. public List<ReferencesVO> loadReferencesVO(List<References> references) {
  153. List<ReferencesVO> referencesVOS = new ArrayList<>();
  154. if (!references.isEmpty()) {
  155. List<String> fileGuids = new ArrayList<>();
  156. List<String> createIds = new ArrayList<>();
  157. List<Integer> projectIds = new ArrayList<>();
  158. references.forEach(item -> {
  159. if (item.getProjectId() != null) {
  160. projectIds.add(item.getProjectId());
  161. }
  162. if (item.getFileGuid() != null) {
  163. fileGuids.add(item.getFileGuid());
  164. }
  165. if (item.getCreateId() != null) {
  166. createIds.add(item.getCreateId());
  167. }
  168. });
  169. List<SystemFile> systemFiles = new ArrayList<>();
  170. if (!fileGuids.isEmpty()) {
  171. try {
  172. String res = fileManagerService.getSystemFileFromFMS(fileGuids);
  173. systemFiles = JSONObject.parseArray(res, SystemFile.class);
  174. } catch (Exception e) {
  175. throw new XiaoShiException("查询文件信息错误");
  176. }
  177. }
  178. List<Personnel> personnels = new ArrayList<>();
  179. if (!createIds.isEmpty()) {
  180. try {
  181. //查询创建人名称
  182. if (createIds.size() != 0) {
  183. String res = permissionService.getPersonnelByIdsFromPCS(createIds);
  184. JSONObject jsonObject = JSONObject.parseObject(res);
  185. personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class);
  186. }
  187. } catch (Exception e) {
  188. throw new XiaoShiException("查询创建人信息错误");
  189. }
  190. }
  191. List<Project> projects = new ArrayList<>();
  192. if (!projectIds.isEmpty()) {
  193. LambdaQueryWrapper<Project> queryWrapper1 = new LambdaQueryWrapper<>();
  194. queryWrapper1.in(Project::getId, projectIds);
  195. projects = projectService.list(queryWrapper1);
  196. }
  197. for (References item : references) {
  198. ReferencesVO referencesVO = new ReferencesVO();
  199. BeanUtils.copyProperties(item, referencesVO);
  200. Project project = projects
  201. .stream()
  202. .filter(p -> p.getId() != null && p.getId().equals(item.getProjectId()))
  203. .findFirst()
  204. .orElse(null);
  205. if (project != null) {
  206. referencesVO.setProjectName(project.getName());
  207. }
  208. if (!CollectionUtils.isEmpty(systemFiles)) {
  209. SystemFile systemFile = systemFiles
  210. .stream()
  211. .filter(file -> file.getGuid() != null && file.getGuid().equals(item.getFileGuid()))
  212. .findFirst()
  213. .orElse(null);
  214. if (systemFile != null) {
  215. referencesVO.setType(systemFile.getType());
  216. }
  217. }
  218. Personnel personnel = personnels
  219. .stream()
  220. .filter(person -> person.getId() != null && person.getId().equals(item.getCreateId()))
  221. .findFirst()
  222. .orElse(null);
  223. if (personnel != null) {
  224. referencesVO.setCreateName(personnel.getPersonnelName());
  225. }
  226. referencesVOS.add(referencesVO);
  227. }
  228. }
  229. return referencesVOS;
  230. }
  231. public String getColumns(String column) {
  232. Map<String, String> map = new HashMap<>();
  233. map.put("remark", "remark");
  234. map.put("projectName", "project_id");
  235. map.put("fileGuid", "file_guid");
  236. map.put("referencesName", "references_name");
  237. map.put("createName", "create_id");
  238. map.put("createTime", "create_time");
  239. String reStr = map.get(column);
  240. return reStr;
  241. }
  242. }