package nc.impl.sscpfa.pfareport;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.impl.sscpfa.pfareport.bp.SSCWorkingGroupBP;
import nc.itf.sscpfa.pfareport.ISSCWorkGroupService;
import nc.pub.sscpfa.report.useropration.UserOprationHandleService;
import nc.pubitf.rbac.IUserPubService;
import nc.vo.pub.BusinessException;
import nc.vo.sm.UserVO;
import nc.vo.sscpfa.pfareport.report.SSCUserOprationReportVO;
import nc.vo.sscpfa.pfareport.report.SSCWorkingGroupBillReportVO;

public class SSCWorkGroupServiceImpl implements ISSCWorkGroupService {
	public List<SSCWorkingGroupBillReportVO> listSSCWorkingGroupBillData(String pkUnit, String[] pkWorkGroups,
			String pre_startdate, String pre_enddate, String cur_startdate, String cur_enddate)
			throws BusinessException {
		SSCWorkingGroupBP workingGroupBP = new SSCWorkingGroupBP();
		String[] leafPkWorkGroups = workingGroupBP.listLeafPkWorkGroups(pkWorkGroups);
		List<SSCWorkingGroupBillReportVO> pers = workingGroupBP.listBillDealData(pkUnit, pkWorkGroups, leafPkWorkGroups,
				pre_startdate, pre_enddate);
		List<SSCWorkingGroupBillReportVO> curs = workingGroupBP.listBillDealData(pkUnit, pkWorkGroups, leafPkWorkGroups,
				cur_startdate, cur_enddate);
		if (pers != null && pers.size() > 0) {
			for (int i = 0; i < pers.size(); ++i) {
				int pre_quantity = ((SSCWorkingGroupBillReportVO) pers.get(i)).getPre_quantity();
				int cur_quantity = ((SSCWorkingGroupBillReportVO) curs.get(i)).getPre_quantity();
				((SSCWorkingGroupBillReportVO) pers.get(i)).setCur_quantity(cur_quantity);
				((SSCWorkingGroupBillReportVO) pers.get(i))
						.setChange_rate(this.formatRate(cur_quantity - pre_quantity, pre_quantity));
			}
		}

		return pers;
	}

	private String formatRate(int num1, int num2) {
		if (num1 == 0) {
			return "0.00";
		} else if (num2 == 0) {
			return "-";
		} else {
			DecimalFormat df = new DecimalFormat();
			df.setMaximumFractionDigits(2);
			df.setMinimumFractionDigits(2);
			return df.format((double) num1 * 100.0D / (double) num2) + "%";
		}
	}

	private String secToMinOrHour(double second, String timeUnit) {
		if (second == 0.0D) {
			return "0.00";
		} else {
			DecimalFormat df = new DecimalFormat("0.00");
			df.setMaximumFractionDigits(2);
			df.setMinimumFractionDigits(2);
			return "hour".equals(timeUnit) ? df.format(second / 3600.0D) : df.format(second / 60.0D);
		}
	}

	private Map<String, Object> listToMap(List<Map<String, Object>> list) {
		Map<String, Object> result = new HashMap();
		String pk_workinggroup = "";
		String pk_user = "";
		String actiontype = "";
		if (list != null && list.size() > 0) {
			Iterator var6 = list.iterator();

			while (var6.hasNext()) {
				Map<String, Object> map = (Map) var6.next();
				pk_workinggroup = map.get("pk_workinggroup") == null ? "" : (String) map.get("pk_workinggroup");
				pk_user = map.get("pk_user") == null ? pk_user : (String) map.get("pk_user");
				actiontype = map.get("actiontype") == null ? actiontype : (String) map.get("actiontype");
				result.put(pk_workinggroup + pk_user + actiontype, map);
			}
		}

		return result;
	}

	private List<SSCUserOprationReportVO> userOprationReportVOHandle(List<Map<String, String>> sscuserInfoList,
			List<Map<String, Object>> preApproveAndRejectData, List<Map<String, Object>> preHandonAndAdjustTimeData,
			List<Map<String, Object>> preRejectedData, List<Map<String, Object>> curApproveAndRejectData,
			List<Map<String, Object>> curHandonAndAdjustTimeData, List<Map<String, Object>> curRejectedData,
			Map<String, Long> preEliminatedTime, Map<String, Long> curEliminatedTime, String timeUnit) {
		List<SSCUserOprationReportVO> result = new ArrayList();
		SSCUserOprationReportVO vo = null;
		if (sscuserInfoList != null && sscuserInfoList.size() > 0) {
			Map<String, Object> preApproveAndReject = this.listToMap(preApproveAndRejectData);
			Map<String, Object> preHandonAndAdjustTime = this.listToMap(preHandonAndAdjustTimeData);
			Map<String, Object> preRejected = this.listToMap(preRejectedData);
			Map<String, Object> curApproveAndReject = this.listToMap(curApproveAndRejectData);
			Map<String, Object> curHandonAndAdjustTime = this.listToMap(curHandonAndAdjustTimeData);
			Map<String, Object> curRejected = this.listToMap(curRejectedData);
			Iterator var17 = sscuserInfoList.iterator();

			while (var17.hasNext()) {
				Map<String, String> sscUserInfo = (Map) var17.next();
				vo = new SSCUserOprationReportVO();
				String pk_workinggroup = (String) sscUserInfo.get("pk_workinggroup");
				String pk_user = (String) sscUserInfo.get("pk_user");
				vo.setPk_workinggroup(pk_workinggroup);
				vo.setPk_workinggroup_code((String) sscUserInfo.get("pk_workinggroup_code"));
				vo.setPk_workinggroup_name((String) sscUserInfo.get("pk_workinggroup_name"));
				vo.setPk_user(pk_user);
				vo.setPk_user_name((String) sscUserInfo.get("pk_user_name"));
				Map<String, Object> data = (Map) preApproveAndReject.get(pk_workinggroup + pk_user + "sscapprove");
				int preDealTime = 0;
				int curDealTime = 0;
				long preSubTime = 0;// 需要减去的时间
				long curSubTime = 0;// 需要减去的时间
				if (preEliminatedTime != null) {
					preSubTime = preEliminatedTime.getOrDefault(pk_workinggroup + pk_user, 0L);
				}
				if (curEliminatedTime != null) {
					curSubTime = curEliminatedTime.getOrDefault(pk_workinggroup + pk_user, 0L);
				}
				if (data == null) {
					vo.setPre_sscapprove(0);
				} else {
					vo.setPre_sscapprove((Integer) data.get("num"));
					preDealTime += (Integer) data.get("totaltime");
				}

				data = (Map) preApproveAndReject.get(pk_workinggroup + pk_user + "sscreject");
				if (data == null) {
					vo.setPre_sscreject(0);
				} else {
					vo.setPre_sscreject((Integer) data.get("num"));
					preDealTime += (Integer) data.get("totaltime");
				}

				data = (Map) preHandonAndAdjustTime.get(pk_workinggroup + pk_user);
				if (data != null) {
					preDealTime += (Integer) data.get("totaltime");
				}
				
				preDealTime = preDealTime - (int)preSubTime;

				data = (Map) preRejected.get(pk_workinggroup + pk_user);
				vo.setPre_sscrejected(data == null ? 0 : (Integer) data.get("num"));
				vo.setPre_sscreject_rate(
						this.formatRate(vo.getPre_sscreject(), vo.getPre_sscreject() + vo.getPre_sscapprove()));
				vo.setPre_deal_time(this.secToMinOrHour((double) preDealTime, timeUnit));
				int oprationNum = vo.getPre_sscapprove() + vo.getPre_sscreject();
				vo.setPre_avg_deal_time(this.secToMinOrHour(
						oprationNum == 0 ? 0.0D : (double) preDealTime / (double) oprationNum, timeUnit));
				data = (Map) curApproveAndReject.get(pk_workinggroup + pk_user + "sscapprove");
				if (data == null) {
					vo.setCur_sscapprove(0);
				} else {
					vo.setCur_sscapprove((Integer) data.get("num"));
					curDealTime += (Integer) data.get("totaltime");
				}

				data = (Map) curApproveAndReject.get(pk_workinggroup + pk_user + "sscreject");
				if (data == null) {
					vo.setCur_sscreject(0);
				} else {
					vo.setCur_sscreject((Integer) data.get("num"));
					curDealTime += (Integer) data.get("totaltime");
				}

				data = (Map) curHandonAndAdjustTime.get(pk_workinggroup + pk_user);
				if (data != null) {
					curDealTime += (Integer) data.get("totaltime");
				}
				
				curDealTime = curDealTime - (int)curSubTime;

				data = (Map) curRejected.get(pk_workinggroup + pk_user);
				vo.setCur_sscrejected(data == null ? 0 : (Integer) data.get("num"));
				vo.setCur_sscreject_rate(
						this.formatRate(vo.getCur_sscreject(), vo.getCur_sscreject() + vo.getCur_sscapprove()));
				vo.setCur_deal_time(this.secToMinOrHour((double) curDealTime, timeUnit));
				oprationNum = vo.getCur_sscapprove() + vo.getCur_sscreject();
				vo.setCur_avg_deal_time(this.secToMinOrHour(
						oprationNum == 0 ? 0.0D : (double) curDealTime / (double) oprationNum, timeUnit));
				vo.setChange_sscapprove(
						this.formatRate(vo.getCur_sscapprove() - vo.getPre_sscapprove(), vo.getPre_sscapprove()));
				vo.setChange_sscreject(
						this.formatRate(vo.getCur_sscreject() - vo.getPre_sscreject(), vo.getPre_sscreject()));
				vo.setChange_sscrejected(
						this.formatRate(vo.getCur_sscrejected() - vo.getPre_sscrejected(), vo.getPre_sscrejected()));
				vo.setChange_deal_time(this.formatRate(curDealTime - preDealTime, preDealTime));
				vo.setChange_avg_deal_time(vo.getChange_deal_time());
				vo.setChange_sscreject_rate(vo.getChange_sscreject());
				result.add(vo);
			}
		}

		return result;
	}

	public List<SSCUserOprationReportVO> listSSCUserOprationData(String pkUnit, String[] pkWorkGroups,
			String pre_startdate, String pre_enddate, String cur_startdate, String cur_enddate, String timeUnit)
			throws BusinessException {
		SSCWorkingGroupBP workingGroupBP = new SSCWorkingGroupBP();
		UserOprationHandleService handleService = (UserOprationHandleService)NCLocator.getInstance().lookup(UserOprationHandleService.class);
		long begin = System.currentTimeMillis();
		List<Map<String, String>> sscuserInfoList = workingGroupBP.listWorkingGroupAndUser(pkWorkGroups);
		List<Map<String, Object>> preApproveAndRejectData = workingGroupBP.listApproveAndRejectData(pkWorkGroups,
				pre_startdate, pre_enddate);
		sscuserInfoList = this.getSSCUserList(sscuserInfoList, preApproveAndRejectData);
		List<Map<String, Object>> preHandonAndAdjustTimeData = workingGroupBP.listHandonAndAdjustTimeData(pkWorkGroups,
				pre_startdate, pre_enddate);
		List<Map<String, Object>> preRejectedData = workingGroupBP.listRejectedData(pkWorkGroups, pre_startdate,
				pre_enddate);
		List<Map<String, Object>> curApproveAndRejectData = workingGroupBP.listApproveAndRejectData(pkWorkGroups,
				cur_startdate, cur_enddate);
		List<Map<String, Object>> curHandonAndAdjustTimeData = workingGroupBP.listHandonAndAdjustTimeData(pkWorkGroups,
				cur_startdate, cur_enddate);
		List<Map<String, Object>> curRejectedData = workingGroupBP.listRejectedData(pkWorkGroups, cur_startdate,
				cur_enddate);
		
		List<Map<String, Object>> preAllApproveAndRejectData = workingGroupBP.getAllApproveAndRejectData(pkWorkGroups,
				pre_startdate, pre_enddate);
		List<Map<String, Object>> curAllApproveAndRejectData = workingGroupBP.getAllApproveAndRejectData(pkWorkGroups,
				cur_startdate, cur_enddate);
		List<Map<String, Object>> preAllHandonAndAdjustTimeData = workingGroupBP.getAllHandonAndAdjustTimeData(pkWorkGroups,
				pre_startdate, pre_enddate);
		List<Map<String, Object>> curAllHandonAndAdjustTimeData = workingGroupBP.getAllHandonAndAdjustTimeData(pkWorkGroups,
				cur_startdate, cur_enddate);
		Map<String, Long> preEliminatedTime = handleService.calcEliminatedTime(preAllApproveAndRejectData, preAllHandonAndAdjustTimeData);
		Map<String, Long> curEliminatedTime = handleService.calcEliminatedTime(curAllApproveAndRejectData, curAllHandonAndAdjustTimeData);
		long end1 = System.currentTimeMillis();
		Logger.error("sscpfa listSSCUserOprationData sql cost time is " + (end1 - begin) + "ms");
		List<SSCUserOprationReportVO> result = this.userOprationReportVOHandle(sscuserInfoList, preApproveAndRejectData,
				preHandonAndAdjustTimeData, preRejectedData, curApproveAndRejectData, curHandonAndAdjustTimeData,
				curRejectedData, preEliminatedTime, curEliminatedTime, timeUnit);
		long end2 = System.currentTimeMillis();
		Logger.error("sscpfa listSSCUserOprationData java cost time is " + (end2 - end1) + "ms");
		return result;
	}

	public String[] listLeafPkWorkGroups(String[] pkWorkGroups) throws BusinessException {
		return (new SSCWorkingGroupBP()).listLeafPkWorkGroups(pkWorkGroups);
	}

	private List<Map<String, String>> getSSCUserList(List<Map<String, String>> sscuserInfoList,
			List<Map<String, Object>> preApproveAndRejectData) throws BusinessException {
		List<String> sscuser_list = new LinkedList();
		List<String> user_list = new LinkedList();
		List<Map<String, String>> user_list_new = new LinkedList();
		List<Map<String, String>> user_list_new2 = new LinkedList();
		List<String> user_list_new3 = new LinkedList();
		String pk_sscuser = "";
		String pk_workinggroup = "";
		if (sscuserInfoList != null && sscuserInfoList.size() > 0) {
			Iterator var10 = sscuserInfoList.iterator();

			while (var10.hasNext()) {
				Map<String, String> map = (Map) var10.next();
				pk_sscuser = map.get("pk_user") == null ? pk_sscuser : (String) map.get("pk_user");
				sscuser_list.add(pk_sscuser);
			}
		}

		String pk_user = "";
		Map map_sscuser;
		Iterator var18;
		if (preApproveAndRejectData != null && preApproveAndRejectData.size() > 0) {
			var18 = preApproveAndRejectData.iterator();

			while (var18.hasNext()) {
				map_sscuser = (Map) var18.next();
				pk_user = map_sscuser.get("pk_user") == null ? pk_user : (String) map_sscuser.get("pk_user");
				user_list.add(pk_user);
				if (!sscuser_list.contains(pk_user)) {
					Map<String, String> map_new = new HashMap();
					pk_workinggroup = map_sscuser.get("pk_workinggroup") == null
							? ""
							: (String) map_sscuser.get("pk_workinggroup");
					map_new.put("pk_user", pk_user);
					map_new.put("pk_workinggroup", pk_workinggroup);
					user_list_new.add(map_new);
				}
			}
		}

		var18 = sscuserInfoList.iterator();

		while (var18.hasNext()) {
			map_sscuser = (Map) var18.next();
			Iterator var19 = user_list_new.iterator();

			while (var19.hasNext()) {
				Map<String, String> map_user = (Map) var19.next();
				if (((String) map_sscuser.get("pk_workinggroup")).equals(map_user.get("pk_workinggroup"))
						&& !user_list_new3.contains(
								(String) map_user.get("pk_workinggroup") + (String) map_user.get("pk_user"))) {
					user_list_new3.add((String) map_user.get("pk_workinggroup") + (String) map_user.get("pk_user"));
					UserVO[] uservo = ((IUserPubService) NCLocator.getInstance().lookup(IUserPubService.class))
							.getUsersByPKs(new String[]{(String) map_user.get("pk_user")});
					Map<String, String> map_new = new HashMap();
					map_new.put("pk_user", map_user.get("pk_user"));
					map_new.put("pk_user_name", uservo[0].getUser_name());
					map_new.put("pk_workinggroup", map_user.get("pk_workinggroup"));
					map_new.put("pk_workinggroup_code", (String)map_sscuser.get("pk_workinggroup_code"));
					map_new.put("pk_workinggroup_name", (String)map_sscuser.get("pk_workinggroup_name"));
					user_list_new2.add(map_new);
				}
			}
		}

		sscuserInfoList.addAll(user_list_new2);
		return sscuserInfoList;
	}
}