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

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

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("bapTransactionWaitService")
public class BapTransactionWaitService extends BapBaseService {

	@Autowired
	@Qualifier("bapTransactionWaitDao")
	private BapTransactionWaitDao dao;

	public HashMap<String, Object> selectList(int startNum, int endNum, HashMap<String, Object> paramMap) throws Exception {
		// 전체 목록수 얻기
		int totalCount = dao.selectListCount(paramMap);

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

		List<Map<String, Object>> list = dao.selectList(paramMap);

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("totalCount", totalCount);
		map.put("list", list);

		return map;
	}

	public HashMap<String, Object> selectDetail(HashMap<String, Object> param) throws Exception {

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

		String sndrcvStartHMS = (String)detail.get("SNDRCVSTARTHMS");
		String sndrcvEndHMS = (String)detail.get("SNDRCVENDHMS");
		String bjobDmndMsgCretnHMS = (String)detail.get("BJOBDMNDMSGCRETNHMS");

		detail.put("SNDRCVSTARTYMD", sndrcvStartHMS.substring(0, 8));
		detail.put("SNDRCVSTARTHMS", Integer.parseInt(sndrcvStartHMS.substring(8, 14)) > 240000 ? "235959" : sndrcvStartHMS.substring(8, 14));
		detail.put("SNDRCVENDYMD", sndrcvEndHMS.substring(0, 8));
		detail.put("SNDRCVENDHMS", Integer.parseInt(sndrcvEndHMS.substring(8, 14)) > 240000 ? "235959" : sndrcvEndHMS.substring(8, 14));
		detail.put("BJOBDMNDMSGCRETNYMD", bjobDmndMsgCretnHMS.substring(0, 8));
		detail.put("BJOBDMNDMSGCRETNHMS", Integer.parseInt(bjobDmndMsgCretnHMS.substring(8, 14)) > 240000 ? "235959" : bjobDmndMsgCretnHMS.substring(8, 14));

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("detail", detail);

		return map;
	}

	public void update(HashMap paramMap) throws Exception {
		dao.updateBS03(paramMap);
	}

	public void delete(HashMap paramMap) throws Exception {
		dao.updateBJ03(paramMap);
		//dao.updateBJ08(paramMap);
		dao.delete(paramMap);
	}
	
	public void deleteBs03(HashMap<String, Object>[] paramList) throws Exception {

		HashMap<String, Object> map = new HashMap<String, Object>();
		for (HashMap<String, Object> paramMap : paramList) {
			map.put("bjobDmndMsgID", paramMap.get("BJOBDMNDMSGID"));
			map.put("bjobDmndSubMsgID", paramMap.get("BJOBDMNDSUBMSGID"));
			dao.delete(map);
		}

	}
}