Commit 41f30511 authored by Administrator's avatar Administrator

2024-7.13 新增爆破振动设备的数据展示 以及redis keys 相关的命令替换

parent 86761849
......@@ -15,19 +15,24 @@
*/
package me.zhengjie.utils;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisClusterNode;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisClusterConnection;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
......@@ -184,7 +189,8 @@ public class RedisUtils {
} else {
Set<Object> keySet = new HashSet<>();
for (String key : keys) {
keySet.addAll(redisTemplate.keys(key));
Set<String> strings = this.scanMatch(key);
keySet.addAll(strings);
}
long count = redisTemplate.delete(keySet);
log.debug("--------------------------------------------");
......@@ -216,7 +222,7 @@ public class RedisUtils {
public List<Object> multiGet(List<String> keys) {
List list = redisTemplate.opsForValue().multiGet(Sets.newHashSet(keys));
List resultList = Lists.newArrayList();
Optional.ofNullable(list).ifPresent(e-> list.forEach(ele-> Optional.ofNullable(ele).ifPresent(resultList::add)));
Optional.ofNullable(list).ifPresent(e -> list.forEach(ele -> Optional.ofNullable(ele).ifPresent(resultList::add)));
return resultList;
}
......@@ -699,7 +705,7 @@ public class RedisUtils {
public void delByKeys(String prefix, Set<Long> ids) {
Set<Object> keys = new HashSet<>();
for (Long id : ids) {
keys.addAll(redisTemplate.keys(new StringBuffer(prefix).append(id).toString()));
keys.addAll(this.scanMatch(new StringBuffer(prefix).append(id).toString()));
}
long count = redisTemplate.delete(keys);
// 此处提示可自行删除
......@@ -708,4 +714,62 @@ public class RedisUtils {
log.debug("缓存删除数量:" + count + "个");
log.debug("--------------------------------------------");
}
private static final Integer SCAN_COUNT = 10000;
/**
* 使用scan遍历key
* 为什么不使用keys 因为Keys会引发Redis锁,并且增加Redis的CPU占用,特别是数据庞大的情况下。这个命令千万别在生产环境乱用。
* 支持redis单节点和集群调用
*
* @param matchKey
* @return
*/
public Set<String> scanMatch(String matchKey) {
Set<String> keys = new HashSet();
RedisConnectionFactory connectionFactory = redisTemplate.getConnectionFactory();
RedisConnection redisConnection = connectionFactory.getConnection();
Cursor<byte[]> scan = null;
//集群
if (redisConnection instanceof JedisClusterConnection) {
RedisClusterConnection clusterConnection = connectionFactory.getClusterConnection();
Iterable<RedisClusterNode> redisClusterNodes = clusterConnection.clusterGetNodes();
Iterator<RedisClusterNode> iterator = redisClusterNodes.iterator();
while (iterator.hasNext()) {
RedisClusterNode next = iterator.next();
scan = clusterConnection.scan(next, ScanOptions.scanOptions().match(matchKey).count(Integer.MAX_VALUE).build());
while (scan.hasNext()) {
keys.add(new String(scan.next()));
}
try {
if (scan != null) {
scan.close();
}
} catch (Exception e) {
log.error("scan遍历key关闭游标异常", e);
}
}
log.info(JSONObject.toJSONString(keys));
return keys;
}
//单机 (redisConnection instanceof JedisConnection)
else {
scan = redisConnection.scan(ScanOptions.scanOptions().match(matchKey + "*").count(SCAN_COUNT).build());
while (scan.hasNext()) {
//找到一次就添加一次
keys.add(new String(scan.next()));
}
try {
if (scan != null) {
scan.close();
}
} catch (Exception e) {
log.error("scan遍历key关闭游标异常", e);
}
return keys;
}
//return keys;
}
}
......@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.gemho.util.SpringUtil;
import me.zhengjie.utils.RedisUtils;
import org.apache.ibatis.cache.Cache;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
......@@ -30,6 +31,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
public class MybatisRedisCache implements Cache {
// 读写锁
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
......@@ -96,7 +98,8 @@ public class MybatisRedisCache implements Cache {
if (redisTemplate == null) {
redisTemplate = (RedisTemplate<String, Object>) SpringUtil.getBean("redisTemplate");
}
Set<String> keys = redisTemplate.keys("*:" + this.id + "*");
RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
Set<String> keys = redisUtils.scanMatch ("*:" + this.id + "*");
if (!CollectionUtils.isEmpty(keys)) {
redisTemplate.delete(keys);
}
......
package me.zhengjie.gemho.controller.data;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.gemho.entity.data.Zhidian;
import me.zhengjie.gemho.service.data.ZhidianService;
import me.zhengjie.gemho.util.PageResult;
import me.zhengjie.gemho.util.PostOrPutResult;
import me.zhengjie.gemho.x_datavo.data.DataQueryCriteria;
import me.zhengjie.gemho.x_datavo.data.ImgDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
/**
* <p>
* 前端控制器
* </p>
*
* @author llj
* @since 2024-07-13
*/
@Api(tags = "")
@RequiredArgsConstructor
@RestController
@RequestMapping("/data/zhidian")
public class ZhidianController {
@Autowired
private ZhidianService zhidianService;
@ApiOperation(value = "分页列表", response = Zhidian.class)
@GetMapping(value = "/page")
public ResponseEntity<Object> list(DataQueryCriteria dataQueryCriteria,HttpServletRequest request) {
HashMap <String, Object> data = zhidianService.plist(dataQueryCriteria,request);
return new ResponseEntity<>(new PageResult().success(data), HttpStatus.OK);
}
@ApiOperation(value = "图标历史")
@GetMapping("imghistory")
public ResponseEntity<Object> imgList(DataQueryCriteria dataQueryCriteria, HttpServletRequest request) {
ImgDataVo dbData = zhidianService.imgList(dataQueryCriteria, request);
return new ResponseEntity<>(new PageResult().success(dbData), HttpStatus.OK);
}
@ApiOperation(value = "新增")
@PostMapping()
public Object add(@Valid @RequestBody Zhidian param) {
boolean result = zhidianService.add(param);
if (result) {
return new ResponseEntity<>(new PostOrPutResult().success(), HttpStatus.OK);
} else {
return new ResponseEntity<>(new PostOrPutResult().failed(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@ApiOperation(value = "修改")
@PutMapping()
public Object modify(@Valid @RequestBody Zhidian param) {
boolean result = zhidianService.modify(param);
if (result) {
return new ResponseEntity<>(new PostOrPutResult().success(), HttpStatus.OK);
} else {
return new ResponseEntity<>(new PostOrPutResult().failed(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@ApiOperation(value = "删除(单个条目)")
@DeleteMapping()
public Object remove(@RequestBody HashMap
<String
, Object> map) {
Integer id = Integer.valueOf(map.get("id").toString());
boolean result = zhidianService.removeById(id);
if (result) {
return new ResponseEntity<>(new PostOrPutResult().success(), HttpStatus.OK);
} else {
return new ResponseEntity<>(new PostOrPutResult().failed(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
package me.zhengjie.gemho.entity.data;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
* <p>
*
* </p>
*
* @author llj
* @since 2024-07-13
*/
@Getter
@Setter
@TableName("tab_zhidian")
public class Zhidian implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
/**
* 设备id
*/
private String sensorid;
@TableField(exist = false)
private String sensorname;
private String jctime;
private String ifbj;
private String jcbjtime;
/**
* x方向速度
*/
public String speedx;
/**
* y方向速度
*/
public String speedy;
/**
* z方向速度
*/
public String speedz;
/**
* 合成速度
*/
public String speed;
/**
* x轴速度
*/
public String addspeedx;
/**
* y轴速度
*/
public String addspeedy;
/**
* z轴速度
*/
public String addspeedz;
/**
* 加速度
*/
public String addspeed;
/**
* x轴频率
*/
public String pinlvx;
/**
* y轴频率
*/
public String pinlvy;
/**
* z轴频率
*/
public String pinlvz;
public String state;
}
package me.zhengjie.gemho.mapper.data;
import me.zhengjie.gemho.entity.data.Zhidian;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author llj
* @since 2024-07-13
*/
@Mapper
public interface ZhidianMapper extends BaseMapper<Zhidian> {
}
package me.zhengjie.gemho.service.data;
import me.zhengjie.gemho.entity.data.Zhidian;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.HashMap;
import me.zhengjie.gemho.x_datavo.data.DataQueryCriteria;
import com.baomidou.mybatisplus.core.metadata.IPage;
import me.zhengjie.gemho.x_datavo.data.ImgDataVo;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author llj
* @since 2024-07-13
*/
public interface ZhidianService extends IService<Zhidian> {
/**
* 分页列表
* @param dataQueryCriteria
* @return
*/
HashMap
<String, Object> plist(DataQueryCriteria dataQueryCriteria,HttpServletRequest request);
/**
* 图表数据
*
* @param dataQueryCriteria
* @param request
* @return
*/
ImgDataVo imgList(DataQueryCriteria dataQueryCriteria, HttpServletRequest request);
/**
* 新增
* @param param 根据需要进行传值
* @return
*/
boolean add(Zhidian param);
/**
* 修改
* @param param 根据需要进行传值
* @return
*/
boolean modify(Zhidian param);
}
package me.zhengjie.gemho.service.data.impl;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.gemho.entity.data.WtData;
import me.zhengjie.gemho.entity.data.Zhidian;
import me.zhengjie.gemho.entity.tab.Drybeachequipinfor;
import me.zhengjie.gemho.mapper.data.ZhidianMapper;
import me.zhengjie.gemho.mapper.tab.DrybeachequipinforMapper;
import me.zhengjie.gemho.service.data.ZhidianService;
import me.zhengjie.gemho.util.ServiceUtil;
import me.zhengjie.gemho.util.TailNoForInfoUtil;
import me.zhengjie.gemho.x_datavo.data.DataQueryCriteria;
import me.zhengjie.gemho.x_datavo.data.ImgDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
/**
* <p>
* 服务实现类
* </p>
*
* @author llj
* @since 2024-07-13
*/
@Service
public class ZhidianServiceImpl extends ServiceImpl<ZhidianMapper, Zhidian> implements ZhidianService {
@Autowired
private ZhidianMapper zhidianMapper;
@Autowired
private DrybeachequipinforMapper drybeachequipinforMapper;
/**
* 分页列表
*
* @return
*/
@Override
public HashMap<String, Object> plist(DataQueryCriteria dataQueryCriteria,HttpServletRequest request) {
long size = dataQueryCriteria.getSize();
long page = dataQueryCriteria.getPage();
Page<Zhidian> ZhidianPage = new Page<>(page + 1, size);
QueryWrapper<Zhidian> queryWrapper = new QueryWrapper<>();
ServiceUtil.dataQueryJcTime(queryWrapper, dataQueryCriteria);
ZhidianPage = zhidianMapper.selectPage(ZhidianPage, queryWrapper);
List<Zhidian> records = ZhidianPage.getRecords();
//获取编码-名字map
HashMap<String, String> noName = ServiceUtil.getNoName(request);
records.forEach(item-> {
String sensorid = item.getSensorid();
item.setSensorname(noName.get(sensorid));
});
long total = ZhidianPage.getTotal();
HashMap<String, Object> map = new HashMap<>();
map.put("list", records);
map.put("total", total);
return map;
}
@Override
public ImgDataVo imgList(DataQueryCriteria dataQueryCriteria, HttpServletRequest request) {
String code = dataQueryCriteria.getCode();
if (code == null) {
//获取当前尾矿库
String tailInfoNo = TailNoForInfoUtil.getTailInfoNo(request);
if (tailInfoNo == null) {
return null;
}
QueryWrapper<Drybeachequipinfor> drybeachequipinforQueryWrapper = new QueryWrapper<>();
drybeachequipinforQueryWrapper.eq("devicetype", "19").eq("tailingid", tailInfoNo).orderByDesc("id");
Drybeachequipinfor drybeachequipinfor = drybeachequipinforMapper.selectList(drybeachequipinforQueryWrapper).get(0);
dataQueryCriteria.setCode(drybeachequipinfor.getEquipno());
}
QueryWrapper<Zhidian> wtDataQueryWrapper = new QueryWrapper<>();
ServiceUtil.imgQueryJcTime(wtDataQueryWrapper, dataQueryCriteria);
List<Zhidian> wtData = zhidianMapper.selectList(wtDataQueryWrapper);
//获取编码-名
HashMap<String, String> noName = ServiceUtil.getNoName(request);
HashMap<String, String> noDan = ServiceUtil.getNoDan(request);
String danwei = noDan.get(dataQueryCriteria.getCode());
String sensorname = noName.get(dataQueryCriteria.getCode());
//处理封装数据
ImgDataVo mpdata = ServiceUtil.deal("zhidian", sensorname, wtData, danwei,dataQueryCriteria.getCode());
return mpdata;
}
/**
* 新增
*
* @param param 根据需要进行传值
* @return
*/
@Override
public boolean add(Zhidian param) {
int result = zhidianMapper.insert(param);
if (result > 0) {
return true;
}
return false;
}
/**
* 修改
*
* @param param 根据需要进行传值
* @return
*/
@Override
public boolean modify(Zhidian param) {
QueryWrapper<Zhidian> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", param.getId());
int result = zhidianMapper.update(param, queryWrapper);
if (result > 0) {
return true;
}
return false;
}
}
......@@ -22,7 +22,7 @@ import java.util.Map;
public class ReadJsonFileUtil {
public static HashMap<String, List<Map>> getMap(String path) {
String externalFilePath = System.getProperty("user.dir") + "\\jsons\\" + path + ".json"; // 外部文件路径
String externalFilePath = System.getProperty("user.dir") + "\\json\\" + path + ".json"; // 外部文件路径
String resourceName = "\\json\\" + path + ".json"; // 资源文件名
String json = readJsonFile(externalFilePath, resourceName);
......
......@@ -270,6 +270,71 @@ public class ServiceUtil {
}
public static void dataQueryJcTime(QueryWrapper<?> queryWrapper, DataQueryCriteria dataQueryCriteria) {
//获取精确查询字段
String exact1 = dataQueryCriteria.getExact();
if (exact1 != null) {
HashMap exact = JSON.parseObject(exact1, HashMap.class);
//HashMap<String, Object> exact = dataQueryCriteria.getExact();
//处理精确查询字段
Set<String> strings = exact.keySet();
if (!strings.isEmpty()) {
for (String string : strings) {
if (string.equals("state")) {
exact.put("state", 1);
}
if (string.equals("monitorType")) {
continue;
}
queryWrapper.eq(string, exact.get(string));
}
}
}
//获取模糊查询字段
String vague1 = dataQueryCriteria.getVague();
if (vague1 != null) {
Map vague = JSON.parseObject(vague1, Map.class);
Set<String> strings1 = vague.keySet();
if (!strings1.isEmpty()) {
for (String s : strings1) {
queryWrapper.like(s, vague.get(s));
}
}
}
//判断是否有时间字段
String timeSpace = dataQueryCriteria.getDaterange();
if (timeSpace != null) {
if (timeSpace.contains(",")) {
String starttime = timeSpace.split(",")[0] + " 00:00:00";
String endtime = timeSpace.split(",")[1] + " 23:59:59";
Date start = toDate(starttime);
Date end = toDate(endtime);
queryWrapper.ge("jctime", start).le("jctime", end);
} else {
Date start = toDate(timeSpace + " 00:00:00");
Date end = toDate(timeSpace + " 23:59:59");
queryWrapper.ge("jctime", start).le("jctime", end);
}
}
//正序倒序
String sort = dataQueryCriteria.getSort();
if (sort != null) {
String s = sort.split(",")[1];
if (s.equals("desc")) {
queryWrapper.orderByDesc("jctime");
} else {
queryWrapper.orderByAsc("jctime");
}
} else {
queryWrapper.orderByAsc("jctime");
}
//获取当前尾矿库所有设备编码
List<String> dryNo = TailNoForInfoUtil.getDryNo();
if (dryNo != null) {
queryWrapper.in("sensorid", dryNo);
}
}
public static void jrxStepQuery(QueryWrapper<?> queryWrapper, DataQueryCriteria dataQueryCriteria) {
//获取精确查询字段
String exact1 = dataQueryCriteria.getExact();
......@@ -424,6 +489,38 @@ public class ServiceUtil {
queryWrapper.orderByAsc("time");
}
}
public static void imgQueryJcTime(QueryWrapper<?> queryWrapper, DataQueryCriteria dataQueryCriteria) {
String code = dataQueryCriteria.getCode();
if (code != null) {
queryWrapper.eq("sensorid", code);
}
//判断是否有时间字段
String timeSpace = dataQueryCriteria.getDaterange();
if (timeSpace != null) {
if (timeSpace.contains(",")) {
String starttime = timeSpace.split(",")[0] + " 00:00:00";
String endtime = timeSpace.split(",")[1] + " 23:59:59";
Date start = toDate(starttime);
Date end = toDate(endtime);
queryWrapper.ge("jctime", start).le("jctime", end);
} else {
Date start = toDate(timeSpace + " 00:00:00");
Date end = toDate(timeSpace + " 23:59:59");
queryWrapper.ge("jctime", start).le("jctime", end);
}
} else {
queryWrapper.apply("DATE_FORMAT(jctime,'%Y-%m-%d') =DATE_FORMAT(NOW(),'%Y-%m-%d')");
}
//正序倒序
String sort = dataQueryCriteria.getSort();
String s = sort.split(",")[1];
if (s.equals("desc")) {
queryWrapper.orderByDesc("jctime");
} else {
queryWrapper.orderByAsc("jctime");
}
}
public static void rgImgQuery(QueryWrapper<?> queryWrapper, DataQueryCriteria dataQueryCriteria) {
......
......@@ -40,6 +40,16 @@ public class TailNoForInfoUtil {
List<String> list = drybeachequipinforMapper.getequipno(tailno);
return list;
}
public static List<String> getDryNo() {
String tailno = getTailInfoNo();
if (tailno == null) {
return null;
}
//获取当前尾矿库所有的设备编码
DrybeachequipinforMapper drybeachequipinforMapper = SpringContextHolder.getBean(DrybeachequipinforMapper.class);
List<String> list = drybeachequipinforMapper.getequipno(tailno);
return list;
}
//获取设备名称
public static Map getEquipName(HttpServletRequest request) {
......
......@@ -22,15 +22,14 @@ import java.util.List;
public class MyGenerator {
public static void main(String[] args) {
List<String> tables = new ArrayList<>();
tables.add("tab_jrx_dissect");
tables.add("tab_jrx_water");
tables.add("tab_zhidian");
tables.size();
FastAutoGenerator.create(
//数据源配置,url需要修改
new DataSourceConfig.Builder("jdbc:mysql://8.142.46.126:3306/mdm_1_db?serverTimezone=Asia/Shanghai",
"root",
"jinghe2021//")
new DataSourceConfig.Builder("jdbc:mysql://localhost:3310/zhonghe?serverTimezone=Asia/Shanghai",
"gemho",
"jinghe2023")
.dbQuery(new MySqlQuery())
//.schema("postgres")
.typeConvert(new MySqlTypeConvert())
......@@ -51,12 +50,12 @@ public class MyGenerator {
.packageConfig(builder -> {
builder.parent("me.zhengjie.gemho") // 设置父包名,根据实制项目路径修改
//.moduleName("sys")
.entity("entity.tab")
.service("service.tab")
.serviceImpl("service.tab.impl")
.mapper("mapper.tab")
.entity("entity.data")
.service("service.data")
.serviceImpl("service.data.impl")
.mapper("mapper.data")
//.xml("mapper.xml")
.controller("controller.tab")
.controller("controller.data")
//.other("other")
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "/src/main/resources/mapper"));
})
......@@ -64,7 +63,7 @@ public class MyGenerator {
//策略配置
.strategyConfig(builder -> {
builder.addInclude(tables) // 设置需要生成的表名 可以为集合 一次生成多个
.addTablePrefix("")//过滤表名 如表名为tab_user 实体类名为 user
.addTablePrefix("tab")//过滤表名 如表名为tab_user 实体类名为 user
.serviceBuilder()
.formatServiceFileName("%sService")//service 名称
.formatServiceImplFileName("%sServiceImpl") // serviceImpl 名称
......
......@@ -269,7 +269,6 @@ public class OnlineUserService {
*/
public String gettailno(String username, HttpServletRequest request) {
TokenProvider tokenProvider = SpringContextHolder.getBean(TokenProvider.class);
String token = tokenProvider.getToken(request);
String tailno = (String) redisUtils.get("wkk" + username);
return tailno;
}
......
......@@ -37,4 +37,5 @@ public class AuthUserDto {
private String code;
private String uuid = "";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment