package com.oqpo.api.service.oper;


import com.oqpo.api.entity.oper.MainChartEntity;
import com.oqpo.api.mapper.oper.MainChartMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.util.StringUtil;
import com.oqpo.api.web.dto.response.oper.mainchart.ChartResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MainChartService extends CommonService {

    @Autowired
    private MainChartMapper mainChartMapper;

    public ChartResponse selectMainCount(String brandId, String storeId, String spplyId) throws Exception {
        MainChartEntity entity = null;
        if (!StringUtil.isEmpty(spplyId)) {
            entity = mainChartMapper.selectSpplyMainCount(spplyId);
        } else if (!StringUtil.isEmpty(storeId)) {
            entity = mainChartMapper.selectStoreMainCount(storeId);
        } else if (!StringUtil.isEmpty(brandId)) {
            entity = mainChartMapper.selectBrandMainCount(brandId);
        }

        ChartResponse result = new ChartResponse();
        result.setPr20Count( entity == null ? 0 : entity.getPr20Val() == null ? 0 : entity.getPr20Val() );       // 구매요청 건수 : 당일 구매요청 대기 건수
        result.setDp02Amt(entity == null ? 0 : entity.getDp02Val() == null ? 0 : entity.getDp02Val());         // 입금승인금액
        result.setUnpaidAmt(entity == null ? 0 : entity.getUnpaidAmt() == null ? 0 : entity.getUnpaidAmt());     // 미납금액
        result.setPo30Count(entity == null ? 0 : entity.getPo30Val() == null ? 0 : entity.getPo30Val());       // 발주진행
        result.setPoEndCount(entity == null ? 0 : entity.getDlvVal() == null ? 0 : entity.getDlvVal());       // 발주완료
        result.setPoPsblAmt(entity == null ? 0 : entity.getPoPsblAmt() == null ? 0 : entity.getPoPsblAmt());     // 구매가능금액
        result.setOrdersCount(entity == null ? 0 : entity.getOrdersCount() == null ? 0 : entity.getOrdersCount());     // 수주건수
        result.setOrdersAmt(entity == null ? 0 : entity.getOrdersAmt() == null ? 0 : entity.getOrdersAmt());         // 수주금액
        result.setSttlReqAmt(entity == null ? 0 : entity.getSttlReqAmt() == null ? 0 : entity.getSttlReqAmt());       // 정산요청금액
        return result;
    }

    public ChartResponse selectPchReqChart(String brandId, String storeId, String prType) throws Exception {
        MainChartEntity entity = mainChartMapper.selectPchReqChart(brandId, storeId, prType);

        String chartData = "[ [' ', '요청', '접수', '반려'], [' ', " + entity.getPr20Val() + " , " + entity.getPr30Val() + " , " + entity.getPr40Val() + "] ]";

        ChartResponse result = new ChartResponse();
        result.setChartData(chartData);
        return result;
    }

    public ChartResponse selectPchOdrChart(String brandId, String storeId, String spplyId, String poType) throws Exception {
        String chartData = null;

        if (StringUtil.isEmpty(spplyId)) {
            // 브랜드/매장
            MainChartEntity entity = mainChartMapper.selectPchOdrChart(brandId, storeId, spplyId, poType);
            chartData = "[ [' ', '요청', '접수', '취소'], [' ', " + entity.getPo20Val() + " , " + entity.getPo30Val() + " , " + entity.getPo99Val() + "] ]";
        } else {
            // 공급사
            MainChartEntity entity = mainChartMapper.selectPchOdrChart(brandId, storeId, spplyId, poType); // 수주대상
            chartData = "[ [' ', '요청', '접수', '취소'], [' ', " + entity.getPo20Val() + " , " + entity.getPo30Val() + " , " + entity.getPo99Val() + "] ]";

            // MainChartEntity deliEntity = mainChartMapper.selectDeliChart(brandId, storeId, spplyId, poType, "DLV0", "dlv_reg_dt"); // 납품서생성 (납품서생성/납품생성일자 비교)
            // chartData = "[ [' ', '수주대상', '납품서생성'], [' ', " + entity.getPo20Val() + " , " + deliEntity.getDlvVal() + "] ]";
        }
        ChartResponse result = new ChartResponse();
        result.setChartData(chartData);
        return result;
    }

    public ChartResponse selectPrRankChart(String brandId, String storeId, String spplyId, String prType) throws Exception {
        List<MainChartEntity> entityList = null;
        if (!StringUtil.isEmpty(spplyId)) {
            entityList = mainChartMapper.selectPoRankChart4Spply(spplyId, prType);
        } else {
            entityList = mainChartMapper.selectPrRankChart(brandId, storeId, prType);
        }

        StringBuffer retVal = new StringBuffer();
        if (entityList == null || entityList.size() < 1) {
            retVal.append("[ [' ', '품목없음'], [' ', 0] ]");
        } else {
            retVal.append("[  [' '");
            for (MainChartEntity entity : entityList) {
                retVal.append(", '" + entity.getItemNm() + "'");
            }
            retVal.append("], [' '");
            for (MainChartEntity entity : entityList) {
                retVal.append(", " + entity.getItemCnt());
            }
            retVal.append("]  ]");
        }
        ChartResponse result = new ChartResponse();
        result.setChartData(retVal.toString());
        return result;
    }

    public ChartResponse selectStlChart(String brandId, String storeId, String spplyId) throws Exception {
        MainChartEntity entity = null;
        if (!StringUtil.isEmpty(spplyId)) {
            entity = mainChartMapper.selectStlChart(null, null, spplyId);
        } else if (!StringUtil.isEmpty(storeId)) {
            entity = mainChartMapper.selectStlChart(null, storeId, null);
        } else if (!StringUtil.isEmpty(brandId)) {
            entity = mainChartMapper.selectStlChart(brandId, null, null);
        }
        // String chartData = "[ [' ', '요청', '확인', '반려'], [' ', " + entity.getSt10Val() + " , " + entity.getSt20Val() + " , " + entity.getSt30Val() + "] ]";
        String chartData = "[ [' ', ' '], ['요청', " + entity.getSt10Val() + "], ['확정', " + entity.getSt20Val() + "], ['반려', " + entity.getSt30Val() + "] ]";

        ChartResponse result = new ChartResponse();
        result.setChartData(chartData);
        return result;
    }

    public ChartResponse selectSftStckChart(String brandId, String storeId) throws Exception {
        List<MainChartEntity> entityList = mainChartMapper.selectSftStckChart(brandId, storeId);
        StringBuffer charVal = new StringBuffer();

        // String chartData = "[ [' ', '안전재고수량', '현재재고수량'], ['품목1', 1000, 400], ['품목2', 1170, 460], ]";
        if (entityList == null || entityList.size() < 1) {
            charVal.append("['품목없음', 0, 0], ");
        } else {
            for (MainChartEntity entity : entityList) {
                charVal.append("['" + entity.getItemNm() + "', " + entity.getSftStckQty() + ", " + entity.getStckQty() + "], ");
            }
        }

        ChartResponse result = new ChartResponse();
        result.setChartData("[ [' ', '안전재고수량', '현재재고수량'], " + charVal.toString() + " ]");
        return result;
    }

    public ChartResponse selectDeliChart(String brandId, String storeId, String spplyId, String deliType) throws Exception {

        String chartData = null;
        if (!StringUtil.isEmpty(storeId)) {
            // 매장 입고
            MainChartEntity entity1 = mainChartMapper.selectDeliChart(brandId, storeId, spplyId, deliType, "DLV0", "dlv_reg_dt"); // 대기 (납품서생성/납품등록일자 비교)
            MainChartEntity entity2 = mainChartMapper.selectDeliChart(brandId, storeId, spplyId, deliType, "DLV1", "dlv_cmplt_dt"); // 확인 (납품서완료/납품완료일자 비교)

            chartData = "[ [' ', '대기', '확인'], [' ', " + entity1.getDlvVal() + " , " + entity2.getDlvVal() + "] ]";
        } else if (!StringUtil.isEmpty(spplyId)) {
            // 공급사 출고
            // 출고 대상 : 납품서생성, 납품예정일자 비교
            // 출고 확인 : 납품서완료, 납품완료일자 비교
            MainChartEntity entity1 = mainChartMapper.selectDeliChart(brandId, storeId, spplyId, deliType, "DLV0", "dlv_reg_dt"); // 대기 (납품서생성/납품등록일자 비교)
            MainChartEntity entity2 = mainChartMapper.selectDeliChart(brandId, storeId, spplyId, deliType, "DLV1", "dlv_cmplt_dt"); // 확인 (납품서완료/납품완료일자 비교)

            chartData = "[ [' ', '대기', '완료'], [' ', " + entity1.getDlvVal() + " , " + entity2.getDlvVal() + "] ]";
        }
        ChartResponse result = new ChartResponse();
        result.setChartData(chartData);
        return result;
    }

}
