package com.warehouse.manage.warehouse_project.controller;
import com.warehouse.manage.warehouse_project.domain.PageBean;
import com.warehouse.manage.warehouse_project.domain.Result;
import com.warehouse.manage.warehouse_project.domain.Role;
import com.warehouse.manage.warehouse_project.domain.User;
import com.warehouse.manage.warehouse_project.domain.ov.UserOv;
import com.warehouse.manage.warehouse_project.mapper.PremissionMapper;
import com.warehouse.manage.warehouse_project.mapper.RoleMapper;
import com.warehouse.manage.warehouse_project.service.UserService;
import com.warehouse.manage.warehouse_project.utils.JwtUtil;
import com.warehouse.manage.warehouse_project.utils.StringTransfer;
import com.warehouse.manage.warehouse_project.utils.ThreadLocalUtil;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/user")
public class UserController {
//操作redis的对象
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private UserService userService;
@Autowired
private RoleMapper roleMapper;
@Autowired
private PremissionMapper premissionMapper;
@PostMapping("/login")
public Result
login(String mobile, String password){
//查询用户名是否存在
User user = userService.findUserByMobile(mobile);
if(user == null){
return Result.error("用户名不存在");
}
if(user.getState() == 0){
throw new RuntimeException("该用户已被禁用");
}
//判断密码是否正确 loginUser对象中的密码是否和password匹配
if(user.getPassword().equals(password)){
//登录成功
Map claims = new HashMap<>();
claims.put("id",user.getId());
claims.put("username",user.getUsername());
String token = JwtUtil.genToken(claims); //token数据
//把token数据存储到redis中
ValueOperations operations = stringRedisTemplate.opsForValue();
operations.set(token,token,12, TimeUnit.HOURS); //代表存储12个小时
return Result.success("登录成功",token);
}
return Result.error("密码错误");
}
//查询用户信息 那个用户登录成功就查询那个用户的信息
@GetMapping("/userInfo")
public Result getUserDetailMessage(){
//在进行拦截的时候已经获取了token信息 如果没有token就不会到这里
Map userMap = ThreadLocalUtil.get();
Integer id = (Integer) userMap.get("id");
UserOv user = userService.findUserById(id);
//
String roleIds = user.getRoleIds();
System.out.println("--------------------");
System.out.println(123);
System.out.println(roleIds);
if("".equals(user.getRoleIds()) || roleIds == null){
return Result.success(user);
}
//将字符串转换为数组
String[] idArr = roleIds.split(",");
List idList = Arrays.stream(idArr)
.map(Integer::valueOf)
.collect(Collectors.toList());
List roles = roleMapper.selectListByIds(idList);
StringBuilder stringBuilder = new StringBuilder();
roles.forEach(role -> {
stringBuilder.append(role.getPermIds() + ",");
});
String oldIdsArr = stringBuilder.toString();
/*System.out.println(oldIdsArr);*/
String newIdsArr = StringTransfer.removeDuplicateChars(oldIdsArr);
/* System.out.println(newIdsArr);*/
String[] perArr = newIdsArr.split(",");
List ids = Arrays.stream(perArr)
.map(Integer::valueOf)
.collect(Collectors.toList());
List strings = premissionMapper.selectListByIds(ids);
/*System.out.println(strings);*/
//
user.setRoles(strings);
return Result.success(user);
}
@GetMapping("/getUserDetail")
public Result getUserDetail(Integer id){
return Result.success(userService.findUserById(id));
}
@PatchMapping("/updatePwd")
public Result updatePwd(@RequestBody Map params,@RequestHeader("Authorization") String token){
//原密码
String oldPwd = params.get("oldPassword");
//新密码
String newPassword = params.get("newPassword");
//从线程中获取数据
Map userMap = ThreadLocalUtil.get();
//获取id根据id查询数据
Integer id = (Integer)userMap.get("id");
//根据id进行查询哟用户
UserOv user = userService.findUserById(id);
if(!oldPwd.equals(user.getPassword())){
return Result.error("原密码填写不正确");
}
//调用修改密码的接口
userService.updatePassword(id,newPassword);
//删除redis中的对应的token
ValueOperations operations = stringRedisTemplate.opsForValue();
operations.getOperations().delete(token);
return Result.success("修改密码成功");
}
//更换头像
@PatchMapping("/updateAvatar")
public Result updateAvatar(@RequestParam @URL String avatarUrl){
userService.updateAvatar(avatarUrl);
return Result.success();
}
@GetMapping("/selectList")
public Result> selectUserList(Integer page, Integer pagesize, Integer departmentId, String keyword){
return Result.success(userService.selectUserList(page,pagesize,departmentId,keyword));
}
@PutMapping("/updateUserRoleIds")
public Result updateUserRoleIds(Integer id,String roleIds){
userService.updateUserRoleIds(id,roleIds);
return Result.success();
}
@PostMapping("/deleteUserById")
public Result deleteUserById(Integer id){
userService.deleteUserById(id);
return Result.success();
}
@PutMapping("/changeUserState")
public Result changeUserState(Integer id,Integer state){
userService.changeUserState(id,state);
return Result.success();
}
//重置密码
@PutMapping("/restPassword")
public Result restPassword(Integer id){
userService.resetPassword(id);
return Result.success();
}
//添加员工
@PostMapping("/addEmployee")
public Result addEmployee(@RequestBody User user){
User abc = userService.findUserByMobile(user.getMobile());
if(abc != null){
throw new RuntimeException("该账号已存在");
}
userService.addEmployee(user);
return Result.success();
}
}