/*
 * 该类主要负责系统主要业务逻辑的实现,如多表处理的事务操作、权限控制等
 * 该类根据具体的业务逻辑来调用该实体对应的Dao或者多个Dao来实现数据库操作
 * 实际的数据库操作在对应的Dao或其他Dao中实现
 */

package com.blueland.bpm.manage.config.web;

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

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

import com.blueland.bpm.common.util.StringUtils;
import com.blueland.bpm.engine.config.dao.SysGlTypeDao;
import com.blueland.bpm.engine.config.model.SysGlType;
import com.blueland.bpm.engine.config.service.SysGlTypeManager;
import com.blueland.bpm.manage.util.TreeNode;


/**
 * @author lw
 * @version 1.0
 * @since
 */

@Service
public class SysGlTypeTreeManager {
	@Autowired
	private SysGlTypeDao sysGlTypeDao;
	@Autowired
	private SysGlTypeManager sysGlTypeManager;
	
	public List<TreeNode> getTypeTree(String typeId,String appKey) {
		List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		//当前节点
		SysGlType curNode =null;
		if (StringUtils.isEmpty(typeId)){
			//处理多个顶级节点逻辑
			SysGlType param = new SysGlType();
			param.setTypeParentId("NONE");
			List<SysGlType> topNodes =  sysGlTypeManager.findByExample(param);
			for (SysGlType sysGlType : topNodes) {
				TreeNode treeNode = new TreeNode(sysGlType.getTypeId(),
						sysGlType.getTypeParentId(), sysGlType.getTypeName(), true,
						true, isChildNode(sysGlType,appKey), "TYPE", "false",0);
				treeNodeList.add(treeNode);
				addSubTreeNode(sysGlType,treeNodeList,appKey);
			}
			return treeNodeList;
		}else{
			curNode = (SysGlType) sysGlTypeManager.getById(typeId);
		}
		//ajax处理单节点逻辑
		if (curNode==null)
			return treeNodeList;
		addSubTreeNode(curNode,treeNodeList,appKey);
		return treeNodeList;
	}
	private void addSubTreeNode(SysGlType topNode,List<TreeNode> treeNodeList,String appKey){
		SysGlType param = new SysGlType();
		param.setTypeParentId(topNode.getTypeId());
		if(topNode.getAppKey().equals("BIZ_TYPE")){
			param.setAppKey(appKey);
		}
		param.setSortColumns("TYPE_SORT");
		List<SysGlType> list = sysGlTypeManager.findByExample(param);
		for (SysGlType data : list) {
			addTreeNode(treeNodeList, data, false,"true");
		}
	}

	/**
	 * 根据menu组装TreeNode对象并添加到treeNodeList中
	 * 
	 * @param treeNodeList
	 *            treeNodeList
	 * @param menu
	 *            菜单对象
	 * @param isOpenMenu
	 *            是否打开菜单
	 */
	private void addTreeNode(List<TreeNode> treeNodeList, SysGlType sysGlType, boolean isOpenNode,String isAccessTopNode) {
		TreeNode treeNode = new TreeNode(sysGlType.getTypeId(),
				sysGlType.getTypeParentId(), sysGlType.getTypeName(), true,
				isOpenNode, isChildNode(sysGlType,""), "TYPE", isAccessTopNode,0);
		treeNodeList.add(treeNode);

	}
	private boolean isChildNode(SysGlType sysGlType,String appKey){
		SysGlType param = new SysGlType();
		param.setTypeParentId(sysGlType.getTypeId());
		if(sysGlType.getAppKey().equals("BIZ_TYPE")){
			param.setAppKey(appKey);
		}
		long totalCount = sysGlTypeDao.findEntityCount(param);
		/**
		 * 判断当前菜单是否有子菜单
		 */
		boolean isChildNode = true;
		if (totalCount <= 0) {
			isChildNode = false;
		}
		return isChildNode;
	}
	private boolean isTopNode(SysGlType sysGlType){
		if ("".equals(sysGlType.getTypeId())) {
			return true;
		}
		return false;
	}
	
}
