package com.oqpo.api.service.loanmng;


import com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity;
import com.oqpo.api.entity.loanmng.LoanMgntDtlHstEntity;
import com.oqpo.api.entity.stockmng.StckCddMgntInfoEntity;
import com.oqpo.api.entity.stockmng.StckMgntBaseInfoEntity;
import com.oqpo.api.entity.stockmng.StckMgntDtlHistEntity;
import com.oqpo.api.enums.ChgDvsn;
import com.oqpo.api.enums.InOutCd;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.loanmng.LoanStateMapper;
import com.oqpo.api.mapper.stockmng.StockMngMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.stockmng.ProcStockRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.loanmng.LoanHistListResponse;
import com.oqpo.api.web.dto.response.loanmng.LoanMngInfoResponse;
import com.oqpo.api.web.dto.response.loanmng.LoanStateListResponse;
import com.oqpo.api.web.dto.response.stockmng.StockCddInfoResponse;
import com.oqpo.api.web.dto.response.stockmng.StockHistListResponse;
import com.oqpo.api.web.dto.response.stockmng.StockMngListResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class LoanStateService extends CommonService {

    @Autowired
    private LoanStateMapper loanStateMapper;

    /*
      여신현황 그리드 리스트 조회
     */
    public GridResponse selectLoanStateGridList(String sBrandId, String sStoreId, String sLoanDvsn, String sUseYn, GridRequest gridRequest) throws Exception {
        int gridPage = gridRequest.getGridPage();
        int gridSize = gridRequest.getGridSize();

        int gridRecords = loanStateMapper.selectLoanStateGridCnt(sBrandId, sStoreId, sLoanDvsn, sUseYn);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<LoanMgntBaseInfoEntity> entities = loanStateMapper.selectLoanStateGridList(sBrandId, sStoreId, sLoanDvsn, sUseYn, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> LoanStateListResponse.builder()
                        .viewCd("R")
                        .loanMgntUnqNo(m.getLoanMgntUnqNo())
                        .storeId(m.getStoreId())
                        .storeNm(m.getStoreNm())
                        .brandId(m.getBrandId())
                        .brandNm(m.getBrandNm())
                        .loanDvsn(m.getLoanDvsn())
                        .loanDvsnNm(m.getLoanDvsnNm())
                        .credtLimitAmt(m.getCredtLimitAmt())
                        .chrgeLimitAmt(m.getChrgeLimitAmt())
                        .credtLimitUseYn(m.getCredtLimitUseYn())
                        .useAmtTotal(m.getUseAmtTotal())
                        .tmpRcvAcctTotal(m.getTmpRcvAcctTotal())
                        .useYn(m.getUseYn())
                        .useYnNm(m.getUseYnNm())
                        .sysChgDttm(m.getSysChgDttm())
                        .sysChgId(m.getSysChgId())
                        .sysChgNm(m.getSysChgNm())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

    /* 여신관리 정보 */
    public LoanMngInfoResponse selectLoanMngInfo(String loanMgntUnqNo) throws Exception {
        LoanMgntBaseInfoEntity entity = loanStateMapper.selectLoanMgntBaseInfo(loanMgntUnqNo);
        return LoanMngInfoResponse.toDTO(entity);
    }

    /*
      여신이력 그리드 리스트 조회
     */
    public GridResponse selectLoanHistGridList(String sLoanMgntUnqNo, String sDpstPayDvsn, 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 = loanStateMapper.selectLoanHistGridCnt(sLoanMgntUnqNo, sDpstPayDvsn, fromDt, toDt);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<LoanMgntDtlHstEntity> entities = loanStateMapper.selectLoanHistGridList(sLoanMgntUnqNo, sDpstPayDvsn, fromDt, toDt, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> LoanHistListResponse.builder()
                        .viewCd("R")
                        .loanMgntUnqNo(m.getLoanMgntUnqNo())
                        .loanRegDt(m.getLoanRegDt())
                        .loanRegTm(m.getLoanRegTm())
                        .dpstPayDvsn(m.getDpstPayDvsn())
                        .dpstPayDvsnNm(m.getDpstPayDvsnNm())
                        .trscAmt(m.getTrscAmt())
                        .trscBfBal(m.getTrscBfBal())
                        .trscAfBal(m.getTrscAfBal())
                        .mediaDvsn(m.getMediaDvsn())
                        .mediaDvsnNm(m.getMediaDvsnNm())
                        .rcvAcctNo(m.getRcvAcctNo())
                        .paymAcctNo(m.getPaymAcctNo())
                        .dpstDt(m.getDpstDt())
                        .dpstNm(m.getDpstNm())
                        .dpstBnkCd(m.getDpstBnkCd())
                        .dpstBnkNm(m.getDpstBnkNm())
                        .dsptMgntNo(m.getDsptMgntNo())
                        .loanDesc(m.getLoanDesc())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

}
