|
@@ -4,6 +4,7 @@ import com.github.pagehelper.PageHelper;
|
4
|
4
|
import com.github.pagehelper.PageInfo;
|
5
|
5
|
import com.liang.common.JsonTool;
|
6
|
6
|
import com.liang.common.base.BaseResult;
|
|
7
|
+import com.liang.common.utils.DateUtil;
|
7
|
8
|
import com.liang.entity.BasePerson;
|
8
|
9
|
import com.liang.entity.SysLsUserEntity;
|
9
|
10
|
|
|
@@ -12,20 +13,36 @@ import com.liang.service.BaseEducationService;
|
12
|
13
|
import com.liang.service.LsUserService;
|
13
|
14
|
import io.swagger.annotations.ApiOperation;
|
14
|
15
|
import io.swagger.models.auth.In;
|
|
16
|
+import org.apache.commons.io.FilenameUtils;
|
15
|
17
|
import org.apache.ibatis.annotations.Delete;
|
|
18
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
19
|
+import org.apache.poi.ss.usermodel.Row;
|
|
20
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
21
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
22
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
16
|
23
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
24
|
+import org.springframework.core.io.ClassPathResource;
|
|
25
|
+import org.springframework.http.HttpHeaders;
|
|
26
|
+import org.springframework.http.HttpStatus;
|
|
27
|
+import org.springframework.http.MediaType;
|
17
|
28
|
import org.springframework.http.ResponseEntity;
|
18
|
29
|
import org.springframework.stereotype.Controller;
|
19
|
30
|
import org.springframework.ui.Model;
|
20
|
31
|
import org.springframework.web.bind.annotation.*;
|
|
32
|
+import org.springframework.web.multipart.MultipartFile;
|
|
33
|
+import org.springframework.web.multipart.MultipartHttpServletRequest;
|
21
|
34
|
|
22
|
35
|
import javax.annotation.Resource;
|
23
|
36
|
import javax.xml.transform.Result;
|
|
37
|
+import java.io.IOException;
|
|
38
|
+import java.io.InputStream;
|
|
39
|
+import java.nio.file.Path;
|
|
40
|
+import java.nio.file.Paths;
|
|
41
|
+import java.text.DateFormat;
|
24
|
42
|
import java.text.SimpleDateFormat;
|
25
|
|
-import java.util.Date;
|
26
|
|
-import java.util.HashMap;
|
27
|
|
-import java.util.List;
|
28
|
|
-import java.util.Map;
|
|
43
|
+import java.time.LocalDate;
|
|
44
|
+import java.time.format.DateTimeFormatter;
|
|
45
|
+import java.util.*;
|
29
|
46
|
|
30
|
47
|
import static com.liang.common.base.BaseController.RESULT_ROWS;
|
31
|
48
|
import static com.liang.common.base.BaseController.RESULT_TOTAL;
|
|
@@ -44,6 +61,10 @@ public class LsUserController {
|
44
|
61
|
@Resource
|
45
|
62
|
private BaseEducationService baseEducationService;
|
46
|
63
|
|
|
64
|
+ // 允许的表格文件扩展名列表
|
|
65
|
+ private final List<String> allowedExtensions = Arrays.asList("xls", "xlsx", "csv");
|
|
66
|
+
|
|
67
|
+
|
47
|
68
|
@ApiOperation(value = "页面初始化", notes = "页面初始化")
|
48
|
69
|
@RequestMapping(value = "/init", method = RequestMethod.GET)
|
49
|
70
|
public String init( Model model) {
|
|
@@ -206,5 +227,125 @@ public class LsUserController {
|
206
|
227
|
return BaseResult.failure("该人员不存在");
|
207
|
228
|
}
|
208
|
229
|
|
|
230
|
+ @PostMapping("/file")
|
|
231
|
+ @ResponseBody
|
|
232
|
+ public BaseResult handleFileUpload(@RequestParam("file") MultipartFile file) {
|
|
233
|
+ if (file.isEmpty()){
|
|
234
|
+ return BaseResult.failure("上传文件异常,上传失败!");
|
|
235
|
+ }
|
|
236
|
+ List<SysLsUserEntity> lists = new ArrayList<>();
|
|
237
|
+ Integer h = 0;
|
|
238
|
+ try{
|
|
239
|
+ // 验证文件类型
|
|
240
|
+
|
|
241
|
+ DateUtil dateUtil = new DateUtil();
|
|
242
|
+ String fileExtension = FilenameUtils.getExtension(file.getOriginalFilename());
|
|
243
|
+ if (!allowedExtensions.contains(fileExtension.toLowerCase())) {
|
|
244
|
+ return BaseResult.failure("只允许上传 Excel文件");
|
|
245
|
+ }
|
|
246
|
+ // 开始解析这个文件
|
|
247
|
+ List<List<String>> spreadsheetData = parseSpreadsheet(file.getInputStream(), fileExtension);
|
|
248
|
+ System.out.println(spreadsheetData);
|
|
249
|
+ spreadsheetData.remove(0);
|
|
250
|
+ List<SysLsUserEntity> list = new ArrayList<>();
|
|
251
|
+ for (List<String> row : spreadsheetData) {
|
|
252
|
+ SysLsUserEntity employee = new SysLsUserEntity();
|
|
253
|
+ // 设置员工信息
|
|
254
|
+ employee.setXm(row.get(0));
|
|
255
|
+ employee.setXb(row.get(1));
|
|
256
|
+ employee.setPhone(row.get(2));
|
|
257
|
+ employee.setSfzh(row.get(3));
|
|
258
|
+ employee.setXl(row.get(4));
|
|
259
|
+ employee.setByxy(row.get(5));
|
|
260
|
+ employee.setZy(row.get(6));
|
|
261
|
+ employee.setQpsj(dateUtil.convertDate(row.get(7)));
|
|
262
|
+ employee.setDqsj(dateUtil.convertDate(row.get(8)));
|
|
263
|
+ employee.setGznr(row.get(9));
|
|
264
|
+ // 将对象添加到列表中
|
|
265
|
+ list.add(employee);
|
|
266
|
+ }
|
|
267
|
+ System.out.println(list);
|
|
268
|
+
|
|
269
|
+ for(int i = 0; i<list.size();i++){
|
|
270
|
+ SysLsUserEntity sysLsUserEntity = new SysLsUserEntity();
|
|
271
|
+ // 校验合同起始时间和到期时间是否在三个月 内
|
|
272
|
+ if (dateUtil.areDatesWithinThreeMonths(list.get(i).getDqsj(), list.get(i).getQpsj()) !=true) {
|
|
273
|
+ sysLsUserEntity.setXm(list.get(i).getXm());
|
|
274
|
+ // xmfzr 这个字段存储未能存储原因
|
|
275
|
+ sysLsUserEntity.setXmfzr("聘用日期不能大于三个月");
|
|
276
|
+ sysLsUserEntity.setSfzh(list.get(i).getSfzh());
|
|
277
|
+ sysLsUserEntity.setDqsj(list.get(i).getDqsj());
|
|
278
|
+ sysLsUserEntity.setQpsj(list.get(i).getQpsj());
|
|
279
|
+ if (list.get(i).getXm().isEmpty() && list.get(i).getSfzh().isEmpty()){
|
|
280
|
+ continue;
|
|
281
|
+ }
|
|
282
|
+ lists.add(sysLsUserEntity);
|
|
283
|
+ continue;
|
|
284
|
+ }
|
|
285
|
+ // 校验当前传入的 身份证号 是否在数据库中存在
|
|
286
|
+ if (lsUserService.jy(list.get(i).getSfzh()) != true){
|
|
287
|
+ sysLsUserEntity.setXm(list.get(i).getXm());
|
|
288
|
+ sysLsUserEntity.setSfzh(list.get(i).getSfzh());
|
|
289
|
+ sysLsUserEntity.setDqsj(list.get(i).getDqsj());
|
|
290
|
+ sysLsUserEntity.setQpsj(list.get(i).getQpsj());
|
|
291
|
+ // xmfzr 这个字段存储未能存储原因
|
|
292
|
+ sysLsUserEntity.setXmfzr("该人员身份证已存在");
|
|
293
|
+ if (list.get(i).getXm().isEmpty() && list.get(i).getSfzh().isEmpty()){
|
|
294
|
+ continue;
|
|
295
|
+ }
|
|
296
|
+ lists.add(sysLsUserEntity);
|
|
297
|
+
|
|
298
|
+ continue;
|
|
299
|
+ }
|
|
300
|
+ Integer mum = lsUserService.saveOperLog(list.get(i));
|
|
301
|
+ if (mum > 0){
|
|
302
|
+ h++;
|
|
303
|
+ continue;
|
|
304
|
+ }
|
|
305
|
+ }
|
|
306
|
+ }catch (Exception a){
|
|
307
|
+ a.printStackTrace();
|
|
308
|
+ }
|
|
309
|
+
|
|
310
|
+ // 获取文件名后缀
|
|
311
|
+ if (h == 0){
|
|
312
|
+ return BaseResult.failure(43,"成员录入失败,人员已存在!");
|
|
313
|
+ }
|
|
314
|
+ return BaseResult.success("成员录入成功",lists);
|
|
315
|
+ }
|
|
316
|
+ private List<List<String>> parseSpreadsheet(InputStream inputStream, String fileExtension) throws IOException {
|
|
317
|
+ Workbook workbook = null;
|
|
318
|
+ if (fileExtension.equalsIgnoreCase("xlsx")) {
|
|
319
|
+ workbook = new XSSFWorkbook(inputStream);
|
|
320
|
+ } else if (fileExtension.equalsIgnoreCase("xls")) {
|
|
321
|
+ throw new IOException("上传的Excel文件格式不支持,请上传xlsx格式文件。");
|
|
322
|
+ } else if (fileExtension.equalsIgnoreCase("csv")) {
|
|
323
|
+ throw new IOException("上传的文件是CSV格式,当前不支持解析,请上传Excel文件。");
|
|
324
|
+ }
|
|
325
|
+
|
|
326
|
+ List<List<String>> spreadsheetData = new ArrayList<>();
|
|
327
|
+ if (workbook != null) {
|
|
328
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
329
|
+ Iterator<Row> iterator = sheet.iterator();
|
|
330
|
+
|
|
331
|
+ while (iterator.hasNext()) {
|
|
332
|
+ Row currentRow = iterator.next();
|
|
333
|
+ Iterator<Cell> cellIterator = currentRow.iterator();
|
|
334
|
+
|
|
335
|
+ List<String> rowData = new ArrayList<>();
|
|
336
|
+ while (cellIterator.hasNext()) {
|
|
337
|
+ Cell currentCell = cellIterator.next();
|
|
338
|
+ rowData.add(currentCell.toString());
|
|
339
|
+ }
|
|
340
|
+
|
|
341
|
+ spreadsheetData.add(rowData);
|
|
342
|
+ }
|
|
343
|
+
|
|
344
|
+ workbook.close();
|
|
345
|
+ }
|
|
346
|
+
|
|
347
|
+ return spreadsheetData;
|
|
348
|
+ }
|
|
349
|
+
|
209
|
350
|
|
210
|
351
|
}
|