package com.ym.mec.auth.service.impl;

import com.ym.mec.auth.api.entity.SysRole;
import com.ym.mec.auth.api.entity.SysRoleMenu;
import com.ym.mec.auth.dal.dao.SysRoleDao;
import com.ym.mec.auth.dal.dao.SysRoleMenuDao;
import com.ym.mec.auth.service.SysRoleService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysRoleServiceImpl extends BaseServiceImpl<Integer, SysRole>  implements SysRoleService {
	
	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;

	@Override
	public BaseDAO<Integer, SysRole> getDAO() {
		return sysRoleDao;
	}

	@Override
	public List<SysRole> findRoleByUserId(Integer userId) {
		return sysRoleDao.findRoleByUserId(userId);
	}

	@Override
	public SysRole findRoleByCode(String code){
		return sysRoleDao.findRoleByCode(code);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addRole(SysRole sysRole) {
		sysRoleDao.insert(sysRole);
		List<Integer> menuIds = sysRole.getMenuIds();
		if(menuIds != null && menuIds.size() > 0){
			SysRoleMenu sysRoleMenu = new SysRoleMenu(sysRole.getId(),null);
			menuIds.forEach(e->{
				sysRoleMenu.setMenuId(e);
				sysRoleMenuDao.insert(sysRoleMenu);
			});
		}
	}

}