package com.platform.modules.sys.service.impl;

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

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

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.modules.sys.dao.SysRoleMenuDao;
import com.platform.modules.sys.entity.SysRoleMenuEntity;
import com.platform.modules.sys.service.SysMenuService;
import com.platform.modules.sys.service.SysRoleMenuService;



/**
 * 角色与菜单对应关系
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {
	
	@Autowired
	private SysMenuService sysMenuService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
		//先删除角色与菜单关系
		deleteBatch(new Long[]{roleId});

		if(menuIdList.size() == 0){
			return ;
		}

		//保存角色与菜单关系
		List<SysRoleMenuEntity> list = new ArrayList<SysRoleMenuEntity>(menuIdList.size());
//		List<SysRoleMenuEntity> l = new ArrayList<SysRoleMenuEntity>();
//		List<Long> parentMenuIdList = new ArrayList<Long>();
//		boolean flag;
//		boolean f ;
//		System.out.println(menuIdList.size());
		for(Long menuId : menuIdList){
//			System.out.println(menuId);
			SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
			sysRoleMenuEntity.setMenuId(menuId);
			sysRoleMenuEntity.setRoleId(roleId);
			list.add(sysRoleMenuEntity);
//			flag = true;
//			f = true;
//			SysMenuEntity menuEntity = sysMenuService.selectById(menuId);
//			if(menuEntity!=null){
//				if(menuIdList.contains(menuEntity.getParentId())){
//					flag = false;
//				}
//				if(parentMenuIdList.contains(menuEntity.getParentId())){
//					flag = false;
//				}
//				if(flag && f){
//					sysRoleMenuEntity.setMenuId(menuEntity.getParentId());
//					sysRoleMenuEntity.setRoleId(roleId);
//					l.add(sysRoleMenuEntity);
//					parentMenuIdList.add(menuEntity.getParentId());
//				}
//			}
		}
		this.insertBatch(list);
//		this.insertBatch(l);
	}

	@Override
	public List<Long> queryMenuIdList(Long roleId) {
		return baseMapper.queryMenuIdList(roleId);
	}

	@Override
	public int deleteBatch(Long[] roleIds){
		return baseMapper.deleteBatch(roleIds);
	}

}
