package com.blueland.bpm.manage.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.repository.Model;
import org.apache.commons.collections.MultiMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blueland.bpm.common.base.BaseSpringController;
import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.bpm.manage.config.model.SearchProcessResource;
import com.blueland.bpm.manage.util.BaseUser;
import com.blueland.bpm.manage.util.MyContants;

@Controller
@RequestMapping(value = "/searchProcessResource/")
public class SearchProcessResourceController extends BaseSpringController{

	@Autowired
	private ProcessDefinitionService processDefinitionService;	
	/** 
	 * 执行搜索（根据用户选择的系统ID、资源ID和资源类型查询流程）
	 * @throws Exception
	 **/
	@RequestMapping(value = "list")
	public ModelAndView list(SearchProcessResource searchProcessResource,HttpServletRequest request) throws Exception {
		//判断是否为系统管理员，是：显示系统编码文本框可以查询所有系统下的流程，不是只能查询本系统下的流程
		boolean isebpmAdmin =(Boolean) request.getSession().getAttribute(BaseUser.ROLE_EBPMADMIN);
		String appId = "";
		//不是系统管理员，从session中取出系统ID
		if(!isebpmAdmin){
		    appId = request.getSession().getAttribute(MyContants.APP_ID_KEY).toString();
			searchProcessResource.setApplicationId(appId);
		}
		//系统ID
		appId = searchProcessResource.getApplicationId();				
		
		String resourceId = searchProcessResource.getResourceId();	
		
		//资源类型：分为三种（角色role、组织org、人员user）
		String resourceType = searchProcessResource.getResourceType();
		
		//map返回结果,tenantId、proceDefId、procDefName、activityId、activityName
		List<Map<String,String>> inProcess = processDefinitionService.findProcessDefsByResource(appId,"",resourceType,resourceId);
		ModelAndView result = new ModelAndView("/searchProcessResource/list");
		
		//回显资源编码和资源类型值
		result.addObject("searchProcessResource", searchProcessResource);		
	
		result.addObject("inProcess", inProcess);
		return result;
	}
	
	/** 
	 * 执行搜索（根据用户选择的系统ID、资源ID和资源类型查询流程）
	 * @throws Exception
	 **/
	@RequestMapping(value = "errorlistindex")
	public ModelAndView errorlistindex(SearchProcessResource searchProcessResource,HttpServletRequest request) throws Exception {
		/*//判断是否为系统管理员，是：显示系统编码文本框可以查询所有系统下的流程，不是只能查询本系统下的流程
		boolean isebpmAdmin =(Boolean) request.getSession().getAttribute(BaseUser.ROLE_EBPMADMIN);
		*/String appId = "";
		//不是系统管理员，从session中取出系统ID
		//if(!isebpmAdmin){
		    appId = request.getSession().getAttribute(MyContants.APP_ID_KEY).toString();
			searchProcessResource.setApplicationId(appId);
		//}
		//系统ID
		appId = searchProcessResource.getApplicationId();				
		
		ModelAndView result = new ModelAndView("/searchProcessResource/errorlist");
		
		//回显资源编码和资源类型值
		result.addObject("searchProcessResource", null);		
	
		result.addObject("inProcess", null);
		return result;
	}
	
	/** 
	 * 执行搜索（根据用户选择的系统ID、资源ID和资源类型查询流程）
	 * @throws Exception
	 **/
	@RequestMapping(value = "errorlist")
	public ModelAndView errorlist(SearchProcessResource searchProcessResource,HttpServletRequest request) throws Exception {
		/*//判断是否为系统管理员，是：显示系统编码文本框可以查询所有系统下的流程，不是只能查询本系统下的流程
		boolean isebpmAdmin =(Boolean) request.getSession().getAttribute(BaseUser.ROLE_EBPMADMIN);
		*/String appId = "";
		//不是系统管理员，从session中取出系统ID
		//if(!isebpmAdmin){
		    appId = request.getSession().getAttribute(MyContants.APP_ID_KEY).toString();
			searchProcessResource.setApplicationId(appId);
		//}
		//系统ID
		appId = searchProcessResource.getApplicationId();				
		
		
		//map返回结果,tenantId、proceDefId、procDefName、activityId、activityName
		List<Map<String,Object>> inProcess = new ArrayList<Map<String,Object>>();
		ModelAndView result = new ModelAndView("/searchProcessResource/errorlist");
		
		//回显资源编码和资源类型值
		result.addObject("searchProcessResource", searchProcessResource);		
	
		result.addObject("inProcess", inProcess);
		return result;
	}
	
	
	
}
