package lingtao.net.service;

import java.util.List;

import javax.servlet.http.HttpSession;

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

import lingtao.net.bean.Msg;
import lingtao.net.bean.SysRole;
import lingtao.net.bean.SysUser;
import lingtao.net.dao.SysRoleMapper;

@Service
public class SysRoleService {

	@Autowired
	private SysRoleMapper sysRolemapper;

	// 角色列表
	public List<SysRole> getRoles(SysRole role) {
		return sysRolemapper.getRoles(role);
	}

	// 新增
	public Msg addRole(SysRole role, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		try {
			role.setCreateBy(user.getRealname());
			sysRolemapper.insertSelective(role);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}
	}

	// 修改
	public Msg updateRoleById(SysRole role) {
		try {
			sysRolemapper.updateByPrimaryKeySelective(role);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}
	}

	// 改变用户状态
	public Msg changeRoleStatus(String roleId) {
		try {
			sysRolemapper.changeRoleStatus(roleId);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}
	}

	// 根据角色id查询角色拥有的权限
	public List<Integer> getPerIdsByRoleId(Integer id) {
		return sysRolemapper.getPerIdsByRoleId(id);
	}

	// 删除角色已有的权限
	public void deleteRolePermissions(Integer roleId) {
		sysRolemapper.deleteRolePermissions(roleId);

	}

	// 给角色授权
	public void addRolePermissions(Integer roleId, String[] split) {
		sysRolemapper.addRolePermissions(roleId, split);

	}

	// 查询允许被注册的角色
	public List<SysRole> getAllRoleName(String isRegist) {
		return sysRolemapper.getAllRoleName(isRegist);
	}

	// 查询出用户所拥有的且允许被创建的角色
	public List<SysRole> getRolesByUserId(Integer userId) {
		return sysRolemapper.getRolesByUserId(userId);
	}

}
