package com.ssj.service.sys.menu.service.impl;

import com.ssj.bean.sys.menu.domain.Menu;
import com.ssj.bean.sys.role.domain.Admin;
import com.ssj.dao.sys.menu.dao.MenuDao;
import com.ssj.dao.sys.role.dao.AdminRoleDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.service.sys.menu.service.MenuService;
import com.ssj.service.sys.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class MenuServiceImpl  extends BaseServiceImpl<Menu, String> implements MenuService {
	@Autowired
    private MenuDao menuDao;
    
	@Autowired
	private AdminRoleDao adminRoleDao;
	
	@Autowired
	private RoleService roleService;
	
	@Override
	public PagingAndSortingRepository<Menu, String> getDao() {
		return menuDao;
	}

	@Override
	public List<Menu> findByParentId(String parentId) {
		return menuDao.findByParentId(parentId);
	}

	@Override
	public List<Menu> findByAll(Admin admin) {
		List<Menu> list = new ArrayList<Menu>();
		
		 if(admin!=null){
		//	 Collection<String> authorities = new ArrayList<String>();
	//		 List<AdminRole> adminRoleList=adminRoleDao.findByAdminId(admin.getId());
	//		 for (AdminRole adminRole : adminRoleList) {
		//		 Role role = roleService.getById(adminRole.getRoleId());
		//		 authorities.add(role.getName());
				 List<Menu> listMenu=menuDao.findMenuByRoleIdAndParentId(admin.getId(),"0");
				 for (Menu menu : listMenu) {
					 menu.setChildren(menuDao.findMenuByRoleIdAndParentId(admin.getId(),menu.getId()));
					 
					 List<Menu> subListMenu=menu.getChildren();
					 for (Menu subMenu : subListMenu) {
						 subMenu.setChildren(menuDao.findMenuByRoleIdAndParentId(admin.getId(),subMenu.getId()));
					 }
				 }
				 list.addAll(listMenu);
	//		 }
		//	 admin.setAuthorities(authorities);
		//	 session.setAttribute("admin", admin);
		 }
		return list;
	}

	@Override
	public List<Menu> getSubNodesByParentid(String parentId) {
		return menuDao.getSubNodesByParentid(parentId);
	}

	@Override
	public Page<Menu> findByPage(Map<String, Object> params, Pageable sp) {
		 Map<String, SearchFilter> filters = SearchFilter.parse(params);
			Specification<Menu> specification = DynamicSpecifications.bySearchFilter(filters.values(), Menu.class);
			return  menuDao.findAll(specification, sp);
	}

	@Override
	public Integer updateMenuOrder(String id, int MenuOrder) {
		return menuDao.updateMenuOrder(id, MenuOrder);
	}

	@Override
	public List<String> findRoleByAdminId(String adminId) {
		return menuDao.findRoleByAdminId(adminId);
	}
	
		
	

}
