package com.fzzy.igds.service;
|
|
import com.fzzy.igds.constant.RedisConst;
|
import com.fzzy.igds.domain.DeviceIot;
|
import com.fzzy.igds.repository.DeviceIotRepository;
|
import com.fzzy.igds.utils.ContextUtil;
|
import com.ruoyi.common.core.redis.RedisCache;
|
import com.ruoyi.common.utils.StringUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.stereotype.Service;
|
import javax.annotation.Resource;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* @Description
|
* @Author CZT
|
* @Date 2025/11/28 14:45
|
*/
|
@Slf4j
|
@Service
|
public class DeviceIotService {
|
|
@Resource
|
private RedisCache redisCache;
|
@Resource
|
private DeviceIotRepository deviceIotRepository;
|
|
/**
|
* JPA分页查询数据
|
*
|
* @param specification
|
* @param pageable
|
* @return
|
*/
|
public Page<DeviceIot> findAll(Specification<DeviceIot> specification, Pageable pageable) {
|
return deviceIotRepository.findAll(specification, pageable);
|
}
|
|
/**
|
* JPA - 保存数据
|
* @param deviceIot
|
*/
|
public void updateDeviceIot(DeviceIot deviceIot) {
|
if (StringUtils.isEmpty(deviceIot.getCompanyId())) {
|
deviceIot.setCompanyId(ContextUtil.getCompanyId());
|
}
|
if (StringUtils.isEmpty(deviceIot.getDeptId())) {
|
deviceIot.setDeptId(ContextUtil.subDeptId(null));
|
}
|
if (StringUtils.isEmpty(deviceIot.getId())) {
|
deviceIot.setId(ContextUtil.generateId());
|
deviceIot.setCreateBy(ContextUtil.getLoginUserName());
|
deviceIot.setCreateTime(new Date());
|
}
|
deviceIot.setUpdateBy(ContextUtil.getLoginUserName());
|
deviceIot.setUpdateTime(new Date());
|
deviceIotRepository.save(deviceIot);
|
}
|
|
/**
|
* JPA - 删除数据
|
* @param deviceIot
|
*/
|
public void delDepotDeviceIot(DeviceIot deviceIot) {
|
deviceIotRepository.delete(deviceIot);
|
}
|
|
/**
|
* jpa更新设备位置
|
*
|
* @param deviceId
|
* @param posX
|
* @param posY
|
*/
|
public void updatePos(String deviceId, Double posX, Double posY) {
|
deviceIotRepository.updatePos(deviceId, posX, posY);
|
}
|
|
/**
|
* 刷新缓存
|
* @param companyId
|
*/
|
public void refreshCache(String companyId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
// 获取所有的设备信息
|
List<DeviceIot> listAll =deviceIotRepository.getDeviceIotByCompanyId(companyId);
|
|
if (null != listAll) {
|
this.setCacheAllDeviceIot(listAll, companyId);
|
}
|
}
|
|
/**
|
* 设置缓存
|
* @param listAll
|
* @param companyId
|
*/
|
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);
|
}
|
|
}
|
|
/**
|
* 更新缓存
|
* @param listBySer
|
* @param companyId
|
* @param serId
|
*/
|
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);
|
|
redisCache.setCacheObject(key, listBySer);
|
}
|
|
/**
|
* 获取缓存数据
|
* @param companyId
|
* @param serId
|
* @return
|
*/
|
public List<DeviceIot> getCacheDeviceIotBySerId(String companyId, String serId) {
|
String key = RedisConst.buildDeviceKey(companyId,
|
RedisConst.KEY_DEVICE_IOT_LIST, serId);
|
List<DeviceIot> list = (List<DeviceIot>) redisCache.getCacheObject(key);
|
if (null == list || list.isEmpty()) {
|
log.error("字典信息:没有获取到缓存信息,KEY={}", key);
|
return null;
|
}
|
return list;
|
}
|
|
/**
|
* 获取缓存数据
|
* @param companyId
|
* @param depotId
|
* @return
|
*/
|
public List<DeviceIot> getCacheDeviceIotByDepotId(String companyId, String depotId) {
|
|
if(org.apache.commons.lang3.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) + "*";
|
Collection<String> keys = redisCache.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>) redisCache.getCacheObject(key);
|
if(null == result || result.isEmpty()){
|
continue;
|
}
|
for (DeviceIot iot : result) {
|
if(depotId.equals(iot.getDepotId())){
|
list.add(iot);
|
}
|
}
|
}
|
return list;
|
}
|
|
}
|