package me.zhengjie.archives.service.impl;
import java.io.File;
import java.time.LocalDate;
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.GzDetailDto;
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.exception.BadRequestException;
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 : imageIds) {
if (imageId.length() == 32) {
gzFileImages.add(imageId);
} else {
fileInfoImages.add(imageId);
}
}
// 这个是新的图片地址
Map fileInfoMap = new HashMap<>();
if(CollectionUtils.isNotEmpty(fileInfoImages)) {
List fileInfo = fileInfoService.listByIds(fileInfoImages);
for (FileInfoEntity file : fileInfo) {
fileInfoMap.put(file.getId(), file);
}
}
// ------------------------这个是老的地址
Map tempFilesMap = new HashMap<>();
if(CollectionUtils.isNotEmpty(gzFileImages)) {
List tempFiles = gzFileTempService.listByIds(gzFileImages);
for (GzFileTempEntity file : tempFiles) {
tempFilesMap.put(file.getId(), file);
}
}
for (String imageId : imageIds) {
if (imageId.length() == 32) {
if(tempFilesMap.get(imageId)!=null) {
GzFileTempEntity gzFileEntity = tempFilesMap.get(imageId);
files.add(new File(gzFileEntity.getFilePath() + gzFileEntity.getFileName()));
}
} else {
if(fileInfoMap.get(imageId)!=null) {
files.add(new File(path + fileInfoMap.get(imageId).getPath()));
}
}
}
// --------------------------------------------------
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.setPdftime(LocalDate.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.setGzArchivesId(archives.getId());
gzEx.setImageJson(JSON.toJSONString(archives));
gzEx.setCreateTime(LocalDateTime.now());
gzEx.setUpdateTime(LocalDateTime.now());
gzEx.setPdfPath(fileRelatePath+fileName);
gzArchivesExService.save(gzEx);
} else {
gzEx.setGzArchivesId(archives.getId());
gzEx.setPdfPath(fileRelatePath + fileName);
gzEx.setImageJson(JSON.toJSONString(archives));
gzEx.setPdfPath(fileRelatePath+fileName);
gzEx.setUpdateTime(LocalDateTime.now());
gzArchivesExService.updateById(gzEx);
}
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);
GzDetailDto dto = new GzDetailDto();
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");
Map pdfFileName = new HashMap<>();
pdfFileName.put("pdfPath", previewPath + gzExEntity.getPdfPath());
// 卷宗号
pdfFileName.put("pdfName", entity.getArcno());
List