|
|
@ -2,14 +2,15 @@ package demo.Controller; |
|
|
|
|
|
|
|
import demo.Entity.*; |
|
|
|
import demo.Service.RoleService; |
|
|
|
import demo.Service.UserRoleService; |
|
|
|
import demo.Service.UserService; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate; |
|
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
|
import org.springframework.validation.annotation.Validated; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
import javax.transaction.Transactional; |
|
|
|
import javax.validation.Valid; |
|
|
|
import javax.validation.constraints.Pattern; |
|
|
|
import java.time.Duration; |
|
|
@ -18,7 +19,6 @@ import java.time.LocalDateTime; |
|
|
|
import java.time.ZoneOffset; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@RestController |
|
|
@ -29,6 +29,9 @@ public class UserController { |
|
|
|
@Autowired |
|
|
|
private UserService userService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private UserRoleService userroleService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private StringRedisTemplate redisTemplate; |
|
|
|
|
|
|
@ -43,13 +46,20 @@ public class UserController { |
|
|
|
* @param userVO |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@Transactional(rollbackOn = Exception.class) |
|
|
|
@PostMapping("/register") |
|
|
|
public Result<String> register(@RequestBody UserVO userVO) { |
|
|
|
public Result<String> register(@Valid UserVO userVO, @Valid int roleId) { |
|
|
|
// 查询用户名是否被占用
|
|
|
|
User user = userService.findByUserName(userVO.getLoginName()); |
|
|
|
if (user==null){ |
|
|
|
// 没有占用,进行注册
|
|
|
|
userService.register(userVO.getLoginName(), userVO.getUserChnName(), userVO.getPassword()); |
|
|
|
User newUser = userService.findByUserName(userVO.getLoginName()); |
|
|
|
userroleService.insert(newUser.getId(), roleId); |
|
|
|
UserRole userRole = userroleService.findByUserId(newUser.getId()); |
|
|
|
if(userRole==null){ |
|
|
|
throw new RuntimeException("注册失败"); |
|
|
|
} |
|
|
|
return Result.success("注册成功"); |
|
|
|
}else { |
|
|
|
return Result.error(401, "用户名已被占用"); |
|
|
@ -61,13 +71,21 @@ public class UserController { |
|
|
|
* @param loginName |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@Transactional(rollbackOn = Exception.class) |
|
|
|
@DeleteMapping("/delete") |
|
|
|
public Result<String> delete(@Pattern(regexp = "^\\S{5,16}$") String loginName){ |
|
|
|
// 查询用户名是否被占用
|
|
|
|
User user = userService.findByUserName(loginName); |
|
|
|
if (user!=null){ |
|
|
|
// 存在,进行删除
|
|
|
|
userService.deleteByUserName(loginName); |
|
|
|
User newUser = userService.findByUserName(loginName); |
|
|
|
userroleService.delete(newUser.getId()); |
|
|
|
UserRole userRole = userroleService.findByUserId(user.getId()); |
|
|
|
userroleService.delete(userRole.getId()); |
|
|
|
User newUser1 = userService.findByUserName(loginName); |
|
|
|
if(userRole!=null && newUser1!=null){ |
|
|
|
throw new RuntimeException("删除失败"); |
|
|
|
} |
|
|
|
return Result.success("删除成功"); |
|
|
|
}else { |
|
|
|
return Result.error(401, "用户名不存在"); |
|
|
@ -79,8 +97,9 @@ public class UserController { |
|
|
|
* @param userVo |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@Transactional(rollbackOn = Exception.class) |
|
|
|
@PutMapping("/update") |
|
|
|
public Result<String> update(@Valid UserVO userVo){ |
|
|
|
public Result<String> update(@Valid UserVO userVo, @Valid int roleId){ |
|
|
|
// 查询用户名是否被占用
|
|
|
|
User user = userService.findByUserName(userVo.getLoginName()); |
|
|
|
if (user!=null){ |
|
|
@ -92,6 +111,13 @@ public class UserController { |
|
|
|
// 将Instant转换为Date
|
|
|
|
Date date = Date.from(instant); |
|
|
|
userService.updateByUserName(userVo.getLoginName(), userVo.getUserChnName(), date, userVo.getPassword()); |
|
|
|
UserRole userRole = userroleService.findByUserId(user.getId()); |
|
|
|
if(userRole!=null){ |
|
|
|
userroleService.update(user.getId(), roleId); |
|
|
|
} |
|
|
|
else{ |
|
|
|
throw new RuntimeException("用户角色关联数据不存在,无法更新"); |
|
|
|
} |
|
|
|
return Result.success("更新成功"); |
|
|
|
}else { |
|
|
|
return Result.error(401, "用户名不存在"); |
|
|
@ -149,14 +175,14 @@ public class UserController { |
|
|
|
} |
|
|
|
|
|
|
|
@GetMapping("/query") |
|
|
|
public Result<List<User>> query(){ |
|
|
|
public Result<List<UserDTO>> query(){ |
|
|
|
return Result.success(userService.query()); |
|
|
|
} |
|
|
|
|
|
|
|
@PostMapping("/queryByUserName") |
|
|
|
public Result<User> queryByUserName(@Valid String loginName){ |
|
|
|
User user = userService.queryByUserName(loginName); |
|
|
|
if(user==null){ |
|
|
|
public Result<UserDTO> queryByUserName(@Valid String loginName){ |
|
|
|
UserDTO userDTO = userService.queryByUserName(loginName); |
|
|
|
if(userDTO==null){ |
|
|
|
return Result.error(401, "用户不存在"); |
|
|
|
} |
|
|
|
return Result.success(userService.queryByUserName(loginName)); |
|
|
|