package me.zhengjie.archives.service.impl;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.archives.dao.IGzArchivesDao;
import me.zhengjie.archives.dao.IModelDao;
import me.zhengjie.archives.dto.ArchivesDto;
import me.zhengjie.archives.entity.ArchivesEntity;
import me.zhengjie.archives.entity.FileInfoEntity;
import me.zhengjie.archives.entity.GzArchivesEntity;
import me.zhengjie.archives.entity.GzArchivesExEntity;
import me.zhengjie.archives.entity.GzDetailEntity;
import me.zhengjie.archives.entity.GzFileTempEntity;
import me.zhengjie.archives.entity.GzFilepathEntity;
import me.zhengjie.archives.entity.ModelEntity;
import me.zhengjie.archives.plus.AbstractServiceImpl;
import me.zhengjie.archives.plus.QueryMap;
import me.zhengjie.archives.service.FileInfoService;
import me.zhengjie.archives.service.GzArchivesExService;
import me.zhengjie.archives.service.GzArchivesService;
import me.zhengjie.archives.service.GzDetailService;
import me.zhengjie.archives.service.GzFileTempService;
import me.zhengjie.archives.service.GzFilepathService;
import me.zhengjie.archives.util.HtmlToImage;
import me.zhengjie.archives.util.PdfUtils;
import me.zhengjie.archives.vo.GzArchivesVo;
import me.zhengjie.archives.vo.GzPortraitVo;
import me.zhengjie.base.ResponseDTO;
import me.zhengjie.config.FileProperties;
import me.zhengjie.modules.system.service.DictDetailService;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.UUIDGenerator;
/**
*
* 服务实现类
*
*
* @author Erwin Feng
* @since 2023-06-25
*/
@Service
public class GzArchivesServiceImpl extends AbstractServiceImpl
implements GzArchivesService {
@Autowired
DictDetailService dictDetailService;
@Autowired
IModelDao modelDao;
@Autowired
private FileProperties properties;
@Autowired
FileInfoService fileInfoService;
@Autowired
private GzFileTempService gzFileTempService;
@Autowired
GzArchivesExService gzArchivesExService;
@Autowired
private GzDetailService gzDetailService;
@Autowired
private GzFilepathService gzFilepathService;
public void mergeSubmit(GzPortraitVo archives) throws Exception {
image(archives);
}
public void image(GzPortraitVo archives) throws Exception {
String path = properties.getPath().getPath();
// 查询对应的值
GzArchivesEntity entity = this.getById(archives.getId());
// 保管期限
String saveTerm = dictDetailService.getLabelByValue("saveTerm_type", entity.getKeepingterm());
// 卷宗号
String dossierNum = entity.getYear() + "-" + entity.getNotarizationtypefull() + "-" + saveTerm + "-"
+ entity.getFileno();
// TODO 当事人很多,需要修改字体-------------------页号开始------------------------
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("code", 1);
ModelEntity model = modelDao.selectOne(queryWrapper);
String fileRelatePath = FileUtil.getUploadPath(path);
String filePath = path + fileRelatePath;
String headPageHtml = model.getContent();
// 替换内容
headPageHtml = headPageHtml.replace("${year}", entity.getYear().toString())
.replace("${notarialNum}", entity.getNo()).replace("${notarizedItem}", entity.getTitle())
.replace("${partyName}", entity.getConcernedname()).replace("${undertaker}", entity.getUndertaker())
.replace("${notary}", entity.getNotary())
.replace("${acceptDate}", DateUtil.localDate(entity.getAcceptdate()))
.replace("${completeDate}", DateUtil.localDate(entity.getFinishdate()))
.replace("${filingDate}", DateUtil.localDate(entity.getFiledate())).replace("${saveTerm}", saveTerm)
.replace("${dossierNum}", dossierNum).replace("${acceptNum}", entity.getAcceptno())
.replace("${fondsNum}", entity.getFondcode()).replace("${catalogNum}", entity.getMenuno())
.replace("${caseNum}", entity.getFileno());
String headPagePath = filePath + UUIDGenerator.uuid() + ".html";
String headPageToImage = filePath + UUIDGenerator.uuid() + ".jpg";
// 先写入文本
FileUtil.writeUtf8String(headPageHtml, headPagePath);
// 然后生成jpg
String exePath = dictDetailService.getValueByName("file_path").get("exe_path");
HtmlToImage.htmlToImg(exePath, headPagePath, headPageToImage);
FileUtil.deleteFile(new File(headPagePath));
// -------------------页号结束------------------------
// --------------------目录号开始---------
queryWrapper = new QueryWrapper();
queryWrapper.eq("code", 2);
model = modelDao.selectOne(queryWrapper);
String catalogHtml = model.getContent();
int j = 0;
List pages = archives.getPages();
for (int i = 0; i < pages.size(); i++) {
j = i + 1;
catalogHtml = catalogHtml.replace("${num" + j + "}", pages.get(i));
}
String catalogPath = filePath + UUIDGenerator.uuid() + ".html";
String catalogToImage = filePath + UUIDGenerator.uuid() + ".jpg";
// 先写入文本
FileUtil.writeUtf8String(catalogHtml, catalogPath);
// 然后生成jpg
HtmlToImage.htmlToImg(exePath, catalogPath, catalogToImage);
FileUtil.deleteFile(new File(catalogPath));
List files = new ArrayList();
// 页头第一个
files.add(new File(headPageToImage));
// 目录放在第二个
files.add(new File(catalogToImage));
List imageIds = archives.getImageIds();
// 区分是新增加的,还是老的数据,历史数据是32位,新的是22位
List fileInfoImages = new ArrayList<>();
List gzFileImages = new ArrayList<>();
// 判断
for (String imageId : fileInfoImages) {
if (imageId.length() == 32) {
gzFileImages.add(imageId);
} else {
fileInfoImages.add(imageId);
}
}
// 这个是新的图片地址
List fileInfo = fileInfoService.listByIds(fileInfoImages);
Map fileInfoMap = new HashMap<>();
for (FileInfoEntity file : fileInfo) {
fileInfoMap.put(file.getId(), file);
}
// ------------------------这个是老的地址
QueryWrapper tempFile = new QueryWrapper<>();
tempFile.eq("pid", archives.getId());
List tempFiles = gzFileTempService.list(tempFile);
Map tempFilesMap = new HashMap<>();
for (GzFileTempEntity file : tempFiles) {
tempFilesMap.put(file.getId(), file);
}
for (String imageId : imageIds) {
if (imageId.length() == 32) {
GzFileTempEntity gzFileEntity = tempFilesMap.get(imageId);
files.add(new File(gzFileEntity.getFilePath() + gzFileEntity.getFileName()));
} else {
files.add(new File(path + fileInfoMap.get(imageId)));
}
}
// --------------------------------------------------
String fileName = "merge_pdf" + UUIDGenerator.uuid() + ".pdf";
String toPdfTarget = filePath + fileName;
// pdf进行合并
PdfUtils.manyImageToOnePdf(files, toPdfTarget);
// 先更新主表
entity = new GzArchivesEntity();
entity.setId(archives.getId());
entity.setRecflag("1");
entity.setUpdatetime(LocalDateTime.now());
this.updateById(entity);
// -------------------
GzArchivesExEntity archiveEntity = new GzArchivesExEntity();
archiveEntity.setGzArchivesId(archives.getId());
GzArchivesExEntity gzEx = gzArchivesExService.getOne(archiveEntity);
if (gzEx == null) {
gzEx = new GzArchivesExEntity();
gzEx.setPdfPath(fileRelatePath + fileName);
gzEx.setImageJson(JSON.toJSONString(archives));
gzEx.setCreateTime(LocalDateTime.now());
gzEx.setUpdateTime(LocalDateTime.now());
gzArchivesExService.save(gzEx);
} else {
gzEx.setPdfPath(fileRelatePath + fileName);
gzEx.setImageJson(JSON.toJSONString(archives));
gzEx.setUpdateTime(LocalDateTime.now());
gzArchivesExService.updateById(archiveEntity);
}
UpdateWrapper updateWrapper = new UpdateWrapper<>();
updateWrapper.in("id", imageIds);
updateWrapper.set("loan_no", archives.getId());
this.fileInfoService.update(updateWrapper);
}
@AnonymousPostMapping("/detail")
public ResponseDTO detail(GzArchivesVo req) {
GzArchivesEntity entity = this.getById(req.getId());
// GzArchivesDto dto = BeanCopyUtils.convertObj(entity, GzArchivesDto.class);
ArchivesDto dto = new ArchivesDto();
convertArchives(entity, dto);
// 先查询有没有处理过,然后再处理老的数据
GzArchivesExEntity gzExEntity = new GzArchivesExEntity();
gzExEntity.setGzArchivesId(req.getId());
gzExEntity = gzArchivesExService.getOne(gzExEntity);
if (gzExEntity != null) {
String previewPath = dictDetailService.getValueByName("file_path").get("preview_path");
List pdfs = new ArrayList<>();
pdfs.add(previewPath + gzExEntity.getPdfPath());
dto.setPdfPaths(pdfs);
return ResponseDTO.success(dto);
}
QueryWrapper qw = new QueryWrapper<>();
qw.eq("puuid", req.getId());
List gzIds = gzDetailService.list(qw).stream().map(e -> e.getId()).collect(Collectors.toList());
QueryWrapper gzFilepathQw = new QueryWrapper<>();
gzFilepathQw.in("puuid", gzIds);
gzFilepathQw.orderByAsc(" arccode+0 ");
String gzPath = dictDetailService.getValueByName("file_path").get("gz_path");
List gzFilePaths = gzFilepathService.list(gzFilepathQw);
if (CollectionUtils.isNotEmpty(gzFilePaths)) {
List pdfs = new ArrayList<>();
for (GzFilepathEntity gz : gzFilePaths) {
pdfs.add(gzPath + gz.getSubpath() + gz.getFilename() + gz.getExt());
}
dto.setPdfPaths(pdfs);
return ResponseDTO.success(dto);
}
return ResponseDTO.success(dto);
}
public Map page(GzArchivesVo req) {
IPage page = new Page<>(req.getIndex() + 1, req.getSize());
GzArchivesEntity gzArchives = new GzArchivesEntity();
convertGz(gzArchives, req);
QueryMap queryMap = new QueryMap();
Map map = new HashMap<>();
List accept = req.getAcceptDates();
if (CollectionUtils.isNotEmpty(accept)) {
String stringDate = accept.get(0);
map.put("startTime", stringDate);
String endDate = accept.get(1);
map.put("endTime", endDate);
queryMap.setMap(map);
}
List submitTime = req.getSubmitTime();
if (CollectionUtils.isNotEmpty(submitTime)) {
map.put("submitStartTime", submitTime.get(0));
map.put("submitEndTime", submitTime.get(1));
queryMap.setMap(map);
}
page = page(page, gzArchives, queryMap);
List list = new ArrayList<>();
if (page.getRecords() != null) {
for (GzArchivesEntity gz : page.getRecords()) {
ArchivesDto archivesEntity = new ArchivesDto();
convertArchives(gz, archivesEntity);
list.add(archivesEntity);
}
}
return PageUtil.toPage(list, page.getTotal());
}
private void convertArchives(GzArchivesEntity gz, ArchivesDto archivesEntity) {
StringBuffer sb = new StringBuffer();
sb.append(gz.getYear() + "-");
sb.append(gz.getNotarizationtypefull() + "-");
sb.append(gz.getKeepingterm() + "-");
sb.append(gz.getFileno());
archivesEntity.setId(gz.getId());
archivesEntity.setArchivesNum(gz.getArcno());
archivesEntity.setDossierNum(sb.toString());
archivesEntity.setNotarizationType(gz.getNotarialOfficeFull());
archivesEntity.setNotarizedItem(gz.getTitle());
archivesEntity.setNotarialNum(gz.getNotarizationnumber());
archivesEntity.setNotary(gz.getNotary());
archivesEntity.setUndertaker(gz.getUndertaker());
archivesEntity.setPartyName(gz.getConcernedname());
archivesEntity.setAcceptNum(gz.getAcceptno());
archivesEntity.setAnnual(gz.getYear().toString());
archivesEntity.setAcceptDate(DateUtil.formatYmd(gz.getAcceptdate()));
archivesEntity.setCompleteDate(DateUtil.formatYmd(gz.getFinishdate()));
archivesEntity.setFilingDate(DateUtil.formatYmd(gz.getFiledate()));
archivesEntity.setSaveTerm(gz.getKeepingterm());
archivesEntity.setSecrecyType(gz.getSecretclass());
archivesEntity.setNotaryFullName(gz.getNotarialOfficeFull());
archivesEntity.setCaseNum(gz.getFileno());
archivesEntity.setFondsNum(gz.getFondcode());
archivesEntity.setCatalogNum(gz.getMenuno());
archivesEntity.setBoxNum(gz.getBoxnumber());
archivesEntity.setCreateTime(gz.getCreatetime());
archivesEntity.setDossierType(gz.getMenuno().split("\\.")[1]);
}
public static void main(String[] args) {
System.out.println("1.2".split("\\.")[1]);
}
public ResponseDTO edit(@RequestBody GzArchivesVo req) {
GzArchivesEntity gz = new GzArchivesEntity();
convertGz(gz, req);
return null;
}
private void convertGz(GzArchivesEntity gz, GzArchivesVo archivesEntity) {
// 目录号
gz.setMenuno(archivesEntity.getCatalogNum());
// caseNum 案卷号
gz.setFileno(archivesEntity.getCaseNum());
// 公证书编号
gz.setNotarizationnumber(archivesEntity.getNotarialNum());
// 当事人姓名
gz.setConcernedname(archivesEntity.getPartyName());
// 公证员
gz.setNotary(archivesEntity.getNotary());
// 承办人
gz.setUndertaker(archivesEntity.getUndertaker());
// 公证事项
gz.setTitle(archivesEntity.getNotarizedItem());
// 受理号
gz.setAcceptno(archivesEntity.getAcceptNum());
if (StringUtils.isNotBlank(archivesEntity.getAnnual())) {
gz.setYear(Integer.parseInt(archivesEntity.getAnnual()));
}
// 年度
gz.setArcno(archivesEntity.getArchivesNum());
// 三个时间
if (StringUtils.isNotBlank(archivesEntity.getAcceptDate())) {
gz.setAcceptdate(DateUtil.parseYmd(archivesEntity.getAcceptDate()));
}
if (StringUtils.isNotBlank(archivesEntity.getCompleteDate())) {
gz.setFinishdate(DateUtil.parseYmd(archivesEntity.getCompleteDate()));
}
if (StringUtils.isNotBlank(archivesEntity.getFilingDate())) {
gz.setFiledate(DateUtil.parseYmd(archivesEntity.getFilingDate()));
}
// 保管类型
gz.setSecretclass(archivesEntity.getSecrecyType());
// 保管期限
gz.setKeepingterm(archivesEntity.getSaveTerm());
// 将类型转成汉字
String dossierType = dictDetailService.getLabelByValue("saveTerm_type", archivesEntity.getDossierType());
gz.setNotarizationtypefull(dossierType);
gz.setFondcode(archivesEntity.getFondsNum());
gz.setBoxnumber(archivesEntity.getBoxNum());
}
}