package com.yonge.cooleshow.auth.service.impl;

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

import com.yonge.toolset.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonge.cooleshow.auth.api.entity.SysRoleMenu;
import com.yonge.cooleshow.auth.dal.dao.SysRoleMenuDao;
import com.yonge.cooleshow.auth.service.SysRoleMenuService;
import com.yonge.toolset.mybatis.dal.BaseDAO;

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

	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;

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

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

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

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

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

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

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

}