package com.keao.edu.auth.service.impl;

import java.util.ArrayList;
import java.util.List;

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

import com.keao.edu.auth.api.entity.SysRoleMenu;
import com.keao.edu.auth.dal.dao.SysRoleMenuDao;
import com.keao.edu.auth.service.SysRoleMenuService;
import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.service.impl.BaseServiceImpl;

@Service
public class SysRoleMenuServiceImpl extends BaseServiceImpl<Integer, SysRoleMenu> implements SysRoleMenuService {

	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;

	@Override
	public BaseDAO<Integer, SysRoleMenu> getDAO() {
		return sysRoleMenuDao;
	}

	@Override
	public List<String> queryPermissionsByRoleId(Integer roleId) {
		return sysRoleMenuDao.queryPermissionsByRoleId(roleId);
	}

	@Override
	public List<String> queryPermissionsByRoleIdList(List<Integer> roleIdList) {

		List<String> list = new ArrayList<String>();

		for (Integer roleId : roleIdList) {
			list.addAll(queryPermissionsByRoleId(roleId));
		}
		return list;
	}

	@Override
	public boolean batchInsert(Integer roleId, String menuIds) {
		SysRoleMenu sysRoleMenu = new SysRoleMenu();
		sysRoleMenu.setRoleId(roleId);
		String[] split = menuIds.split(",");
		for (String string:split) {
			sysRoleMenu.setMenuId(Integer.parseInt(string));
			sysRoleMenuDao.delete(sysRoleMenu);
		}
		return true;
	}

	@Override
	public boolean batchdel(Integer roleId, String menuIds) {
		SysRoleMenu sysRoleMenu = new SysRoleMenu();
		sysRoleMenu.setRoleId(roleId);
		String[] split = menuIds.split(",");
		for (String string:split) {
			sysRoleMenu.setMenuId(Integer.parseInt(string));
			sysRoleMenuDao.insert(sysRoleMenu);
		}
		return true;
	}

}