|
@@ -1,23 +1,36 @@
|
|
|
package com.ruoyi.web.controller.system;
|
|
|
|
|
|
-import java.util.List;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import com.ruoyi.system.domain.Dangan;
|
|
|
+import com.ruoyi.system.mapper.SysDeptMapper;
|
|
|
+import com.ruoyi.system.mapper.SysUserMapper;
|
|
|
import com.ruoyi.system.service.*;
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.core.io.UrlResource;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.ui.ModelMap;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PathVariable;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import com.ruoyi.common.annotation.Log;
|
|
|
import com.ruoyi.common.constant.UserConstants;
|
|
@@ -36,6 +49,8 @@ import com.ruoyi.common.utils.poi.ExcelUtil;
|
|
|
import com.ruoyi.framework.shiro.service.SysPasswordService;
|
|
|
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
|
|
|
|
|
|
+import static com.ruoyi.common.utils.CacheUtils.put;
|
|
|
+
|
|
|
/**
|
|
|
* 用户信息
|
|
|
*
|
|
@@ -65,6 +80,11 @@ public class SysUserController extends BaseController
|
|
|
@Autowired
|
|
|
private IDanganService danganService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysDeptMapper sysDeptMapper;
|
|
|
+ @Autowired
|
|
|
+ private SysUserMapper sysUserMapper;
|
|
|
+
|
|
|
@RequiresPermissions("system:user:view")
|
|
|
@GetMapping()
|
|
|
public String user(ModelMap modelMap)
|
|
@@ -92,6 +112,160 @@ public class SysUserController extends BaseController
|
|
|
return getDataTable(list);
|
|
|
}
|
|
|
|
|
|
+ @PostMapping("/upload")
|
|
|
+ @ResponseBody
|
|
|
+ public ResponseEntity<Map<String,Object>> uploadFile(@RequestParam("excel") MultipartFile file) {
|
|
|
+ // 检查文件是否为空
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new HashMap<String,Object>(){{
|
|
|
+ put("message", "请上传文件!");
|
|
|
+ }});
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查文件类型
|
|
|
+ if (!file.getContentType().equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
|
|
|
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new HashMap<String,Object>(){{
|
|
|
+ put("message", "请上传Excel文件!");
|
|
|
+ }});
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 将上传的文件转换为POI的工作簿对象以便处理
|
|
|
+ Workbook workbook = new XSSFWorkbook(file.getInputStream());
|
|
|
+ // 处理工作簿...
|
|
|
+ // 例如,可以读取工作簿中的数据
|
|
|
+// 2.得到表。
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
+// // 3.得到行。
|
|
|
+// Row row = sheet.getRow(0);
|
|
|
+// // 4.得到列。
|
|
|
+// Cell cell = row.getCell(0);
|
|
|
+// // 读取值。一定要注意类型,否则会读取失败
|
|
|
+// System.out.println(cell.getStringCellValue());// 字符串类型
|
|
|
+// Cell cell1 = row.getCell(1);
|
|
|
+// System.out.println(cell1.getNumericCellValue());// 数字类型
|
|
|
+ Integer success = 0;//成功数
|
|
|
+ List<String> unDept = new ArrayList<>();//不存在的部门
|
|
|
+ List<String> unUser = new ArrayList<>();//未成功用户
|
|
|
+ List<String> unDoubleUser = new ArrayList<>();//双手机用户
|
|
|
+ for (int i = 0; i < sheet.getPhysicalNumberOfRows()-1; i++) {
|
|
|
+ // 3.得到行。
|
|
|
+ Row row = sheet.getRow(i+1);
|
|
|
+ row.getCell(2).setCellType(CellType.STRING);
|
|
|
+ row.getCell(3).setCellType(CellType.STRING);
|
|
|
+ // 4.得到列。
|
|
|
+ Cell cell = row.getCell(0);//归属机构
|
|
|
+ Cell cell2 = row.getCell(1);//用户姓名
|
|
|
+ Cell cell3 = row.getCell(2);//登录名称
|
|
|
+ Cell cell4 = row.getCell(3);//手机号码
|
|
|
+
|
|
|
+ //TODO 查询归属机构的deptId
|
|
|
+ SysDept sysDept = null;
|
|
|
+ String text = cell.getStringCellValue();
|
|
|
+ if(text.indexOf("(")>=0) {
|
|
|
+ Pattern pattern = Pattern.compile("\\((.*?)\\)");
|
|
|
+ Matcher matcher = pattern.matcher(text);
|
|
|
+ while (matcher.find()) {
|
|
|
+ text = matcher.group(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sysDept = sysDeptMapper.selectDeptByName(text);
|
|
|
+ if(sysDept!=null){
|
|
|
+ try {
|
|
|
+ //TODO 插入用户信息接口, 并建立关联
|
|
|
+ SysUser sysUser = new SysUser();
|
|
|
+ sysUser.setUserName(cell2.getStringCellValue());
|
|
|
+ DecimalFormat df = new DecimalFormat("0");
|
|
|
+ sysUser.setLoginName(cell3.getStringCellValue());
|
|
|
+ sysUser.setPhonenumber(cell4.getStringCellValue());
|
|
|
+ sysUser.setPassword("Lhk" + cell4 + "@2024!");
|
|
|
+ sysUser.setDeptId(sysDept.getDeptId());
|
|
|
+ sysUser.setParentId(sysDept.getParentId());
|
|
|
+ sysUser.setUserType("00");
|
|
|
+ sysUser.setSex("2");
|
|
|
+ sysUser.setUsbkey("1");
|
|
|
+ Integer result = saveUser(sysUser);
|
|
|
+ if (result > 0) {
|
|
|
+ success = success + 1;
|
|
|
+ }
|
|
|
+ }catch(Exception ex){
|
|
|
+ unDoubleUser.add(cell2.getStringCellValue());
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ if(!unDept.contains(text)){
|
|
|
+ unDept.add(text);
|
|
|
+ }
|
|
|
+ if(!unUser.contains(cell2.getStringCellValue())){
|
|
|
+ unUser.add(cell2.getStringCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("未找到部门:"+ Arrays.toString(unDept.toArray()));
|
|
|
+ System.out.println("成功数:"+success);
|
|
|
+ System.out.println("未成功数:"+unUser.size());
|
|
|
+ System.out.println("未成功用户:"+Arrays.toString(unUser.toArray()));
|
|
|
+ System.out.println("双手机未成功用户:"+Arrays.toString(unDoubleUser.toArray()));
|
|
|
+
|
|
|
+ // 5.关闭流。
|
|
|
+ file.getInputStream().close();
|
|
|
+ String message = "成功数:"+success+"未成功数:"+unUser.size()+
|
|
|
+ "未成功用户:"+Arrays.toString(unUser.toArray());
|
|
|
+ return ResponseEntity.status(HttpStatus.OK)
|
|
|
+ .body(new HashMap<String,Object>(){{
|
|
|
+ put("message", message);
|
|
|
+ }});
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new HashMap<String,Object>(){{
|
|
|
+ put("message", "上传文件失败!");
|
|
|
+ }});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private int saveUser(SysUser user){
|
|
|
+ if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(user)))
|
|
|
+ {
|
|
|
+// return error("新增用户'" + user.getLoginName() + "'失败,登录账号已存在");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ else if (StringUtils.isNotEmpty(user.getPhonenumber())
|
|
|
+ && UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
|
|
|
+ {
|
|
|
+// return error("新增用户'" + user.getLoginName() + "'失败,手机号码已存在");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ else if (StringUtils.isNotEmpty(user.getEmail())
|
|
|
+ && UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
|
|
|
+ {
|
|
|
+// return error("新增用户'" + user.getLoginName() + "'失败,邮箱账号已存在").get("code");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ user.setSalt(ShiroUtils.randomSalt());
|
|
|
+ user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
|
|
|
+ user.setCreateBy("admin");
|
|
|
+ return userService.insertUser(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/download-file")
|
|
|
+ @ResponseBody
|
|
|
+ public ResponseEntity<Resource> downloadFile() {
|
|
|
+ // 指定文件路径,这里假设文件位于项目根目录下
|
|
|
+ Path path = Paths.get("人员导入模版.xlsx"); // 修改为你的文件名
|
|
|
+ Resource resource;
|
|
|
+ try {
|
|
|
+ resource = new UrlResource(path.toUri());
|
|
|
+ if (resource.exists() || resource.isReadable()) {
|
|
|
+ return ResponseEntity.ok()
|
|
|
+ .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
|
|
|
+ .contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
+ .body(resource);
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("Could not read the file!");
|
|
|
+ }
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ throw new RuntimeException("Error: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Log(title = "用户管理", businessType = BusinessType.EXPORT)
|
|
|
@RequiresPermissions("system:user:export")
|
|
|
@PostMapping("/export")
|