PermissionJsonContorller.java 3.3 KB
package com.tianbo.controller.json;

import com.tianbo.common.ModelAndPage;
import com.tianbo.model.*;
import com.tianbo.service.PermissionService;
import com.tianbo.service.RolePermissionService;
import com.tianbo.util.dao.Page;
import com.tianbo.util.json.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionJsonContorller {
    //service类
    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @RequestMapping("list.json")
    @ResponseBody
    public ModelAndPage Permissionlist(){ //所有权限列表
        PermissionExample example = new PermissionExample();
        //调用service方法得到用户列表
        List<Permission> pms = permissionService.selectByExample(example);

        ModelAndPage modelAndPage = new ModelAndPage(pms,new Page());

        return modelAndPage;
    }

    @RequestMapping(value = "listByRole.json",method = RequestMethod.POST)
    @ResponseBody
    public ModelAndPage listByRole(@RequestBody RolePermission rolePermission){ //角色权限列表
        BigDecimal testID = new BigDecimal(1);
        RolePermissionExample example = new RolePermissionExample();
        example.createCriteria().andRoleIdEqualTo(testID);
        //调用service方法得到用户列表
        List<RolePermission> rp = rolePermissionService.selectByExample(example);

        ModelAndPage modelAndPage = new ModelAndPage(rp,new Page());

        return modelAndPage;
    }

    @RequestMapping("update.json")
    @ResponseBody
    public ResponseModel insertByroleID(@RequestBody RolePermission rolePermission){
        BigDecimal roleId = rolePermission.getRoleId();

        //先删除以前的权限绑定。
        //再插入新的权限绑定.
        RolePermissionExample delExample = new RolePermissionExample();

        delExample.createCriteria().andRoleIdEqualTo(roleId);
        int delSuccess = rolePermissionService.deleteByExample(delExample);

        if (delSuccess>0){
            //循环取出提交的已选择的Permission id列表,再插入
            for(Integer PmsId:rolePermission.getPmList()){
                RolePermission rolePermission1 = new RolePermission();
                rolePermission1.setRoleId(roleId);
                rolePermission1.setPermissionId(new BigDecimal(PmsId));
                int i = rolePermissionService.insert(rolePermission1);

                if (i<=0){
                    return new ResponseModel(500,"权限更新失败");
                }
            }
        }else {
            return new ResponseModel(500,"权限更新失败");
        }
        return new ResponseModel(200,"权限更新成功");
    }

    @RequestMapping("edit.json")
    @ResponseBody
    public ModelAndPage edit(){
        PermissionExample example = new PermissionExample();
        //调用service方法得到用户列表
        List<Permission> pms = permissionService.selectByExample(example);

        ModelAndPage modelAndPage = new ModelAndPage(pms,new Page());

        return modelAndPage;
    }
}