package com.blueland.bpm.manage.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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.application.service.ApplicationManager;
import com.blueland.bpm.common.base.BaseSpringController;
import com.blueland.bpm.manage.countAnalysis.service.ProcessMonitorService;
/**
 * @作者     dsl
 * @创建时间  2020年2月14日
 * @说明     流程首页
 */
@Controller
@RequestMapping(value = "/flow/monitor")
public class ProcessMonitorController extends BaseSpringController{
	@Autowired
	private ProcessMonitorService processMonitorService;
	@Autowired
	private ApplicationManager applicationManager;
	/** 
	 * 流程监控-流程分析
	 **/
	@RequestMapping(value = "/processAnalysis")
	public ModelAndView processAnalysis(HttpServletRequest request) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/analysis");
		return modelAndView;
	}
	/**
	 * 流程监控-流程分析-根据流程租户下钻
	 * @param request
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/analysisDetailByAppId")
	public ModelAndView analysisDetailByAppId(HttpServletRequest request,String appId) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/analysisDetail");
		//ApplicationEntity app = applicationManager.getById(appId);
		modelAndView.addObject("appId", appId);
		modelAndView.addObject("appName", "");
		return modelAndView;
	}
	/**
	 * 流程监控-流程分析-平均审批耗时最长的流程下钻
	 * @param request
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/avgUserTimePage")
	public ModelAndView avgUserTimePage(HttpServletRequest request,String appId) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/avgUserTimeDetail");
		//ApplicationEntity app = applicationManager.getById(appId);
		modelAndView.addObject("appId", appId);
		modelAndView.addObject("appName", "");
		return modelAndView;
	}
	/**
	 * 流程监控-流程分析-超时最多的流程下钻
	 * @param request
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/overTimePage")
	public ModelAndView overTimePage(HttpServletRequest request,String appId) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/overTimeDetail");
		//ApplicationEntity app = applicationManager.getById(appId);
		modelAndView.addObject("appId", appId);
		modelAndView.addObject("appName", "");
		return modelAndView;
	}
	/** 
	 * 流程监控-节点分析
	 **/
	@RequestMapping(value = "/actAnalysis")
	public ModelAndView defAnalysis(HttpServletRequest request) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/actAnalysis");
		return modelAndView;
	}
	/** 
	 * 流程监控-节点分析-耗时最长的节点下钻
	 **/
	@RequestMapping(value = "/actAvgUserTimePage")
	public ModelAndView actAvgUserTimePage(HttpServletRequest request,String appId) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/actAvgUserTimeDetail");
		//ApplicationEntity app = applicationManager.getById(appId);
		modelAndView.addObject("appId", appId);
		modelAndView.addObject("appName", "");
		return modelAndView;
	}
	/** 
	 * 流程监控-节点分析-待审流程最多的节点下钻
	 **/
	@RequestMapping(value = "/actDoProcessMaxPage")
	public ModelAndView actDoProcessMaxPage(HttpServletRequest request,String appId) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/actDoProcessMaxDetail");
		//ApplicationEntity app = applicationManager.getById(appId);
		modelAndView.addObject("appId", appId);
		modelAndView.addObject("appName", "");
		return modelAndView;
	}
	/**
	 * 流程监控-流程创建最多的流程-下钻
	 * @param request
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createMaxPorcessPage")
	public ModelAndView createMaxPorcessPage(HttpServletRequest request,String appId) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/createMaxPorcessDetail");
		//ApplicationEntity app = applicationManager.getById(appId);
		modelAndView.addObject("appId", appId);
		modelAndView.addObject("appName", "");
		return modelAndView;
	}
	/** 
	 * 流程监控-人员分析
	 **/
	@RequestMapping(value = "/staffAnalysis")
	public ModelAndView staffAnalysis(HttpServletRequest request) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/staffAnalysis");
		return modelAndView;
	}
	/** 
	 * 流程监控-人员分析
	 **/
	@RequestMapping(value = "/staffPage")
	public ModelAndView staffPage(HttpServletRequest request,String appId) throws Exception{
		ModelAndView modelAndView = new ModelAndView("/processReport/staffTopDetail");
		/*ApplicationEntity app = applicationManager.getById(appId);*/
		modelAndView.addObject("appId", appId);
		modelAndView.addObject("appName", "");
		return modelAndView;
	}
	/**
	 * 流程分析详情
	 * 待办最多流程、超时处理最多流程、创建量最高流程、平均审批耗时最长流程
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/analysisDetail")
	@ResponseBody
	public List<Map<String, Object>> analysisDetail(HttpServletRequest request) throws Exception{
		return processMonitorService.analysisDetail();
	}
	/**
	 * 节点分析详情
	 * 节点平均耗时、待办流程最多的节点
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/actAnalysisDetail")
	@ResponseBody
	public List<Map<String, Object>> actAnalysisDetail(HttpServletRequest request) throws Exception{
		return processMonitorService.actAnalysisDetail();
	}
	/**
	 * 人员分析详情
	 * 待处理流程最多的员工
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/staffAnalysisDetail")
	@ResponseBody
	public List<Map<String, Object>> staffAnalysisDetail(HttpServletRequest request) throws Exception{
		return processMonitorService.staffAnalysisDetail();
	}
	/**
	 * 平均耗时最长的流程统计详情
	 * @param request
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/avgUserTimeDetail")
	@ResponseBody
	public List<Map<String, Object>> avgUserTimeDetail(HttpServletRequest request,String appId) throws Exception{
		return processMonitorService.avgUserTimeDetail(appId);
	}
	/**
	 * 流程创建数量排名详情
	 * @param request
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createMaxProcessDetail")
	@ResponseBody
	public List<Map<String, Object>> createProcessProcessDetail(HttpServletRequest request,String appId) throws Exception{
		return processMonitorService.createMaxProcessDetail(appId);
	}
	/**
	 * 待办流程最多-下钻
	 * @param request
	 * @param appId
	 * @return 
	 * @throws Exception
	 */
	@RequestMapping(value = "/appProcessDetail")
	@ResponseBody
	public List<Map<String, Object>> appProcessDetail(HttpServletRequest request,String appId) throws Exception{
		return processMonitorService.appProcessDetail(appId);
	}
	/**
	 * 超时最多的流程详情
	 * @param request
	 * @param appId
	 * @return 
	 * @throws Exception
	 */
	@RequestMapping(value = "/overTimeProcessDetail")
	@ResponseBody
	public List<Map<String, Object>> overTimeProcessDetail(HttpServletRequest request,String appId) throws Exception{
		return processMonitorService.overTimeProcessDetail(appId);
	}
	/**
	 * 节点平均耗时-下钻详情
	 * @param request
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/actAvgUserTimeDetail")
	@ResponseBody
	public List<Map<String, Object>> actAvgUserTimeDetail(HttpServletRequest request,String appId) throws Exception{
		return processMonitorService.actAvgUserTimeDetail(appId);
	}
	/**
	 * 人员分析-下钻详情
	 * @param request
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/staffTopDetail")
	@ResponseBody
	public List<Map<String, Object>> staffTopDetail(HttpServletRequest request,String appId) throws Exception{
		return processMonitorService.staffTopDetail(appId);
	}
	/**
	 * 节点分析-待审流程最多的节点下钻详情
	 * @param request
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/actDoProcessDetail")
	@ResponseBody
	public List<Map<String, Object>> actDoProcessDetail(HttpServletRequest request,String appId) throws Exception{
		return processMonitorService.actDoProcessDetail(appId);
	}
	public static void main(String[] args) {
		String sql="select\r\n" + 
				"      mu.user_uuid inst_id,\r\n" + 
				"      mu.nickname inst_name,\r\n" + 
				"      '性别' type_name,\r\n" + 
				"      ulp.sex tag_name,\r\n" + 
				"      '年龄层次' type_name1,\r\n" + 
				"      age_group tag_name1,\r\n" + 
				"      '用户类型' type_name2, \r\n" + 
				"      user_type tag_name2,\r\n" + 
				"      '用户生命周期' type_name3,\r\n" + 
				"      user_life_cycle tag_name3,\r\n" + 
				"      '主抽品牌偏好' type_name4,\r\n" + 
				"      primary_brand tag_name4,\r\n" + 
				"      '辅抽品牌偏好' type_name5,\r\n" + 
				"      second_brand tag_name5,\r\n" + 
				"      '主抽规格偏好' type_name6,\r\n" + 
				"      primary_trandemark tag_name6,\r\n" + 
				"      '活动用户分层' type_name7,\r\n" + 
				"      user_activity_stratification tag_name7,\r\n" + 
				"      '价值用户' type_name8,\r\n" + 
				"      user_worth tag_name8,\r\n" + 
				"      '是否尝鲜用户' type_name9,\r\n" + 
				"      taste_fresh_type tag_name9\r\n" + 
				"    FROM\r\n" + 
				"      ( select\r\n" + 
				"        user_uuid,\r\n" + 
				"        ifnull(sex,'未知') sex,\r\n" + 
				"        ifnull(age_group,'未知') age_group,\r\n" + 
				"        ifnull(user_type,'未知') user_type,\r\n" + 
				"        ifnull(user_life_cycle,'未知') user_life_cycle,\r\n" + 
				"        ifnull(primary_brand,'未知') primary_brand,\r\n" + 
				"        ifnull(user_activity_stratification,'未知') user_activity_stratification,\r\n" + 
				"        ifnull(user_worth,'未知') user_worth,\r\n" + 
				"        ifnull(taste_fresh_type,'未知') taste_fresh_type,\r\n" + 
				"        ifnull(second_brand,'未知') second_brand,\r\n" + 
				"        ifnull(primary_trandemark,'未知') primary_trandemark\r\n" + 
				"      FROM\r\n" + 
				"        user_label_portrait ) ulp\r\n" + 
				"    left join (SELECT user_uuid, nickname FROM mg_user) mu on ulp.user_uuid = mu.user_uuid limit 10000";
		
		try {
		String sqlEnStr=java.net.URLEncoder.encode(sql);
		System.out.println(sqlEnStr);
		System.out.println(java.net.URLDecoder.decode(sqlEnStr));
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
