EventService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.dto.business.EventDTO;
  3. import cn.cslg.pas.common.dto.business.UpdateEventDTO;
  4. import cn.cslg.pas.common.model.cronModel.*;
  5. import cn.cslg.pas.common.model.request.*;
  6. import cn.cslg.pas.common.utils.CacheUtils;
  7. import cn.cslg.pas.common.utils.LoginUtils;
  8. import cn.cslg.pas.common.vo.business.EventCountVO;
  9. import cn.cslg.pas.common.vo.business.EventVO;
  10. import cn.cslg.pas.domain.business.AssoEventFile;
  11. import cn.cslg.pas.domain.business.Event;
  12. import cn.cslg.pas.domain.business.SystemDict;
  13. import cn.cslg.pas.exception.UnLoginException;
  14. import cn.cslg.pas.exception.XiaoShiException;
  15. import cn.cslg.pas.factorys.businessFactory.Business;
  16. import cn.cslg.pas.factorys.reGroupFactory.QueryGroupFactory;
  17. import cn.cslg.pas.factorys.reGroupFactory.QueryGroupImp;
  18. import cn.cslg.pas.mapper.AssoProjectEventMapper;
  19. import cn.cslg.pas.mapper.EventMapper;
  20. import cn.cslg.pas.service.common.FileManagerService;
  21. import cn.cslg.pas.service.permissions.PermissionService;
  22. import cn.cslg.pas.service.query.FormatQueryService;
  23. import com.alibaba.fastjson.JSON;
  24. import com.alibaba.fastjson.JSONObject;
  25. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  26. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  27. import lombok.extern.slf4j.Slf4j;
  28. import org.springframework.beans.BeanUtils;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.stereotype.Service;
  31. import org.springframework.transaction.annotation.Transactional;
  32. import org.springframework.web.multipart.MultipartFile;
  33. import java.io.IOException;
  34. import java.util.ArrayList;
  35. import java.util.Arrays;
  36. import java.util.List;
  37. import java.util.stream.Collectors;
  38. @Service
  39. @Slf4j
  40. public class EventService extends ServiceImpl<EventMapper, Event> implements Business {
  41. @Autowired
  42. private EventMapper eventMapper;
  43. @Autowired
  44. private FormatQueryService formatQueryService;
  45. @Autowired
  46. private FileManagerService fileManagerService;
  47. @Autowired
  48. private AssoEventFileService assoEventFileService;
  49. @Autowired
  50. private QueryGroupFactory queryGroupFactory;
  51. @Autowired
  52. private PermissionService permissionService;
  53. @Autowired
  54. private SystemDictService systemDictService;
  55. @Autowired
  56. private AssoProjectEventMapper assoProjectEventMapper;
  57. @Autowired
  58. private CacheUtils cacheUtils;
  59. @Autowired
  60. private LoginUtils loginUtils;
  61. public String getEvent(String sql) {
  62. // eventMapper.getEvent(sql);
  63. return "";
  64. }
  65. @Override
  66. @Transactional(rollbackFor = Exception.class)
  67. public Object queryMessage(QueryRequest queryRequest) throws Exception {
  68. List<String> sqls = formatQueryService.reSqls(queryRequest,"event");
  69. //根据sql查询事件信息
  70. List<EventVO> eventVOS = eventMapper.getEvent(sqls.get(0),sqls.get(1),sqls.get(2));
  71. //查询总数
  72. Long total = eventMapper.getEventCount(sqls.get(0));
  73. //装载事件信息
  74. this.loadEvent(eventVOS);
  75. Records records = new Records();
  76. records.setCurrent(queryRequest.getCurrent());
  77. records.setSize(queryRequest.getSize());
  78. records.setData(eventVOS);
  79. records.setTotal(total);
  80. return records;
  81. }
  82. @Override
  83. @Transactional(rollbackFor = Exception.class)
  84. public Integer addMessage(Object object, List<MultipartFile> files) {
  85. EventDTO eventDTO = (EventDTO) object;
  86. //检查事件格式
  87. if (eventDTO.getScenarioId() == null) {
  88. throw new XiaoShiException("参数错误");
  89. }
  90. //获取登录人信息
  91. PersonnelVO personnelVO = new PersonnelVO();
  92. try {
  93. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  94. } catch (Exception e) {
  95. throw new UnLoginException("未登录");
  96. }
  97. //根据名称查询是否重复
  98. eventDTO.setName(eventDTO.getName().trim());
  99. String name = eventDTO.getName();
  100. LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
  101. queryWrapper.eq(Event::getName, name);
  102. List<Event> events = this.list(queryWrapper);
  103. if (events != null && events.size() != 0) {
  104. throw new XiaoShiException("名称重复");
  105. }
  106. //事件入库
  107. Event event = new Event();
  108. BeanUtils.copyProperties(eventDTO, event);
  109. event.setCreateId(personnelVO.getId());
  110. event.setTenantId(personnelVO.getTenantId());
  111. event.insert();
  112. if (files != null && files.size() != 0) {
  113. try {
  114. List<String> guids = fileManagerService.uploadFileGetGuid(files);
  115. List<AssoEventFile> assoEventFiles = new ArrayList<>();
  116. for (String item : guids) {
  117. AssoEventFile assoEventFile = new AssoEventFile();
  118. assoEventFile.setEventId(event.getId());
  119. assoEventFile.setFileGuid(item);
  120. assoEventFile.setCreateId(personnelVO.getId());
  121. assoEventFiles.add(assoEventFile);
  122. }
  123. if (assoEventFiles != null && assoEventFiles.size() != 0) {
  124. assoEventFileService.saveBatch(assoEventFiles);
  125. }
  126. } catch (Exception e) {
  127. }
  128. }
  129. return event.getId();
  130. }
  131. @Override
  132. @Transactional(rollbackFor = Exception.class)
  133. public Object deleteMessage(List<Integer> ids) throws IOException {
  134. //根据事件id删除事件和文件关联
  135. LambdaQueryWrapper<AssoEventFile> queryWrapper = new LambdaQueryWrapper<>();
  136. queryWrapper.in(AssoEventFile::getEventId, ids);
  137. List<AssoEventFile> assoEventFiles = assoEventFileService.list(queryWrapper);
  138. List<String> guids = assoEventFiles.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList());
  139. // 根据guid删除文件
  140. if (guids.size() != 0) {
  141. fileManagerService.deleteFileFromFMS(guids);
  142. }
  143. //删除事件和文件关联表
  144. assoEventFiles.remove(queryWrapper);
  145. //根据事件id删除事件
  146. this.removeBatchByIds(ids);
  147. return ids;
  148. }
  149. /**
  150. * 更新事件接口
  151. *
  152. * @param object
  153. * @param files
  154. * @return
  155. */
  156. @Override
  157. @Transactional(rollbackFor = Exception.class)
  158. public Object updateMessage(Object object, List<MultipartFile> files) {
  159. PersonnelVO personnelVO =cacheUtils.getLoginUser(loginUtils.getId());
  160. UpdateEventDTO updateEventDTO = (UpdateEventDTO) object;
  161. //检查事件格式
  162. if (updateEventDTO == null || updateEventDTO.getId() == null) {
  163. throw new XiaoShiException("参数错误");
  164. }
  165. Event event = this.getById(updateEventDTO.getId());
  166. //根据名称查询是否重复
  167. updateEventDTO.setName(updateEventDTO.getName().trim());
  168. String name = updateEventDTO.getName();
  169. LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
  170. queryWrapper.eq(Event::getName, name);
  171. List<Event> events = this.list(queryWrapper);
  172. if (!updateEventDTO.getName().equals(event.getName()) && events.size() != 0) {
  173. throw new XiaoShiException("名称重复");
  174. }
  175. BeanUtils.copyProperties(updateEventDTO, event);
  176. event.updateById();
  177. // 根据事件Id查询对应的附件Id
  178. LambdaQueryWrapper<AssoEventFile> wrapper = new LambdaQueryWrapper<>();
  179. wrapper.eq(AssoEventFile::getEventId, updateEventDTO.getId());
  180. List<AssoEventFile> assoReportFiles = assoEventFileService.list(wrapper);
  181. List<String> fileGuIds = assoReportFiles.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList());
  182. // 获得事件更新后的附件Id
  183. List<String> updateFilGuId = new ArrayList<>();
  184. if (updateEventDTO.getFileGuids() != null && updateEventDTO.getFileGuids().size() != 0) {
  185. updateFilGuId = updateEventDTO.getFileGuids();
  186. }
  187. fileGuIds.removeAll(updateFilGuId);
  188. //做差获得被删除的文件Id
  189. if (fileGuIds.size() != 0) {
  190. //根据文件Id删除报事件文件关联表记录
  191. LambdaQueryWrapper<AssoEventFile> deleteWrapper = new LambdaQueryWrapper<>();
  192. deleteWrapper.in(AssoEventFile::getFileGuid, fileGuIds);
  193. assoEventFileService.remove(deleteWrapper);
  194. //远程删除服务器上文件
  195. try {
  196. fileManagerService.deleteFileFromFMS(fileGuIds);
  197. } catch (Exception e) {
  198. }
  199. }
  200. //添加文件
  201. if (files != null && files.size() != 0) {
  202. try {
  203. List<String> guids = fileManagerService.uploadFileGetGuid(files);
  204. List<AssoEventFile> assoEventFiles = new ArrayList<>();
  205. guids.forEach(item -> {
  206. AssoEventFile assoEventFile = new AssoEventFile();
  207. assoEventFile.setEventId(event.getId());
  208. assoEventFile.setFileGuid(item);
  209. assoEventFile.setCreateId(personnelVO.getId());
  210. assoEventFiles.add(assoEventFile);
  211. });
  212. if (assoEventFiles != null && assoEventFiles.size() != 0) {
  213. assoEventFileService.saveBatch(assoEventFiles);
  214. }
  215. } catch (Exception e) {
  216. }
  217. }
  218. return event.getId();
  219. }
  220. /**
  221. * 查询事件分组信息
  222. *
  223. * @param groupRequest
  224. * @return
  225. * @throws Exception
  226. */
  227. @Transactional(rollbackFor = Exception.class)
  228. public Object getGroup(GroupRequest groupRequest,String tableName) throws Exception {
  229. StringRequest stringRequest =new StringRequest();
  230. BeanUtils.copyProperties(groupRequest,stringRequest);
  231. List<String> sqls = formatQueryService.reSqls(stringRequest,tableName);
  232. //格式化 分组
  233. GroupConfig groupConfig=null;
  234. if (groupRequest.getGroupBy() != null) {
  235. String json = CommonService.readJsonFile(tableName+".json");
  236. List<GroupConfig> groupConfigs = JSON.parseArray(json, GroupConfig.class);
  237. groupConfig = groupConfigs.stream().filter(item -> groupRequest.getGroupBy().equals(item.getField())).findFirst().orElse(null);
  238. if (groupConfig == null) {
  239. throw new XiaoShiException("未找到配置");
  240. }
  241. }
  242. //返回分组数据
  243. QueryGroupImp queryGroupImp = queryGroupFactory.getClass(groupConfig.getGroupClass());
  244. ReGroupDataVO reGroupDataVO = queryGroupImp.getGroup(sqls, tableName, groupConfig.getSqlField());
  245. //装载数据
  246. GroupVO groupVO = new GroupVO();
  247. groupVO.setField(groupRequest.getGroupBy());
  248. groupVO.setValues(reGroupDataVO.getValues());
  249. Records records = new Records();
  250. records.setCurrent(groupRequest.getCurrent());
  251. records.setSize(groupRequest.getSize());
  252. records.setData(groupVO);
  253. records.setTotal(reGroupDataVO.getTotal());
  254. return records;
  255. }
  256. @Override
  257. public Object addMessage(Object object) {
  258. return null;
  259. }
  260. @Override
  261. public Object updateMessage(Object object) {
  262. return null;
  263. }
  264. /**
  265. * 装载事件返回类
  266. *
  267. * @param eventVOs
  268. */
  269. private void loadEvent(List<EventVO> eventVOs) throws IOException {
  270. List<String> createIds = new ArrayList<>();
  271. List<Integer> clientIds = new ArrayList<>();
  272. List<Integer> ids = new ArrayList<>();
  273. List<EventCountVO> eventCountVOS = new ArrayList<>();
  274. //获得所有文件的guid
  275. eventVOs.forEach(
  276. item -> {
  277. if (item.getClientId() != null) {
  278. clientIds.add(item.getClientId());
  279. }
  280. if (item.getCreateId() != null) {
  281. createIds.add(item.getCreateId());
  282. }
  283. if (item.getId() != null) {
  284. ids.add(item.getId());
  285. }
  286. }
  287. );
  288. List<Personnel> personnels = new ArrayList<>();
  289. List<Client> clients = new ArrayList<>();
  290. List<String> guids = new ArrayList<>();
  291. List<SystemFile> systemFiles = new ArrayList<>();
  292. List<AssoEventFile> assoEventFiles = new ArrayList<>();
  293. if (ids.size() != 0) {
  294. //根据事件id获得事件文件关联表
  295. LambdaQueryWrapper<AssoEventFile> queryWrapper = new LambdaQueryWrapper<>();
  296. queryWrapper.in(AssoEventFile::getEventId, ids);
  297. assoEventFiles = assoEventFileService.list(queryWrapper);
  298. guids = assoEventFiles.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList());
  299. //根据事件id分组查询事件关联专题库或报告数量
  300. eventCountVOS = assoProjectEventMapper.getEventProjectCount(ids);
  301. }
  302. //查询创建人名称
  303. if (createIds.size() != 0) {
  304. String res = permissionService.getPersonnelByIdsFromPCS(createIds);
  305. JSONObject jsonObject = JSONObject.parseObject(res);
  306. personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class);
  307. }
  308. //查询客户名称
  309. if (clientIds.size() != 0) {
  310. String res = permissionService.getClientByIdsFromPCS(clientIds);
  311. JSONObject jsonObject = JSONObject.parseObject(res);
  312. clients = JSONObject.parseArray(jsonObject.getString("data"), Client.class);
  313. }
  314. //查询文件
  315. if (guids.size() != 0) {
  316. String res = fileManagerService.getSystemFileFromFMS(guids);
  317. systemFiles = JSONObject.parseArray(res, SystemFile.class);
  318. }
  319. //查询关联报告或专题库
  320. //查询应用场景
  321. List<SystemDict> systemDictList = systemDictService.getSystemDictListByType(Arrays.asList("ENTERPRISE_APPLICATION_SCENARIO"));
  322. //查询专题库数量
  323. //装载信息
  324. for (EventVO eventVO : eventVOs) {
  325. //装载人员信息
  326. Personnel personnel = personnels.stream().filter(item -> item.getId().equals(Integer.parseInt(eventVO.getCreateId()))).findFirst().orElse(null);
  327. if (personnel != null) {
  328. eventVO.setCreateName(personnel.getPersonnelName());
  329. }
  330. //装载客户信息
  331. Client client = clients.stream().filter(item -> item.getId().equals(eventVO.getClientId())).findFirst().orElse(null);
  332. if (client != null) {
  333. eventVO.setClientName(client.getName());
  334. }
  335. //装载场景
  336. SystemDict systemDict = systemDictList.stream().filter(item -> item.getValue().equals(eventVO.getScenarioId().toString())).findFirst().orElse(null);
  337. if (systemDict != null) {
  338. eventVO.setScenarioName(systemDict.getLabel());
  339. }
  340. //装载文件信息
  341. List<AssoEventFile> assoEventFileTemp = assoEventFiles.stream().filter(item -> item.getEventId().equals(eventVO.getId())).collect(Collectors.toList());
  342. if (assoEventFileTemp.size() != 0) {
  343. List<String> guidTemp = assoEventFileTemp.stream().map(AssoEventFile::getFileGuid).collect(Collectors.toList());
  344. if (guidTemp.size() != 0) {
  345. List<SystemFile> systemFileTemp = systemFiles.stream().filter(item -> guidTemp.contains(item.getGuid())).collect(Collectors.toList());
  346. if (systemFileTemp.size() != 0) {
  347. eventVO.setSystemFileList(systemFileTemp);
  348. }
  349. }
  350. }
  351. //装载专题库或报告数量
  352. if (eventCountVOS.size() != 0) {
  353. //专题库数量
  354. EventCountVO eventCountVO1 = eventCountVOS.stream().filter(item ->
  355. item.getEventId().equals(eventVO.getId()) && item.getProjectType().equals(1)).findFirst().orElse(null);
  356. //报告数量
  357. EventCountVO eventCountVO2 = eventCountVOS.stream().filter(item ->
  358. item.getEventId().equals(eventVO.getId()) && item.getProjectType().equals(2)).findFirst().orElse(null);
  359. //设置专题库数量
  360. if (eventCountVO1 != null) {
  361. eventVO.setPatentProjectNum(eventCountVO1.getProjectCount());
  362. } else {
  363. eventVO.setPatentProjectNum(0);
  364. }
  365. //设置报告数量
  366. if (eventCountVO2 != null) {
  367. eventVO.setReportProjectNum(eventCountVO2.getProjectCount());
  368. } else {
  369. eventVO.setReportProjectNum(0);
  370. }
  371. } else {
  372. eventVO.setReportProjectNum(0);
  373. eventVO.setPatentProjectNum(0);
  374. }
  375. }
  376. }
  377. }