JqlServiceImpl.java
5.42 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
package com.bsth.service.oil.impl;
import com.bsth.entity.oil.Jql;
import com.bsth.entity.report.Ticket;
import com.bsth.repository.oil.JqlRepository;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.oil.JqlService;
import com.bsth.util.ReportUtils;
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.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class JqlServiceImpl extends BaseServiceImpl<Jql, Integer> implements JqlService {
@Autowired
JqlRepository repository;
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public String importExcel(File file, String gsbm, String gsName, String fgsbm, String fgsName) {
gsbm="99";
gsName="青浦公交";
fgsbm="100";
fgsName="青浦公交";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DecimalFormat df = new DecimalFormat("######0.00");
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;
for(int i = 2; 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 < 5; 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 nbbm = split[1].trim();//内部编号
/*String jsy= split[2].trim();//驾驶员工号*/
String jsy="";
double jql = Double.valueOf(split[2].trim().length()!=0?split[2]:"0");//加注量
String jqz = split[3].trim();//能源站
String plate = split[4].trim();//车牌号
String remarks = split[5].trim();//备注
if(rq.trim().length() == 0){
rq = sdf.format(new Date());
}
List<Double> jql_ = repository.queryBySame(gsbm, fgsbm, rq, nbbm);
if(jql_.size() == 0){
repository.insertData(gsbm, gsName, fgsbm, fgsName, rq, nbbm,
df.format(jql), jqz, remarks, sd.format(new Date()),jsy,plate);
}else{
repository.UpdateJql(df.format(jql), gsbm, fgsbm, rq, nbbm,jsy,plate);
}
}
wb.close();
fs.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "文件导入失败";
} finally {
file.delete();
}
return "文件导入成功";
}
@Override
public Map<String, Object> query(Map<String, Object> map) {
Map<String, Object> modelMap = new HashMap<String, Object>();
Integer page = Integer.valueOf(map.containsKey("page")?map.get("page").toString():"0");
StringBuffer sql=new StringBuffer("SELECT * FROM bsth_c_jql where 1=1 ");
if(map.get("rq")!=null && !"".equals(map.get("rq").toString())){
sql.append("and rq>='"+map.get("rq")+"'");
}
if(map.get("nbbm")!=null && !"".equals(map.get("nbbm").toString())){
sql.append("and nbbm='"+map.get("nbbm")+"'");
}
List<Jql> query = jdbcTemplate.query(sql.toString(), BeanPropertyRowMapper.newInstance(Jql.class));
if(!map.containsKey("type")){
int end = (page+1)*10>query.size()?query.size():(page+1)*10;
modelMap.put("dataList", query.subList(page*10, end));
modelMap.put("totalPages", query.size()%10>0?query.size()/10+1:query.size()/10);
} else if(map.get("type").toString().equals("export")){
SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd"),
sdfSimple = new SimpleDateFormat("yyyyMMdd");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for(Jql jql : query){
Map<String, Object> m = new HashMap<String, Object>();
m.put("rq", sdfMonth.format(jql.getRq()));
m.put("nbbm", jql.getNbbm());
m.put("jql", jql.getJql());
m.put("jqz", jql.getJqz());
m.put("plate", jql.getPlate());
m.put("remarks", jql.getRemarks());
list.add(m);
}
/*List<Iterator<?>> listI = new ArrayList<Iterator<?>>();
Map<String, Object> m = new HashMap<String, Object>();
ReportUtils ee = new ReportUtils();
try {
listI.add(list.iterator());
String path = this.getClass().getResource("/").getPath()+"static/pages/forms/";
ee.excelReplace(listI, new Object[] { m }, path+"mould/export_Jql.xls",
path+"export/车辆加氢量" + sdfSimple.format(sdfMonth.parse(rq)) + ".xls");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}*/
}
return modelMap;
}
}