UserJsonController.java 4.3 KB
package com.tianbo.controller.json;

import com.tianbo.common.ModelAndPage;
import com.tianbo.controller.base.BaseController;
import com.tianbo.model.Users;
import com.tianbo.model.UsersExample;
import com.tianbo.service.UserService;
import com.tianbo.util.dao.Page;
import com.tianbo.util.json.ResponseModel;
import com.tianbo.util.tools.MD5Tools;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by mrz on 2017/8/23.
 */
@Controller
@RequestMapping("/user")
public class UserJsonController extends BaseController{

    //service类
    @Autowired
    private UserService userService;

    @RequestMapping("userExample.json")
    @RequiresRoles("admin")
    @ResponseBody
    public Users userExample() {

        Users users = new Users();
        return users;
    }

    @RequestMapping("userById.json")
    @RequiresRoles("admin")
    @ResponseBody
    public Users userList(@ModelAttribute Users user) throws Exception{

        Users users = userService.selectByUserId(user.getUserId());
        return users;
    }


    /**
     * 查找所用用户控制器方法
     * @return
     * @throws Exception
     */
    @RequestMapping("list.json")
    @RequiresRoles("admin")
    @ResponseBody
    public ModelAndPage userList(@ModelAttribute Page frontPage,@ModelAttribute Users user) throws Exception{

        if(frontPage.getBegin()>=0){ defaultDataStart = frontPage.getBegin();}
        if (frontPage.getLength()>0){ defaultPerPage = frontPage.getLength();}
        //调用service方法得到用户列表
        ModelAndPage users = userService.userList(defaultDataStart,defaultPerPage,user);

        return users;
    }

    @RequestMapping(value = "baseUpdate.json",method = RequestMethod.POST)
    @RequiresRoles("admin")
    @RequiresPermissions("user:edit")
    @ResponseBody
    public ResponseModel baseUpdate(@RequestBody Users user){
       user.setUpdatetime(new Date());
       int n =  userService.updateByPrimaryKey(user);
       ResponseModel responseModel = new ResponseModel();

       if (n>0){
           responseModel.setStatus(200);
           responseModel.setMsg("用户资料更新成功");
           return responseModel;
       }else {
           responseModel.setStatus(500);
           responseModel.setMsg("用户资料更新失败");
           return responseModel;
       }

    }

    @RequestMapping(value = "add.json",method = RequestMethod.POST)
    @RequiresRoles("admin")
    @ResponseBody
    public ResponseModel add(@RequestBody Users user){

        ResponseModel responseModel = new ResponseModel();

        if(checkUser(user)){
            String userPW = user.getPassword();
            String PWMD5 = MD5Tools.MD5(userPW);
            user.setPassword(PWMD5);
            user.setCreattime(new Date());
            int n =  userService.insert(user);

            if (n>0){
                responseModel.setStatus(200);
                responseModel.setMsg("用户添加成功");
            }else {

            }
        }else {
            responseModel.setStatus(500);
            responseModel.setMsg("用户添加失败,已存在用户");
        }
        return responseModel;
    }

    @RequestMapping(value = "del.json",method = RequestMethod.POST)
    @RequiresRoles("admin")
    @ResponseBody
    public ResponseModel del(@ModelAttribute Users user){

        ResponseModel responseModel = new ResponseModel();

        int n =  userService.deleteByPrimaryKey(user.getUserId());

        if (n>0){
            responseModel.setStatus(200);
            responseModel.setMsg("用户删除成功");
        }else {
            responseModel.setStatus(500);
            responseModel.setMsg("用户删除失败");
        }
        return responseModel;
    }

    private Boolean checkUser( Users user){
        if (userService.checkUser(user)){
            return  Boolean.FALSE; //存在用户,返回false
        }else {
            return Boolean.TRUE;  //不存在用户,返回true,可以添加用户
        }
    }


}