package com.ssm.service.impl.system;

import java.util.List;

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

import com.github.pagehelper.PageHelper;
import com.ssm.mapper.system.SysMenuMapper;
import com.ssm.mapper.system.SysRoleMapper;
import com.ssm.model.system.SysMenu;
import com.ssm.model.system.SysRole;
import com.ssm.model.system.SysUser;
import com.ssm.service.system.SysRoleService;
import com.ssm.util.page.BeanUtil;
import com.ssm.util.page.PagedResult;

@Transactional
@Service
public class SysRoleServiceImpl  implements SysRoleService{
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	@Autowired SysMenuMapper sysMenuMapper;
	
	public PagedResult<SysRole> queryByRoleName(String roleName,Integer pageNo,
			Integer pageSize) {
	    //Helper.setPageInfo(pageNo, pageSize);
		pageNo = pageNo == null?1:pageNo;  
	    pageSize = pageSize == null?5	:pageSize;
	    PageHelper.startPage(pageNo,pageSize);  //startPage是告诉拦截器说我要开始分页了。分页参数是这两个。  
	   // roleName = null;
	    PagedResult<SysRole> pagedResult = null;
	    List<SysRole> list = sysRoleMapper.findAll(roleName);
	    if(list !=null && list.size() > 0){
	        pagedResult = BeanUtil.toPagedResult(list);
	    }
	    return pagedResult;
}

	@Override
	public void insertSysRole(SysRole role) {
		sysRoleMapper.insert(role);
	}

	@Override
	public int updateRoleByRoleId(SysRole role) {
		return sysRoleMapper.updateByPrimaryKey(role);
	}

	@Override
	public void deleteRoleByRoleId(Integer roleId) {
		sysRoleMapper.deleteByPrimaryKey(roleId);
	}

	@Override
	public SysRole selectSysRoleByRoleId(Integer roleId) {
		 return sysRoleMapper.selectByPrimaryKey(roleId);	
	}

	@Override
	public List<SysMenu> selectMenusByRoleId(Integer roleId) {
		return sysMenuMapper.selectMenusByRoleId(roleId);
	}
	
	@Override
	public List<SysMenu> selectMenusLevelLT2ByRoleId(Integer roleId) {
		return sysMenuMapper.selectMenusLevelLT2ByRoleId(roleId);
		
	}
	
	@Override
	public Integer addRoleRelevancePer(Integer roleId, Integer menuId) {
		 return sysRoleMapper.addRoleRelevancePer(roleId,menuId);
	}

	@Override
	public Integer selectRoleRelevancePer(Integer roleId, Integer menuId) {
		 return sysRoleMapper.selectRoleRelevancePer(roleId,menuId);
	}

	@Override
	public PagedResult<SysUser> selectUserByRoleId(Integer roleId,Integer pageNo,
			Integer pageSize,String userName ,String employeeNo) {
		pageNo = pageNo == null?1:pageNo;  
	    pageSize = pageSize == null?5:pageSize;
	    PageHelper.startPage(pageNo,pageSize);
	    PagedResult<SysUser> pagedResult = null;
	    List<SysUser> list = sysRoleMapper.selectUserByRoleId(roleId,userName,employeeNo);
	    if(list !=null && list.size() > 0){
	        pagedResult = BeanUtil.toPagedResult(list);
	    }
	    return pagedResult;
	}

	@Override
	public Integer deleteUserByRole(Integer roleId, Integer userId) {
		 return sysRoleMapper.deleteUserByRole(roleId,userId);
	}

	@Override
	public List<SysRole> selectRoleAndMenuList() {
		return sysRoleMapper.selectRoleAndMenuList();
	}

	@Override
	public List<SysRole> findUserRoleByUserId(int userId) {
		return sysRoleMapper.findUserRoleByUserId(userId);
	}

	@Override
	public List<SysMenu> selectButtonByRoleId(Integer roleId) {
		return sysMenuMapper.selectButtonByRoleId(roleId);
	}

	@Override
	public void deletePerPageByRoleIdAndMenuId(Integer menuId, Integer roleId) {
		sysRoleMapper.deletePerPageByRoleIdAndMenuId(menuId,roleId);           //删除选择的pgae菜单
//		List<SysMenu> childList = sysMenuMapper.selectChildMenusByPageMenuId(menuId);
//		if(childList.size() > 0){
//			for (SysMenu sysMenu : childList) {					//删除选择page的button菜单
//				sysRoleMapper.deletePerPageByRoleIdAndMenuId(sysMenu.getMenuid(),roleId);
//			}
//		}
	}

	@Override
	public List<SysMenu> selectPageMenusByRole(Integer roleId) {
		return sysMenuMapper.selectPageMenusByRole(roleId);
		
	}

	@Override
	public List<SysMenu> selectButtonMenusByRole(Integer roleId) {
		return sysMenuMapper.selectButtonMenusByRole(roleId);
	}

	@Override
	public void deleteButtonMenusByMenuIdAndRoleId(Integer menuId,
			Integer roleId) {
		sysRoleMapper.deletePerPageByRoleIdAndMenuId(menuId,roleId);
	}

	@Override
	public List<SysMenu> selectPageMenusByRoleId(Integer roleId) {
		return sysMenuMapper.selectPageMenusByRoleId(roleId);
	}
	
	@Override
	public List<SysMenu> selectButtonMenusByRoleId(Integer roleId) {
		return sysMenuMapper.selectButtonMenusByRoleId(roleId);
	}

	@Override
	public List<SysMenu> selectChildMenusByPageMenuId(Integer menuId) {
		return sysMenuMapper.selectChildMenusByPageMenuId(menuId);
	}

	@Override
	public Integer selectReByMenuIdAndRoleId(Integer menuId, Integer roleId) {
		return sysMenuMapper.selectReByMenuIdAndRoleId(menuId, roleId);
	}

	@Override
	public SysRole selectByRoleName(String roleName) {
		return sysRoleMapper.selectByRoleName(roleName);
	}
	
	
}
