Commit 7b5803d2564f019ce84242f2f5f34995a563cc13

Authored by 娄高锋
1 parent 59239920

氢能源相关类

src/main/java/com/bsth/entity/HInfo.java 0 → 100644
  1 +package com.bsth.entity;
  2 +
  3 +
  4 +import org.springframework.format.annotation.DateTimeFormat;
  5 +
  6 +import javax.persistence.Entity;
  7 +import javax.persistence.Id;
  8 +import javax.persistence.Table;
  9 +import java.io.Serializable;
  10 +import java.util.Date;
  11 +
  12 +/**
  13 + * 电量信息表
  14 + */
  15 +@Entity
  16 +@Table(name = "bsth_c_qlb")
  17 +public class HInfo implements Serializable {
  18 + @Id
  19 + private Integer id;
  20 + /**
  21 + * 日期
  22 + */
  23 + @DateTimeFormat(pattern = "yyyy-MM-dd")
  24 + private Date rq;
  25 + /**
  26 + * 内部编码
  27 + */
  28 + private String nbbm;
  29 + /**
  30 + * 驾驶员
  31 + */
  32 + private String jsy;
  33 + /**
  34 + * 充氢量
  35 + */
  36 + private Double cql;
  37 + /**
  38 + * 耗氢
  39 + */
  40 + private Double hq;
  41 +
  42 + @Override
  43 + public int hashCode() {
  44 + return this.toString().hashCode();
  45 + }
  46 +
  47 + @Override
  48 + public boolean equals(Object obj) {
  49 + return this.toString().equals(((HInfo)obj).toString());
  50 + }
  51 +
  52 + @Override
  53 + public String toString() {
  54 + return "hydrogen_" + this.id;
  55 + }
  56 +
  57 + public Integer getId() {
  58 + return id;
  59 + }
  60 +
  61 + public void setId(Integer id) {
  62 + this.id = id;
  63 + }
  64 +
  65 + public Date getRq() {
  66 + return rq;
  67 + }
  68 +
  69 + public void setRq(Date rq) {
  70 + this.rq = rq;
  71 + }
  72 +
  73 + public String getNbbm() {
  74 + return nbbm;
  75 + }
  76 +
  77 + public void setNbbm(String nbbm) {
  78 + this.nbbm = nbbm;
  79 + }
  80 +
  81 + public String getJsy() {
  82 + return jsy;
  83 + }
  84 +
  85 + public void setJsy(String jsy) {
  86 + this.jsy = jsy;
  87 + }
  88 +
  89 + public Double getCql() {
  90 + return cql;
  91 + }
  92 +
  93 + public void setCql(Double cql) {
  94 + this.cql = cql;
  95 + }
  96 +
  97 + public Double getHq() {
  98 + return hq;
  99 + }
  100 +
  101 + public void setHq(Double hq) {
  102 + this.hq = hq;
  103 + }
  104 +
  105 +}
... ...
src/main/java/com/bsth/redis/HRedisService.java 0 → 100644
  1 +package com.bsth.redis;
  2 +
  3 +import com.bsth.Application;
  4 +import com.bsth.entity.HInfo;
  5 +import com.bsth.redis.util.RedisUtils;
  6 +import com.bsth.repository.HInfoRepository;
  7 +import com.bsth.util.ConfigUtil;
  8 +import com.bsth.util.ConvertUtil;
  9 +import com.google.common.collect.ArrayListMultimap;
  10 +import org.joda.time.DateTime;
  11 +import org.slf4j.Logger;
  12 +import org.slf4j.LoggerFactory;
  13 +import org.springframework.beans.factory.annotation.Autowired;
  14 +import org.springframework.boot.CommandLineRunner;
  15 +import org.springframework.core.annotation.Order;
  16 +import org.springframework.data.redis.core.RedisTemplate;
  17 +import org.springframework.data.redis.serializer.StringRedisSerializer;
  18 +import org.springframework.stereotype.Component;
  19 +import org.springframework.stereotype.Service;
  20 +
  21 +import java.util.*;
  22 +import java.util.concurrent.TimeUnit;
  23 +
  24 +/**
  25 + * 油量数据Redis缓存
  26 + * Created by panzhao on 2017/3/19.
  27 + */
  28 +@Service
  29 +@Order(3)
  30 +public class HRedisService implements CommandLineRunner {
  31 +
  32 + @Autowired
  33 + private RedisTemplate redisTemplate;
  34 +
  35 + @Autowired
  36 + HInfoRepository hInfoRepository;
  37 +
  38 + @Autowired
  39 + RedisUtils redisUtils;
  40 +
  41 + static Logger logger = LoggerFactory.getLogger(HRedisService.class);
  42 +
  43 + private final static String REDIS_KEY_PREFIX = "H:";
  44 +
  45 + /**
  46 + * 将油量数据写入redis
  47 + *
  48 + * @param list
  49 + */
  50 + public void wirte(List<HInfo> list) {
  51 + ArrayListMultimap<String, HInfo> multimap;
  52 + try {
  53 + if (list.size() == 0)
  54 + return;
  55 + //按日期和线路分组数据
  56 + Class clazz = HInfo.class;
  57 + multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("rq"));
  58 +
  59 + //写入redis
  60 + Iterator<String> iterator = multimap.keySet().iterator();
  61 + String key;
  62 + while (iterator.hasNext()) {
  63 + key = iterator.next();
  64 + mergeData(key, multimap.get(key));
  65 + }
  66 + } catch (Exception e) {
  67 + logger.error("", e);
  68 + }
  69 + }
  70 +
  71 + /**
  72 + * 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key
  73 + *
  74 + * @param nbbmArray
  75 + * @param rq
  76 + * @return
  77 + */
  78 + public ArrayListMultimap findByNbbmGroup(Iterable<String> nbbmArray, String rq) {
  79 + ArrayListMultimap rs = ArrayListMultimap.create();
  80 +
  81 + rq = rq.replaceAll("-", "");
  82 + try {
  83 + List<HInfo> list = new ArrayList<>();
  84 + for (String nbbm : nbbmArray) {
  85 + nbbm = nbbm.split("_")[1];
  86 + list.addAll(read(nbbm, rq));
  87 + }
  88 + Class clazz = HInfo.class;
  89 + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy"));
  90 + } catch (Exception e) {
  91 + logger.error("", e);
  92 + }
  93 + return rs;
  94 + }
  95 +
  96 + /**
  97 + * 根据车辆和日期获取油耗数据,以 车辆 为key
  98 + *
  99 + * @param nbbmArray
  100 + * @param rq
  101 + * @return
  102 + */
  103 + public ArrayListMultimap findByNbbmGroup1(Iterable<String> nbbmArray, String rq) {
  104 + ArrayListMultimap rs = ArrayListMultimap.create();
  105 +
  106 + rq = rq.replaceAll("-", "");
  107 + try {
  108 + List<HInfo> list = new ArrayList<>();
  109 + for (String nbbm : nbbmArray) {
  110 + list.addAll(read(nbbm, rq));
  111 + }
  112 + Class clazz = HInfo.class;
  113 + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"));
  114 + } catch (Exception e) {
  115 + logger.error("", e);
  116 + }
  117 + return rs;
  118 + }
  119 +
  120 + public List<HInfo> read(String nbbm, String rq) {
  121 + return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1);
  122 + }
  123 +
  124 + /**
  125 + * 将 list 与redis里的数据合并
  126 + *
  127 + * @param key
  128 + * @param list
  129 + */
  130 + public void mergeData(String key, List<HInfo> list) {
  131 + key = REDIS_KEY_PREFIX + key;
  132 +
  133 + //更新 直接覆盖更新
  134 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
  135 + }
  136 +
  137 + @Autowired
  138 + ElecRefreshThread elecRefreshThread;
  139 +
  140 + @Override
  141 + public void run(String... strings) throws Exception {
  142 + Application.mainServices.schedule(new Runnable() {
  143 + @Override
  144 + public void run() {
  145 + //启动加载油耗缓存
  146 + synchData(null);
  147 + }
  148 + }, 30, TimeUnit.SECONDS);
  149 +
  150 +
  151 + //定时刷新油耗信息
  152 + Application.mainServices.scheduleWithFixedDelay(elecRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS);
  153 + }
  154 +
  155 + /**
  156 + * 和数据库同步数据
  157 + */
  158 + public void synchData(Integer days) {
  159 + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
  160 + if (null != days && days < cacheDays)
  161 + cacheDays = days;
  162 + //设置key 序列化器
  163 + redisTemplate.setKeySerializer(new StringRedisSerializer());
  164 +
  165 + DateTime dt = new DateTime();
  166 + dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays);
  167 + List<HInfo> list = hInfoRepository.findByDateLT(dt.toDate());
  168 + //写入redis
  169 + wirte(list);
  170 + logger.info("刷新氢耗数据, days: " + cacheDays + " -size: " + list.size() + " -LT: " + dt.toString("yyyy-MM-dd"));
  171 + }
  172 +
  173 + @Component
  174 + public static class ElecRefreshThread extends Thread {
  175 +
  176 + @Autowired
  177 + HRedisService hRedisService;
  178 +
  179 + @Override
  180 + public void run() {
  181 + try {
  182 + hRedisService.synchData(5);
  183 + } catch (Exception e) {
  184 + logger.error("", e);
  185 + }
  186 + }
  187 + }
  188 +}
... ...
src/main/java/com/bsth/repository/HInfoRepository.java 0 → 100644
  1 +package com.bsth.repository;
  2 +
  3 +import com.bsth.entity.HInfo;
  4 +
  5 +import org.springframework.data.jpa.repository.Query;
  6 +import org.springframework.data.repository.PagingAndSortingRepository;
  7 +import org.springframework.stereotype.Repository;
  8 +
  9 +import java.util.Date;
  10 +import java.util.List;
  11 +
  12 +@Repository
  13 +public interface HInfoRepository extends PagingAndSortingRepository<HInfo, Long> {
  14 +
  15 +
  16 + @Query("select h from HInfo h where h.rq>?1")
  17 + List<HInfo> findByDateLT(Date rq);
  18 +}
... ...