package com.qmth.qrzk.repository.user.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.qmth.qrzk.dao.IRoleDao;
import com.qmth.qrzk.repository.user.model.Role;
import com.qmth.qrzk.repository.user.model.UserRole;
import com.qmth.qrzk.repository.user.service.IRoleService;

@Service("roleServiceImpl")
public class RoleServiceImpl implements IRoleService{

	private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);
	
	@Autowired
	@Qualifier("roleDaoImpl")
	private IRoleDao roleDao;
	
	
	public List<Role> getRoleNames(List<Integer> ls) {
		List<Role> list;
		try {
			list = roleDao.getRoleIds(ls);
			return list;
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}
	
	public List<Role> getRolesByUserId(int userId)
	{
		try
		{
			List<Role> roleList = roleDao.getRolesByUserId(userId);
			return roleList;
		}catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}
		
		return null;
	}

	
	public List<Role> getAllRoles() {
		try
		{
			List<Role> roleList = roleDao.getAllRoles();
			return roleList;
		}catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	public List<Integer> getRoleIds(int user_id) {
		List<Role> ls=null;
		try {
			ls=roleDao.getRolesByUserId(user_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Integer> list=new ArrayList<Integer>();
		for(Role role:ls){
			list.add(role.getId());
		}
		return list;
	}

	public void delByUserId(int user_id) {
		roleDao.delUserRoleByUserId(user_id);
	}

	public void insertUserRole(UserRole userRole) {
		roleDao.insert(userRole);
	}
}
