package com.eactive.eai.rms.bap.tansaction.transactionStatus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.rms.bap.common.base.BapBaseService;

@Service("bapTransactionStatusService")
public class BapTransactionStatusService extends BapBaseService {

	@Autowired
	@Qualifier("bapTransactionStatusDao")
	private BapTransactionStatusDao dao;

	public HashMap<String, Object> selectList(int startNum,int endNum, HashMap<String, Object> paramMap) throws Exception {
		 

		String activeTab = (String)paramMap.get("activeTab");
		ArrayList<String> tranPrcssDstcds = new ArrayList<String>();
		
		paramMap.put("startNum" , startNum);
		paramMap.put("endNum"   , endNum);

		HashMap<String, Object> map = new HashMap<String, Object>();
  
		if ( activeTab.equals("tabE")){
			tranPrcssDstcds.add("T");
			tranPrcssDstcds.add("R");
			paramMap.put("tranPrcssDstcds", tranPrcssDstcds);
			List<Map<String, Object>> listNomal = dao.findStatusList(paramMap);
			List<Map<String, Object>> listError = dao.findErrorList(paramMap);
			map.put("rows", processErrorList(listNomal, listError));
			map.put("totalCount", dao.findErrorListCount(paramMap));
		}else if ( activeTab.equals("tabF")){
			List<Map<String, Object>> listF = dao.findFutureList(paramMap);
			map.put("rows", listF);
			map.put("totalCount", dao.findFutureListCount(paramMap));
		}else if ( activeTab.equals("tabQ")){
			tranPrcssDstcds.add("Q");
			paramMap.put("tranPrcssDstcds", tranPrcssDstcds);
			List<Map<String, Object>> waitlist = dao.findStatusList(paramMap);
			map.put("rows", waitlist);
			map.put("totalCount", dao.findStatusListCount(paramMap));
		}else if ( activeTab.equals("tabS")){
			tranPrcssDstcds.add("S");
			paramMap.put("tranPrcssDstcds", tranPrcssDstcds);
			List<Map<String, Object>> sendlist = dao.findStatusList(paramMap);
			@SuppressWarnings("unchecked")
			List<Map<String, Serializable>> memList = (List<Map<String, Serializable>>)paramMap.get("procList");
			
			if(memList != null) {
				processMemList(sendlist, memList);
			}
			map.put("rows", sendlist);
			map.put("totalCount", dao.findStatusListCount(paramMap));
		}else if ( activeTab.equals("tabA")){
			List<Map<String, Object>> listA = dao.findSpecList(paramMap);
			map.put("rows", listA);
			map.put("totalCount", dao.findSpecListCount(paramMap));
		}

		return map;
	}	

	public HashMap<String, Object> selectDetailFile(int startNum, int endNum, HashMap<String, Object> param) throws Exception {

		HashMap<String, Object>   detail        = dao.selectDetail(param);

		int totalCount = dao.getFileLogCount(param);

		param.put("startNum" , startNum);// 시작 record
		param.put("endNum"   , endNum);// 끝 record

		List<Map<String, Object>> file          = dao.getFilelog(param);

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("detail", detail);
		map.put("file", file);
		map.put("totalCount", totalCount);

		return map;
	}

	public HashMap<String, Object> selectDetailStage(int startNum, int endNum, HashMap<String, Object> param) throws Exception {

		HashMap<String, Object>   detail        = dao.selectDetail(param);
		
		int totalCount = dao.getStageLogCount(param);

		param.put("startNum" , startNum);// 시작 record
		param.put("endNum"   , endNum);// 끝 record

		List<Map<String, Object>> stage         = dao.getStagelog(param);

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("detail", detail);
		map.put("stage", stage);
		map.put("totalCount", totalCount);

		return map;
	}

	
	public List<Map<String, Object>> selectOsidInstiDstcd(String bjobBzwkDstcd) throws Exception {

		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("bjobBzwkDstcd", bjobBzwkDstcd);

		return dao.selectOsidInstiDstcd(param);
	}
	private List<Map<String, Object>> processErrorList( List<Map<String, Object>> listNomal, List<Map<String, Object>> listError){

//		listError.stream()
//		.forEach(x -> System.out.println(x.get("BJOBMSGDSTCD") + ":" + x.get("BJOBMSGDSTCDSEQ")));
		
		List<Map<String, Object>> result = listError.stream()
			.filter(x -> listNomal.stream().noneMatch(n -> {
				return n.get("BJOBMSGDSTCD") != null 
					&& n.get("BJOBMSGDSTCD").equals(x.get("BJOBMSGDSTCD"))
					&& ! x.get("TRANPRCSSDSTCD").equals("Q") && ! x.get("TRANPRCSSDSTCD").equals("H") 
					&& Long.parseLong(n.get("TRANENDHMS").toString()) > Long.parseLong(x.get("TRANENDHMS").toString().equals("") ? "99999999999999999" : x.get("TRANENDHMS").toString());
			})).collect(Collectors.toList());

		return result;
	}
	
	private List<Map<String, Object>> processMemList( List<Map<String, Object>> dbList, List<Map<String, Serializable>> memList){
		for (Map<String, Object> map : dbList) {
			String bjobDmndMsgID    = (String)map.get("BJOBDMNDMSGID");
			String bjobDmndSubMsgID = (String)map.get("BJOBDMNDSUBMSGID");
			for (Map<String, Serializable> mem : memList) {
				String uuid    = (String)mem.get("Uuid");
				String subUuid = (String)mem.get("SubUuid");
				if ( bjobDmndMsgID.equals(uuid) && bjobDmndSubMsgID.equals(subUuid)){
					map.put( "RATE", mem.get("Rate"));
					map.put( "CURRENTFILESIZE", mem.get("CurrFileSize"));
					break;
				}
			}
		}
		return dbList;
	}

	public void updateStatusChange(HashMap<String, Object> paramMap) throws Exception {
		dao.updateStatusChange(paramMap);
	}

	public void updateStatusChangeMaster(HashMap<String, Object> paramMap) throws Exception {
		dao.updateStatusChangeMaster(paramMap);
	}
}