package com.fzzy.igds.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.fzzy.igds.constant.RedisConst;
|
import com.fzzy.igds.data.DeviceIotParam;
|
import com.fzzy.igds.domain.DeviceIot;
|
import com.fzzy.igds.mapper.DeviceIotMapper;
|
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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
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 DeviceIotMapper deviceIotMapper;
|
|
/**
|
* 分页查询数据
|
* @param page
|
* @param param
|
*/
|
public void listPageData(Page<DeviceIot> page, DeviceIotParam param) {
|
QueryWrapper<DeviceIot> queryWrapper = new QueryWrapper<>();
|
|
param.setCompanyId(ContextUtil.getCompanyId());
|
param.setDeptId(ContextUtil.subDeptId(null));
|
queryWrapper.eq("company_id", param.getCompanyId());
|
queryWrapper.eq("dept_id", param.getDeptId());
|
|
if (StringUtils.isNotBlank(param.getDepotId())) {
|
queryWrapper.eq("depot_id", param.getDepotId());
|
}
|
if (StringUtils.isNotBlank(param.getType())) {
|
queryWrapper.eq("type", param.getType());
|
}
|
if (StringUtils.isNotBlank(param.getSerId())) {
|
queryWrapper.eq("ser_id", param.getSerId());
|
}
|
|
deviceIotMapper.selectPage(page, queryWrapper);
|
}
|
|
/**
|
* 根据条件查询
|
* @param companyId
|
*/
|
public List<DeviceIot> listData(String companyId) {
|
QueryWrapper<DeviceIot> queryWrapper = new QueryWrapper<>();
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
queryWrapper.eq("company_id", companyId);
|
|
return deviceIotMapper.selectList(queryWrapper);
|
}
|
|
/**
|
* 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());
|
deviceIotMapper.insert(deviceIot);
|
}else {
|
deviceIot.setUpdateBy(ContextUtil.getLoginUserName());
|
deviceIot.setUpdateTime(new Date());
|
deviceIotMapper.updateById(deviceIot);
|
}
|
}
|
|
/**
|
* JPA - 删除数据
|
* @param deviceIot
|
*/
|
public void delDepotDeviceIot(DeviceIot deviceIot) {
|
deviceIotMapper.deleteById(deviceIot);
|
}
|
|
/**
|
* jpa更新设备位置
|
*
|
* @param deviceId
|
* @param posX
|
* @param posY
|
*/
|
public void updatePos(String deviceId, Double posX, Double posY) {
|
UpdateWrapper<DeviceIot> updateWrapper = new UpdateWrapper<>();
|
updateWrapper.eq("id", deviceId).set("pos_x", posX).set("pos_y", posY);
|
deviceIotMapper.update(null, updateWrapper);
|
}
|
|
/**
|
* 刷新缓存
|
* @param companyId
|
*/
|
public void refreshCache(String companyId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
// 获取所有的设备信息
|
List<DeviceIot> listAll =this.listData(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;
|
}
|
|
}
|