package com.example.demo.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.*;
import com.example.demo.enums.PwdEnum;
import com.example.demo.enums.RoleEnum;
import com.example.demo.mapper.PermissionMapper;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.UserRoleMapper;
import com.example.demo.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
@Resource
UserMapper userMapper;
@Resource
RoleMapper roleMapper;
@Resource
PermissionMapper permissionMapper;
@Resource
UserRoleMapper userRoleMapper;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder; //注入bcryct加密
@PostMapping("/login")
public Result<?> login(@RequestBody User userParam) {
User userPwd = userMapper.selectByName(userParam.getUsername());
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", userParam.getUsername());
queryWrapper.eq("password", userPwd.getPassword());
User res = userMapper.selectOne(queryWrapper);
// 判断密码是否正确
if (!bCryptPasswordEncoder.matches(userParam.getPassword(), userPwd.getPassword())) {
return Result.error("-1", "密码错误");
}
if (res == null) {
return Result.error("-1", "用户名或密码错误");
}
HashSet<Permission> permissionSet = new HashSet<>();
// 1. 从user_role表通过用户id查询所有的角色信息
Integer userId = res.getId();
List<UserRole> userRoles = roleMapper.getUserRoleByUserId(userId);
// 设置角色id
res.setRoles(userRoles.stream().map(UserRole::getRoleId).distinct().collect(Collectors.toList()));
for (UserRole userRole : userRoles) {
// 2.根据roleId从role_permission表查询出所有的permissionId
List<RolePermission> rolePermissions = permissionMapper.getRolePermissionByRoleId(userRole.getRoleId());
for (RolePermission rolePermission : rolePermissions) {
Integer permissionId = rolePermission.getPermissionId();
// 3. 根据permissionId查询permission信息
Permission permission = permissionMapper.selectById(permissionId);
permissionSet.add(permission);
}
}
// 对资源根据id进行排序
LinkedHashSet<Permission> sortedSet = permissionSet.stream().sorted(Comparator.comparing(Permission::getId)).collect(Collectors.toCollection(LinkedHashSet::new));
//设置当前用户的资源信息
res.setPermissions(sortedSet);
// 生成token
String token = TokenUtils.genToken(res);
res.setToken(token);
return Result.success(res);
}
@PostMapping("/register")
public Result<?> register(@RequestBody User user) {
User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
if (res != null) {
return Result.error("-1", "用户名重复");
}
// if (user.getPassword() == null) {
// user.setPassword("123456");
// }
User userInfo = User.builder()
.username(user.getUsername())
.password(bCryptPasswordEncoder.encode(user.getPassword()))
.nickName("用户" + IdWorker.getId())
.build();
userMapper.insert(userInfo);
UserRole userRole = UserRole.builder()
.userId(userInfo.getId())
.roleId(RoleEnum.USER.getRoleId())
.build();
userRoleMapper.insert(userRole);
return Result.success();
}
@PostMapping
public Result<?> save(@RequestBody User user) {
if (user.getPassword() == null) {
user.setPassword(bCryptPasswordEncoder.encode(PwdEnum.PASSWORD.getPassword()));
}
userMapper.insert(user);
UserRole userRole = UserRole.builder()
.userId(user.getId())
.roleId(RoleEnum.USER.getRoleId())
.build();
userRoleMapper.insert(userRole);
return Result.success();
}
@PutMapping
public Result<?> update(@RequestBody User user) {
userMapper.updateById(user);
return Result.success();
}
@PutMapping("/pass")
public Result<?> pass(@RequestBody Map<String, Object> map) {
User user = userMapper.selectById((Integer) map.get("userId"));
if (user== null) {
return Result.error("-1", "未找到用户");
}
if (!bCryptPasswordEncoder.matches(map.get("password").toString(), user.getPassword())) {
return Result.error("-1", "密码错误");
}
map.put("newPass", (bCryptPasswordEncoder.encode(map.get("newPass").toString())));
userMapper.updatePass(map);
return Result.success();
}
// 改变权限接口
@PutMapping("/changeRole")
public Result<?> changeRole(@RequestBody User user) {
// 先根据角色id删除所有的角色跟权限的绑定关系
roleMapper.deleteRoleByUserId(user.getId());
// 再新增 新的绑定关系
for (Integer roleId : user.getRoles()) {
roleMapper.insertUserRole(user.getId(), roleId);
}
// 获取当前登录用户的角色id列表
User currentUser = getUser();
// 如果当前登录用户的角色列表包含需要修改的角色id,那么就重新登录
if (user.getId().equals(currentUser.getId())) {
return Result.success(true);
}
// 如果不包含,则返回false,不需要重新登录。
return Result.success(false);
}
@DeleteMapping("/{id}")
public Result<?> update(@PathVariable Long id) {
userRoleMapper.deleteByUserRoleId(id);
userMapper.deleteById(id);
return Result.success();
}
@GetMapping("/{id}")
public Result<?> getById(@PathVariable Long id) {
return Result.success(userMapper.selectById(id));
}
@GetMapping("/all")
public Result<?> findAll() {
return Result.success(userMapper.selectList(null));
}
/**
* 统计数据
*
* @return
*/
@GetMapping("/count")
public Result<?> count() {
// User user = getUser(); // 当前登录的用户信息
return Result.success(userMapper.countAddress());
}
/**
* 用户分页列表查询,包含书籍表的一对多查询
*
* @param pageNum
* @param pageSize
* @param search
* @return
*/
@GetMapping
public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(de