package org.fouram.service;

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

import org.apache.commons.lang.StringUtils;
import org.fouram.core.base.constants.SysConstants;
import org.fouram.core.base.model.OperateBean;
import org.fouram.core.base.model.Page;
import org.fouram.core.base.model.PageData;
import org.fouram.core.base.service.BaseService;
import org.fouram.core.util.RightsHelper;
import org.fouram.core.util.UUIDUtil;
import org.fouram.entity.SysMenu;
import org.fouram.entity.SysRole;
import org.fouram.entity.SysUser;
import org.springframework.stereotype.Service;

@Service
public class SysMenuService extends BaseService {

	public void deleteMenuById(String MENU_ID) throws Exception {
		save("SysMenuMapper.deleteMenuById", MENU_ID);
	}

	public PageData getMenuById(PageData pd) throws Exception {
		return (PageData) findObject("SysMenuMapper.getMenuById", pd);
	}

	public PageData findMaxId(PageData pd) throws Exception {
		return (PageData) findObject("SysMenuMapper.findMaxId", pd);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<SysMenu> listAllParentMenuByRoleId(PageData pd) throws Exception {
		return (List) findList("SysMenuMapper.listAllParentMenuByRoleId", pd);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<SysMenu> listAllParentMenuByAdmin(PageData pd) throws Exception {
		return (List) findList("SysMenuMapper.listAllParentMenuByAdmin", pd);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<PageData> listPageByParam(Page page) throws Exception {
		return (List) findList("SysMenuMapper.listPageByParam", page);
	}

	/**
	 * 获取所有父菜单
	 * 
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<PageData> getAllParent(PageData pd) throws Exception {
		return (List) findList("SysMenuMapper.getAllParent", pd);
	}

	public Long findCount(Page page) throws Exception {
		return (Long) findObject("SysMenuMapper.findCount", page);
	}

	public void saveMenu(PageData pd) throws Exception {
		save("SysMenuMapper.insertMenu", pd);
	}

	@SuppressWarnings("unchecked")
	public List<SysMenu> listAllSubMenuByParentId(String parentId) throws Exception {
		return (List<SysMenu>) findList("SysMenuMapper.listAllSubMenuByParentId", parentId);
	}
	
	@SuppressWarnings("unchecked")
	public List<SysMenu> getSubMenuByParentIdAndRoleId(String parentId, String roleId) throws Exception {
		PageData pd = new PageData();
		pd.put("parentId", parentId);
		pd.put("roleId", roleId);
		return (List<SysMenu>) findList("SysMenuMapper.listSubMenuByParentIdAndRoleId", pd);
	}

	/**
	 * 菜单分配时，获取所有菜单
	 * 
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<SysMenu> listAllMenuOfRole(PageData pd) throws Exception {
		List<String> menu_ids = (List<String>) findList("SysMenuMapper.menuListOfRole", pd);
		List<SysMenu> menus = (List<SysMenu>) findList("SysMenuMapper.getAllParents", pd);
		for (SysMenu menu : menus) {
			if (menu_ids.contains(menu.getId())) {
				menu.setHasMenu(true);
			}
			List<SysMenu> subList = listAllSubMenuByParentId(menu.getId());
			menu.setSubMenu(subList);
			if (subList != null && !subList.isEmpty()) {
				int count = 0;
				for (SysMenu subMenu : subList) {
					if (menu_ids.contains(subMenu.getId())) {
						subMenu.setHasMenu(true);
						count += 1;
					}
				}
				if (count > 0) {
					menu.setHasMenu(true);
				}
			}
		}
		return menus;
	}

	/**
	 * 获取该权限下的所有菜单
	 * 
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public List<SysMenu> getAllMenuByRoleId(PageData pd) throws Exception {
		List<SysMenu> rl = null;
		if(SysConstants.ADMIN.equals(pd.getString("username"))){
			rl = listAllParentMenuByAdmin(pd);
		}else{
			rl = listAllParentMenuByRoleId(pd);
		}
		for (SysMenu menu : rl) {
			List<SysMenu> subList = null;
			if(SysConstants.ADMIN.equals(pd.getString("username"))){
				subList = listAllSubMenuByParentId(menu.getId());
			}else{
				subList = getSubMenuByParentIdAndRoleId(menu.getId(), pd.getString("role_id"));
			}
			String pUuidKey = "";
			for(SysMenu sub : subList){
				String uuidKey = UUIDUtil.get32UUID();
				sub.setKey(uuidKey);
				pUuidKey += uuidKey;
			}
			menu.setSubMenu(subList);
			if(StringUtils.isBlank(pUuidKey)){
				pUuidKey = UUIDUtil.get32UUID();
			}
			menu.setKey(pUuidKey);
			if (subList != null && !subList.isEmpty()) {
				menu.setHasMenu(true);
			} else {
				menu.setHasMenu(false);
			}
		}
		return rl;
	}

	public PageData edit(PageData pd) throws Exception {
		return (PageData) findObject("SysMenuMapper.updateMenu", pd);
	}

	public List<String> getParentIdById(String id) throws Exception {
		List<String> list = new ArrayList<String>();
		list.add(id);
		PageData pa = new PageData();
		pa.put("id", id);
		PageData pd = (PageData) findObject("SysMenuMapper.getMenuById", pa);
		if (pd != null && pd.getString("parent_id") != null) {
			list.add(pd.getString("parent_id"));
		}
		return list;
	}

	/**
	 * 根据菜单id和角色id获取对应菜单的增删改查权限
	 * @param menuId
	 * @param roleId
	 * @return
	 * @throws Exception 
	 */
	public OperateBean getOperateBean(String menuId, SysUser user) throws Exception {
		SysRole role = (SysRole) findObject("SysRoleMapper.getRoleById", user.getRoleId());
		OperateBean op = new OperateBean();
		if(RightsHelper.testRights(role.getAddFlag(), menuId) || SysConstants.ADMIN.equals(user.getUserName())){
			op.setAdd(true);
		}
		if(RightsHelper.testRights(role.getUpdateFlag(), menuId) || SysConstants.ADMIN.equals(user.getUserName())){
			op.setUpdate(true);
		}
		if(RightsHelper.testRights(role.getDeleteFlag(), menuId) || SysConstants.ADMIN.equals(user.getUserName())){
			op.setDelete(true);
		}
		if(RightsHelper.testRights(role.getQueryFlag(), menuId) || SysConstants.ADMIN.equals(user.getUserName())){
			op.setQuery(true);
		}
		return op;
	}
}