@ApiOperation("新增用户")
@PostMapping
public void saveUser(@RequestBody UserFormDTO userFormDTO) {
User user = new User();
BeanUtils.copyProperties(userFormDTO,user);
user.setCreateTime(LocalDateTime.now());
user.setUpdateTime(LocalDateTime.now());
userService.save(user);
}
2.删除用户
复制代码
@ApiOperation("删除用户")
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.removeById(id);
}
@ApiOperation("根据id查询用户")
@GetMapping("/{id}")
public UserVO getUserById(@PathVariable Long id){
User user=userService.getById(id);
UserVO userVO=new UserVO();
BeanUtils.copyProperties(user,userVO);
return userVO;
}
3.根据id查询用户
复制代码
@ApiOperation("根据id查询用户")
@GetMapping("/{id}")
public UserVO getUserById(@PathVariable Long id){
User user=userService.getById(id);
UserVO userVO=new UserVO();
BeanUtils.copyProperties(user,userVO);
return userVO;
}
@ApiOperation("根据id扣减余额")
@PutMapping("/{id}/deduction/{money}")
public void deductionBalanceById(@PathVariable("id") Long id,@PathVariable("money") int money){
userService.deductBalance(id,money);
}
Service
复制代码
@Autowired
private UserMapper userMapper;
@Override
public void deductBalance(Long id, int money) {
User user = getById(id);
if(user==null||user.getStatus()==2){
throw new RuntimeException("账户被冻结");
}
if (user.getBalance() < money) {
throw new RuntimeException("余额不足");
}
userMapper.deductBalance(id,money);
}
Controller:
复制代码
@ApiOperation("根据复杂条件查询")
@GetMapping("/list")
public List<UserVO> queryUsers(UserQuery query){
List<User>users=userService.queryUsers(query.getName(),query.getStatus(),query.getMinBalance(),query.getMaxBalance());
return BeanUtil.copyToList(users,UserVO.class);
}