| | |
| | | package com.linghu.controller; |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.linghu.model.common.ResponseResult; |
| | | import com.linghu.model.entity.Type; |
| | | import com.linghu.model.entity.Sectionalization; |
| | | import com.linghu.model.entity.User; |
| | | import com.linghu.model.excel.UserExcel; |
| | | import com.linghu.model.vo.UserPageVO; |
| | | import com.linghu.service.SectionalizationService; |
| | | import com.linghu.service.UserService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @RestController |
| | | @RequestMapping("/user") |
| | |
| | | @Autowired |
| | | private UserService userService; |
| | | |
| | | /* |
| | | @PostMapping |
| | | @ApiOperation(value = "添加类型") |
| | | public ResponseResult<User> add(@RequestBody User user) { |
| | | boolean success = typeService.save(type); |
| | | if (success) { |
| | | return ResponseResult.success(type); |
| | | } |
| | | return ResponseResult.error("添加类型失败"); |
| | | } |
| | | */ |
| | | |
| | | |
| | | /** |
| | | * 新增用户 |
| | | */ |
| | | |
| | | @PostMapping |
| | | @ApiOperation(value = "添加") |
| | | public ResponseResult add(@RequestBody User user) { |
| | | List<User> list = userService.list(new LambdaQueryWrapper<User>().eq(User::getUser_email, user.getUser_email())); |
| | | if (list != null && list.size() > 0) { |
| | | return ResponseResult.error("该邮箱已存在"); |
| | | } |
| | | user.setStatus("正常"); |
| | | userService.save(user); |
| | | return ResponseResult.success(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 修改用户 |
| | | */ |
| | | @PutMapping |
| | | @ApiOperation(value = "修改") |
| | | public ResponseResult edit(@RequestBody User user) { |
| | | User user1 = userService.getById(user.getUser_id()); |
| | | if (user1 == null) { |
| | | return ResponseResult.error("该账户不存在"); |
| | | } |
| | | List<User> list = userService.list(new LambdaQueryWrapper<User>() |
| | | .ne(User::getUser_id, user.getUser_id()) |
| | | .eq(User::getUser_email, user.getUser_email())); |
| | | if (list != null && list.size() > 0) { |
| | | return ResponseResult.error("该邮箱已存在"); |
| | | } |
| | | userService.updateById(user); |
| | | return ResponseResult.success(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 删除用户 |
| | | */ |
| | | |
| | | @DeleteMapping("/{user_id}") |
| | | @ApiOperation(value = "删除") |
| | | public ResponseResult delete(@PathVariable("user_id") Integer user_id) { |
| | | userService.removeById(user_id); |
| | | return ResponseResult.success(); |
| | | |
| | | } |
| | | /** |
| | | * 分页查询 |
| | | */ |
| | | @GetMapping |
| | | @ApiOperation(value = "分页") |
| | | public ResponseResult<Page<UserPageVO>> page(@RequestParam(value = "pageSize", required = false, defaultValue = "10")Integer pageSize, |
| | | @RequestParam(value = "pageNum", required = false,defaultValue = "1")Integer pageNum, |
| | | @RequestParam(value = "sectionalization_id",required = false)Integer sectionalization_id, |
| | | @RequestParam(value = "status" ,required = false)String status) { |
| | | Page<UserPageVO> page = new Page<>(pageNum, pageSize); |
| | | return ResponseResult.success( userService.getPage(page,sectionalization_id,status)); |
| | | } |
| | | |
| | | /** |
| | | * 下载模板 |
| | | */ |
| | | |
| | | @GetMapping("/downloadTemplate") |
| | | @ApiOperation("下载模板") |
| | | public ResponseEntity<byte[]> downloadTemplate() throws IOException { |
| | | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| | | EasyExcel.write(out, UserExcel.class).sheet("账号模板").doWrite(new ArrayList<>()); |
| | | |
| | | return ResponseEntity.ok() |
| | | .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=keyword_template.xlsx") |
| | | .contentType(MediaType.APPLICATION_OCTET_STREAM) |
| | | .body(out.toByteArray()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 导入文件 |
| | | */ |
| | | // 导入文件 |
| | | @Autowired |
| | | private SectionalizationService sectionalizationService; |
| | | @PostMapping("/importUserExcel") |
| | | @ApiOperation("导入用户数据") |
| | | public ResponseResult importUserExcel(@RequestParam("file") MultipartFile file) { |
| | | //查找出用户列表 |
| | | List<User> userList = userService.list(); |
| | | //名称 |
| | | Map<String, User> nameMap = userList.stream() |
| | | .collect(Collectors.toMap( |
| | | User::getUser_name, // Key映射 |
| | | excel -> excel, // Value映射 |
| | | (oldValue, newValue) -> oldValue // 键冲突处理(保留旧值) |
| | | )); |
| | | |
| | | //查找出分组列表 |
| | | List<Sectionalization> list = sectionalizationService.list(); |
| | | //分组 |
| | | Map<String, Sectionalization> sectionalizationMap = list.stream() |
| | | .collect(Collectors.toMap( |
| | | Sectionalization::getSectionalization_name, // Key映射 |
| | | excel -> excel, // Value映射 |
| | | (oldValue, newValue) -> oldValue // 键冲突处理(保留旧值) |
| | | )); |
| | | |
| | | try { |
| | | // 检查文件是否为空 |
| | | if (file.isEmpty()) { |
| | | return ResponseResult.error("上传文件不能为空"); |
| | | } |
| | | |
| | | // 读取Excel数据 |
| | | List<UserExcel> excelList = EasyExcel.read(file.getInputStream()) |
| | | .head(UserExcel.class) |
| | | .sheet() |
| | | .doReadSync(); |
| | | |
| | | // 数据转换与验证 |
| | | List<String> errorMessages = new ArrayList<>(); |
| | | for (UserExcel excel : excelList) { |
| | | // 检查必要字段 |
| | | if (!StringUtils.hasText(excel.getUser_name())) { |
| | | errorMessages.add("账户名不能为空"); |
| | | continue; |
| | | } |
| | | if (!StringUtils.hasText(excel.getUser_email())) { |
| | | errorMessages.add("邮箱不能为空"); |
| | | continue; |
| | | } |
| | | if (!StringUtils.hasText(String.valueOf(excel.getPassword()))) { |
| | | errorMessages.add("密码不能为空"); |
| | | continue; |
| | | } |
| | | |
| | | if (!StringUtils.hasText(excel.getSectionalization_name())) { |
| | | errorMessages.add("分组不能为空"); |
| | | continue; |
| | | } |
| | | if (nameMap.containsKey(excel.getUser_name())){ |
| | | errorMessages.add("用户名称重复"); |
| | | continue; |
| | | } |
| | | } |
| | | // 处理错误 |
| | | if (!errorMessages.isEmpty()) { |
| | | return ResponseResult.error("数据验证失败: " + String.join("; ", errorMessages)); |
| | | } |
| | | // 开始添加 |
| | | for (UserExcel excel : excelList) { |
| | | User user = new User(); |
| | | if (sectionalizationMap.containsKey(excel.getSectionalization_name())) { |
| | | user.setSectionalization_id(sectionalizationMap.get(excel.getSectionalization_name()).getSectionalization_id()); |
| | | }else { |
| | | //新增 |
| | | Sectionalization sectionalization = new Sectionalization(); |
| | | sectionalization.setSectionalization_name(excel.getSectionalization_name()); |
| | | sectionalizationService.save(sectionalization); |
| | | user.setSectionalization_id(sectionalization.getSectionalization_id()); |
| | | } |
| | | user.setUser_email(excel.getUser_email()); |
| | | user.setUser_name(excel.getUser_name()); |
| | | user.setPassword(excel.getPassword()); |
| | | user.setStatus("正常"); |
| | | userService.save(user); |
| | | |
| | | } |
| | | // 返回信息 |
| | | return ResponseResult.success(); |
| | | } |
| | | catch (Exception e) { |
| | | // 记录详细异常信息 |
| | | |
| | | return ResponseResult.error("文件解析失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | } |