package com.example.jz.controller;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.baomidou.mybatisplus.extension.api.ApiController;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.example.jz.modle.R;
|
import com.example.jz.modle.entity.User;
|
import com.example.jz.service.UserService;
|
import com.example.jz.utils.Md5Utils;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.io.Serializable;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 用户表(User)表控制层
|
*
|
* @author 安瑾然
|
* @since 2022-07-11 16:35:57
|
*/
|
@RestController
|
@RequestMapping("user")
|
@Api(tags = "用户管理")
|
public class UserController extends ApiController {
|
private UserService userService;
|
|
@Autowired
|
public void setUserService(UserService userService) {
|
this.userService = userService;
|
}
|
|
/**
|
* 分页查询所有数据
|
*
|
* @param page 分页对象
|
* @param user 查询实体
|
* @return 所有数据
|
*/
|
@GetMapping
|
@ApiOperation("分页查询所有数据")
|
public R selectAll(Page<User> page, User user) {
|
return R.ok(this.userService.page(page, new QueryWrapper<User>()
|
.like(StringUtils.isNotBlank(user.getNickName()),"nick_name",user.getNickName())
|
.or()
|
.like(StringUtils.isNotBlank(user.getRealName()),"real_name",user.getRealName())
|
.orderByDesc("ctime")));
|
}
|
|
/**
|
* 通过主键查询单条数据
|
*
|
* @param id 主键
|
* @return 单条数据
|
*/
|
@GetMapping("{id}")
|
@ApiOperation("通过主键查询单条数据")
|
public R selectOne(@PathVariable Serializable id) {
|
return R.ok(this.userService.getById(id));
|
}
|
|
/**
|
* 修改数据
|
*
|
* @param user 实体对象
|
* @return 修改结果
|
*/
|
@PutMapping
|
@ApiOperation("修改用户数据")
|
public R update(@RequestBody User user) {
|
return R.ok(userService.updateById(user));
|
}
|
|
/**
|
* 删除数据
|
*
|
* @param idList 主键结合
|
* @return 删除结果
|
*/
|
@DeleteMapping
|
@ApiOperation("删除用户数据")
|
public R delete(@RequestParam("id") Integer id) {
|
return R.ok(this.userService.removeById(id));
|
}
|
|
/**
|
* 添加管理员
|
*
|
* @param id 用户id
|
* @param username 管理员用户名
|
* @param password 管理员密码
|
* @return
|
*/
|
@PostMapping("/addAdmin/{id}")
|
@ApiOperation("添加管理员")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
|
@ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
|
})
|
public R addAdmin(@PathVariable Serializable id, String username, String password) {
|
User user = userService.getById(id).setLoginUsername(username).setLoginPassword(Md5Utils.md5(password)).setRole(1);
|
return R.ok(userService.updateById(user));
|
}
|
|
@PostMapping("/add")
|
@ApiOperation("添加")
|
public R add(@RequestBody User user) {
|
user.setCtime(new Date());
|
return R.ok(userService.save(user));
|
}
|
|
|
/**
|
* 重置管理员密码
|
*
|
* @param id 用户id
|
* @return
|
*/
|
@GetMapping("/resetPassword/{id}")
|
@ApiOperation("重置管理员密码")
|
public R resetPassword(@PathVariable Serializable id) {
|
User user = userService.getById(id);
|
// 重置初始密码为身份证后六位
|
user.setLoginPassword(Md5Utils.md5(user.getUserIdcard().substring(user.getUserIdcard().length() - 6)));
|
return R.ok(userService.updateById(user));
|
}
|
}
|