GzArchivesServiceImpl.java 15 KB

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