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.AccountResources;
import com.cloudcross.ssp.model.Resources;
import com.cloudcross.ssp.model.ResourcesRole;
import com.cloudcross.ssp.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("resourcesSqlMapper.query", map);
		pageView.setRecords(list);
		return pageView;
	}

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

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

	@Override
	public void add(Resources t) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("resourcesSqlMapper.add",t);
	}
//-----------------------↓↓↓↓↓↓↓↓↓↓↓↓↓↓这部分主要用于对于t_res_accounts表的增、查、改、删-----------------------------------------------------------------------------------------
	@Override
	public List<Resources> findResourcesByAccount(Long id) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.findResourcesByAccount",id);
	}
		@Override
	public List<Resources> findAccountResourcess(String accountId) {
		// TODO Auto-generated method stub
		System.out.println("我爱你啦啦啦");
		return myBatisDao.getList("resourcesSqlMapper.findAccountResourcess",accountId);
	}
//-----------------------↑↑↑↑↑↑↑↑↑↑↑↑↑这部分主要用于对于t_res_accounts表的增、查、改、删-----------------------------------------------------------------------------------------
	@Override
	public List<Resources> findRoleRes(String roleId) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("resourcesSqlMapper.findRoleRes", roleId);
	}

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

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

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

	}

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

	@Override
	public int getMaxLevel() {
		// TODO Auto-generated method stub
		return myBatisDao.get("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;
	}

	
}
