package com.zcloud.service.system.impl;

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

import com.zcloud.entity.system.Route;
import com.zcloud.mapper.dsno2.system.ComponentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Menu;
import com.zcloud.mapper.dsno2.system.MenuMapper;
import com.zcloud.service.system.MenuService;

/**
 * 说明：菜单服务接口实现类
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Service
@Transactional //开启事物
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private ComponentMapper componentMapper;

	/**新增菜单
	 * @param menu
	 * @throws Exception
	 */
	@CacheEvict(value="menucache", allEntries=true)
	public void addMenu(Menu menu) throws Exception{
		menuMapper.addMenu(menu);
	}

	/**保存修改菜单
	 * @param menu
	 * @throws Exception
	 * @return
	 */
	@CacheEvict(value="menucache", allEntries=true)
	public List<PageData> edit(Menu menu) throws Exception{
		menuMapper.edit(menu);
		return null;
	}
	public void editmenuType(Menu menu) throws Exception{
		menuMapper.editmenuType(menu);
	}

	/**
	 * 通过菜单ID获取数据
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public PageData getMenuById(PageData pd) throws Exception {
		return menuMapper.getMenuById(pd);
	}

	/**获取最大的菜单ID
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public PageData findMaxId(PageData pd) throws Exception{
		return menuMapper.findMaxId(pd);
	}

	public PageData findById(PageData pd) throws Exception{
		return menuMapper.findById(pd);
	}

	/**
	 * 通过ID获取其子一级菜单
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@Cacheable(key="'menu-'+#parentId",value="menucache")
	public List<Menu> listSubMenuByParentId(String parentId) throws Exception {
		return menuMapper.listSubMenuByParentId(parentId);
	}

	/**
	 * 获取所有菜单并填充每个菜单的子菜单列表(菜单管理)(递归处理)
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Menu> listAllMenu(String MENU_ID) throws Exception {
//		List<Menu> menuList = this.listSubMenuByParentId(MENU_ID);
//		for(Menu menu : menuList){
//			menu.setMENU_URL("menu_edit.html?MENU_ID="+menu.getMENU_ID());
//			menu.setSubMenu(this.listAllMenu(menu.getMENU_ID()));
//			menu.setTarget("treeFrame");
//		}
//		return menuList;
		List<Menu> menuList = menuMapper.listSubMenuAll();
		List<Menu> menuListMenu = new ArrayList<>();
		//一级菜单
		for (Menu menu : menuList) {
			if("0".equals(menu.getPARENT_ID())){
				menu.setTarget("treeFrame");
				menu.setMENU_URL("menu_edit.html?MENU_ID="+menu.getMENU_ID());
				menuListMenu.add(menu);
			}
		}
		//一级菜单增加子菜单
		for (Menu routeMenu : menuListMenu) {
			List<Menu> childList = TreeChildren(routeMenu.getMENU_ID(),menuList);
			routeMenu.setSubMenu(childList);
		}
		return menuListMenu;
	}

	/**
	 * 获取所有菜单并填充每个菜单的子菜单列表(系统菜单列表)(递归处理)
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Menu> listAllMenuQx(String MENU_ID) throws Exception {
//		List<Menu> menuList = this.listSubMenuByParentId(MENU_ID);
//		for(Menu menu : menuList){
//			menu.setSubMenu(this.listAllMenuQx(menu.getMENU_ID()));
//			menu.setTarget("treeFrame");
//			if(menu.getHAS_COMPONENT()>0){
//				menu.setSubCom(componentMapper.listAll(menu.getMENU_ID()));
//			}
//		}
//		return menuList;
		// 全查出来 代码递归
		List<Menu> menuList = menuMapper.listSubMenuAll();
		List<Menu> menuListMenu = new ArrayList<>();
		//获取所有级别菜单

		//一级菜单
		for (Menu route : menuList) {
			if("0".equals(route.getPARENT_ID())){
				menuListMenu.add(route);
				route.setTarget("treeFrame");
			}
		}
		//一级菜单增加子菜单
		for (Menu routeMenu : menuListMenu) {
			List<Menu> childList = TreeChildren(routeMenu.getMENU_ID(),menuList);
			routeMenu.setSubMenu(childList);
		}
		return menuListMenu;
	}
	private List<Menu> TreeChildren(String parentId, List<Menu> routeList) {
		List<Menu> childList = new ArrayList<>();
		for (Menu route : routeList) {
			//遍历所有节点，与上级传过来的父节点id比较
			if (parentId.equals(route.getPARENT_ID())) {
				childList.add(route);
			}
		}
		//递归
		for (Menu route : childList) {
			route.setSubMenu(TreeChildren(route.getMENU_ID(), routeList));
			route.setTarget("treeFrame");
		}
		return childList;
	}
	/**删除菜单
	 * @param MENU_ID
	 * @throws Exception
	 */
	@CacheEvict(value="menucache", allEntries=true)
	public void deleteMenuById(String MENU_ID) throws Exception{
		menuMapper.deleteMenuById(MENU_ID);
	}

	/**保存菜单图标
	 * @param pd
	 * @throws Exception
	 */
	@CacheEvict(value="menucache", allEntries=true)
	public void editicon(PageData pd) throws Exception{
		menuMapper.editicon(pd);
	}
}
