package com.xintong.visualinspection.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xintong.visualinspection.bean.User;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xintong.visualinspection.bean.Menu;
import com.xintong.visualinspection.bean.Permission;
import com.xintong.visualinspection.dao.master.MenuDao;
import com.xintong.visualinspection.dao.master.PermissionDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.MenuService;

import lombok.Data;

/**
 * 文件名：UserServiceImpl 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017
 * 版权所有.
 */
@Service
@Data
public class MenuServiceImpl extends BaseService implements MenuService {

	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MenuService.class);

	@Autowired
	private MenuDao menuDao;
	@Autowired
	private PermissionDao permissionDao;

	@Override
	public List<Menu> getMenus(Menu menu, User user, Integer judge) {
		
		List<Menu> menus = 	menuDao.getMenu(menu);
		if(judge==null || judge.intValue()==0){
			menus=getMenusByPermison(menus,user);
		}
		
		
		List<Menu> mlist = 	new ArrayList<>();
		
		Map<Integer,List<Menu>> map = new HashMap<>();
		
		//找出所有父
		for (Menu m : menus) {
			List<Menu> menuList = map.get(m.getParentId());
			if(menuList==null){
				menuList = new ArrayList<>();
			}
			if(m.getParentId()!=null){
				menuList.add(m);
				map.put(m.getParentId(), menuList);
			}else{
				if(!map.containsKey(m.getId()))
					map.put(m.getId(), menuList);
			}
		}
		//找出子
		for (Integer mid : map.keySet()) {
			for (Menu m2 : menus) {
				if(m2.getId().intValue()==mid.intValue()){
					m2.setChilds(map.get(mid));
					if(m2.getParentId()==null){
						mlist.add(m2);
					}
				}
			}
		}
		
		mlist.sort(new Comparator<Menu>() {

			@Override
			public int compare(Menu arg0, Menu arg1) {
				if(arg0.getSortNo()>arg1.getSortNo()){
					return 1;
				}
				return -1;
			}
		});
		
		return mlist;
	}

	@Override
	public void insert(Menu menu) {
		menuDao.insert(menu);

	}

	@Override
	public void update(Menu menu) {
		menuDao.update(menu);
	}

	@Override
	public void delete(Integer id) {
		menuDao.delete(id);
	}

	@Override
	public List<Menu> getMenuByParent(Menu menu) {
		List<Menu> menus = 	menuDao.getMenuByParent(menu);
		return menus;
	}
	
	
	 private List<Menu> getMenusByPermison(List<Menu> menus,User user){
	    	//判断有权限的资源
	    	//获取权限资源
	    	List<Permission> permissions= permissionDao.findByAdminUserId(user.getId());
	    	Map<Integer,Permission> pmap = new HashMap<>();
	    	for(Permission p : permissions){
	    		pmap.put(p.getResourceid(), p);
	    	}
	    	List<Menu> menulist = new ArrayList<>();
	    	for(Menu m : menus){
	    		//为有权限的资源
	    		if(pmap.get(m.getId())!=null && pmap.get(m.getId()).getPermission_type()==1){
	    			menulist.add(m);
	    		}
	    	}

	    	return menulist;
	    }

}
