通用后台管理系统(4)-编写权限接口、实现、控制器、

1、权限接口类

package com.sundablog.service.backend.system.upms.permissions;

import java.util.List;

import com.sundablog.pojo.AdminPermission;
import com.sundablog.result.LayUiResult;
import com.sundablog.result.ZTreeResult;

/**
 * 权限接口类
 * @ClassName:  PermissionsService   
 * @Description:权限接口类   
 * @author: 哒哒 
 * @date:   2018年3月14日 下午6:54:53   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
public interface PermissionsService {
    
    /**
     * 添加一条权限
     * @Title: addPermissions   
     * @Description: 添加一条权限   
     * @param: @param adminPermission 
     * @param: @return      
     * @return: 
     * @throws
     */
    int addPermissions(AdminPermission adminPermission);
    /**
     * 修改一条权限数据
     * @Title: updataPermissions   
     * @Description: 修改一条权限数据   
     * @param: @param adminPermission
     * @param: @return      
     * @return: int      
     * @throws
     */
    int updataPermissions(AdminPermission adminPermission);
    /**
     * 禁用一条权限(注意:如果当前状态是禁用,如调用此方法会变成启用)
     * @Title: updataDisablePermissionId   
     * @Description: 禁用一条权限(注意:如果当前状态是禁用,如调用此方法会变成启用)  
     * @param: @param permissionId
     * @param: @return      
     * @return: int      
     * @throws
     */
    int updataDisablePermissionId(int permissionId);
    
    /**
     * 根据permissionId查询一条数据
     * @Title: updataPermissionsByPermissionId   
     * @Description: 根据permissionId查询一条数据 
     * @param: @param permissionId
     * @param: @return      
     * @return: int      
     * @throws
     */
    AdminPermission selectPermissionsByPermissionId(int permissionId);
    
    /**
     * 删除一条权限
     * @Title: deletePermission   
     * @Description: 删除一条权限   
     * @param: @param permissionId
     * @param: @return      
     * @return: int      
     * @throws
     */
    int deletePermission(int permissionId);
    
    
    /**
     * 获取全部权限
     * @Title: findPagingQuery   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @return      
     * @return: LayUiResult      
     * @throws
     */
    LayUiResult findPagingQuery();
    
    /**
     * 查询全部权限返回ztree需要的结构
     * @Title: selectPermissions   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @return      
     * @return: List ztree的树形结构     
     * @throws
     */
    List selectPermissions();
    
}

2、权限接口实现类

package com.sundablog.service.impl.backend.system.upms.permissions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundablog.mapper.AdminPermissionMapper;
import com.sundablog.mapper.AdminRolePermissionMapper;
import com.sundablog.pojo.AdminPermission;
import com.sundablog.pojo.AdminPermissionExample;
import com.sundablog.pojo.AdminRolePermissionExample;
import com.sundablog.pojo.AdminPermissionExample.Criteria;
import com.sundablog.result.LayUiResult;
import com.sundablog.result.ZTreeResult;
import com.sundablog.service.backend.system.upms.filesystem.FilesyStemService;
import com.sundablog.service.backend.system.upms.permissions.PermissionsService;

/**
 * 权限接口实现类
 * 
 * @ClassName: PermissionsServiceImpl
 * @Description:权限接口实现类
 * @author: 哒哒
 * @date: 2018年3月14日 下午7:01:46
 * 
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class PermissionsServiceImpl implements PermissionsService {

    @Autowired
    private AdminPermissionMapper adminPermissionMapper;
    
    @Autowired
    private AdminRolePermissionMapper adminRolePermissionMapper;
    
    /**
     * 添加一条权限
     * Title: addPermissions   
     * Description:添加一条权限    
     * @param adminPermission
     * @return   
     * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#addPermissions(com.sundablog.pojo.AdminPermission)
     */
    @Override
    public int addPermissions(AdminPermission adminPermission) {
        //添加根目录默认pid为0
        if (null == adminPermission.getPid()) {
            adminPermission.setPid(0);
        }
        //状态默认正常
        adminPermission.setStatus((byte) 1);
        //设置时间为现在时间
        adminPermission.setCtime(new Date());
        int i = adminPermissionMapper.insertSelective(adminPermission);
        return i;
    }
    
    /**
     * 修改一条权限
     * Title: updataPermissions   
     * Description:修改一条权限
     * @param adminPermission
     * @return   
     * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#updataPermissions(com.sundablog.pojo.AdminPermission)
     */
    @Override
    public int updataPermissions(AdminPermission adminPermission) {
        int i = adminPermissionMapper.updateByPrimaryKeySelective(adminPermission);
        return i;
    }
    
    /**
     * 禁用一条权限
     * Title: updataDisablePermissionId   
     * Description:禁用一条权限  
     * @param permissionId
     * @return   
     * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#updataDisablePermissionId(int)
     */
    @Override
    public int updataDisablePermissionId(int permissionId) {
        AdminPermission adminPermission = adminPermissionMapper.selectByPrimaryKey(permissionId);
        if (0 == adminPermission.getStatus().intValue()) {
            adminPermission.setStatus((byte) 1);
        } else {
            adminPermission.setStatus((byte) 0);
        }
        int i = adminPermissionMapper.updateByPrimaryKeySelective(adminPermission);
        return i;
    }

    /**
     * 根据permissionId查询一条数据
     * Title: updataPermissionsByPermissionId   
     * Description:    
     * @param permissionId
     * @return   
     * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#updataPermissionsByPermissionId(int)
     */
    @Override
    public AdminPermission selectPermissionsByPermissionId(int permissionId) {
        AdminPermission permission = adminPermissionMapper.selectByPrimaryKey(permissionId);
        return permission;
    }
    
    /**
     * 获取全部权限
     * Title: findPagingQuery   
     * Description:    
     * @return   
     * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#findPagingQuery()
     */
    @Override
    public LayUiResult findPagingQuery() {
        AdminPermissionExample example = new AdminPermissionExample();
        List list = adminPermissionMapper.selectByExample(example);
        for (AdminPermission adminPermission : list) {
        }
        
        
        LayUiResult layUiResult = new LayUiResult();
        layUiResult.setCode(0);
        layUiResult.setMsg("");
        layUiResult.setCount(0);
        layUiResult.setData(list);
        return layUiResult;
    }

    /**
     * 删除一条权限
     * Title: deletePermission   
     * Description:    
     * @param permissionId
     * @return   
     * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#deletePermission(int)
     */
    @Override
    public int deletePermission(int permissionId) {
        AdminPermissionExample example = new AdminPermissionExample();
        Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(permissionId);
        List list = adminPermissionMapper.selectByExample(example);
        if (list.size() > 0) {
            //删除角色权限关联表的数据
            for (AdminPermission adminPermission : list) {
                AdminRolePermissionExample rolePermissionExample = new AdminRolePermissionExample();
                com.sundablog.pojo.AdminRolePermissionExample.Criteria rolePermissionExampleCriteria = rolePermissionExample.createCriteria();
                rolePermissionExampleCriteria.andPermissionIdEqualTo(adminPermission.getPermissionId());
                adminRolePermissionMapper.deleteByExample(rolePermissionExample);
            }
            //在删除权限表在数据
            AdminPermissionExample permissionExample = new AdminPermissionExample();
            Criteria createCriteria = permissionExample.createCriteria();
            createCriteria.andPidEqualTo(permissionId);
            int i = adminPermissionMapper.deleteByExample(permissionExample);
            return i;
        } else {
            int i = adminPermissionMapper.deleteByPrimaryKey(permissionId);
            return i;
        }
        
        
        
    }
    /**
     * 查询全部权限返回ztree需要的结构
     * Title: selectPermissions   
     * Description:    
     * @return   
     * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#selectPermissions()
     */
    @Override
    public List selectPermissions() {
        AdminPermissionExample example = new AdminPermissionExample();
        List list = adminPermissionMapper.selectByExample(example);
        List zTreeResults = new ArrayList<>();
        for (AdminPermission adminPermission : list) {
            ZTreeResult zTreeResult = new ZTreeResult();
            zTreeResult.setId(adminPermission.getPermissionId());
            zTreeResult.setpId(adminPermission.getPid());
            zTreeResult.setName(adminPermission.getName());
            zTreeResults.add(zTreeResult);
        }
        return zTreeResults;
    }

}

3、权限控制器类

package com.sundablog.controller.backend.system.upms.permissions;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sundablog.pojo.AdminPermission;
import com.sundablog.pojo.AdminRoleExample;
import com.sundablog.pojo.AdminUser;
import com.sundablog.result.BaseResult;
import com.sundablog.result.LayUiResult;
import com.sundablog.result.ZTreeResult;
import com.sundablog.service.backend.system.upms.permissions.PermissionsService;
import com.sundablog.utlis.RedisUtil;

/**
 * 权限管理控制器
 * @ClassName:  PermissionsController   
 * @Description:权限管理控制器   
 * @author: 哒哒 
 * @date:   2018年3月14日 下午7:10:13   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("/backend/system/upms/permissions")
public class PermissionsController {

    @Autowired
    private PermissionsService permissionsService;
    
    /**
     * 权限界面显示
     * @Title: index   
     * @Description: 界面显示 
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_permissions_index")
    @RequestMapping("/index")
    public String index() { 
        return "backend/system/upms/permissions/index";
    }
    
    /**
     * 添加根界面显示
     * @Title: add   
     * @Description: 添加界面显示   
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_permissions_add")
    @RequestMapping("/add")
    public String add() {
        //清空Redis
        AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
        RedisUtil.remove("backend_menu_"+user.getUserId());
        RedisUtil.remove("backend_reaim_"+user.getUserId());
        
        return "backend/system/upms/permissions/add";
    }
    
    /**
     * 添加子菜单界面显示
     * @Title: addPermissions   
     * @Description: 添加子菜单界面显示  
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_permissions_addPermissions")
    @RequestMapping("/addPermissionsIndex")
    public String addPermissionsIndex() {
        //清空Redis
        AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
        RedisUtil.remove("backend_menu_"+user.getUserId());
        RedisUtil.remove("backend_reaim_"+user.getUserId());
        
        return "backend/system/upms/permissions/addPermissions";
    }
    
    /**
     * 编辑界面显示
     * @Title: edit   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_permissions_edit")
    @RequestMapping("/edit")
    public String edit() {
        //清空Redis
        AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
        RedisUtil.remove("backend_menu_"+user.getUserId());
        RedisUtil.remove("backend_reaim_"+user.getUserId());
        
        return "backend/system/upms/permissions/edit";
    }
    
    /**
     * 删除一条权限
     * @Title: deletePermission   
     * @Description: 删除一条权限   
     * @param: @param permissionId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_permissions_deletePermission")
    @RequestMapping("/deletePermission")
    @ResponseBody
    public BaseResult deletePermission(int permissionId) {
        //清空Redis
        AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
        RedisUtil.remove("backend_menu_"+user.getUserId());
        RedisUtil.remove("backend_reaim_"+user.getUserId());
        
        int i = permissionsService.deletePermission(permissionId);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.delectSuccess);
        } else {
            return BaseResult.build(209, BaseResult.delectFail);
        }
    }
    
    /**
     * 禁用一条权限(注意:如果当前状态是禁用,如调用此方法会变成启用)
     * @Title: updataDisablePermissionId   
     * @Description: 禁用一条权限(注意:如果当前状态是禁用,如调用此方法会变成启用)
     * @param: @param permissionId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequiresPermissions("backend_system_upms_permissions_updataDisablePermissionId")
    @RequestMapping("/updataDisablePermissionId")
    @ResponseBody
    public BaseResult updataDisablePermissionId(int permissionId) {
        //清空Redis
        AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
        RedisUtil.remove("backend_menu_"+user.getUserId());
        RedisUtil.remove("backend_reaim_"+user.getUserId());
        
        int i = permissionsService.updataDisablePermissionId(permissionId);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.updataSuccess);
        } else {
            return BaseResult.build(209, BaseResult.updataFail);
        }
    }

    
    
    
    /**
     * 图标显示
     * @Title: larryfont   
     * @Description: 图标显示   
     * @param: @return      
     * @return: String      
     * @throws
     */
    @RequestMapping("/larryfont")
    public String larryfont() {
        return "backend/system/upms/permissions/larryfont";
    }
    
    
    /**
     * 查询权限
     * @Title: findPagingQuery   
     * @Description: 查询权限   
     * @param: @return      
     * @return: LayUiResult      
     * @throws
     */
    @RequestMapping("/findPagingQuery")
    @ResponseBody
    public LayUiResult findPagingQuery() {
        LayUiResult result = permissionsService.findPagingQuery();
        
        return result;
    }
    
    /**
     * 添加权限
     * @Title: addPermissions   
     * @Description: 添加权限   
     * @param: @param adminPermission
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/addPermissions")
    @ResponseBody
    public BaseResult addPermissions(AdminPermission adminPermission) {
        int i = permissionsService.addPermissions(adminPermission);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.addSuccess);
        } else {
            return BaseResult.build(209, BaseResult.addFail);
        }
    }
    

    
    /**
     * 根据id查询一条权限数据
     * @Title: selectPermissionsByPermissionId   
     * @Description: 根据id查询一条权限数据 
     * @param: @param permissionId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/selectPermissionsByPermissionId")
    @ResponseBody
    public BaseResult selectPermissionsByPermissionId(int permissionId) {
        AdminPermission adminPermission = permissionsService.selectPermissionsByPermissionId(permissionId);
        return BaseResult.build(200, "", adminPermission);
    }
    
    /**
     * 修改权限
     * @Title: updataPermissions   
     * @Description: 修改权限  
     * @param: @param permissionId
     * @param: @return      
     * @return: BaseResult      
     * @throws
     */
    @RequestMapping("/updataPermissions")
    @ResponseBody
    public BaseResult updataPermissions(AdminPermission adminPermission) {
        int i = permissionsService.updataPermissions(adminPermission);
        if (i > 0) {
            return BaseResult.build(200, BaseResult.updataSuccess);
        } else {
            return BaseResult.build(200, BaseResult.updataFail);
        }
    }
    
    /**
     * 查询全部权限返回ztree需要的结构
     * @Title: selectPermissions   
     * @Description: 查询全部权限返回ztree需要的结构
     * @param: @return      
     * @return: List      
     * @throws
     */
    @RequestMapping("/selectPermissions")
    @ResponseBody
    public BaseResult selectPermissions(){
        List list = permissionsService.selectPermissions();
        return BaseResult.build(200, "", list);
    }
}

你可能感兴趣的:(通用后台管理系统(4)-编写权限接口、实现、控制器、)