ImportTaskService.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.dto.ExportTaskDTO;
  3. import cn.cslg.pas.common.dto.ImportTaskDTO;
  4. import cn.cslg.pas.common.dto.PatentStarListDTO;
  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.GroupRequest;
  10. import cn.cslg.pas.common.model.request.QueryRequest;
  11. import cn.cslg.pas.common.utils.*;
  12. import cn.cslg.pas.common.vo.ImportTaskAMVO;
  13. import cn.cslg.pas.common.vo.business.ImportTaskVO;
  14. import cn.cslg.pas.domain.business.*;
  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.mapper.ImportTaskMapper;
  19. import cn.cslg.pas.service.common.FileManagerService;
  20. import cn.cslg.pas.service.common.PatentStarApiService;
  21. import cn.cslg.pas.service.importPatent.SchedulingTaskService;
  22. import cn.cslg.pas.service.permissions.PermissionService;
  23. import cn.cslg.pas.service.query.FormatQueryService;
  24. import com.alibaba.fastjson.JSONArray;
  25. import com.alibaba.fastjson.JSONObject;
  26. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  27. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  28. import lombok.RequiredArgsConstructor;
  29. import org.apache.commons.compress.utils.IOUtils;
  30. import org.apache.poi.ss.usermodel.Sheet;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.context.annotation.Lazy;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import org.springframework.web.multipart.MultipartFile;
  36. import java.io.*;
  37. import java.util.ArrayList;
  38. import java.util.Arrays;
  39. import java.util.List;
  40. import java.util.Map;
  41. /**
  42. * 导入任务表业务层
  43. *
  44. * @author chenyu
  45. * @date 2023/10/20
  46. */
  47. @Service
  48. @RequiredArgsConstructor
  49. public class ImportTaskService extends ServiceImpl<ImportTaskMapper, ImportTask> implements Business {
  50. @Autowired
  51. private FormatQueryService formatQueryService;
  52. @Autowired
  53. private ImportTaskMapper importTaskMapper;
  54. @Autowired
  55. private FileManagerService fileManagerService;
  56. @Autowired
  57. private PatentStarApiService patentStarApiService;
  58. @Autowired
  59. private PermissionService permissionService;
  60. @Autowired
  61. @Lazy
  62. private SchedulingTaskService schedulingTaskService;
  63. @Autowired
  64. private CacheUtils cacheUtils;
  65. @Autowired
  66. private LoginUtils loginUtils;
  67. @Autowired
  68. private SystemDictService systemDictService;
  69. @Autowired
  70. private ProductService productService;
  71. @Autowired
  72. private ProjectService projectService;
  73. @Autowired
  74. private ImportTaskConditionService importTaskConditionService;
  75. @Override
  76. public Object queryMessage(QueryRequest queryRequest) throws Exception {
  77. List<String> sqls = formatQueryService.reSqls(queryRequest, "importTask");
  78. this.loadSearchSql(sqls,queryRequest.getProjectId());
  79. //根据sql查询事件信息
  80. List<ImportTaskVO> importTaskVOS = importTaskMapper.getImportTask(sqls.get(0), sqls.get(1), sqls.get(2));
  81. //查询总数
  82. Long total = importTaskMapper.getImportTaskCount(sqls.get(0));
  83. //装载事件信息
  84. this.loadImportTask(importTaskVOS);
  85. Records records = new Records();
  86. records.setCurrent(queryRequest.getCurrent());
  87. records.setSize(queryRequest.getSize());
  88. records.setData(importTaskVOS);
  89. records.setTotal(total);
  90. return records;
  91. }
  92. @Override
  93. public Object addMessage(Object object, List<MultipartFile> files) {
  94. return null;
  95. }
  96. @Override
  97. public Object deleteMessage(List<Integer> ids) throws IOException {
  98. return null;
  99. }
  100. @Override
  101. public Object updateMessage(Object object, List<MultipartFile> files) {
  102. return null;
  103. }
  104. @Override
  105. public Object getGroup(GroupRequest groupRequest, String tableName) throws Exception {
  106. return null;
  107. }
  108. @Override
  109. @Transactional(rollbackFor = Exception.class)
  110. public Object addMessage(Object object) {
  111. if (object == null) {
  112. throw new XiaoShiException("参数不能为空");
  113. }
  114. //TODO 校验参数
  115. ImportTaskDTO importTaskDTO = (ImportTaskDTO) object;
  116. PersonnelVO personnelVO = new PersonnelVO();
  117. try {
  118. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  119. } catch (Exception e) {
  120. throw new UnLoginException("未登录");
  121. }
  122. if (importTaskDTO.getType() == null) {
  123. importTaskDTO.setType(4);
  124. }
  125. List<String> addPatentNos = importTaskDTO.getPatentNos();
  126. ImportTaskCondition importTaskCondition = new ImportTaskCondition();
  127. if (importTaskDTO.getImportToId() != null) {
  128. if (importTaskDTO.getImportToType().equals(0)) {
  129. importTaskCondition.setProjectType(0);
  130. importTaskCondition.setProjectId(importTaskDTO.getImportToId());
  131. } else if (importTaskDTO.getImportToType().equals(1)) {
  132. importTaskCondition.setProjectType(1);
  133. importTaskCondition.setProjectId(importTaskDTO.getImportToId());
  134. } else if (importTaskDTO.getImportToType().equals(2)) {
  135. importTaskCondition.setProductId(importTaskDTO.getImportToId());
  136. }
  137. }
  138. if (!importTaskDTO.getType().equals(1)) {
  139. importTaskDTO.setImportContent(MathUtils.BinaryToDecimal(importTaskDTO.getImportContent()));
  140. }
  141. importTaskCondition.setImportContent(importTaskDTO.getImportContent());
  142. importTaskCondition.setCrons(importTaskDTO.getCrons());
  143. importTaskCondition.setIfUpdate(importTaskDTO.getIfUpdate());
  144. importTaskCondition.setSearchCondition(importTaskDTO.getSearchCondition());
  145. importTaskCondition.setType(importTaskDTO.getType());
  146. importTaskCondition.setFileGuid(importTaskDTO.getFileGuid());
  147. importTaskCondition.setSourceId(importTaskDTO.getSourceId());
  148. importTaskCondition.setDbType(importTaskDTO.getDBType());
  149. importTaskCondition.setOrderBy(importTaskDTO.getOrderBy());
  150. importTaskCondition.setOrderByType(importTaskDTO.getOrderByType());
  151. importTaskCondition.setCreateId(personnelVO.getId());
  152. if (importTaskDTO.getPatentNos() != null && importTaskDTO.getPatentNos().size() > 0) {
  153. String jsons = JSONObject.toJSON(importTaskDTO.getPatentNos()).toString();
  154. importTaskCondition.setPatentNos(jsons);
  155. }
  156. //保存标引信息
  157. if (importTaskDTO.getFieldDTOS() != null && importTaskDTO.getFieldDTOS().size() > 0) {
  158. String fieldJson = JsonUtils.objectToJson(importTaskDTO.getFieldDTOS());
  159. importTaskCondition.setCustomFields(fieldJson);
  160. }
  161. importTaskCondition.insert();
  162. //装载任务
  163. ImportTask importTask = new ImportTask();
  164. importTask.setImportTaskConditionId(importTaskCondition.getId());
  165. importTask.setType(importTaskDTO.getType());
  166. importTask.setSearchCondition(importTaskDTO.getSearchCondition());
  167. importTask.setCreateId(personnelVO.getId());
  168. if (importTaskCondition.getType().equals(1)) {
  169. try {
  170. String res = fileManagerService.getSystemFileFromFMS(Arrays.asList(importTaskCondition.getFileGuid()));
  171. List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
  172. SystemFile systemFile = systemFiles.get(0);
  173. String suffix = systemFile.getFileName().substring(systemFile.getFileName().lastIndexOf("."));
  174. //调用文件系统取出文件接口,获得文件流
  175. byte[] bytes = fileManagerService.downloadSystemFileFromFMS(importTaskCondition.getFileGuid());
  176. //创建临时文件tempFile,并将文件读取到tempFile
  177. File tempFile = File.createTempFile(systemFile.getFileName() + "temp", suffix);
  178. try (
  179. InputStream inputStream = new ByteArrayInputStream(bytes);
  180. FileOutputStream outputStream = new FileOutputStream(tempFile)
  181. ) {
  182. IOUtils.copy(inputStream, outputStream); // 将输入流复制到临时文件
  183. }
  184. Integer totalNum = ReadExcelUtils.textExcel(tempFile, importTaskCondition.getSourceId().toString());
  185. importTask.setAllNum(totalNum);
  186. //删除临时文件tempFile
  187. new File(tempFile.getPath()).delete();
  188. } catch (Exception e) {
  189. throw new XiaoShiException("文件错误");
  190. }
  191. } else if (importTaskCondition.getType().equals(4)) {
  192. try {
  193. if (addPatentNos == null || addPatentNos.size() == 0) {
  194. PatentStarListDTO patentStarListDto = new PatentStarListDTO()
  195. .setCurrentQuery(importTaskCondition.getSearchCondition())
  196. .setOrderBy(importTaskCondition.getOrderBy())
  197. .setOrderByType(importTaskCondition.getOrderByType())
  198. .setPageNum(1)
  199. .setRowCount(50)
  200. .setDBType(importTaskCondition.getDbType());
  201. //调用一般接口返回一批专利著录相关数据
  202. Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(patentStarListDto);
  203. if (resultMap == null || (Integer) resultMap.get("total") == 0) {
  204. ThrowException.throwXiaoShiException("未检索到相关专利");
  205. } else {
  206. importTask.setAllNum((Integer) resultMap.get("total"));
  207. }
  208. } else {
  209. importTask.setAllNum(addPatentNos.size());
  210. }
  211. } catch (Exception e) {
  212. throw new XiaoShiException("文件错误");
  213. }
  214. } else if (importTaskCondition.getType().equals(2) || importTaskCondition.getType().equals(3)) {
  215. List<String> patentNos = new ArrayList<>();
  216. if (importTaskCondition.getType().equals(2)) {
  217. patentNos = Arrays.asList(importTaskCondition.getSearchCondition().split("[,|,]"));
  218. } else if (importTaskCondition.getType().equals(3)) {
  219. try {
  220. String res = fileManagerService.getSystemFileFromFMS(Arrays.asList(importTaskCondition.getFileGuid()));
  221. List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
  222. SystemFile systemFile = systemFiles.get(0);
  223. String suffix = systemFile.getFileName().substring(systemFile.getFileName().lastIndexOf("."));
  224. //调用文件系统取出文件接口,获得文件流
  225. byte[] bytes = fileManagerService.downloadSystemFileFromFMS(importTaskCondition.getFileGuid());
  226. //创建临时文件tempFile,并将文件读取到tempFile
  227. File tempFile = File.createTempFile(systemFile.getFileName() + "temp", suffix);
  228. try (
  229. InputStream inputStream = new ByteArrayInputStream(bytes);
  230. FileOutputStream outputStream = new FileOutputStream(tempFile)
  231. ) {
  232. IOUtils.copy(inputStream, outputStream); // 将输入流复制到临时文件
  233. }
  234. Sheet sheet = ReadExcelUtils.readExcel(tempFile);
  235. patentNos = ReadExcelUtils.getPatentNoFromExcel(sheet);
  236. } catch (Exception e) {
  237. }
  238. }
  239. importTask.setAllNum(patentNos.size());
  240. }
  241. importTask.setProgress(0.0);
  242. String taskName = this.setImportTaskName(importTaskCondition, personnelVO.getName());
  243. importTask.setName(taskName);
  244. importTask.insert();
  245. schedulingTaskService.startTask();
  246. return importTask.getId();
  247. }
  248. @Override
  249. public Object updateMessage(Object object) {
  250. return null;
  251. }
  252. private void loadImportTask(List<ImportTaskVO> importTaskVOS) throws IOException {
  253. List<ImportTaskAMVO> importTaskAMVOS = schedulingTaskService.getImportTaskAMVOs();
  254. if (importTaskAMVOS == null) {
  255. importTaskAMVOS = new ArrayList<>();
  256. }
  257. List<String> createIds = new ArrayList<>();
  258. importTaskVOS.forEach(item -> {
  259. if (item.getCreateId() != null) {
  260. createIds.add(item.getCreateId());
  261. }
  262. });
  263. List<Personnel> personnels = new ArrayList<>();
  264. List<Product> products = new ArrayList<>();
  265. List<Project> projects = new ArrayList<>();
  266. //查询创建人名称
  267. if (createIds.size() != 0) {
  268. String res = permissionService.getPersonnelByIdsFromPCS(createIds);
  269. JSONObject jsonObject = JSONObject.parseObject(res);
  270. personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class);
  271. }
  272. LambdaQueryWrapper<SystemDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  273. lambdaQueryWrapper.eq(SystemDict::getGroupType, "IMPORT_TASK");
  274. List<SystemDict> systemDictList = systemDictService.list(lambdaQueryWrapper);
  275. for (ImportTaskVO importTaskVO : importTaskVOS) {
  276. //装载创建人
  277. Personnel personnel = personnels.stream().filter(item -> item.getId().equals(importTaskVO.getCreateId())).findFirst().orElse(null);
  278. if (personnel != null) {
  279. importTaskVO.setCreateName(personnel.getPersonnelName());
  280. }
  281. //装载状态
  282. SystemDict systemDict1 = systemDictList.stream()
  283. .filter(item -> item.getType().equals("IMPORT_TASK_TYPE") && item.getType()
  284. .equals(importTaskVO.getType().toString())).findFirst().orElse(null);
  285. SystemDict systemDict2 = systemDictList.stream()
  286. .filter(item -> item.getType().equals("IMPORT_TASk_STATE") && item.getType()
  287. .equals(importTaskVO.getState().toString())).findFirst().orElse(null);
  288. if (systemDict1 != null) {
  289. importTaskVO.setTypeStr(systemDict1.getLabel());
  290. }
  291. if (systemDict2 != null) {
  292. importTaskVO.setStateStr(systemDict2.getLabel());
  293. }
  294. //装载正在进行中任务的完成条数
  295. ImportTaskAMVO importTaskAMVO = importTaskAMVOS.stream().filter(item -> item.getId().equals(importTaskVO.getId())).findFirst().orElse(null);
  296. if (importTaskAMVO != null) {
  297. importTaskVO.setDoneNum(importTaskAMVO.getDoneNum());
  298. importTaskAMVO.setAllNum(importTaskAMVO.getAllNum());
  299. }
  300. long percentage = (long) Math.floor((importTaskVO.getDoneNum() + 0D) / importTaskVO.getAllNum() * 100D);
  301. importTaskVO.setPercentage(percentage);
  302. }
  303. }
  304. private String setImportTaskName(ImportTaskCondition importTaskCondition, String name) {
  305. //装载名称
  306. String type = "";
  307. String toName = "";
  308. String to = "";
  309. String taskName = "";
  310. if (importTaskCondition.getType().equals(2)) {
  311. type = "【专利号】";
  312. } else if (importTaskCondition.getType().equals(3) || importTaskCondition.getType().equals(4)) {
  313. type = "【检索式】";
  314. } else if (importTaskCondition.getType().equals(1)) {
  315. type = "【Excel】";
  316. }
  317. if (importTaskCondition.getProjectId() != null && importTaskCondition.getProjectType().equals(0)) {
  318. to = "专题库";
  319. LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
  320. queryWrapper.in(Project::getId, importTaskCondition.getProjectId());
  321. List<Project> projects = projectService.list(queryWrapper);
  322. if (projects.size() != 0) {
  323. toName = projects.get(0).getName();
  324. }
  325. } else if (importTaskCondition.getProjectId() != null && importTaskCondition.getProjectType().equals(1)) {
  326. to = "报告";
  327. LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
  328. queryWrapper.in(Project::getId, importTaskCondition.getProjectId());
  329. List<Project> projects = projectService.list(queryWrapper);
  330. if (projects.size() != 0) {
  331. toName = projects.get(0).getName();
  332. }
  333. } else if (importTaskCondition.getProductId() != null) {
  334. to = "产品";
  335. LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
  336. queryWrapper.in(Product::getId, importTaskCondition.getProductId());
  337. List<Product> products = productService.list(queryWrapper);
  338. if (products.size() != 0) {
  339. toName = products.get(0).getName();
  340. }
  341. }
  342. //导入任务
  343. taskName = type + "导入到" + to + "【" + toName + "】 - " + name;
  344. return taskName;
  345. }
  346. public void updateImportTaskState(Integer taskId, Integer state) {
  347. ImportTask importTask = this.getById(taskId);
  348. importTask.setState(state);
  349. importTask.updateById();
  350. if (state.equals(4) || state.equals(5)) {
  351. schedulingTaskService.pauseTask(taskId, state);
  352. }
  353. if (state.equals(0)) {
  354. schedulingTaskService.startTask();
  355. }
  356. }
  357. public Integer addExportTask(ExportTaskDTO exportTaskDTO) {
  358. if (exportTaskDTO == null) {
  359. throw new XiaoShiException("入参不能为空");
  360. }
  361. ImportTaskCondition importTaskCondition = new ImportTaskCondition();
  362. if (exportTaskDTO.getProjectId() != null) {
  363. importTaskCondition.setProjectId(exportTaskDTO.getProjectId());
  364. }
  365. importTaskCondition.setIfUpdate(false);
  366. importTaskCondition.setType(exportTaskDTO.getType());
  367. importTaskCondition.setCreateId(exportTaskDTO.getCreateId());
  368. importTaskCondition.insert();
  369. Integer importTaskConditionId = importTaskCondition.getId();
  370. if (importTaskConditionId != null) {
  371. ImportTask importTask = new ImportTask();
  372. importTask.setImportTaskConditionId(importTaskConditionId);
  373. //设置任务的类型 6导出excel 7导出pdf首页
  374. importTask.setType(exportTaskDTO.getType());
  375. importTask.setAllNum(exportTaskDTO.getAllNum());
  376. importTask.setFinishTime(exportTaskDTO.getFinishTime());
  377. importTask.setState(0);
  378. importTask.setCreateId(exportTaskDTO.getCreateId());
  379. String name = "";
  380. if (exportTaskDTO.getType().equals(6)) {
  381. name = "【专利】";
  382. } else if (exportTaskDTO.getType().equals(7)) {
  383. name = "【PDF首页】";
  384. }
  385. String importTaskName = "导出" + name + "-" + exportTaskDTO.getCreateName();
  386. importTask.setName(importTaskName);
  387. importTask.insert();
  388. return importTask.getId();
  389. } else {
  390. throw new XiaoShiException("导出任务记录失败");
  391. }
  392. }
  393. public Integer updateExportTask(ExportTaskDTO exportTaskDTO) {
  394. if (exportTaskDTO == null) {
  395. throw new XiaoShiException("入参不能为空");
  396. }
  397. ImportTask importTask = this.getById(exportTaskDTO.getTaskId());
  398. if (importTask != null) {
  399. Integer importTaskConditionId = importTask.getImportTaskConditionId();
  400. ImportTaskCondition importTaskCondition = importTaskConditionService.getById(importTaskConditionId);
  401. importTaskCondition.setFileGuid(exportTaskDTO.getFileGuid());
  402. importTaskCondition.updateById();
  403. importTask.setState(exportTaskDTO.getState());
  404. importTask.setAllNum(exportTaskDTO.getAllNum());
  405. importTask.setDoneNum(exportTaskDTO.getDoneNum());
  406. importTask.setDefaultNum(exportTaskDTO.getDefaultNum());
  407. importTask.setFinishTime(exportTaskDTO.getFinishTime());
  408. importTask.updateById();
  409. }
  410. return importTask.getId();
  411. }
  412. //装载查询语句
  413. private List<String> loadSearchSql(List<String> sqls,Integer projectId) {
  414. PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  415. String id = personnelVO.getId();
  416. String rootSql = "";
  417. if (sqls.get(0) == null || projectId==null) {
  418. rootSql = "it.create_id=" + id;
  419. } else {
  420. Project project =projectService.getById(projectId);
  421. if(project==null){
  422. throw new XiaoShiException("未查询到专题库");
  423. }
  424. String createId =project.getCreateId();
  425. String headId =project.getHeadId();
  426. if(createId!=id&&headId!=id){
  427. rootSql = "it.create_id=" + id;
  428. }
  429. }
  430. if (sqls.get(0) != null && !sqls.get(0).equals("") && !rootSql.equals("")) {
  431. sqls.set(0, rootSql + " and " + "(" + sqls.get(0) + ")");
  432. } else if ((sqls.get(0) == null || sqls.get(0).equals("")) && !rootSql.equals("")) {
  433. sqls.set(0, rootSql);
  434. }
  435. return sqls;
  436. }
  437. }