MenuServiceImpl.java 10.6 KB
package com.huaheng.pc.system.menu.service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.pc.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huaheng.common.constant.UserConstants;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.TreeUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.pc.system.menu.domain.Menu;
import com.huaheng.pc.system.menu.mapper.MenuMapper;
import com.huaheng.pc.system.role.domain.Role;
import com.huaheng.pc.system.role.mapper.RoleMenuMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 菜单 业务层处理
 * 
 * @author huaheng
 */
@Service
public class MenuServiceImpl implements IMenuService
{
    public static final String PREMISSION_STRING = "perms[\"{0}\"]";

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    /**
     * 根据用户ID查询PC菜单
     * 
     * @param userId 用户ID
     * @return 菜单列表
     */
       @Override
    public List<Menu> selectPCMenusByUserId(Integer userId)
    {
        if (User.unAdmin(userId)) {
            Menu menu = new Menu();
            menu.setMenuMobile(false);
            menu.setVisible(true);
            List<Menu> menus = menuMapper.selectMenuPageList(menu);
            return TreeUtils.getChildPerms(menus, 0);
        }
        else
        {
            List<Menu> menus = menuMapper.selectPCMenusByUserId(ShiroUtils.getWarehouseCode(), userId);
            return TreeUtils.getChildPerms(menus, 0);
        }
    }


    @Override
    public List<Menu> selectMenusByUserId(Integer userId)
    {
        if (User.unAdmin(userId))
        {
            Menu menu = new Menu();
            menu.setMenuMobile(false);
            menu.setVisible(true);
            List<Menu> menus = menuMapper.selectMenuPageList(menu);
            return TreeUtils.getChildPerms(menus, 0);
        }
        else
        {
            List<Menu> menus = menuMapper.selectMenusByUserId(userId);
            return TreeUtils.getChildPerms(menus, 0);
        }
    }

    /**
     * 查询菜单集合
     * 
     * @return 所有菜单信息
     */
    @Override
    public List<Menu> selectMenuList(Menu menu)
    {
        if(StringUtils.isEmpty(menu.getWarehouseCode())) {
            menu.setWarehouseCode(ShiroUtils.getWarehouseCode());
        }
        return menuMapper.selectMenuList(menu);
    }

    @Override
    public Menu selectMenu(Menu menu) {
        return menuMapper.selectMenu(menu);
    }

    /**
     * 查询菜单集合
     * 
     * @return 所有菜单信息
     */
    @Override
    public List<Menu> selectMenuAll()
    {
        Menu menu = new Menu();
        return menuMapper.selectMenuList(menu);
    }

    /**
     * 根据用户ID查询权限
     * 
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectPermsByUserId(Integer userId)
    {
        Set<String> permsSet = new HashSet<>();
        List<String> perms = null;
        if (User.unAdmin(userId))
        {
            perms = menuMapper.selectPermsBywarehouseCode(ShiroUtils.getWarehouseCode());
        }
        else
        {
            perms = menuMapper.selectPermsByUserId(ShiroUtils.getWarehouseCode(), userId);
        }

        for (String perm : perms)
        {
            if (StringUtils.isNotEmpty(perm))
            {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    /**
     * 根据角色ID查询菜单
     * 
     * @param role 角色对象
     * @return 菜单列表
     */
    @Override
    public List<Map<String, Object>> roleMenuTreeData(Role role)
    {
        Integer roleId = role.getId();
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        Menu menu = new Menu();
//        menu.setWarehouseCode(ShiroUtils.getWarehouseCode());
        List<Menu> menuList = menuMapper.selectMenuList(menu);
        if (StringUtils.isNotNull(roleId))
        {
            List<String> roleMenuList = menuMapper.selectMenuTree(ShiroUtils.getWarehouseCode(), roleId);
            trees = getTrees(menuList, true, roleMenuList, true);
        }
        else
        {
            trees = getTrees(menuList, false, null, true);
        }
        return trees;
    }

    /**
     * 查询所有菜单
     * 
     * @return 菜单列表
     */
    @Override
    public List<Map<String, Object>> menuTreeData()
    {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        Menu menu = new Menu();
        menu.setWarehouseCode(ShiroUtils.getWarehouseCode());
        List<Menu> menuList = menuMapper.selectMenuList(menu);
        trees = getTrees(menuList, false, null, false);
        return trees;
    }

    /**
     * 查询系统所有权限
     * 
     * @return 权限列表
     */
    @Override
    public LinkedHashMap<String, String> selectPermsAll()
    {
        LinkedHashMap<String, String> section = new LinkedHashMap<>();
        Menu menu = new Menu();
        menu.setWarehouseCode(ShiroUtils.getWarehouseCode());
        List<Menu> permissions = menuMapper.selectMenuList(menu);
        if (StringUtils.isNotEmpty(permissions))
        {
            for (Menu item : permissions)
            {
                section.put(item.getUrl(), MessageFormat.format(PREMISSION_STRING, item.getPerms()));
            }
        }
        return section;
    }

    /**
     * 对象转菜单树
     * 
     * @param menuList 菜单列表
     * @param isCheck 是否需要选中
     * @param roleMenuList 角色已存在菜单列表
     * @param permsFlag 是否需要显示权限标识
     * @return
     */
    public List<Map<String, Object>> getTrees(List<Menu> menuList, boolean isCheck, List<String> roleMenuList,
            boolean permsFlag)
    {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (Menu menu : menuList)
        {
            Map<String, Object> deptMap = new HashMap<String, Object>();
            deptMap.put("id", menu.getId());
            deptMap.put("pId", menu.getParentId());
            deptMap.put("name", transMenuName(menu, roleMenuList, permsFlag));
            deptMap.put("title", menu.getMenuName());
            if (isCheck)
            {
                deptMap.put("checked", roleMenuList.contains(menu.getId() + menu.getPerms()));
            }
            else
            {
                deptMap.put("checked", false);
            }
            trees.add(deptMap);
        }
        return trees;
    }

    public String transMenuName(Menu menu, List<String> roleMenuList, boolean permsFlag)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(menu.getMenuName());
        if (permsFlag)
        {
            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPerms() + "</font>");
        }
        return sb.toString();
    }

    /**
     * 删除菜单管理信息
     * 
     * @param id 菜单ID
     * @return 结果
     */
    @Override
    public int deleteMenuById(Integer id)
    {
        ShiroUtils.clearCachedAuthorizationInfo();
        return menuMapper.deleteMenuById(ShiroUtils.getWarehouseCode(), id);
    }

    /**
     * 根据菜单ID查询信息
     * 
     * @param id 菜单ID
     * @return 菜单信息
     */
    @Override
    public Menu selectMenuById(Integer id)
    {
        return menuMapper.selectMenuById(ShiroUtils.getWarehouseCode(), id);
    }

    /**
     * 查询子菜单数量
     * 
     * @param parentId 菜单ID
     * @return 结果
     */
    @Override
    public int selectCountMenuByParentId(Integer parentId)
    {
        return menuMapper.selectCountMenuByParentId(ShiroUtils.getWarehouseCode(), parentId);
    }

    /**
     * 查询菜单使用数量
     * 
     * @param id 菜单ID
     * @return 结果
     */
    @Override
    public int selectCountRoleMenuByMenuId(Integer id)
    {
        return roleMenuMapper.selectCountRoleMenuByMenuId(id);
    }

    /**
     * 新增保存菜单信息
     * 
     * @param menu 菜单信息
     * @return 结果
     */
    @Override
    public int insertMenu(Menu menu)
    {
        menu.setWarehouseCode(QuantityConstant.WAREHOUSE_CS);
        menu.setCreateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return menuMapper.insertMenu(menu);
    }

    /**
     * 修改保存菜单信息
     * 
     * @param menu 菜单信息
     * @return 结果
     */
    @Override
    public int updateMenu(Menu menu)
    {
//        menu.setWarehouseCode(ShiroUtils.getWarehouseCode());
        menu.setUpdateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return menuMapper.updateMenu(menu);
    }

    /**
     * 校验菜单名称是否唯一
     * 
     * @param menu 菜单信息
     * @return 结果
     */
    @Override
    public Integer checkMenuNameUnique(Menu menu)
    {
        Integer id = StringUtils.isNull(menu.getId()) ? -1 : menu.getId();
        Menu info = menuMapper.checkMenuNameUnique(ShiroUtils.getWarehouseCode(), menu.getMenuMobile(),  menu.getMenuName());
        if (StringUtils.isNotNull(info) && info.getId().intValue() != id.intValue())
        {
            return UserConstants.MENU_NAME_NOT_UNIQUE;
        }
        return UserConstants.MENU_NAME_UNIQUE;
    }

    /**
     * 根据用户ID查询手机菜单
     *
     * @param userId 用户ID
     * @return 菜单列表
     */
    @Override
    public List<Menu> selectMobileMenusByUserId(Integer userId)
    {
        List<Menu> menus = menuMapper.selectMobileMenusByUserId(ShiroUtils.getWarehouseCode(), userId);
        return menus;
    }

    //复制菜单
    @Override
    @Transactional
    public Boolean menuCopy(String code, String newCode) {
        int i = 0;
        //复制菜单数据
        i = menuMapper.menuCopy(code,newCode, ShiroUtils.getLoginName());
        if(i < 1){
            throw new ServiceException("复制菜单数据失败");
        }
        //修改父id
        i= menuMapper.updateParentId(newCode);
        if(i < 1){
            throw new ServiceException("修改父Id失败");
        }
        return true;
    }


}