IMFSaveHandle.java 3.6 KB
package com.tianbo.warehouse.handle;

import com.tianbo.warehouse.model.T_ETL_MESSAGE;
import com.tianbo.warehouse.service.T_ETL_MESSAGEService;

import com.tianbo.warehouse.util.Date.DateUtil;
import com.tianbo.warehouse.util.IO.FileTool;
import com.tianbo.warehouse.util.XML.XMLXPath;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * 存储IMF过来的原始报文到数据仓库T_ELT_MESSAGE原始报文表中
 */
@Component
public class IMFSaveHandle {

    @Autowired
    protected T_ETL_MESSAGEService messageService;

    private static IMFSaveHandle saveHandle;

    public final static String SNDR = "//MSG/META/SNDR";

    public final static String RCVR = "//MSG/META/RCVR";

    public final static String DDTM = "//MSG/META/DDTM";

    public final static String TYPE = "//MSG/META/TYPE";

    public final static String STYP = "//MSG/META/STYP";

    public final static String SEQN = "//MSG/META/SEQN";

    protected static final Logger logger = Logger.getLogger(IMFSaveHandle.class);

    @PostConstruct
    public  void  init(){
        saveHandle = this;
        saveHandle.messageService = this.messageService;
    }

    public void handle(String xmlmessage){
        try {
            //有人发报文头部不按规范发
            xmlmessage = xmlmessage.replace("<Msg>","<MSG>")
                    .replace("<msg>","<MSG>")
                    .replace("</Msg>","</MSG>")
                    .replace("</msg>","</MSG>");
            Document document = DocumentHelper.parseText(xmlmessage);
            T_ETL_MESSAGE message = new T_ETL_MESSAGE();

            String sndrm = XMLXPath.getSingleValueByPath(document,SNDR);
            String typem = XMLXPath.getSingleValueByPath(document,TYPE);
            String stypm = XMLXPath.getSingleValueByPath(document,STYP);

            if ("TXD".equals(sndrm)){
                if("EDEP".equals(stypm)
                        || "EFOH".equals(stypm)
                        || "ERCF".equals(stypm)
                        || "PFFM".equals(stypm)
                        || "UDEP".equals(stypm)
                        || "UFOH".equals(stypm)
                        || "URCF".equals(stypm)
                        || "FFM".equals(stypm)
                        || "IFWB".equals(stypm)
                        || "IFHL".equals(stypm)){
                    //存储至备份目录
                    FileTool.writeFileToBak(xmlmessage);
                }
            }

            message.setSndr(sndrm);
            message.setRcvr(XMLXPath.getSingleValueByPath(document,RCVR));
            message.setType(typem);
            message.setStyp(stypm);
            message.setSeqn(XMLXPath.getSingleValueByPath(document,SEQN));



            String ddtm = XMLXPath.getSingleValueByPath(document,DDTM);
            Date ddtmDate = DateUtil.formatByyyyyMMddHHmmss(ddtm);
            message.setDdtm(ddtmDate);
            message.setSntm(ddtmDate);
            message.setOper("ALL");
            message.setAppid("W");
            message.setEtltim(new Date());



            message.setContent(xmlmessage);

            int i = saveHandle.messageService.insertSelective(message);
        }catch (Exception e){
            FileTool.writeFile("err",e.toString()+"\n"+xmlmessage,false);
            logger.warn("*报文入库失败已存储成备份文件*");
            logger.error(e);
            logger.warn(e);
        }



    }
}