package com.oqpo.api.service.salesmng;


import com.oqpo.api.mapper.salesmng.SalesMngMapper;
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.salesmng.ItemSalesGridResponse;
import com.oqpo.api.web.dto.response.salesmng.StoreSalesGridResponse;
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 SalesMngService extends CommonService {

    @Autowired
    private SalesMngMapper salesMngMapper;

    public GridResponse selectStoreSalesGridList(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 = salesMngMapper.selectStoreSalesGridCnt(sBrandId, sStoreId, fromDt, toDt);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<StoreSalesGridResponse> entities = salesMngMapper.selectStoreSalesGridList(sBrandId, sStoreId, fromDt, toDt, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> StoreSalesGridResponse.builder()
                        .viewCd("R")
                        .brandId(m.getBrandId())
                        .brandNm(m.getBrandNm())
                        .storeId(m.getStoreId())
                        .storeNm(m.getStoreNm())
                        .whsQty(m.getWhsQty())
                        .supplyAmt(m.getSupplyAmt())
                        .storeAmt(m.getStoreAmt())
                        .incomeAmt(m.getIncomeAmt())
                        .whsQty1(m.getWhsQty1())
                        .supplyAmt1(m.getSupplyAmt1())
                        .storeAmt1(m.getStoreAmt1())
                        .incomeAmt1(m.getIncomeAmt1())
                        .totIncomeAmt(m.getTotIncomeAmt())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

    public GridResponse selectItemSalesGridList(String sBrandId, String sStoreId, String sItemNm,
                                                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 = salesMngMapper.selectItemSalesGridCnt(sBrandId, sStoreId, sItemNm, fromDt, toDt);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<ItemSalesGridResponse> entities = salesMngMapper.selectItemSalesGridList(sBrandId, sStoreId, sItemNm, fromDt, toDt, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> ItemSalesGridResponse.builder()
                        .viewCd("R")
                        .brandId(m.getBrandId())
                        .brandNm(m.getBrandNm())
                        .storeId(m.getStoreId())
                        .storeNm(m.getStoreNm())
                        .itemId(m.getItemId())
                        .itemNm(m.getItemNm())
                        .whsQty(m.getWhsQty())
                        .supplyAmt(m.getSupplyAmt())
                        .storeAmt(m.getStoreAmt())
                        .incomeAmt(m.getIncomeAmt())
                        .whsQty1(m.getWhsQty1())
                        .supplyAmt1(m.getSupplyAmt1())
                        .storeAmt1(m.getStoreAmt1())
                        .incomeAmt1(m.getIncomeAmt1())
                        .totIncomeAmt(m.getTotIncomeAmt())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

}
