ImportTaskService.java 30 KB

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