package com.oqpo.api.service.sttlmng;


import com.oqpo.api.entity.settmng.PgKiccPayPtclEntity;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.sttlmng.KiccPayMngMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.service.loanmng.EasypayPaymentService;
import com.oqpo.api.util.DateUtil;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.loanmng.EasypayPaymentRequest;
import com.oqpo.api.web.dto.request.sttlmng.KiccPayCancelRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.loanmng.EasypayPaymentResponse;
import com.oqpo.api.web.dto.response.sttlmng.KiccPayGridResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class KiccPayMngService extends CommonService {

    @Autowired
    private KiccPayMngMapper kiccPayMngMapper;

    @Autowired
    private EasypayPaymentService easypayPaymentService;

    public GridResponse selectKiccPayGridList(String sBrandId, String sStoreId, String sTranType, String sAuthNo,
                                              String fromDt, String toDt, GridRequest gridRequest) throws Exception {
        int gridPage = gridRequest.getGridPage();
        int gridSize = gridRequest.getGridSize();

        fromDt = fromDt == null ? null : fromDt.replace(".", "");
        toDt = toDt == null ? null : toDt.replace(".", "");

        int gridRecords = kiccPayMngMapper.selectKiccPayGridCnt(sBrandId, sStoreId, sTranType, sAuthNo, fromDt, toDt);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<PgKiccPayPtclEntity> entities = kiccPayMngMapper.selectKiccPayGridList(sBrandId, sStoreId, sTranType, sAuthNo, fromDt, toDt, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> KiccPayGridResponse.builder()
                        .viewCd("R")
                        .poOrdNo(m.getPoOrdNo())
                        .brandId(m.getBrandId())
                        .storeId(m.getStoreId())
                        .storeNm(m.getStoreNm())
                        .mallId(m.getMallId())
                        .paymentNo(m.getPaymentNo())
                        .tranType(m.getTranType())
                        .tranTypeNm(m.getTranTypeNm())
                        .resCd(m.getResCd())
                        .resMsg(m.getResMsg())
                        .cno(m.getCno())
                        .amount(m.getAmount())
                        .authNo(m.getAuthNo())
                        .tranDate(m.getTranDate())
                        .statCd(m.getStatCd())
                        .statMsg(m.getStatMsg())
                        .payType(m.getPayType())
                        .cardNo(m.getCardNo())
                        .issuerCd(m.getIssuerCd())
                        .issuerNm(m.getIssuerNm())
                        .acquirerCd(m.getAcquirerCd())
                        .acquirerNm(m.getAcquirerNm())
                        .installPeriod(m.getInstallPeriod())
                        .noint(m.getNoint())
                        .cancelYn(m.getCancelYn())
                        .cancelDate(m.getCancelDate())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

    @Transactional
    public void procCancel(String userId, HttpServletRequest request, KiccPayCancelRequest kiccPayCancelRequest) throws Exception {
        PgKiccPayPtclEntity entity = kiccPayMngMapper.selectPgKiccPayPtcl(kiccPayCancelRequest.getPoOrdNo());

        EasypayPaymentRequest easypayPaymentRequest = new EasypayPaymentRequest();
        easypayPaymentRequest.setEP_tr_cd("00201000");
        easypayPaymentRequest.setEP_mall_id(entity.getMallId());
        easypayPaymentRequest.setMgr_txtype("40");
        easypayPaymentRequest.setMgr_subtype("RF01");
        easypayPaymentRequest.setOrg_cno(entity.getCno());
        easypayPaymentRequest.setReq_id(userId);
        easypayPaymentRequest.setOrg_payment_no(entity.getPoOrdNo());
        // 취소 주문번호 등
        easypayPaymentRequest.setPoOrdNo(fnGetDealNo(36, ""));
        easypayPaymentRequest.setBrandId(entity.getBrandId());
        easypayPaymentRequest.setStoreId(entity.getStoreId());

        EasypayPaymentResponse response = easypayPaymentService.requestPayment(userId, request, easypayPaymentRequest);

        if ("0000".equals(response.getRes_cd())) {
            entity.setCancelYn("Y");
            entity.setCancelDate(DateUtil.getCurrentDateTime());
            kiccPayMngMapper.updatePgKiccPayPtcl4Cancel(userId, entity);
        } else {
            throw new GlobalException(SystemMessageCode.ERR_PG_FAIL);
        }

    }

}
