package com.ssm.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import com.ssm.model.system.SysDepartment;
import com.ssm.model.system.SysMenu;

public class PackageBeanUtils {

	public static SysMenu getBean(HttpServletRequest request,
			SysMenu menuBLOBs) {

		return menuBLOBs;

	}

	@SuppressWarnings("rawtypes")
	public static <T> void BeanPackage(HttpServletRequest request, T t)
			throws IllegalAccessException, InvocationTargetException {

		Map<String, String[]> parameterMap = request.getParameterMap();

		Set entryset = parameterMap.entrySet();
		Iterator iter = entryset.iterator();
		while (iter.hasNext()) {
			Entry entry = (Entry) iter.next();

			String[] valueArr = (String[]) entry.getValue();
			String value = valueArr[0];
			if (StringUtils.isNotBlank(value.toString())) {
				System.out.println(entry.getKey() + "===>" + value.trim());
				BeanUtils.setProperty(t, entry.getKey().toString(),
						value.trim());
			}

		}
	}

	public static <T> List<T> BeanPackageTolist(
			List<Map<String, Object>> sysMenusMap, T t)
			throws IllegalAccessException, InvocationTargetException {
		
		List<T> list = new ArrayList<T>();
		for (Map<String, Object> map : sysMenusMap) {
			Set<Entry<String, Object>> entryset = map.entrySet();
			Iterator<Entry<String, Object>> iter = entryset.iterator();
			while (iter.hasNext()) {
				Entry<?, ?> entry = (Entry<?, ?>) iter.next();
				String value = (String) entry.getValue();
				if (StringUtils.isNotBlank(value.toString())) {
					System.out.println(entry.getKey() + "===>" + value.trim());
					BeanUtils.setProperty(t, entry.getKey().toString()
							.toLowerCase(), value.trim());
				}
			}
			list.add(t);
		}

		return list;
	}
	
	/**
	 * 规定了子菜单的类型 必须是page 
	 * @param sysMenuWithBLOBs
	 * @param menus
	 */
	public static void packageChildSysmenus(SysMenu sysMenuWithBLOBs,
			List<SysMenu> menus) {
		Integer menuid = sysMenuWithBLOBs.getMenuid();
		List<SysMenu> childmenmenus = new ArrayList<SysMenu>();
		for (SysMenu sysMenuWithBLOBs2 : menus) {
			if(menuid == sysMenuWithBLOBs2.getParentid() && "page".equalsIgnoreCase(sysMenuWithBLOBs2.getMenutype())){
				childmenmenus.add(sysMenuWithBLOBs2);
			}
		}
		if(!childmenmenus.isEmpty()){
			for (SysMenu sysMenuWithBLOBs3 : childmenmenus) {
				packageChildSysmenus(sysMenuWithBLOBs3,menus);
			}
		}
		sysMenuWithBLOBs.setChildMenu(childmenmenus);
		
	}

	/**
	 * 包含三层结构
	 * @param supMenucnname
	 * @param sysMenuWithBLOBs
	 * @param menus
	 */
	public static void packageParentlist( SysMenu sysMenuWithBLOBs,
			List<SysMenu> menus) {
		Integer menuid = sysMenuWithBLOBs.getMenuid();
		List<SysMenu> childmenmenus = new ArrayList<SysMenu>();
		for (SysMenu sysMenuWithBLOBs2 : menus) {
			if(menuid == sysMenuWithBLOBs2.getParentid()){
				if(sysMenuWithBLOBs2.getLevel() == 2) {
					sysMenuWithBLOBs2.setMenucnname(sysMenuWithBLOBs.getMenucnname() +"&gt;&gt;"+ sysMenuWithBLOBs2.getMenucnname());
				} else if(sysMenuWithBLOBs2.getLevel() == 3){
					
					sysMenuWithBLOBs2.setMenucnname( sysMenuWithBLOBs.getMenucnname() 
							+"&gt;&gt;"+ sysMenuWithBLOBs2.getMenucnname());
				}
				childmenmenus.add(sysMenuWithBLOBs2);
			}
		}
		if(!childmenmenus.isEmpty()){
			for (SysMenu sysMenuWithBLOBs3 : childmenmenus) {
				packageParentlist( sysMenuWithBLOBs3, menus);
			}
		}
		sysMenuWithBLOBs.setChildMenu(childmenmenus);
		
	}
	
	public static void packageParentDepartmentlist(SysDepartment sysDepartment,
			List<SysDepartment> sysDepartmentlist) {
		Integer parentid=sysDepartment.getDepartmentid();
		List<SysDepartment> childmenmenus = new ArrayList<SysDepartment>();
		for (SysDepartment sysDepartment2 : sysDepartmentlist) {
			if(parentid==sysDepartment2.getParentid()){	
				sysDepartment2.setDepname(sysDepartment.getDepname()+"&gt;&gt;"+sysDepartment2.getDepname());
				childmenmenus.add(sysDepartment2);
			}
		}
		
		if(null != childmenmenus && childmenmenus.size() != 0){
			for (SysDepartment sysDepartment3 : childmenmenus) {
				packageParentDepartmentlist(sysDepartment3, sysDepartmentlist);
			}
		}
		sysDepartment.setChildSysDepartment(childmenmenus);
		
	}
}
