From 81a6a662b5db806e0824f311a89993d853db9371 Mon Sep 17 00:00:00 2001 From: huangzhen <867127663@qq.com> Date: 星期四, 13 十月 2022 16:15:46 +0800 Subject: [PATCH] 新增前端获取危险单元名称和编码、加入redis缓存 --- src/main/java/com/ruoyi/doublePrevention/service/impl/RiskServiceImpl.java | 3 src/main/java/com/ruoyi/doublePrevention/config/redis/RedisUtils.java | 531 ++++++++++++++++++++-------------------- src/main/java/com/ruoyi/doublePrevention/service/impl/SPIDataCountServiceImpl.java | 70 ++++ src/main/java/com/ruoyi/doublePrevention/enums/CountContentEnum.java | 33 ++ pom.xml | 13 src/main/java/com/ruoyi/doublePrevention/controller/PreventRiskUnitController.java | 33 ++ src/main/java/com/ruoyi/doublePrevention/config/redis/RedisConfig.java | 84 +++--- 7 files changed, 454 insertions(+), 313 deletions(-) diff --git a/pom.xml b/pom.xml index 4da3b93..519dd04 100644 --- a/pom.xml +++ b/pom.xml @@ -324,6 +324,18 @@ <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency> + <!--redis--> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-redis</artifactId> + <version>2.3.12.RELEASE</version> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-pool2</artifactId> + </dependency> + + <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> @@ -335,7 +347,6 @@ <artifactId>mybatis-plus-extension</artifactId> <version>3.5.2</version> </dependency> - </dependencies> diff --git a/src/main/java/com/ruoyi/doublePrevention/config/redis/RedisConfig.java b/src/main/java/com/ruoyi/doublePrevention/config/redis/RedisConfig.java index cb916f8..d64871f 100644 --- a/src/main/java/com/ruoyi/doublePrevention/config/redis/RedisConfig.java +++ b/src/main/java/com/ruoyi/doublePrevention/config/redis/RedisConfig.java @@ -1,43 +1,41 @@ -//package com.ruoyi.doublePrevention.config.redis; -// -//import org.springframework.context.annotation.Bean; -//import org.springframework.context.annotation.Configuration; -//import org.springframework.data.redis.connection.RedisConnectionFactory; -//import org.springframework.data.redis.core.RedisTemplate; -//import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; -//import org.springframework.data.redis.serializer.StringRedisSerializer; -// -//@Configuration -//public class RedisConfig { -// -// -// -// /** -// * @Description: key和value的序列化方式 -// */ -// @Bean -// public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { -// -// RedisTemplate<String, Object> template = new RedisTemplate<>(); -// template.setConnectionFactory(factory); -// // json序列化对象 -// GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); -// StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); -// // key=>string -// template.setKeySerializer(stringRedisSerializer); -// // hash=>string -// template.setHashKeySerializer(stringRedisSerializer); -// // value=>json -// template.setValueSerializer(jackson2JsonRedisSerializer); -// // hashValue=>json -// template.setHashValueSerializer(jackson2JsonRedisSerializer); -// // set -// template.afterPropertiesSet(); -// return template; -// } -// -// -// -// -// -//} +package com.ruoyi.doublePrevention.config.redis; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@Configuration +public class RedisConfig { + + /** + * @Description: key和value的序列化方式 + */ + @Bean + public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { + + RedisTemplate<String, Object> template = new RedisTemplate<>(); + template.setConnectionFactory(factory); + // json序列化对象 + GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); + // key=>string + template.setKeySerializer(stringRedisSerializer); + // hash=>string + template.setHashKeySerializer(stringRedisSerializer); + // value=>json + template.setValueSerializer(jackson2JsonRedisSerializer); + // hashValue=>json + template.setHashValueSerializer(jackson2JsonRedisSerializer); + // set + template.afterPropertiesSet(); + return template; + } + + + + + +} diff --git a/src/main/java/com/ruoyi/doublePrevention/config/redis/RedisUtils.java b/src/main/java/com/ruoyi/doublePrevention/config/redis/RedisUtils.java index 2ea8c61..a97e3f0 100644 --- a/src/main/java/com/ruoyi/doublePrevention/config/redis/RedisUtils.java +++ b/src/main/java/com/ruoyi/doublePrevention/config/redis/RedisUtils.java @@ -1,261 +1,270 @@ -//package com.ruoyi.doublePrevention.config.redis; -// -// -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -//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.connection.jedis.JedisConnection; -//import org.springframework.data.redis.core.*; -//import org.springframework.stereotype.Repository; -// -//import javax.annotation.PostConstruct; -//import javax.annotation.Resource; -//import java.io.Serializable; -//import java.util.HashSet; -//import java.util.Iterator; -//import java.util.List; -//import java.util.Set; -//import java.util.concurrent.TimeUnit; -// -///** -//* @Description: redis工具类 -//*/ -// -//@Repository("configRedisRepository") -//@SuppressWarnings(value = { "unchecked", "rawtypes" }) -//public class RedisUtils { -// -// -// @Resource -// private RedisTemplate redisTemplate; -// /** -// * logger -// */ -// private final Logger logger = LoggerFactory.getLogger(this.getClass()); -// -// -// @PostConstruct -// public void initRepository(){ -// try { -// this.set("test:module:Web", "testConnection", 60L, TimeUnit.SECONDS); -// logger.info("[ModuleRedis][Web] is connected"); -// } catch (Exception e) { -// logger.error("[ModuleRedis][Web] connected failed!!"); -// } -// } -// -// /** -// * 写入缓存 -// * @param key -// * @param value -// * @return -// */ -// public boolean set(final String key, Object value) { -// boolean result = false; -// try { -// ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); -// operations.set(key, value); -// result = true; -// } catch (Exception e) { -// e.printStackTrace(); -// } -// return result; -// } -// /** -// * 写入缓存设置时效时间 -// * @param key -// * @param value -// * @return -// */ -// public boolean set(final String key, Object value, Long expireTime ,TimeUnit timeUnit) { -// boolean result = false; -// try { -// ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); -// operations.set(key, value); -// redisTemplate.expire(key, expireTime, timeUnit); -// result = true; -// } catch (Exception e) { -// e.printStackTrace(); -// } -// return result; -// } -// /** -// * 批量删除对应的value -// * @param keys -// */ -// public void remove(final String... keys) { -// for (String key : keys) { -// remove(key); -// } -// } -// /** -// * 批量删除key -// * @param pattern -// */ -// public void removePattern(final String pattern) { -// Set<Serializable> keys = redisTemplate.keys(pattern); -// if (keys.size() > 0){ -// redisTemplate.delete(keys); -// } -// } -// /** -// * 删除对应的value -// * @param key -// */ -// public void remove(final String key) { -// if (exists(key)) { -// redisTemplate.delete(key); -// } -// } -// /** -// * 判断缓存中是否有对应的value -// * @param key -// * @return -// */ -// public boolean exists(final String key) { -// return redisTemplate.hasKey(key); -// } -// /** -// * 读取缓存 -// * @param key -// * @return -// */ -// public Object get(final String key) { -// Object result = null; -// ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); -// result = operations.get(key); -// return result; -// } -// /** -// * 哈希 添加 -// * @param key -// * @param hashKey -// * @param value -// */ -// public void hmSet(String key, Object hashKey, Object value){ -// HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); -// hash.put(key,hashKey,value); -// } -// /** -// * 哈希获取数据 -// * @param key -// * @param hashKey -// * @return -// */ -// public Object hmGet(String key, Object hashKey){ -// HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); -// return hash.get(key,hashKey); -// } -// /** -// * 列表添加 -// * @param k -// * @param v -// */ -// public void lPush(String k,Object v){ -// ListOperations<String, Object> list = redisTemplate.opsForList(); -// list.rightPush(k,v); -// } -// /** -// * 列表获取 -// * @param k -// * @param l -// * @param l1 -// * @return -// */ -// public List<Object> lRange(String k, long l, long l1){ -// ListOperations<String, Object> list = redisTemplate.opsForList(); -// return list.range(k,l,l1); -// } -// /** -// * 集合添加 -// * @param key -// * @param value -// */ -// public void add(String key,Object value){ -// SetOperations<String, Object> set = redisTemplate.opsForSet(); -// set.add(key,value); -// } -// /** -// * 集合获取 -// * @param key -// * @return -// */ -// public Set<Object> setMembers(String key){ -// SetOperations<String, Object> set = redisTemplate.opsForSet(); -// return set.members(key); -// } -// /** -// * 有序集合添加 -// * @param key -// * @param value -// * @param scoure -// */ -// public void zAdd(String key,Object value,double scoure){ -// ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); -// zset.add(key,value,scoure); -// } -// /** -// * 有序集合获取 -// * @param key -// * @param scoure -// * @param scoure1 -// * @return -// */ -// public Set<Object> rangeByScore(String key,double scoure,double scoure1){ -// ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); -// return zset.rangeByScore(key, scoure, scoure1); -// } -// -// /** -// * @Description: 获取过期时间 返回 秒 -// */ -// -// public Long getExpireTime(String key) { -// return redisTemplate.getExpire(key, TimeUnit.SECONDS); -// } -// -// -// /** -// * @Description: 重置key 的 过期时间 -// */ -// public void resetKeyExpireTime(String key, Long seconds) { -// redisTemplate.expire(key, seconds, TimeUnit.SECONDS); -// } -// -// 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())); -// } -// scan.close(); -// } -// return keys; -// } -// if(redisConnection instanceof JedisConnection){ -// scan = redisConnection.scan(ScanOptions.scanOptions().match(matchKey).count(Integer.MAX_VALUE).build()); -// while (scan.hasNext()){ -// //找到一次就添加一次 -// keys.add(new String(scan.next())); -// } -// scan.close(); -// return keys; -// } -// -// return keys; -//} -// -//} +package com.ruoyi.doublePrevention.config.redis; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.connection.jedis.JedisConnection; +import org.springframework.data.redis.core.*; +import org.springframework.stereotype.Repository; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +/** +* @Description: redis工具类 +*/ + +@Repository("configRedisRepository") +@SuppressWarnings(value = { "unchecked", "rawtypes" }) +public class RedisUtils { + + + @Resource + private RedisTemplate redisTemplate; + /** + * logger + */ + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + + @PostConstruct + public void initRepository(){ + try { + this.set("test:module:Web", "testConnection", 60L, TimeUnit.SECONDS); + logger.info("[ModuleRedis][Web] is connected"); + } catch (Exception e) { + logger.error("[ModuleRedis][Web] connected failed!!"); + } + } + + /** + * 写入缓存 + * @param key + * @param value + * @return + */ + public boolean set(final String key, Object value) { + boolean result = false; + try { + ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); + operations.set(key, value); + result = true; + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + /** + * 写入缓存设置时效时间 + * @param key + * @param value + * @return + */ + public boolean set(final String key, Object value, Long expireTime ,TimeUnit timeUnit) { + boolean result = false; + try { + ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); + operations.set(key, value); + redisTemplate.expire(key, expireTime, timeUnit); + result = true; + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + /** + * 批量删除对应的value + * @param keys + */ + public void remove(final String... keys) { + for (String key : keys) { + remove(key); + } + } + /** + * 批量删除key + * @param pattern + */ + public void removePattern(final String pattern) { + Set<Serializable> keys = redisTemplate.keys(pattern); + if (keys.size() > 0){ + redisTemplate.delete(keys); + } + } + /** + * 删除对应的value + * @param key + */ + public void remove(final String key) { + if (exists(key)) { + redisTemplate.delete(key); + } + } + /** + * 判断缓存中是否有对应的value + * @param key + * @return + */ + public boolean exists(final String key) { + return redisTemplate.hasKey(key); + } + /** + * 读取缓存 + * @param key + * @return + */ + public Object get(final String key) { + Object result = null; + ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); + result = operations.get(key); + return result; + } + /** + * 哈希 添加 + * @param key + * @param hashKey + * @param value + */ + public void hmSet(String key, Object hashKey, Object value){ + HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); + hash.put(key,hashKey,value); + } + /** + * 哈希获取数据 + * @param key + * @param hashKey + * @return + */ + public Object hmGet(String key, Object hashKey){ + HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); + return hash.get(key,hashKey); + } + /** + * 列表添加 + * @param k + * @param v + */ + public void lPush(String k,Object v){ + ListOperations<String, Object> list = redisTemplate.opsForList(); + list.rightPush(k,v); + } + /** + * 列表获取 + * @param k + * @param l + * @param l1 + * @return + */ + public List<Object> lRange(String k, long l, long l1){ + ListOperations<String, Object> list = redisTemplate.opsForList(); + return list.range(k,l,l1); + } + /** + * 集合添加 + * @param key + * @param value + */ + public void add(String key,Object value){ + SetOperations<String, Object> set = redisTemplate.opsForSet(); + set.add(key,value); + } + /** + * 集合获取 + * @param key + * @return + */ + public Set<Object> setMembers(String key){ + SetOperations<String, Object> set = redisTemplate.opsForSet(); + return set.members(key); + } + /** + * 有序集合添加 + * @param key + * @param value + * @param scoure + */ + public void zAdd(String key,Object value,double scoure){ + ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); + zset.add(key,value,scoure); + } + /** + * 有序集合获取 + * @param key + * @param scoure + * @param scoure1 + * @return + */ + public Set<Object> rangeByScore(String key,double scoure,double scoure1){ + ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); + return zset.rangeByScore(key, scoure, scoure1); + } + + /** + * @Description: 获取过期时间 返回 秒 + */ + + public Long getExpireTime(String key) { + return redisTemplate.getExpire(key, TimeUnit.SECONDS); + } + + + /** + * @Description: 重置key 的 过期时间 + */ + public void resetKeyExpireTime(String key, Long seconds) { + redisTemplate.expire(key, seconds, TimeUnit.SECONDS); + } + + 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 { + scan.close(); + } catch (IOException e) { + throw new RuntimeException("scanMatch关闭失败"); + } + } + return keys; + } + if(redisConnection instanceof JedisConnection){ + scan = redisConnection.scan(ScanOptions.scanOptions().match(matchKey).count(Integer.MAX_VALUE).build()); + while (scan.hasNext()){ + //找到一次就添加一次 + keys.add(new String(scan.next())); + } + try { + scan.close(); + } catch (IOException e) { + throw new RuntimeException("scanMatch关闭失败"); + } + return keys; + } + + return keys; +} + +} diff --git a/src/main/java/com/ruoyi/doublePrevention/controller/PreventRiskUnitController.java b/src/main/java/com/ruoyi/doublePrevention/controller/PreventRiskUnitController.java new file mode 100644 index 0000000..97889ac --- /dev/null +++ b/src/main/java/com/ruoyi/doublePrevention/controller/PreventRiskUnitController.java @@ -0,0 +1,33 @@ +package com.ruoyi.doublePrevention.controller; + + +import com.ruoyi.doublePrevention.entity.dto.resp.PreventRiskUnitCodeAndNameListQueryRespDTO; +import com.ruoyi.doublePrevention.enums.ResultCodes; +import com.ruoyi.doublePrevention.service.RiskService; +import com.ruoyi.doublePrevention.vo.ResultVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/riskUnit") +public class PreventRiskUnitController { + + @Autowired + private RiskService riskService; + + /** + * 获取所有的风险分析单元的编码和姓名 + */ + @PostMapping("/select/listRiskUnitCodeAndName") + public ResultVO<List<PreventRiskUnitCodeAndNameListQueryRespDTO>> listRiskUnitCodeAndName() { + ResultVO resultVO = new ResultVO<>(); + resultVO.setCode(ResultCodes.OK.getCode()); + List<PreventRiskUnitCodeAndNameListQueryRespDTO> respDTOS = riskService.listRiskUnitCodeAndName(); + resultVO.setData(respDTOS); + return resultVO; + } +} diff --git a/src/main/java/com/ruoyi/doublePrevention/enums/CountContentEnum.java b/src/main/java/com/ruoyi/doublePrevention/enums/CountContentEnum.java new file mode 100644 index 0000000..8aff7a5 --- /dev/null +++ b/src/main/java/com/ruoyi/doublePrevention/enums/CountContentEnum.java @@ -0,0 +1,33 @@ +package com.ruoyi.doublePrevention.enums; + +/** + * @description SPI统计内容 + */ +public enum CountContentEnum { + + //统计时间段内所有、死亡、重伤、轻伤的隐患数量 + RESULT_COUNT("所有、死亡、重伤、轻伤的隐患数量"), + + //统计时间段内一般、重大的隐患数量 + LEVEL_COUNT("一般、重大的隐患数量"), + + //统计时间段内一般已整改、重大已整改的隐患数量 + LEVEL_RECTIFIED_COUNT("一般已整改、重大已整改的隐患数量"), + + ; + + + CountContentEnum(String desc) { + this.desc = desc; + } + + private String desc; + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } +} diff --git a/src/main/java/com/ruoyi/doublePrevention/service/impl/RiskServiceImpl.java b/src/main/java/com/ruoyi/doublePrevention/service/impl/RiskServiceImpl.java index 7d9aa9d..31fa462 100644 --- a/src/main/java/com/ruoyi/doublePrevention/service/impl/RiskServiceImpl.java +++ b/src/main/java/com/ruoyi/doublePrevention/service/impl/RiskServiceImpl.java @@ -1265,6 +1265,9 @@ @Override public List<PreventRiskUnitCodeAndNameListQueryRespDTO> listRiskUnitCodeAndName() { List<PreventRiskList> riskLists = preventRiskListService.listRiskUnitCodeAndName(); + if (riskLists == null){ + return null; + } List<PreventRiskUnitCodeAndNameListQueryRespDTO> listQueryRespDTOs= riskLists.stream().map((riskList)->{ PreventRiskUnitCodeAndNameListQueryRespDTO listQueryRespDTO = new PreventRiskUnitCodeAndNameListQueryRespDTO(); BeanUtils.copyProperties(riskList,listQueryRespDTO); diff --git a/src/main/java/com/ruoyi/doublePrevention/service/impl/SPIDataCountServiceImpl.java b/src/main/java/com/ruoyi/doublePrevention/service/impl/SPIDataCountServiceImpl.java index 5531f62..827faec 100644 --- a/src/main/java/com/ruoyi/doublePrevention/service/impl/SPIDataCountServiceImpl.java +++ b/src/main/java/com/ruoyi/doublePrevention/service/impl/SPIDataCountServiceImpl.java @@ -6,6 +6,7 @@ import com.ruoyi.doublePrevention.entity.dto.DataCountDangerResultRespDO; import com.ruoyi.doublePrevention.entity.dto.req.SPIDataCountReqDTO; import com.ruoyi.doublePrevention.entity.dto.resp.SPIDataCountRespDTO; +import com.ruoyi.doublePrevention.enums.CountContentEnum; import com.ruoyi.doublePrevention.repository.param.DataCountStartAndEndTimeParams; import com.ruoyi.doublePrevention.service.SPIDataCountService; import com.ruoyi.doublePrevention.service.baseService.DoublePreventionDeptService; @@ -16,6 +17,7 @@ import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -26,6 +28,9 @@ @Service("SPIDataCountService") public class SPIDataCountServiceImpl implements SPIDataCountService { + + @Autowired + private RedisTemplate redisTemplate; @Autowired private DoublePreventionDeptService doublePreventionDeptService; @@ -66,26 +71,77 @@ DataCountDangerLevelRespDO levelCountByTime = null; DataCountDangerLevelRectifiedRespDO levelRectifiedCountByTime = null; + String SPIDataCountKey = "SPI_PREVENT_RISK_"; + //判断month是否null + Calendar calendar = Calendar.getInstance(); + int currentYear = calendar.get(Calendar.YEAR); + int currentMonth = calendar.get(Calendar.MONTH) + 1; + int currentDay = calendar.get(Calendar.DATE); + + if (month == null){ + if (currentYear != year){ + SPIDataCountKey = SPIDataCountKey + year+"_"+"【1-12】_"+depId; + }else { + SPIDataCountKey = SPIDataCountKey + year + "_" + "【1-" + currentMonth + "/" + currentDay + "】_" + depId; + } + }else { + if (currentYear != year){ + SPIDataCountKey = SPIDataCountKey + year+"_"+month+"_"+depId; + }else { + SPIDataCountKey = SPIDataCountKey + year + "_" + currentMonth +"_【1-" + currentDay + "】_" + depId; + } + } + if (ObjectUtils.isNotEmpty(month) && ObjectUtils.isNotEmpty(year)){ DataCountStartAndEndTimeParams params = timeInterval(month, year); + // 月-统计时间段内所有、死亡、重伤、轻伤的隐患数量 - resultCountByTime = preventRiskDangerInfoService.listDangerResultCountByTime(params.getStartTime(), params.getEndTime(), depIds); + if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name())){ + resultCountByTime = preventRiskDangerInfoService.listDangerResultCountByTime(params.getStartTime(), params.getEndTime(), depIds); + redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name(),resultCountByTime); + } + resultCountByTime = (DataCountDangerResultRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name()); + // 月-统计时间段内一般、重大的隐患数量 - levelCountByTime = checkPointService.listDangerLevelCountByTime(params.getStartTime(), params.getEndTime(), depIds); + if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name())){ + levelCountByTime = checkPointService.listDangerLevelCountByTime(params.getStartTime(), params.getEndTime(), depIds); + redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name(),levelCountByTime); + } + levelCountByTime = (DataCountDangerLevelRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name()); // 月-统计时间段内一般已整改、重大已整改的隐患数量 - levelRectifiedCountByTime = checkPointService.listDangerLevelRectifiedCountByTime(params.getStartTime(), params.getEndTime(), depIds); + if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name())){ + levelRectifiedCountByTime = checkPointService.listDangerLevelRectifiedCountByTime(params.getStartTime(), params.getEndTime(), depIds); + redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name(),levelRectifiedCountByTime); + } + levelRectifiedCountByTime = (DataCountDangerLevelRectifiedRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name()); + }else { DataCountStartAndEndTimeParams params = timeInterval(null, year); // 年-统计时间段内所有、死亡、重伤、轻伤的隐患数量 - resultCountByTime = preventRiskDangerInfoService.listDangerResultCountByTime(params.getStartTime(), params.getEndTime(), depIds); + if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name())){ + resultCountByTime = preventRiskDangerInfoService.listDangerResultCountByTime(params.getStartTime(), params.getEndTime(), depIds); + redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name(),resultCountByTime); + } + resultCountByTime = (DataCountDangerResultRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.RESULT_COUNT.name()); + // 年-统计时间段内一般、重大的隐患数量 - levelCountByTime = checkPointService.listDangerLevelCountByTime(params.getStartTime(), params.getEndTime(), depIds); + if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name())){ + levelCountByTime = checkPointService.listDangerLevelCountByTime(params.getStartTime(), params.getEndTime(), depIds); + redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name(),levelCountByTime); + } + levelCountByTime = (DataCountDangerLevelRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.LEVEL_COUNT.name()); + // 年-统计时间段内一般已整改、重大已整改的隐患数量 - levelRectifiedCountByTime = checkPointService.listDangerLevelRectifiedCountByTime(params.getStartTime(), params.getEndTime(), depIds); + if (!redisTemplate.opsForHash().hasKey(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name())){ + levelRectifiedCountByTime = checkPointService.listDangerLevelRectifiedCountByTime(params.getStartTime(), params.getEndTime(), depIds); + redisTemplate.opsForHash().put(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name(),levelRectifiedCountByTime); + } + levelRectifiedCountByTime = (DataCountDangerLevelRectifiedRespDO)redisTemplate.opsForHash().get(SPIDataCountKey,CountContentEnum.LEVEL_RECTIFIED_COUNT.name()); + } if (resultCountByTime == null || levelCountByTime == null || levelRectifiedCountByTime == null){ parameterVerificationResult.setCode("400"); @@ -195,6 +251,4 @@ params.setEndTime(endTime); return params; } - - } -- Gitblit v1.9.2