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

import com.eactive.eai.batch.job.jobConstant.TelegramKeys;
import com.eactive.eai.batch.job.jobItem.DefaultTelegram;
import com.eactive.eai.batch.job.jobItem.TelegramService;
import com.eactive.eai.batch.rule.nodeinfo.NodeInfoManager;
import com.eactive.eai.batch.rule.nodeinfo.NodeInfoVO;
import com.eactive.eai.common.exception.ExceptionUtil;

public class RECV_FIUSC extends DefaultTelegram implements TelegramService {

    /**
     * 생성자 : 금융정보분석원 길이 결정용 텔레그램 클래스 이다.
     */
    public RECV_FIUSC() {
    }

    public boolean doExecute() {
        return true;
    }

    //---------------- 특정 필드값을 구하는 부분
    // 전문종별코드 필드값 리턴
    public String getTelegramType()
    {
    	String telegramNo = super.getFieldValueString(5);
    	if ( telegramNo.length() < 8 ) return "";
		return telegramNo.substring(0,3) + telegramNo.substring(6,7);
    }
    // 거래구분코드 필드값 리턴
    public String getTrClass()
    {
		return super.getFieldValueString(6);
    }
    //응답코드 필드값 리턴
    public String getResCode()
    {
		return super.getFieldValueString(9);
    }

    public int getRemainDataLength()
    {
        try {
            int     idx;
            String  strLength;
            int llLength;
    
            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));
            llLength = ((byte[])this.al_Field.get(idx-1)).length;
            int     nDocLen = Integer.parseInt(strLength);
            int     nReceivedLen = 0;
            for (int i=1; i<this.al_FieldLength.length ; i++) {
                nReceivedLen = nReceivedLen + Integer.parseInt(this.al_FieldLength[i]);
            }
    
            return (nDocLen-llLength-nReceivedLen);
            
        } catch (Exception ex) {
            this.batchDoc.getBatchMsg().getBody().setErrorMsg(ex.toString());
            return -1;
        }
    }
}
