package com.zbooksoft.gdmis.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ruili.wcp.common.EncryptUtil; import com.ruili.wcp.data.entity.management.Dept; import com.ruili.wcp.data.entity.management.Role; import com.ruili.wcp.data.entity.management.User; import com.ruili.wcp.service.config.ImportLogService; import com.ruili.wcp.service.management.DeptService; import com.ruili.wcp.service.management.RoleService; import com.ruili.wcp.service.management.UserService; import com.ruili.wcp.web.common.LicenseException; import com.ruili.wcp.web.model.AjaxResponse; import com.ruili.wcp.web.model.ErrorInfo; 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.xssf.usermodel.XSSFWorkbook; import org.apache.shiro.authz.annotation.RequiresUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import static com.ruili.wcp.web.controller.general.DataImportController.getExcelValue; /** * @Description: * @Author: zhai * @Date: 2025/11/25 **/ @Controller @RequestMapping("/ghUser") public class GhUserController { @Autowired DeptService deptService; @Autowired UserService userService; @Autowired RoleService roleService; private static final Logger logger = LoggerFactory.getLogger(GhUserController.class); /** * xml导入页面 * * @return */ @RequestMapping(value = "/userIndex") @RequiresUser public ModelAndView wasm() { ModelAndView mv = new ModelAndView("gh/user/userIndex"); return mv; } /** * @param file 0 导入转孔信息 1 导入图幅信息 2 导入实物案卷信息 * @return * @throws LicenseException */ @RequestMapping({"/importExcel"}) @ResponseBody @RequiresUser public Object importExcel(@RequestParam(value = "file", required = false) MultipartFile file) throws LicenseException { try { readExcel(file); return new AjaxResponse(true); } catch (Exception e) { logger.error(e.getMessage(), e); return new AjaxResponse(new ErrorInfo("导入失败"), false); } } private void readExcel(MultipartFile file) throws IOException { String originalFilename = file.getOriginalFilename(); InputStream inputStream = file.getInputStream(); if (originalFilename.matches("^.+\\.(?i)(xls)$")) { //创建工作簿对象 // HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream); // HashMap map = readProject2003(hssfWorkbook); // readExcel2003(hssfWorkbook, map); } if (originalFilename.matches("^.+\\.(?i)(xlsx)$")) { //创建工作簿对象 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream); insertDept(xssfWorkbook.getSheetAt(1)); insertUser(xssfWorkbook.getSheetAt(0)); } } private void insertDept(Sheet sheet) { int maxRow = sheet.getLastRowNum(); for (int j = 1; j <= maxRow; j++) { try { Row newRow = sheet.getRow(j); Dept dept = new Dept(); dept.setIcon("fa fa-address-card"); dept.setDeptType(0); dept.setSortNum(j); Cell deptName = newRow.getCell(1); String deptNameString = getExcelValue(deptName); String[] split1 = deptNameString.split("\\."); dept.setDeptName(split1[split1.length - 1]); dept.setDeptExtendStringField5(deptNameString); Cell deptNum = newRow.getCell(2); dept.setDeptNum(getExcelValue(deptNum)); Cell deptCode = newRow.getCell(3); String deptCodeString = getExcelValue(deptCode); dept.setDeptCode(deptCodeString); String[] split = deptCodeString.split("\\."); String parentDeptCode = ""; if (split.length > 1) { if (split.length == 2) { parentDeptCode = split[0]; } if (split.length == 3) { parentDeptCode = split[0] + "." + split[1]; } QueryWrapper deptQueryWrapper = new QueryWrapper<>(); deptQueryWrapper.eq("dept_code", parentDeptCode); List list = deptService.list(deptQueryWrapper); if (list.size() > 0) { dept.setParentId(list.get(0).getDeptId()); } } else { dept.setParentId(0L); } Cell oaDeptId = newRow.getCell(0); dept.setDeptExtendStringField1(getExcelValue(oaDeptId)); deptService.saveOrUpdate(dept); } catch (Exception e) { logger.error(e.getMessage(), e); } } } private void insertUser(Sheet sheet) { int maxRow = sheet.getLastRowNum(); for (int j = 1; j <= maxRow; j++) { Row newRow = sheet.getRow(j); User user = new User(); Cell userName = newRow.getCell(1); user.setUserName(getExcelValue(userName)); Cell mobile = newRow.getCell(4); user.setMobilePhone(getExcelValue(mobile)); String mobileString = getExcelValue(mobile); if (mobileString.length() >= 4) { String pwd = mobileString.substring(mobileString.length() - 4, mobileString.length()); String newPwd = "gh@" + pwd; String md5Pwd = EncryptUtil.getInstance().MD5(newPwd); user.setPassword(md5Pwd); } else { String newPwd = "gh@0000"; String md5Pwd = EncryptUtil.getInstance().MD5(newPwd); user.setPassword(md5Pwd); } Cell trueName = newRow.getCell(2); user.setTrueName(getExcelValue(trueName)); Byte enableLogin = 0; user.setEnableLogin(enableLogin); user.setUserType(1); user.setCreateTime(new Date()); Cell deptName = newRow.getCell(3); String deptNameString = getExcelValue(deptName); // String[] split = deptNameString.split("\\."); // String deptCode = split[split.length - 1]; LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Dept::getDeptExtendStringField5, deptNameString); Dept dept = deptService.getOne(wrapper); user.setDeptId(dept.getDeptId()); user.setDeptName(dept.getDeptName()); Cell oldId = newRow.getCell(0); user.setUserExtendStringField1(getExcelValue(oldId)); //插入系统用户表 userService.saveUser(user); //初始化用户默认角色 List roles = roleService.addUserToDefaultRole(user.getUserId()); } } }