CZT
2023-09-05 8cfc3732f3fe53d8794da15b80d554ad8b2c794c
igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java
@@ -15,11 +15,9 @@
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
@@ -50,13 +48,26 @@
        for (DeviceModbus hibDevice : listAll) {
            entityDevice = new DeviceModbus();
            BeanUtils.copyProperties(hibDevice, entityDevice);
            key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode());
            if (null == hibDevice.getBizType()) hibDevice.setBizType(BizType.AREATION.getCode());
            if (BizType.AREATION.getCode().equals(hibDevice.getBizType())) {
                key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode());
            } else {
                key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode(), hibDevice.getBizType());
            }
            redisUtil.set(key, entityDevice);
        }
    }
    public DeviceModbus getCacheDeviceModbus(String companyId, String deviceCode) {
        return getCacheDeviceModbus(companyId, deviceCode, null);
    }
    public DeviceModbus getCacheDeviceModbus(String companyId, String deviceCode, String bizType) {
        String key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, deviceCode);
        if (null != bizType && !BizType.AREATION.equals(bizType)) {
            key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, deviceCode, bizType);
        }
        return (DeviceModbus) redisUtil.get(key);
    }
@@ -129,6 +140,23 @@
        return null;
    }
    /**
     * 根据类型获取Modbus能耗设备配置信息
     * @param bizType
     * @param serId
     * @return
     */
    public List<DeviceModbus> getDataByBizType(String bizType, String serId) {
        String hql = " from " + DeviceModbus.class.getName() + " where bizType=:bizType and serId =:serId";
        Map<String, Object> param = new HashMap<>();
        param.put("bizType", bizType);
        param.put("serId", serId);
        return this.query(hql, param);
    }
    public DeviceModbus getGasData(String companyId, String depotId, String bizType) {
@@ -195,32 +223,6 @@
                session.update(gas);
            }
        }
        flushCacheGas(null);
    }
    public void flushCacheGas(String companyId) {
        List<GasModbus> listAll = this.listGasModBus(null);
        if (null == listAll || listAll.isEmpty()) return;
        if (null == companyId) companyId = ContextUtil.getDefaultCompanyId();
        Map<String, List<GasModbus>> collect = listAll.stream().collect(Collectors.groupingBy(GasModbus::getDepotId));
        String key;
        for (String depotId : collect.keySet()) {
            key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_GAS_CACHE_LIST, depotId);
            redisUtil.set(key, key);
        }
    }
    public List<GasModbus> getCacheGasModbus(String companyId, String depotId) {
        if (null == companyId) companyId = ContextUtil.getDefaultCompanyId();
        String key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_GAS_CACHE_LIST, depotId);
        return (List<GasModbus>) redisUtil.get(key);
    }
@@ -228,8 +230,6 @@
        Session session = this.getSessionFactory().openSession();
        try {
            session.delete(data);
            flushCacheGas(null);
        } catch (Exception e) {
        } finally {
            session.flush();