package com.cloudcross.ssp.back.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.back.model.RoleAccount;
import com.cloudcross.ssp.back.model.Roles;
import com.cloudcross.ssp.back.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("roleSqlMapper.query", map);
		pageView.setRecords(list);
		return pageView;
	}

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

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

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

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

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

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

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

	@Override
	public void addAccRole(RoleAccount roleAccount) {
		// TODO Auto-generated method stub
		myBatisDao.save("back.rolesSqlMapper.deleteAccountRole", (roleAccount.getAccountId().toString()));
		myBatisDao.save("back.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;
	}

}
