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

import com.icomsys.main_vm.biz.rcp.statistics.call.service.StatisticsCallInfoService;
import com.icomsys.main_vm.biz.rcp.statistics.call.vo.StatisticsCallInfoExcelVO;
import com.icomsys.main_vm.db.mybatis.alias.StatisticsCallInfoVO;
import com.icomsys.main_vm.db.mybatis.mapper.StatisticsCallInfoMapper;
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 StatisticsCallInfoServiceImpl implements StatisticsCallInfoService {

    @Autowired
    private StatisticsCallInfoMapper statisticsCallInfoMapper;

    @Override
    public List<HashMap> selectStatCallInfo(StatisticsCallInfoVO vo) {
        List<HashMap> result = statisticsCallInfoMapper.selectStatCallInfo(vo);
        List<HashMap> convert = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        Map<String, Double> totCntMap = new HashMap<>();

        Map<String, Double> traUserReqCntMap = new HashMap<>();
        Map<String, Double> traScenarioCntMap = new HashMap<>();
        Map<String, Double> traSilentFbCntMap = new HashMap<>();
        Map<String, Double> traSpeakFbCntMap = new HashMap<>();
        Map<String, Double> traTotCntMap = new HashMap<>();

        Map<String, Double> endUserReqCntMap = new HashMap<>();
        Map<String, Double> endScenarioCntMap = new HashMap<>();
        Map<String, Double> endSilentFbCntMap = new HashMap<>();
        Map<String, Double> endSpeakFbCntMap = new HashMap<>();
        Map<String, Double> endInitEndCntMap = new HashMap<>();
        Map<String, Double> endUserEndCntMap = new HashMap<>();
        Map<String, Double> endSystemCntMap = new HashMap<>();
        Map<String, Double> endTotCntMap = new HashMap<>();

        Map<String, Double> talkCntMap = new HashMap<>();

        for (HashMap e : result) {
            if (!dateList.contains(String.valueOf(e.get("callInitDate")))) {
                dateList.add(String.valueOf(e.get("callInitDate")));
            }

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

            if ("TRANSFER".equals(e.get("callEndConversion"))) {
                if ("USER_REQ".equals(e.get("callEndReason"))) {
                    if (traUserReqCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = traUserReqCntMap.get(String.valueOf(e.get("callInitDate")));
                        traUserReqCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        traUserReqCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (traTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = traTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        traTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        traTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }

                if ("SCENARIO".equals(e.get("callEndReason"))) {
                    if (traScenarioCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = traScenarioCntMap.get(String.valueOf(e.get("callInitDate")));
                        traScenarioCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        traScenarioCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (traTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = traTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        traTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        traTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }

                if ("SILENT_FB".equals(e.get("callEndReason"))) {
                    if (traSilentFbCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = traSilentFbCntMap.get(String.valueOf(e.get("callInitDate")));
                        traSilentFbCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        traSilentFbCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (traTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = traTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        traTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        traTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }

                if ("SPEAK_FB".equals(e.get("callEndReason"))) {
                    if (traSpeakFbCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = traSpeakFbCntMap.get(String.valueOf(e.get("callInitDate")));
                        traSpeakFbCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        traSpeakFbCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (traTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = traTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        traTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        traTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }
            }

            if ("END".equals(e.get("callEndConversion"))) {
                if ("USER_REQ".equals(e.get("callEndReason"))) {
                    if (endUserReqCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endUserReqCntMap.get(String.valueOf(e.get("callInitDate")));
                        endUserReqCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endUserReqCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (endTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }

                if ("SCENARIO".equals(e.get("callEndReason"))) {
                    if (endScenarioCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endScenarioCntMap.get(String.valueOf(e.get("callInitDate")));
                        endScenarioCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endScenarioCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (endTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }

                if ("SILENT_FB".equals(e.get("callEndReason"))) {
                    if (endSilentFbCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endSilentFbCntMap.get(String.valueOf(e.get("callInitDate")));
                        endSilentFbCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endSilentFbCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (endTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }

                if ("SPEAK_FB".equals(e.get("callEndReason"))) {
                    if (endSpeakFbCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endSpeakFbCntMap.get(String.valueOf(e.get("callInitDate")));
                        endSpeakFbCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endSpeakFbCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (endTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }

                if ("INIT_END".equals(e.get("callEndReason"))) {
                    if (endInitEndCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endInitEndCntMap.get(String.valueOf(e.get("callInitDate")));
                        endInitEndCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endInitEndCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (endTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }

                if ("USER_END".equals(e.get("callEndReason"))) {
                    if (endUserEndCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endUserEndCntMap.get(String.valueOf(e.get("callInitDate")));
                        endUserEndCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endUserEndCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (endTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }

                if ("SYSTEM".equals(e.get("callEndReason"))) {
                    if (endSystemCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endSystemCntMap.get(String.valueOf(e.get("callInitDate")));
                        endSystemCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endSystemCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }

                    if (endTotCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                        double cnt = endTotCntMap.get(String.valueOf(e.get("callInitDate")));
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), cnt += 1);
                    } else {
                        endTotCntMap.put(String.valueOf(e.get("callInitDate")), 1.0);
                    }
                }
            }

            if (talkCntMap.containsKey(String.valueOf(e.get("callInitDate")))) {
                double cnt = talkCntMap.get(String.valueOf(e.get("callInitDate")));
                talkCntMap.put(String.valueOf(e.get("callInitDate")), cnt += Double.valueOf(String.valueOf(e.get("talkSeqCnt"))));
            } else {
                talkCntMap.put(String.valueOf(e.get("callInitDate")), Double.valueOf(String.valueOf(e.get("talkSeqCnt"))));
            }
        }

        for (String e : dateList) {
            HashMap map = new HashMap();
            map.put("callInitDate", e);
            convert.add(map);
        }

        double tmpTalkCnt = 0.0;
        double tmpTotCnt = 1.0;

        for (HashMap convertMap : convert) {
            for (HashMap e : result) {
                if (convertMap.size() < 2) {
                    if (convertMap.get("callInitDate").equals(String.valueOf(e.get("callInitDate")))) {
                        convertMap.put("totCnt", (totCntMap.get(e.get("callInitDate")) == null) ? 0 : totCntMap.get(e.get("callInitDate")));

                        convertMap.put("ingUserReq", (traUserReqCntMap.get(e.get("callInitDate")) == null) ? 0 : traUserReqCntMap.get(e.get("callInitDate")));
                        convertMap.put("ingScenario", (traScenarioCntMap.get(e.get("callInitDate")) == null) ? 0 : traScenarioCntMap.get(e.get("callInitDate")));
                        convertMap.put("ingSilentFb", (traSilentFbCntMap.get(e.get("callInitDate")) == null) ? 0 : traSilentFbCntMap.get(e.get("callInitDate")));
                        convertMap.put("ingSpeakFb", (traSpeakFbCntMap.get(e.get("callInitDate")) == null) ? 0 : traSpeakFbCntMap.get(e.get("callInitDate")));
                        convertMap.put("ingTot", (traTotCntMap.get(e.get("callInitDate")) == null) ? 0 : traTotCntMap.get(e.get("callInitDate")));

                        convertMap.put("endUserReq", (endUserReqCntMap.get(e.get("callInitDate")) == null) ? 0 : endUserReqCntMap.get(e.get("callInitDate")));
                        convertMap.put("endScenario", (endScenarioCntMap.get(e.get("callInitDate")) == null) ? 0 : endScenarioCntMap.get(e.get("callInitDate")));
                        convertMap.put("endSilentFb", (endSilentFbCntMap.get(e.get("callInitDate")) == null) ? 0 : endSilentFbCntMap.get(e.get("callInitDate")));
                        convertMap.put("endSpeakFb", (endSpeakFbCntMap.get(e.get("callInitDate")) == null) ? 0 : endSpeakFbCntMap.get(e.get("callInitDate")));
                        convertMap.put("endInitEnd", (endInitEndCntMap.get(e.get("callInitDate")) == null) ? 0 : endInitEndCntMap.get(e.get("callInitDate")));
                        convertMap.put("endUserEnd", (endUserEndCntMap.get(e.get("callInitDate")) == null) ? 0 : endUserEndCntMap.get(e.get("callInitDate")));
                        convertMap.put("endSystem", (endSystemCntMap.get(e.get("callInitDate")) == null) ? 0 : endSystemCntMap.get(e.get("callInitDate")));
                        convertMap.put("endTot", (endTotCntMap.get(e.get("callInitDate")) == null) ? 0 : endTotCntMap.get(e.get("callInitDate")));

                        tmpTalkCnt = (talkCntMap.get(e.get("callInitDate")) == null) ? 0.0 : talkCntMap.get(e.get("callInitDate"));
                        tmpTotCnt = (totCntMap.get(e.get("callInitDate")) == null) ? 1.0 : totCntMap.get(e.get("callInitDate"));

                        convertMap.put("aveTalkCnt", Math.round(tmpTalkCnt/tmpTotCnt*10)/10.0);
                    }
                }
            }
        }

        return convert;
    }
}
