SttlStateService.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.oqpo.api.service.sttlmng;
  2. import com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity;
  3. import com.oqpo.api.entity.loanmng.LoanMgntDtlHstEntity;
  4. import com.oqpo.api.entity.settmng.StlMgntBaseInfoEntity;
  5. import com.oqpo.api.enums.DpstPayDvsn;
  6. import com.oqpo.api.enums.MediaDvsn;
  7. import com.oqpo.api.enums.PayType;
  8. import com.oqpo.api.enums.SttlStCd;
  9. import com.oqpo.api.mapper.loanmng.DsptMngMapper;
  10. import com.oqpo.api.mapper.sttlmng.SttlReqMapper;
  11. import com.oqpo.api.mapper.sttlmng.SttlStateMapper;
  12. import com.oqpo.api.service.CommonService;
  13. import com.oqpo.api.util.DateUtil;
  14. import com.oqpo.api.web.dto.request.GridRequest;
  15. import com.oqpo.api.web.dto.request.sttlmng.SttlConfirmRequest;
  16. import com.oqpo.api.web.dto.response.GridResponse;
  17. import com.oqpo.api.web.dto.response.sttlmng.SttlStateGridResponse;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import java.util.List;
  23. import java.util.stream.Collectors;
  24. @Service
  25. @Slf4j
  26. public class SttlStateService extends CommonService {
  27. @Autowired
  28. private SttlStateMapper sttlStateMapper;
  29. @Autowired
  30. private SttlReqMapper sttlReqMapper;
  31. @Autowired
  32. private DsptMngMapper dsptMngMapper;
  33. /*
  34. 정산요청 그리드 리스트 조회
  35. */
  36. public GridResponse selectSttlStateGridList(String sGubun, String sBrandId, String sStoreId, String sSpplyId, String sItemNm,
  37. String sSttlStCd, String fromDt, String toDt, GridRequest gridRequest) throws Exception {
  38. int gridPage = gridRequest.getGridPage();
  39. int gridSize = gridRequest.getGridSize();
  40. fromDt = fromDt == null ? null : fromDt.replace(".", "");
  41. toDt = toDt == null ? null : toDt.replace(".", "");
  42. int gridRecords = sttlStateMapper.selectSttlStateGridCnt(sGubun, sBrandId, sStoreId, sSpplyId, sItemNm, sSttlStCd, fromDt, toDt);
  43. int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
  44. List<StlMgntBaseInfoEntity> entities = sttlStateMapper.selectSttlStateGridList(sGubun, sBrandId, sStoreId, sSpplyId, sItemNm, sSttlStCd, fromDt, toDt, gridRequest);
  45. List<Object> gridRows = entities.stream()
  46. .map(m -> SttlStateGridResponse.builder()
  47. .viewCd("R")
  48. .sttlMgntUnqNo(m.getSttlMgntUnqNo()) /* 정산관리고유번호 */
  49. .brandId(m.getBrandId())
  50. .brandNm(m.getBrandNm())
  51. .storeId(m.getStoreId())
  52. .storeNm(m.getStoreNm())
  53. .spplyId(m.getSpplyNm())
  54. .spplyNm(m.getSpplyNm())
  55. .sttlStCd(m.getSttlStCd()) /* 정산상태코드 */
  56. .sttlStNm(m.getSttlStNm()) /* 정산상태명 */
  57. .sttlReqDt(m.getSttlReqDt()) /* 정산요청일자 */
  58. .sttlDt(m.getSttlDt()) /* 정산확정일자 */
  59. .sttlReqAmt(m.getSttlReqAmt())
  60. .sttlAmt(m.getSttlAmt())
  61. .unpaidAmt(m.getUnpaidAmt())
  62. .sttlRegMgrId(m.getSttlRegMgrId())
  63. .sttlRegMgrNm(m.getSttlRegMgrNm())
  64. .loanDvsn(m.getLoanDvsn())
  65. .loanDvsnNm(m.getLoanDvsnNm())
  66. .build())
  67. .collect(Collectors.toList());
  68. return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
  69. }
  70. /* 정산완료 */
  71. @Transactional
  72. public void endSttl(String userId, SttlConfirmRequest sttlConfirmRequest) throws Exception {
  73. // 정산기본정보 조회
  74. StlMgntBaseInfoEntity stlEntity = sttlReqMapper.selectStlMgntBaseInfo(sttlConfirmRequest.getSttlMgntUnqNo());
  75. // 정산관리기본정보 - 정산상태코드 변경
  76. stlEntity.setSttlStCd(SttlStCd.STTL_CONFIRM.getCd());
  77. stlEntity.setSttlDt(DateUtil.getCurrentDate());
  78. stlEntity.setSttlAmt(stlEntity.getSttlReqAmt()); // 정산요청금액을 정산금액으로 지정한다.
  79. sttlReqMapper.updateStlMgntBaseInfo4SttlConfirm(userId, stlEntity);
  80. if ("N".equals(stlEntity.getRcptYn())) { // 수납여부가 N인 경우
  81. // 매장의 계좌잔액이 있은 경우 정산지급 처리를 한다.
  82. // 매장 계좌잔액 조회
  83. LoanMgntBaseInfoEntity loanEntity = dsptMngMapper.selectLoanMgntBaseInfo(stlEntity.getBrandId(), stlEntity.getStoreId());
  84. if (loanEntity.getAcctBal() > 0) {
  85. // 계좌잔액이 미납금액 이상인 경우 미납금액을 지급처리하고 정산 데이터는 수납처리한다.
  86. // 계좌잔액이 미납금액 미만인 경우 계좌잔액을 지급처리하고 정산 데이터는 일부 수납처리한다. (미납금 발생)
  87. long trscAmt = 0; // 수납금액
  88. if (loanEntity.getAcctBal() >= stlEntity.getUnpaidAmt()) {
  89. trscAmt = stlEntity.getUnpaidAmt();
  90. } else {
  91. trscAmt = loanEntity.getAcctBal();
  92. }
  93. // 1. 여신관리기본정보 처리
  94. long trscBfBal = loanEntity.getAcctBal();
  95. long trscAfBal = loanEntity.getAcctBal() - trscAmt;
  96. loanEntity.setAcctBal(trscAfBal); // 계좌잔액
  97. loanEntity.setUseAmtTotal(loanEntity.getUseAmtTotal() - trscAmt); // 사용금액합계 감소
  98. sttlReqMapper.updateLoanMgntBaseInfo4SttlReqProc(userId, loanEntity);
  99. // 2. 여신관리상세이력
  100. LoanMgntDtlHstEntity hstEntity = new LoanMgntDtlHstEntity();
  101. hstEntity.setLoanMgntUnqNo(loanEntity.getLoanMgntUnqNo());
  102. hstEntity.setLoanRegDt(DateUtil.getCurrentDate());
  103. hstEntity.setLoanRegTm(DateUtil.getCurrentTime());
  104. hstEntity.setDpstPayDvsn(DpstPayDvsn.PAYMENT.getCd());
  105. hstEntity.setMediaDvsn(MediaDvsn.SETTLMENT.getCd());
  106. hstEntity.setPayType(PayType.STTL_PAY.getCd());
  107. hstEntity.setTrscAmt(trscAmt); // 거래금액
  108. hstEntity.setTrscBfBal(trscBfBal); // 거래전잔액
  109. hstEntity.setTrscAfBal(trscAfBal); // 거래후잔액
  110. dsptMngMapper.insertLoanMgntDtlHst(userId, hstEntity);
  111. // 3. 정산관리기본정보 수납처리
  112. long rcptAmt = stlEntity.getRcptAmt() + trscAmt; // 수납금액
  113. stlEntity.setRcptYn(rcptAmt >= stlEntity.getSttlReqAmt() ? "Y" : "N");
  114. stlEntity.setRcptAmt(rcptAmt);
  115. sttlReqMapper.updateStlMgntBaseInfo4Rcpt(userId, stlEntity);
  116. }
  117. }
  118. }
  119. }