EventService.java 20 KB

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