package com.blueland.bpm.manage.countAnalysis.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueland.bpm.manage.countAnalysis.dao.IndexDao;

import groovy.util.logging.Slf4j;

/**
 * @作者     dsl
 * @创建时间  2020年2月5日
 * @说明    首页统计 
 */
@Slf4j
@Service
public class IndexService{
	@Autowired
	private IndexDao indexDao;
	/**
	 * 所有流程
	 * @return
	 */
	public int getAppCount() {	
		return indexDao.getAppCount();
	}
	/**
	 * 流程定义总数
	 * @return
	 */
	public int getProcDefKeyCount() {
		return indexDao.getProcDefKeyCount();
	}
	/**
	 * 流程运行情况总数
	 * @return
	 */
	public Map<String, Object> getProcessRunCount() {
		return indexDao.getProcessRunCount();
	}
	/**
	 * 流程执行情况
	 * @return
	 */
	public Map<String, Object> getProcessLogCount() {
		return indexDao.getProcessLogCount();
	}
	/**
	 * 近一年、一月、一周流程
	 * @param params
	 * @return
	 */
	public Map<String, Object> getProcessByTime(Map<String, Object> params) {
		return indexDao.getProcessByTime(params);
	}
	/**
	 * 流程运行情况
	 * @return
	 */
	public List<Map<String, Object>> getAllFlowDatas() {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		//流程运行情况总数
		Map<String,Object> precessRunCount = indexDao.getProcessRunCount();
		//近一年、一月、一周流程
		Map<String,Object> params = new HashMap<String, Object>();
		Map<String,Object> runCount = indexDao.getProcessByTime(params);
		//所有流程
		int total = Integer.parseInt(precessRunCount.get("TOTAL").toString());
		int un_deal = Integer.parseInt(precessRunCount.get("UN_DEAL").toString());
		int deal = Integer.parseInt(precessRunCount.get("DEAL").toString());
		list.add(this.appendPie("allFlow","所有流程",un_deal,deal,total));
		//近一年流程
		int runYear = Integer.parseInt(runCount.get("YEAR").toString());
		int finishYear = Integer.parseInt(runCount.get("FINISH_YEAR").toString());
		int yearTotal = runYear+finishYear;
		list.add(this.appendPie("yearFlow","近一年流程",runYear,finishYear,yearTotal));
		//近一月流程
		int runMonth = Integer.parseInt(runCount.get("MONTH").toString());
		int finishMonth = Integer.parseInt(runCount.get("FINISH_MONTH").toString());
		int monthTotal = runMonth+finishMonth;
		list.add(this.appendPie("monthFlow","近一月流程",runMonth,finishMonth,monthTotal));
		//近一星期流程
		int runWeek = Integer.parseInt(runCount.get("WEEK").toString());
		int finishWeek = Integer.parseInt(runCount.get("FINISH_WEEK").toString());
		int weekTotal = runWeek+finishWeek;
		list.add(this.appendPie("weekFlow","近一星期流程",runWeek,finishWeek,weekTotal));
		//组装 总计、作废、已处理、未处理总数数据
		Map<String,Object> singleCount = new HashMap<String,Object>();
		singleCount.put("total", total);
		singleCount.put("cancle", precessRunCount.get("CANCLE"));
		singleCount.put("deal", deal);
		singleCount.put("un_deal", un_deal);
		singleCount.put("id", "singleCount");
		list.add(singleCount);
		return list;
	}
	/**
	 * 拼装饼图需要的数据
	 * @param id
	 * @param name
	 * @param run
	 * @param finish
	 * @param total
	 * @return
	 */
	private Map<String, Object> appendPie(String id, String name, int run, int finish, int total) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("id", id);
		obj.put("name", name);
		obj.put("allCount", "<br>（"+total+"）");
		obj.put("datas", this.getAppendDatas(run,finish));
		return obj;
	}
	/**
	 * 拼装饼图数组
	 * @param run
	 * @param finish
	 * @return
	 */
	private List<Map<String, Object>> getAppendDatas(int run, int finish) {
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		Map<String,Object> runData = new HashMap<String, Object>();
		runData.put("name", "运行中");
		runData.put("y", run);
		datas.add(runData);
		Map<String,Object> finishData = new HashMap<String, Object>();
		finishData.put("name", "已完成");
		finishData.put("y", finish);
		datas.add(finishData);
		return datas;
	}
	public Map<String, Object> getProcessMonitorTop() {
		String column = "";
		String row = "";
		//获取横坐标数据
		List<String> monthList = initMonth();
		for (String month : monthList) {
			row += "'" + month + "月',";
		}
		if (!StringUtils.isEmpty(row)) {
			row = row.substring(0, row.length() - 1);
		}
		List<Map<String, Object>> processLogResult = indexDao.getAppTop();
		Map<String,Object> result =  new HashMap<String, Object>();
		Map<String, List> resultMap = new LinkedHashMap<String, List>();
		for (Map<String , Object> obj : processLogResult) {
			if (resultMap.containsKey(obj.get("APPNAME"))) {
				List<Map> resultList = resultMap.get(obj.get("APPNAME"));
				resultList.add(obj);
			} else {
				List<Map> resultList = new ArrayList<Map>();
				resultList.add(obj);
				resultMap.put(obj.get("APPNAME").toString(), resultList);
			}
			int runCountInt = Integer.parseInt(obj.get("RUNCOUNT").toString());
			
		}
		//获取纵坐标数据
		for (Map.Entry<String, List> entry : resultMap.entrySet()) {
			List<Map<String, List>> processLogResultList = entry.getValue();
			String data = "";
			column += "{name: '" + entry.getKey() + "',";
			for (String month : monthList) {
				boolean isContains = false;
				Map<String, List> processLog = null;
				for (Map<String, List> processLog1 : processLogResultList) {
					if (month.equals(processLog1.get("MONTH"))) {
						isContains = true;
						processLog = processLog1;
						break;
					}
				}
				if (isContains) {
					data += processLog.get("RUNCOUNT") + ",";
				} else {
					data += "0,";
				}
			}
			if (!StringUtils.isEmpty(data)) {
				data = data.substring(0, data.length() - 1);
			}
			column += "data: eval([" + data + "])},";
		}

		result.put("row", row);
		result.put("column", column);
		return result;
	}
	/**
	 * 系统负载数据
	 * @return
	 */
	public Map<String, Object> getSysLoadData() {
		List<Map<String, Object>> list = indexDao.getSysLoadData();
		Map<String,Object> result =  new HashMap<String, Object>();
		ArrayList<ArrayList> datas =new ArrayList<ArrayList>();
		for (Map<String, Object> obj : list) {
			ArrayList<String> data=new ArrayList<String>();
			data.add("'"+Integer.parseInt(obj.get("MONTH").toString())+"-"+Integer.parseInt(obj.get("DAY").toString())+"'");
			data.add(obj.get("RUNCOUNT").toString());
			datas.add(data);	
		}
		result.put("datas", "[{data:"+datas+"}]");
		return result;
	}
	private List<String> initMonth(){
		List<String> months=new ArrayList<String>();
		for (int i = 1; i <=12; i++) {
			if(i<10){
				months.add("0"+i);
			}else{
				months.add(""+i);
			}
		}
		return months;
	}
	
	
}
