package com.cloudcross.ssp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.cloudcross.ssp.model.RoleAccount;
import com.cloudcross.ssp.model.Roles;
import com.cloudcross.ssp.service.IRolesService;
import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.Common;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.pulgin.mybatis.plugin.PageView;
@Service
public class RolesService implements IRolesService {
	@Autowired
	private GenericIBatisDao myBatisDao;
	@Override
	public PageView query(PageView pageView, Roles t) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("t", t);
		List<Roles> list = myBatisDao.getList("rolesSqlMapper.query", map);
		pageView.setRecords(list);
		return pageView;
	}

	@Override
	public List<Roles> queryAll(Roles t) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("rolesSqlMapper.queryAll", t);
	}

	@Override
	public void delete(String id) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("rolesSqlMapper.delete", id);
	}

	@Override
	public void update(Roles t) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("rolesSqlMapper.update", t);
	}

	@Override
	public Roles getById(String id) {
		// TODO Auto-generated method stub
		return myBatisDao.get("rolesSqlMapper.getById", id);
	}

	@Override
	public void add(Roles t) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("rolesSqlMapper.add", t);
	}

	@Override
	public Roles isExist(String name) {
		// TODO Auto-generated method stub
		return myBatisDao.get("rolesSqlMapper.getById", name);
	}

	@Override
	public Roles findbyAccountRole(String accountId) {
		// TODO Auto-generated method stub
		 return myBatisDao.get("rolesSqlMapper.getById", accountId);
	}

	@Override
	public void addAccRole(RoleAccount roleAccount) {
		// TODO Auto-generated method stub
		myBatisDao.save("rolesSqlMapper.deleteAccountRole", (roleAccount.getAccountId().toString()));
		myBatisDao.save("rolesSqlMapper.addAccRole", roleAccount);
		
	}

	@Override
	public void addAccRole(String accountId, List<String> ids) {
		// TODO Auto-generated method stub
		myBatisDao.save("roleMapper.deleteAccountRole", accountId);
		for (String roleId : ids) {
			if(!Common.isEmpty(roleId)){
				RoleAccount roleAccount = new RoleAccount();
				roleAccount.setAccountId(Integer.parseInt(accountId) );
				roleAccount.setRoleId(Integer.parseInt(roleId) );
				myBatisDao.save("roleMapper.addAccRole", roleAccount);
			}
		}
	}

	@Override
	public int countByParams(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Roles> findByParams(Map<String, Object> paramMap, Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}

}
