GzArchivesServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. package me.zhengjie.archives.service.impl;
  2. import java.io.File;
  3. import java.time.LocalDate;
  4. import java.time.LocalDateTime;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.stream.Collectors;
  10. import org.apache.commons.collections4.CollectionUtils;
  11. import org.apache.commons.lang.StringUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.web.bind.annotation.RequestBody;
  15. import com.alibaba.fastjson.JSON;
  16. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  17. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  18. import com.baomidou.mybatisplus.core.metadata.IPage;
  19. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  20. import me.zhengjie.annotation.rest.AnonymousPostMapping;
  21. import me.zhengjie.archives.dao.IGzArchivesDao;
  22. import me.zhengjie.archives.dao.IModelDao;
  23. import me.zhengjie.archives.dto.GzDetailDto;
  24. import me.zhengjie.archives.entity.ArchivesEntity;
  25. import me.zhengjie.archives.entity.FileInfoEntity;
  26. import me.zhengjie.archives.entity.GzArchivesEntity;
  27. import me.zhengjie.archives.entity.GzArchivesExEntity;
  28. import me.zhengjie.archives.entity.GzDetailEntity;
  29. import me.zhengjie.archives.entity.GzFileTempEntity;
  30. import me.zhengjie.archives.entity.GzFilepathEntity;
  31. import me.zhengjie.archives.entity.ModelEntity;
  32. import me.zhengjie.archives.plus.AbstractServiceImpl;
  33. import me.zhengjie.archives.plus.QueryMap;
  34. import me.zhengjie.archives.service.FileInfoService;
  35. import me.zhengjie.archives.service.GzArchivesExService;
  36. import me.zhengjie.archives.service.GzArchivesService;
  37. import me.zhengjie.archives.service.GzDetailService;
  38. import me.zhengjie.archives.service.GzFileTempService;
  39. import me.zhengjie.archives.service.GzFilepathService;
  40. import me.zhengjie.archives.util.HtmlToImage;
  41. import me.zhengjie.archives.util.PdfUtils;
  42. import me.zhengjie.archives.vo.GzArchivesVo;
  43. import me.zhengjie.archives.vo.GzPortraitVo;
  44. import me.zhengjie.base.ResponseDTO;
  45. import me.zhengjie.config.FileProperties;
  46. import me.zhengjie.exception.BadRequestException;
  47. import me.zhengjie.modules.system.service.DictDetailService;
  48. import me.zhengjie.utils.DateUtil;
  49. import me.zhengjie.utils.FileUtil;
  50. import me.zhengjie.utils.PageUtil;
  51. import me.zhengjie.utils.UUIDGenerator;
  52. /**
  53. * <p>
  54. * 服务实现类
  55. * </p>
  56. *
  57. * @author <a href="https://www.fengwenyi.com?code">Erwin Feng</a>
  58. * @since 2023-06-25
  59. */
  60. @Service
  61. public class GzArchivesServiceImpl extends AbstractServiceImpl<IGzArchivesDao, GzArchivesEntity>
  62. implements GzArchivesService {
  63. @Autowired
  64. DictDetailService dictDetailService;
  65. @Autowired
  66. IModelDao modelDao;
  67. @Autowired
  68. private FileProperties properties;
  69. @Autowired
  70. FileInfoService fileInfoService;
  71. @Autowired
  72. private GzFileTempService gzFileTempService;
  73. @Autowired
  74. GzArchivesExService gzArchivesExService;
  75. @Autowired
  76. private GzDetailService gzDetailService;
  77. @Autowired
  78. private GzFilepathService gzFilepathService;
  79. public void mergeSubmit(GzPortraitVo archives) throws Exception {
  80. image(archives);
  81. }
  82. public void image(GzPortraitVo archives) throws Exception {
  83. String path = properties.getPath().getPath();
  84. // 查询对应的值
  85. GzArchivesEntity entity = this.getById(archives.getId());
  86. // 保管期限
  87. String saveTerm = dictDetailService.getLabelByValue("saveTerm_type", entity.getKeepingterm());
  88. // 卷宗号
  89. String dossierNum = entity.getYear() + "-" + entity.getNotarizationtypefull() + "-" + saveTerm + "-"
  90. + entity.getFileno();
  91. // TODO 当事人很多,需要修改字体-------------------页号开始------------------------
  92. QueryWrapper<ModelEntity> queryWrapper = new QueryWrapper<ModelEntity>();
  93. queryWrapper.eq("code", 1);
  94. ModelEntity model = modelDao.selectOne(queryWrapper);
  95. String fileRelatePath = FileUtil.getUploadPath(path);
  96. String filePath = path + fileRelatePath;
  97. String headPageHtml = model.getContent();
  98. // 替换内容
  99. headPageHtml = headPageHtml.replace("${year}", entity.getYear().toString())
  100. .replace("${notarialNum}", entity.getNo()).replace("${notarizedItem}", entity.getTitle())
  101. .replace("${partyName}", entity.getConcernedname()).replace("${undertaker}", entity.getUndertaker())
  102. .replace("${notary}", entity.getNotary())
  103. .replace("${acceptDate}", DateUtil.localDate(entity.getAcceptdate()))
  104. .replace("${completeDate}", DateUtil.localDate(entity.getFinishdate()))
  105. .replace("${filingDate}", DateUtil.localDate(entity.getFiledate())).replace("${saveTerm}", saveTerm)
  106. .replace("${dossierNum}", dossierNum).replace("${acceptNum}", entity.getAcceptno())
  107. .replace("${fondsNum}", entity.getFondcode()).replace("${catalogNum}", entity.getMenuno())
  108. .replace("${caseNum}", entity.getFileno());
  109. String headPagePath = filePath + UUIDGenerator.uuid() + ".html";
  110. String headPageToImage = filePath + UUIDGenerator.uuid() + ".jpg";
  111. // 先写入文本
  112. FileUtil.writeUtf8String(headPageHtml, headPagePath);
  113. // 然后生成jpg
  114. String exePath = dictDetailService.getValueByName("file_path").get("exe_path");
  115. HtmlToImage.htmlToImg(exePath, headPagePath, headPageToImage);
  116. FileUtil.deleteFile(new File(headPagePath));
  117. // -------------------页号结束------------------------
  118. // --------------------目录号开始---------
  119. queryWrapper = new QueryWrapper<ModelEntity>();
  120. queryWrapper.eq("code", 2);
  121. model = modelDao.selectOne(queryWrapper);
  122. String catalogHtml = model.getContent();
  123. int j = 0;
  124. List<String> pages = archives.getPages();
  125. for (int i = 0; i < pages.size(); i++) {
  126. j = i + 1;
  127. catalogHtml = catalogHtml.replace("${num" + j + "}", pages.get(i));
  128. }
  129. String catalogPath = filePath + UUIDGenerator.uuid() + ".html";
  130. String catalogToImage = filePath + UUIDGenerator.uuid() + ".jpg";
  131. // 先写入文本
  132. FileUtil.writeUtf8String(catalogHtml, catalogPath);
  133. // 然后生成jpg
  134. HtmlToImage.htmlToImg(exePath, catalogPath, catalogToImage);
  135. FileUtil.deleteFile(new File(catalogPath));
  136. List<File> files = new ArrayList<File>();
  137. // 页头第一个
  138. files.add(new File(headPageToImage));
  139. // 目录放在第二个
  140. files.add(new File(catalogToImage));
  141. List<String> imageIds = archives.getImageIds();
  142. // 区分是新增加的,还是老的数据,历史数据是32位,新的是22位
  143. List<String> fileInfoImages = new ArrayList<>();
  144. List<String> gzFileImages = new ArrayList<>();
  145. // 判断
  146. for (String imageId : imageIds) {
  147. if (imageId.length() == 32) {
  148. gzFileImages.add(imageId);
  149. } else {
  150. fileInfoImages.add(imageId);
  151. }
  152. }
  153. // 这个是新的图片地址
  154. Map<String, FileInfoEntity> fileInfoMap = new HashMap<>();
  155. if(CollectionUtils.isNotEmpty(fileInfoImages)) {
  156. List<FileInfoEntity> fileInfo = fileInfoService.listByIds(fileInfoImages);
  157. for (FileInfoEntity file : fileInfo) {
  158. fileInfoMap.put(file.getId(), file);
  159. }
  160. }
  161. // ------------------------这个是老的地址
  162. Map<String, GzFileTempEntity> tempFilesMap = new HashMap<>();
  163. if(CollectionUtils.isNotEmpty(gzFileImages)) {
  164. List<GzFileTempEntity> tempFiles = gzFileTempService.listByIds(gzFileImages);
  165. for (GzFileTempEntity file : tempFiles) {
  166. tempFilesMap.put(file.getId(), file);
  167. }
  168. }
  169. for (String imageId : imageIds) {
  170. if (imageId.length() == 32) {
  171. if(tempFilesMap.get(imageId)!=null) {
  172. GzFileTempEntity gzFileEntity = tempFilesMap.get(imageId);
  173. files.add(new File(gzFileEntity.getFilePath() + gzFileEntity.getFileName()));
  174. }
  175. } else {
  176. if(fileInfoMap.get(imageId)!=null) {
  177. files.add(new File(path + fileInfoMap.get(imageId).getPath()));
  178. }
  179. }
  180. }
  181. // --------------------------------------------------
  182. String fileName = "merge_pdf" + UUIDGenerator.uuid() + ".pdf";
  183. String toPdfTarget = filePath + fileName;
  184. // pdf进行合并
  185. PdfUtils.manyImageToOnePdf(files, toPdfTarget);
  186. // 先更新主表
  187. entity = new GzArchivesEntity();
  188. entity.setId(archives.getId());
  189. entity.setRecflag("1");
  190. entity.setPdftime(LocalDate.now());
  191. this.updateById(entity);
  192. // -------------------
  193. GzArchivesExEntity archiveEntity = new GzArchivesExEntity();
  194. archiveEntity.setGzArchivesId(archives.getId());
  195. GzArchivesExEntity gzEx = gzArchivesExService.getOne(archiveEntity);
  196. if (gzEx == null) {
  197. gzEx = new GzArchivesExEntity();
  198. gzEx.setPdfPath(fileRelatePath + fileName);
  199. gzEx.setGzArchivesId(archives.getId());
  200. gzEx.setImageJson(JSON.toJSONString(archives));
  201. gzEx.setCreateTime(LocalDateTime.now());
  202. gzEx.setUpdateTime(LocalDateTime.now());
  203. gzEx.setPdfPath(fileRelatePath+fileName);
  204. gzArchivesExService.save(gzEx);
  205. } else {
  206. gzEx.setGzArchivesId(archives.getId());
  207. gzEx.setPdfPath(fileRelatePath + fileName);
  208. gzEx.setImageJson(JSON.toJSONString(archives));
  209. gzEx.setPdfPath(fileRelatePath+fileName);
  210. gzEx.setUpdateTime(LocalDateTime.now());
  211. gzArchivesExService.updateById(gzEx);
  212. }
  213. UpdateWrapper<FileInfoEntity> updateWrapper = new UpdateWrapper<>();
  214. updateWrapper.in("id", imageIds);
  215. updateWrapper.set("loan_no", archives.getId());
  216. this.fileInfoService.update(updateWrapper);
  217. }
  218. @AnonymousPostMapping("/detail")
  219. public ResponseDTO<GzDetailDto> detail(GzArchivesVo req) {
  220. GzArchivesEntity entity = this.getById(req.getId());
  221. // GzArchivesDto dto = BeanCopyUtils.convertObj(entity, GzArchivesDto.class);
  222. GzDetailDto dto = new GzDetailDto();
  223. convertArchives(entity, dto);
  224. // 先查询有没有处理过,然后再处理老的数据
  225. GzArchivesExEntity gzExEntity = new GzArchivesExEntity();
  226. gzExEntity.setGzArchivesId(req.getId());
  227. gzExEntity = gzArchivesExService.getOne(gzExEntity);
  228. if (gzExEntity != null) {
  229. String previewPath = dictDetailService.getValueByName("file_path").get("preview_path");
  230. Map<String,String> pdfFileName = new HashMap<>();
  231. pdfFileName.put("pdfPath", previewPath + gzExEntity.getPdfPath());
  232. // 卷宗号
  233. pdfFileName.put("pdfName", entity.getArcno());
  234. List<Map<String,String>> pdfList=new ArrayList<>();
  235. pdfList.add(pdfFileName);
  236. dto.setFilePath(pdfList);
  237. return ResponseDTO.success(dto);
  238. }
  239. QueryWrapper<GzDetailEntity> qw = new QueryWrapper<>();
  240. qw.eq("puuid", req.getId());
  241. List<String> gzIds = gzDetailService.list(qw).stream().map(e -> e.getId()).collect(Collectors.toList());
  242. QueryWrapper<GzFilepathEntity> gzFilepathQw = new QueryWrapper<>();
  243. gzFilepathQw.in("puuid", gzIds);
  244. gzFilepathQw.orderByAsc(" arccode+0 ");
  245. String gzPath = dictDetailService.getValueByName("file_path").get("gz_path");
  246. List<GzFilepathEntity> gzFilePaths = gzFilepathService.list(gzFilepathQw);
  247. if (CollectionUtils.isNotEmpty(gzFilePaths)) {
  248. List<Map<String,String>> pdfs = new ArrayList<>();
  249. for (GzFilepathEntity gz : gzFilePaths) {
  250. //pdfs.add(gzPath + gz.getSubpath() + gz.getFilename() + gz.getExt());
  251. Map<String,String> pdfFileName = new HashMap<>();
  252. pdfFileName.put("pdfPath", gzPath + gz.getSubpath() + gz.getFilename() + gz.getExt());
  253. pdfFileName.put("pdfName", gz.getFilename());
  254. pdfs.add(pdfFileName);
  255. }
  256. dto.setFilePath(pdfs);
  257. return ResponseDTO.success(dto);
  258. }
  259. return ResponseDTO.success(dto);
  260. }
  261. public Map<String, Object> page(GzArchivesVo req) {
  262. IPage<GzArchivesEntity> page = new Page<>(req.getIndex() + 1, req.getSize());
  263. GzArchivesEntity gzArchives = new GzArchivesEntity();
  264. convertGz(gzArchives, req);
  265. QueryMap queryMap = new QueryMap();
  266. Map<String, Object> map = new HashMap<>();
  267. List<String> accept = req.getAcceptDates();
  268. if (CollectionUtils.isNotEmpty(accept)) {
  269. String stringDate = accept.get(0);
  270. map.put("startTime", stringDate);
  271. String endDate = accept.get(1);
  272. map.put("endTime", endDate);
  273. queryMap.setMap(map);
  274. }
  275. List<String> submitTime = req.getSubmitTimes();
  276. if (CollectionUtils.isNotEmpty(submitTime)) {
  277. map.put("submitStartTime", submitTime.get(0));
  278. map.put("submitEndTime", submitTime.get(1));
  279. queryMap.setMap(map);
  280. }
  281. page = page(page, gzArchives, queryMap);
  282. List<GzDetailDto> list = new ArrayList<>();
  283. if (page.getRecords() != null) {
  284. for (GzArchivesEntity gz : page.getRecords()) {
  285. GzDetailDto archivesEntity = new GzDetailDto();
  286. convertArchives(gz, archivesEntity);
  287. list.add(archivesEntity);
  288. }
  289. }
  290. return PageUtil.toPage(list, page.getTotal());
  291. }
  292. private void convertArchives(GzArchivesEntity gz, GzDetailDto archivesEntity) {
  293. StringBuffer sb = new StringBuffer();
  294. sb.append(gz.getYear() + "-");
  295. sb.append(gz.getNotarizationtypefull() + "-");
  296. sb.append(gz.getKeepingterm() + "-");
  297. sb.append(gz.getFileno());
  298. archivesEntity.setId(gz.getId());
  299. archivesEntity.setArchivesNum(gz.getArcno());
  300. archivesEntity.setDossierNum(sb.toString());
  301. archivesEntity.setNotarizationType(gz.getNotarialOfficeFull());
  302. archivesEntity.setNotarizedItem(gz.getTitle());
  303. archivesEntity.setNotarialNum(gz.getNotarizationnumber());
  304. archivesEntity.setNotary(gz.getNotary());
  305. archivesEntity.setUndertaker(gz.getUndertaker());
  306. archivesEntity.setPartyName(gz.getConcernedname());
  307. archivesEntity.setAcceptNum(gz.getAcceptno());
  308. archivesEntity.setAnnual(gz.getYear().toString());
  309. archivesEntity.setAcceptDate(DateUtil.formatYmd(gz.getAcceptdate()));
  310. archivesEntity.setCompleteDate(DateUtil.formatYmd(gz.getFinishdate()));
  311. archivesEntity.setFilingDate(DateUtil.formatYmd(gz.getFiledate()));
  312. archivesEntity.setSaveTerm(gz.getKeepingterm());
  313. archivesEntity.setSecrecyType(gz.getSecretclass());
  314. archivesEntity.setNotaryFullName(gz.getNotarialOfficeFull());
  315. archivesEntity.setCaseNum(gz.getFileno());
  316. archivesEntity.setStatus(gz.getRecflag());
  317. archivesEntity.setFondsNum(gz.getFondcode());
  318. archivesEntity.setCatalogNum(gz.getMenuno());
  319. archivesEntity.setBoxNum(gz.getBoxnumber());
  320. archivesEntity.setCreateTime(gz.getCreatetime());
  321. archivesEntity.setDossierType(gz.getMenuno().split("\\.")[1]);
  322. }
  323. public static void main(String[] args) {
  324. System.out.println("1.2".split("\\.")[1]);
  325. }
  326. public ResponseDTO<GzArchivesEntity> edit(@RequestBody GzArchivesVo req) {
  327. GzArchivesEntity entity = getById(req.getId());
  328. String arcno=entity.getArcno();
  329. convertGz(entity, req);
  330. QueryWrapper<GzArchivesEntity> qw = new QueryWrapper<>();
  331. qw.ne("arcno", arcno);
  332. qw.eq("arcno", req.getArchivesNum());
  333. long result = this.count(qw);
  334. // 过来掉自己
  335. if (result >= 1) {
  336. // return ResponseDTO.error(ResultCode.ARCHIVES_NUM_ALREADY_EXISTS,
  337. // archives.getArchivesNum());
  338. throw new BadRequestException(entity.getArcno() + "档案号已存在!");
  339. }
  340. this.updateById(entity);
  341. return ResponseDTO.success();
  342. }
  343. private void convertGz(GzArchivesEntity gz, GzArchivesVo archivesEntity) {
  344. // 目录号
  345. gz.setMenuno(archivesEntity.getCatalogNum());
  346. // caseNum 案卷号
  347. gz.setFileno(archivesEntity.getCaseNum());
  348. // 公证书编号
  349. gz.setNotarizationnumber(archivesEntity.getNotarialNum());
  350. // 当事人姓名
  351. gz.setConcernedname(archivesEntity.getPartyName());
  352. // 公证员
  353. gz.setNotary(archivesEntity.getNotary());
  354. // 承办人
  355. gz.setUndertaker(archivesEntity.getUndertaker());
  356. // 公证事项
  357. gz.setTitle(archivesEntity.getNotarizedItem());
  358. // 受理号
  359. gz.setAcceptno(archivesEntity.getAcceptNum());
  360. if (StringUtils.isNotBlank(archivesEntity.getAnnual())) {
  361. gz.setYear(Integer.parseInt(archivesEntity.getAnnual()));
  362. }
  363. // 年度
  364. gz.setArcno(archivesEntity.getArchivesNum());
  365. // 三个时间
  366. if (StringUtils.isNotBlank(archivesEntity.getAcceptDate())) {
  367. gz.setAcceptdate(DateUtil.parseYmd(archivesEntity.getAcceptDate()));
  368. }
  369. if (StringUtils.isNotBlank(archivesEntity.getCompleteDate())) {
  370. gz.setFinishdate(DateUtil.parseYmd(archivesEntity.getCompleteDate()));
  371. }
  372. if (StringUtils.isNotBlank(archivesEntity.getFilingDate())) {
  373. gz.setFiledate(DateUtil.parseYmd(archivesEntity.getFilingDate()));
  374. }
  375. //设置状态值
  376. gz.setRecflag(archivesEntity.getStatus());
  377. // 保管类型
  378. gz.setSecretclass(archivesEntity.getSecrecyType());
  379. // 保管期限
  380. gz.setKeepingterm(archivesEntity.getSaveTerm());
  381. // 将类型转成汉字
  382. String dossierType = dictDetailService.getLabelByValue("saveTerm_type", archivesEntity.getDossierType());
  383. gz.setNotarizationtypefull(dossierType);
  384. gz.setFondcode(archivesEntity.getFondsNum());
  385. gz.setBoxnumber(archivesEntity.getBoxNum());
  386. }
  387. }