diff --git a/src/main/java/cokr/xit/ens/modules/kkotalk/service/support/KkoTalkApiService.java b/src/main/java/cokr/xit/ens/modules/kkotalk/service/support/KkoTalkApiService.java index 567963a..f8d5f05 100644 --- a/src/main/java/cokr/xit/ens/modules/kkotalk/service/support/KkoTalkApiService.java +++ b/src/main/java/cokr/xit/ens/modules/kkotalk/service/support/KkoTalkApiService.java @@ -229,9 +229,11 @@ public class KkoTalkApiService { * @param reqDTO KkotalkDTO.BulkSendRequest * @return KkotalkDTO.BulkSendResponse */ - public String requestSendBulk2(final KkotalkDTO.BulkSendRequest reqDTO) { + public ResponseEntity requestSendBulk2(final KkotalkDTO.BulkSendRequest reqDTO) { + ResponseEntity resEntity = null; + if(Checks.isEmpty(reqDTO.getProductCode())){ - throw BizRuntimeException.create("상품 코드는 필수 입니다."); + return new ResponseEntity("상품 코드는 필수 입니다.", HttpStatus.BAD_REQUEST); } List errors = new ArrayList<>(); @@ -273,14 +275,14 @@ public class KkoTalkApiService { } } if(!errors.isEmpty()){ - throw BizRuntimeException.create(errors.toString()); + return new ResponseEntity(errors.toString(), HttpStatus.BAD_REQUEST); } String param = "{\"envelopes\":" + JsonUtils.toJson(envelopes) + "}"; return webClient.exchangeKkotalk( HOST + API_BULKSEND[0].replace("{PRODUCT_CODE}", reqDTO.getProductCode()), HttpMethod.valueOf(API_BULKSEND[1]), param, - String.class, + ResponseEntity.class, getRlaybsnmInfo(reqDTO)); } diff --git a/src/main/java/cokr/xit/ens/modules/kkotalk/service/support/KkoTalkSender.java b/src/main/java/cokr/xit/ens/modules/kkotalk/service/support/KkoTalkSender.java index 55d6587..6e36e1f 100644 --- a/src/main/java/cokr/xit/ens/modules/kkotalk/service/support/KkoTalkSender.java +++ b/src/main/java/cokr/xit/ens/modules/kkotalk/service/support/KkoTalkSender.java @@ -6,6 +6,7 @@ import java.util.stream.*; import org.apache.commons.lang3.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.*; +import org.springframework.http.*; import org.springframework.stereotype.*; import org.springframework.transaction.annotation.*; @@ -54,7 +55,7 @@ public class KkoTalkSender extends SendProcTemplate { @Override @Transactional(propagation = Propagation.REQUIRES_NEW) - public EnsResponseVO execute(Long sendMastId) { + public EnsResponseVO execute(Long sendMastId) { SendMast sendMast = null; List sendDetails = null; @@ -63,83 +64,99 @@ public class KkoTalkSender extends SendProcTemplate { if (CmmnUtil.isEmpty(sendMastId)) throw new EnsException(EnsErrCd.SEND410, "발송마스터ID(은)는 필수조건 입니다."); - sendMast = sendMastRepository.findById(sendMastId).orElseThrow(() -> new EnsException(EnsErrCd.SEND404, String.format("일치하는 발송마스터 자료가 없습니다. [ sendMastId %s ]", sendMastId))); + sendMast = sendMastRepository.findById(sendMastId) + .orElseThrow(() -> new EnsException(EnsErrCd.SEND404, + String.format("일치하는 발송마스터 자료가 없습니다. [ sendMastId %s ]", sendMastId))); sendDetails = talkMapper.findAllFetchBySendMastId(sendMast.getSendMastId()); OrgMng orgMng = orgMngService.find(sendMast.getOrgCd()).getResultInfo(); - Lists.partition(sendDetails, SEND_BATCH_UNIT).stream() - .forEach(list -> { - - String resp = null; - List envelopes = null; - - try { - - envelopes = this.makeMessage(list); - resp = kkoTalkApi.requestSendBulk2( - KkotalkDTO.BulkSendRequest.builder() - .envelopes(envelopes) - .signguCode(orgMng.getOrgCd()) - .ffnlgCode("11") - - .build() - ); - - EnsResponseVO mResponse = this.respMsgToMap(resp); - if (!EnsErrCd.OK.equals(mResponse.getErrCode())) - throw new EnsException(mResponse.getErrCode(), mResponse.getErrMsg()); - List> documents = (List>) ((Map) mResponse.getResultInfo()).get("envelopeIds"); - - Map mApiResp = documents.stream() - .map(this::toApiRespVOMap) - .collect(Collectors.toMap(m -> String.valueOf(m.get("key")), m -> (KkoTalkApiRespVO) m.get("value"), (k1, k2) -> k1)); - - list.stream() - .forEach(row -> this.modifyTalkSendRsltByExtDocUuid(row, mApiResp)); - - } catch (EnsException e) { - - list.stream() - .forEach(row -> { - row.setStatus(KkoTalkStatusCd.SENT_FAIL); - row.setErrorCode(e.getErrCd().getCode()); - row.setErrorMessage(e.getMessage()); - }); - } catch (Exception e) { - - list.stream() - .forEach(row -> { - row.setStatus(KkoTalkStatusCd.SENT_FAIL); - row.setErrorCode(EnsErrCd.SEND500.getCode()); - row.setErrorMessage(e.getMessage()); - }); - } finally { - if (!CmmnUtil.isEmpty(envelopes)) - sendDetailKkoTalkReqHistRepository.saveAll(this.toSendDetailReqHist(list, JsonUtils.toJson(envelopes), resp)); - } - - }); - + .forEach(list -> { + + List envelopes = null; + String sendRespBody = null; + + try { + + envelopes = this.makeMessage(list); + ResponseEntity resp = kkoTalkApi.requestSendBulk2( + KkotalkDTO.BulkSendRequest.builder() + // FIXME: 카카오톡 신규 추가 - 테스트를 위해 임시로 넣음 + .productCode("D10_2") + .envelopes(envelopes) + .signguCode(orgMng.getOrgCd()) + .ffnlgCode("11") + + .build() + ); + + sendRespBody = resp.getBody(); + if (resp.getStatusCode() != HttpStatus.OK) + throw new EnsException(EnsErrCd.SEND620, String.format("전송요청 중.. %s %s", resp.getStatusCode().toString(), resp.getBody())); + EnsResponseVO mResponse = this.respMsgToMap(resp.getBody()); + if (!EnsErrCd.OK.equals(mResponse.getErrCode())) + throw new EnsException(mResponse.getErrCode(), mResponse.getErrMsg()); + + + List> documents = (List>)((Map)mResponse.getResultInfo()).get( + "envelopeIds"); + + Map mApiResp = documents.stream() + .map(this::toApiRespVOMap) + .collect(Collectors.toMap(m -> String.valueOf(m.get("key")), + m -> (KkoTalkApiRespVO)m.get("value"), (k1, k2) -> k1)); + + list.stream() + .forEach(row -> this.modifyTalkSendRsltByExtDocUuid(row, mApiResp)); + + } catch (EnsException e) { + + list.stream() + .forEach(row -> { + row.setStatus(KkoTalkStatusCd.SENT_FAIL); + row.setErrorCode(e.getErrCd().getCode()); + row.setErrorMessage(e.getMessage()); + }); + } catch (Exception e) { + + list.stream() + .forEach(row -> { + row.setStatus(KkoTalkStatusCd.SENT_FAIL); + row.setErrorCode(EnsErrCd.SEND500.getCode()); + row.setErrorMessage(e.getMessage()); + }); + } finally { + if (!CmmnUtil.isEmpty(envelopes)) + sendDetailKkoTalkReqHistRepository.saveAll( + this.toSendDetailReqHist(list, JsonUtils.toJson(envelopes), sendRespBody)); + } + + }); Optional cntSuccess = sendDetails.stream() - .map(row -> CmmnUtil.isEmpty(row.getErrorCode()) ? 1 : 0) - .reduce(Integer::sum); + .map(row -> CmmnUtil.isEmpty(row.getErrorCode()) ? 1 : 0) + .reduce(Integer::sum); if (cntSuccess.get() > 0) respVO = EnsResponseVO.okBuilder() - .resultInfo(sendDetails.stream() - .map(row -> this.toResultInfo(row)) - .collect(Collectors.toList())) - .build(); + .resultInfo(sendDetails.stream() + .map(row -> this.toResultInfo(row)) + .collect(Collectors.toList())) + .build(); else throw new EnsException(EnsErrCd.SEND500, String.format("전체건수 전송 실패(총 %d 건)", sendDetails.size())); - } catch (EnsException e) { + } catch (BizRuntimeException be){ + respVO = EnsResponseVO.errBuilder() + .errCode(EnsErrCd.SEND500) + .errMsg(be.getMessage()) + .build(); + } catch (EnsException e) { respVO = EnsResponseVO.errBuilder().errCode(e.getErrCd()).errMsg(e.getMessage()).build(); - } catch (Exception e) { + } catch (Exception e) { respVO = EnsResponseVO.errBuilder().errCode(EnsErrCd.SEND500).errMsg(e.getMessage()).build(); + } finally { if (!CmmnUtil.isEmpty(sendMast)) { if (EnsErrCd.OK.equals(respVO.getErrCode())) { @@ -278,9 +295,7 @@ public class KkoTalkSender extends SendProcTemplate { * @return */ private List toSendDetailReqHist(List list, String sendMsg, String respMsg) { - - - List> sendMsgList = (List>) gson.fromJson(sendMsg, Map.class).get("envelopes"); + List sendMsgList = JsonUtils.toObjectList(sendMsg, Map.class); Map mSendDetailIds = list.stream() .map(row -> { Map m = new HashMap<>(); @@ -290,7 +305,7 @@ public class KkoTalkSender extends SendProcTemplate { }) .collect(Collectors.toMap(m -> String.valueOf(m.get("key")), m -> (Long) m.get("value"), (k1, k2) -> k1)); - EnsResponseVO respVO = this.respMsgToMap(respMsg); + EnsResponseVO respVO = this.respMsgToMap(respMsg); if (EnsErrCd.OK.equals(respVO.getErrCode())) {