Newer
Older
zq-big-sreen / src / main / java / com / casic / service / impl / InstalledDeviceBuilder.java
chaizhuang on 19 Oct 2022 3 KB 没有改变
package com.casic.service.impl;

import com.casic.config.DeviceTypesConfig;
import com.casic.dao.cms.CmsDataMapper;
import com.casic.dao.smartwell.SmartwellDataMapper;
import com.casic.dao.spantilt.TiltDataMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

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

    public InstalledDeviceBuilder(SmartwellDataMapper smartwellDataMapper, CmsDataMapper cmsDataMapper,
                                  TiltDataMapper tiltDataMapper, DeviceTypesConfig deviceTypesConfig) {
        this.smartwellDataMapper = smartwellDataMapper;
        this.cmsDataMapper = cmsDataMapper;
        this.tiltDataMapper = tiltDataMapper;
        this.deviceTypesConfig = deviceTypesConfig;
    }

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

    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", tiltDataMapper.countColudDevice());
        installedDeviceMap.put("typeName", "云台");
        installedDeviceList.add(installedDeviceMap);
    }
}