/*
 * 该类主要负责系统主要业务逻辑的实现,如多表处理的事务操作、权限控制等
 * 该类根据具体的业务逻辑来调用该实体对应的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.application.dao.ApplicationDao;
import com.blueland.bpm.application.model.ApplicationEntity;
import com.blueland.bpm.common.util.StringUtils;
import com.blueland.bpm.engine.config.model.ProcessInfoConfig;
import com.blueland.bpm.engine.config.service.ProcessInfoConfigManager;
import com.blueland.bpm.engine.core.model.ProcessDefinitionModel;
import com.blueland.bpm.engine.core.model.SearchProcessListDTO;
import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.bpm.manage.util.BaseUser;
import com.blueland.bpm.manage.util.TreeNode;


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

@Service
public class ProcDefTreeManager {
	@Autowired
	private ProcessDefinitionService processDefinitionService;
	@Autowired
	private ProcessInfoConfigManager processInfoConfigManager;
	@Autowired
	private ApplicationDao applicationDao;
	
	
	public List<TreeNode> getProcDefTree(String typeId,String userId,String selectType) {
		List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		if(null == typeId){//初始化树
			boolean isParent = true;
			//selectType不为空时，是代表委托界面获取流程节点，不需要展示顶层节点和二级以下的节点，isParent=false是不展示二级节点用的
			if(!StringUtils.isEmpty(selectType)){
				isParent = false;	
				//自定义顶级节点
				TreeNode treeNode = new TreeNode("1",
						"-1", "可视化流程平台", true,
						true, true, "TOP", "false",0);
				treeNodeList.add(treeNode);
			}else{
				//自定义顶级节点
				TreeNode treeNode = new TreeNode("1",
						"-1", "所有流程", true,
						true, true, "TOP", "false",0);
				treeNodeList.add(treeNode);
			}
			
			
			//获取当前登录用户有权限查看的业务系统列表
			List<ApplicationEntity> applications = applicationDao.selectByUserId(userId);
			
			//添加业务系统节点
			for (ApplicationEntity application : applications) {				
					TreeNode tempnode = new TreeNode(application.getAppId(),
							"1",application.getAppName(), true,
							true,isParent, "SYS", "false",1);
					treeNodeList.add(tempnode);			
			}
		}else{//选中某个节点展开
			ApplicationEntity application = applicationDao.getById(typeId);
			if(null!=application){//点击的是业务系统节点
				//根据业务系统id查询该系统下对应的流程定义数据
				ProcessInfoConfig query = new ProcessInfoConfig();
				query.setTenantId(application.getAppId());
				query.setProcessMgrIsshow(ProcessInfoConfig.PROCESS_MGR_ISSHOW_Y);
				
				List<ProcessInfoConfig> processInfoConfigs =  processInfoConfigManager.findByExample(query);
				for (ProcessInfoConfig processInfoConfig : processInfoConfigs) {
					TreeNode processInfoConfignode = new TreeNode(
							processInfoConfig.getProcessDefId(),
							application.getAppId(),
							processInfoConfig.getProcessDefName(), false,
							false, false, "TYPE", "false",2);
					treeNodeList.add(processInfoConfignode);
				}
			}else{//如果不是业务系统节点
				
			}
		}
		return treeNodeList;

	}
	public List<TreeNode> getProcDefTree_bak(SearchProcessListDTO searchProcessListDTO) {
		List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		List<ProcessDefinitionModel> list = null;
		TreeNode treeNode = new TreeNode("1",
				"-1", "所有流程", true,
				true, true, "TOP", "false",0);
		treeNodeList.add(treeNode);
		if (StringUtils.isEmpty(searchProcessListDTO.getFlowType())){
			list =  processDefinitionService.findProcessDefs(searchProcessListDTO);
		}
		for (ProcessDefinitionModel processDefinitionModel : list) {
			addTreeNode(treeNodeList, processDefinitionModel, false,"true");
		}
		return treeNodeList;
		
	}

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

	}
	/*private boolean isChildNode(SysGlType sysGlType){
		SysGlType param = new SysGlType();
		param.setTypeParentId(sysGlType.getTypeId());
		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;
	}*/
	
}
