package com.icomsys.main_vm.biz.rcp.statistics.dashboard.service.impl;

import com.icomsys.main_vm.biz.rcp.statistics.dashboard.service.StatisticsDashboardService;
import com.icomsys.main_vm.db.mybatis.alias.StatisticsDashboardVO;
import com.icomsys.main_vm.db.mybatis.mapper.StatisticsDashboardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsDashboardServiceImpl implements StatisticsDashboardService {

    @Autowired
    private StatisticsDashboardMapper statisticsDashboardMapper;

    @Override
    public List<HashMap> selectStatCallInfo(StatisticsDashboardVO vo) {
        List<HashMap> result = statisticsDashboardMapper.selectStatCallInfo(vo);
        List<HashMap> convert = new ArrayList<>();
        List<String> oprDateList = new ArrayList<>();
        Map<String, Double> totCntMap = new HashMap<>();
        Map<String, Double> ingCntMap = new HashMap<>();
        Map<String, Double> endCntMap = new HashMap<>();

        for (HashMap e : result) {
            if (!oprDateList.contains(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                oprDateList.add(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
            }

            if (totCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                double cnt = totCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
                totCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), cnt += 1);
            } else {
                if (e.get("callInitDate") != null) {
                    totCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 1.0);
                } else {
                    totCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 0.0);
                }
            }

            if ("TRANSFER".equals(e.get("CALL_END_CONVERSION"))) {
                if (ingCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                    double cnt = ingCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
                    ingCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), cnt += 1);
                } else {
                    if (e.get("callInitDate") != null) {
                        ingCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 1.0);
                    } else {
                        ingCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 0.0);
                    }
                }
            }

            if ("END".equals(e.get("CALL_END_CONVERSION"))) {
                if (endCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                    double cnt = endCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
                    endCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), cnt += 1.0);
                } else {
                    if (e.get("callInitDate") != null) {
                        endCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 1.0);
                    } else {
                        endCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 0.0);
                    }
                }
            }
        }

        for (String e : oprDateList) {
            HashMap map = new HashMap();
            map.put("oprMngCode", e.split("/")[0]);
            map.put("callInitDate", e.split("/")[1]);
            convert.add(map);
        }

        for (HashMap convertMap : convert) {
            for (HashMap e : result) {
                if (convertMap.size() < 3) {
                    if (convertMap.get("oprMngCode").equals(String.valueOf(e.get("oprMngCode"))) && convertMap.get("callInitDate").equals(String.valueOf(e.get("callInitDateB")))) {
                        convertMap.put("totCnt", (totCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")) == null) ? 0 : totCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")));
                        convertMap.put("ingCnt", (ingCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")) == null) ? 0 : ingCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")));
                        convertMap.put("endCnt", (endCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")) == null) ? 0 : endCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")));
                    }
                }
            }
        }

        return convert;
    }

    @Override
    public List<HashMap> selectStatIntent(StatisticsDashboardVO vo) {
        List<HashMap> result = statisticsDashboardMapper.selectStatIntent(vo);
        List<HashMap> convert = new ArrayList<>();
        List<String> oprDateList = new ArrayList<>();
        Map<String, Double> totCntMap = new HashMap<>();
        Map<String, Double> succCntMap = new HashMap<>();
        Map<String, Double> failCntMap = new HashMap<>();

        for (HashMap e : result) {
            if (!oprDateList.contains(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                oprDateList.add(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
            }

            if (totCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                double cnt = totCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
                totCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), cnt += 1);
            } else {
                if (e.get("callInitDate") != null) {
                    totCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 1.0);
                } else {
                    totCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 0.0);
                }
            }

            if ("INTENT_SUCCESS".equals(e.get("INTENT_RESULT_CODE"))) {
                if (succCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                    double cnt = succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
                    succCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), cnt += 1);
                } else {
                    if (e.get("callInitDate") != null) {
                        succCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 1.0);
                    } else {
                        succCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 0.0);
                    }
                }
            }

            if (!"INTENT_SUCCESS".equals(e.get("INTENT_RESULT_CODE"))) {
                if (failCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                    double cnt = failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
                    failCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), cnt += 1.0);
                } else {
                    if (e.get("callInitDate") != null) {
                        failCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 1.0);
                    } else {
                        failCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 0.0);
                    }
                }
            }
        }

        for (String e : oprDateList) {
            HashMap map = new HashMap();
            map.put("oprMngCode", e.split("/")[0]);
            map.put("callInitDate", e.split("/")[1]);
            convert.add(map);
        }

        for (HashMap convertMap : convert) {
            for (HashMap e : result) {
                if (convertMap.size() < 3) {
                    if (convertMap.get("oprMngCode").equals(String.valueOf(e.get("oprMngCode"))) && convertMap.get("callInitDate").equals(String.valueOf(e.get("callInitDateB")))) {
                        convertMap.put("totCnt", (totCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")) == null) ? 0 : totCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")));
                        convertMap.put("succCnt", (succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")) == null) ? 0 : succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")));
                        convertMap.put("failCnt", (failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")) == null) ? 0 : failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")));
                    }
                }
            }
        }

        return convert;
    }

    @Override
    public List<HashMap> selectStatScenario(StatisticsDashboardVO vo) {
        List<HashMap> result = statisticsDashboardMapper.selectStatScenario(vo);
        List<HashMap> convert = new ArrayList<>();
        List<String> oprDateList = new ArrayList<>();
        Map<String, Double> useCntMap = new HashMap<>();
        Map<String, Double> succCntMap = new HashMap<>();
        Map<String, Double> failCntMap = new HashMap<>();

        for (HashMap e : result) {
            if (!oprDateList.contains(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                oprDateList.add(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
            }

            if (useCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                double cnt = useCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
                useCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), cnt += 1);
            } else {
                if (e.get("callInitDate") != null) {
                    useCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 1.0);
                } else {
                    useCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 0.0);
                }
            }

            if ("S".equals(e.get("SCENARIO_RESULT_CODE"))) {
                if (succCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                    double cnt = succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
                    succCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), cnt += 1);
                } else {
                    if (e.get("callInitDate") != null) {
                        succCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 1.0);
                    } else {
                        succCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 0.0);
                    }
                }
            }

            if ("F".equals(e.get("SCENARIO_RESULT_CODE"))) {
                if (failCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDateB"))) {
                    double cnt = failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB"));
                    failCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), cnt += 1.0);
                } else {
                    if (e.get("callInitDate") != null) {
                        failCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 1.0);
                    } else {
                        failCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDateB"), 0.0);
                    }
                }
            }
        }

        for (String e : oprDateList) {
            HashMap map = new HashMap();
            map.put("oprMngCode", e.split("/")[0]);
            map.put("callInitDate", e.split("/")[1]);
            convert.add(map);
        }

        for (HashMap convertMap : convert) {
            for (HashMap e : result) {
                if (convertMap.size() < 3) {
                    if (convertMap.get("oprMngCode").equals(String.valueOf(e.get("oprMngCode"))) && convertMap.get("callInitDate").equals(String.valueOf(e.get("callInitDateB")))) {
                        convertMap.put("useCnt", (useCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")) == null) ? 0 : useCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")));
                        convertMap.put("succCnt", (succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")) == null) ? 0 : succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")));
                        convertMap.put("failCnt", (failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")) == null) ? 0 : failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDateB")));
                    }
                }
            }
        }

        return convert;
    }

    @Override
    public List<HashMap> selectCallCntByToday(StatisticsDashboardVO vo) {
        return statisticsDashboardMapper.selectCallCntByToday(vo);
    }

    @Override
    public int selectCurCallCnt(StatisticsDashboardVO vo) {
        return statisticsDashboardMapper.selectCurCallCnt(vo);
    }
}
