From ba9b6ff9f99e2dbdea8a4759bd98ccd43bbb78a7 Mon Sep 17 00:00:00 2001 From: MaxKey Date: Tue, 16 Nov 2021 21:35:53 +0800 Subject: [PATCH] ExcelImport --- .../main/java/org/maxkey/util/ExcelUtils.java | 35 ++ .../java/org/maxkey/entity/ExcelImport.java | 45 +++ .../service/OrganizationsService.java | 176 ---------- .../persistence/service/UserInfoService.java | 314 +----------------- .../contorller/OrganizationsController.java | 194 +++++++---- .../web/contorller/UserInfoController.java | 168 +++++++++- 6 files changed, 365 insertions(+), 567 deletions(-) create mode 100644 maxkey-common/src/main/java/org/maxkey/util/ExcelUtils.java diff --git a/maxkey-common/src/main/java/org/maxkey/util/ExcelUtils.java b/maxkey-common/src/main/java/org/maxkey/util/ExcelUtils.java new file mode 100644 index 000000000..ab2230a0e --- /dev/null +++ b/maxkey-common/src/main/java/org/maxkey/util/ExcelUtils.java @@ -0,0 +1,35 @@ +package org.maxkey.util; + +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; + +public class ExcelUtils { + + /** + * 根据数据格式返回数据 + * + * @param cell + * @return + */ + public static String getValue(Cell cell) { + + if (cell == null) { + return ""; + } else if (cell.getCellType() == CellType.BOOLEAN) { + return String.valueOf(cell.getBooleanCellValue()); + } else if (cell.getCellType() == CellType.NUMERIC) { + if ("General".equals(cell.getCellStyle().getDataFormatString())) { + return new DecimalFormat("0").format(cell.getNumericCellValue()); + } else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) { + return new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue()); + } else { + return new DecimalFormat("0").format(cell.getNumericCellValue()); + } + } else { + return String.valueOf(cell.getStringCellValue().trim()); + } + } +} diff --git a/maxkey-core/src/main/java/org/maxkey/entity/ExcelImport.java b/maxkey-core/src/main/java/org/maxkey/entity/ExcelImport.java index 6440cffb0..d131a4dad 100644 --- a/maxkey-core/src/main/java/org/maxkey/entity/ExcelImport.java +++ b/maxkey-core/src/main/java/org/maxkey/entity/ExcelImport.java @@ -18,11 +18,18 @@ package org.maxkey.entity; import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.io.IOException; +import java.io.InputStream; + import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import org.apache.mybatis.jpa.persistence.JpaBaseEntity; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.multipart.MultipartFile; /** @@ -48,6 +55,10 @@ public class ExcelImport extends JpaBaseEntity { String updateExist; + InputStream inputStream = null; + + Workbook workbook = null; + public ExcelImport() { super(); } @@ -76,5 +87,39 @@ public class ExcelImport extends JpaBaseEntity { this.excelFile = excelFile; } + public boolean isExcelNotEmpty() { + return excelFile != null && !excelFile.isEmpty() ; + } + + + public Workbook biuldWorkbook() throws IOException { + workbook = null; + inputStream = excelFile.getInputStream(); + if (excelFile.getOriginalFilename().toLowerCase().endsWith(".xls")) { + workbook = new HSSFWorkbook(inputStream); + } else if (excelFile.getOriginalFilename().toLowerCase().endsWith(".xlsx")) { + workbook = new XSSFWorkbook(inputStream); + } else { + throw new RuntimeException("Excel suffix error."); + } + return workbook; + } + + public void closeWorkbook() { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if(workbook != null) { + try { + workbook.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } } diff --git a/maxkey-persistence/src/main/java/org/maxkey/persistence/service/OrganizationsService.java b/maxkey-persistence/src/main/java/org/maxkey/persistence/service/OrganizationsService.java index cca167dc7..a26a87dc3 100644 --- a/maxkey-persistence/src/main/java/org/maxkey/persistence/service/OrganizationsService.java +++ b/maxkey-persistence/src/main/java/org/maxkey/persistence/service/OrganizationsService.java @@ -17,22 +17,10 @@ package org.maxkey.persistence.service; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Comparator; import java.util.List; -import java.util.TreeSet; -import java.util.stream.Collectors; - import org.apache.mybatis.jpa.persistence.JpaBaseService; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.maxkey.entity.Organizations; import org.maxkey.persistence.kafka.KafkaIdentityAction; import org.maxkey.persistence.kafka.KafkaIdentityTopic; @@ -40,10 +28,6 @@ import org.maxkey.persistence.kafka.KafkaPersistService; import org.maxkey.persistence.mapper.OrganizationsMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; -import org.springframework.util.CollectionUtils; -import org.springframework.web.multipart.MultipartFile; - -import com.google.common.collect.Lists; @Repository @@ -94,166 +78,6 @@ public class OrganizationsService extends JpaBaseService{ } return false; } - - public boolean importing(MultipartFile file) { - if(file ==null){ - return false; - } - InputStream is = null; - Workbook wb = null; - List orgsList = null; - try { - is = file.getInputStream(); - - String xls = ".xls"; - String xlsx = ".xlsx"; - int columnSize = 46; - orgsList = Lists.newArrayList(); - if (file.getOriginalFilename().toLowerCase().endsWith(xls)) { - wb = new HSSFWorkbook(is); - } else if (file.getOriginalFilename().toLowerCase().endsWith(xlsx)) { - wb = new XSSFWorkbook(is); - } else { - throw new RuntimeException("maxKey用户导入没有Excel类型"); - } - - int sheetSize = wb.getNumberOfSheets(); - //遍历sheet页 - for (int i = 0; i < sheetSize; i++) { - Sheet sheet = wb.getSheetAt(i); - - int rowSize = sheet.getLastRowNum() + 1; - //遍历行 - for (int j = 1; j < rowSize; j++) { - Row row = sheet.getRow(j); - //略过空行和前3行 - if (row == null || j <3 ) { - continue; - } else { - //其他行是数据行 - Organizations organization =new Organizations(); - - for (int k = 0; k < columnSize; k++) { - if (k == 0) { - // 上级编码 - Cell cell = row.getCell(k); - organization.setParentId(getValue(cell)); - } else if (k == 1) { - // 上级名称 - Cell cell = row.getCell(k); - organization.setParentName(getValue(cell)); - } else if (k == 2) { - // 机构编码 - Cell cell = row.getCell(k); - organization.setId(getValue(cell)); - } else if (k == 3) { - // 机构名称 - Cell cell = row.getCell(k); - organization.setName(getValue(cell)); - } else if (k == 4) { - // 机构全称 - Cell cell = row.getCell(k); - organization.setFullName(getValue(cell)); - } else if (k == 5) { - // 编码路径 - Cell cell = row.getCell(k); - organization.setCodePath(getValue(cell)); - } else if (k == 6) { - // 名称路径 - Cell cell = row.getCell(k); - organization.setNamePath(getValue(cell)); - } else if (k == 7) { - // 机构类型 - Cell cell = row.getCell(k); - organization.setType(getValue(cell)); - } else if (k == 8) { - // 所属分支机构 - Cell cell = row.getCell(k); - organization.setDivision(getValue(cell)); - } else if (k == 9) { - // 级别 - Cell cell = row.getCell(k); - String level=getValue(cell); - organization.setLevel(level.equals("") ? "1" : level); - } else if (k == 10) { - // 排序 - Cell cell = row.getCell(k); - String sortIndex=getValue(cell); - organization.setSortIndex(sortIndex.equals("") ? 1 : Integer.parseInt(sortIndex)); - } else if (k == 11) { - // 联系人 - Cell cell = row.getCell(k); - organization.setContact(getValue(cell)); - } else if (k == 12) { - // 联系电话 - Cell cell = row.getCell(k); - organization.setPhone(getValue(cell)); - }else if (k == 13) { - // 邮箱 - Cell cell = row.getCell(k); - organization.setEmail(getValue(cell)); - }else if (k == 14) { - // 传真 - Cell cell = row.getCell(k); - organization.setFax(getValue(cell)); - }else if (k == 24) { - // 工作-国家 - Cell cell = row.getCell(k); - organization.setCountry(getValue(cell)); - }else if (k == 25) { - // 工作-省 - Cell cell = row.getCell(k); - organization.setRegion(getValue(cell)); - }else if (k == 26) { - // 工作-城市 - Cell cell = row.getCell(k); - organization.setLocality(getValue(cell)); - }else if (k == 27) { - // 工作-地址 - Cell cell = row.getCell(k); - organization.setLocality(getValue(cell)); - }else if (k == 28) { - // 邮编 - Cell cell = row.getCell(k); - organization.setPostalCode(getValue(cell)); - }else if (k == 29) { - // 详细描述 - Cell cell = row.getCell(k); - organization.setDescription(getValue(cell)); - } - } - organization.setStatus(1); - orgsList.add(organization); - } - - } - } - // 数据去重 - if(CollectionUtils.isEmpty(orgsList)){ - orgsList = orgsList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId()))), ArrayList::new)); - } - - } catch (IOException e) { - e.printStackTrace(); - }finally { - if (is != null) { - try { - is.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if(wb != null) { - try { - wb.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - return batchInsert(orgsList); - } /** * 根据数据格式返回数据 diff --git a/maxkey-persistence/src/main/java/org/maxkey/persistence/service/UserInfoService.java b/maxkey-persistence/src/main/java/org/maxkey/persistence/service/UserInfoService.java index 958b97361..4db3de955 100644 --- a/maxkey-persistence/src/main/java/org/maxkey/persistence/service/UserInfoService.java +++ b/maxkey-persistence/src/main/java/org/maxkey/persistence/service/UserInfoService.java @@ -18,25 +18,7 @@ package org.maxkey.persistence.service; -import java.io.IOException; -import java.io.InputStream; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import java.util.TreeSet; -import java.util.stream.Collectors; - import org.apache.mybatis.jpa.persistence.JpaBaseService; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellType; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.maxkey.constants.ConstantsStatus; import org.maxkey.crypto.ReciprocalUtils; import org.maxkey.crypto.password.PasswordReciprocal; @@ -57,10 +39,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Repository; -import org.springframework.util.CollectionUtils; -import org.springframework.web.multipart.MultipartFile; - -import com.google.common.collect.Lists; /** @@ -390,298 +368,8 @@ public class UserInfoService extends JpaBaseService { e.printStackTrace(); } } - - public boolean importing(MultipartFile file) { - if(file ==null){ - return false; - } - InputStream is = null; - Workbook wb = null; - List userInfoList = null; - try { - is = file.getInputStream(); - - String xls = ".xls"; - String xlsx = ".xlsx"; - int columnSize = 46; - userInfoList = Lists.newArrayList(); - if (file.getOriginalFilename().toLowerCase().endsWith(xls)) { - wb = new HSSFWorkbook(is); - } else if (file.getOriginalFilename().toLowerCase().endsWith(xlsx)) { - wb = new XSSFWorkbook(is); - } else { - throw new RuntimeException("maxKey用户导入没有Excel类型"); - } - int recordCount = 0; - int sheetSize = wb.getNumberOfSheets(); - //遍历sheet页 - for (int i = 0; i < sheetSize; i++) { - Sheet sheet = wb.getSheetAt(i); - - int rowSize = sheet.getLastRowNum() + 1; - //遍历行 - for (int j = 1; j < rowSize; j++) { - Row row = sheet.getRow(j); - //略过空行和前3行 - if (row == null || j <3 ) { - continue; - } else { - //其他行是数据行 - UserInfo userInfo = new UserInfo(); - userInfo.setCreatedDate(DateUtils.formatDateTime(new Date())); - - for (int k = 0; k < columnSize; k++) { - if (k == 0) { - // 登录账号 - Cell cell = row.getCell(k); - userInfo.setUsername(getValue(cell)); - } else if (k == 1) { - // 密码 - Cell cell = row.getCell(k); - userInfo.setPassword(getValue(cell)); - } else if (k == 2) { - // 用户显示 - Cell cell = row.getCell(k); - userInfo.setDisplayName(getValue(cell)); - } else if (k == 3) { - // 姓 - Cell cell = row.getCell(k); - userInfo.setFamilyName(getValue(cell)); - } else if (k == 4) { - // 名 - Cell cell = row.getCell(k); - userInfo.setGivenName(getValue(cell)); - } else if (k == 5) { - // 中间名 - Cell cell = row.getCell(k); - userInfo.setMiddleName(getValue(cell)); - } else if (k == 6) { - // 昵称 - Cell cell = row.getCell(k); - userInfo.setNickName(getValue(cell)); - } else if (k == 7) { - // 性别 - Cell cell = row.getCell(k); - String gender = getValue(cell); - userInfo.setGender(gender.equals("")? 1 : Integer.valueOf(getValue(cell))); - } else if (k == 8) { - // 语言偏好 - Cell cell = row.getCell(k); - userInfo.setPreferredLanguage(getValue(cell)); - } else if (k == 9) { - // 时区 - Cell cell = row.getCell(k); - userInfo.setTimeZone(getValue(cell)); - } else if (k == 10) { - // 用户类型 - Cell cell = row.getCell(k); - userInfo.setUserType(getValue(cell)); - } else if (k == 11) { - // 员工编码 - Cell cell = row.getCell(k); - userInfo.setEmployeeNumber(getValue(cell)); - } else if (k == 12) { - // AD域账号 - Cell cell = row.getCell(k); - userInfo.setWindowsAccount(getValue(cell)); - }else if (k == 13) { - // 所属机构 - Cell cell = row.getCell(k); - userInfo.setOrganization(getValue(cell)); - }else if (k == 14) { - // 分支机构 - Cell cell = row.getCell(k); - userInfo.setDivision(getValue(cell)); - }else if (k == 15) { - // 部门编号 - Cell cell = row.getCell(k); - userInfo.setDepartmentId(getValue(cell)); - }else if (k == 16) { - // 部门名称 - Cell cell = row.getCell(k); - userInfo.setDepartment(getValue(cell)); - }else if (k == 17) { - // 成本中心 - Cell cell = row.getCell(k); - userInfo.setCostCenter(getValue(cell)); - }else if (k == 18) { - // 职位 - Cell cell = row.getCell(k); - userInfo.setJobTitle(getValue(cell)); - }else if (k == 19) { - // 级别 - Cell cell = row.getCell(k); - userInfo.setJobLevel(getValue(cell)); - }else if (k == 20) { - // 上级经理 - Cell cell = row.getCell(k); - userInfo.setManager(getValue(cell)); - }else if (k == 21) { - // 助理 - Cell cell = row.getCell(k); - userInfo.setAssistant(getValue(cell)); - }else if (k == 22) { - // 入职时间 - Cell cell = row.getCell(k); - userInfo.setEntryDate(getValue(cell)); - }else if (k == 23) { - // 离职时间 - Cell cell = row.getCell(k); - userInfo.setQuitDate(getValue(cell)); - }else if (k == 24) { - // 工作-国家 - Cell cell = row.getCell(k); - userInfo.setWorkCountry(getValue(cell)); - }else if (k == 25) { - // 工作-省 - Cell cell = row.getCell(k); - userInfo.setWorkRegion(getValue(cell)); - }else if (k == 26) { - // 工作-城市 - Cell cell = row.getCell(k); - userInfo.setTimeZone(getValue(cell)); - }else if (k == 27) { - // 工作-地址 - Cell cell = row.getCell(k); - userInfo.setWorkLocality(getValue(cell)); - }else if (k == 28) { - // 邮编 - Cell cell = row.getCell(k); - userInfo.setWorkPostalCode(getValue(cell)); - }else if (k == 29) { - // 传真 - Cell cell = row.getCell(k); - userInfo.setWorkFax(getValue(cell)); - }else if (k == 30) { - // 工作电话 - Cell cell = row.getCell(k); - userInfo.setWorkPhoneNumber(getValue(cell)); - }else if (k == 31) { - // 工作邮件 - Cell cell = row.getCell(k); - userInfo.setWorkEmail(getValue(cell)); - }else if (k == 32) { - // 证件类型 todo 现在数据库中存储的是tinyint -// Cell cell = row.getCell(k); -// userInfo.setIdType(getValue(cell)); - }else if (k == 33) { - // 证件号码 - Cell cell = row.getCell(k); - userInfo.setIdCardNo(getValue(cell)); - } else if (k == 34) { - // 出生日期 - Cell cell = row.getCell(k); - userInfo.setBirthDate(getValue(cell)); - }else if (k == 35) { - // 婚姻状态 todo 现在数据字段类型是 tinyint -// Cell cell = row.getCell(k); -// userInfo.setMarried(getValue(cell)); - }else if (k == 36) { - // 开始工作时间 - Cell cell = row.getCell(k); - userInfo.setStartWorkDate(getValue(cell)); - }else if (k == 37) { - // 个人主页 - Cell cell = row.getCell(k); - userInfo.setWebSite(getValue(cell)); - }else if (k == 38) { - // 即时通讯 - Cell cell = row.getCell(k); - userInfo.setDefineIm(getValue(cell)); - }else if (k == 39) { - // 国家 - Cell cell = row.getCell(k); - userInfo.setHomeCountry(getValue(cell)); - }else if (k == 40) { - // 省 - Cell cell = row.getCell(k); - userInfo.setHomeRegion(getValue(cell)); - }else if (k == 41) { - // 城市 - Cell cell = row.getCell(k); - userInfo.setHomeLocality(getValue(cell)); - }else if (k == 42) { - // 家庭地址 - Cell cell = row.getCell(k); - userInfo.setHomeStreetAddress(getValue(cell)); - }else if (k == 43) { - // 家庭邮编 - Cell cell = row.getCell(k); - userInfo.setHomePostalCode(getValue(cell)); - }else if (k == 44) { - // 家庭传真 - Cell cell = row.getCell(k); - userInfo.setHomeFax(getValue(cell)); - }else if (k == 45) { - // 家庭电话 - Cell cell = row.getCell(k); - userInfo.setHomePhoneNumber(getValue(cell)); - }else if (k == 46) { - // 家庭邮箱 - Cell cell = row.getCell(k); - userInfo.setHomeEmail(getValue(cell)); - } - } - userInfo.setStatus(1); - userInfoList.add(passwordEncoder(userInfo)); - recordCount ++; - _logger.debug("record {} user {} account {}",recordCount,userInfo.getDisplayName(),userInfo.getUsername()); - } - } - } - // 数据去重 - if(CollectionUtils.isEmpty(userInfoList)){ - userInfoList = userInfoList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getUsername()))), ArrayList::new)); - } - - } catch (IOException e) { - e.printStackTrace(); - }finally { - if (is != null) { - try { - is.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if(wb != null) { - try { - wb.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - return batchInsert(userInfoList); - } - - - /** - * 根据数据格式返回数据 - * - * @param cell - * @return - */ - public static String getValue(Cell cell) { - - if (cell == null) { - return ""; - } else if (cell.getCellType() == CellType.BOOLEAN) { - return String.valueOf(cell.getBooleanCellValue()); - } else if (cell.getCellType() == CellType.NUMERIC) { - if("General".equals(cell.getCellStyle().getDataFormatString())){ - return new DecimalFormat("0").format(cell.getNumericCellValue()); - }else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){ - return new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue()); - }else{ - return new DecimalFormat("0").format(cell.getNumericCellValue()); - } - } else { - return String.valueOf(cell.getStringCellValue().trim()); - } - } + public boolean changeSharedSecret(UserInfo userInfo){ return getMapper().changeSharedSecret(userInfo)>0; diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/OrganizationsController.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/OrganizationsController.java index 9eb93f04e..573cef7b5 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/OrganizationsController.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/OrganizationsController.java @@ -17,14 +17,23 @@ package org.maxkey.web.contorller; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.List; +import java.util.TreeSet; +import java.util.stream.Collectors; import org.apache.mybatis.jpa.persistence.JpaPageResults; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.maxkey.constants.ConstantsOperateMessage; import org.maxkey.entity.ExcelImport; import org.maxkey.entity.Organizations; import org.maxkey.persistence.service.OrganizationsService; +import org.maxkey.util.ExcelUtils; import org.maxkey.web.WebContext; import org.maxkey.web.component.TreeNode; import org.maxkey.web.component.TreeNodeList; @@ -36,6 +45,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -43,17 +53,17 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; +import com.google.common.collect.Lists; + @Controller @RequestMapping({"/orgs"}) public class OrganizationsController { static final Logger _logger = LoggerFactory.getLogger(OrganizationsController.class); - @Autowired OrganizationsService organizationsService; - @ResponseBody @RequestMapping({"/tree"}) public List> organizationsTree(@RequestParam(value = "id", required = false) String id) { @@ -82,8 +92,6 @@ public class OrganizationsController { return treeNodeList.getTreeNodeList(); } - - @RequestMapping({ "/list" }) public ModelAndView orgsTreeList() { return new ModelAndView("orgs/orgsList"); @@ -96,7 +104,6 @@ public class OrganizationsController { } - @RequestMapping({"/orgsSelect/{deptId}/{department}"}) public ModelAndView orgsSelect(@PathVariable("deptId") String deptId, @PathVariable("department") String department) { ModelAndView modelAndView = new ModelAndView("orgs/orgsSelect"); @@ -105,7 +112,6 @@ public class OrganizationsController { return modelAndView; } - @RequestMapping(value = { "/forwardAdd" }) public ModelAndView forwardAdd(@ModelAttribute("org") Organizations org) { ModelAndView modelAndView=new ModelAndView("/orgs/orgsAdd"); @@ -114,9 +120,6 @@ public class OrganizationsController { return modelAndView; } - - - @ResponseBody @RequestMapping({"/add"}) public Message insert(@ModelAttribute("org") Organizations org) { @@ -132,26 +135,16 @@ public class OrganizationsController { return new Message(WebContext.getI18nValue("message.action.insert.success"), MessageType.error); } - - - - - - - - @ResponseBody - @RequestMapping({"/query"}) - public Message query(@ModelAttribute("org") Organizations org) { - _logger.debug("-query :" + org); - if (this.organizationsService.load(org) != null) { - return new Message(WebContext.getI18nValue("message.action.insert.success"), MessageType.success); - } + @ResponseBody + @RequestMapping({"/query"}) + public Message query(@ModelAttribute("org") Organizations org) { + _logger.debug("-query :" + org); + if (this.organizationsService.load(org) != null) { + return new Message(WebContext.getI18nValue("message.action.insert.success"), MessageType.success); + } - return new Message(WebContext.getI18nValue("message.action.insert.error"), MessageType.error); - } - - - + return new Message(WebContext.getI18nValue("message.action.insert.error"), MessageType.error); + } @RequestMapping(value = { "/forwardUpdate/{id}" }) public ModelAndView forwardUpdate(@PathVariable("id") String id) { @@ -162,57 +155,120 @@ public class OrganizationsController { return modelAndView; } - - - @ResponseBody - @RequestMapping({"/update"}) - public Message update(@ModelAttribute("org") Organizations org) { - _logger.debug("-update organization :" + org); - if (this.organizationsService.update(org)) { - return new Message(WebContext.getI18nValue("message.action.update.success"), MessageType.success); - } + @ResponseBody + @RequestMapping({"/update"}) + public Message update(@ModelAttribute("org") Organizations org) { + _logger.debug("-update organization :" + org); + if (this.organizationsService.update(org)) { + return new Message(WebContext.getI18nValue("message.action.update.success"), MessageType.success); + } - return new Message(WebContext.getI18nValue("message.action.update.error"), MessageType.error); - } + return new Message(WebContext.getI18nValue("message.action.update.error"), MessageType.error); + } - - - - - - - @ResponseBody - @RequestMapping({"/delete"}) - public Message delete(@ModelAttribute("org") Organizations org) { - _logger.debug("-delete organization :" + org); - if (this.organizationsService.batchDelete(org.getId())) { - return new Message(WebContext.getI18nValue("message.action.delete.success"), MessageType.success); - } + @ResponseBody + @RequestMapping({"/delete"}) + public Message delete(@ModelAttribute("org") Organizations org) { + _logger.debug("-delete organization :" + org); + if (this.organizationsService.batchDelete(org.getId())) { + return new Message(WebContext.getI18nValue("message.action.delete.success"), MessageType.success); + } - return new Message(WebContext.getI18nValue("message.action.delete.success"), MessageType.error); - } + return new Message(WebContext.getI18nValue("message.action.delete.success"), MessageType.error); + } - - - @RequestMapping({"/orgUsersList"}) public ModelAndView orgUsersList() { return new ModelAndView("orgs/orgUsersList"); } - @RequestMapping(value = "/import") public ModelAndView importing(@ModelAttribute("excelImportFile")ExcelImport excelImportFile) { - ModelAndView modelAndView=new ModelAndView("/orgs/orgsImport"); + if (excelImportFile.isExcelNotEmpty() ) { + try { + int columnSize = 46; + List orgsList = Lists.newArrayList(); + Workbook workbook = excelImportFile.biuldWorkbook(); + int sheetSize = workbook.getNumberOfSheets(); + //遍历sheet页 + for (int i = 0; i < sheetSize; i++) { + Sheet sheet = workbook.getSheetAt(i); + int rowSize = sheet.getLastRowNum() + 1; + for (int j = 1; j < rowSize; j++) {//遍历行 + Row row = sheet.getRow(j); + if (row == null || j <3 ) {//略过空行和前3行 + continue; + } else {//其他行是数据行 + Organizations organization =new Organizations(); + for (int k = 0; k < columnSize; k++) { + if (k == 0) {// 上级编码 + organization.setParentId(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 1) {// 上级名称 + organization.setParentName(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 2) {// 组织编码 + organization.setId(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 3) {// 组织名称 + organization.setName(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 4) {// 组织全称 + organization.setFullName(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 5) {// 编码路径 + organization.setCodePath(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 6) {// 名称路径 + organization.setNamePath(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 7) { // 组织类型 + organization.setType(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 8) {// 所属分支机构 + organization.setDivision(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 9) {// 级别 + String level=ExcelUtils.getValue(row.getCell(k)); + organization.setLevel(level.equals("") ? "1" : level); + } else if (k == 10) {// 排序 + String sortIndex=ExcelUtils.getValue(row.getCell(k)); + organization.setSortIndex(sortIndex.equals("") ? 1 : Integer.parseInt(sortIndex)); + } else if (k == 11) {// 联系人 + organization.setContact(ExcelUtils.getValue(row.getCell(k))); + } else if (k == 12) {// 联系电话 + organization.setPhone(ExcelUtils.getValue(row.getCell(k))); + }else if (k == 13) {// 邮箱 + organization.setEmail(ExcelUtils.getValue(row.getCell(k))); + }else if (k == 14) {// 传真 + organization.setFax(ExcelUtils.getValue(row.getCell(k))); + }else if (k == 24) {// 工作-国家 + organization.setCountry(ExcelUtils.getValue(row.getCell(k))); + }else if (k == 25) {// 工作-省 + organization.setRegion(ExcelUtils.getValue(row.getCell(k))); + }else if (k == 26) {// 工作-城市 + organization.setLocality(ExcelUtils.getValue(row.getCell(k))); + }else if (k == 27) {// 工作-地址 + organization.setLocality(ExcelUtils.getValue(row.getCell(k))); + }else if (k == 28) {// 邮编 + organization.setPostalCode(ExcelUtils.getValue(row.getCell(k))); + }else if (k == 29) {// 详细描述 + organization.setDescription(ExcelUtils.getValue(row.getCell(k))); + } + } + organization.setStatus(1); + orgsList.add(organization); + } + } + } + // 数据去重 + if(!CollectionUtils.isEmpty(orgsList)){ + orgsList = orgsList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId()))), ArrayList::new)); + if(organizationsService.batchInsert(orgsList)) { + new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_SUCCESS), null, MessageType.success, OperateType.add, MessageScope.DB); + }else { + new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_ERROR), MessageType.error); + } + } + } catch (IOException e) { + e.printStackTrace(); + }finally { + excelImportFile.closeWorkbook(); + } + }else { + new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_ERROR), MessageType.error); + } - if (excelImportFile.getExcelFile() != null && !excelImportFile.getExcelFile().isEmpty() && organizationsService.importing(excelImportFile.getExcelFile())) { - new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_SUCCESS), null, MessageType.success, OperateType.add, MessageScope.DB); - }else { - new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_ERROR), MessageType.error); - } - - return modelAndView; + return new ModelAndView("/orgs/orgsImport"); } - - - } diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java index 8bef2662a..82a99a6a6 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java @@ -18,20 +18,32 @@ package org.maxkey.web.contorller; import java.beans.PropertyEditorSupport; +import java.io.IOException; import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Comparator; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.TreeSet; +import java.util.stream.Collectors; import javax.validation.Valid; import org.apache.mybatis.jpa.persistence.JpaPageResults; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.maxkey.constants.ConstantsOperateMessage; import org.maxkey.constants.ConstantsPasswordSetType; import org.maxkey.crypto.ReciprocalUtils; import org.maxkey.entity.ExcelImport; import org.maxkey.entity.UserInfo; import org.maxkey.persistence.service.UserInfoService; +import org.maxkey.util.DateUtils; +import org.maxkey.util.ExcelUtils; import org.maxkey.util.JsonUtils; import org.maxkey.util.StringUtils; import org.maxkey.web.WebContext; @@ -45,6 +57,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; +import org.springframework.util.CollectionUtils; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; @@ -55,6 +68,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; +import com.google.common.collect.Lists; /** * @author Crystal.Sea @@ -90,9 +104,6 @@ public class UserInfoController { return modelAndView; } - - - @RequestMapping(value={"/list"}) public ModelAndView usersList(){ return new ModelAndView("/userinfo/usersList"); @@ -276,15 +287,152 @@ public class UserInfoController { @RequestMapping(value = "/import") public ModelAndView importing(@ModelAttribute("excelImportFile")ExcelImport excelImportFile) { - ModelAndView modelAndView=new ModelAndView("/userinfo/usersImport"); - - if (excelImportFile.getExcelFile() != null && !excelImportFile.getExcelFile().isEmpty() && userInfoService.importing(excelImportFile.getExcelFile())) { - new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_SUCCESS), null, MessageType.success, OperateType.add, MessageScope.DB); + if (excelImportFile.isExcelNotEmpty() ) { + try { + int columnSize = 46; + List userInfoList = Lists.newArrayList(); + Workbook workbook = excelImportFile.biuldWorkbook(); + int recordCount = 0; + int sheetSize = workbook.getNumberOfSheets(); + //遍历sheet页 + for (int i = 0; i < sheetSize; i++) { + Sheet sheet = workbook.getSheetAt(i); + int rowSize = sheet.getLastRowNum() + 1; + for (int j = 1; j < rowSize; j++) {//遍历行 + Row row = sheet.getRow(j); + if (row == null || j <3 ) {//略过空行和前3行 + continue; + } else {//其他行是数据行 + UserInfo userInfo = new UserInfo(); + userInfo.setCreatedDate(DateUtils.formatDateTime(new Date())); + + for (int k = 0; k < columnSize; k++) { + Cell cell = row.getCell(k); + if (k == 0) {// 登录账号 + userInfo.setUsername(ExcelUtils.getValue(cell)); + } else if (k == 1) {// 密码 + userInfo.setPassword(ExcelUtils.getValue(cell)); + } else if (k == 2) {// 用户显示 + userInfo.setDisplayName(ExcelUtils.getValue(cell)); + } else if (k == 3) {// 姓 + userInfo.setFamilyName(ExcelUtils.getValue(cell)); + } else if (k == 4) {// 名 + userInfo.setGivenName(ExcelUtils.getValue(cell)); + } else if (k == 5) {// 中间名 + userInfo.setMiddleName(ExcelUtils.getValue(cell)); + } else if (k == 6) {// 昵称 + userInfo.setNickName(ExcelUtils.getValue(cell)); + } else if (k == 7) {// 性别 + String gender = ExcelUtils.getValue(cell); + userInfo.setGender(gender.equals("")? 1 : Integer.valueOf(ExcelUtils.getValue(cell))); + } else if (k == 8) {// 语言偏好 + userInfo.setPreferredLanguage(ExcelUtils.getValue(cell)); + } else if (k == 9) {// 时区 + userInfo.setTimeZone(ExcelUtils.getValue(cell)); + } else if (k == 10) {// 用户类型 + userInfo.setUserType(ExcelUtils.getValue(cell)); + } else if (k == 11) {// 员工编码 + userInfo.setEmployeeNumber(ExcelUtils.getValue(cell)); + } else if (k == 12) {// AD域账号 + userInfo.setWindowsAccount(ExcelUtils.getValue(cell)); + }else if (k == 13) {// 所属机构 + userInfo.setOrganization(ExcelUtils.getValue(cell)); + }else if (k == 14) {// 分支机构 + userInfo.setDivision(ExcelUtils.getValue(cell)); + }else if (k == 15) {// 部门编号 + userInfo.setDepartmentId(ExcelUtils.getValue(cell)); + }else if (k == 16) {// 部门名称 + userInfo.setDepartment(ExcelUtils.getValue(cell)); + }else if (k == 17) {// 成本中心 + userInfo.setCostCenter(ExcelUtils.getValue(cell)); + }else if (k == 18) {// 职位 + userInfo.setJobTitle(ExcelUtils.getValue(cell)); + }else if (k == 19) { // 级别 + userInfo.setJobLevel(ExcelUtils.getValue(cell)); + }else if (k == 20) {// 上级经理 + userInfo.setManager(ExcelUtils.getValue(cell)); + }else if (k == 21) {// 助理 + userInfo.setAssistant(ExcelUtils.getValue(cell)); + }else if (k == 22) {// 入职时间 + userInfo.setEntryDate(ExcelUtils.getValue(cell)); + }else if (k == 23) { + // 离职时间 + userInfo.setQuitDate(ExcelUtils.getValue(cell)); + }else if (k == 24) {// 工作-国家 + userInfo.setWorkCountry(ExcelUtils.getValue(cell)); + }else if (k == 25) {// 工作-省 + userInfo.setWorkRegion(ExcelUtils.getValue(cell)); + }else if (k == 26) {// 工作-城市 + userInfo.setTimeZone(ExcelUtils.getValue(cell)); + }else if (k == 27) {// 工作-地址 + userInfo.setWorkLocality(ExcelUtils.getValue(cell)); + }else if (k == 28) {// 邮编 + userInfo.setWorkPostalCode(ExcelUtils.getValue(cell)); + }else if (k == 29) {// 传真 + userInfo.setWorkFax(ExcelUtils.getValue(cell)); + }else if (k == 30) {// 工作电话 + userInfo.setWorkPhoneNumber(ExcelUtils.getValue(cell)); + }else if (k == 31) {// 工作邮件 + userInfo.setWorkEmail(ExcelUtils.getValue(cell)); + }else if (k == 32) {// 证件类型 todo 现在数据库中存储的是tinyint +// userInfo.setIdType(ExcelUtils.getValue(cell)); + }else if (k == 33) {// 证件号码 + userInfo.setIdCardNo(ExcelUtils.getValue(cell)); + } else if (k == 34) { + // 出生日期 + userInfo.setBirthDate(ExcelUtils.getValue(cell)); + }else if (k == 35) {// 婚姻状态 todo 现在数据字段类型是 tinyint +// userInfo.setMarried(ExcelUtils.getValue(cell)); + }else if (k == 36) {// 开始工作时间 + userInfo.setStartWorkDate(ExcelUtils.getValue(cell)); + }else if (k == 37) {// 个人主页 + userInfo.setWebSite(ExcelUtils.getValue(cell)); + }else if (k == 38) {// 即时通讯 + userInfo.setDefineIm(ExcelUtils.getValue(cell)); + }else if (k == 39) {// 国家 + userInfo.setHomeCountry(ExcelUtils.getValue(cell)); + }else if (k == 40) {// 省 + userInfo.setHomeRegion(ExcelUtils.getValue(cell)); + }else if (k == 41) {// 城市 + userInfo.setHomeLocality(ExcelUtils.getValue(cell)); + }else if (k == 42) {// 家庭地址 + userInfo.setHomeStreetAddress(ExcelUtils.getValue(cell)); + }else if (k == 43) {// 家庭邮编 + userInfo.setHomePostalCode(ExcelUtils.getValue(cell)); + }else if (k == 44) {// 家庭传真 + userInfo.setHomeFax(ExcelUtils.getValue(cell)); + }else if (k == 45) {// 家庭电话 + userInfo.setHomePhoneNumber(ExcelUtils.getValue(cell)); + }else if (k == 46) {// 家庭邮箱 + userInfo.setHomeEmail(ExcelUtils.getValue(cell)); + } + } + userInfo.setStatus(1); + userInfoList.add(userInfoService.passwordEncoder(userInfo)); + recordCount ++; + _logger.debug("record {} user {} account {}",recordCount,userInfo.getDisplayName(),userInfo.getUsername()); + } + } + } + // 数据去重 + if(!CollectionUtils.isEmpty(userInfoList)){ + userInfoList = userInfoList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getUsername()))), ArrayList::new)); + if( userInfoService.batchInsert(userInfoList)) { + new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_SUCCESS), null, MessageType.success, OperateType.add, MessageScope.DB); + }else { + new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_ERROR), MessageType.error); + } + } + } catch (IOException e) { + e.printStackTrace(); + }finally { + excelImportFile.closeWorkbook(); + } }else { - new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_ERROR), MessageType.error); + new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_ERROR), MessageType.error); } - return modelAndView; + return new ModelAndView("/userinfo/usersImport"); } @InitBinder @@ -305,4 +453,6 @@ public class UserInfoController { dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } + + }