YYC
2023-09-07 7243e0b4bd9a267a633d2e481e17646509b6868d
igds-core/src/main/java/com/ld/igds/common/impl/CoreDeviceServiceImpl.java
@@ -1,15 +1,16 @@
package com.ld.igds.common.impl;
import java.util.*;
import java.util.stream.Collectors;
import com.ld.igds.common.CoreDeviceService;
import com.ld.igds.common.dto.DepotSerData;
import com.ld.igds.common.mapper.DeviceMapper;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.DeviceStatus;
import com.ld.igds.constant.DeviceType;
import com.ld.igds.io.request.ExeDevice;
import com.ld.igds.models.Depot;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -32,37 +33,63 @@
    private DeviceMapper deviceMapper;
    @Override
    @SuppressWarnings({ "unchecked", "rawtypes" })
   @Override
    public void setCacheAllDevice(List<Device> listAll, String companyId) {
        // 首先按照分机分组,然后存更新缓存。
        Map<String, List<Device>> map = listAll.stream().collect(Collectors.groupingBy(Device::getSerId));
        if (null == map || map.isEmpty()) {
            log.error("字典信息:所有设备按照分机分组保存缓存失败,没有分组成功=={}", companyId);
            return;
        }
        if (null == listAll || listAll.isEmpty()) return;
        for (String serId : map.keySet()) {
            updateCacheDeviceBySerId(map.get(serId), companyId, serId);
        }
        //按照分机分类
        Map<String, List<Device>> mapSer = new HashMap<>();
        this.setCacheDepotDeviceSer(listAll, companyId);
    }
        //仓库-设备-分机对象分类
        Map<String, DepotSerData> mapDepot = new HashMap<>();
    @SuppressWarnings({"unchecked", "rawtypes"})
    private void setCacheDepotDeviceSer(List<Device> listAll, String companyId) {
        // 处理仓库与分机的关系缓存
        Map<String, DepotSerData> tempMap = new HashMap<>();
        String key;
        for (Device item : listAll) {
            key = companyId + "-" + item.getDepotId() + "-" + item.getSerId();
            if (tempMap.get(key) == null) {
                tempMap.put(key, new DepotSerData(companyId, item.getDepotId(), item.getSerId()));
        //把设备按照分机进行分组,同事吧设备添加到缓存列表中
        String deviceKey, depotDataKey;
        for (Device device : listAll) {
            //封装仓库-设备-分机对象关系
            depotDataKey = companyId + "-" + device.getDepotId() + "-" + device.getSerId();
            if (mapDepot.get(depotDataKey) == null) {
                mapDepot.put(depotDataKey, new DepotSerData(companyId, device.getDepotId(), device.getSerId()));
            }
            //单个设备进行缓存
            deviceKey = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE, device.getId());
            redisUtil.set(deviceKey, device);
            //按照分机分类
            if (null == mapSer.get(device.getSerId())) mapSer.put(device.getSerId(), new ArrayList<Device>());
            mapSer.get(device.getSerId()).add(device);
        }
        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(tempMap.values()));
        //以分机为单位缓存设备
        for (String serId : mapSer.keySet()) {
            updateCacheDeviceBySerId(mapSer.get(serId), companyId, serId);
        }
        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(mapDepot.values()));
    }
//    private void setCacheDepotDeviceSer(List<Device> listAll, String companyId) {
//        // 处理仓库与分机的关系缓存
//        Map<String, DepotSerData> tempMap = new HashMap<>();
//        String key;
//        for (Device item : listAll) {
//            key = companyId + "-" + item.getDepotId() + "-" + item.getSerId();
//            if (tempMap.get(key) == null) {
//                tempMap.put(key, new DepotSerData(companyId, item.getDepotId(), item.getSerId()));
//            }
//        }
//
//        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(tempMap.values()));
//    }
    @Override
    public void updateCacheDeviceBySerId(List<Device> listBySer, String companyId, String serId) {
@@ -85,6 +112,36 @@
            return null;
        }
        return list;
    }
    @SuppressWarnings("unchecked")
    @Override
    public Set<String> getCacheDeviceSerByDepotId(String companyId, String depotId) {
        String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_LIST);
        Set<String> keys = redisUtil.keys(patten);
        if (null == keys) return null;
        List<Device> list = new ArrayList<>();
        for (String key : keys) {
            list.addAll((List<Device>) redisUtil.get(key));
        }
        if (list.isEmpty()) {
            return null;
        }
        Set<String> result = new HashSet<>();
        for (Device device : list) {
            if(depotId.equals(device.getDepotId())){
                result.add(device.getSerId());
            }
        }
        return result;
    }
    @Override
    public Device getCacheDeviceById(String companyId, String id) {
        String key = RedisConst.buildDeviceKey(companyId, RedisConst.KEY_DEVICE, id);
        return (Device) redisUtil.get(key);
    }
    @Override
@@ -173,7 +230,7 @@
            }
            log.debug("解析设备状态--主通道={}---关联通道={}--解析后状态={}", curDevice.getPassCode(), curDevice.getLink(), curDevice.getStatus());
        }
        // 更新缓存
        this.updateCacheDeviceBySerId(listCache, companyId, serId);
    }
@@ -279,7 +336,7 @@
    }
    @Override
    public List<Device> getDeviceByTimId(String companyId,String timId){
        return deviceMapper.queryByTimId(companyId,timId);
    public List<Device> getDeviceByTimId(String companyId, String timId) {
        return deviceMapper.queryByTimId(companyId, timId);
    }
}