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

import com.icomsys.main_vm.biz.rcp.statistics.scenario.service.StatisticsScenarioService;
import com.icomsys.main_vm.biz.test.controller.FailNodeVO;
import com.icomsys.main_vm.db.mybatis.alias.MonitoringOutscenarioVO;
import com.icomsys.main_vm.db.mybatis.alias.MonitoringScenarioBreakVO;
import com.icomsys.main_vm.db.mybatis.mapper.StatisticsScenarioMapper;
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 StatisticsScenarioServiceImpl implements StatisticsScenarioService {

    @Autowired
    private StatisticsScenarioMapper statisticsScenarioMapper;

    @Override
    public List<HashMap> selectStatScenarioDay(MonitoringOutscenarioVO vo) {
        List<HashMap> result = statisticsScenarioMapper.selectStatScenarioDay(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("callInitDate"))) {
                oprDateList.add(e.get("oprMngCode") + "/" + e.get("callInitDate"));
            }

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

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

            if ("F".equals(e.get("scenarioResultCode"))) {
                if (failCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDate"))) {
                    double cnt = failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate"));
                    failCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDate"), cnt += 1.0);
                } else {
                    failCntMap.put(e.get("oprMngCode") + "/" + e.get("callInitDate"), 1.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("callInitDate")))) {
                        convertMap.put("oprMngCodeName", e.get("oprMngCodeName"));
                        convertMap.put("useCnt", useCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate")));
                        convertMap.put("succCnt", (succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate")) == null) ? 0 : succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate")));
                        convertMap.put("failCnt", (failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate")) == null) ? 0 : failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate")));
                        convertMap.put("succPer", Math.round((succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate")) == null) ? 0.0 : succCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate"))/useCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate"))*1000)/10.0);
                        convertMap.put("failPer", Math.round((failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate")) == null) ? 0.0 : failCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate"))/useCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate"))*1000)/10.0);
                    }
                }
            }
        }

        return convert;
    }

    @Override
    public List<HashMap> selectStatScenarioItem(MonitoringOutscenarioVO vo) {
        List<HashMap> result = statisticsScenarioMapper.selectStatScenarioItem(vo);
        List<HashMap> convert = new ArrayList<>();
        List<String> oprSeqList = new ArrayList<>();
        Map<String, Double> useCntMap = new HashMap<>();
        Map<String, Double> succCntMap = new HashMap<>();
        Map<String, Double> failCntMap = new HashMap<>();
        double totCnt = result.size();

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

            if (useCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("scenarioSeq"))) {
                double cnt = useCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq"));
                useCntMap.put(e.get("oprMngCode") + "/" + e.get("scenarioSeq"), cnt += 1);
            } else {
                useCntMap.put(e.get("oprMngCode") + "/" + e.get("scenarioSeq"), 1.0);
            }

            if ("S".equals(e.get("scenarioResultCode"))) {
                if (succCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("scenarioSeq"))) {
                    double cnt = succCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq"));
                    succCntMap.put(e.get("oprMngCode") + "/" + e.get("scenarioSeq"), cnt += 1);
                } else {
                    succCntMap.put(e.get("oprMngCode") + "/" + e.get("scenarioSeq"), 1.0);
                }
            }

            if ("F".equals(e.get("scenarioResultCode"))) {
                if (failCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("scenarioSeq"))) {
                    double cnt = failCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq"));
                    failCntMap.put(e.get("oprMngCode") + "/" + e.get("scenarioSeq"), cnt += 1.0);
                } else {
                    failCntMap.put(e.get("oprMngCode") + "/" + e.get("scenarioSeq"), 1.0);
                }
            }
        }

        for (String e : oprSeqList) {
            HashMap map = new HashMap();
            map.put("oprMngCode", e.split("/")[0]);
            map.put("scenarioSeq", 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("scenarioSeq").equals(String.valueOf(e.get("scenarioSeq")))) {
                        convertMap.put("scenarioName", e.get("scenarioName"));
                        convertMap.put("oprMngCodeName", e.get("oprMngCodeName"));
                        convertMap.put("useCnt", useCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq")));
                        convertMap.put("succCnt", (succCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq")) == null) ? 0 : succCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq")));
                        convertMap.put("failCnt", (failCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq")) == null) ? 0 : failCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq")));
                        convertMap.put("usePer", Math.round(useCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq"))/totCnt*1000)/10.0);
                        convertMap.put("succPer", Math.round((succCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq")) == null) ? 0.0 : succCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq"))/useCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq"))*1000)/10.0);
                        convertMap.put("failPer", Math.round((failCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq")) == null) ? 0.0 : failCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq"))/useCntMap.get(e.get("oprMngCode") + "/" + e.get("scenarioSeq"))*1000)/10.0);
                    }
                }
            }
        }

        return convert;
    }
}
