审查视图

src/main/java/com/tianbo/warehouse/controller/UserController.java 4.6 KB
朱兆平 authored
1 2 3
package com.tianbo.warehouse.controller;

import com.github.pagehelper.PageInfo;
朱兆平 authored
4
import com.tianbo.warehouse.annotation.LogAnnotation;
朱兆平 authored
5
import com.tianbo.warehouse.annotation.RequestRequire;
6
import com.tianbo.warehouse.annotation.UserPasswordMd5;
7
import com.tianbo.warehouse.controller.response.ResultJson;
朱兆平 authored
8
import com.tianbo.warehouse.model.USERS;
9
import com.tianbo.warehouse.model.UserRole;
朱兆平 authored
10
import com.tianbo.warehouse.service.UserService;
朱兆平 authored
11 12 13 14
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
朱兆平 authored
15 16 17
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
18
import org.springframework.validation.BindingResult;
19
import org.springframework.web.bind.annotation.*;
朱兆平 authored
20
21 22
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
23
import javax.validation.Valid;
朱兆平 authored
24 25

@RestController
朱兆平 authored
26 27
@RequestMapping("/user")
@Api("swaggerDemoController相关的api")
朱兆平 authored
28 29 30 31 32
public class UserController {

    @Autowired
    UserService userService;
朱兆平 authored
33 34 35
    @ApiOperation(value = "查询用户列表及信息", notes = "查询用户列表及单个用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页-当前页", required = false, dataType = "int",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页-每页显示多少条", required = false, dataType = "int",defaultValue = "5")})
朱兆平 authored
36
    @RequestRequire
朱兆平 authored
37
    @GetMapping("/list")
38
    public ResultJson<PageInfo> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1")
朱兆平 authored
39
                                        int pageNum,
40
                                @RequestParam(value = "pageSize",required = false,defaultValue = "5")
朱兆平 authored
41
                                        int pageSize,
shenhailong authored
42 43
                                @RequestParam(value = "userName",required = false) String username,
                                @RequestParam(value = "realName",required = false) String realname)
朱兆平 authored
44
    {
shenhailong authored
45 46

        PageInfo<USERS> usersPageInfo = userService.selectAllUser(pageNum,pageSize, username,  realname);
47
        return new ResultJson("200","success",usersPageInfo);
朱兆平 authored
48 49
    }
朱兆平 authored
50
    public String getCurrentUser(){
朱兆平 authored
51 52 53

        //通过session获取当前登录的用户信息
        UserDetails userDetails =(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
朱兆平 authored
54
        return userDetails.getUsername();
朱兆平 authored
55
    }
56
朱兆平 authored
57
    @LogAnnotation(moduleName = "用户管理",operate = "用户编辑")
朱兆平 authored
58
    @PutMapping("/edit")
59
    public ResultJson updateUserById(@RequestBody @Valid USERS user){
60 61
       int i = userService.updateByPrimaryKeySelective(user);
        ResultJson resultJson = new ResultJson();
62 63
        return i==1 ? new ResultJson("200","success") :new ResultJson("500","update faild");
64 65
    }
66 67 68 69 70 71 72 73
    @LogAnnotation(moduleName = "用户管理",operate = "用户密码修改")
    @UserPasswordMd5
    @PutMapping("/password")
    public ResultJson updateUserPassById(@RequestBody USERS user){
        int i = userService.updateByPrimaryKeySelective(user);
        return i==1 ? new ResultJson("200","success") :new ResultJson("500","update faild");
    }
74
    @UserPasswordMd5
朱兆平 authored
75
    @LogAnnotation(moduleName = "用户管理",operate = "用户添加")
朱兆平 authored
76
    @PostMapping("/add")
77 78 79 80 81 82
    public ResultJson addUser(@RequestBody @Valid USERS user, HttpServletRequest request, HttpServletResponse response, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
           String s =  bindingResult.toString();
        }
83 84
        int i = userService.insertSelective(user);
        ResultJson resultJson = new ResultJson();
85 86
        return i==1 ? new ResultJson("200","新建账户成功") :new ResultJson("500","insert faild");
87 88
    }
朱兆平 authored
89
    @LogAnnotation(moduleName = "用户管理",operate = "用户删除")
朱兆平 authored
90
    @DeleteMapping("/del")
91 92 93 94
    public ResultJson delUser(@RequestBody USERS user,HttpServletRequest request,HttpServletResponse response){
        //String username = getusername();
        int i = userService.deleteByPrimaryKey(user.getUserId());
        ResultJson resultJson = new ResultJson();
95 96 97 98
        return i==1 ? new ResultJson("200","删除账户成功") :new ResultJson("500","delete faild");

    }
朱兆平 authored
99
    @PutMapping("/roleset")
100 101 102 103
    public ResultJson roleSet(@RequestBody UserRole userRole){

       int i =  userService.setUserRole(userRole);
朱兆平 authored
104
       return i==1 ? new ResultJson("200","设置角色成功") :new ResultJson("500","设置角色失败");
105
106 107
    }
朱兆平 authored
108
}