package com.eactive.eai.batch.job.jobItem.skt;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

import com.eactive.eai.batch.job.jobConstant.TelegramKeys;
import com.eactive.eai.batch.job.jobHandle.JobProcessData;
import com.eactive.eai.batch.job.jobItem.DefaultTelegram;
import com.eactive.eai.batch.job.jobItem.TelegramService;

import com.eactive.eai.common.exception.ExceptionUtil;

public class RECV_SKT01_2210 extends DefaultTelegram implements TelegramService
{

    /**
     * 생성자
     */
    public RECV_SKT01_2210() {
        //결번확인통보
    }

    /**
     * 전문 해체하기
     */
    public boolean doExecute() {
        if (!super.doExecute()) return false;

        try {

            String missing_field = new String ((byte[]) al_Field.get(12));

            int lastSeq = batchDoc.getBatchMsg().getBody().getSeqNum();
            int recv_migging_count = 0;
            for (int inx = 0; inx < lastSeq; inx++) {
				if ( (missing_field.getBytes())[inx] == '0' )
					recv_migging_count++;
			}
            

            if (recv_migging_count == 0 ) {
                String uuid = this.batchDoc.getBatchMsg().getHeader().getUUID();
                JobProcessData job = JobProcessData.getInstance();
                HashMap<String, Object> hm = job.getBatchStageData(uuid);
                hm.put(JobProcessData.PROCDATA_BLOCKDATA, null);    // Vector 초기화
                
                long fileSize = batchDoc.getBatchMsg().getHeader().getFileSize();
                long curSize = batchDoc.getBatchMsg().getBody().getCurFileSize();
                if ( curSize >= fileSize){
                    this.setConditionTxt(TelegramKeys.EOF);
                }
            }
            this.batchDoc.getBatchMsg().getBody().setMissingNmCount(recv_migging_count);
            this.batchDoc.getBatchMsg().getBody().setMissingNmField(missing_field);
        } catch ( Exception e) {
            String[] msgArgs = new String[1];
            msgArgs[0] = this.batchDoc.getBatchMsg().getHeader().getProcessCode();
            String errCode = "BECEAIFJI001";
            String errMsg = ExceptionUtil.getErrorCode (e, errCode, msgArgs);
            this.batchDoc.getBatchMsg().getBody().setErrorMsg(errMsg);
            logger.error( e.getMessage(), e);
            return false;
        }
        return true;
    }

    public boolean doPostExecute() {
        if (!super.doPostExecute()) return false;
        try {
			//DB로그에 남길 전문 헤더 설정
            ByteArrayOutputStream baout = new ByteArrayOutputStream();
            for (int i=0; i<11; i++) {
                baout.write((byte[])this.al_Field.get(i));
            }
            batchDoc.getBatchMsg().setTelegramHeader(new String(baout.toByteArray()));

            //DB로그에 남길 전문 바디 설정
            baout = new ByteArrayOutputStream();
            for (int i=11; i< al_Field.size(); i++) {
                baout.write((byte[])this.al_Field.get(i));
            }
            batchDoc.getBatchMsg().setTelegramBody(new String(baout.toByteArray()));

            return true;

        } catch(Exception ex) {
            String[] msgArgs = new String[1];
            msgArgs[0] = this.batchDoc.getBatchMsg().getHeader().getProcessCode();
            String errCode = "BECEAIFJI002";
            String errMsg = ExceptionUtil.getErrorCode (ex, errCode, msgArgs);
            this.batchDoc.getBatchMsg().getBody().setErrorMsg(errMsg);
            logger.error( ex.getMessage(), ex);
            return false;
        }
    }

    //---------------- 특정 필드값을 구하는 부분
    // 전문종별코드 필드값 리턴
    public String getTelegramType()
    {
		return super.getFieldValueString(3);
    }
    // 거래구분코드 필드값 리턴
//    public String getTrClass()
//    {
//		return super.getFieldValueString(5);
//    }
    //응답코드 필드값 리턴
    public String getResCode()
    {
		return super.getFieldValueString(8);
    }
    //업무관리코드 필드값 리턴
    public String getOpCode()
    {
		return super.getOpCode();
    }

}
