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.AccountResources;
import com.cloudcross.ssp.back.model.Resources;
import com.cloudcross.ssp.back.model.ResourcesRole;
import com.cloudcross.ssp.back.service.IResourcesService;
import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.pulgin.mybatis.plugin.PageView;
@Service
public class ResourcesService implements IResourcesService {
	@Autowired
	private GenericIBatisDao myBatisDao;
	@Override
	public PageView query(PageView pageView, Resources t) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("t", t);
		List<Resources> list = myBatisDao.getList("back.resourcesSqlMapper.query", map);
		pageView.setRecords(list);
		return pageView;
	}

	@Override
	public List<Resources> queryAll(Resources t) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("back.resourcesSqlMapper.queryAll",t);
	}
	public boolean deleteResourceByAccount(Long accountId){
		myBatisDao.save("back.resourcesSqlMapper.deleteAccountResources", accountId);
		return true;
	}
	@Override
	public void delete(String id) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("back.resourcesSqlMapper.delete", id);
	}
	public boolean addAccountResourcess(Long accountId, List<Long> list) {
		// TODO Auto-generated method stub
		//先将权限表中所有对应这一accountId的权限都删掉
		myBatisDao.save("back.resourcesSqlMapper.deleteAccountResources", accountId);
		for (Long l : list) {
			//再循环将新传进来的权限一个一个添加进来
			AccountResources ar = new AccountResources();
			ar.setAccountId(accountId);
			ar.setResourceId(l);
			myBatisDao.save("back.resourcesSqlMapper.addAccountResourcess", ar);;
		}
		return true;
	}
	@Override
	public void update(Resources t) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("back.resourcesSqlMapper.update", t);
	}

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

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

	@Override
	public List<Resources> findAccountResourcess(String accountId) {
		// TODO Auto-generated method stub
		System.out.println("我爱你啦啦啦");
		return myBatisDao.getList("back.resourcesSqlMapper.findAccountResourcess",accountId);
	}

	@Override
	public List<Resources> findRoleRes(String roleId) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("back.resourcesSqlMapper.findRoleRes", roleId);
	}

	@Override
	public List<Resources> queryByParentId(Resources resources) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("back.resourcesSqlMapper.queryByParentId", resources);
	}

	@Override
	public void updateSortOrder(List<Resources> menus) {
		// TODO Auto-generated method stub
		for (Resources m : menus) {
			myBatisDao.save("back.resourcesSqlMapper.updateSortOrder", m);
		}
	}

	@Override
	public void addRoleRes(String roleId, List<String> list) {
		// TODO Auto-generated method stub
		myBatisDao.save("back.resourcesSqlMapper.deleteResourcesRole", roleId);
		for (String string : list) {
			ResourcesRole rr = new ResourcesRole();
			rr.setRoleId(roleId);
			rr.setResId(string);
			myBatisDao.save("back.resourcesSqlMapper.addRoleRes", rr);;
		}

	}

	@Override
	public Resources isExist(String menuName) {
		// TODO Auto-generated method stub
		return myBatisDao.get("back.resourcesSqlMapper.isExist", menuName);
	}

	@Override
	public int getMaxLevel() {
		// TODO Auto-generated method stub
		return myBatisDao.get("back.resourcesSqlMapper.getMaxLevel",null);
	}

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

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

	@Override
	public List<Resources> findResourcesByAccount(Long id) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("back.resourcesSqlMapper.findResourcesByAccount",id);
	}

}
