diff --git a/src/main/java/com/kri/notihub/server/common/enums/SendCorpType.java b/src/main/java/com/kri/notihub/server/common/enums/SendCorpType.java index 4a00827..f9d13f3 100644 --- a/src/main/java/com/kri/notihub/server/common/enums/SendCorpType.java +++ b/src/main/java/com/kri/notihub/server/common/enums/SendCorpType.java @@ -20,7 +20,7 @@ public enum SendCorpType implements CodeEnum { return null; } for (SendCorpType s : values()) { - if (s.code == code) { + if (s.code.equals(code)) { return s; } } diff --git a/src/main/java/com/kri/notihub/server/common/external/intranet/IntranetApiClient.java b/src/main/java/com/kri/notihub/server/common/external/intranet/IntranetApiClient.java new file mode 100644 index 0000000..afeced4 --- /dev/null +++ b/src/main/java/com/kri/notihub/server/common/external/intranet/IntranetApiClient.java @@ -0,0 +1,73 @@ +package com.kri.notihub.server.common.external.intranet; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.kri.common.feature.http.ExternalApiClient; +import com.kri.common.feature.http.ExternalApiResponse; +import com.kri.notihub.server.common.external.intranet.dto.IntranetProjectBaseInfoDto; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class IntranetApiClient { + + private static final String PROJECT_WITH_P_CHILD_SN_PATH = + "/project/search/all/with-p-child-sn"; + + // ExternalApiClient is constructed directly because the starter may not provide it as a Bean. + private final ExternalApiClient externalApiClient = new ExternalApiClient(); + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Value("${kri.api-url.intranet-extension}") + private String intranetExtensionBaseUrl; + + @Value("${kri.auth.apikey.header-name:X-API-Key}") + private String apiKeyHeaderName; + + @Value("${kri.auth.apikey.key}") + private String apiKey; + + /** + * POST {intranetExtensionBaseUrl}/project/search/all/with-p-child-sn + */ + public List getProjectWithPChildSn(List childSnList) { + if (childSnList == null || childSnList.isEmpty()) { + return Collections.emptyList(); + } + + String url = String.format("%s%s", normalizeBaseUrl(intranetExtensionBaseUrl), + PROJECT_WITH_P_CHILD_SN_PATH); + + Map headers = new HashMap<>(); + headers.put(apiKeyHeaderName, apiKey); + + try { + String body = objectMapper.writeValueAsString(childSnList); + ExternalApiResponse response = externalApiClient.post(url, headers, body); + + return objectMapper.readValue(response.getBody(), + new TypeReference>() { + }); + } catch (JsonProcessingException e) { + return Collections.emptyList(); + } + } + + public List getProjectWithPChildSn(String... childSnArray) { + if (childSnArray == null || childSnArray.length == 0) { + return Collections.emptyList(); + } + return getProjectWithPChildSn(List.of(childSnArray)); + } + + private String normalizeBaseUrl(String baseUrl) { + return baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; + } +} diff --git a/src/main/java/com/kri/notihub/server/common/external/intranet/dto/IntranetProjectBaseInfoDto.java b/src/main/java/com/kri/notihub/server/common/external/intranet/dto/IntranetProjectBaseInfoDto.java new file mode 100644 index 0000000..f272199 --- /dev/null +++ b/src/main/java/com/kri/notihub/server/common/external/intranet/dto/IntranetProjectBaseInfoDto.java @@ -0,0 +1,17 @@ +package com.kri.notihub.server.common.external.intranet.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class IntranetProjectBaseInfoDto { + + private String childSn; + + private String title; + + private String researchTeam; + + private String researchTeamName; +} diff --git a/src/main/java/com/kri/notihub/server/controller/ReportController.java b/src/main/java/com/kri/notihub/server/controller/ReportController.java index c33e646..41f5ec5 100644 --- a/src/main/java/com/kri/notihub/server/controller/ReportController.java +++ b/src/main/java/com/kri/notihub/server/controller/ReportController.java @@ -8,6 +8,8 @@ import com.kri.notihub.server.common.enums.ChannelType; 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.SendCostSummaryRequestDto; +import com.kri.notihub.server.dto.report.SendCostSummaryWithItrntPrjctDto; import com.kri.notihub.server.dto.report.SendResultLastWithSidDto; import com.kri.notihub.server.dto.report.SendResultCodeDto; import com.kri.notihub.server.dto.report.SendResultInfoDto; @@ -17,8 +19,14 @@ import com.kri.notihub.server.service.ReportService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Base64; import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -88,6 +96,40 @@ public class ReportController { return reportService.getSendResultSummary(req); } + @Operation(summary = "인트라넷 프로젝트별 발송 비용 요약 조회") + @PostMapping("/cost/summary-itrnt-prjct") + public List getSendCostSummaryWithItrntPrjct( + @Valid @RequestBody SendCostSummaryRequestDto req + ) { + return reportService.getSendCostSummaryWithItrntPrjct(req); + } + + @Operation(summary = "인트라넷 프로젝트별 발송 비용 요약 다운로드") + @PostMapping("/cost/summary-itrnt-prjct/excel") + public ReportExcelResponseDto getSendCostSummaryWithItrntPrjctExcel( + @Valid @RequestBody SendCostSummaryRequestDto req + ) { + return reportService.getSendCostSummaryWithItrntPrjctExcel(req); + } + + @Operation(summary = "인트라넷 프로젝트별 발송 비용 요약 다운로드 For Swagger") + @PostMapping("/cost/summary-itrnt-prjct/excel/swagger") + public ResponseEntity getSendCostSummaryWithItrntPrjctExcelForSwagger( + @Valid @RequestBody SendCostSummaryRequestDto req + ) { + ReportExcelResponseDto result = reportService.getSendCostSummaryWithItrntPrjctExcel(req); + byte[] bytes = Base64.getDecoder().decode(result.getFileBase64()); + + String contentDisposition = "attachment; filename*=UTF-8''" + + URLEncoder.encode(result.getFileName(), StandardCharsets.UTF_8).replace("+", "%20"); + + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition) + .contentType(MediaType.parseMediaType( + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) + .body(bytes); + } + @Operation(summary = "리스트 별 최종 발송 결과 조회") @PostMapping("/last/send/{channelTy}/{reqstSvc}/{prjctCd}") public List getSendResultLastWithSid( diff --git a/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryRequestDto.java b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryRequestDto.java new file mode 100644 index 0000000..dc82cbb --- /dev/null +++ b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryRequestDto.java @@ -0,0 +1,27 @@ +package com.kri.notihub.server.dto.report; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import java.time.LocalDate; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(description = "발송 비용 프로젝트별 요약 요청") +public class SendCostSummaryRequestDto { + + @Schema(description = "조회 시작 일시") + @NotNull + private LocalDate startDt; + + @Schema(description = "조회 종료 일시") + @NotNull + private LocalDate endDt; +} diff --git a/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctDto.java b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctDto.java new file mode 100644 index 0000000..2bb8060 --- /dev/null +++ b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctDto.java @@ -0,0 +1,60 @@ +package com.kri.notihub.server.dto.report; + +import com.kri.notihub.server.common.external.intranet.dto.IntranetProjectBaseInfoDto; +import io.swagger.v3.oas.annotations.media.Schema; +import java.math.BigDecimal; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(description = "인트라넷 프로젝트 정보 포함 발송 비용 요약") +public class SendCostSummaryWithItrntPrjctDto { + + @Schema(description = "요청 서비스") + private String reqstSvc; + + @Schema(description = "인트라넷 프로젝트 코드") + private String itrntPrjctCd; + + @Schema(description = "발송 타입") + private Short sendTy; + + @Schema(description = "발송 업체") + private Short sendCorp; + + @Schema(description = "발송 건수") + private Long sendCnt; + + @Schema(description = "단가") + private BigDecimal unitPrice; + + @Schema(description = "총 비용") + private BigDecimal totalPrice; + + @Schema(description = "인트라넷 프로젝트 정보") + private IntranetProjectBaseInfoDto itrntPrjctInfo; + + public SendCostSummaryWithItrntPrjctDto( + String reqstSvc, + String itrntPrjctCd, + Short sendTy, + Short sendCorp, + Long sendCnt, + Integer unitPrice + ) { + this.reqstSvc = reqstSvc; + this.itrntPrjctCd = itrntPrjctCd; + this.sendTy = sendTy; + this.sendCorp = sendCorp; + this.sendCnt = sendCnt; + this.unitPrice = unitPrice != null ? BigDecimal.valueOf(unitPrice) : BigDecimal.ZERO; + this.totalPrice = this.unitPrice.multiply(BigDecimal.valueOf(sendCnt != null ? sendCnt : 0L)); + } +} diff --git a/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctExcelDto.java b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctExcelDto.java new file mode 100644 index 0000000..742b465 --- /dev/null +++ b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctExcelDto.java @@ -0,0 +1,90 @@ +package com.kri.notihub.server.dto.report; + +import com.kri.common.feature.excel.ExcelColumn; +import com.kri.notihub.server.common.enums.SendCorpType; +import com.kri.notihub.server.common.enums.SendType; +import com.kri.notihub.server.common.external.intranet.dto.IntranetProjectBaseInfoDto; +import java.math.BigDecimal; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class SendCostSummaryWithItrntPrjctExcelDto { + + @ExcelColumn(header = "요청 서비스", order = 0) + private String reqstSvc; + + @ExcelColumn(header = "인트라넷 프로젝트 코드", order = 1) + private String itrntPrjctCd; + + @ExcelColumn(header = "프로젝트 명", order = 2) + private String title; + + @ExcelColumn(header = "연구팀 명", order = 3) + private String researchTeamName; + + @ExcelColumn(header = "발송 타입", order = 4) + private String sendTy; + + @ExcelColumn(header = "발송 업체", order = 5) + private String sendCorp; + + @ExcelColumn(header = "발송 건수", order = 6) + private Long sendCnt; + + @ExcelColumn(header = "단가", order = 7) + private BigDecimal unitPrice; + + @ExcelColumn(header = "총 비용", order = 8) + private BigDecimal totalPrice; + + public static SendCostSummaryWithItrntPrjctExcelDto from( + SendCostSummaryWithItrntPrjctDto source) { + if (source == null) { + return null; + } + + IntranetProjectBaseInfoDto projectInfo = source.getItrntPrjctInfo(); + + return SendCostSummaryWithItrntPrjctExcelDto.builder() + .reqstSvc(source.getReqstSvc()) + .itrntPrjctCd(source.getItrntPrjctCd()) + .title(projectInfo != null ? projectInfo.getTitle() : null) + .researchTeamName(projectInfo != null ? projectInfo.getResearchTeamName() : null) + .sendTy(getSendTypeLabel(source.getSendTy())) + .sendCorp(getSendCorpLabel(source.getSendCorp())) + .sendCnt(source.getSendCnt()) + .unitPrice(source.getUnitPrice()) + .totalPrice(source.getTotalPrice()) + .build(); + } + + private static String getSendTypeLabel(Short code) { + if (code == null) { + return null; + } + try { + return SendType.fromCode(code).name(); + } catch (IllegalArgumentException e) { + return code.toString(); + } + } + + private static String getSendCorpLabel(Short code) { + if (code == null) { + return null; + } + try { + return SendCorpType.fromCode(code).name(); + } catch (IllegalArgumentException e) { + return code.toString(); + } + } +} diff --git a/src/main/java/com/kri/notihub/server/persistence/repository/SendRepository.java b/src/main/java/com/kri/notihub/server/persistence/repository/SendRepository.java index 8058181..11422a4 100644 --- a/src/main/java/com/kri/notihub/server/persistence/repository/SendRepository.java +++ b/src/main/java/com/kri/notihub/server/persistence/repository/SendRepository.java @@ -2,6 +2,7 @@ package com.kri.notihub.server.persistence.repository; import com.kri.notihub.server.common.enums.ChannelType; import com.kri.notihub.server.common.enums.SendType; +import com.kri.notihub.server.dto.report.SendCostSummaryWithItrntPrjctDto; import com.kri.notihub.server.dto.report.SendResultLastWithSidDto; import com.kri.notihub.server.dto.report.SendResultSummaryDto; import com.kri.notihub.server.persistence.entity.TSendEntity; @@ -212,4 +213,29 @@ public interface SendRepository extends JpaRepository { @Param("sendStartDt") LocalDateTime sendStartDt, @Param("sendEndDt") LocalDateTime sendEndDt ); + + @Query(""" + select new com.kri.notihub.server.dto.report.SendCostSummaryWithItrntPrjctDto( + s.reqstSvc, + s.itrntPrjctCd, + s.sendTy, + s.sendCorp, + count(s), + coalesce(up.price, 0) + ) + from TSendEntity s + left join TUnitPriceEntity up + on up.sendTy = s.sendTy + and up.sendCorp = s.sendCorp + and up.use = true + where s.sendStatus = 1 + and s.sendDt >= :startDt + and s.sendDt < :endDt + group by s.reqstSvc, s.itrntPrjctCd, s.sendTy, s.sendCorp, up.price + order by s.reqstSvc, s.itrntPrjctCd, s.sendTy, s.sendCorp + """) + List findSendCostSummaryWithItrntPrjct( + @Param("startDt") LocalDateTime startDt, + @Param("endDt") LocalDateTime endDt + ); } diff --git a/src/main/java/com/kri/notihub/server/service/ReportService.java b/src/main/java/com/kri/notihub/server/service/ReportService.java index 6f39dd5..566caa1 100644 --- a/src/main/java/com/kri/notihub/server/service/ReportService.java +++ b/src/main/java/com/kri/notihub/server/service/ReportService.java @@ -1,6 +1,7 @@ package com.kri.notihub.server.service; import com.kri.common.feature.excel.ExcelMapper; +import com.kri.common.feature.excel.ExcelUtil; import com.kri.common.feature.web.BusinessException; import com.kri.common.feature.web.page.PageRequestDto; import com.kri.common.feature.web.page.PageResponseDto; @@ -8,8 +9,13 @@ import com.kri.common.feature.web.page.Pageables; 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.external.intranet.IntranetApiClient; +import com.kri.notihub.server.common.external.intranet.dto.IntranetProjectBaseInfoDto; import com.kri.notihub.server.dto.report.BulkSendInfoDto; import com.kri.notihub.server.dto.report.ReportExcelResponseDto; +import com.kri.notihub.server.dto.report.SendCostSummaryRequestDto; +import com.kri.notihub.server.dto.report.SendCostSummaryWithItrntPrjctDto; +import com.kri.notihub.server.dto.report.SendCostSummaryWithItrntPrjctExcelDto; import com.kri.notihub.server.dto.report.SendInfoDto; import com.kri.notihub.server.dto.report.SendResultCodeDto; import com.kri.notihub.server.dto.report.SendResultInfoDto; @@ -25,6 +31,7 @@ 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 com.kri.notihub.server.persistence.repository.UnitPriceRepository; +import jakarta.validation.Valid; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.math.BigDecimal; @@ -39,6 +46,7 @@ import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; import lombok.RequiredArgsConstructor; +import org.apache.poi.ss.usermodel.Workbook; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -54,6 +62,7 @@ public class ReportService { private final BulkSendRepository bulkSendRepository; private final SendRepository sendRepository; private final UnitPriceRepository unitPriceRepository; + private final IntranetApiClient intranetApiClient; public List getSendResultCodeList() { return sendResultCodeRepository.findAll().stream() @@ -203,6 +212,72 @@ public class ReportService { .toList(); } + public List getSendCostSummaryWithItrntPrjct( + SendCostSummaryRequestDto req + ) { + SendCostSummaryRequestDto condition = req != null ? req : new SendCostSummaryRequestDto(); + LocalDateTime startDt = condition.getStartDt().atStartOfDay(); + LocalDateTime endDt = condition.getEndDt().plusDays(1).atStartOfDay(); + + List summaries = + sendRepository.findSendCostSummaryWithItrntPrjct( + startDt, + endDt + ); + + List itrntPrjctCds = summaries.stream() + .map(SendCostSummaryWithItrntPrjctDto::getItrntPrjctCd) + .filter(StringUtils::hasText) + .distinct() + .toList(); + + Map projectInfoMap = + intranetApiClient.getProjectWithPChildSn(itrntPrjctCds).stream() + .filter(project -> StringUtils.hasText(project.getChildSn())) + .collect(Collectors.toMap( + IntranetProjectBaseInfoDto::getChildSn, + Function.identity(), + (left, right) -> left + )); + + summaries.forEach(summary -> + summary.setItrntPrjctInfo(projectInfoMap.get(summary.getItrntPrjctCd()))); + + return summaries; + } + + public ReportExcelResponseDto getSendCostSummaryWithItrntPrjctExcel( + SendCostSummaryRequestDto req + ) { + List summaries = + getSendCostSummaryWithItrntPrjct(req).stream() + .map(SendCostSummaryWithItrntPrjctExcelDto::from) + .toList(); + + String ts = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")); + String fileName = "발송 비용_%s.xlsx".formatted(ts); + + try ( + Workbook costWb = ExcelMapper.writeToWorkbook( + summaries, + SendCostSummaryWithItrntPrjctExcelDto.class + ); + ByteArrayOutputStream bos = new ByteArrayOutputStream() + ) { + costWb.write(bos); + byte[] bytes = bos.toByteArray(); + String base64 = Base64.getEncoder().encodeToString(bytes); + + return ReportExcelResponseDto.builder() + .fileName(fileName) + .fileBase64(base64) + .build(); + + } catch (Exception e) { + throw BusinessException.internalError("FAIL_WRITE_EXCEL",e.getMessage()); + } + } + public List getSendResultLastWithSid( ChannelType channelTy, String reqstSvc, diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 54cbf55..b7877fa 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -1,4 +1,6 @@ kri: + api-url: + intranet-extension: https://intranet-ext.kric.com/api/v1 body-image: public-base-url: https://noti-hub.kric.com/body-image auth: diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 0f1e45a..13e7c5d 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -1,4 +1,6 @@ kri: + api-url: + intranet-extension: https://intranet-ext.kric.com/api/v1 body-image: public-base-url: http://localhost:4011/body-image auth: diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 4be310c..1b14d7d 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -1,4 +1,6 @@ kri: + api-url: + intranet-extension: https://intranet-ext.kric.com/api/v1 body-image: public-base-url: https://noti-hub.kric.com/body-image auth: diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b01757c..ad3a9b1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -59,7 +59,7 @@ kri: auth: apikey: - key: "4B726963-4E6F7469487562-53657276696365-402531403136" + key: "4B726963-4E6F74696669636174696F6E-487562-4E6F7469487562" header-name: "X-API-Key" # API Key 헤더 enabled: true # API Key 인증 허용 여부 service-code: notihub-service