GoalImportOperation.jsp
11.6 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ include file="/systeminfo/init_wev8.jsp" %>
<%@ page import="weaver.file.FileUpload"%>
<%@ page import="java.io.FileInputStream"%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.regex.*"%>
<%@ page import="org.apache.poi.hssf.usermodel.*"%>
<%@ page import="org.apache.poi.poifs.filesystem.POIFSFileSystem"%>
<%@ page import="weaver.conn.*"%>
<jsp:useBean id="RecordSet" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="cmutil" class="weaver.workrelate.util.CommonTransUtil" scope="page"/>
<%
response.setHeader("cache-control", "no-cache");
response.setHeader("pragma", "no-cache");
response.setHeader("expires", "Mon 1 Jan 1990 00:00:00 GMT");
%>
<%String userid = user.getUID()+"";
if(cmutil.getGoalMaint(userid)[0]<2){
response.sendRedirect("/notice/noright.jsp");
return;
}
FileUpload fu = new FileUpload(request,false);
int fileid = 0 ;
try {
fileid = Util.getIntValue(fu.uploadFiles("filename"),0);
String sql = "select filerealpath from imagefile where imagefileid = "+fileid;
RecordSet.executeSql(sql);
String uploadfilepath="";
if(RecordSet.next()) uploadfilepath = Util.null2String(RecordSet.getString("filerealpath"));
/**
if(!uploadfilepath.equals("")){
Excelfilepath = GCONST.getRootPath()+"workrelate/goal/import/ExcelToDB"+File.separatorChar+filename ;
//System.out.println("Excelfilepath="+Excelfilepath);
fm.copy(uploadfilepath,Excelfilepath);
}
*/
boolean result = this.ExcelToDB(uploadfilepath,user.getUID()+"",request);
staticobj.removeObject("GM_GOALSHOW");
response.sendRedirect("GoalImport.jsp");
}
catch(Exception e) {
}
%>
<%!
/**
* 私有方法,获取Excel表格对应表格中的数据
*
* @param cell
* Excel表格的行
* @param row
* Excel表格的列
* @return Excel表格对应表格中的数据
*/
private String getCellValue(HSSFCell cell, HSSFRow row) {
//this.setCellValue("");
if(cell==null) return "";
String cellValue = "";
switch (cell.getCellType()) {
case NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//cellValue = (DateFormat.getDateInstance().format((cell.getDateCellValue()))).toString();
cellValue = (sdf.format((cell.getDateCellValue()))).toString();
} else {
//cellValue = String.valueOf(cell.getNumericCellValue());
cellValue = String.valueOf((long)cell.getNumericCellValue());//
//cellValue = String.format("%1$-9d", cell.getNumericCellValue());
}
break;
case STRING:
cellValue = cell.getStringCellValue();
break;
case FORMULA:
cellValue = (DateFormat.getDateInstance().format((cell
.getDateCellValue()))).toString();
break;
default:
break;
}
return Util.null2String(cellValue.trim());
}
/**
* 检查excel项是否为空
* @param row
* @return
*/
private boolean checkIsNull(HSSFRow row,StringBuffer errormsg){
if(row == null){
return false;
}
boolean b = true;
//检查第1-6列
int col = 6;
for(int i=0;i<col;i++){
if(i!=1){
HSSFCell cell = row.getCell((short) i);
if (cell == null || "3".equals(cell.getCellType())) {
errormsg.append("第"+String.valueOf(row.getRowNum() + 1)+"行必要信息不完整;<br>");
b = false;
return b;
}
}
}
return b;
}
/**
* 检测字符串是否是指定日期格式
* @param str 待检测的字符串
* @param datePattern 指定日期格式
* @return
*/
public boolean checkDate(String str,String datePattern) {
if(datePattern == null || "".equals(datePattern)){
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
sdf.setLenient(false);
try {
if (str.equals(sdf.format(sdf.parse(str)))) {
return true;
} else {
return false;
}
} catch (ParseException e) {
return false;
}
}
/**
* 检测字符串是否是指定格式
* @param str 待检测的字符串
* @param rex 正则表达式
* @return
*/
public static boolean check(String str, String rex) {
if(rex == null || "".equals(rex)){
return false;
}
Pattern pattern = Pattern.compile(rex);
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
/**
* 将Excel表格中的数据导入数据库
*
* @param FileName
* Excel表格的路径及文件名
*/
public boolean ExcelToDB(String FileName,String userid,HttpServletRequest request) {
StringBuffer errormsg = new StringBuffer();
String sql = "";
int total = 0;//导入总数
int success = 0;//成功个数
int fail = 0;//失败个数
try {
FileInputStream finput = new FileInputStream(FileName);
POIFSFileSystem fs = new POIFSFileSystem(finput);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
finput.close();
HSSFRow row = null;
Map uidmap = new HashMap();
RecordSet rs = new RecordSet();
String uid = "";
String puid = "";
String parentid = "";
String name = "";
String cate = "";
int period = 0;
int periody = 0;
int periodm = 0;
int periods = 0;
String enddate = "";
String principalid = "";
double target = 0;
String tunit = "";
double result = 0;
String runit = "";
double showorder = 0;
int rowsNum = sheet.getLastRowNum();
/**
if(rowsNum-1 > maxNum){
return false;
}*/
//System.out.println("rowNum:"+rowsNum);
for (int i = 1; i < rowsNum + 1; i++) {
total++;
row = sheet.getRow(i);
if(this.checkIsNull(row,errormsg)){//此行必填项都已填写
String createdate = TimeUtil.getCurrentDateString();
String createtime = TimeUtil.getOnlyCurrentTimeString();
//标识
HSSFCell cell0 = row.getCell((short) 0);
uid = this.getCellValue(cell0, row);
//上级标识
HSSFCell cell1 = row.getCell((short) 1);
puid = "";
parentid = this.getCellValue(cell1, row);
if(!"".equals(parentid)) puid = Util.null2String((String)uidmap.get(parentid));
if(!"".equals(puid)) parentid = puid;
parentid = Util.getIntValue(parentid,0)+"";
//目标名称
HSSFCell cell2 = row.getCell((short) 2);
name = this.getCellValue(cell2, row);
HSSFCell cell13 = row.getCell((short) 13);
name += this.getCellValue(cell13, row);
//目标类型
HSSFCell cell3 = row.getCell((short) 3);
cate = this.getCellValue(cell3, row);
//目标周期
HSSFCell cell4 = row.getCell((short) 4);
period = Util.getIntValue(this.getCellValue(cell4, row),3);
//责任人
HSSFCell cell5 = row.getCell((short) 5);
principalid = this.getCellValue(cell5, row);
//目标值
HSSFCell cell6 = row.getCell((short) 6);
target = Util.getDoubleValue(this.getCellValue(cell6, row),0);
//目标值单位
HSSFCell cell7 = row.getCell((short) 7);
tunit = this.getCellValue(cell7, row);
//完成值
HSSFCell cell8 = row.getCell((short) 8);
result = Util.getDoubleValue(this.getCellValue(cell8, row),0);
//完成值单位
HSSFCell cell9 = row.getCell((short) 9);
runit = this.getCellValue(cell9, row);
HSSFCell cell10 = row.getCell((short) 10);
HSSFCell cell11 = row.getCell((short) 11);
HSSFCell cell12 = row.getCell((short) 12);
periody = Util.getIntValue(this.getCellValue(cell10, row),Integer.parseInt(TimeUtil.getCurrentDateString().substring(0,4)));
periodm = Util.getIntValue(this.getCellValue(cell11, row),Integer.parseInt(TimeUtil.getCurrentDateString().substring(5,7)));
periods = Util.getIntValue(this.getCellValue(cell12, row),Integer.parseInt(TimeUtil.getCurrentSeason()));
if(period==1){
//enddate = TimeUtil.getYearMonthEndDay(year,month);
}else if(period==2){
if(periods==1) periodm = 3;
if(periods==2) periodm = 6;
if(periods==3) periodm = 9;
if(periods==4) periodm = 12;
}else if(period==3){
periodm = 12;
}else if(period==4){
periody = periody+2;
periodm = 12;
}else if(period==5){
periody = periody+4;
periodm = 12;
}
enddate = TimeUtil.getYearMonthEndDay(periody,periodm);
//排序
HSSFCell cell14 = row.getCell((short) 14);
showorder = Util.getDoubleValue(this.getCellValue(cell14, row),0);
if(name.equals("") || principalid.equals("")) continue;
//保存主信息
sql = "insert into GM_GoalInfo (name,status,creater,createdate,createtime,begindate,enddate,cate,principalid,parentid,period,target,tunit,result,runit,showorder) "
+"values('"+name+"',1,"+userid+",'"+createdate+"','"+createtime+"','','"+enddate+"','"+cate+"',"+principalid+",'"+parentid+"','"+period+"',"+target+",'"+tunit+"',"+result+",'"+runit+"',"+showorder+")";
//System.out.println("sql:"+sql);
boolean insert = rs.executeSql(sql);
if(insert){
rs.executeSql("select max(id) from GM_GoalInfo");
if(rs.next()){
success++;
String goalid = Util.null2String(rs.getString(1));
uidmap.put(uid,goalid);
//添加新建反馈
String content = "导入";
sql = "insert into GM_GoalFeedback (goalid,content,hrmid,docids,wfids,crmids,projectids,meetingids,fileids,createdate,createtime)"
+" values("+goalid+",'"+content+"',"+userid+",'','','','','','','"+createdate+"','"+createtime+"')";
rs.executeSql(sql);
//记录日志
sql = "insert into GM_GoalLog (goalid,type,operator,operatedate,operatetime,operatefiled,operatevalue)"
+" values("+goalid+",1,"+userid+",'"+createdate+"','"+createtime+"','','')";
rs.executeSql(sql);
//判断下级任务则需要默认加入查看日志并更新所有上级任务的参与人
if(!parentid.equals("")){
sql = "insert into GM_GoalLog (goalid,type,operator,operatedate,operatetime,operatefiled,operatevalue)"
+" values("+parentid+",0,"+userid+",'"+createdate+"','"+createtime+"','','')";
rs.executeSql(sql);
this.updateSupPartner(goalid);
}
}
}
}
}
return true;
} catch (Exception e) {
errormsg.append("导入失败:格式不正确;请确认导入文件中不要含有筛选公式等特殊数据类型!<br>"+e);
return true;
} finally{
String msg = "目标导入完成!<br>";
msg += "总计:"+total+"个; 成功:"+success+"个; 失败:"+(total-success)+"个;";
request.getSession().setAttribute("GOAL_IMPORT_INFO",msg+"<br><br>"+errormsg.toString());
}
}
private void updateSupPartner(String goalid){
RecordSet rs = new RecordSet();
List hrmids = new ArrayList();
String parentid = "";
String parentPrincipalid = "";
rs.executeSql("select principalid,parentid from GM_GoalInfo where parentid<>0 and parentid is not null and (deleted=0 or deleted is null) and id="+goalid);
if(rs.next()){
parentid = Util.null2String(rs.getString("parentid"));
if(!parentid.equals("")){
hrmids.add(Util.null2String(rs.getString("principalid")));
rs.executeSql("select principalid from GM_GoalInfo where (deleted=0 or deleted is null) and id="+parentid);
if(rs.next()){
parentPrincipalid = Util.null2String(rs.getString("principalid"));
String partnerid = "";
rs.executeSql("select partnerid from GM_GoalPartner where goalid="+goalid);
while(rs.next()){
partnerid = Util.null2String(rs.getString("partnerid"));
if(!partnerid.equals("") && !partnerid.equals(parentPrincipalid) && hrmids.indexOf(partnerid)<0){
hrmids.add(partnerid);
}
}
for(int i=0;i<hrmids.size();i++){
partnerid = (String)hrmids.get(i);
if(!partnerid.equals("")){
rs.executeSql("delete from GM_GoalPartner where goalid="+parentid+" and partnerid="+partnerid);
rs.executeSql("insert into GM_GoalPartner (goalid,partnerid) values('"+parentid+"','"+partnerid+"')");
}
}
this.updateSupPartner(parentid);
}
}
}
}
%>