Newer
Older
zq-big-sreen / src / main / java / com / casic / service / impl / DeviceBuilder.java
ChaiZhuang on 28 Oct 2022 5 KB 章丘大屏接口变更
package com.casic.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.casic.config.DeviceTypesConfig;
import com.casic.dao.cms.CmsDataMapper;
import com.casic.dao.smartwell.SmartwellDataMapper;
import com.casic.dao.spantilt.TiltDataMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 已安装的设备接口,来自于三个平台电子标识器、管网、云台
 */
@Service
@AllArgsConstructor
public class DeviceBuilder {

    private final DeviceTypesConfig deviceTypesConfig;
    private final SmartwellDataMapper smartwellDataMapper;
    private final CmsDataMapper cmsDataMapper;
    private final TiltDataMapper tiltDataMapper;


    public List<Map<String, Object>> InstalledDeviceManger() {
        String[] deviceTypeList = deviceTypesConfig.getDeviceTypes().split(",");
        List<Map<String, Object>> installedDeviceList = new ArrayList<>();
        cmsInstalledDevice(installedDeviceList);
        tiltInstalledDevice(installedDeviceList);
        smartwellInstalledDevice(installedDeviceList, deviceTypeList);
        return installedDeviceList;
    }

    public List<Map<String, String>> DeviceNumMangger() {
        List<Map<String, String>> deptMapList = smartwellDataMapper.getDeptIds(deviceTypesConfig.getTopDeptId());
        List<Map<String, String>> deviceList = smartwellDataMapper.getDeviceList();
        Map<String, String> typeNameMap = smartwellDeviceType();
        return this.getDeviceNumList(deviceList,deptMapList,typeNameMap);
    }

    private List<Map<String, String>> getDeviceNumList(List<Map<String, String>> deviceList,
                                                       List<Map<String, String>> deptMapList, Map<String, String> typeNameMap){
        List<Map<String, String>> deviceNumList = new ArrayList<>();
        Map<String, Map<String, String>> deptDeviceMap = deviceList.stream()
                .collect(Collectors.groupingBy(e -> String.valueOf(e.get("deptid")),
                        Collectors.toMap(e -> String.valueOf(e.get("deviceType")), e -> String.valueOf(e.get("count")))));
        List<String> deviceTypeList = Arrays.asList(deviceTypesConfig.getDeviceTypes().split(","));
        deptMapList.forEach(
                deptMap -> {
                    String deptId = String.valueOf(deptMap.get("id"));
                    if (ObjectUtils.isEmpty(deptId) || "04".equals(deptMap.get("type"))) {
                        return;
                    }
                    Map<String, String> map = new HashMap<>();
                    map.put("deptName", deptMap.get("name"));
                    if (deptDeviceMap.containsKey(deptId)) {
                        Map<String, String> deviceMap = deptDeviceMap.get(deptId);
                        deviceTypeList.forEach(
                                deviceType -> map.put(typeNameMap.get(deviceType), deviceMap.containsKey(deviceType) ? deviceMap.get(deviceType) : "0")
                        );
                    } else {
                        deviceTypeList.forEach(
                                deviceType -> map.put(typeNameMap.get(deviceType), "0")
                        );
                    }
                    deviceNumList.add(map);
                });
        return deviceNumList;
    }

    private void smartwellInstalledDevice(List<Map<String, Object>> installedDeviceList, String[] deviceTypeList) {
        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"))));
        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, Object> installedDeviceMap = new HashMap<>();
            installedDeviceMap.put("typeName", typeNameMap.get(deviceType));
            installedDeviceMap.put("count", deviceCountMap.containsKey(deviceType) ? deviceCountMap.get(deviceType) : "0");
            installedDeviceList.add(installedDeviceMap);
        }
    }

    private void cmsInstalledDevice(List<Map<String, Object>> installedDeviceList) {
        Map<String, Object> installedDeviceMap = new HashMap<>();
        installedDeviceMap.put("count", cmsDataMapper.countElectronicDevice());
        installedDeviceMap.put("typeName", "电子标识器");
        installedDeviceList.add(installedDeviceMap);
    }

    private void tiltInstalledDevice(List<Map<String, Object>> installedDeviceList) {
        Map<String, Object> installedDeviceMap = new HashMap<>();
        installedDeviceMap.put("count", deviceTypesConfig.getDeviceYT());
        installedDeviceMap.put("typeName", "云台");
        installedDeviceList.add(installedDeviceMap);
    }


    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;
    }
}