260115_01_ENH : Front 연동 테스트 완료 및 대량발송 결과 다운로드 구현 완료
This commit is contained in:
@@ -68,13 +68,11 @@ public class FileService {
|
||||
* 2) Asset 디렉토리에서 더 이상 사용되지 않는 파일 삭제
|
||||
* 3) 정리된 HTML 반환
|
||||
*/
|
||||
public String syncOnSave(String prjctCd, String rawHtml) {
|
||||
public String syncOnSave(String prjctCd, Long tmplatIdx, String rawHtml) {
|
||||
if (rawHtml == null) rawHtml = "";
|
||||
|
||||
String returnHtml = rawHtml;
|
||||
try{
|
||||
|
||||
|
||||
// body fragment로 파싱 (KriEditor가 body 없이 <p>...</p> 형태 저장하므로)
|
||||
Document doc = Jsoup.parseBodyFragment(rawHtml);
|
||||
Elements imgs = doc.select("img[src]");
|
||||
@@ -86,23 +84,33 @@ public class FileService {
|
||||
if (src == null || src.isBlank()) continue;
|
||||
if (!src.contains("/" + prjctCd + "/")) continue;
|
||||
|
||||
String fileName = FileUtil.extractFileNameFromPath(src);
|
||||
|
||||
if (src.contains("/" + prjctCd + "/Temp/")) {
|
||||
// Temp → Asset 이동
|
||||
String fileName = FileUtil.extractFileNameFromPath(src);
|
||||
moveTempToAsset(prjctCd, fileName);
|
||||
moveTempToAsset(prjctCd, tmplatIdx, fileName);
|
||||
|
||||
String assetSrc = src.replace("/Temp/", "/Asset/");
|
||||
String assetSrc = src.replace("/Temp/", "/Asset/" + tmplatIdx + "/");
|
||||
img.attr("src", assetSrc);
|
||||
|
||||
assetFileNamesToKeep.add(fileName);
|
||||
} else if (src.contains("/" + prjctCd + "/Asset/")) {
|
||||
String fileName = FileUtil.extractFileNameFromPath(src);
|
||||
if (!src.contains("/" + prjctCd + "/Asset/" + tmplatIdx + "/")){
|
||||
String[] srcArr = src.split("/" + prjctCd + "/Asset/");
|
||||
String fromTmplatIdx = srcArr[1].split("/")[0].replace(fileName, "");
|
||||
|
||||
copyAssetToAsset(prjctCd, fromTmplatIdx, tmplatIdx, fileName);
|
||||
|
||||
String assetSrc = src.replace("/Asset/" + fromTmplatIdx + "/", "/Asset/" + tmplatIdx + "/");
|
||||
img.attr("src", assetSrc);
|
||||
}
|
||||
|
||||
assetFileNamesToKeep.add(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
// Asset 디렉토리 정리 (본문에 없는 파일 삭제)
|
||||
cleanupAssetDir(prjctCd, assetFileNamesToKeep);
|
||||
cleanupAssetDir(prjctCd, tmplatIdx, assetFileNamesToKeep);
|
||||
|
||||
// body 안의 HTML만 반환 (불필요한 <html><body> wrapper 제거)
|
||||
returnHtml = doc.body().html();
|
||||
@@ -126,15 +134,36 @@ public class FileService {
|
||||
return uploadBaseDir.resolve(prjctCd).resolve("Temp");
|
||||
}
|
||||
|
||||
private Path getAssetDir(String prjctCd) {
|
||||
return uploadBaseDir.resolve(prjctCd).resolve("Asset");
|
||||
private Path getAssetDir(String prjctCd, String tmplatIdx) {
|
||||
return uploadBaseDir.resolve(prjctCd).resolve("Asset").resolve(tmplatIdx);
|
||||
}
|
||||
|
||||
private void moveTempToAsset(String prjctCd, String fileName) throws IOException {
|
||||
private Path getAssetDir(String prjctCd, Long tmplatIdx) {
|
||||
return uploadBaseDir.resolve(prjctCd).resolve("Asset").resolve(tmplatIdx.toString());
|
||||
}
|
||||
|
||||
private void copyAssetToAsset(String prjctCd, String fromTmplatIdx, Long toTmplatIdx, String fileName) throws IOException {
|
||||
if (fileName == null || fileName.isBlank()) return;
|
||||
Path fromDir = getAssetDir(prjctCd, fromTmplatIdx);
|
||||
Path toDir = getAssetDir(prjctCd, toTmplatIdx);
|
||||
|
||||
Path src = fromDir.resolve(fileName).normalize();
|
||||
Path dst = toDir.resolve(fileName).normalize();
|
||||
|
||||
Files.createDirectories(toDir);
|
||||
|
||||
if (!Files.exists(src)) {
|
||||
throw new NoSuchFileException("Source asset not found: " + src);
|
||||
}
|
||||
|
||||
Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
private void moveTempToAsset(String prjctCd, Long tmplatIdx, String fileName) throws IOException {
|
||||
if (fileName == null || fileName.isBlank()) return;
|
||||
|
||||
Path tempDir = getTempDir(prjctCd);
|
||||
Path assetDir = getAssetDir(prjctCd);
|
||||
Path assetDir = getAssetDir(prjctCd, tmplatIdx);
|
||||
Files.createDirectories(assetDir);
|
||||
|
||||
Path source = tempDir.resolve(fileName).normalize();
|
||||
@@ -151,8 +180,8 @@ public class FileService {
|
||||
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
private void cleanupAssetDir(String prjctCd, Set<String> keepFileNames) throws IOException {
|
||||
Path assetDir = getAssetDir(prjctCd);
|
||||
private void cleanupAssetDir(String prjctCd, Long tmplatIdx, Set<String> keepFileNames) throws IOException {
|
||||
Path assetDir = getAssetDir(prjctCd, tmplatIdx);
|
||||
if (!Files.exists(assetDir)) return;
|
||||
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(assetDir)) {
|
||||
|
||||
@@ -1,25 +1,41 @@
|
||||
package com.kri.notihub.server.service;
|
||||
|
||||
import com.kri.common.excel.ExcelMapper;
|
||||
import com.kri.common.web.BusinessException;
|
||||
import com.kri.common.webmvc.page.PageRequestDto;
|
||||
import com.kri.common.webmvc.page.PageResponseDto;
|
||||
import com.kri.common.webmvc.page.Pageables;
|
||||
import com.kri.notihub.server.common.consts.NotihubConst;
|
||||
import com.kri.notihub.server.common.enums.SendType;
|
||||
import com.kri.notihub.server.dto.report.BulkSendInfoDto;
|
||||
import com.kri.notihub.server.dto.report.ReportExcelResponseDto;
|
||||
import com.kri.notihub.server.dto.report.SendInfoDto;
|
||||
import com.kri.notihub.server.persistence.entity.TBulkSendEntity;
|
||||
import com.kri.notihub.server.persistence.entity.TSendEntity;
|
||||
import com.kri.notihub.server.persistence.entity.TSendResultCodeEntity;
|
||||
import com.kri.notihub.server.persistence.repository.BulkSendRepository;
|
||||
import com.kri.notihub.server.persistence.repository.SendRepository;
|
||||
import com.kri.notihub.server.persistence.repository.SendResultCodeRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ReportService {
|
||||
|
||||
private final SendResultCodeRepository sendResultCodeRepository;
|
||||
private final BulkSendRepository bulkSendRepository;
|
||||
private final SendRepository sendRepository;
|
||||
|
||||
@@ -37,10 +53,67 @@ public class ReportService {
|
||||
}
|
||||
|
||||
public PageResponseDto<SendInfoDto> getBulkSendDetail(String bulkSendKey, PageRequestDto pageReq){
|
||||
List<TSendResultCodeEntity> resultCodeList = sendResultCodeRepository.findAll();
|
||||
|
||||
Pageable pageable = Pageables.of(pageReq, TSendEntity.class);
|
||||
Page<SendInfoDto> page = sendRepository.findByBulkSendKey(bulkSendKey, pageable)
|
||||
.map(SendInfoDto::from);
|
||||
.map(entity -> SendInfoDto.from(entity, resultCodeList));
|
||||
|
||||
return PageResponseDto.from(page);
|
||||
}
|
||||
|
||||
public ReportExcelResponseDto getBulkSendDetailExcel(String bulkSendKey){
|
||||
List<TSendResultCodeEntity> resultCodeList = sendResultCodeRepository.findAll();
|
||||
|
||||
Sort sort = Sort.by(Sort.Direction.ASC, "sendIdx");
|
||||
|
||||
TBulkSendEntity bulkSend = bulkSendRepository.findByBulkSendKey(bulkSendKey).orElse(null);
|
||||
if(bulkSend == null){
|
||||
throw BusinessException.badRequest("NOT FOUND BULK SEND","해당 대량발송은 없습니다.")
|
||||
.with("bulkSendKey", bulkSendKey);
|
||||
}
|
||||
|
||||
String ts = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
|
||||
String fileName = "대량 발송 내역_%s_%s_%s.xlsx".formatted(bulkSend.getReqstSvc(), bulkSend.getPrjctCd(), ts);
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(64 * 1024);
|
||||
|
||||
// Base64 스트림으로 바로 쓰기 (엑셀 bytes -> base64 bytes)
|
||||
try (OutputStream b64Out = Base64.getEncoder().wrap(bos);
|
||||
ExcelMapper.StreamingWriter<SendInfoDto> writer =
|
||||
ExcelMapper.openStreamingWriter(SendInfoDto.class, NotihubConst.ROW_WINDOW)) {
|
||||
|
||||
int page = 0;
|
||||
while (true) {
|
||||
Page<TSendEntity> chunkPage = sendRepository.findByBulkSendKey(
|
||||
bulkSendKey,
|
||||
PageRequest.of(page++, NotihubConst.CHUNK_SIZE, sort)
|
||||
);
|
||||
|
||||
if (chunkPage.isEmpty()) break;
|
||||
|
||||
List<SendInfoDto> chunk = chunkPage.getContent()
|
||||
.stream()
|
||||
.map(entity -> SendInfoDto.from(entity, resultCodeList))
|
||||
.toList();
|
||||
|
||||
writer.appendAll(chunk);
|
||||
|
||||
if (!chunkPage.hasNext()) break;
|
||||
}
|
||||
|
||||
//최종 1회 write (XLSX 마무리)
|
||||
writer.writeTo(b64Out);
|
||||
} catch (Exception e) {
|
||||
throw BusinessException.internalError("FAIL MAKE EXCEL","엑셀(Base64) 생성 실패. " + e.getMessage())
|
||||
.with("bulkSendKey", bulkSendKey);
|
||||
}
|
||||
|
||||
String base64 = new String(bos.toByteArray(), StandardCharsets.UTF_8);
|
||||
|
||||
return ReportExcelResponseDto.builder()
|
||||
.errorFileName(fileName)
|
||||
.errorFileBase64(base64)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.kri.notihub.server.service;
|
||||
import com.kri.common.datetime.DateTimeUtil;
|
||||
import com.kri.common.web.BusinessException;
|
||||
import com.kri.notihub.server.common.consts.NotihubConst;
|
||||
import com.kri.notihub.server.common.enums.ChannelType;
|
||||
import com.kri.notihub.server.common.enums.SendType;
|
||||
import com.kri.notihub.server.common.enums.TemplateEventType;
|
||||
import com.kri.notihub.server.common.util.TemplateContentSanitizer;
|
||||
@@ -28,7 +29,7 @@ public class TemplateService {
|
||||
private final TemplateEventLogRepository templateEventLogRepository;
|
||||
|
||||
@Transactional
|
||||
public long createTemplate(String itrntPrjctCd, TemplateBasicDto template, String regUser) {
|
||||
public TemplateDto createTemplate(String itrntPrjctCd, TemplateBasicDto template, String regUser) {
|
||||
final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
|
||||
|
||||
validateBySendType(template);
|
||||
@@ -42,11 +43,15 @@ public class TemplateService {
|
||||
|
||||
saveLog(saved.getTmplatIdx(), TemplateEventType.CREATE_TEMPLATE, null, template, regUser, now);
|
||||
|
||||
return saved.getTmplatIdx();
|
||||
return TemplateDto.from(saved);
|
||||
}
|
||||
|
||||
public List<TemplateSummaryDto> getTemplateList(String itrntPrjctCd) {
|
||||
return templateRepository.findAllByItrntPrjctCdAndDelFalse(itrntPrjctCd).stream()
|
||||
public List<TemplateSummaryDto> getTemplateList(ChannelType chnnlTy, String itrntPrjctCd) {
|
||||
List<Short> tmplatTyList = chnnlTy == ChannelType.SMS
|
||||
? NotihubConst.MOBILE_TYPE_CODE_LIST
|
||||
: NotihubConst.EMAIL_TYPE_CODE_LIST;
|
||||
|
||||
return templateRepository.findAllByTmplatTyInAndItrntPrjctCdAndDelFalse(tmplatTyList, itrntPrjctCd).stream()
|
||||
.map(TemplateSummaryDto::from)
|
||||
.toList();
|
||||
}
|
||||
@@ -60,6 +65,17 @@ public class TemplateService {
|
||||
);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateTemplateBodyWithCreate(TemplateDto template) {
|
||||
TTmplatEntity entity = templateRepository.findByTmplatIdxAndDelFalse(template.getTmplatIdx()).orElseThrow(() ->
|
||||
BusinessException.notFound("TEMPLATE_NOT_FOUND", "템플릿을 찾을 수 없습니다.")
|
||||
.with("tmplatIdx", template.getTmplatIdx())
|
||||
);
|
||||
|
||||
entity.setTmplatBody(template.getTmplatBody());
|
||||
templateRepository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TemplateDto updateTemplate(TemplateDto template, String modUser) {
|
||||
long tmplatIdx = template.getTmplatIdx();
|
||||
|
||||
Reference in New Issue
Block a user