package com.nokia.tsl_data.service;

import com.nokia.tsl_data.dao.HighQualityCountMapper;
import com.nokia.tsl_data.dao.SysDataDictionaryRepository;
import com.nokia.tsl_data.properties.TslDataProperties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 处理客户端统计数据
 */
@Service
public class HighQualityCountService {

    @Autowired
    private HighQualityCountMapper highQualityCountMapper;
    @Autowired
    private SysDataDictionaryRepository sysDataDictionaryRepository;
    @Autowired
    private TslDataProperties tslDataProperties;

    public List<List<Object>> getThreeRateOfCityV3(String day) {
        List<List<Object>> result = new ArrayList<>();
        // 各地市
        for (String cityName : sysDataDictionaryRepository.findAllCityName()) {
            List<Object> list = new ArrayList<>();
            list.add(cityName);
            result.add(list);
        }
        // 顺序是 满意率 解决率 响应率
        Map<String, Double[]> map = generateThreeRateOfCityV3(day);
        result.forEach(list -> {
            Double[] arr = map.get(list.get(0).toString());
            list.add(arr[0]);
            list.add(arr[1]);
            list.add(arr[2]);
            // 移网综合投诉评价“三率”=响应率*0.1+解决率*0.4+满意率*0.5
            double rate = arr[2] * 0.1 + arr[1] * 0.4 + arr[0] * 0.5;
            list.add(rate);
            list.add(tslDataProperties.getEvaluation());
            list.add(tslDataProperties.getEvaluation() - rate);
        });
        // 逆序排序
        result.sort((o1, o2) -> Double.compare((double) o2.get(6), (double) o1.get(6)));
        // 全省
        Double[] allCity = map.get("全省");
        double rate = allCity[2] * 0.1 + allCity[1] * 0.4 + allCity[0] * 0.5;
        result.add(new ArrayList<Object>() {
            {
                add("全省");
                add(allCity[0]);
                add(allCity[1]);
                add(allCity[2]);
                add(rate);
                add(tslDataProperties.getEvaluation());
                add(tslDataProperties.getEvaluation() - rate);
            }
        });
        return result;
    }

    /**
     * 客户端地市满意度 客户端地市解决率 客户端地市响应率
     */
    public Map<String, Double[]> generateThreeRateOfCityV3(String day) {
        // 顺序是 满意率 解决率 响应率
        Map<String, Double[]> result = new HashMap<>();
        for (String cityName : sysDataDictionaryRepository.findAllCityName()) {
            Double[] arr = new Double[6];
            Arrays.fill(arr, 1.0d);
            result.put(cityName, arr);
        }
        Double[] allCity = new Double[3];
        result.put("全省", allCity);
        double numerator = 0, denominator = 0;
        // 客户端地市满意率
        List<Map<String, Object>> satisfiedOfCity = highQualityCountMapper.selectSatisfiedOfCity(day);
        for (Map<String, Object> map : satisfiedOfCity) {
            String key = (String) map.get("city");
            Double[] arr;
            if (key.equals("雄安新区")) {
                arr = result.get("雄安");
            } else {
                arr = result.get(key);
            }
            arr[0] = (double) map.get("rate");
            numerator += (double) map.get("numerator");
            denominator += (double) map.get("denominator");
        }
        allCity[0] = numerator / denominator;
        // 客户端地市解决率
        numerator = 0;
        denominator = 0;
        List<Map<String, Object>> resolutionOfCity = highQualityCountMapper.selectResolutionOfCity(day);
        for (Map<String, Object> map : resolutionOfCity) {
            String key = (String) map.get("city");
            Double[] arr;
            if (key.equals("雄安新区")) {
                arr = result.get("雄安");
            } else {
                arr = result.get(key);
            }
            arr[1] = (double) map.get("rate");
            numerator += (double) map.get("numerator");
            denominator += (double) map.get("denominator");
        }
        allCity[1] = numerator / denominator;
        // 客户端地市响应率
        numerator = 0;
        denominator = 0;
        List<Map<String, Object>> responseOfCity = highQualityCountMapper.selectResponseOfCity(day);
        for (Map<String, Object> map : responseOfCity) {
            String key = (String) map.get("city");
            Double[] arr;
            if (key.equals("雄安新区")) {
                arr = result.get("雄安");
            } else {
                arr = result.get(key);
            }
            arr[2] = (double) map.get("rate");
            numerator += (double) map.get("numerator");
            denominator += (double) map.get("denominator");
        }
        allCity[2] = numerator / denominator;

        return result;
    }

    /**
     * 客户端地市满意度 客户端地市解决率 客户端地市响应率
     */
    public Map<String, List<Object>> generateThreeRateOfCity(String day) {
        // 全部地市
        List<String> allCityName = sysDataDictionaryRepository.findAllCityName();
        Map<String, List<Object>> result = new HashMap<>();
        for (String cityName : allCityName) {
            List<Object> list = new ArrayList<>();
            list.add(cityName);
            result.put(cityName, list);
        }
        List<Object> allCity = new ArrayList<>();
        allCity.add("全省");
        result.put("全省", allCity);
        double numerator = 0, denominator = 0, rate;
        // 客户端地市满意度
        List<Map<String, Object>> satisfiedOfCity = highQualityCountMapper.selectSatisfiedOfCity(day);
        for (Map<String, Object> map : satisfiedOfCity) {
            String key = (String) map.get("city");
            List<Object> list;
            if (key.equals("雄安新区")) {
                list = result.get("雄安");
            } else {
                list = result.get(key);
            }
            list.add(map.get("rate"));
            numerator += (double) map.get("numerator");
            denominator += (double) map.get("denominator");
            list.add(tslDataProperties.getSatisfiedCompliance());
            list.add((double) map.get("rate") - tslDataProperties.getSatisfiedCompliance());
        }
        rate = numerator / denominator;
        allCity.add(rate);
        allCity.add(tslDataProperties.getSatisfiedCompliance());
        allCity.add(rate - tslDataProperties.getSatisfiedCompliance());
        // 客户端地市解决率
        numerator = 0;
        denominator = 0;
        List<Map<String, Object>> resolutionOfCity = highQualityCountMapper.selectResolutionOfCity(day);
        for (Map<String, Object> map : resolutionOfCity) {
            String key = (String) map.get("city");
            List<Object> list;
            if (key.equals("雄安新区")) {
                list = result.get("雄安");
            } else {
                list = result.get(key);
            }
            list.add(map.get("rate"));
            numerator += (double) map.get("numerator");
            denominator += (double) map.get("denominator");
            list.add(tslDataProperties.getResolutionCompliance());
            list.add((double) map.get("rate") - tslDataProperties.getResolutionCompliance());
        }
        rate = numerator / denominator;
        allCity.add(rate);
        allCity.add(tslDataProperties.getResolutionCompliance());
        allCity.add(rate - tslDataProperties.getResolutionCompliance());
        // 客户端地市响应率
        numerator = 0;
        denominator = 0;
        List<Map<String, Object>> responseOfCity = highQualityCountMapper.selectResponseOfCity(day);
        for (Map<String, Object> map : responseOfCity) {
            String key = (String) map.get("city");
            List<Object> list;
            if (key.equals("雄安新区")) {
                list = result.get("雄安");
            } else {
                list = result.get(key);
            }
            list.add(map.get("rate"));
            numerator += (double) map.get("numerator");
            denominator += (double) map.get("denominator");
            list.add(tslDataProperties.getResponseCompliance());
            list.add((double) map.get("rate") - tslDataProperties.getResponseCompliance());
        }
        rate = numerator / denominator;
        allCity.add(rate);
        allCity.add(tslDataProperties.getResponseCompliance());
        allCity.add(rate - tslDataProperties.getResponseCompliance());
        return result;
    }

    /**
     * 客户端地市和全省按天级的统计数据
     */
    public Map<String, int[]> getComplaintsForDay(String day) {
        // 预处理时间
        String monthId = day.substring(0, 6);
        int dayIndex = Integer.parseInt(day.substring(6));

        List<String> allCityName = sysDataDictionaryRepository.findAllCityName();
        Map<String, int[]> result = new HashMap<>();
        // 每个地市一个数组，数组保存dayIndex天数据
        for (String areaName : allCityName) {
            result.put(areaName, new int[dayIndex + 1]);
        }
        // 全省
        result.put("全省", new int[dayIndex + 1]);
        for (int i = 1; i <= dayIndex; i++) {
            // 拼接一个表示天的字符串，天如果小于10要补0
            String newDay = i >= 10 ? monthId + i : monthId + "0" + i;
            // 这里有一个查询操作
            List<Map<String, Object>> totalComplaintsForDay = highQualityCountMapper
                    .selectTotalComplaintsForDay(newDay);
            if (totalComplaintsForDay.size() == 0) {
                throw new RuntimeException(String.format("HighQualityCount 缺少 %s 账期数据", newDay));
            }
            int provinceAll = 0;
            for (Map<String, Object> map : totalComplaintsForDay) {
                String city = map.get("businoareaname").toString();
                // 这里雄安新区需要特殊处理一下
                city = city.equals("雄安新区") ? "雄安" : city;
                int[] arr = result.get(city);
                arr[i - 1] = Integer.parseInt(map.get("total_complaints").toString());
                provinceAll += arr[i - 1];
                if (i == dayIndex) {
                    arr[dayIndex] = arr[dayIndex - 1];
                }
            }
            result.get("全省")[i - 1] = provinceAll;
            if (i == dayIndex) {
                result.get("全省")[dayIndex] = provinceAll;
            }
        }
        // 以上得到的result是每天累加，需要计算一下每天的增量
        for (int[] arr : result.values()) {
            for (int i = dayIndex - 1; i >= 1; i--) {
                arr[i] = arr[i] - arr[i - 1];
            }
        }
        return result;
    }
}
