Newer
Older
zq-big-sreen / src / main / java / com / casic / service / impl / AlarmBulider.java
package com.casic.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.casic.config.DeviceTypesConfig;
import com.casic.dao.smartwell.SmartwellDataMapper;
import com.casic.dao.spantilt.TiltDataMapper;
import com.casic.util.FormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class AlarmBulider {

    @Resource
    private SmartwellDataMapper smartwellDataMapper;
    @Resource
    private TiltDataMapper tiltDataMapper;
    @Autowired
    private DeviceTypesConfig deviceTypesConfig;


    public List<Map<String, String>> alarmTypeManger(String beginTime, String endTime) {
        String[] deviceTypeList = deviceTypesConfig.getDeviceTypes().split(",");
        Map<String, String> typeNameMap = smartwellDeviceType();
        List<Map<String, String>> rateDeviceList = calculateRateSmartwell(beginTime, endTime, typeNameMap, deviceTypeList);
        calculateRateTilt(beginTime, endTime, rateDeviceList, typeNameMap);
        return rateDeviceList;
    }

    public List<Map<String, Object>> jobsByDeptManger(String beginTime, String endTime) {

        List<Map<String, Object>> jobDeptList = smartwellDataMapper.jobsByDeptId(beginTime, endTime);
        Map<String, Map<String, Object>> jobDeptMap = jobDeptList.stream().collect(
                Collectors.toMap(e -> String.valueOf(e.get("deptid")), e -> e)
        );
        return this.jobsByDept(jobDeptMap);
    }

    public List<Map<String, Object>> alarmsByDay() {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
        List<Map<String, Object>> mapList = new ArrayList<>();
        try {
            List<Map<String, Object>> alarmsList = smartwellDataMapper.countAlarmsByDay(deviceTypesConfig.getAlarmDay());
            Map<String,String> alarmsMap=alarmsList.stream().collect(
                Collectors.toMap(e->String.valueOf(e.get("alarmtimedate")),e->String.valueOf(e.get("count")))
             );
            Calendar beginDate = Calendar.getInstance();
            beginDate.setTime(new Date());
            beginDate.add(Calendar.DAY_OF_MONTH, 1-deviceTypesConfig.getAlarmDay());
            Calendar endDate = Calendar.getInstance();
            endDate.setTime(new Date());
            while (beginDate.getTime().compareTo(endDate.getTime()) <= 0) {
                Map dataMap = new HashMap();
                dataMap.put("date", sdf2.format(beginDate.getTime()));
                dataMap.put("alarmCount",alarmsMap.containsKey(sdf1.format(beginDate.getTime()))?alarmsMap.get(sdf1.format(beginDate.getTime())):"0");
                mapList.add(dataMap);
                beginDate.add(Calendar.DAY_OF_MONTH, 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapList;
    }

    private Map<String, String> smartwellDeviceType() {
        List<Map<String, String>> typeNameList = smartwellDataMapper.getTypeName();
        Map<String, String> typeNameMap = typeNameList.stream().collect(
                Collectors.toMap(e -> String.valueOf(e.get("id")), e -> String.valueOf(e.get("typeName"))));
        return typeNameMap;
    }

    private List<Map<String, String>> calculateRateSmartwell(String beginTime, String endTime, Map<String, String> typeNameMap, String[] deviceTypeList) {
        List<Map<String, String>> rateDeviceList = new ArrayList<>();
        List<Map<String, Integer>> alarmCountList = smartwellDataMapper.countAlarmType(beginTime, endTime);
        Map<String, Integer> alarmCountMap = alarmCountList.stream().collect(
                Collectors.toMap(
                        e -> String.valueOf(e.get("deviceType")), e -> Integer.valueOf(String.valueOf(e.get("count")))
                )
        );
        List<Map<String, Integer>> deviceCountList = smartwellDataMapper.countDeviceByType();
        Map<String, String> deviceCountMap = deviceCountList.stream().collect(
                Collectors.toMap(e -> String.valueOf(e.get("deviceType")), e -> String.valueOf(e.get("count"))));
        for (String deviceType : deviceTypeList) {
            Map<String, String> rateDeviceMap = new HashMap<>();
            Integer totalAlarm = deviceCountMap.containsKey(deviceType) ? Integer.valueOf(deviceCountMap.get(deviceType)) : 0;
            Integer alarmCount = alarmCountMap.containsKey(deviceType) ? Integer.valueOf(alarmCountMap.get(deviceType)) : 0;
            rateDeviceMap.put("rate", totalAlarm == 0 ? "0.00%" : FormatUtil.DF.format(alarmCount.doubleValue() / totalAlarm.doubleValue()));
            rateDeviceMap.put("typeName", typeNameMap.get(deviceType));
            rateDeviceList.add(rateDeviceMap);
        }
        return rateDeviceList;
    }

    private void calculateRateTilt(String beginTime, String endTime, List<Map<String, String>> rateDeviceList, Map<String, String> typeNameMap) {
        Integer alarmCount = tiltDataMapper.countColudAlarm(beginTime, endTime);
        Integer totalAlarm = tiltDataMapper.countColudDevice();
        Map<String, String> rateDeviceMap = new HashMap<>();
        rateDeviceMap.put("rate", totalAlarm == 0 ? "0" : FormatUtil.DF.format(Double.valueOf(alarmCount) / totalAlarm));
        rateDeviceMap.put("typeName", "云台");
        rateDeviceList.add(rateDeviceMap);
    }

    private List<Map<String, Object>> jobsByDept(Map<String, Map<String, Object>> jobDeptMap) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<Map<String, String>> deptMapList = smartwellDataMapper.getDeptIds(deviceTypesConfig.getTopDeptId());
        deptMapList.forEach(
                deptMap -> {
                    String deptId=String.valueOf(deptMap.get("id"));
                    if (ObjectUtils.isEmpty(deptId) || "04".equals(deptMap.get("type"))) {
                        return;
                    }
                    Map<String, Object> map = new HashMap<>();
                    map.put("deptName", deptMap.get("name"));
                    if (jobDeptMap.containsKey(String.valueOf(deptMap.get("id")))) {
                        map.putAll(jobDeptMap.get(String.valueOf(deptMap.get("id"))));
                    } else {
                        map.put("beforeGet", 0);
                        map.put("beforeConfirm", 0);
                        map.put("inHandle", 0);
                        map.put("over", 0);
                        map.put("cancel", 0);
                    }
                    mapList.add(map);
                }
        );
        return mapList;
    }

}