UserController.java 3.0 KB
package com.tianbo.warehouse.controller;

import com.github.pagehelper.PageInfo;
import com.tianbo.warehouse.annotation.UserPasswordMd5;
import com.tianbo.warehouse.controller.response.ResultJson;
import com.tianbo.warehouse.model.USERS;
import com.tianbo.warehouse.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/user/list")
    public PageInfo<USERS> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1")
                                        int pageNum,
                                @RequestParam(value = "pageSize",required = false,defaultValue = "5")
                                        int pageSize){
        return userService.selectAllUser(pageNum,pageSize);
    }

    public String getusername(){

        //通过session获取当前登录的用户信息
        UserDetails userDetails =(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return "欢迎回来:"+userDetails.getUsername();
    }

    @UserPasswordMd5
    @PutMapping("/user/edit")
    public ResultJson updateUserById(@RequestBody @Valid USERS user){
       int i = userService.updateByPrimaryKeySelective(user);
        ResultJson resultJson = new ResultJson();
        if (1==i){
            resultJson = new ResultJson("200","success");
        }else {
            resultJson = new ResultJson("500","update faild");
        }
       return resultJson;
    }

    @UserPasswordMd5
    @PostMapping("/user/add")
    public ResultJson addUser(@RequestBody @Valid USERS user, HttpServletRequest request, HttpServletResponse response, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
           String s =  bindingResult.toString();
        }

        int i = userService.insertSelective(user);
        ResultJson resultJson = new ResultJson();
        if (1==i){
            resultJson = new ResultJson("200","新建账户成功");
        }else {
            resultJson = new ResultJson("500","insert faild");
        }
        return resultJson;
    }

    @DeleteMapping("/user/del")
    public ResultJson delUser(@RequestBody USERS user,HttpServletRequest request,HttpServletResponse response){
        //String username = getusername();
        int i = userService.deleteByPrimaryKey(user.getUserId());
        ResultJson resultJson = new ResultJson();
        if (1==i){
            resultJson = new ResultJson("200","删除账户成功");
        }else {
            resultJson = new ResultJson("500","delete faild");
        }
        return resultJson;
    }

}