Commit 4d12176a2ccf089c3b2a178e0e59950c1d864fa1

Authored by 王通
2 parents a35800c6 201b3279

1.电科里程数据加入公司和公司代码

src/main/java/com/bsth/server_rs/dks/BusMileage.java
1   -package com.bsth.server_rs.dks;
2   -
3   -public class BusMileage {
4   -
5   - /**
6   - *
7   - */
8   - private String scheduleDate;
9   -
10   - /**
11   - * 内部编码
12   - */
13   - private String nbbm;
14   -
15   - /**
16   - * 计划里程
17   - */
18   - private Double jhlc;
19   -
20   - /**
21   - * 实际计划里程
22   - */
23   - private Double sjjhlc;
24   -
25   - /**
26   - * 实际出场里程
27   - */
28   - private Double sjcclc;
29   -
30   - /**
31   - * 实际进场里程
32   - */
33   - private Double sjjclc;
34   -
35   - /**
36   - * 营运里程
37   - */
38   - private Double yylc;
39   -
40   - /**
41   - * 空驶里程
42   - */
43   - private Double kslc;
44   -
45   - /**
46   - * 抽减里程
47   - */
48   - private Double cjlc;
49   -
50   - /**
51   - * 烂班里程
52   - */
53   - private Double lblc;
54   -
55   - /**
56   - * 增加里程
57   - */
58   - private Double zjlc;
59   -
60   - /**
61   - * 总里程
62   - */
63   - private Double zlc;
64   -
65   - /**
66   - * 油耗
67   - */
68   - private Double yh;
69   -
70   - /**
71   - * 电耗
72   - */
73   - private Double dh;
74   -
75   - public String getScheduleDate() {
76   - return scheduleDate;
77   - }
78   -
79   - public void setScheduleDate(String scheduleDate) {
80   - this.scheduleDate = scheduleDate;
81   - }
82   -
83   - public String getNbbm() {
84   - return nbbm;
85   - }
86   -
87   - public void setNbbm(String nbbm) {
88   - this.nbbm = nbbm;
89   - }
90   -
91   - public Double getJhlc() {
92   - return jhlc;
93   - }
94   -
95   - public void setJhlc(Double jhlc) {
96   - this.jhlc = jhlc;
97   - }
98   -
99   - public Double getSjjhlc() {
100   - return sjjhlc;
101   - }
102   -
103   - public void setSjjhlc(Double sjjhlc) {
104   - this.sjjhlc = sjjhlc;
105   - }
106   -
107   - public Double getSjcclc() {
108   - return sjcclc;
109   - }
110   -
111   - public void setSjcclc(Double sjcclc) {
112   - this.sjcclc = sjcclc;
113   - }
114   -
115   - public Double getSjjclc() {
116   - return sjjclc;
117   - }
118   -
119   - public void setSjjclc(Double sjjclc) {
120   - this.sjjclc = sjjclc;
121   - }
122   -
123   - public Double getYylc() {
124   - return yylc;
125   - }
126   -
127   - public void setYylc(Double yylc) {
128   - this.yylc = yylc;
129   - }
130   -
131   - public Double getKslc() {
132   - return kslc;
133   - }
134   -
135   - public void setKslc(Double kslc) {
136   - this.kslc = kslc;
137   - }
138   -
139   - public Double getCjlc() {
140   - return cjlc;
141   - }
142   -
143   - public void setCjlc(Double cjlc) {
144   - this.cjlc = cjlc;
145   - }
146   -
147   - public Double getLblc() {
148   - return lblc;
149   - }
150   -
151   - public void setLblc(Double lblc) {
152   - this.lblc = lblc;
153   - }
154   -
155   - public Double getZjlc() {
156   - return zjlc;
157   - }
158   -
159   - public void setZjlc(Double zjlc) {
160   - this.zjlc = zjlc;
161   - }
162   -
163   - public Double getZlc() {
164   - return zlc;
165   - }
166   -
167   - public void setZlc(Double zlc) {
168   - this.zlc = zlc;
169   - }
170   -
171   - public Double getYh() {
172   - return yh;
173   - }
174   -
175   - public void setYh(Double yh) {
176   - this.yh = yh;
177   - }
178   -
179   - public Double getDh() {
180   - return dh;
181   - }
182   -
183   - public void setDh(Double dh) {
184   - this.dh = dh;
185   - }
186   -}
  1 +package com.bsth.server_rs.dks;
  2 +
  3 +public class BusMileage {
  4 +
  5 + /**
  6 + *
  7 + */
  8 + private String scheduleDate;
  9 +
  10 + /**
  11 + * 内部编码
  12 + */
  13 + private String nbbm;
  14 +
  15 + /**
  16 + * 计划里程
  17 + */
  18 + private Double jhlc;
  19 +
  20 + /**
  21 + * 实际计划里程
  22 + */
  23 + private Double sjjhlc;
  24 +
  25 + /**
  26 + * 实际出场里程
  27 + */
  28 + private Double sjcclc;
  29 +
  30 + /**
  31 + * 实际进场里程
  32 + */
  33 + private Double sjjclc;
  34 +
  35 + /**
  36 + * 营运里程
  37 + */
  38 + private Double yylc;
  39 +
  40 + /**
  41 + * 空驶里程
  42 + */
  43 + private Double kslc;
  44 +
  45 + /**
  46 + * 抽减里程
  47 + */
  48 + private Double cjlc;
  49 +
  50 + /**
  51 + * 烂班里程
  52 + */
  53 + private Double lblc;
  54 +
  55 + /**
  56 + * 增加里程
  57 + */
  58 + private Double zjlc;
  59 +
  60 + /**
  61 + * 总里程
  62 + */
  63 + private Double zlc;
  64 +
  65 + /**
  66 + * 油耗
  67 + */
  68 + private Double yh;
  69 +
  70 + /**
  71 + * 电耗
  72 + */
  73 + private Double dh;
  74 +
  75 + private String company;
  76 +
  77 + private String companyCode;
  78 +
  79 + public String getScheduleDate() {
  80 + return scheduleDate;
  81 + }
  82 +
  83 + public void setScheduleDate(String scheduleDate) {
  84 + this.scheduleDate = scheduleDate;
  85 + }
  86 +
  87 + public String getNbbm() {
  88 + return nbbm;
  89 + }
  90 +
  91 + public void setNbbm(String nbbm) {
  92 + this.nbbm = nbbm;
  93 + }
  94 +
  95 + public Double getJhlc() {
  96 + return jhlc;
  97 + }
  98 +
  99 + public void setJhlc(Double jhlc) {
  100 + this.jhlc = jhlc;
  101 + }
  102 +
  103 + public Double getSjjhlc() {
  104 + return sjjhlc;
  105 + }
  106 +
  107 + public void setSjjhlc(Double sjjhlc) {
  108 + this.sjjhlc = sjjhlc;
  109 + }
  110 +
  111 + public Double getSjcclc() {
  112 + return sjcclc;
  113 + }
  114 +
  115 + public void setSjcclc(Double sjcclc) {
  116 + this.sjcclc = sjcclc;
  117 + }
  118 +
  119 + public Double getSjjclc() {
  120 + return sjjclc;
  121 + }
  122 +
  123 + public void setSjjclc(Double sjjclc) {
  124 + this.sjjclc = sjjclc;
  125 + }
  126 +
  127 + public Double getYylc() {
  128 + return yylc;
  129 + }
  130 +
  131 + public void setYylc(Double yylc) {
  132 + this.yylc = yylc;
  133 + }
  134 +
  135 + public Double getKslc() {
  136 + return kslc;
  137 + }
  138 +
  139 + public void setKslc(Double kslc) {
  140 + this.kslc = kslc;
  141 + }
  142 +
  143 + public Double getCjlc() {
  144 + return cjlc;
  145 + }
  146 +
  147 + public void setCjlc(Double cjlc) {
  148 + this.cjlc = cjlc;
  149 + }
  150 +
  151 + public Double getLblc() {
  152 + return lblc;
  153 + }
  154 +
  155 + public void setLblc(Double lblc) {
  156 + this.lblc = lblc;
  157 + }
  158 +
  159 + public Double getZjlc() {
  160 + return zjlc;
  161 + }
  162 +
  163 + public void setZjlc(Double zjlc) {
  164 + this.zjlc = zjlc;
  165 + }
  166 +
  167 + public Double getZlc() {
  168 + return zlc;
  169 + }
  170 +
  171 + public void setZlc(Double zlc) {
  172 + this.zlc = zlc;
  173 + }
  174 +
  175 + public Double getYh() {
  176 + return yh;
  177 + }
  178 +
  179 + public void setYh(Double yh) {
  180 + this.yh = yh;
  181 + }
  182 +
  183 + public Double getDh() {
  184 + return dh;
  185 + }
  186 +
  187 + public void setDh(Double dh) {
  188 + this.dh = dh;
  189 + }
  190 +
  191 + public String getCompany() {
  192 + return company;
  193 + }
  194 +
  195 + public void setCompany(String company) {
  196 + this.company = company;
  197 + }
  198 +
  199 + public String getCompanyCode() {
  200 + return companyCode;
  201 + }
  202 +
  203 + public void setCompanyCode(String companyCode) {
  204 + this.companyCode = companyCode;
  205 + }
  206 +}
... ...
src/main/java/com/bsth/server_rs/dks/DksRestService.java
1   -package com.bsth.server_rs.dks;
2   -
3   -import com.alibaba.fastjson.JSON;
4   -import com.bsth.entity.ElecInfo;
5   -import com.bsth.entity.OilInfo;
6   -import com.bsth.entity.SchedulePlanInfo;
7   -import com.bsth.entity.ScheduleRealInfo;
8   -import com.bsth.redis.ElecRedisService;
9   -import com.bsth.redis.OilRedisService;
10   -import com.bsth.redis.ScheduleRedisService;
11   -import com.bsth.repository.SchedulePlanInfoRepository;
12   -import com.bsth.repository.ScheduleRealInfoRepository;
13   -import com.bsth.server_rs.base_info.line.Line;
14   -import com.bsth.server_rs.base_info.line.buffer.LineBufferData;
15   -import com.bsth.server_ws.util.ScheduleCalculator;
16   -import com.bsth.util.Arith;
17   -import com.google.common.collect.ArrayListMultimap;
18   -import org.joda.time.DateTime;
19   -import org.joda.time.format.DateTimeFormat;
20   -import org.slf4j.Logger;
21   -import org.slf4j.LoggerFactory;
22   -import org.springframework.beans.factory.annotation.Autowired;
23   -import org.springframework.jdbc.core.BatchPreparedStatementSetter;
24   -import org.springframework.jdbc.core.BeanPropertyRowMapper;
25   -import org.springframework.jdbc.core.JdbcTemplate;
26   -import org.springframework.jdbc.datasource.DataSourceTransactionManager;
27   -import org.springframework.scheduling.annotation.EnableScheduling;
28   -import org.springframework.scheduling.annotation.Scheduled;
29   -import org.springframework.stereotype.Component;
30   -import org.springframework.transaction.TransactionDefinition;
31   -import org.springframework.transaction.TransactionStatus;
32   -import org.springframework.transaction.support.DefaultTransactionDefinition;
33   -import org.springframework.util.StringUtils;
34   -
35   -import javax.ws.rs.*;
36   -import javax.ws.rs.core.MediaType;
37   -import java.sql.PreparedStatement;
38   -import java.sql.SQLException;
39   -import java.util.*;
40   -
41   -/**
42   - * @author Hill
43   - * @date 2021-09
44   - */
45   -@Component
46   -@EnableScheduling
47   -@Path("/dks")
48   -@Produces({MediaType.APPLICATION_JSON})
49   -public class DksRestService {
50   -
51   - private final static Logger log = LoggerFactory.getLogger(DksRestService.class);
52   -
53   - @Autowired
54   - private JdbcTemplate jdbcTemplate;
55   -
56   - @Autowired
57   - private ScheduleRealInfoRepository scheduleRealInfoRepository;
58   -
59   - @Autowired
60   - private SchedulePlanInfoRepository schedulePlanInfoRepository;
61   -
62   - @Autowired
63   - private ScheduleRedisService scheduleRedisService;
64   -
65   - @Autowired
66   - private OilRedisService oilRedisService;
67   -
68   - @Autowired
69   - private ElecRedisService elecRedisService;
70   -
71   - /**
72   - * 根据日期统计数据重新计算
73   - * @param rq
74   - */
75   - @GET
76   - @Path("/reCompute/{rq}")
77   - public void reStatisticalComputation(@PathParam("rq") String rq) {
78   - DateTime dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(rq);
79   - planComputation(dateTime);
80   - realComputation(dateTime);
81   - carMileageComputation(dateTime);
82   - }
83   -
84   - /**
85   - *
86   - */
87   - private List<LineServiceConfigVo> reGroup(List<LineServiceConfig> configs, String month) {
88   - List<LineServiceConfigVo> result = new ArrayList<>();
89   - String oldLineCode = "";
90   - LineServiceConfigVo vo = null;
91   - for (int i = 0, len = configs.size();i < len;i++) {
92   - LineServiceConfig config = configs.get(i);
93   - if (i == 0) {
94   - oldLineCode = config.getLineCode();
95   - vo = new LineServiceConfigVo();
96   - vo.setLineName(config.getLineName());
97   - vo.setMonth(month);
98   - vo.setConductorFirstCount(config.getConductorCount());
99   - vo.setDriverFirstCount(config.getDriverCount());
100   - } else if (i == len - 1) {
101   - result.add(vo);
102   - } else {
103   - if (!oldLineCode.equals(config.getLineCode())) {
104   - oldLineCode = config.getLineCode();
105   - result.add(vo);
106   - vo = new LineServiceConfigVo();
107   - vo.setLineName(config.getLineName());
108   - vo.setMonth(month);
109   - vo.setConductorFirstCount(config.getConductorCount());
110   - vo.setDriverFirstCount(config.getDriverCount());
111   - }
112   - }
113   - vo.setConductorLastCount(config.getConductorCount());
114   - vo.setDriverLastCount(config.getDriverCount());
115   - }
116   -
117   - return result;
118   - }
119   -
120   - /**
121   - * 计划司售配档数
122   - * @param month
123   - */
124   - @GET
125   - @Path("/plan/{month}")
126   - public List<LineServiceConfigVo> plan(@PathParam("month") String month) {
127   - List<LineServiceConfig> configs = jdbcTemplate.query("select * from control_interface.bsth_t_plan where schedule_date like CONCAT(?, '%') order by line_code, schedule_date", new Object[]{ month }, BeanPropertyRowMapper.newInstance(LineServiceConfig.class));
128   - return reGroup(configs, month);
129   - }
130   -
131   - /**
132   - * 实际司售配档数
133   - * @param month
134   - */
135   - @GET
136   - @Path("/actual/{month}")
137   - public List<LineServiceConfigVo> actual(@PathParam("month") String month) {
138   - List<LineServiceConfig> configs = jdbcTemplate.query("select * from control_interface.bsth_t_real where schedule_date like CONCAT(?, '%') order by line_code, schedule_date", new Object[]{ month }, BeanPropertyRowMapper.newInstance(LineServiceConfig.class));
139   - return reGroup(configs, month);
140   - }
141   -
142   - /**
143   - * 计划配车数
144   - * @param month
145   - */
146   - @GET
147   - @Path("/bus/plan/{month}")
148   - public List<BusVo> busPlan(@PathParam("month") String month) {
149   - List<LineServiceConfig> configs = jdbcTemplate.query("select * from control_interface.bsth_t_plan where schedule_date like CONCAT(?, '%') order by line_code, schedule_date", new Object[]{ month }, BeanPropertyRowMapper.newInstance(LineServiceConfig.class));
150   - List<BusVo> result = new ArrayList<>();
151   - String oldLineCode = "";
152   - BusVo vo = null;
153   - int max = 0;
154   - for (int i = 0, len = configs.size();i < len;i++) {
155   - LineServiceConfig config = configs.get(i);
156   - if (i == 0) {
157   - oldLineCode = config.getLineCode();
158   - vo = new BusVo();
159   - vo.setLineName(config.getLineName());
160   - vo.setMonth(month);
161   - vo.setBusFirstCount(config.getCarCount());
162   - } else if (!oldLineCode.equals(config.getLineCode())) {
163   - oldLineCode = config.getLineCode();
164   - vo.setBusMaxCount(max);
165   - result.add(vo);
166   - max = 0;
167   - vo = new BusVo();
168   - vo.setLineName(config.getLineName());
169   - vo.setMonth(month);
170   - vo.setBusFirstCount(config.getCarCount());
171   - }
172   - max = Math.max(max, config.getCarCount());
173   - if (i == len - 1) {
174   - vo.setBusMaxCount(max);
175   - result.add(vo);
176   - }
177   - vo.setBusLastCount(config.getCarCount());
178   - }
179   -
180   - return result;
181   - }
182   -
183   - /**
184   - * 实际配车数
185   - * @param month
186   - */
187   - @GET
188   - @Path("/bus/{month}")
189   - public List<BusVo> bus(@PathParam("month") String month) {
190   - List<LineServiceConfig> configs = jdbcTemplate.query("select * from control_interface.bsth_t_real where schedule_date like CONCAT(?, '%') order by line_code, schedule_date", new Object[]{ month }, BeanPropertyRowMapper.newInstance(LineServiceConfig.class));
191   - List<BusVo> result = new ArrayList<>();
192   - String oldLineCode = "";
193   - BusVo vo = null;
194   - int max = 0;
195   - for (int i = 0, len = configs.size();i < len;i++) {
196   - LineServiceConfig config = configs.get(i);
197   - if (i == 0) {
198   - oldLineCode = config.getLineCode();
199   - vo = new BusVo();
200   - vo.setLineName(config.getLineName());
201   - vo.setMonth(month);
202   - vo.setBusFirstCount(config.getCarCount());
203   - } else if (!oldLineCode.equals(config.getLineCode())) {
204   - oldLineCode = config.getLineCode();
205   - vo.setBusMaxCount(max);
206   - result.add(vo);
207   - max = 0;
208   - vo = new BusVo();
209   - vo.setLineName(config.getLineName());
210   - vo.setMonth(month);
211   - vo.setBusFirstCount(config.getCarCount());
212   - }
213   - max = Math.max(max, config.getCarCount());
214   - if (i == len - 1) {
215   - vo.setBusMaxCount(max);
216   - result.add(vo);
217   - }
218   - vo.setBusLastCount(config.getCarCount());
219   - }
220   -
221   - return result;
222   - }
223   -
224   - /**
225   - * 车辆公里、油耗数据
226   - * @param month
227   - */
228   - @GET
229   - @Path("/mileage/{month}")
230   - public List<BusMileageVo> mileage(@PathParam("month") String month) {
231   - List<BusMileage> mileages = jdbcTemplate.query("select nbbm, schedule_month schedule_date, sum(zlc) zlc, sum(yh) yh, sum(dh) dh from control_interface.bsth_t_mileage where schedule_month = ? group by nbbm, schedule_month", new Object[]{ month }, BeanPropertyRowMapper.newInstance(BusMileage.class));
232   - List<BusMileageVo> result = new ArrayList<>();
233   - for (BusMileage mileage : mileages) {
234   - BusMileageVo vo = new BusMileageVo();
235   - vo.setMonth(mileage.getScheduleDate());
236   - vo.setInsideCode(mileage.getNbbm());
237   - vo.setTotalMileage(mileage.getZlc());
238   - vo.setOilConsume(mileage.getYh());
239   - vo.setElectricConsume(mileage.getDh());
240   -
241   - result.add(vo);
242   - }
243   -
244   - return result;
245   - }
246   -
247   - @Scheduled(cron = "0 0/25 23 * * ?")
248   - public void statisticalComputation() {
249   - log.info("执行电科所接口数据统计计算");
250   - DateTime dateTime = DateTime.now().plusDays(1);
251   - try {
252   - for (int i = 0;i < 3;i++) {
253   - dateTime = dateTime.minusDays(1);
254   - planComputation(dateTime);
255   - realComputation(dateTime);
256   - carMileageComputation(dateTime);
257   - }
258   - } catch (Exception e) {
259   - e.printStackTrace();
260   - }
261   - }
262   -
263   - /**
264   - * 计划排班中的线路司售配档数
265   - */
266   - private void planComputation(DateTime dateTime) {
267   - // 记录实际线路司售配档情况
268   - List<SchedulePlanInfo> schedulePlanInfos = schedulePlanInfoRepository.findByDate(DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(dateTime.toString("yyyy-MM-dd")).toDate());
269   - Set<String> lineCodes = new HashSet<>();
270   - Map<String, Set<String>> line2conductors = new HashMap<>(), line2drivers = new HashMap<>(), line2cars = new HashMap<>();
271   - final List<LineServiceConfig> objects = new ArrayList<>();
272   - for (SchedulePlanInfo schedulePlanInfo : schedulePlanInfos) {
273   - String lineCode = schedulePlanInfo.getXlBm();
274   - lineCodes.add(lineCode);
275   - Set<String> conductors = line2conductors.get(lineCode), drivers = line2drivers.get(lineCode), cars = line2cars.get(lineCode);
276   - if (conductors == null) {
277   - conductors = new HashSet<>();
278   - line2conductors.put(lineCode, conductors);
279   - }
280   - if (drivers == null) {
281   - drivers = new HashSet<>();
282   - line2drivers.put(lineCode, drivers);
283   - }
284   - if (cars == null) {
285   - cars = new HashSet<>();
286   - line2cars.put(lineCode, cars);
287   - }
288   - if (!StringUtils.isEmpty(schedulePlanInfo.getsGh())) {
289   - conductors.add(schedulePlanInfo.getsGh());
290   - }
291   - drivers.add(schedulePlanInfo.getjGh());
292   - cars.add(schedulePlanInfo.getClZbh());
293   - }
294   - for (String lineCode : lineCodes) {
295   - LineServiceConfig object = new LineServiceConfig();
296   - Line line = LineBufferData.findOne(lineCode);
297   - object.setLineCode(lineCode);
298   - object.setLineName(line == null ? "" : line.getName());
299   - object.setScheduleDate(dateTime.toString("yyyy-MM-dd"));
300   - object.setConductorCount(line2conductors.get(lineCode).size());
301   - object.setDriverCount(line2drivers.get(lineCode).size());
302   - object.setCarCount(line2cars.get(lineCode).size());
303   -
304   - objects.add(object);
305   - }
306   -
307   - //编程式事务
308   - DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
309   - DefaultTransactionDefinition def = new DefaultTransactionDefinition();
310   - def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
311   - TransactionStatus status = tran.getTransaction(def);
312   -
313   - try {
314   - jdbcTemplate.update("delete from control_interface.bsth_t_plan where schedule_date = ?", new Object[]{ dateTime.toString("yyyy-MM-dd") });
315   - jdbcTemplate.batchUpdate("insert into control_interface.bsth_t_plan (line_code, line_name, schedule_date, conductor_count, driver_count, car_count) values (?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
316   - @Override
317   - public void setValues(PreparedStatement ps, int i) throws SQLException {
318   - LineServiceConfig object = objects.get(i);
319   - ps.setString(1, object.getLineCode());
320   - ps.setString(2, object.getLineName());
321   - ps.setString(3, object.getScheduleDate());
322   - ps.setInt(4, object.getConductorCount());
323   - ps.setInt(5, object.getDriverCount());
324   - ps.setInt(6, object.getCarCount());
325   - }
326   -
327   - @Override
328   - public int getBatchSize() {
329   - return objects.size();
330   - }
331   - });
332   - tran.commit(status);
333   - } catch (Exception e) {
334   - tran.rollback(status);
335   - }
336   - }
337   -
338   - /**
339   - * 实际排班中的线路司售配档数
340   - */
341   - private void realComputation(DateTime dateTime) {
342   - // 记录实际线路司售配档情况
343   - List<ScheduleRealInfo> scheduleRealInfos = scheduleRealInfoRepository.findAll(dateTime.toString("yyyy-MM-dd"));
344   - Set<String> lineCodes = new HashSet<>();
345   - Map<String, Set<String>> line2conductors = new HashMap<>(), line2drivers = new HashMap<>(), line2cars = new HashMap<>();
346   - final List<LineServiceConfig> objects = new ArrayList<>();
347   - for (ScheduleRealInfo scheduleRealInfo : scheduleRealInfos) {
348   - // 烂班班次不计入配档数
349   - if (scheduleRealInfo.getStatus() == -1) { continue; }
350   - String lineCode = scheduleRealInfo.getXlBm();
351   - lineCodes.add(lineCode);
352   - Set<String> conductors = line2conductors.get(lineCode), drivers = line2drivers.get(lineCode), cars = line2cars.get(lineCode);
353   - if (conductors == null) {
354   - conductors = new HashSet<>();
355   - line2conductors.put(lineCode, conductors);
356   - }
357   - if (drivers == null) {
358   - drivers = new HashSet<>();
359   - line2drivers.put(lineCode, drivers);
360   - }
361   - if (cars == null) {
362   - cars = new HashSet<>();
363   - line2cars.put(lineCode, cars);
364   - }
365   - if (!StringUtils.isEmpty(scheduleRealInfo.getsGh())) {
366   - conductors.add(scheduleRealInfo.getsGh());
367   - }
368   - drivers.add(scheduleRealInfo.getjGh());
369   - cars.add(scheduleRealInfo.getClZbh());
370   - }
371   - for (String lineCode : lineCodes) {
372   - LineServiceConfig object = new LineServiceConfig();
373   - Line line = LineBufferData.findOne(lineCode);
374   - object.setLineCode(lineCode);
375   - object.setLineName(line == null ? "" : line.getName());
376   - object.setScheduleDate(dateTime.toString("yyyy-MM-dd"));
377   - object.setConductorCount(line2conductors.get(lineCode).size());
378   - object.setDriverCount(line2drivers.get(lineCode).size());
379   - object.setCarCount(line2cars.get(lineCode).size());
380   -
381   - objects.add(object);
382   - }
383   -
384   - //编程式事务
385   - DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
386   - DefaultTransactionDefinition def = new DefaultTransactionDefinition();
387   - def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
388   - TransactionStatus status = tran.getTransaction(def);
389   - try {
390   - jdbcTemplate.update("delete from control_interface.bsth_t_real where schedule_date = ?", new Object[]{ dateTime.toString("yyyy-MM-dd") });
391   - jdbcTemplate.batchUpdate("insert into control_interface.bsth_t_real (line_code, line_name, schedule_date, conductor_count, driver_count, car_count) values (?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
392   - @Override
393   - public void setValues(PreparedStatement ps, int i) throws SQLException {
394   - LineServiceConfig object = objects.get(i);
395   - ps.setString(1, object.getLineCode());
396   - ps.setString(2, object.getLineName());
397   - ps.setString(3, object.getScheduleDate());
398   - ps.setInt(4, object.getConductorCount());
399   - ps.setInt(5, object.getDriverCount());
400   - ps.setInt(6, object.getCarCount());
401   - }
402   -
403   - @Override
404   - public int getBatchSize() {
405   - return objects.size();
406   - }
407   - });
408   - tran.commit(status);
409   - } catch (Exception e) {
410   - tran.rollback(status);
411   - }
412   - }
413   -
414   - private void carMileageComputation(final DateTime dateTime) {
415   - String rq = dateTime.toString("yyyy-MM-dd");
416   - // 实际排班信息
417   - ArrayListMultimap<String, ScheduleRealInfo> nbbm2schedules = scheduleRedisService.findByDate(rq);
418   - // 油耗信息
419   - ArrayListMultimap<String, OilInfo> oilInfoMap = oilRedisService.findByNbbmGroup1(nbbm2schedules.keySet(), rq);
420   - //电耗信息
421   - ArrayListMultimap<String, ElecInfo> elecInfoMap = elecRedisService.findByNbbmGroup1(nbbm2schedules.keySet(), rq);
422   - Set<String> nbbms = nbbm2schedules.keySet();
423   - final List<BusMileage> busMileages = new ArrayList<>();
424   - for (String nbbm : nbbms) {
425   - List<ScheduleRealInfo> scheduleRealInfos = nbbm2schedules.get(nbbm);
426   - if (scheduleRealInfos.size() == 0) {
427   - continue;
428   - }
429   - BusMileage busMileage = new BusMileage();
430   - busMileage.setScheduleDate(rq);
431   - busMileage.setNbbm(nbbm);
432   - busMileage.setJhlc(ScheduleCalculator.calcJHLC(scheduleRealInfos));
433   - busMileage.setSjjhlc(ScheduleCalculator.calcSJLC(scheduleRealInfos));
434   - busMileage.setSjcclc(ScheduleCalculator.calcCCLC(scheduleRealInfos));
435   - busMileage.setSjjclc(ScheduleCalculator.calcJCLC(scheduleRealInfos));
436   - busMileage.setYylc(ScheduleCalculator.calcYYLC(scheduleRealInfos));
437   - busMileage.setKslc(ScheduleCalculator.calcKSLC(scheduleRealInfos));
438   - busMileage.setCjlc(ScheduleCalculator.calcCJLC(scheduleRealInfos));
439   - busMileage.setLblc(ScheduleCalculator.calcLBLC(scheduleRealInfos));
440   - busMileage.setZjlc(ScheduleCalculator.calcZJLC(scheduleRealInfos));
441   - busMileage.setZlc(ScheduleCalculator.calcZLC(scheduleRealInfos));
442   - List<OilInfo> oilInfos = oilInfoMap.get(nbbm);
443   - List<ElecInfo> elecInfos = elecInfoMap.get(nbbm);
444   - Double yh = 0.0, dh = 0.0;
445   - for (OilInfo oilInfo : oilInfos) {
446   - yh = Arith.add(yh, oilInfo.getYh());
447   - }
448   - for (ElecInfo elecInfo : elecInfos) {
449   - dh = Arith.add(yh, elecInfo.getHd());
450   - }
451   - busMileage.setYh(yh);
452   - busMileage.setDh(dh);
453   -
454   - busMileages.add(busMileage);
455   - }
456   -
457   - //编程式事务
458   - DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
459   - DefaultTransactionDefinition def = new DefaultTransactionDefinition();
460   - def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
461   - TransactionStatus status = tran.getTransaction(def);
462   - try {
463   - jdbcTemplate.update("delete from control_interface.bsth_t_mileage where schedule_date = ?", new Object[]{ dateTime.toString("yyyy-MM-dd") });
464   - jdbcTemplate.batchUpdate("insert into control_interface.bsth_t_mileage (schedule_date,nbbm,jhlc,sjjhlc,sjcclc,sjjclc,yylc,kslc,cjlc,lblc,zjlc,zlc,yh,dh,schedule_month) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
465   - @Override
466   - public void setValues(PreparedStatement ps, int i) throws SQLException {
467   - BusMileage busMileage = busMileages.get(i);
468   - ps.setString(1, busMileage.getScheduleDate());
469   - ps.setString(2, busMileage.getNbbm());
470   - ps.setDouble(3, busMileage.getJhlc());
471   - ps.setDouble(4, busMileage.getSjjhlc());
472   - ps.setDouble(5, busMileage.getSjcclc());
473   - ps.setDouble(6, busMileage.getSjjclc());
474   - ps.setDouble(7, busMileage.getYylc());
475   - ps.setDouble(8, busMileage.getKslc());
476   - ps.setDouble(9, busMileage.getCjlc());
477   - ps.setDouble(10, busMileage.getLblc());
478   - ps.setDouble(11, busMileage.getZjlc());
479   - ps.setDouble(12, busMileage.getZlc());
480   - ps.setDouble(13, busMileage.getYh());
481   - ps.setDouble(14, busMileage.getDh());
482   - ps.setString(15, dateTime.toString("yyyy-MM"));
483   - }
484   -
485   - @Override
486   - public int getBatchSize() {
487   - return busMileages.size();
488   - }
489   - });
490   - tran.commit(status);
491   - } catch (Exception e) {
492   - tran.rollback(status);
493   - }
494   - }
495   -
496   - /**
497   - * 线路司售配档对象
498   - */
499   - private final static class LineServiceConfigVo {
500   -
501   - private String lineName;
502   -
503   - private String month;
504   -
505   - private Integer driverFirstCount;
506   -
507   - private Integer driverLastCount;
508   -
509   - private Integer conductorFirstCount;
510   -
511   - private Integer conductorLastCount;
512   -
513   - public String getLineName() {
514   - return lineName;
515   - }
516   -
517   - public void setLineName(String lineName) {
518   - this.lineName = lineName;
519   - }
520   -
521   - public String getMonth() {
522   - return month;
523   - }
524   -
525   - public void setMonth(String month) {
526   - this.month = month;
527   - }
528   -
529   - public Integer getDriverFirstCount() {
530   - return driverFirstCount;
531   - }
532   -
533   - public void setDriverFirstCount(Integer driverFirstCount) {
534   - this.driverFirstCount = driverFirstCount;
535   - }
536   -
537   - public Integer getDriverLastCount() {
538   - return driverLastCount;
539   - }
540   -
541   - public void setDriverLastCount(Integer driverLastCount) {
542   - this.driverLastCount = driverLastCount;
543   - }
544   -
545   - public Integer getConductorFirstCount() {
546   - return conductorFirstCount;
547   - }
548   -
549   - public void setConductorFirstCount(Integer conductorFirstCount) {
550   - this.conductorFirstCount = conductorFirstCount;
551   - }
552   -
553   - public Integer getConductorLastCount() {
554   - return conductorLastCount;
555   - }
556   -
557   - public void setConductorLastCount(Integer conductorLastCount) {
558   - this.conductorLastCount = conductorLastCount;
559   - }
560   - }
561   -
562   - /**
563   - * 车辆配档对象
564   - */
565   - private final static class BusVo {
566   -
567   - private String lineName;
568   -
569   - private String month;
570   -
571   - private Integer busFirstCount;
572   -
573   - private Integer busLastCount;
574   -
575   - private Integer busMaxCount;
576   -
577   - public String getLineName() {
578   - return lineName;
579   - }
580   -
581   - public void setLineName(String lineName) {
582   - this.lineName = lineName;
583   - }
584   -
585   - public String getMonth() {
586   - return month;
587   - }
588   -
589   - public void setMonth(String month) {
590   - this.month = month;
591   - }
592   -
593   - public Integer getBusFirstCount() {
594   - return busFirstCount;
595   - }
596   -
597   - public void setBusFirstCount(Integer busFirstCount) {
598   - this.busFirstCount = busFirstCount;
599   - }
600   -
601   - public Integer getBusLastCount() {
602   - return busLastCount;
603   - }
604   -
605   - public void setBusLastCount(Integer busLastCount) {
606   - this.busLastCount = busLastCount;
607   - }
608   -
609   - public Integer getBusMaxCount() {
610   - return busMaxCount;
611   - }
612   -
613   - public void setBusMaxCount(Integer busMaxCount) {
614   - this.busMaxCount = busMaxCount;
615   - }
616   - }
617   -
618   - /**
619   - * 车辆公里、油耗对象
620   - */
621   - private final static class BusMileageVo {
622   -
623   - private String insideCode;
624   -
625   - private String month;
626   -
627   - private Double totalMileage;
628   -
629   - private Double oilConsume;
630   -
631   - private Double electricConsume;
632   -
633   - public String getInsideCode() {
634   - return insideCode;
635   - }
636   -
637   - public void setInsideCode(String insideCode) {
638   - this.insideCode = insideCode;
639   - }
640   -
641   - public String getMonth() {
642   - return month;
643   - }
644   -
645   - public void setMonth(String month) {
646   - this.month = month;
647   - }
648   -
649   - public Double getTotalMileage() {
650   - return totalMileage;
651   - }
652   -
653   - public void setTotalMileage(Double totalMileage) {
654   - this.totalMileage = totalMileage;
655   - }
656   -
657   - public Double getOilConsume() {
658   - return oilConsume;
659   - }
660   -
661   - public void setOilConsume(Double oilConsume) {
662   - this.oilConsume = oilConsume;
663   - }
664   -
665   - public Double getElectricConsume() {
666   - return electricConsume;
667   - }
668   -
669   - public void setElectricConsume(Double electricConsume) {
670   - this.electricConsume = electricConsume;
671   - }
672   - }
673   -}
  1 +package com.bsth.server_rs.dks;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.bsth.entity.ElecInfo;
  5 +import com.bsth.entity.OilInfo;
  6 +import com.bsth.entity.SchedulePlanInfo;
  7 +import com.bsth.entity.ScheduleRealInfo;
  8 +import com.bsth.redis.ElecRedisService;
  9 +import com.bsth.redis.OilRedisService;
  10 +import com.bsth.redis.ScheduleRedisService;
  11 +import com.bsth.repository.SchedulePlanInfoRepository;
  12 +import com.bsth.repository.ScheduleRealInfoRepository;
  13 +import com.bsth.server_rs.base_info.line.Line;
  14 +import com.bsth.server_rs.base_info.line.buffer.LineBufferData;
  15 +import com.bsth.server_ws.util.ScheduleCalculator;
  16 +import com.bsth.util.Arith;
  17 +import com.google.common.collect.ArrayListMultimap;
  18 +import org.joda.time.DateTime;
  19 +import org.joda.time.format.DateTimeFormat;
  20 +import org.slf4j.Logger;
  21 +import org.slf4j.LoggerFactory;
  22 +import org.springframework.beans.factory.annotation.Autowired;
  23 +import org.springframework.jdbc.core.BatchPreparedStatementSetter;
  24 +import org.springframework.jdbc.core.BeanPropertyRowMapper;
  25 +import org.springframework.jdbc.core.JdbcTemplate;
  26 +import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  27 +import org.springframework.scheduling.annotation.EnableScheduling;
  28 +import org.springframework.scheduling.annotation.Scheduled;
  29 +import org.springframework.stereotype.Component;
  30 +import org.springframework.transaction.TransactionDefinition;
  31 +import org.springframework.transaction.TransactionStatus;
  32 +import org.springframework.transaction.support.DefaultTransactionDefinition;
  33 +import org.springframework.util.StringUtils;
  34 +
  35 +import javax.ws.rs.*;
  36 +import javax.ws.rs.core.MediaType;
  37 +import java.sql.PreparedStatement;
  38 +import java.sql.SQLException;
  39 +import java.util.*;
  40 +
  41 +/**
  42 + * @author Hill
  43 + * @date 2021-09
  44 + */
  45 +@Component
  46 +@EnableScheduling
  47 +@Path("/dks")
  48 +@Produces({MediaType.APPLICATION_JSON})
  49 +public class DksRestService {
  50 +
  51 + private final static Logger log = LoggerFactory.getLogger(DksRestService.class);
  52 +
  53 + @Autowired
  54 + private JdbcTemplate jdbcTemplate;
  55 +
  56 + @Autowired
  57 + private ScheduleRealInfoRepository scheduleRealInfoRepository;
  58 +
  59 + @Autowired
  60 + private SchedulePlanInfoRepository schedulePlanInfoRepository;
  61 +
  62 + @Autowired
  63 + private ScheduleRedisService scheduleRedisService;
  64 +
  65 + @Autowired
  66 + private OilRedisService oilRedisService;
  67 +
  68 + @Autowired
  69 + private ElecRedisService elecRedisService;
  70 +
  71 + /**
  72 + * 根据日期统计数据重新计算
  73 + * @param rq
  74 + */
  75 + @GET
  76 + @Path("/reCompute/{rq}")
  77 + public void reStatisticalComputation(@PathParam("rq") String rq) {
  78 + DateTime dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(rq);
  79 + planComputation(dateTime);
  80 + realComputation(dateTime);
  81 + carMileageComputation(dateTime);
  82 + }
  83 +
  84 + /**
  85 + *
  86 + */
  87 + private List<LineServiceConfigVo> reGroup(List<LineServiceConfig> configs, String month) {
  88 + List<LineServiceConfigVo> result = new ArrayList<>();
  89 + String oldLineCode = "";
  90 + LineServiceConfigVo vo = null;
  91 + for (int i = 0, len = configs.size();i < len;i++) {
  92 + LineServiceConfig config = configs.get(i);
  93 + if (i == 0) {
  94 + oldLineCode = config.getLineCode();
  95 + vo = new LineServiceConfigVo();
  96 + vo.setLineName(config.getLineName());
  97 + vo.setMonth(month);
  98 + vo.setConductorFirstCount(config.getConductorCount());
  99 + vo.setDriverFirstCount(config.getDriverCount());
  100 + } else if (i == len - 1) {
  101 + result.add(vo);
  102 + } else {
  103 + if (!oldLineCode.equals(config.getLineCode())) {
  104 + oldLineCode = config.getLineCode();
  105 + result.add(vo);
  106 + vo = new LineServiceConfigVo();
  107 + vo.setLineName(config.getLineName());
  108 + vo.setMonth(month);
  109 + vo.setConductorFirstCount(config.getConductorCount());
  110 + vo.setDriverFirstCount(config.getDriverCount());
  111 + }
  112 + }
  113 + vo.setConductorLastCount(config.getConductorCount());
  114 + vo.setDriverLastCount(config.getDriverCount());
  115 + }
  116 +
  117 + return result;
  118 + }
  119 +
  120 + /**
  121 + * 计划司售配档数
  122 + * @param month
  123 + */
  124 + @GET
  125 + @Path("/plan/{month}")
  126 + public List<LineServiceConfigVo> plan(@PathParam("month") String month) {
  127 + List<LineServiceConfig> configs = jdbcTemplate.query("select * from control_interface.bsth_t_plan where schedule_date like CONCAT(?, '%') order by line_code, schedule_date", new Object[]{ month }, BeanPropertyRowMapper.newInstance(LineServiceConfig.class));
  128 + return reGroup(configs, month);
  129 + }
  130 +
  131 + /**
  132 + * 实际司售配档数
  133 + * @param month
  134 + */
  135 + @GET
  136 + @Path("/actual/{month}")
  137 + public List<LineServiceConfigVo> actual(@PathParam("month") String month) {
  138 + List<LineServiceConfig> configs = jdbcTemplate.query("select * from control_interface.bsth_t_real where schedule_date like CONCAT(?, '%') order by line_code, schedule_date", new Object[]{ month }, BeanPropertyRowMapper.newInstance(LineServiceConfig.class));
  139 + return reGroup(configs, month);
  140 + }
  141 +
  142 + /**
  143 + * 计划配车数
  144 + * @param month
  145 + */
  146 + @GET
  147 + @Path("/bus/plan/{month}")
  148 + public List<BusVo> busPlan(@PathParam("month") String month) {
  149 + List<LineServiceConfig> configs = jdbcTemplate.query("select * from control_interface.bsth_t_plan where schedule_date like CONCAT(?, '%') order by line_code, schedule_date", new Object[]{ month }, BeanPropertyRowMapper.newInstance(LineServiceConfig.class));
  150 + List<BusVo> result = new ArrayList<>();
  151 + String oldLineCode = "";
  152 + BusVo vo = null;
  153 + int max = 0;
  154 + for (int i = 0, len = configs.size();i < len;i++) {
  155 + LineServiceConfig config = configs.get(i);
  156 + if (i == 0) {
  157 + oldLineCode = config.getLineCode();
  158 + vo = new BusVo();
  159 + vo.setLineName(config.getLineName());
  160 + vo.setMonth(month);
  161 + vo.setBusFirstCount(config.getCarCount());
  162 + } else if (!oldLineCode.equals(config.getLineCode())) {
  163 + oldLineCode = config.getLineCode();
  164 + vo.setBusMaxCount(max);
  165 + result.add(vo);
  166 + max = 0;
  167 + vo = new BusVo();
  168 + vo.setLineName(config.getLineName());
  169 + vo.setMonth(month);
  170 + vo.setBusFirstCount(config.getCarCount());
  171 + }
  172 + max = Math.max(max, config.getCarCount());
  173 + if (i == len - 1) {
  174 + vo.setBusMaxCount(max);
  175 + result.add(vo);
  176 + }
  177 + vo.setBusLastCount(config.getCarCount());
  178 + }
  179 +
  180 + return result;
  181 + }
  182 +
  183 + /**
  184 + * 实际配车数
  185 + * @param month
  186 + */
  187 + @GET
  188 + @Path("/bus/{month}")
  189 + public List<BusVo> bus(@PathParam("month") String month) {
  190 + List<LineServiceConfig> configs = jdbcTemplate.query("select * from control_interface.bsth_t_real where schedule_date like CONCAT(?, '%') order by line_code, schedule_date", new Object[]{ month }, BeanPropertyRowMapper.newInstance(LineServiceConfig.class));
  191 + List<BusVo> result = new ArrayList<>();
  192 + String oldLineCode = "";
  193 + BusVo vo = null;
  194 + int max = 0;
  195 + for (int i = 0, len = configs.size();i < len;i++) {
  196 + LineServiceConfig config = configs.get(i);
  197 + if (i == 0) {
  198 + oldLineCode = config.getLineCode();
  199 + vo = new BusVo();
  200 + vo.setLineName(config.getLineName());
  201 + vo.setMonth(month);
  202 + vo.setBusFirstCount(config.getCarCount());
  203 + } else if (!oldLineCode.equals(config.getLineCode())) {
  204 + oldLineCode = config.getLineCode();
  205 + vo.setBusMaxCount(max);
  206 + result.add(vo);
  207 + max = 0;
  208 + vo = new BusVo();
  209 + vo.setLineName(config.getLineName());
  210 + vo.setMonth(month);
  211 + vo.setBusFirstCount(config.getCarCount());
  212 + }
  213 + max = Math.max(max, config.getCarCount());
  214 + if (i == len - 1) {
  215 + vo.setBusMaxCount(max);
  216 + result.add(vo);
  217 + }
  218 + vo.setBusLastCount(config.getCarCount());
  219 + }
  220 +
  221 + return result;
  222 + }
  223 +
  224 + /**
  225 + * 车辆公里、油耗数据
  226 + * @param month
  227 + */
  228 + @GET
  229 + @Path("/mileage/{month}")
  230 + public List<BusMileageVo> mileage(@PathParam("month") String month) {
  231 + List<BusMileage> mileages = jdbcTemplate.query("select company, company_code, nbbm, schedule_month schedule_date, sum(zlc) zlc, sum(yh) yh, sum(dh) dh from control_interface.bsth_t_mileage where schedule_month = ? group by company, company_code, nbbm, schedule_month", new Object[]{ month }, BeanPropertyRowMapper.newInstance(BusMileage.class));
  232 + List<BusMileageVo> result = new ArrayList<>();
  233 + for (BusMileage mileage : mileages) {
  234 + BusMileageVo vo = new BusMileageVo();
  235 + vo.setMonth(mileage.getScheduleDate());
  236 + vo.setInsideCode(mileage.getNbbm());
  237 + vo.setTotalMileage(mileage.getZlc());
  238 + vo.setOilConsume(mileage.getYh());
  239 + vo.setElectricConsume(mileage.getDh());
  240 + vo.setCompany(mileage.getCompany());
  241 + vo.setCompanyCode(mileage.getCompanyCode());
  242 +
  243 + result.add(vo);
  244 + }
  245 +
  246 + return result;
  247 + }
  248 +
  249 + @Scheduled(cron = "0 0/25 23 * * ?")
  250 + public void statisticalComputation() {
  251 + log.info("执行电科所接口数据统计计算");
  252 + DateTime dateTime = DateTime.now().plusDays(1);
  253 + try {
  254 + for (int i = 0;i < 3;i++) {
  255 + dateTime = dateTime.minusDays(1);
  256 + planComputation(dateTime);
  257 + realComputation(dateTime);
  258 + carMileageComputation(dateTime);
  259 + }
  260 + } catch (Exception e) {
  261 + e.printStackTrace();
  262 + }
  263 + }
  264 +
  265 + /**
  266 + * 计划排班中的线路司售配档数
  267 + */
  268 + private void planComputation(DateTime dateTime) {
  269 + // 记录实际线路司售配档情况
  270 + List<SchedulePlanInfo> schedulePlanInfos = schedulePlanInfoRepository.findByDate(DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(dateTime.toString("yyyy-MM-dd")).toDate());
  271 + Set<String> lineCodes = new HashSet<>();
  272 + Map<String, Set<String>> line2conductors = new HashMap<>(), line2drivers = new HashMap<>(), line2cars = new HashMap<>();
  273 + final List<LineServiceConfig> objects = new ArrayList<>();
  274 + for (SchedulePlanInfo schedulePlanInfo : schedulePlanInfos) {
  275 + String lineCode = schedulePlanInfo.getXlBm();
  276 + lineCodes.add(lineCode);
  277 + Set<String> conductors = line2conductors.get(lineCode), drivers = line2drivers.get(lineCode), cars = line2cars.get(lineCode);
  278 + if (conductors == null) {
  279 + conductors = new HashSet<>();
  280 + line2conductors.put(lineCode, conductors);
  281 + }
  282 + if (drivers == null) {
  283 + drivers = new HashSet<>();
  284 + line2drivers.put(lineCode, drivers);
  285 + }
  286 + if (cars == null) {
  287 + cars = new HashSet<>();
  288 + line2cars.put(lineCode, cars);
  289 + }
  290 + if (!StringUtils.isEmpty(schedulePlanInfo.getsGh())) {
  291 + conductors.add(schedulePlanInfo.getsGh());
  292 + }
  293 + drivers.add(schedulePlanInfo.getjGh());
  294 + cars.add(schedulePlanInfo.getClZbh());
  295 + }
  296 + for (String lineCode : lineCodes) {
  297 + LineServiceConfig object = new LineServiceConfig();
  298 + Line line = LineBufferData.findOne(lineCode);
  299 + object.setLineCode(lineCode);
  300 + object.setLineName(line == null ? "" : line.getName());
  301 + object.setScheduleDate(dateTime.toString("yyyy-MM-dd"));
  302 + object.setConductorCount(line2conductors.get(lineCode).size());
  303 + object.setDriverCount(line2drivers.get(lineCode).size());
  304 + object.setCarCount(line2cars.get(lineCode).size());
  305 +
  306 + objects.add(object);
  307 + }
  308 +
  309 + //编程式事务
  310 + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
  311 + DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  312 + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  313 + TransactionStatus status = tran.getTransaction(def);
  314 +
  315 + try {
  316 + jdbcTemplate.update("delete from control_interface.bsth_t_plan where schedule_date = ?", new Object[]{ dateTime.toString("yyyy-MM-dd") });
  317 + jdbcTemplate.batchUpdate("insert into control_interface.bsth_t_plan (line_code, line_name, schedule_date, conductor_count, driver_count, car_count) values (?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
  318 + @Override
  319 + public void setValues(PreparedStatement ps, int i) throws SQLException {
  320 + LineServiceConfig object = objects.get(i);
  321 + ps.setString(1, object.getLineCode());
  322 + ps.setString(2, object.getLineName());
  323 + ps.setString(3, object.getScheduleDate());
  324 + ps.setInt(4, object.getConductorCount());
  325 + ps.setInt(5, object.getDriverCount());
  326 + ps.setInt(6, object.getCarCount());
  327 + }
  328 +
  329 + @Override
  330 + public int getBatchSize() {
  331 + return objects.size();
  332 + }
  333 + });
  334 + tran.commit(status);
  335 + } catch (Exception e) {
  336 + tran.rollback(status);
  337 + }
  338 + }
  339 +
  340 + /**
  341 + * 实际排班中的线路司售配档数
  342 + */
  343 + private void realComputation(DateTime dateTime) {
  344 + // 记录实际线路司售配档情况
  345 + List<ScheduleRealInfo> scheduleRealInfos = scheduleRealInfoRepository.findAll(dateTime.toString("yyyy-MM-dd"));
  346 + Set<String> lineCodes = new HashSet<>();
  347 + Map<String, Set<String>> line2conductors = new HashMap<>(), line2drivers = new HashMap<>(), line2cars = new HashMap<>();
  348 + final List<LineServiceConfig> objects = new ArrayList<>();
  349 + for (ScheduleRealInfo scheduleRealInfo : scheduleRealInfos) {
  350 + // 烂班班次不计入配档数
  351 + if (scheduleRealInfo.getStatus() == -1) { continue; }
  352 + String lineCode = scheduleRealInfo.getXlBm();
  353 + lineCodes.add(lineCode);
  354 + Set<String> conductors = line2conductors.get(lineCode), drivers = line2drivers.get(lineCode), cars = line2cars.get(lineCode);
  355 + if (conductors == null) {
  356 + conductors = new HashSet<>();
  357 + line2conductors.put(lineCode, conductors);
  358 + }
  359 + if (drivers == null) {
  360 + drivers = new HashSet<>();
  361 + line2drivers.put(lineCode, drivers);
  362 + }
  363 + if (cars == null) {
  364 + cars = new HashSet<>();
  365 + line2cars.put(lineCode, cars);
  366 + }
  367 + if (!StringUtils.isEmpty(scheduleRealInfo.getsGh())) {
  368 + conductors.add(scheduleRealInfo.getsGh());
  369 + }
  370 + drivers.add(scheduleRealInfo.getjGh());
  371 + cars.add(scheduleRealInfo.getClZbh());
  372 + }
  373 + for (String lineCode : lineCodes) {
  374 + LineServiceConfig object = new LineServiceConfig();
  375 + Line line = LineBufferData.findOne(lineCode);
  376 + object.setLineCode(lineCode);
  377 + object.setLineName(line == null ? "" : line.getName());
  378 + object.setScheduleDate(dateTime.toString("yyyy-MM-dd"));
  379 + object.setConductorCount(line2conductors.get(lineCode).size());
  380 + object.setDriverCount(line2drivers.get(lineCode).size());
  381 + object.setCarCount(line2cars.get(lineCode).size());
  382 +
  383 + objects.add(object);
  384 + }
  385 +
  386 + //编程式事务
  387 + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
  388 + DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  389 + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  390 + TransactionStatus status = tran.getTransaction(def);
  391 + try {
  392 + jdbcTemplate.update("delete from control_interface.bsth_t_real where schedule_date = ?", new Object[]{ dateTime.toString("yyyy-MM-dd") });
  393 + jdbcTemplate.batchUpdate("insert into control_interface.bsth_t_real (line_code, line_name, schedule_date, conductor_count, driver_count, car_count) values (?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
  394 + @Override
  395 + public void setValues(PreparedStatement ps, int i) throws SQLException {
  396 + LineServiceConfig object = objects.get(i);
  397 + ps.setString(1, object.getLineCode());
  398 + ps.setString(2, object.getLineName());
  399 + ps.setString(3, object.getScheduleDate());
  400 + ps.setInt(4, object.getConductorCount());
  401 + ps.setInt(5, object.getDriverCount());
  402 + ps.setInt(6, object.getCarCount());
  403 + }
  404 +
  405 + @Override
  406 + public int getBatchSize() {
  407 + return objects.size();
  408 + }
  409 + });
  410 + tran.commit(status);
  411 + } catch (Exception e) {
  412 + tran.rollback(status);
  413 + }
  414 + }
  415 +
  416 + private void carMileageComputation(final DateTime dateTime) {
  417 + String rq = dateTime.toString("yyyy-MM-dd");
  418 + // 实际排班信息
  419 + ArrayListMultimap<String, ScheduleRealInfo> nbbm2schedules = scheduleRedisService.findByDate(rq);
  420 + // 油耗信息
  421 + ArrayListMultimap<String, OilInfo> oilInfoMap = oilRedisService.findByNbbmGroup1(nbbm2schedules.keySet(), rq);
  422 + //电耗信息
  423 + ArrayListMultimap<String, ElecInfo> elecInfoMap = elecRedisService.findByNbbmGroup1(nbbm2schedules.keySet(), rq);
  424 + Set<String> nbbms = nbbm2schedules.keySet();
  425 + final List<BusMileage> busMileages = new ArrayList<>();
  426 + for (String nbbm : nbbms) {
  427 + List<ScheduleRealInfo> scheduleRealInfos = nbbm2schedules.get(nbbm);
  428 + if (scheduleRealInfos.size() == 0) {
  429 + continue;
  430 + }
  431 + BusMileage busMileage = new BusMileage();
  432 + busMileage.setScheduleDate(rq);
  433 + busMileage.setNbbm(nbbm);
  434 + busMileage.setJhlc(ScheduleCalculator.calcJHLC(scheduleRealInfos));
  435 + busMileage.setSjjhlc(ScheduleCalculator.calcSJLC(scheduleRealInfos));
  436 + busMileage.setSjcclc(ScheduleCalculator.calcCCLC(scheduleRealInfos));
  437 + busMileage.setSjjclc(ScheduleCalculator.calcJCLC(scheduleRealInfos));
  438 + busMileage.setYylc(ScheduleCalculator.calcYYLC(scheduleRealInfos));
  439 + busMileage.setKslc(ScheduleCalculator.calcKSLC(scheduleRealInfos));
  440 + busMileage.setCjlc(ScheduleCalculator.calcCJLC(scheduleRealInfos));
  441 + busMileage.setLblc(ScheduleCalculator.calcLBLC(scheduleRealInfos));
  442 + busMileage.setZjlc(ScheduleCalculator.calcZJLC(scheduleRealInfos));
  443 + busMileage.setZlc(ScheduleCalculator.calcZLC(scheduleRealInfos));
  444 + List<OilInfo> oilInfos = oilInfoMap.get(nbbm);
  445 + List<ElecInfo> elecInfos = elecInfoMap.get(nbbm);
  446 + Double yh = 0.0, dh = 0.0;
  447 + for (OilInfo oilInfo : oilInfos) {
  448 + yh = Arith.add(yh, oilInfo.getYh());
  449 + }
  450 + for (ElecInfo elecInfo : elecInfos) {
  451 + dh = Arith.add(yh, elecInfo.getHd());
  452 + }
  453 + busMileage.setYh(yh);
  454 + busMileage.setDh(dh);
  455 +
  456 + busMileages.add(busMileage);
  457 + }
  458 +
  459 + //编程式事务
  460 + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
  461 + DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  462 + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  463 + TransactionStatus status = tran.getTransaction(def);
  464 + try {
  465 + jdbcTemplate.update("delete from control_interface.bsth_t_mileage where schedule_date = ?", new Object[]{ dateTime.toString("yyyy-MM-dd") });
  466 + jdbcTemplate.batchUpdate("insert into control_interface.bsth_t_mileage (schedule_date,nbbm,jhlc,sjjhlc,sjcclc,sjjclc,yylc,kslc,cjlc,lblc,zjlc,zlc,yh,dh,schedule_month,company,company_code) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
  467 + @Override
  468 + public void setValues(PreparedStatement ps, int i) throws SQLException {
  469 + BusMileage busMileage = busMileages.get(i);
  470 + ps.setString(1, busMileage.getScheduleDate());
  471 + ps.setString(2, busMileage.getNbbm());
  472 + ps.setDouble(3, busMileage.getJhlc());
  473 + ps.setDouble(4, busMileage.getSjjhlc());
  474 + ps.setDouble(5, busMileage.getSjcclc());
  475 + ps.setDouble(6, busMileage.getSjjclc());
  476 + ps.setDouble(7, busMileage.getYylc());
  477 + ps.setDouble(8, busMileage.getKslc());
  478 + ps.setDouble(9, busMileage.getCjlc());
  479 + ps.setDouble(10, busMileage.getLblc());
  480 + ps.setDouble(11, busMileage.getZjlc());
  481 + ps.setDouble(12, busMileage.getZlc());
  482 + ps.setDouble(13, busMileage.getYh());
  483 + ps.setDouble(14, busMileage.getDh());
  484 + ps.setString(15, dateTime.toString("yyyy-MM"));
  485 + ps.setString(16, busMileage.getCompany());
  486 + ps.setString(17, busMileage.getCompanyCode());
  487 + }
  488 +
  489 + @Override
  490 + public int getBatchSize() {
  491 + return busMileages.size();
  492 + }
  493 + });
  494 + tran.commit(status);
  495 + } catch (Exception e) {
  496 + tran.rollback(status);
  497 + }
  498 + }
  499 +
  500 + /**
  501 + * 线路司售配档对象
  502 + */
  503 + private final static class LineServiceConfigVo {
  504 +
  505 + private String lineName;
  506 +
  507 + private String month;
  508 +
  509 + private Integer driverFirstCount;
  510 +
  511 + private Integer driverLastCount;
  512 +
  513 + private Integer conductorFirstCount;
  514 +
  515 + private Integer conductorLastCount;
  516 +
  517 + public String getLineName() {
  518 + return lineName;
  519 + }
  520 +
  521 + public void setLineName(String lineName) {
  522 + this.lineName = lineName;
  523 + }
  524 +
  525 + public String getMonth() {
  526 + return month;
  527 + }
  528 +
  529 + public void setMonth(String month) {
  530 + this.month = month;
  531 + }
  532 +
  533 + public Integer getDriverFirstCount() {
  534 + return driverFirstCount;
  535 + }
  536 +
  537 + public void setDriverFirstCount(Integer driverFirstCount) {
  538 + this.driverFirstCount = driverFirstCount;
  539 + }
  540 +
  541 + public Integer getDriverLastCount() {
  542 + return driverLastCount;
  543 + }
  544 +
  545 + public void setDriverLastCount(Integer driverLastCount) {
  546 + this.driverLastCount = driverLastCount;
  547 + }
  548 +
  549 + public Integer getConductorFirstCount() {
  550 + return conductorFirstCount;
  551 + }
  552 +
  553 + public void setConductorFirstCount(Integer conductorFirstCount) {
  554 + this.conductorFirstCount = conductorFirstCount;
  555 + }
  556 +
  557 + public Integer getConductorLastCount() {
  558 + return conductorLastCount;
  559 + }
  560 +
  561 + public void setConductorLastCount(Integer conductorLastCount) {
  562 + this.conductorLastCount = conductorLastCount;
  563 + }
  564 + }
  565 +
  566 + /**
  567 + * 车辆配档对象
  568 + */
  569 + private final static class BusVo {
  570 +
  571 + private String lineName;
  572 +
  573 + private String month;
  574 +
  575 + private Integer busFirstCount;
  576 +
  577 + private Integer busLastCount;
  578 +
  579 + private Integer busMaxCount;
  580 +
  581 + public String getLineName() {
  582 + return lineName;
  583 + }
  584 +
  585 + public void setLineName(String lineName) {
  586 + this.lineName = lineName;
  587 + }
  588 +
  589 + public String getMonth() {
  590 + return month;
  591 + }
  592 +
  593 + public void setMonth(String month) {
  594 + this.month = month;
  595 + }
  596 +
  597 + public Integer getBusFirstCount() {
  598 + return busFirstCount;
  599 + }
  600 +
  601 + public void setBusFirstCount(Integer busFirstCount) {
  602 + this.busFirstCount = busFirstCount;
  603 + }
  604 +
  605 + public Integer getBusLastCount() {
  606 + return busLastCount;
  607 + }
  608 +
  609 + public void setBusLastCount(Integer busLastCount) {
  610 + this.busLastCount = busLastCount;
  611 + }
  612 +
  613 + public Integer getBusMaxCount() {
  614 + return busMaxCount;
  615 + }
  616 +
  617 + public void setBusMaxCount(Integer busMaxCount) {
  618 + this.busMaxCount = busMaxCount;
  619 + }
  620 + }
  621 +
  622 + /**
  623 + * 车辆公里、油耗对象
  624 + */
  625 + private final static class BusMileageVo {
  626 +
  627 + private String insideCode;
  628 +
  629 + private String month;
  630 +
  631 + private Double totalMileage;
  632 +
  633 + private Double oilConsume;
  634 +
  635 + private Double electricConsume;
  636 +
  637 + private String company;
  638 +
  639 + private String companyCode;
  640 +
  641 + public String getInsideCode() {
  642 + return insideCode;
  643 + }
  644 +
  645 + public void setInsideCode(String insideCode) {
  646 + this.insideCode = insideCode;
  647 + }
  648 +
  649 + public String getMonth() {
  650 + return month;
  651 + }
  652 +
  653 + public void setMonth(String month) {
  654 + this.month = month;
  655 + }
  656 +
  657 + public Double getTotalMileage() {
  658 + return totalMileage;
  659 + }
  660 +
  661 + public void setTotalMileage(Double totalMileage) {
  662 + this.totalMileage = totalMileage;
  663 + }
  664 +
  665 + public Double getOilConsume() {
  666 + return oilConsume;
  667 + }
  668 +
  669 + public void setOilConsume(Double oilConsume) {
  670 + this.oilConsume = oilConsume;
  671 + }
  672 +
  673 + public Double getElectricConsume() {
  674 + return electricConsume;
  675 + }
  676 +
  677 + public void setElectricConsume(Double electricConsume) {
  678 + this.electricConsume = electricConsume;
  679 + }
  680 +
  681 + public String getCompany() {
  682 + return company;
  683 + }
  684 +
  685 + public void setCompany(String company) {
  686 + this.company = company;
  687 + }
  688 +
  689 + public String getCompanyCode() {
  690 + return companyCode;
  691 + }
  692 +
  693 + public void setCompanyCode(String companyCode) {
  694 + this.companyCode = companyCode;
  695 + }
  696 + }
  697 +}
... ...