package com.casic.missiles.modular.system.service.impl;

import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.modular.system.dao.ScreenDataServiceMapper;
import com.casic.missiles.modular.system.service.ScreenDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ScreenDataServiceImpl implements ScreenDataService {


    private final ScreenDataServiceMapper screenDataServiceMapper;

    private final AbstractPermissionContext permissionService;

    private final DecimalFormat df = new DecimalFormat("0.00%");

    public ScreenDataServiceImpl(ScreenDataServiceMapper screenDataServiceMapper, AbstractPermissionContext permissionService) {
        this.screenDataServiceMapper = screenDataServiceMapper;
        this.permissionService = permissionService;
    }

    public Object getInstalledDevice(String beginTime, String endTime) {
        List<Map<String, Object>> installedDeviceList = null;
        try {
            installedDeviceList = initInstalledDevice();
        } catch (DataAccessException dex) {
            log.error("主题：查询燃气探测仪设备数据出现数据异常,开始时间{},结束时间{},异常信息{}", beginTime, endTime, dex);
        }
        return installedDeviceList;
    }


    public Object getKindsAlarmRate(String beginTime, String endTime) {
        List<Map<String, Object>> rateDeviceList = null;
        try {
            rateDeviceList = countTypeAlarm();
        } catch (DataAccessException dex) {
            log.error("主题：查询燃气探测仪设备数据出现数据异常,开始时间{},结束时间{},异常信息{}", beginTime, endTime, dex);
        }
        return rateDeviceList;
    }

    private List<Map<String, Object>> initInstalledDevice() {
        List<Map<String, Object>> installedDeviceList = new ArrayList<>();
        List<Map<String, String>> typeNameList = screenDataServiceMapper.getTypeName();
        Map<String, String> typeNameMap = typeNameList.stream().collect(
                Collectors.toMap(e -> String.valueOf(e.get("id")), e -> String.valueOf(e.get("typeName"))));
        List<Map<String, Integer>> deviceCountList = screenDataServiceMapper.countDeviceByType();
        deviceCountList.forEach(
                deviceCountMap -> {
                    Map<String, Object> installedDeviceMap = new HashMap<>();
                    installedDeviceMap.put("count", deviceCountMap.get("count"));
                    installedDeviceMap.put("typeName", typeNameMap.get(String.valueOf(deviceCountMap.get("deviceType"))));
                    installedDeviceList.add(installedDeviceMap);
                }
        );
        return installedDeviceList;
    }

    private List<Map<String, Object>> countTypeAlarm() {
        List<Map<String, Object>> rateDeviceList = new ArrayList<>();
        List<Map<String, String>> typeNameList = screenDataServiceMapper.getTypeName();
        Map<String, String> typeNameMap = typeNameList.stream().collect(
                Collectors.toMap(e -> String.valueOf(e.get("id")), e -> String.valueOf(e.get("typeName"))));
        List<Map<String, Integer>> deviceCountList = screenDataServiceMapper.countAlarmType();
        Integer totalAlarm = 0;
        for (Map deviceCount : deviceCountList) {
            totalAlarm += Integer.valueOf(deviceCount.get("count").toString());
        }
        for (Map deviceCountMap : deviceCountList) {
            Map<String, Object> rateDeviceMap = new HashMap<>();
            rateDeviceMap.put("rate", totalAlarm == 0 ? 0 : df.format(Double.valueOf(deviceCountMap.get("count") == null ? "0" : deviceCountMap.get("count").toString()) / totalAlarm));
            rateDeviceMap.put("typeName", typeNameMap.get(String.valueOf(deviceCountMap.get("deviceType"))));
            rateDeviceList.add(rateDeviceMap);
        }
        return rateDeviceList;
    }


}
