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

import com.eactive.eai.batch.rule.nodeinfo.NodeInfoManager;
import com.eactive.eai.batch.rule.nodeinfo.NodeInfoVO;

import com.eactive.eai.batch.job.jobItem.DefaultTelegram;
import com.eactive.eai.batch.job.jobItem.TelegramService;

public class RECV_T_KFB extends DefaultTelegram implements TelegramService {

    /**
     * 생성자 : 은행연합회 길이 결정용 텔레그램 클래스 이다.
     */
    public RECV_T_KFB() {
    }

    public boolean doExecute() {
        return true;
    }

    //---------------- 특정 필드값을 구하는 부분
    // 전문종별코드 필드값 리턴
    public String getTelegramType()
    {
        return super.getFieldValueString(4);
    }
    // 거래구분코드 필드값 리턴
    public String getTrClass()
    {
        return super.getFieldValueString(10);
    }
    //응답코드 필드값 리턴
    public String getResCode()
    {
        return super.getFieldValueString(7);
    }
    //업무관리코드 필드값 리턴
    public String getOpCode()
    {
        if ( super.getFieldValueString(4).equalsIgnoreCase("0600") ||super.getFieldValueString(4).equalsIgnoreCase("0610") ){
            return super.getFieldValueString(12);
        }
        else {
            return super.getOpCode();
        }
    }

    public int getRemainDataLength()
    {
        try {
            int     idx;
            String  strLength;
    
            String  strRuleCode   = batchDoc.getBatchMsg().getHeader().getRuleCode();
            String  strPhaseCode  = batchDoc.getBatchMsg().getBody().getFlowPhaseCode();
            NodeInfoVO  vo = NodeInfoManager.getInstance().getNodeInfo(strRuleCode, strPhaseCode);
    
            idx = vo.getLengthField();
            if (idx == 0) return -1;
    
            strLength = new String((byte[])this.al_Field.get(idx-1));
            int     nDocLen = Integer.parseInt(strLength);
            int     nReceivedLen = 0;
            for (int i=2; i<this.al_FieldLength.length ; i++) {
                nReceivedLen = nReceivedLen + Integer.parseInt(this.al_FieldLength[i]);
            }
    
            return (nDocLen-nReceivedLen);
            
        } catch (Exception ex) {
            this.batchDoc.getBatchMsg().getBody().setErrorMsg(ex.toString());
            return -1;
        }
    }
}
