package com.lz; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.google.common.collect.Lists; import com.lz.common.utils.DateUtils; import com.lz.common.utils.StringUtil; import com.lz.modules.app.entity.*; import com.lz.modules.app.enums.ExcelStaffHeardEnum; import com.lz.modules.app.enums.GenderEnum; import com.lz.modules.app.enums.MaritalStatusEnum; import com.lz.modules.app.service.*; import com.lz.modules.job.business.FeishuBusiness; import com.lz.modules.sys.entity.SysCaptchaEntity; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.util.HashMap; import java.util.List; import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest public class FumeiaiTest { @Autowired private FeishuBusiness feishuBusiness; @Test public void contextLoads() throws IOException, InvalidFormatException { feishuBusiness.getFeishuDepartmentsIntoData(); File xlsFile = new File("/Users/fumeiai/tmp/员工档案字段表-0427.xlsx"); List> sheet12List = Lists.newArrayList(); List> sheet3List = Lists.newArrayList(); List> sheet4List = Lists.newArrayList(); // 获得工作簿 Workbook workbook = WorkbookFactory.create(xlsFile); // 获得工作表个数 int sheetCount = workbook.getNumberOfSheets(); // 遍历工作表 for (int i = 0; i < sheetCount; i++) { Sheet sheet = workbook.getSheetAt(i); // 获得行数 int rows = sheet.getLastRowNum() + 1; // 获得列数,先获得一行,在得到改行列数 Row tmp = sheet.getRow(0); if (tmp == null) { continue; } int cols = tmp.getPhysicalNumberOfCells(); List headList = Lists.newArrayList(); // 读取数据 for (int row = 0; row < rows; row++) { Row r = sheet.getRow(row); Map dataMap = new HashMap(); for (int col = 0; col < cols; col++) { String cellValue = getCellValue(r.getCell(col)); //System.out.print(" " + cellValue); if (row == 0) { headList.add(cellValue); } else { if (StringUtil.equals(GenderEnum.female.getName(), cellValue) || StringUtil.equals(GenderEnum.male.getName(), cellValue)) { cellValue = GenderEnum.findCodeByName(cellValue).toString(); } else if (StringUtil.equals(MaritalStatusEnum.unmarried.getName(), cellValue) || StringUtil.equals(MaritalStatusEnum.married.getName(), cellValue)) { cellValue = MaritalStatusEnum.findCodeByName(cellValue).toString(); } dataMap.put(ExcelStaffHeardEnum.findFieldByName(headList.get(col)), cellValue); } } if (dataMap.size() > 0) { if (i == 1) { dataMap.put("staffStatus", "1"); } switch (i) { case 0: case 1: sheet12List.add(dataMap); break; case 2: sheet3List.add(dataMap); break; case 3: sheet4List.add(dataMap); break; default: break; } } } } enterDatabase(sheet12List, sheet3List, sheet4List); } @Resource StaffService staffService; @Resource StaffOccupationService staffOccupationService; @Resource StaffEducationService staffEducationService; @Resource StaffProjectExperienceService staffProjectExperienceService; @Resource StaffWorkTransferRecordService staffWorkTransferRecordService; public void enterDatabase(List> sheet12List, List> sheet3List, List> sheet4List) { List staffs = Lists.newArrayList(); List staffOccupations = Lists.newArrayList(); List staffEducations = Lists.newArrayList(); List staffProjectExperiences = Lists.newArrayList(); List staffWorkTransferRecords = Lists.newArrayList(); for (Map map : sheet12List) { StaffEntity staffEntity = JSON.parseObject(JSON.toJSONString(map), StaffEntity.class); StaffOccupationEntity occupationEntity = JSON.parseObject(JSON.toJSONString(map), StaffOccupationEntity.class); StaffEducationEntity educationEntity = JSON.parseObject(JSON.toJSONString(map), StaffEducationEntity.class); // staffs.add(JSON.parseObject(JSON.toJSONString(map), StaffEntity.class)); // staffOccupations.add(JSON.parseObject(JSON.toJSONString(map), StaffOccupationEntity.class)); // staffEducations.add(JSON.parseObject(JSON.toJSONString(map), StaffEducationEntity.class)); // for (Map.Entry element : map.entrySet()) { // System.out.print(element.getKey() + "=" + element.getValue() + " "); // } StaffEntity Staff = staffService.getByName(staffEntity.getName()); if (Staff == null) { staffService.save(staffEntity); } else { staffEntity.setId(Staff.getId()); staffService.updateById(staffEntity); } StaffOccupationEntity occupation = staffOccupationService.getOne(new QueryWrapper().eq("staff_id", staffEntity.getId())); if (occupation == null) { occupationEntity.setStaffId(staffEntity.getId()); staffOccupationService.save(occupationEntity); } else { occupationEntity.setId(occupation.getId()); staffOccupationService.updateById(occupationEntity); } StaffEducationEntity education = staffEducationService.getOne(new QueryWrapper().eq("staff_id", staffEntity.getId())); if (education == null) { educationEntity.setStaffId(staffEntity.getId()); staffEducationService.save(educationEntity); } else { educationEntity.setId(education.getId()); staffEducationService.updateById(educationEntity); } // // // System.out.println(JSON.parseObject(JSON.toJSONString(map), StaffEntity.class).toString()); // System.out.println(JSON.parseObject(JSON.toJSONString(map), StaffOccupationEntity.class).toString()); // System.out.println(JSON.parseObject(JSON.toJSONString(map), StaffEducationEntity.class).toString()); } for (Map map : sheet3List) { StaffProjectExperienceEntity staffProjectExperienceEntity = JSON.parseObject(JSON.toJSONString(map), StaffProjectExperienceEntity.class); StaffEntity Staff = staffService.getByName(map.get("name")); if (Staff == null) { continue; } else { staffProjectExperienceService.update(new UpdateWrapper().set("is_delete", 1).eq("staff_id", Staff.getId())); StaffProjectExperienceEntity projectExperienceEntity = staffProjectExperienceService.getOne(new QueryWrapper().eq("staff_id", Staff.getId()).eq("project_name", staffProjectExperienceEntity.getProjectName())); if (projectExperienceEntity == null) { staffProjectExperienceEntity.setStaffId(Staff.getId()); staffProjectExperienceService.save(staffProjectExperienceEntity); } else { staffProjectExperienceEntity.setId(projectExperienceEntity.getId()); staffProjectExperienceEntity.setIsDelete(0); staffProjectExperienceService.updateById(staffProjectExperienceEntity); } } // staffProjectExperiences.add(JSON.parseObject(JSON.toJSONString(map), StaffProjectExperienceEntity.class)); // for (Map.Entry element : map.entrySet()) { // System.out.print(element.getKey() + "=" + element.getValue() + " "); // } // System.out.println(JSON.parseObject(JSON.toJSONString(map), StaffProjectExperienceEntity.class).toString()); } for (Map map : sheet4List) { StaffWorkTransferRecordEntity staffWorkTransferRecordEntity = JSON.parseObject(JSON.toJSONString(map), StaffWorkTransferRecordEntity.class); StaffEntity Staff = staffService.getByName(map.get("name")); if (Staff == null) { continue; } else { staffWorkTransferRecordService.update(new UpdateWrapper().set("is_delete", 1).eq("staff_id", Staff.getId())); StaffWorkTransferRecordEntity workTransferRecordEntity = staffWorkTransferRecordService.getOne(new QueryWrapper().eq("staff_id", Staff.getId()).eq("transfer_time", staffWorkTransferRecordEntity.getTransferTime())); if (workTransferRecordEntity == null) { staffWorkTransferRecordEntity.setStaffId(Staff.getId()); staffWorkTransferRecordService.save(staffWorkTransferRecordEntity); } else { staffWorkTransferRecordEntity.setId(workTransferRecordEntity.getId()); staffWorkTransferRecordEntity.setIsDelete(0); staffWorkTransferRecordService.updateById(staffWorkTransferRecordEntity); } } // staffWorkTransferRecords.add(JSON.parseObject(JSON.toJSONString(map), StaffWorkTransferRecordEntity.class)); // for (Map.Entry element : map.entrySet()) { // System.out.print(element.getKey() + "=" + element.getValue() + " "); // } // System.out.println(JSON.parseObject(JSON.toJSONString(map), StaffWorkTransferRecordEntity.class).toString()); } } /** * 功能描述: 获取单元格值 * * @param cell 单元格 * @return String */ public String getCellValue(Cell cell) { String value = ""; if (cell != null) { switch (cell.getCellTypeEnum()) { case STRING: value = cell.getRichStringCellValue().getString(); break; case NUMERIC: //System.out.println(" "+cell.getCellStyle().getDataFormatString()+" "); if ("yyyy/m/d;@".equals(cell.getCellStyle().getDataFormatString()) || "yyyy\\-mm\\-dd".equals(cell.getCellStyle().getDataFormatString()) || "yyyymmdd".equals(cell.getCellStyle().getDataFormatString())) { value = DateUtils.format(cell.getDateCellValue()); } else { DecimalFormat df = new DecimalFormat("#"); value = df.format(cell.getNumericCellValue()); } break; case BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; default: break; } } return value.trim(); } }