RefuelServiceImpl.java
10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package com.bsth.service.impl;
import com.bsth.entity.Refuel;
import com.bsth.entity.realcontrol.ChildTaskPlan;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.sys.SysUser;
import com.bsth.repository.RefuelRepository;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.RefuelService;
import com.bsth.service.report.CulateMileageService;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created in 19/9/3.
*/
@Service
public class RefuelServiceImpl extends BaseServiceImpl<Refuel, Long> implements RefuelService {
@Autowired
private RefuelRepository repository;
@Autowired
private CulateMileageService culateMileageService;
@Override
public List<Refuel> query(Map<String, Object> map) {
// TODO Auto-generated method stub
String date = "", car = "%%", driver = "%%";
if(map.get("date") != null){
date = map.get("date").toString();
}
if(map.get("car") != null){
car = "%" + map.get("car").toString() + "%";
}
if(map.get("driver") != null){
driver = "%" + map.get("driver").toString() + "%";
}
List<Refuel> list = repository.findByDate(date, car, driver);
return list;
}
@Override
public List<Map<String, Object>> queryDaily(Map<String, Object> map) {
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat("0.##");
String date = "", car = "%%", driver = "%%";
if(map.get("date") != null){
date = map.get("date").toString();
}
if(map.get("car") != null){
car = "%" + map.get("car").toString() + "%";
}
if(map.get("driver") != null){
driver = "%" + map.get("driver").toString() + "%";
}
List<Refuel> list = repository.findByDate(date, car, driver);
List<String> keyList = new ArrayList<String>();
Map<String, Refuel> rMap = new HashMap<String, Refuel>();
for(Refuel rf : list){
String key = rf.getCar() + "/" + rf.getDriver();
if("/".equals(key)){
continue;
}
if(rMap.containsKey(key)){
Refuel r = rMap.get(key);
if(rf.getOutOil().length() > 0){
if(r.getOutOil().length() == 0){
r.setOutOil(rf.getOutOil());
} else if(Double.valueOf(rf.getOutOil()) > Double.valueOf(r.getOutOil())){
r.setOutOil(rf.getOutOil());
}
}
if(rf.getInOil().length() > 0){
if(r.getInOil().length() == 0){
r.setInOil(rf.getInOil());
} else if(Double.valueOf(rf.getInOil()) < Double.valueOf(r.getInOil())){
r.setInOil(rf.getInOil());
}
}
if(rf.getInStation0().length() > 0){
if(r.getInStation0().length() == 0){
r.setInStation0(rf.getInStation0());
} else {
r.setInStation0(add(r.getInStation0(), rf.getInStation0()));
}
}
if(rf.getInStation5().length() > 0){
if(r.getInStation5().length() == 0){
r.setInStation5(rf.getInStation5());
} else {
r.setInStation5(add(r.getInStation5(), rf.getInStation5()));
}
}
if(rf.getOilCard0().length() > 0){
if(r.getOilCard0().length() == 0){
r.setOilCard0(rf.getOilCard0());
} else {
r.setOilCard0(add(r.getOilCard0(), rf.getOilCard0()));
}
}
if(rf.getOilCard10().length() > 0){
if(r.getOilCard10().length() == 0){
r.setOilCard10(rf.getOilCard10());
} else {
r.setOilCard10(add(r.getOilCard10(), rf.getOilCard10()));
}
}
if(rf.getEastStation0().length() > 0){
if(r.getEastStation0().length() == 0){
r.setEastStation0(rf.getEastStation0());
} else {
r.setEastStation0(add(r.getEastStation0(), rf.getEastStation0()));
}
}
if(rf.getEastStation10().length() > 0){
if(r.getEastStation10().length() == 0){
r.setEastStation10(rf.getEastStation10());
} else {
r.setEastStation10(add(r.getEastStation10(), rf.getEastStation10()));
}
}
if(rf.getOutStation0().length() > 0){
if(r.getOutStation0().length() == 0){
r.setOutStation0(rf.getOutStation0());
} else {
r.setOutStation0(add(r.getOutStation0(), rf.getOutStation0()));
}
}
if(rf.getOutStation10().length() > 0){
if(r.getOutStation10().length() == 0){
r.setOutStation10(rf.getOutStation10());
} else {
r.setOutStation10(add(r.getOutStation10(), rf.getOutStation10()));
}
}
} else {
keyList.add(key);
rMap.put(key, rf);
}
}
List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
for(String key : keyList){
Refuel r = rMap.get(key);
Map<String, Object> m = new HashMap<String, Object>();
String realMileage = "", oil = "0", consume = "";
List<ScheduleRealInfo> temp = repository.scheduleByDate(date, r.getCar(), r.getDriver());
List<ScheduleRealInfo> temp2 = new ArrayList<ScheduleRealInfo>();
if(temp.size() > 0){
for(ScheduleRealInfo s : temp){
Set<ChildTaskPlan> cts = s.getcTasks();
if(cts != null && cts.size() > 0){
temp2.add(s);
}else{
if(s.getZdsjActual()!=null
&& s.getFcsjActual()!=null){
temp2.add(s);
}
}
}
Double sjgl = culateMileageService.culateSjgl(temp2);
Double ksgl = culateMileageService.culateKsgl(temp);
Double jccgl = culateMileageService.culateJccgl(temp2);
Double ljgl = culateMileageService.culateLjgl(temp2);
realMileage = add(add(sjgl, ksgl), add(jccgl, ljgl));
}
if(r.getOutOil().length() > 0 && r.getInOil().length() > 0){
oil = new BigDecimal(r.getOutOil()).subtract(new BigDecimal(r.getInOil())).toString();
}
if(r.getInStation0().length() > 0){
oil = add(oil, r.getInStation0());
}
if(r.getInStation5().length() > 0){
oil = add(oil, r.getInStation5());
}
if(r.getOilCard0().length() > 0){
oil = add(oil, r.getOilCard0());
}
if(r.getOilCard10().length() > 0){
oil = add(oil, r.getOilCard10());
}
if(r.getEastStation0().length() > 0){
oil = add(oil, r.getEastStation0());
}
if(r.getEastStation10().length() > 0){
oil = add(oil, r.getEastStation10());
}
if(r.getOutStation0().length() > 0){
oil = add(oil, r.getOutStation0());
}
if(r.getOutStation10().length() > 0){
oil = add(oil, r.getOutStation10());
}
if(realMileage.length() > 0 && oil.length() > 0 && !("0".equals(realMileage))){
consume = df.format(new BigDecimal(oil).divide(new BigDecimal(realMileage), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
}
m.put("date", r.getDateStr());
m.put("line", r.getLineName());
m.put("car", r.getCar());
m.put("driver", r.getDriver());
m.put("realMileage", realMileage);
m.put("oil", oil);
m.put("consume", consume);
resList.add(m);
}
return resList;
}
@Override
public String importExcel(File file) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<String> textList = new ArrayList<String>();
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
// 取得总行数
int rowNum = sheet.getLastRowNum() + 1;
// 取得总列数
int cellNum = sheet.getRow(0).getLastCellNum();
HSSFRow row = null;
HSSFCell cell = null;
String userName = "";
SysUser user = SecurityUtils.getCurrentUser();
if(user != null && user.getUserName() != null){
userName = user.getUserName();
}
for(int i = 1; i < rowNum; i++){
row = sheet.getRow(i);
if (row == null){
continue;
}
String text = "";
for(int j = 0; j < cellNum; j++){
cell = row.getCell(j);
if(cell == null){
text += ",";
continue;
}
text += String.valueOf(cell) + ",";
}
String[] split = (text+";").split(",");
String str = "";
for(int j = 0; j < split.length && j < 14; j++){
str += split[j];
}
if(str.trim().length() == 0)
continue;
textList.add(text + ";");
}
for(int i = 0; i < textList.size(); i++){
String text = textList.get(i);
String[] split = text.split(",");
String rq = split[0].trim();
String line = split[1].trim();
String car = split[2].trim();
String driver = split[3].trim();
if((car + driver).trim().length() == 0){
continue;
}
String outOil = split[4].trim();
String inOil = split[5].trim();
String inStation0 = split[6].trim();
String inStation5 = split[7].trim();
String oilCard0 = split[8].trim();
String oilCard10 = split[9].trim();
String eastStation0 = split[10].trim();
String eastStation10 = split[11].trim();
String outStation0 = split[12].trim();
String outStation10 = split[13].trim();
List<Refuel> list = repository.selectByDateAndCar(rq, line, car, driver);
if(list.size() > 0){
for(Refuel r : list){
if(r.getId() != null){
repository.update(r.getId(), rq, rq, line, car, driver, outOil, inOil, inStation0, inStation5,
oilCard0, oilCard10, eastStation0, eastStation10, outStation0, outStation10, userName, sd.format(new Date()));
}
}
} else {
repository.insertData(rq, rq, line, car, driver, outOil, inOil, inStation0, inStation5, oilCard0, oilCard10,
eastStation0, eastStation10, outStation0, outStation10, userName, sd.format(new Date()));
}
}
wb.close();
fs.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "文件导入失败";
} finally {
file.delete();
}
return "文件导入成功";
}
public String add(Object v1, Object v2)
{
BigDecimal b1 = new BigDecimal(v1.toString());
BigDecimal b2 = new BigDecimal(v2.toString());
return b1.add(b2).toString();
}
}