package com.dcit.authority.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

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

import com.dcit.authority.dao.SysResourcesMapper;
import com.dcit.authority.dao.SysRoleMapper;
import com.dcit.authority.dao.SysRoleResourcesMapper;
import com.dcit.authority.dto.RoleResourcesInfoDTO;
import com.dcit.authority.dto.UserRoleMenuInfoDTO;
import com.dcit.authority.model.SysRole;
import com.dcit.authority.model.SysRoleExample;
import com.dcit.authority.model.SysRoleResources;
import com.dcit.authority.service.SysRoleService;

@Service
public class SysRoleServiceImpl implements SysRoleService {
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	@Autowired
	private SysRoleResourcesMapper sysRoleResourcesMapper;
	
	@Autowired
	private SysResourcesMapper sysResourcesMapper;

	@Override
	public boolean authorityRoleResource(String roleId, String resourceId) throws DataAccessException {
		SysRoleResources sysRoleResource = new SysRoleResources();
		sysRoleResource.setRoleId(roleId);
		sysRoleResource.setResourceId(resourceId);
		sysRoleResource.setCreateTime(new Date());
        return sysRoleResourcesMapper.insert(sysRoleResource) == 1 ? Boolean.TRUE : Boolean.FALSE;
	}

	@Override
	public boolean addRole(SysRole role) throws DataAccessException {
		int num = sysRoleMapper.insertSelective(role);
        return num == 1 ? Boolean.TRUE : Boolean.FALSE;
	}

	@Override
	public boolean updateRole(SysRole role) throws DataAccessException {
		int num = sysRoleMapper.updateByPrimaryKeySelective(role);
        return num == 1 ? Boolean.TRUE : Boolean.FALSE;
	}

	@Override
	public boolean deleteRoleByRoleId(String roleId) throws DataAccessException {
		int num = sysRoleMapper.deleteByPrimaryKey(roleId);
        return num == 1 ? Boolean.TRUE : Boolean.FALSE;
	}

	@Override
	public List<SysRole> getRoleList() throws DataAccessException {
		return sysRoleMapper.selectRoles();
	}

	@Override
	public int countByExample(SysRoleExample example) {
		// TODO Auto-generated method stub
		return sysRoleMapper.countByExample(example);
	}

	@Override
	public int deleteByExample(SysRoleExample example) {
		// TODO Auto-generated method stub
		return sysRoleMapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return sysRoleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(SysRole record) {
		// TODO Auto-generated method stub
		return sysRoleMapper.insert(record);
	}

	@Override
	public int insertSelective(SysRole record) {
		// TODO Auto-generated method stub
		return sysRoleMapper.insertSelective(record);
	}

	@Override
	public List<SysRole> selectByExample(SysRoleExample example) {
		// TODO Auto-generated method stub
		return sysRoleMapper.selectByExample(example);
	}

	@Override
	public SysRole selectByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return sysRoleMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByExampleSelective(SysRole record, SysRoleExample example) {
		// TODO Auto-generated method stub
		return sysRoleMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(SysRole record, SysRoleExample example) {
		// TODO Auto-generated method stub
		return sysRoleMapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(SysRole record) {
		// TODO Auto-generated method stub
		return sysRoleMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(SysRole record) {
		// TODO Auto-generated method stub
		return sysRoleMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<RoleResourcesInfoDTO> selectRoleResourcesByRoleId(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return sysRoleResourcesMapper.selectRoleResourcesByRoleId(map);
	}

	@Override
	public int deleteRoleResourcesByRoleId(String roleId) {
		// TODO Auto-generated method stub
		
		return sysRoleResourcesMapper.deleteRoleResourcesByRoleId(roleId);
	}

	@Override
	public int addRoleResources(SysRoleResources sysRoleResources) {
		// TODO Auto-generated method stub
		return sysRoleResourcesMapper.insertSelective(sysRoleResources);
	}

	@Override
	public List<UserRoleMenuInfoDTO> selectUserRoleMenuByRoleId(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return sysRoleResourcesMapper.selectUserRoleMenuByRoleId(map);
	}

}
