package com.moral.api.controller;
|
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiOperation;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.util.Map;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.moral.api.entity.User;
|
import com.moral.api.service.UserService;
|
import com.moral.constant.ResponseCodeEnum;
|
import com.moral.constant.ResultMessage;
|
import com.moral.util.PageResult;
|
import com.moral.util.WebUtils;
|
|
@Slf4j
|
@Api(tags = {"用户管理"})
|
@RestController
|
@RequestMapping("/user")
|
public class UserController {
|
|
@Autowired
|
private UserService userService;
|
|
@ApiOperation(value = "添加账户", notes = "添加账户")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
|
})
|
@RequestMapping(value = "insert", method = RequestMethod.POST)
|
public ResultMessage insert(@RequestBody User user) {
|
if (user.getAccount().isEmpty() || user.getPassword().isEmpty()) {
|
return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
|
ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
|
}
|
Map<String, Object> result = userService.addUser(user);
|
if (!result.isEmpty()) {
|
return ResultMessage.fail((int) result.get("code"), result.get("msg").toString());
|
}
|
return ResultMessage.ok();
|
}
|
|
@ApiOperation(value = "删除账户", notes = "删除账户")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "query", dataType = "int"),
|
@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
|
})
|
@RequestMapping(value = "delete", method = RequestMethod.GET)
|
public ResultMessage delete(Integer id) {
|
if (id == null) {
|
return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
|
ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
|
}
|
userService.deleteUser(id);
|
return ResultMessage.ok();
|
}
|
|
@ApiOperation(value = "修改账户信息", notes = "修改账户信息")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
|
})
|
@RequestMapping(value = "update", method = RequestMethod.POST)
|
public ResultMessage update(@RequestBody User user) {
|
if (user.getId() == null) {
|
return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
|
ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
|
}
|
Map<String, Object> result = userService.updateUser(user);
|
if (!result.isEmpty()) {
|
return ResultMessage.fail((int) result.get("code"), result.get("msg").toString());
|
}
|
return ResultMessage.ok();
|
}
|
|
@ApiOperation(value = "分页查询账户列表", notes = "分页查询账户列表")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "page", value = "当前页", required = false, paramType = "query", dataType = "int"),
|
@ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
|
@ApiImplicitParam(name = "order", value = "排序字段", required = false, paramType = "query", dataType = "String"),
|
@ApiImplicitParam(name = "orderType", value = "排序类型,升序:0,降序:1", defaultValue = "0", required = false, paramType = "query", dataType = "String"),
|
@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
|
@ApiImplicitParam(name = "account", value = "账号模糊查询", required = false, paramType = "query", dataType = "String"),
|
@ApiImplicitParam(name = "userName", value = "用户名模糊查询", required = false, paramType = "query", dataType = "String"),
|
@ApiImplicitParam(name = "mobile", value = "手机号模糊查询", required = false, paramType = "query", dataType = "String"),
|
@ApiImplicitParam(name = "email", value = "邮箱模糊查询", required = false, paramType = "query", dataType = "String"),
|
@ApiImplicitParam(name = "wechat", value = "微信模糊查询", required = false, paramType = "query", dataType = "String"),
|
|
})
|
@RequestMapping(value = "select", method = RequestMethod.GET)
|
public ResultMessage select(HttpServletRequest request) {
|
Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
|
if (!parameters.containsKey("page") || !parameters.containsKey("size")) {
|
return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
|
ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
|
}
|
Page<User> userPage = userService.selectUsers(parameters);
|
PageResult<User> pageResult = new PageResult<>(
|
userPage.getTotal(), userPage.getPages(), userPage.getRecords()
|
);
|
return ResultMessage.ok(pageResult);
|
}
|
|
}
|