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

import com.icomsys.main_vm.biz.rcp.statistics.intent.service.StatisticsIntentService;
import com.icomsys.main_vm.db.mybatis.alias.MonitoringIntentAnalysisVO;
import com.icomsys.main_vm.db.mybatis.mapper.StatisticsIntentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
public class StatisticsIntentServiceImpl implements StatisticsIntentService {

    @Autowired
    private StatisticsIntentMapper statisticsIntentMapper;

    @Override
    public List<HashMap> selectStatIntentDay(MonitoringIntentAnalysisVO vo) {
        List<HashMap> result = statisticsIntentMapper.selectStatIntentDay(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<>();
        Map<String, Double> avgIntentPerMap = new HashMap<>();

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

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

            if ("INTENT_SUCCESS".equals(e.get("intentResultCode"))) {
                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 (!"INTENT_SUCCESS".equals(e.get("intentResultCode"))) {
                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);
                }
            }

            double score = e.get("score") == null || !StringUtils.hasText(String.valueOf(e.get("score"))) ? 0 : Double.valueOf(String.valueOf(e.get("score")));
            if (avgIntentPerMap.containsKey(e.get("oprMngCode") + "/" + e.get("callInitDate"))) {
                double cnt = avgIntentPerMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate"));
                avgIntentPerMap.put(e.get("oprMngCode") + "/" + e.get("callInitDate"), cnt += score);
            } else {
                avgIntentPerMap.put(e.get("oprMngCode") + "/" + e.get("callInitDate"), score);
            }
        }

        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("oprMngCode", e.get("oprMngCode"));
                        convertMap.put("totCnt", totCntMap.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"))/totCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate"))*1000)/10.0);
                        convertMap.put("avgIntentPer", Math.round(avgIntentPerMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate"))/totCntMap.get(e.get("oprMngCode") + "/" + e.get("callInitDate"))*10)/10.0);
                    }
                }
            }
        }

        return convert;
    }

    @Override
    public List<HashMap> selectStatIntentItem(MonitoringIntentAnalysisVO vo) {
        List<HashMap> result = statisticsIntentMapper.selectStatIntentItem(vo);
        List<HashMap> convert = new ArrayList<>();
        List<String> oprIdList = new ArrayList<>();
        Map<String, Double> totCntMap = new HashMap<>();
        Map<String, Double> succCntMap = new HashMap<>();
        Map<String, Double> failCntMap = new HashMap<>();
        Map<String, Double> avgIntentPerMap = new HashMap<>();

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

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

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

            if (!"INTENT_SUCCESS".equals(e.get("intentResultCode"))) {
                if (failCntMap.containsKey(e.get("oprMngCode") + "/" + e.get("intentId"))) {
                    double cnt = failCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId"));
                    failCntMap.put(e.get("oprMngCode") + "/" + e.get("intentId"), cnt += 1.0);
                } else {
                    failCntMap.put(e.get("oprMngCode") + "/" + e.get("intentId"), 1.0);
                }
            }

            double score = e.get("score") == null || !StringUtils.hasText(String.valueOf(e.get("score"))) ? 0 : Double.valueOf(String.valueOf(e.get("score")));
            if (avgIntentPerMap.containsKey(e.get("oprMngCode") + "/" + e.get("intentId"))) {
                double cnt = avgIntentPerMap.get(e.get("oprMngCode") + "/" + e.get("intentId"));
                avgIntentPerMap.put(e.get("oprMngCode") + "/" + e.get("intentId"), cnt += score);
            } else {
                avgIntentPerMap.put(e.get("oprMngCode") + "/" + e.get("intentId"), score);
            }
        }

        for (String e : oprIdList) {
            HashMap map = new HashMap();
            map.put("oprMngCode", e.split("/")[0]);
            map.put("intentId", 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("intentId").equals(String.valueOf(e.get("intentId")))) {
                        convertMap.put("oprMngCodeName", e.get("oprMngCodeName"));
                        convertMap.put("oprMngCode", e.get("oprMngCode"));
                        convertMap.put("intentGroup", e.get("intentGroup"));
                        convertMap.put("intentId", e.get("intentId"));
                        convertMap.put("intentName", e.get("intentName"));
                        convertMap.put("totCnt", totCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId")));
                        convertMap.put("succCnt", (succCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId")) == null) ? 0 :  succCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId")));
                        convertMap.put("failCnt", (failCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId")) == null) ? 0 : failCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId")));
                        convertMap.put("succPer", Math.round((succCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId")) == null) ? 0.0 : succCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId"))/totCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId"))*1000)/10.0);
                        convertMap.put("avgIntentPer", Math.round(avgIntentPerMap.get(e.get("oprMngCode") + "/" + e.get("intentId"))/totCntMap.get(e.get("oprMngCode") + "/" + e.get("intentId"))*10)/10.0);
                    }
                }
            }
        }

        return convert;
    }
}
