|
|
|
|
@@ -25,6 +25,8 @@ import org.apache.ibatis.session.SqlSession;
|
|
|
|
|
import org.json.simple.JSONArray;
|
|
|
|
|
import org.json.simple.JSONObject;
|
|
|
|
|
import org.json.simple.parser.JSONParser;
|
|
|
|
|
import org.apache.pdfbox.multipdf.PDFMergerUtility;
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
@@ -3406,6 +3408,42 @@ private String encodeImageToBase64(String imagePath) {
|
|
|
|
|
* @param estimateTemplate
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* 견적서 PDF + 추가견적(estimate02) PDF 파일들을 하나로 병합
|
|
|
|
|
*/
|
|
|
|
|
private File mergePdfWithAddEstimate(File estimatePdf, List<Map> addEstFiles) throws Exception {
|
|
|
|
|
String tempDir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
String mergedFileName = estimatePdf.getName().replace(".pdf", "_merged.pdf");
|
|
|
|
|
File mergedFile = new File(tempDir + File.separator + mergedFileName);
|
|
|
|
|
|
|
|
|
|
PDFMergerUtility merger = new PDFMergerUtility();
|
|
|
|
|
merger.setDestinationFileName(mergedFile.getAbsolutePath());
|
|
|
|
|
|
|
|
|
|
// 1. 견적서 PDF 추가
|
|
|
|
|
merger.addSource(estimatePdf);
|
|
|
|
|
|
|
|
|
|
// 2. 추가견적 PDF 파일들 추가
|
|
|
|
|
for(Map fileInfo : addEstFiles) {
|
|
|
|
|
String filePath = CommonUtils.checkNull(fileInfo.get("FILE_PATH"));
|
|
|
|
|
if("".equals(filePath)) filePath = CommonUtils.checkNull(fileInfo.get("file_path"));
|
|
|
|
|
String savedName = CommonUtils.checkNull(fileInfo.get("SAVED_FILE_NAME"));
|
|
|
|
|
if("".equals(savedName)) savedName = CommonUtils.checkNull(fileInfo.get("saved_file_name"));
|
|
|
|
|
if("".equals(savedName)) savedName = CommonUtils.checkNull(fileInfo.get("FILE_SAVED_NAME"));
|
|
|
|
|
if("".equals(filePath)) filePath = Constants.FILE_STORAGE;
|
|
|
|
|
|
|
|
|
|
File addFile = new File(filePath + File.separator + savedName);
|
|
|
|
|
if(addFile.exists() && addFile.getName().toLowerCase().endsWith(".pdf")) {
|
|
|
|
|
merger.addSource(addFile);
|
|
|
|
|
System.out.println("추가견적 PDF 병합 추가: " + addFile.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
merger.mergeDocuments(null);
|
|
|
|
|
mergedFile.deleteOnExit();
|
|
|
|
|
|
|
|
|
|
return mergedFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private File getPdfFromSession(String sessionId, Map estimateTemplate) {
|
|
|
|
|
try {
|
|
|
|
|
String tempDir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
@@ -3539,18 +3577,37 @@ private String encodeImageToBase64(String imagePath) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. PDF 파일 처리
|
|
|
|
|
// 5. PDF 파일 처리 (견적서 + 추가견적 PDF 병합)
|
|
|
|
|
ArrayList<HashMap> attachFileList = new ArrayList<HashMap>();
|
|
|
|
|
if(!"".equals(pdfSessionId)) {
|
|
|
|
|
File pdfFile = getPdfFromSession(pdfSessionId, estimateTemplate);
|
|
|
|
|
if(pdfFile != null && pdfFile.exists()) {
|
|
|
|
|
// 추가견적(estimate02) PDF 파일 조회
|
|
|
|
|
Map<String, Object> fileParam = new HashMap<String, Object>();
|
|
|
|
|
fileParam.put("targetObjId", objId);
|
|
|
|
|
fileParam.put("docType", "estimate02");
|
|
|
|
|
List<Map> addEstFiles = sqlSession.selectList("common.getFileList", fileParam);
|
|
|
|
|
|
|
|
|
|
File finalPdf = pdfFile;
|
|
|
|
|
|
|
|
|
|
// 추가견적 PDF가 있으면 병합
|
|
|
|
|
if(addEstFiles != null && !addEstFiles.isEmpty()) {
|
|
|
|
|
try {
|
|
|
|
|
finalPdf = mergePdfWithAddEstimate(pdfFile, addEstFiles);
|
|
|
|
|
System.out.println("PDF 병합 완료: 견적서 + 추가견적 " + addEstFiles.size() + "건");
|
|
|
|
|
} catch(Exception mergeEx) {
|
|
|
|
|
System.out.println("PDF 병합 실패, 견적서만 첨부: " + mergeEx.getMessage());
|
|
|
|
|
finalPdf = pdfFile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HashMap<String, String> fileMap = new HashMap<String, String>();
|
|
|
|
|
fileMap.put(Constants.Db.COL_FILE_REAL_NAME, pdfFile.getName());
|
|
|
|
|
fileMap.put(Constants.Db.COL_FILE_SAVED_NAME, pdfFile.getName());
|
|
|
|
|
fileMap.put(Constants.Db.COL_FILE_PATH, pdfFile.getParent());
|
|
|
|
|
fileMap.put(Constants.Db.COL_FILE_REAL_NAME, finalPdf.getName());
|
|
|
|
|
fileMap.put(Constants.Db.COL_FILE_SAVED_NAME, finalPdf.getName());
|
|
|
|
|
fileMap.put(Constants.Db.COL_FILE_PATH, finalPdf.getParent());
|
|
|
|
|
attachFileList.add(fileMap);
|
|
|
|
|
|
|
|
|
|
System.out.println("PDF 파일 첨부 완료: " + pdfFile.getAbsolutePath());
|
|
|
|
|
|
|
|
|
|
System.out.println("PDF 파일 첨부 완료: " + finalPdf.getAbsolutePath());
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("PDF 파일을 찾을 수 없습니다: " + pdfSessionId);
|
|
|
|
|
}
|
|
|
|
|
|