Commit ba993fd05e1fc4df6c7d1272cc356f06156a79d7

Authored by 潘钊
1 parent b1a880e3

update...

src/main/java/com/bsth/StartCommand.java
... ... @@ -2,11 +2,11 @@ package com.bsth;
2 2  
3 3  
4 4 import com.bsth.data.msg_queue.SignalAndAttConsumeQueue;
5   -import com.bsth.data.real_park.BerthDataBuffer;
6 5 import com.bsth.data.real_park.CarParkRealHandler;
7 6 import com.bsth.data.real_park.thread.RealParkDataPersistenceThread;
8 7 import com.bsth.data.signal.thread.SignalPstThread;
9 8 import com.bsth.security.SecurityMetadataSourceService;
  9 +import com.bsth.service.berth.impl.BerthServiceImpl;
10 10 import org.springframework.beans.factory.annotation.Autowired;
11 11 import org.springframework.boot.CommandLineRunner;
12 12 import org.springframework.stereotype.Component;
... ... @@ -28,13 +28,14 @@ public class StartCommand implements CommandLineRunner{
28 28 @Autowired
29 29 SignalPstThread signalPstThread;
30 30 @Autowired
31   - BerthDataBuffer.BerthCacheRefreshThread berthCacheRefreshThread;
32   - @Autowired
33 31 RealParkDataPersistenceThread realParkDataPersistenceThread;
34 32  
35 33 @Autowired
36 34 CarParkRealHandler carParkRealHandler;
37 35  
  36 + @Autowired
  37 + BerthServiceImpl.BerthCacheRefreshThread berthCacheRefreshThread;
  38 +
38 39 @Override
39 40 public void run(String... arg0){
40 41  
... ... @@ -49,8 +50,8 @@ public class StartCommand implements CommandLineRunner{
49 50 //进出场数据入库线程
50 51 sexec.scheduleWithFixedDelay(signalPstThread, 30, 10, TimeUnit.SECONDS);
51 52  
52   - //泊位缓存刷新
53   - sexec.scheduleWithFixedDelay(berthCacheRefreshThread, 0, 60 * 2, TimeUnit.SECONDS);
  53 + //泊位数据刷新
  54 + sexec.scheduleWithFixedDelay(berthCacheRefreshThread, 0, 60 * 15, TimeUnit.SECONDS);
54 55  
55 56 //从数据库恢复场内实时停放数据
56 57 carParkRealHandler.recovery();
... ...
src/main/java/com/bsth/controller/berth/BerthController.java
... ... @@ -17,14 +17,4 @@ public class BerthController extends BaseController<RegionBerth, Integer> {
17 17 @Autowired
18 18 BerthService berthService;
19 19  
20   -/* @RequestMapping(value = "real_change", method = RequestMethod.POST)
21   - public Map<String, Object> realChange(@RequestParam String s,@RequestParam String d){
22   - return null;//berthService.realChange(s, d);
23   - }*/
24   -
25   - @RequestMapping("max_order_no")
26   - public String maxOrderNo() {
27   - String rs = berthService.maxOrderNo();
28   - return rs==null?"0":rs;
29   - }
30 20 }
... ...
src/main/java/com/bsth/data/real_park/BerthDataBuffer.java deleted 100644 → 0
1   -package com.bsth.data.real_park;
2   -
3   -import com.bsth.entity.berth.RegionBerth;
4   -import com.bsth.repository.berth.BerthRepository;
5   -import org.slf4j.Logger;
6   -import org.slf4j.LoggerFactory;
7   -import org.springframework.beans.factory.annotation.Autowired;
8   -import org.springframework.stereotype.Component;
9   -
10   -import java.util.List;
11   -
12   -/**
13   - * 预设泊位数据缓存
14   - * Created by panzhao on 2017/9/14.
15   - */
16   -public class BerthDataBuffer {
17   -
18   - private static List<RegionBerth> allList;
19   -
20   - static Logger logger = LoggerFactory.getLogger(BerthDataBuffer.class);
21   -
22   - private static void update(RegionBerth b){
23   - RegionBerth cacheB = getById(b.getId());
24   - if(null == cacheB)
25   - allList.add(cacheB);
26   - else{
27   - //cacheB.setNbbm(b.getNbbm());
28   - cacheB.setOrderNo(b.getOrderNo());
29   - cacheB.setName(b.getName());
30   - cacheB.setRegion(b.getRegion());
31   - cacheB.setUpdateDate(b.getUpdateDate());
32   - cacheB.setRemark(b.getRemark());
33   - }
34   - }
35   -
36   - private static RegionBerth getById(Integer id) {
37   - for(RegionBerth b : allList){
38   - if(id.equals(b.getId()))
39   - return b;
40   - }
41   - return null;
42   - }
43   -/*
44   - public static RegionBerth get(String nbbm) {
45   - for(RegionBerth b : allList){
46   - if(nbbm.equals(b.getNbbm()))
47   - return b;
48   - }
49   - return null;
50   - }*/
51   -
52   - /**
53   - * 泊位缓存刷新线程
54   - */
55   - @Component
56   - public static class BerthCacheRefreshThread extends Thread {
57   -
58   - @Autowired
59   - BerthRepository berthRepository;
60   -
61   - @Override
62   - public void run() {
63   - /*try{
64   - List<RegionBerth> list = (List<RegionBerth>) berthRepository.findAll();
65   - Map<String, RegionBerth> nbbm2BerthMapCopy = new HashMap<>();
66   - for (RegionBerth b : list) {
67   - if (StringUtils.isNotEmpty(b.getName())) {
68   - //nbbm2BerthMapCopy.put(b.getNbbm(), b);
69   - }
70   - }
71   -
72   - list = new ArrayList(nbbm2BerthMapCopy.values());
73   -
74   - if (null == allList)
75   - allList = list;
76   - else {
77   - //增量更新
78   - for (RegionBerth b : list) {
79   - update(b);
80   - }
81   - }
82   -
83   - //排序
84   - Collections.sort(allList, new Comparator<RegionBerth>() {
85   - @Override
86   - public int compare(RegionBerth o1, RegionBerth o2) {
87   - return Integer.parseInt(o1.getOrderNo()) - Integer.parseInt(o2.getOrderNo());
88   - }
89   - });
90   - }catch (Exception e){
91   - logger.error("泊位数据刷新失败!!", e);
92   - }*/
93   - }
94   - }
95   -}
src/main/java/com/bsth/data/real_park/CarParkRealHandler.java
... ... @@ -5,8 +5,10 @@ import com.bsth.data.basic.bus.BusDataBuffer;
5 5 import com.bsth.data.electric.CarElectricBuffer;
6 6 import com.bsth.entity.Bus;
7 7 import com.bsth.entity.ac.CarInOutEntity;
  8 +import com.bsth.entity.berth.RegionBerth;
8 9 import com.bsth.entity.electric.BusElectric;
9 10 import com.bsth.entity.real.RealCarPark;
  11 +import com.bsth.service.berth.BerthService;
10 12 import org.apache.commons.lang3.StringUtils;
11 13 import org.slf4j.Logger;
12 14 import org.slf4j.LoggerFactory;
... ... @@ -26,6 +28,9 @@ import java.util.concurrent.ConcurrentMap;
26 28 @Component
27 29 public class CarParkRealHandler {
28 30  
  31 + @Autowired
  32 + BerthService berthService;
  33 +
29 34 /**
30 35 * 公交车
31 36 * K: 自编号
... ... @@ -57,6 +62,22 @@ public class CarParkRealHandler {
57 62 }
58 63 }
59 64  
  65 + /**
  66 + * 最大电量的泊位名称
  67 + * @return
  68 +
  69 + public String getMaxElecBerth(){
  70 + String name = null;
  71 + double maxSoc=0.0;
  72 + Collection<RealCarPark> vs = allBus();
  73 +
  74 + for(RealCarPark rcp : vs){
  75 + if(Double.parseDouble(rcp.getSoc()) > maxSoc)
  76 + name = rcp.getBerthName();
  77 + }
  78 + return name;
  79 + }*/
  80 +
60 81 public static Collection<RealCarPark> allBus() {
61 82 return busRcps.values();
62 83 }
... ... @@ -100,9 +121,10 @@ public class CarParkRealHandler {
100 121  
101 122 RealCarPark rcp = new RealCarPark();
102 123 //泊位信息
103   - /*RegionBerth b = BerthDataBuffer.get(nbbm);
104   - if (null != b)
105   - rcp.setBerthName(b.getName());*/
  124 + RegionBerth b = berthService.next();
  125 + //泊位设置车辆
  126 + b.setNbbm(nbbm);
  127 + rcp.setBerthName(b.getName());
106 128  
107 129 rcp.setNbbm(nbbm);
108 130 rcp.setInTime(System.currentTimeMillis());
... ... @@ -169,6 +191,11 @@ public class CarParkRealHandler {
169 191 return null;
170 192 }
171 193  
  194 + public Double soc(String nbbm){
  195 + RealCarPark rcp = busRcps.get(nbbm);
  196 + return rcp!=null?Double.parseDouble(rcp.getSoc()):null;
  197 + }
  198 +
172 199 @Autowired
173 200 JdbcTemplate jdbcTemplate;
174 201  
... ...
src/main/java/com/bsth/entity/berth/RegionBerth.java
... ... @@ -11,7 +11,7 @@ import java.util.Date;
11 11 */
12 12 @Entity
13 13 @Table(name = "bsth_p_berth")
14   -public class RegionBerth {
  14 +public class RegionBerth implements Cloneable{
15 15  
16 16 @Id
17 17 @GeneratedValue
... ... @@ -60,13 +60,6 @@ public class RegionBerth {
60 60 private int chargePile;
61 61  
62 62 /**
63   - * 是否是充电桩的第一个泊位
64   - *
65   - * 默认 1 个桩 2个 泊位
66   - */
67   - private boolean first;
68   -
69   - /**
70 63 * 状态
71 64 * 0: 可用
72 65 * -1: 禁用
... ... @@ -85,6 +78,27 @@ public class RegionBerth {
85 78 @Column(name = "update_date", columnDefinition = "timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
86 79 private Date updateDate;
87 80  
  81 + /**
  82 + * 停放车辆
  83 + */
  84 + @Transient
  85 + private String nbbm;
  86 +
  87 + @Override
  88 + public int hashCode() {
  89 + return this.toString().hashCode();
  90 + }
  91 +
  92 + @Override
  93 + public boolean equals(Object obj) {
  94 + return this.toString().equals(obj.toString());
  95 + }
  96 +
  97 + @Override
  98 + public String toString() {
  99 + return ("berth_" + this.getName());
  100 + }
  101 +
88 102 public Integer getId() {
89 103 return id;
90 104 }
... ... @@ -181,14 +195,6 @@ public class RegionBerth {
181 195 this.type = type;
182 196 }
183 197  
184   - public boolean isFirst() {
185   - return first;
186   - }
187   -
188   - public void setFirst(boolean first) {
189   - this.first = first;
190   - }
191   -
192 198 public int getChargePile() {
193 199 return chargePile;
194 200 }
... ... @@ -196,4 +202,12 @@ public class RegionBerth {
196 202 public void setChargePile(int chargePile) {
197 203 this.chargePile = chargePile;
198 204 }
  205 +
  206 + public String getNbbm() {
  207 + return nbbm;
  208 + }
  209 +
  210 + public void setNbbm(String nbbm) {
  211 + this.nbbm = nbbm;
  212 + }
199 213 }
... ...
src/main/java/com/bsth/repository/berth/BerthRepository.java
... ... @@ -2,7 +2,6 @@ package com.bsth.repository.berth;
2 2  
3 3 import com.bsth.entity.berth.RegionBerth;
4 4 import com.bsth.repository.BaseRepository;
5   -import org.springframework.data.jpa.repository.Query;
6 5 import org.springframework.stereotype.Repository;
7 6  
8 7 /**
... ... @@ -11,6 +10,6 @@ import org.springframework.stereotype.Repository;
11 10 @Repository
12 11 public interface BerthRepository extends BaseRepository<RegionBerth, Integer>{
13 12  
14   - @Query("select max(b.orderNo) from RegionBerth b")
15   - String maxOrderNo();
  13 +/* @Query("select max(b.orderNo) from RegionBerth b")
  14 + String maxOrderNo();*/
16 15 }
... ...
src/main/java/com/bsth/service/berth/BerthService.java
... ... @@ -7,7 +7,8 @@ import com.bsth.service.BaseService;
7 7 * Created by panzhao on 2017/8/22.
8 8 */
9 9 public interface BerthService extends BaseService<RegionBerth, Integer>{
10   - String maxOrderNo();
  10 + RegionBerth next();
  11 + //String maxOrderNo();
11 12  
12 13 //void berthInOut(Bus bus);
13 14  
... ...
src/main/java/com/bsth/service/berth/impl/BerthServiceImpl.java
1 1 package com.bsth.service.berth.impl;
2 2  
  3 +import com.bsth.data.real_park.CarParkRealHandler;
3 4 import com.bsth.entity.berth.RegionBerth;
4 5 import com.bsth.repository.berth.BerthRepository;
5 6 import com.bsth.service.berth.BerthService;
6 7 import com.bsth.service.impl.BaseServiceImpl;
  8 +import com.bsth.util.ConvertUtil;
  9 +import com.google.common.collect.ArrayListMultimap;
7 10 import org.slf4j.Logger;
8 11 import org.slf4j.LoggerFactory;
9 12 import org.springframework.beans.factory.annotation.Autowired;
  13 +import org.springframework.stereotype.Component;
10 14 import org.springframework.stereotype.Service;
11 15  
12   -import java.util.List;
  16 +import java.util.*;
13 17  
14 18 /**
15 19 * Created by panzhao on 2017/8/22.
... ... @@ -17,77 +21,145 @@ import java.util.List;
17 21 @Service
18 22 public class BerthServiceImpl extends BaseServiceImpl<RegionBerth, Integer> implements BerthService {
19 23  
  24 + //泊位集合
  25 + private static List<RegionBerth> berthList;
  26 +
20 27 @Autowired
21   - BerthRepository berthRepository;
  28 + CarParkRealHandler carParkRealHandler;
22 29  
23   - private static List<RegionBerth> allList;
24   - /** 待入库的
25   - private static LinkedList<RegionBerth> psts = new LinkedList<>();
  30 + /**
  31 + * 充电枪状态
  32 + * -404 空闲
  33 + * 其他:当前充电车辆电量
26 34 */
  35 + private static Map<String, Double> chargeMap;
27 36 Logger logger = LoggerFactory.getLogger(this.getClass());
28 37  
29   - @Override
30   - public String maxOrderNo() {
31   - return berthRepository.maxOrderNo();
  38 +
  39 + static {
  40 + berthList = new ArrayList<>();
  41 + chargeMap = new HashMap<>();
32 42 }
33   -/*
  43 +
  44 + /**
  45 + * 获取下一个可用泊位
  46 + *
  47 + * @return
  48 + */
34 49 @Override
35   - public void berthInOut(Bus bus) {
  50 + public RegionBerth next() {
  51 + RegionBerth b = null;
  52 +
  53 + try {
  54 + b = nextChargeBerth();
  55 + if (null != b)
  56 + return b;
  57 +
  58 + //最快充电完成枪的空余泊位
  59 + b = maxElecBerth();
  60 + } catch (Exception e) {
  61 + logger.error("", e);
  62 + }
  63 + return b;
  64 + }
  65 +
  66 + private RegionBerth maxElecBerth() {
  67 + RegionBerth near = null;
  68 + //按充电桩分组
  69 + try {
  70 + ArrayListMultimap<String, RegionBerth> multimap = new ConvertUtil<RegionBerth>()
  71 + .groupMultiList(berthList, "", RegionBerth.class.getDeclaredField("chargePile"));
  72 +
  73 + Set<String> cps = multimap.keySet();
  74 + int status;
  75 + double maxSoc = 0.0;
  76 + List<RegionBerth> list;
  77 + for (String cp : cps) {
  78 + list = multimap.get(cp);
  79 + //充电枪状态
  80 + status = calcCpStatus(list);
  81 + if (status == 1) {
  82 + double soc = calcCpMaxSoc(list);
  83 + RegionBerth tempBerth = nearBerthByCp(list);
  84 +
  85 + if (soc > maxSoc) {
  86 + maxSoc = soc;
  87 + near = tempBerth;
  88 + }
  89 + }
  90 + }
  91 + } catch (NoSuchFieldException e) {
  92 + logger.error("", e);
  93 + }
  94 + return near;
  95 + }
36 96  
37   - String nbbm = bus.getNbbm();
38   - RegionBerth b = get(nbbm);
39   - if(null != b){
40   - b.setNbbmReal(nbbm);
41   - //通知客户端
  97 + private RegionBerth nearBerthByCp(List<RegionBerth> list) {
  98 + Collections.sort(list, new Comparator<RegionBerth>() {
  99 + @Override
  100 + public int compare(RegionBerth b1, RegionBerth b2) {
  101 + return b1.getOrderNo().compareTo(b2.getOrderNo());
  102 + }
  103 + });
  104 + for (RegionBerth b : list) {
  105 + if (null == b.getNbbm())
  106 + return b;
  107 + }
  108 + return null;
  109 + }
42 110  
43   - //入库
44   - psts.add(b);
  111 + private double calcCpMaxSoc(List<RegionBerth> list) {
  112 + Double maxSoc = 0.0, soc;
  113 + for (RegionBerth b : list) {
  114 + soc = carParkRealHandler.soc(b.getNbbm());
  115 + if (null != soc && soc > maxSoc)
  116 + maxSoc = soc;
45 117 }
46   - }*/
  118 + return maxSoc;
  119 + }
47 120  
48 121 /**
49   - * 车辆泊位交换
50   - * @param s
51   - * @param d
  122 + * 充电桩状态
  123 + * 0:枪空闲(2个泊位都空闲)
  124 + * 1:有车充电且有空余泊位
  125 + * 2:无空余泊位
  126 + *
  127 + * @param regionBerths
52 128 * @return
  129 + */
  130 + private int calcCpStatus(List<RegionBerth> list) {
  131 + int count = 0;
  132 + for (RegionBerth b : list) {
  133 + if (null != b.getNbbm())
  134 + count++;
  135 + }
53 136  
54   - @Override
55   - public Map<String, Object> realChange(String s, String d) {
  137 + if (count == 0)
  138 + return 0;
56 139  
57   - Map<String, Object> rs = new HashMap<>();
58   - try{
59   - RegionBerth berthS = getByName(s);
60   - RegionBerth berthD = getByName(d);
  140 + if (count >= list.size())
  141 + return 2;
  142 + return 1;
  143 + }
61 144  
62   - String sNbbm = berthS.getNbbmReal();
63   - berthS.setNbbmReal(berthD.getNbbmReal());
64   - berthD.setNbbmReal(sNbbm);
  145 + /**
  146 + * 获取下一个可充电的泊位
  147 + *
  148 + * @return
  149 + */
  150 + private RegionBerth nextChargeBerth() {
  151 + for (RegionBerth b : berthList) {
  152 + if (null == b.getNbbm()
  153 + && (!chargeMap.containsKey(b.getChargePile()) || chargeMap.get(b.getChargePile()) == -404))
  154 + return b;
65 155  
66   - rs.put("status", ResponseCode.SUCCESS);
67   - rs.put("list", allList);
68   - }catch (Exception e){
69   - logger.error("", e);
70   - rs.put("status", ResponseCode.ERROR);
71 156 }
72   -
73   - return rs;
74   - }*/
75   -
76   - /*@Override
77   - public Iterable<RegionBerth> list(Map<String, Object> map) {
78   - return allList;
  157 + return null;
79 158 }
80   -*/
81   -/* @Override
82   - public void putElec(BusElectric be){
83   - RegionBerth rb = get(be.getNbbm());
84   - if(null != rb)
85   - rb.setBe(be);
86   - }*/
87   -
88   -/* *//**
89   - * 泊位缓存刷新线程
90   - *//*
  159 +
  160 + /**
  161 + * 定时将泊位数据load 到内存
  162 + */
91 163 @Component
92 164 public static class BerthCacheRefreshThread extends Thread {
93 165  
... ... @@ -97,104 +169,24 @@ public class BerthServiceImpl extends BaseServiceImpl&lt;RegionBerth, Integer&gt; impl
97 169 @Override
98 170 public void run() {
99 171 List<RegionBerth> list = (List<RegionBerth>) berthRepository.findAll();
100   - Map<String, RegionBerth> nbbm2BerthMapCopy = new HashMap<>();
101   - for (RegionBerth b : list) {
102   - if (StringUtils.isNotEmpty(b.getName())) {
103   - nbbm2BerthMapCopy.put(b.getNbbm(), b);
104   - }
105   - }
106 172  
107   - list = new ArrayList(nbbm2BerthMapCopy.values());
  173 + List<RegionBerth> listCopy = new ArrayList<>();
  174 + Collections.copy(listCopy, berthList);
108 175  
109   - if(null == allList)
110   - allList = list;
111   - else{
112   - //增量更新
113   - for(RegionBerth b : list){
114   - update(b);
115   - }
  176 + for (RegionBerth b : list) {
  177 + if (!listCopy.contains(b))
  178 + listCopy.add(b);
116 179 }
117 180  
118 181 //排序
119   - Collections.sort(allList, new Comparator<RegionBerth>() {
  182 + Collections.sort(listCopy, new Comparator<RegionBerth>() {
120 183 @Override
121   - public int compare(RegionBerth o1, RegionBerth o2) {
122   - return Integer.parseInt(o1.getOrderNo()) - Integer.parseInt(o2.getOrderNo());
  184 + public int compare(RegionBerth b1, RegionBerth b2) {
  185 + return b1.getOrderNo().compareTo(b2.getOrderNo());
123 186 }
124 187 });
125   - }
126   - }*/
127   -
128   - /**
129   - * 泊位实时停放信息持久化线程
130   -
131   - @Component
132   - public static class BerthCachePersistenceThread extends Thread {
133   -
134   - @Autowired
135   - JdbcTemplate jdbcTemplate;
136   -
137   - Logger logger = LoggerFactory.getLogger(this.getClass());
138   -
139   - @Override
140   - public void run() {
141   - RegionBerth b;
142   - for(int i = 0; i < 10; i++){
143   - b = psts.poll();
144   - if(null == b)
145   - break;
146 188  
147   - updateDb(b);
148   - }
149   - }
150   -
151   - private void updateDb(RegionBerth b){
152   - try{
153   - String sql = "update bsth_p_berth set nbbm_real=?, charging = ? where id=?";
154   - jdbcTemplate.update(sql, b.getNbbmReal(), b.getCharging() , b.getId());
155   - }catch (Exception e){
156   - logger.error("", e);
157   - }
158   - }
159   - }
160   - */
161   -/*
162   - private static RegionBerth get(String nbbm) {
163   - for(RegionBerth b : allList){
164   - if(nbbm.equals(b.getNbbm()))
165   - return b;
  189 + berthList = listCopy;
166 190 }
167   - return null;
168 191 }
169   -*/
170   -
171   -/* private static RegionBerth getById(Integer id) {
172   - for(RegionBerth b : allList){
173   - if(id.equals(b.getId()))
174   - return b;
175   - }
176   - return null;
177   - }
178   -
179   - private static RegionBerth getByName(String name) {
180   - for(RegionBerth b : allList){
181   - if(name.equals(b.getName()))
182   - return b;
183   - }
184   - return null;
185   - }*/
186   -
187   -/* private static void update(RegionBerth b){
188   - RegionBerth cacheB = getById(b.getId());
189   - if(null == cacheB)
190   - allList.add(cacheB);
191   - else{
192   - cacheB.setNbbm(b.getNbbm());
193   - cacheB.setOrderNo(b.getOrderNo());
194   - cacheB.setName(b.getName());
195   - cacheB.setRegion(b.getRegion());
196   - cacheB.setUpdateDate(b.getUpdateDate());
197   - cacheB.setRemark(b.getRemark());
198   - }
199   - }*/
200 192 }
... ...
src/main/java/com/bsth/util/ConvertUtil.java 0 → 100644
  1 +package com.bsth.util;
  2 +
  3 +import com.google.common.collect.ArrayListMultimap;
  4 +import org.joda.time.format.DateTimeFormat;
  5 +import org.joda.time.format.DateTimeFormatter;
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +
  9 +import java.lang.reflect.Field;
  10 +import java.util.*;
  11 +
  12 +/**
  13 + * 数据转换
  14 + * Created by panzhao on 2017/3/13.
  15 + */
  16 +public class ConvertUtil<T> {
  17 +
  18 + Logger logger = LoggerFactory.getLogger(this.getClass());
  19 + private static DateTimeFormatter fmtyyyyMMdd = DateTimeFormat.forPattern("yyyyMMdd");
  20 +
  21 + /**
  22 + * 根据指定字段 将 list 分组
  23 + *
  24 + * @param list
  25 + * @param separator 字段使用分隔符连接 组成key
  26 + * @param fields
  27 + * @return
  28 + */
  29 + public ArrayListMultimap<String, T> groupMultiList(List<T> list, String separator, Field... fields) {
  30 + ArrayListMultimap<String, T> multimap = ArrayListMultimap.create();
  31 +
  32 + String key;
  33 + //Object field;
  34 + try {
  35 + for (T t : list) {
  36 +
  37 + key = "";
  38 + for (Field f : fields) {
  39 + f.setAccessible(true);
  40 + if(null == f.get(t))
  41 + continue;
  42 + //日期类型格式化为 YYYY-MM-DD
  43 + if (f.getType().equals(Date.class))
  44 + key += (separator + fmtyyyyMMdd.print(((Date) f.get(t)).getTime()));
  45 + else
  46 + key += (separator + f.get(t).toString());
  47 + }
  48 + if(key.length() > 1)
  49 + key = key.substring(1);
  50 +
  51 + multimap.put(key, t);
  52 + }
  53 + } catch (Exception e) {
  54 + logger.error("", e);
  55 + }
  56 +
  57 + return multimap;
  58 + }
  59 +
  60 + /**
  61 + * 根据指定字段 将 list 分组
  62 + *
  63 + * @param list
  64 + * @param separator 字段使用分隔符连接 组成key
  65 + * @param fields
  66 + * @return
  67 + */
  68 + public Map<String, T> groupList(List<T> list, String separator, Field... fields) {
  69 + Map<String, T> map = new HashMap<>();
  70 +
  71 + String key;
  72 + //Object field;
  73 + try {
  74 + for (T t : list) {
  75 +
  76 + key = "";
  77 + for (Field f : fields) {
  78 + f.setAccessible(true);
  79 + //日期类型格式化为 YYYY-MM-DD
  80 + if (f.getType().equals(Date.class))
  81 + key += (separator + fmtyyyyMMdd.print(((Date) f.get(t)).getTime()));
  82 + else
  83 + key += (separator + f.get(t).toString());
  84 + }
  85 + key = key.substring(1);
  86 +
  87 + map.put(key, t);
  88 + }
  89 + } catch (Exception e) {
  90 + logger.error("", e);
  91 + }
  92 +
  93 + return map;
  94 + }
  95 +
  96 + /**
  97 + * 计算并集
  98 + *
  99 + * @param all
  100 + * @param sub
  101 + * @return
  102 + */
  103 + public List<String> calcUnion(Collection<String> all, Collection<String> sub) {
  104 + List<String> rs = new ArrayList<>();
  105 +
  106 + for (String str : all) {
  107 + if (sub.contains(str))
  108 + rs.add(str);
  109 + }
  110 + return rs;
  111 + }
  112 +
  113 + /**
  114 + * 计算补集
  115 + *
  116 + * @param all
  117 + * @param sub
  118 + * @return
  119 + */
  120 + public List<String> calcComplement(Collection<String> all, Collection<String> sub) {
  121 + List<String> rs = new ArrayList<>();
  122 +
  123 + for (String str : all) {
  124 + if (!sub.contains(str))
  125 + rs.add(str);
  126 + }
  127 + return rs;
  128 + }
  129 +}
... ...