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.common.base.BaseDao;
import com.qmth.qrzk.repository.service.query.UserQueryParams;
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;

	@Autowired
    private BaseDao dao;
	
	@Override
	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;
	}

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

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

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

	@Override
	public List<Integer> findSECRETARY() {
		List<Integer> list=new ArrayList<Integer>();
		List<UserRole> ls=roleDao.findSECRETARYUserId();
		for(UserRole ur:ls){
			list.add(ur.getUserId());
		}
		return list;
	}

	@Override
	public int getAllRolesCount(UserQueryParams params) {
		return roleDao.getAllRolesCount(params);
	}

	@Override
	public void addLinkRole(List<UserRole> list) {
		roleDao.addLinkRole(list);
	}

	@Override
	public Role findByCode(String code) {
		return dao.get("findRoleByCode", code);
	}

	@Override
	public boolean isQuesAuditor(int userId) {
		return dao.getList("getCurAuditCourseIdsByAuditorId", userId).size() > 0;
	}

	@Override
	public boolean isQuesInputer(int userId) {
		return dao.getList("getCurInputCourseIdsByInputerId", userId).size() > 0;
//		User user = dao.get("findUserById", userId);
//		return Consts.USER_TYPE_INPUTER == user.getUserType() && user.getIsEnabled() == 1;
	}
}