package com.oqpo.api.service.loanmng;


import com.oqpo.api.entity.settmng.PgKiccBaseInfoEntity;
import com.oqpo.api.entity.settmng.StlMgntBaseInfoEntity;
import com.oqpo.api.entity.settmng.StlMgntDtlPtclEntity;
import com.oqpo.api.mapper.loanmng.UnpaidMngMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.loanmng.StlDtlListResponse;
import com.oqpo.api.web.dto.response.loanmng.UnpaidMngInfoResponse;
import com.oqpo.api.web.dto.response.loanmng.UnpaidMngListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class UnpaidMngService extends CommonService {

    @Autowired
    private UnpaidMngMapper unpaidMngMapper;

    /*
      미납금관리 그리드 리스트 조회
     */
    public GridResponse selectUnpaidMngGridList(String sBrandId, String sStoreId, 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 = unpaidMngMapper.selectUnpaidMngGridCnt(sBrandId, sStoreId, fromDt, toDt);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<StlMgntBaseInfoEntity> entities = unpaidMngMapper.selectUnpaidMngGridList(sBrandId, sStoreId, fromDt, toDt, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> UnpaidMngListResponse.builder()
                        .viewCd("R")
                        .sttlMgntUnqNo(m.getSttlMgntUnqNo())
                        .sttlReqDt(m.getSttlReqDt())
                        .brandId(m.getBrandId())
                        .brandNm(m.getBrandNm())
                        .storeId(m.getStoreId())
                        .storeNm(m.getStoreNm())
                        .sttlStCd(m.getSttlStCd())
                        .sttlStNm(m.getSttlStNm())
                        .sttlAddStNm(m.getSttlAddStNm())
                        .sttlReqAmt(m.getSttlReqAmt())      /* 정산요청금액 */
                        .sttlDt(m.getSttlDt())              /* 정산확정일자 */
                        .sttlAmt(m.getSttlAmt())            /* 정산확정금액 */
                        .unpaidAmt(m.getUnpaidAmt())        /* 미납금액 */
                        .loanDvsn(m.getLoanDvsn())
                        .loanDvsnNm(m.getLoanDvsnNm())
                        .useAmtTotal(m.getUseAmtTotal())    /* 사용가능한도 */
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

    /* 미납금관리 정보 조회 */
    public UnpaidMngInfoResponse selectUnpaidMngInfo(String sttlMgntUnqNo) throws Exception {
        StlMgntBaseInfoEntity entity = unpaidMngMapper.selectStlMgntBaseInfo(sttlMgntUnqNo);
        // 브랜드 PG정보 조회
        PgKiccBaseInfoEntity pgInfo = unpaidMngMapper.selectPgKiccBaseInfo(entity.getBrandId());
        if (pgInfo != null) {
            entity.setMallId(pgInfo.getMallId());
        }
        return UnpaidMngInfoResponse.toDTO(entity);
    }

    /*
      정산대상내역 그리드 리스트 조회
     */
    public GridResponse selectStlDtlGridList(String sttlMgntUnqNo, GridRequest gridRequest) throws Exception {
        int gridPage = gridRequest.getGridPage();
        int gridSize = gridRequest.getGridSize();

        List<StlMgntDtlPtclEntity> entities = unpaidMngMapper.selectStlMgntDtlPtclList(sttlMgntUnqNo);
        int gridRecords = entities.size();
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<Object> gridRows = entities.stream()
                .map(m -> StlDtlListResponse.builder()
                        .viewCd("R")
                        .sttlMgntUnqNo(m.getSttlMgntUnqNo())
                        .sttlMgntDtlNo(m.getSttlMgntDtlNo())
                        .brandId(m.getBrandId())
                        .brandNm(m.getBrandNm())
                        .whsMgntUnqNo(m.getWhsMgntUnqNo())
                        .rtnMgntUnqNo(m.getRtnMgntUnqNo())
                        .storeId(m.getStoreId())
                        .storeNm(m.getStoreNm())
                        .itemId(m.getItemId())
                        .itemNm(m.getItemNm())
                        .unit(m.getUnit())
                        .unitAmt(m.getUnitAmt())
                        .podrQty(m.getPodrQty())
                        .dlvQty(m.getDlvQty())
                        .whsQty(m.getWhsQty())
                        .colQty(m.getColQty())
                        .whsColQty(m.getWhsColQty())        /* 입고/수거수량 */
                        .dlvCmpltDt(m.getDlvCmpltDt())
                        .colDt(m.getColDt())
                        .whsColDt(m.getWhsColDt())          /* 입고/수거일자 */
                        .whsAmt(m.getWhsAmt())
                        .colAmt(m.getColAmt())
                        .whsColAmt(m.getWhsColAmt())        /* 입고/수거금액 */
                        .sttlDvsn(m.getSttlDvsn())
                        .sttlDvsnNm(m.getSttlDvsnNm())
                        .podrUnqNo(m.getPodrUnqNo())
                        .podrDtlNo(m.getPodrDtlNo())
                        .pchReqUnqNo(m.getPchReqUnqNo())
                        .pchReqDtlNo(m.getPchReqDtlNo())
                        .rtnPodrUnqNo(m.getRtnPodrUnqNo())
                        .rtnPodrDtlNo(m.getRtnPodrDtlNo())
                        .rtnReqUnqNo(m.getRtnReqUnqNo())
                        .rtnReqDtlNo(m.getRtnReqDtlNo())
                        .sysRegDttm(m.getSysRegDttm())
                        .sysRegId(m.getSysRegId())
                        .sysChgDttm(m.getSysChgDttm())
                        .sysChgId(m.getSysChgId())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

}
