package com.ssm.service.impl.system;

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

import javax.servlet.http.HttpSession;

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

import com.ssm.mapper.system.SysMenuMapper;
import com.ssm.model.system.SysMenu;
import com.ssm.model.system.SysUser;
import com.ssm.service.system.SysMenuService;
import com.ssm.util.PackageBeanUtils;


@Service
public class SysMenuServiceImp implements SysMenuService{

	
	@Autowired
	 SysMenuMapper menuMapper;
	
	
	/**
	 * 获取菜单列表
	 * @return
	 */
	public List<SysMenu> getMenuList(HttpSession session,SysUser sysUser){
		
		List<SysMenu> lstRoot = new ArrayList<SysMenu>();
		List<SysMenu> menus = new ArrayList<SysMenu>();
		
		menus  = menuMapper.selectSysUserMenus(sysUser.getUserid());
		
		
		if(null != menus && menus.size() > 0){
			for (SysMenu sysMenuWithBLOBs : menus) {
				
				if(sysMenuWithBLOBs.getLevel() == 1){
					lstRoot.add(sysMenuWithBLOBs);
					
				}
				
			}
			if(null != lstRoot && lstRoot.size() > 0){
				
					for (int i = 0; i < lstRoot.size(); i++) {
						
						PackageBeanUtils.packageChildSysmenus(lstRoot.get(i) , menus);
					}
				}
			}
		/**
		 * 用户的所有权限菜单
		 */
		session.setAttribute("ValidateMenus",menus);
		/**
		 * 主页面展示菜单
		session.setAttribute("menu",lstRoot);
		 */
		return lstRoot;

	}
	
	
	
	
	/**
	 * 设置当前选中的菜单项
	 */
	public void setCurrentMenu(List<SysMenu> lstMenuInfo, String menuId){
		
		for(SysMenu rootMenu : lstMenuInfo){
			
			if(rootMenu.getMenuenname().equals(menuId)){
				rootMenu.setIsenable(true);
			} else {
				//List<SysMenuWithBLOBs> lstMenuChild = rootMenu.getChildMenu();
				
				List<SysMenu> lstMenuChild = rootMenu.getChildMenu() ;//= menuMapper.selectSysMenuWithBLOBsByPid(parentid);
				
				if(lstMenuChild != null){
					for(SysMenu menuInfo : lstMenuChild){
						
						if(menuInfo.getMenuenname().equals(menuId)){
							menuInfo.setIsenable(true);
							rootMenu.setIsenable(true);
						} else{
							menuInfo.setIsenable(false);
							rootMenu.setIsenable(false);
						}
					}
				}
			}
		}
		
	}
	
	/**
	 * 获取菜单列表
	 * @return
	 */
	@Override
	public List<SysMenu> menuTree(SysMenu menu) {
		
		return menuMapper.selectBySysMenuWithBLOBs(menu);
	}
	
	public List<SysMenu> selectMenus(SysMenu menu) {
		
		return menuMapper.selectBySysMenuWithBLOBs2(menu);
	}

	@Override
	public void  insertMenu(SysMenu menu) {
		
		menuMapper.insertSelective(menu);
	}

	@Override
	public List<SysMenu> updateMenu(SysMenu menu) {
		menuMapper.updateByPrimaryKeySelective(menu);
		return null;// 
	}

	@Override
	public List<SysMenu> selectFirstMenu() {
		return menuMapper.selectFirstMenu();
	}

	@Override
	public List<SysMenu> selectFirstMenuByParentId(int parentId) {
		return menuMapper.selectFirstMenuByParentId(parentId);
	}


	@Override
	public void deleteMenuById(String id, Integer useid) {
		
		SysMenu menu = menuMapper.selectByPrimaryKey(Integer.valueOf(id));
		menu.setIsdelete(true);
		menu.setLastupdateuserid(useid);
		menu.setLastupdatedatetime(new Date());
		menuMapper.updateByPrimaryKeySelective(menu);
	}

	@Override
	public SysMenu selectByPrimaryKey(Integer menuid) {
		return menuMapper.selectByPrimaryKey(Integer.valueOf(menuid));
	}

	@Override
	public Integer selectCountByParentid(Integer parentid) {
		return menuMapper.selectCountByParentid(parentid);
	}

	@Override
	public Integer selectMaxmenuIdByParentid(Integer parentid) {
		return menuMapper.selectMaxmenuIdByParentid(parentid);
	}

	@Override
	public SysMenu selectByMenuId(Integer menuid) {
		return menuMapper.selectByMenuId(menuid);
	}

	@Override
	public List<SysMenu> selectAllpageMenus() {
		return menuMapper.selectAllpageMenus();
	}

	@Override
	public List<SysMenu> selectMenusByPage() {
		
		SysMenu menu= new SysMenu();
		return menuMapper.selectMenusByPage(menu);
	}
	
	@Override
	public Integer freezeMenuById(SysMenu menu) {
		return menuMapper.updateByPrimaryKeySelective(menu);
	}
	
	/**
	 * 获取菜单列表
	 * @return
	 */
	public void renewalMenuSession(HttpSession session,SysUser sysUser){
		
		List<SysMenu> lstRoot = new ArrayList<SysMenu>();
		List<SysMenu> menus = new ArrayList<SysMenu>();
		
		menus  = menuMapper.selectSysUserMenus(sysUser.getUserid());
		
		
		if(null != menus && menus.size() > 0){
			for (SysMenu sysMenuWithBLOBs : menus) {
				
				if(sysMenuWithBLOBs.getLevel() == 1){
					lstRoot.add(sysMenuWithBLOBs);
					
				}
				
			}
			if(null != lstRoot && lstRoot.size() > 0){
				
					for (int i = 0; i < lstRoot.size(); i++) {
						
						PackageBeanUtils.packageChildSysmenus(lstRoot.get(i) , menus);
					}
				}
			}
		/**
		 * 用户的所有权限菜单
		 */
		session.setAttribute("ValidateMenus",menus);
		/**
		 * 主页面展示菜单
		 */
		session.setAttribute("menu",lstRoot);

	}




	@Override
	public Integer selectMaxOrder() {
		return menuMapper.selectMaxOrder();
	}
	
	@Override
	public Integer selectMaxOrderUnderParent(Integer parentId) {
		return menuMapper.selectMaxOrderUnderParent(parentId);
	}
	
	@Override
	public Integer selectMaxOrderIdUnderParent(Integer parentId) {
		return menuMapper.selectMaxOrderIdUnderParent(parentId);
	}
	
	@Override
	public int updateOrderBy(Integer orderBy){
		return menuMapper.updateOrderBy(orderBy);
	}
	
	@Override
	public int updateOrderByCount(Integer addNum, Integer orderBy){
		return menuMapper.updateOrderByCount(addNum, orderBy);
	}

	@Override
	public int countSonMenu(Integer menuId) {
		return menuMapper.countSonMenu(menuId);
	}




	@Override
	public int selectPrevOrder(Integer level, Integer orderBy, Integer parentId) {
		return menuMapper.selectPrevOrder(level, orderBy, parentId);
	}




	@Override
	public SysMenu selectMenuByOrderAndParent(Integer orderBy, Integer parentId) {
		return menuMapper.selectMenuByOrderAndParent(orderBy, parentId);
	}




	@Override
	public List<SysMenu> selectMenuByOrderBy(Integer orderBy) {
		return menuMapper.selectMenuByOrderBy(orderBy);
	}




	@Override
	public Integer selectNextOrder(Integer level, Integer orderBy,
			Integer parentId) {
		return menuMapper.selectNextOrder(level, orderBy, parentId);
	}




	@Override
	public Integer selectNextLevel(Integer level, Integer orderBy) {
		return menuMapper.selectNextLevel(level, orderBy);
	}




	@Override
	public Integer countAdd(Integer from, Integer to) {
		return menuMapper.countAdd(from, to);
	}




	@Override
	public List<SysMenu> addOrderBy(Integer to, Integer from) {
		return menuMapper.addOrderBy(to, from);
	}




	@Override
	public List<SysMenu> deleteOrderBy(Integer from, Integer next) {
		return menuMapper.deleteOrderBy( from, next);
	}




	@Override
	public SysMenu selectMenuByUrl(String menuUrl) {
		return menuMapper.selectMenuByUrl(menuUrl);
	}
}
