ImportTaskService.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.common.core.base.Cycle;
  3. import cn.cslg.pas.common.dto.ExportTaskDTO;
  4. import cn.cslg.pas.common.dto.ImportTaskDTO;
  5. import cn.cslg.pas.common.dto.PatentStarListDTO;
  6. import cn.cslg.pas.common.dto.es.EsCustomFieldDTO;
  7. import cn.cslg.pas.common.importTask.QuartzConditionDTO;
  8. import cn.cslg.pas.common.model.cronModel.Personnel;
  9. import cn.cslg.pas.common.model.cronModel.PersonnelVO;
  10. import cn.cslg.pas.common.model.cronModel.Records;
  11. import cn.cslg.pas.common.model.cronModel.SystemFile;
  12. import cn.cslg.pas.common.model.importPDF.AddImportPDFTaskDTO;
  13. import cn.cslg.pas.common.model.request.GroupRequest;
  14. import cn.cslg.pas.common.model.request.QueryRequest;
  15. import cn.cslg.pas.common.utils.*;
  16. import cn.cslg.pas.common.vo.ImportTaskAMVO;
  17. import cn.cslg.pas.common.vo.StarPatentVO;
  18. import cn.cslg.pas.common.vo.business.ImportTaskVO;
  19. import cn.cslg.pas.domain.business.*;
  20. import cn.cslg.pas.exception.UnLoginException;
  21. import cn.cslg.pas.exception.XiaoShiException;
  22. import cn.cslg.pas.factorys.businessFactory.Business;
  23. import cn.cslg.pas.mapper.ImportTaskMapper;
  24. import cn.cslg.pas.service.MailSendService;
  25. import cn.cslg.pas.service.common.FileManagerService;
  26. import cn.cslg.pas.service.common.PatentStarApiService;
  27. import cn.cslg.pas.service.importPatent.SchedulingTaskService;
  28. import cn.cslg.pas.service.importPatent.WebVOTransformService;
  29. import cn.cslg.pas.service.permissions.PermissionService;
  30. import cn.cslg.pas.service.query.FormatQueryService;
  31. import com.alibaba.fastjson.JSONArray;
  32. import com.alibaba.fastjson.JSONObject;
  33. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  34. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  35. import lombok.RequiredArgsConstructor;
  36. import org.apache.commons.compress.utils.IOUtils;
  37. import org.apache.poi.ss.usermodel.Sheet;
  38. import org.joda.time.DateTime;
  39. import org.springframework.beans.BeanUtils;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.context.annotation.Lazy;
  42. import org.springframework.stereotype.Service;
  43. import org.springframework.transaction.annotation.Transactional;
  44. import org.springframework.util.CollectionUtils;
  45. import org.springframework.web.multipart.MultipartFile;
  46. import java.io.*;
  47. import java.time.LocalDate;
  48. import java.util.*;
  49. import java.util.stream.Collectors;
  50. /**
  51. * 导入任务表业务层
  52. *
  53. * @author chenyu
  54. * @date 2023/10/20
  55. */
  56. @Service
  57. @RequiredArgsConstructor
  58. public class ImportTaskService extends ServiceImpl<ImportTaskMapper, ImportTask> implements Business {
  59. @Autowired
  60. private FormatQueryService formatQueryService;
  61. @Autowired
  62. private ImportTaskMapper importTaskMapper;
  63. @Autowired
  64. private FileManagerService fileManagerService;
  65. @Autowired
  66. private PatentStarApiService patentStarApiService;
  67. @Autowired
  68. private PermissionService permissionService;
  69. @Autowired
  70. @Lazy
  71. private SchedulingTaskService schedulingTaskService;
  72. @Autowired
  73. private CacheUtils cacheUtils;
  74. @Autowired
  75. private LoginUtils loginUtils;
  76. @Autowired
  77. private SystemDictService systemDictService;
  78. @Autowired
  79. private ProductService productService;
  80. @Autowired
  81. private ProjectService projectService;
  82. @Autowired
  83. private ImportTaskConditionService importTaskConditionService;
  84. @Autowired
  85. private MailSendService mailSendService;
  86. @Autowired
  87. private ReportProjectService reportProjectService;
  88. @Autowired
  89. private WebVOTransformService webVOTransformService;
  90. @Override
  91. public Object queryMessage(QueryRequest queryRequest) throws Exception {
  92. List<String> sqls = formatQueryService.reSqls(queryRequest, "importTask");
  93. this.loadSearchSql(sqls, queryRequest.getProjectId());
  94. //根据sql查询事件信息
  95. List<ImportTaskVO> importTaskVOS = importTaskMapper.getImportTask(sqls.get(0), sqls.get(1), sqls.get(2));
  96. //查询总数
  97. Long total = importTaskMapper.getImportTaskCount(sqls.get(0));
  98. //装载事件信息
  99. this.loadImportTask(importTaskVOS);
  100. Records records = new Records();
  101. records.setCurrent(queryRequest.getCurrent());
  102. records.setSize(queryRequest.getSize());
  103. records.setData(importTaskVOS);
  104. records.setTotal(total);
  105. return records;
  106. }
  107. @Override
  108. public Object addMessage(Object object, List<MultipartFile> files) {
  109. return null;
  110. }
  111. @Override
  112. public Object deleteMessage(List<Integer> ids) throws IOException {
  113. return null;
  114. }
  115. @Override
  116. public Object updateMessage(Object object, List<MultipartFile> files) {
  117. return null;
  118. }
  119. @Override
  120. public Object getGroup(GroupRequest groupRequest, String tableName) throws Exception {
  121. return null;
  122. }
  123. @Override
  124. @Transactional(rollbackFor = Exception.class)
  125. public Object addMessage(Object object) {
  126. if (object == null) {
  127. throw new XiaoShiException("参数不能为空");
  128. }
  129. //TODO 校验参数
  130. ImportTaskDTO importTaskDTO = (ImportTaskDTO) object;
  131. Boolean ifUpdate = importTaskDTO.getIfUpdate();
  132. PersonnelVO personnelVO = new PersonnelVO();
  133. personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  134. if (importTaskDTO.getType() == null) {
  135. importTaskDTO.setType(4);
  136. }
  137. List<String> addPatentNos = importTaskDTO.getPatentNos();
  138. if (!importTaskDTO.getType().equals(1)) {
  139. importTaskDTO.setImportContent(MathUtils.BinaryToDecimal(importTaskDTO.getImportContent()));
  140. }
  141. ImportTaskCondition importTaskCondition = new ImportTaskCondition();
  142. if (ifUpdate != null && ifUpdate) {
  143. QuartzConditionDTO quartzConditionDTO = new QuartzConditionDTO();
  144. BeanUtils.copyProperties(importTaskDTO, quartzConditionDTO);
  145. importTaskCondition = importTaskConditionService.addQuartzCondition(quartzConditionDTO);
  146. } else {
  147. importTaskCondition = importTaskConditionService.addQuartzCondition(importTaskDTO);
  148. }
  149. //装载任务
  150. ImportTask importTask = new ImportTask();
  151. importTask.setImportTaskConditionId(importTaskCondition.getId());
  152. importTask.setType(importTaskDTO.getType());
  153. importTask.setSearchCondition(importTaskDTO.getSearchCondition());
  154. importTask.setCreateId(personnelVO.getId());
  155. if (importTaskCondition.getType().equals(1)) {
  156. try {
  157. String res = fileManagerService.getSystemFileFromFMS(Arrays.asList(importTaskCondition.getFileGuid()));
  158. List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
  159. SystemFile systemFile = systemFiles.get(0);
  160. String suffix = systemFile.getFileName().substring(systemFile.getFileName().lastIndexOf("."));
  161. //调用文件系统取出文件接口,获得文件流
  162. byte[] bytes = fileManagerService.downloadSystemFileFromFMS(importTaskCondition.getFileGuid());
  163. //创建临时文件tempFile,并将文件读取到tempFile
  164. File tempFile = File.createTempFile(systemFile.getFileName() + "temp", suffix);
  165. try (
  166. InputStream inputStream = new ByteArrayInputStream(bytes);
  167. FileOutputStream outputStream = new FileOutputStream(tempFile);
  168. ) {
  169. IOUtils.copy(inputStream, outputStream); // 将输入流复制到临时文件
  170. }
  171. Integer totalNum = ReadExcelUtils.textExcel(tempFile, importTaskCondition.getSourceId().toString());
  172. importTask.setAllNum(totalNum);
  173. //删除临时文件tempFile
  174. new File(tempFile.getPath()).delete();
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. throw new XiaoShiException(e.getMessage());
  178. }
  179. } else if (importTaskCondition.getType().equals(4)) {
  180. try {
  181. if (addPatentNos == null || addPatentNos.size() == 0) {
  182. PatentStarListDTO patentStarListDto = new PatentStarListDTO()
  183. .setCurrentQuery(importTaskCondition.getSearchCondition())
  184. .setOrderBy(importTaskCondition.getOrderBy())
  185. .setOrderByType(importTaskCondition.getOrderByType())
  186. .setPageNum(1)
  187. .setRowCount(10)
  188. .setDBType(importTaskCondition.getDbType());
  189. //调用一般接口返回一批专利著录相关数据
  190. Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(patentStarListDto);
  191. if (resultMap == null || (Integer) resultMap.get("total") == 0) {
  192. ThrowException.throwXiaoShiException("未检索到相关专利");
  193. } else {
  194. importTask.setAllNum((Integer) resultMap.get("total"));
  195. }
  196. } else {
  197. importTask.setAllNum(addPatentNos.size());
  198. }
  199. } catch (Exception e) {
  200. throw new XiaoShiException("文件错误");
  201. }
  202. } else if (importTaskCondition.getType().equals(2) || importTaskCondition.getType().equals(3)) {
  203. List<String> patentNos = new ArrayList<>();
  204. if (importTaskCondition.getType().equals(2)) {
  205. patentNos = Arrays.asList(importTaskCondition.getSearchCondition().split("[,|,]"));
  206. } else if (importTaskCondition.getType().equals(3)) {
  207. try {
  208. String res = fileManagerService.getSystemFileFromFMS(Arrays.asList(importTaskCondition.getFileGuid()));
  209. List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
  210. SystemFile systemFile = systemFiles.get(0);
  211. String suffix = systemFile.getFileName().substring(systemFile.getFileName().lastIndexOf("."));
  212. //调用文件系统取出文件接口,获得文件流
  213. byte[] bytes = fileManagerService.downloadSystemFileFromFMS(importTaskCondition.getFileGuid());
  214. //创建临时文件tempFile,并将文件读取到tempFile
  215. File tempFile = File.createTempFile(systemFile.getFileName() + "temp", suffix);
  216. try (
  217. InputStream inputStream = new ByteArrayInputStream(bytes);
  218. FileOutputStream outputStream = new FileOutputStream(tempFile)
  219. ) {
  220. IOUtils.copy(inputStream, outputStream); // 将输入流复制到临时文件
  221. }
  222. Sheet sheet = ReadExcelUtils.readExcel(tempFile);
  223. patentNos = ReadExcelUtils.getPatentNoFromExcel(sheet);
  224. } catch (Exception e) {
  225. }
  226. }
  227. importTask.setAllNum(patentNos.size());
  228. }
  229. importTask.setProgress(0.0);
  230. String taskName = this.setImportTaskName(importTaskCondition, personnelVO.getName());
  231. importTask.setName(taskName);
  232. importTask.insert();
  233. schedulingTaskService.startTask();
  234. return importTask.getId();
  235. }
  236. @Override
  237. public Object updateMessage(Object object) {
  238. return null;
  239. }
  240. private void loadImportTask(List<ImportTaskVO> importTaskVOS) throws IOException {
  241. List<ImportTaskAMVO> importTaskAMVOS = schedulingTaskService.getImportTaskAMVOs();
  242. if (importTaskAMVOS == null) {
  243. importTaskAMVOS = new ArrayList<>();
  244. }
  245. List<String> createIds = new ArrayList<>();
  246. List<Integer> projectIds = new ArrayList<>();
  247. List<Integer> productIds = new ArrayList<>();
  248. List<String> guids = new ArrayList<>();
  249. importTaskVOS.forEach(item -> {
  250. if (item.getCreateId() != null) {
  251. createIds.add(item.getCreateId());
  252. }
  253. if (item.getProductId() != null) {
  254. productIds.add(item.getProductId());
  255. }
  256. if (item.getProjectId() != null) {
  257. projectIds.add(item.getProjectId());
  258. }
  259. if (item.getFileGuid() != null) {
  260. guids.add(item.getFileGuid());
  261. }
  262. });
  263. List<Personnel> personnels = new ArrayList<>();
  264. List<Product> products = new ArrayList<>();
  265. List<Project> projects = new ArrayList<>();
  266. List<ReportProject> reportProjects = new ArrayList<>();
  267. List<SystemFile> systemFiles = new ArrayList<>();
  268. //查询创建人名称
  269. if (createIds.size() != 0) {
  270. String res = permissionService.getPersonnelByIdsFromPCS(createIds);
  271. JSONObject jsonObject = JSONObject.parseObject(res);
  272. personnels = JSONObject.parseArray(jsonObject.getString("data"), Personnel.class);
  273. }
  274. //查询项目名称
  275. if (projectIds.size() != 0) {
  276. LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
  277. queryWrapper.in(Project::getId, projectIds);
  278. projects = projectService.list(queryWrapper);
  279. LambdaQueryWrapper<ReportProject> queryWrapper1 = new LambdaQueryWrapper<>();
  280. queryWrapper1.in(ReportProject::getProjectId, projectIds);
  281. reportProjects = reportProjectService.list(queryWrapper1);
  282. }
  283. //查询产品名称
  284. if (productIds.size() != 0) {
  285. LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
  286. queryWrapper.in(Product::getId, productIds);
  287. products = productService.list(queryWrapper);
  288. }
  289. //查询文件
  290. if (guids.size() != 0) {
  291. String res = fileManagerService.getSystemFileFromFMS(guids);
  292. systemFiles = JSONObject.parseArray(res, SystemFile.class);
  293. if (systemFiles == null) {
  294. systemFiles = new ArrayList<>();
  295. }
  296. }
  297. LambdaQueryWrapper<SystemDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  298. lambdaQueryWrapper.eq(SystemDict::getGroupType, "IMPORT_TASK");
  299. List<SystemDict> systemDictList = systemDictService.list(lambdaQueryWrapper);
  300. for (ImportTaskVO importTaskVO : importTaskVOS) {
  301. //装载创建人
  302. Personnel personnel = personnels.stream().filter(item -> item.getId().equals(importTaskVO.getCreateId())).findFirst().orElse(null);
  303. if (personnel != null) {
  304. importTaskVO.setCreateName(personnel.getPersonnelName());
  305. }
  306. //装载状态
  307. SystemDict systemDict1 = systemDictList.stream()
  308. .filter(item -> item.getType().equals("IMPORT_TASK_TYPE") && item.getType()
  309. .equals(importTaskVO.getType().toString())).findFirst().orElse(null);
  310. SystemDict systemDict2 = systemDictList.stream()
  311. .filter(item -> item.getType().equals("IMPORT_TASk_STATE") && item.getType()
  312. .equals(importTaskVO.getState().toString())).findFirst().orElse(null);
  313. if (systemDict1 != null) {
  314. importTaskVO.setTypeStr(systemDict1.getLabel());
  315. }
  316. if (systemDict2 != null) {
  317. importTaskVO.setStateStr(systemDict2.getLabel());
  318. }
  319. //装载正在进行中任务的完成条数
  320. ImportTaskAMVO importTaskAMVO = importTaskAMVOS.stream().filter(item -> item.getId().equals(importTaskVO.getId())).findFirst().orElse(null);
  321. if (importTaskAMVO != null) {
  322. importTaskVO.setDoneNum(importTaskAMVO.getDoneNum());
  323. importTaskAMVO.setAllNum(importTaskAMVO.getAllNum());
  324. }
  325. long percentage = (long) Math.floor((importTaskVO.getDoneNum() + 0D) / importTaskVO.getAllNum() * 100D);
  326. importTaskVO.setPercentage(percentage);
  327. //装载目标名称
  328. if (projects.size() > 0 && importTaskVO.getProjectId() != null) {
  329. Project project = projects.stream().filter(item -> item.getId().equals(importTaskVO.getProjectId())).findFirst().orElse(null);
  330. if (project != null) {
  331. importTaskVO.setImportToId(project.getId());
  332. importTaskVO.setProjectType(project.getType());
  333. if (project.getType().equals(1)) {
  334. importTaskVO.setImportToName(project.getName() + "-专利数据库");
  335. } else if (project.getType().equals(4)) {
  336. if (StringUtils.isNotEmpty(project.getName())) {
  337. importTaskVO.setImportToName(project.getName() + "-报告");
  338. } else {
  339. importTaskVO.setImportToName("查新检索" + "-报告");
  340. }
  341. } else if (project.getType().equals(2)) {
  342. ReportProject reportProject = reportProjects.stream().filter(item -> item.getProjectId().equals(importTaskVO.getProjectId())).findFirst().orElse(null);
  343. if (reportProject != null) {
  344. importTaskVO.setReportType(reportProject.getReportType());
  345. }
  346. importTaskVO.setImportToName(project.getName() + "-报告");
  347. }
  348. }
  349. } else if (products.size() > 0 && importTaskVO.getProductId() != null) {
  350. Product product = products.stream().filter(item -> item.getId().equals(importTaskVO.getProductId())).findFirst().orElse(null);
  351. if (product != null) {
  352. importTaskVO.setImportToId(product.getId());
  353. importTaskVO.setImportToType(2);
  354. importTaskVO.setImportToName(product.getName());
  355. }
  356. }
  357. if (importTaskVO.getFileGuid() != null) {
  358. SystemFile systemFile = systemFiles.stream().filter(item -> item.getGuid().equals(importTaskVO.getFileGuid())).findFirst().orElse(null);
  359. importTaskVO.setSystemFile(systemFile);
  360. }
  361. if (importTaskVO.getImportContent() != null) {
  362. String icn = this.getImportContentStr(importTaskVO.getImportContent(), importTaskVO.getType());
  363. importTaskVO.setImportContentName(icn);
  364. }
  365. }
  366. }
  367. private String setImportTaskName(ImportTaskCondition importTaskCondition, String name) {
  368. //装载名称
  369. String type = "";
  370. String toName = "";
  371. String to = "";
  372. String taskName = "";
  373. if (importTaskCondition.getType().equals(2)) {
  374. type = "【专利号】";
  375. } else if (importTaskCondition.getType().equals(3) || importTaskCondition.getType().equals(4)) {
  376. type = "【检索式】";
  377. } else if (importTaskCondition.getType().equals(1)) {
  378. type = "【Excel】";
  379. }
  380. if (importTaskCondition.getProjectId() != null && importTaskCondition.getProjectType().equals(0)) {
  381. to = "专题库";
  382. LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
  383. queryWrapper.in(Project::getId, importTaskCondition.getProjectId());
  384. List<Project> projects = projectService.list(queryWrapper);
  385. if (projects.size() != 0) {
  386. toName = projects.get(0).getName();
  387. }
  388. } else if (importTaskCondition.getProjectId() != null && importTaskCondition.getProjectType().equals(1)) {
  389. to = "报告";
  390. LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
  391. queryWrapper.in(Project::getId, importTaskCondition.getProjectId());
  392. List<Project> projects = projectService.list(queryWrapper);
  393. if (projects.size() != 0) {
  394. toName = projects.get(0).getName();
  395. }
  396. } else if (importTaskCondition.getProjectId() != null && importTaskCondition.getProjectType().equals(4)) {
  397. to = "报告";
  398. LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
  399. queryWrapper.in(Project::getId, importTaskCondition.getProjectId());
  400. List<Project> projects = projectService.list(queryWrapper);
  401. if (!CollectionUtils.isEmpty(projects) && StringUtils.isNotEmpty(projects.get(0).getName())) {
  402. toName = projects.get(0).getName();
  403. } else {
  404. toName = "查新检索";
  405. }
  406. } else if (importTaskCondition.getProductId() != null) {
  407. to = "产品";
  408. LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
  409. queryWrapper.in(Product::getId, importTaskCondition.getProductId());
  410. List<Product> products = productService.list(queryWrapper);
  411. if (products.size() != 0) {
  412. toName = products.get(0).getName();
  413. }
  414. }
  415. //导入任务
  416. taskName = type + "导入到" + to + "【" + toName + "】 - " + name;
  417. return taskName;
  418. }
  419. public void updateImportTaskState(Integer taskId, Integer state) {
  420. ImportTask importTask = this.getById(taskId);
  421. importTask.setState(state);
  422. importTask.updateById();
  423. if (state.equals(4) || state.equals(5)) {
  424. schedulingTaskService.pauseTask(taskId, state);
  425. }
  426. if (state.equals(0)) {
  427. schedulingTaskService.startTask();
  428. }
  429. }
  430. public Integer addExportTask(ExportTaskDTO exportTaskDTO) {
  431. if (exportTaskDTO == null) {
  432. throw new XiaoShiException("入参不能为空");
  433. }
  434. ImportTaskCondition importTaskCondition = new ImportTaskCondition();
  435. if (exportTaskDTO.getProjectId() != null) {
  436. importTaskCondition.setProjectId(exportTaskDTO.getProjectId());
  437. }
  438. importTaskCondition.setIfUpdate(false);
  439. importTaskCondition.setType(exportTaskDTO.getType());
  440. importTaskCondition.setCreateId(exportTaskDTO.getCreateId());
  441. importTaskCondition.insert();
  442. Integer importTaskConditionId = importTaskCondition.getId();
  443. if (importTaskConditionId != null) {
  444. ImportTask importTask = new ImportTask();
  445. importTask.setImportTaskConditionId(importTaskConditionId);
  446. //设置任务的类型 6导出excel 7导出pdf首页
  447. importTask.setType(exportTaskDTO.getType());
  448. importTask.setAllNum(exportTaskDTO.getAllNum());
  449. importTask.setFinishTime(exportTaskDTO.getFinishTime());
  450. importTask.setState(0);
  451. importTask.setCreateId(exportTaskDTO.getCreateId());
  452. String name = "";
  453. if (exportTaskDTO.getType().equals(6)) {
  454. name = "【专利】";
  455. } else if (exportTaskDTO.getType().equals(7)) {
  456. name = "【PDF首页】";
  457. }
  458. String importTaskName = "导出" + name + "-" + exportTaskDTO.getCreateName();
  459. importTask.setName(importTaskName);
  460. importTask.insert();
  461. return importTask.getId();
  462. } else {
  463. throw new XiaoShiException("导出任务记录失败");
  464. }
  465. }
  466. public Integer updateExportTask(ExportTaskDTO exportTaskDTO) {
  467. if (exportTaskDTO == null) {
  468. throw new XiaoShiException("入参不能为空");
  469. }
  470. ImportTask importTask = this.getById(exportTaskDTO.getTaskId());
  471. if (importTask != null) {
  472. Integer importTaskConditionId = importTask.getImportTaskConditionId();
  473. ImportTaskCondition importTaskCondition = importTaskConditionService.getById(importTaskConditionId);
  474. importTaskCondition.setFileGuid(exportTaskDTO.getFileGuid());
  475. importTaskCondition.updateById();
  476. importTask.setState(exportTaskDTO.getState());
  477. if (exportTaskDTO.getAllNum() != null) {
  478. importTask.setAllNum(exportTaskDTO.getAllNum());
  479. }
  480. importTask.setDoneNum(exportTaskDTO.getDoneNum());
  481. if (exportTaskDTO.getDefaultNum() != null) {
  482. importTask.setDefaultNum(exportTaskDTO.getDefaultNum());
  483. }
  484. if (exportTaskDTO.getDoneNum() != null) {
  485. importTask.setDoneNum(exportTaskDTO.getDoneNum());
  486. }
  487. importTask.setFinishTime(exportTaskDTO.getFinishTime());
  488. importTask.updateById();
  489. }
  490. return importTask.getId();
  491. }
  492. //装载查询语句
  493. private List<String> loadSearchSql(List<String> sqls, Integer projectId) {
  494. PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
  495. String id = personnelVO.getId();
  496. String rootSql = "";
  497. if (sqls.get(0) == null || projectId == null) {
  498. rootSql = "it.create_id=" + id;
  499. } else {
  500. Project project = projectService.getById(projectId);
  501. if (project == null) {
  502. throw new XiaoShiException("未查询到专题库");
  503. }
  504. String createId = project.getCreateId();
  505. String headId = project.getHeadId();
  506. if (createId != id && headId != id) {
  507. rootSql = "it.create_id=" + id;
  508. }
  509. }
  510. if (sqls.get(0) != null && !sqls.get(0).equals("") && !rootSql.equals("")) {
  511. sqls.set(0, rootSql + " and " + "(" + sqls.get(0) + ")");
  512. } else if ((sqls.get(0) == null || sqls.get(0).equals("")) && !rootSql.equals("")) {
  513. sqls.set(0, rootSql);
  514. }
  515. return sqls;
  516. }
  517. public Integer addPdfTask(AddImportPDFTaskDTO addImportPDFTaskDTO) {
  518. String fileGuid = addImportPDFTaskDTO.getFileGuid();
  519. Integer pdfType = addImportPDFTaskDTO.getPdfType();
  520. String remark = addImportPDFTaskDTO.getRemark();
  521. ImportTaskCondition importTaskCondition = new ImportTaskCondition();
  522. importTaskCondition.setFileGuid(fileGuid);
  523. importTaskCondition.setType(5);
  524. importTaskCondition.setPdfType(pdfType);
  525. importTaskCondition.setCreateId("1");
  526. importTaskCondition.insert();
  527. ImportTask importTask = new ImportTask();
  528. importTask.setImportTaskConditionId(importTaskCondition.getId());
  529. importTask.setRemark(remark);
  530. importTask.setCreateId("1");
  531. importTask.setType(5);
  532. importTask.insert();
  533. schedulingTaskService.startTask();
  534. return importTask.getId();
  535. }
  536. public void addImportTaskByCondition(ImportTaskCondition importTaskCondition, PatentProject project) {
  537. String condition = importTaskCondition.getSearchCondition();
  538. String updateCycle = project.getUpdateCycle();
  539. if (updateCycle == null) {
  540. return;
  541. }
  542. LocalDate localDate = LocalDate.now();
  543. List<String> dateRange = new ArrayList<>();
  544. switch (updateCycle) {
  545. case Cycle.WEEK:
  546. LocalDate localDateWeek = localDate.minusWeeks(2);
  547. dateRange = DateUtils2.getWeekRange(localDateWeek);
  548. break;
  549. case Cycle.MONTH:
  550. LocalDate localDateMonth = localDate.minusMonths(1);
  551. dateRange = DateUtils2.getMonthRange(localDateMonth);
  552. break;
  553. case Cycle.QUARTER:
  554. LocalDate localDateQuartz = localDate.minusMonths(4);
  555. dateRange = DateUtils2.getQuartzRange(localDateQuartz);
  556. break;
  557. case Cycle.YEAR:
  558. LocalDate localYear = localDate.minusYears(1);
  559. dateRange = DateUtils2.getYearRange(localYear);
  560. break;
  561. }
  562. if (dateRange.size() < 2) {
  563. return;
  564. }
  565. Date start = DateUtils.strToDate(dateRange.get(0));
  566. Date end = DateUtils.strToDate((dateRange.get(1)));
  567. LambdaQueryWrapper<ImportTask> queryWrapper = new LambdaQueryWrapper<>();
  568. queryWrapper.eq(ImportTask::getImportTaskConditionId, importTaskCondition.getId())
  569. .eq(ImportTask::getUpdateToDate, end)
  570. .eq(ImportTask::getLastUpdateToDate, start);
  571. ImportTask lastImportTask = this.getOne(queryWrapper, false);
  572. if (lastImportTask != null) {
  573. return;
  574. }
  575. ImportTask importTask = new ImportTask();
  576. String fromDateStr = DateUtils2.dateTimeToPatentStr(start);
  577. String todateStr = DateUtils2.dateTimeToPatentStr(end);
  578. importTask.setLastUpdateToDate(start);
  579. importTask.setUpdateToDate(end);
  580. condition = condition + " and (PD=" + fromDateStr + "~" + todateStr + " or GD=" + fromDateStr + "~" + todateStr + ")";
  581. PatentStarListDTO patentStarListDto = new PatentStarListDTO()
  582. .setCurrentQuery(condition)
  583. .setPageNum(1)
  584. .setRowCount(10)
  585. .setDBType(importTaskCondition.getDbType());
  586. //调用一般接口返回一批专利著录相关数据
  587. try {
  588. Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(patentStarListDto);
  589. if (resultMap == null) {
  590. importTask.setAllNum(-1);
  591. } else {
  592. importTask.setAllNum((Integer) resultMap.get("total"));
  593. }
  594. } catch (Exception e) {
  595. importTask.setAllNum(-1);
  596. e.printStackTrace();
  597. }
  598. importTask.setImportTaskConditionId(importTaskCondition.getId());
  599. importTask.setType(importTaskCondition.getType());
  600. importTask.setSearchCondition(condition);
  601. importTask.setCreateId(importTaskCondition.getCreateId());
  602. importTask.setIfCreateBySys(true);
  603. if (importTask.getAllNum() != null && importTask.getAllNum() <= 0) {
  604. importTask.setState(2);
  605. if (importTask.getAllNum() < 0) {
  606. mailSendService.sendSysCycleStartEmail("任务查询专利报错,请管理员手动更正");
  607. }
  608. importTask.insert();
  609. } else {
  610. importTask.insert();
  611. schedulingTaskService.startTask();
  612. }
  613. ImportTaskAMVO importTaskAMVO = new ImportTaskAMVO();
  614. importTaskAMVO.setUpdateToDate(importTask.getUpdateToDate());
  615. importTaskAMVO.setUpdateCycle(updateCycle);
  616. importTaskAMVO.setProjectId(project.getProjectId());
  617. List<EsCustomFieldDTO> dtoList = webVOTransformService.getEsCustomFieldDTOList(importTaskAMVO);
  618. }
  619. public String getImportContentStr(Integer importContent, Integer taskType) {
  620. List<String> contents = new ArrayList<>();
  621. String imContents = "0000";
  622. //当任务为excel导入
  623. if (taskType.equals(1)) {
  624. imContents = "1100";
  625. }
  626. //当任务为pdf导入
  627. else if (taskType.equals(5)) {
  628. imContents = "0001";
  629. } else if (!importContent.equals(0)) {
  630. imContents = MathUtils.fun(2, importContent);
  631. }
  632. //下载字段
  633. char[] importCells = imContents.toCharArray();
  634. char ifCataloguing = importCells[0];
  635. char ifAddPicture = importCells[1];
  636. char ifFullText = importCells[2];
  637. char ifPdf = importCells[3];
  638. if (ifCataloguing == '1') {
  639. contents.add("著录信息");
  640. }
  641. if (ifAddPicture == '1') {
  642. contents.add("摘要附图");
  643. }
  644. if (ifFullText == '1') {
  645. contents.add("权要或说明书");
  646. }
  647. if (ifPdf == '1') {
  648. contents.add("pdf或外观附图");
  649. }
  650. String contentStr = "";
  651. if (contents.size() > 0) {
  652. contentStr = StringUtils.join(contents, " ");
  653. }
  654. return contentStr;
  655. }
  656. }