Commit 7788ef69b189b0d0f3b93b2da8407d60ffcb6bd4

Authored by 娄高锋
1 parent 7b5803d2

氢能源

src/main/java/com/bsth/entity/HInfo.java
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   -}
  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 jql;
  37 + /**
  38 + * 耗氢
  39 + */
  40 + private Double hn;
  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 getJql() {
  90 + return jql;
  91 + }
  92 +
  93 + public void setJql(Double jql) {
  94 + this.jql = jql;
  95 + }
  96 +
  97 + public Double getHn() {
  98 + return hn;
  99 + }
  100 +
  101 + public void setHn(Double hn) {
  102 + this.hn = hn;
  103 + }
  104 +
  105 +}
... ...
src/main/java/com/bsth/redis/HRedisService.java
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   -}
  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 = "hydrogen:";
  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 + Set<String> nbbms = new HashSet<>();
  85 + for (String nbbm : nbbmArray) {
  86 + nbbm = nbbm.split("_")[1];
  87 + if (!nbbms.contains(nbbm)) {
  88 + nbbms.add(nbbm);
  89 + list.addAll(read(nbbm, rq));
  90 + }
  91 + }
  92 + Class clazz = HInfo.class;
  93 + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy"));
  94 + } catch (Exception e) {
  95 + logger.error("", e);
  96 + }
  97 + return rs;
  98 + }
  99 +
  100 + /**
  101 + * 根据车辆和日期获取油耗数据,以 车辆 为key
  102 + *
  103 + * @param nbbmArray
  104 + * @param rq
  105 + * @return
  106 + */
  107 + public ArrayListMultimap findByNbbmGroup1(Iterable<String> nbbmArray, String rq) {
  108 + ArrayListMultimap rs = ArrayListMultimap.create();
  109 +
  110 + rq = rq.replaceAll("-", "");
  111 + try {
  112 + List<HInfo> list = new ArrayList<>();
  113 + Set<String> nbbms = new HashSet<>();
  114 + for (String nbbm : nbbmArray) {
  115 + if (!nbbms.contains(nbbm)) {
  116 + nbbms.add(nbbm);
  117 + list.addAll(read(nbbm, rq));
  118 + }
  119 + }
  120 + Class clazz = HInfo.class;
  121 + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"));
  122 + } catch (Exception e) {
  123 + logger.error("", e);
  124 + }
  125 + return rs;
  126 + }
  127 +
  128 + public List<HInfo> read(String nbbm, String rq) {
  129 + return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1);
  130 + }
  131 +
  132 + /**
  133 + * 将 list 与redis里的数据合并
  134 + *
  135 + * @param key
  136 + * @param list
  137 + */
  138 + public void mergeData(String key, List<HInfo> list) {
  139 + key = REDIS_KEY_PREFIX + key;
  140 +
  141 + //更新 直接覆盖更新
  142 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
  143 + }
  144 +
  145 + @Autowired
  146 + HRefreshThread hRefreshThread;
  147 +
  148 + @Override
  149 + public void run(String... strings) throws Exception {
  150 + Application.mainServices.schedule(new Runnable() {
  151 + @Override
  152 + public void run() {
  153 + //启动加载油耗缓存
  154 + synchData(null);
  155 + }
  156 + }, 30, TimeUnit.SECONDS);
  157 +
  158 +
  159 + //定时刷新油耗信息
  160 + Application.mainServices.scheduleWithFixedDelay(hRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS);
  161 + }
  162 +
  163 + /**
  164 + * 和数据库同步数据
  165 + */
  166 + public void synchData(Integer days) {
  167 + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
  168 + if (null != days && days < cacheDays)
  169 + cacheDays = days;
  170 + //设置key 序列化器
  171 + redisTemplate.setKeySerializer(new StringRedisSerializer());
  172 +
  173 + DateTime dt = new DateTime();
  174 + dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays);
  175 + List<HInfo> list = hInfoRepository.findByDateLT(dt.toDate());
  176 + //写入redis
  177 + wirte(list);
  178 + logger.info("刷新氢耗数据, days: " + cacheDays + " -size: " + list.size() + " -LT: " + dt.toString("yyyy-MM-dd"));
  179 + }
  180 +
  181 + @Component
  182 + public static class HRefreshThread extends Thread {
  183 +
  184 + @Autowired
  185 + HRedisService hRedisService;
  186 +
  187 + @Override
  188 + public void run() {
  189 + try {
  190 + hRedisService.synchData(5);
  191 + } catch (Exception e) {
  192 + logger.error("", e);
  193 + }
  194 + }
  195 + }
  196 +}
... ...
src/main/java/com/bsth/server_ws/util/WSDataConver.java
... ... @@ -367,8 +367,8 @@ public class WSDataConver {
367 367 if (hInfo != null && hInfo.size() > 0) {
368 368 Double cql = 0., hq = 0.;
369 369 for (HInfo h : hInfo) {
370   - cql = Arith.add(cql, h.getCql());
371   - hq = Arith.add(hq, h.getHq());
  370 + cql = Arith.add(cql, h.getJql());
  371 + hq = Arith.add(hq, h.getHn());
372 372 }
373 373 //出场存油
374 374 nh_waybillItem.setM_dblCCCY(100.0);
... ...