package com.ld.igds.common.impl;
|
|
import com.ld.igds.common.CoreDeviceIotService;
|
import com.ld.igds.common.dto.DepotSerData;
|
import com.ld.igds.common.mapper.DeviceIotMapper;
|
import com.ld.igds.constant.RedisConst;
|
import com.ld.igds.models.DeviceIot;
|
import com.ld.igds.util.ContextUtil;
|
import com.ld.igds.util.RedisUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
@Slf4j
|
@Component
|
public class CoreDeviceIotServiceImpl implements CoreDeviceIotService {
|
|
@Autowired
|
private RedisUtil redisUtil;
|
@Autowired
|
private DeviceIotMapper deviceMapper;
|
|
|
@Override
|
public void setCacheAllDeviceIot(List<DeviceIot> listAll, String companyId) {
|
// 首先按照分机分组,然后存更新缓存。
|
Map<String, List<DeviceIot>> map = listAll.stream().collect(
|
Collectors.groupingBy(DeviceIot::getSerId));
|
|
if (null == map || map.isEmpty()) {
|
log.error("字典信息:所有设备按照分机分组保存缓存失败,没有分组成功=={}", companyId);
|
return;
|
}
|
|
for (String serId : map.keySet()) {
|
updateCacheDeviceIotBySerId(map.get(serId), companyId, serId);
|
}
|
|
this.setCacheDepotDeviceIotSer(listAll, companyId);
|
}
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
private void setCacheDepotDeviceIotSer(List<DeviceIot> listAll, String companyId) {
|
// 处理仓库与分机的关系缓存
|
Map<String, DepotSerData> tempMap = new HashMap<>();
|
String key;
|
for (DeviceIot 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 updateCacheDeviceIotBySerId(List<DeviceIot> listBySer,
|
String companyId, String serId) {
|
|
String key = RedisConst.buildDeviceKey(companyId,
|
RedisConst.KEY_DEVICE_IOT_LIST, serId);
|
|
log.debug("分机-设备-KEY={}", key);
|
|
redisUtil.set(key, listBySer);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public List<DeviceIot> getCacheDeviceIotBySerId(String companyId, String serId) {
|
String key = RedisConst.buildDeviceKey(companyId,
|
RedisConst.KEY_DEVICE_IOT_LIST, serId);
|
List<DeviceIot> list = (List<DeviceIot>) redisUtil.get(key);
|
if (null == list || list.isEmpty()) {
|
log.error("字典信息:没有获取到缓存信息,KEY={}", key);
|
return null;
|
}
|
return list;
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public List<DeviceIot> getCacheDeviceIotByDepotId(String companyId, String depotId) {
|
|
if(StringUtils.isEmpty(depotId)){
|
log.error("仓库编码为空,获取Iot设备失败,depotId={}", depotId);
|
return null;
|
}
|
if(StringUtils.isEmpty(companyId)){
|
companyId = ContextUtil.getCompanyId();
|
}
|
|
String pattern = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_IOT_LIST);
|
Set<String> keys = redisUtil.keys(pattern);
|
if (null == keys) {
|
log.error("没有获取到Iot缓存key信息");
|
return null;
|
}
|
|
List<DeviceIot> list = new ArrayList<>();
|
List<DeviceIot> result;
|
|
for (String key : keys) {
|
result = (List<DeviceIot>) redisUtil.get(key);
|
if(null == result || result.isEmpty()){
|
continue;
|
}
|
for (DeviceIot iot : result) {
|
if(depotId.equals(iot.getDepotId())){
|
list.add(iot);
|
}
|
}
|
}
|
|
return list;
|
}
|
|
@Override
|
public void refreshCache(String companyId) {
|
// 获取所有的设备信息
|
List<DeviceIot> listAll = deviceMapper.queryAll(companyId);
|
if (null != listAll) {
|
this.setCacheAllDeviceIot(listAll, companyId);
|
}
|
}
|
|
|
}
|