package com.eactive.eai.rms.bap.manage.mmi;

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

import org.apache.log4j.Logger;
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.manage.mmi.MMIBAPSyncController.BapDeploy;
import com.eactive.eai.rms.common.base.BaseService;

@Service("mmiBapSyncService")
public class MMIBAPSyncService extends BaseService {

	@Autowired
	@Qualifier("mmiBapSyncDao")
	private MMIBAPSyncDao dao;

   private static final Logger logger = Logger.getLogger(MMIBAPSyncService.class);

	//일괄전송 BJ01 및 본 테이블 반영
	public void mergeData(BapDeploy bapDeploy) throws Exception {

      String bj01pkgen = "";
		//TODO hd 스케줄러 등록 고려
      String bs01pkgen = "";
      logger.info( "BF BJ01 I/U" + bapDeploy);
		// TSEAIBJ01 데이터 처리
		int selCountBJ01 = dao.selectCountBJ01(bapDeploy);

		//일괄전송 고유 업무 코드 구하기.
		String bzwkDstCd = "";
		try {
			bzwkDstCd = dao.selectBzwkDstCd(bapDeploy);
		} catch(Exception e) {
			e.printStackTrace();
		}
		logger.info("bzwkDstCd : "+ bzwkDstCd);
		bapDeploy.setBzwkDstCd(bzwkDstCd);
      String sndst = bapDeploy.getSnDst();
		String fileSendUseYN = "0";
		String fileRecvUseYN = "0";
		if(sndst.equals("S")) { //송신
			fileSendUseYN = "1";
		} else if(sndst.equals("R")) { // 수신
			fileRecvUseYN = "1";
		}  else if(sndst.equals("A")) { // 송수신
			fileSendUseYN = "1";
			fileRecvUseYN = "1";
		} else {  // 잘못된 코드
			throw new Exception("잘못된 코드입니다");
		}

		bapDeploy.setFileSendUseYN(fileSendUseYN);
		bapDeploy.setfileRecvUseYN(fileRecvUseYN);
		logger.info(">>>>>>>>>>>bapDeploy : "+ bapDeploy.toString());
		if (selCountBJ01 == 0) {
         logger.info("bapDeploy : "+ bapDeploy.toString());
         bj01pkgen = dao.generatePKBJ01(bapDeploy);
         logger.info("bj01pkgen : "+ bj01pkgen);
         bapDeploy.setBjobMsgDstcd(bj01pkgen);
			dao.insertBJ01(bapDeploy);
			logger.info("insert BAP interface into TSEAIBJ01 : "+ bapDeploy);
		} else {
			bj01pkgen = dao.selectPKBJ01(bapDeploy);
         logger.info("else bj01pkgen : "+ bj01pkgen);
			bapDeploy.setBjobMsgDstcd(bj01pkgen);
			dao.updateBJ01(bapDeploy);
			logger.info("update BAP interface of TSEAIBJ01 : "+ bapDeploy);
		}

	}

	public void delete(HashMap<String, Object> paramMap) throws Exception {

      //int selCountBJ01 = dao.selectCountBJ01(paramMap);

//		if (selCountBJ01 > 0) {
//         //paramMap.put( MMIBAPSyncField.bjobMsgDstcd.name(), dao.selectPKBJ01(paramMap));
//         dao.deleteBJ01(paramMap);
//			logger.info("delete BAP interface in TSEAIBJ01 : "+ paramMap);
//		} else {
//			logger.error("Fail BAP interface to delete in TSEAIBJ01 : "+ paramMap);
//			return;
//		}

		int selCountBS01 = dao.selectCountBS01(paramMap);

		if (selCountBS01 > 0) {
			paramMap.put( MMIBAPSyncField.bjobMsgScheId.name(), dao.selectPKBS01(paramMap));
			dao.deleteBS01(paramMap);
			logger.info("delete BAP interface in TSEAIBS01 : "+ paramMap);
		} else {
			logger.error("Fail BAP interface to delete in TSEAIBS01 : "+ paramMap);
			return;
		}
	}

   public String checkFieldLength(HashMap<String,Object> paramap) throws Exception
	{
		String result = null;
      for (MMIBAPSyncField field : MMIBAPSyncField.values())
		{
			if (!field.getFieldType().toUpperCase().equals("STRING") || !field.getFieldUseYn()) continue;
			if (paramap.get(field.name()) == null) {
				result = "column validation fail - [" + field.name() + "] column is not exist";
				break;
			}
			if (paramap.get(field.name()).toString().length() > field.getFieldLength()) {
				result = "column validation fail - [" + field.name() + "] column length can't over than "+field.getFieldLength();
				break;
			}
		}
		return result;
	}

   public List<Map<String, Object>> selectSysGbCd( String sysGbName ) throws Exception{
      HashMap<String, Object> paramMap = new HashMap<String, Object>();
      paramMap.put( "sysGbName", sysGbName);
      logger.info("sysGbName --->" + sysGbName);
      return dao.selectSysGbCd(paramMap);
   }

   public String getNdmNodeBySysId( String systemId ) throws Exception{
      HashMap<String, Object> paramMap = new HashMap<String, Object>();
      paramMap.put( "systemId", systemId);
      logger.info("systemId=--->" + systemId);
      return dao.getNdmNodeBySysId(paramMap);
   }

   public void saveBapDeploy(BapDeploy bapDeploy) throws Exception {
      logger.info("saveBapDeploy=--->" + bapDeploy);
      dao.insertBapDeploy(bapDeploy);
   }

}