package com.brilliance.isc.swift.fremsg.document;

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.Ecm;
import com.brilliance.isc.bo.Spt;
import com.brilliance.isc.bo.model.Bcdgrp;
import com.brilliance.isc.bo.model.Bodgrp;
import com.brilliance.isc.bo.model.Fremsg;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.doc.TextResultVo;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.transaction.vo.BaseTransactionVo;
import com.brilliance.isc.common.trndoc.service.TrndocService;
import com.brilliance.isc.common.trndoc.vo.DoceotVo;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.FremsgVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.swift.objmtconvert.manager.MtCreatorManager;
import com.brilliance.isc.swift.objmtconvert.model.commontag.ActnoBicAdrTagXXX;
import com.brilliance.isc.swift.objmtconvert.model.commontag.DcDateCurAmtTagXXX;
import com.brilliance.isc.swift.objmtconvert.model.mt1xx.*;
import com.brilliance.isc.swift.objmtconvert.model.mt2xx.MT292Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt2xx.MT295Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt2xx.MT296Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt2xx.MT299Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt4xx.MT410Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt4xx.MT495Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt4xx.MT496Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt4xx.MT499Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt4xx.mt4XXAbsCreator.MT410AbsCreator;
import com.brilliance.isc.swift.objmtconvert.model.mt7xx.*;
import com.brilliance.isc.swift.objmtconvert.model.mt9xx.MT992Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt9xx.MT995Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt9xx.MT996Creator;
import com.brilliance.isc.swift.objmtconvert.model.mt9xx.MT999Creator;
import com.brilliance.isc.swift.objmtconvert.result.MtResult;
import com.brilliance.isc.swift.swtmsg.SwiftMessageComponent;
import com.brilliance.isc.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

@Component
public class FreMessageDoc {
    public static final String CR = "\n";
    private static final String TEXT = "text";
    @Autowired
    private SwiftMessageComponent swiftMessageComponent;
    @Autowired
    private TrndocService trndocService;
    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;
    @Autowired
    TransactionHelpService transactionHelpService;
    @Autowired
    PtyCacheService ptyCacheService;

    public MtResult buildMt195(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT195Creator mt195Creator = new MT195Creator();
        //收报行bic
        mt195Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt195Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt195Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt195Creator.setRelatedReference(ref);
        }else {
            mt195Creator.setRelatedReference("NONREF");
        }
        //75
        mt195Creator.setQueries(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        String tag77atxt = fremsg.getTag77a();
        mt195Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));
        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt195Creator.setMtDateTag11a(tag11);
        } else {
            mt195Creator.setMtAndDateoftheOriginalMessage(tag11);
        }
        //79
        mt195Creator.setNarrativeDescription(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt195Creator);
    }

    public MtResult buildMt295(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT295Creator mt295Creator = new MT295Creator();
        //收报行bic
        mt295Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt295Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt295Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt295Creator.setRelatedReference(ref);
        }else {
            mt295Creator.setRelatedReference("NONREF");
        }
        //75
        mt295Creator.setQueries(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        String tag77atxt = fremsg.getTag77a();
        mt295Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));
        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt295Creator.setMtandDate(tag11);
        } else {
            mt295Creator.setMtAndDateoftheOriginalMessage(tag11);
        }
        //79
        mt295Creator.setNarrativeDescriptionofQueryRelates(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt295Creator);
    }

    public MtResult buildMt495(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT495Creator mt495Creator = new MT495Creator();
        //收报行bic
        mt495Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt495Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt495Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt495Creator.setRelatedReference(ref);
        }else {
            mt495Creator.setRelatedReference("NONREF");
        }
        //75
        mt495Creator.setQueries(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        String tag77atxt = fremsg.getTag77a();
        mt495Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));
        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt495Creator.setMtandDate(tag11);
        } else {
            mt495Creator.setMtAndDateoftheOriginalMessage(tag11);
        }
        //79
        mt495Creator.setNarrativeDescriptionofQueryRelates(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt495Creator);
    }
    public MtResult buildMt795(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT795Creator mt795Creator = new MT795Creator();
        //收报行bic
        mt795Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt795Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt795Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt795Creator.setRelatedReference(ref);
        }else {
            mt795Creator.setRelatedReference("NONREF");
        }
        //75
        mt795Creator.setQueries(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        String tag77atxt = fremsg.getTag77a();
        mt795Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));
        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt795Creator.setMtDateTag11a(tag11);
        } else {
            mt795Creator.setMtAndDateoftheOriginalMessage(tag11);
        }
        //79
        mt795Creator.setNarrativeDescription(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt795Creator);
    }

    public MtResult buildMt995(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT995Creator mt995Creator = new MT995Creator();
        //收报行bic
        mt995Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt995Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt995Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt995Creator.setRelatedReference(ref);
        }else {
            mt995Creator.setRelatedReference("NONREF");
        }
        //75
        mt995Creator.setQueries(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        //77a
        String tag77atxt = fremsg.getTag77a();
        mt995Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));
        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt995Creator.setMtDateTag11a(tag11);
        } else {
            mt995Creator.setMtAndDateoftheOriginalMessage(tag11);
        }
        //79
        mt995Creator.setNarrativeDescription(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt995Creator);
    }

    public MtResult buildMt196(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT196Creator mt196Creator = new MT196Creator();
        //收报行bic
        mt196Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt196Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt196Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt196Creator.setRelatedReference(ref);
        }else {
            mt196Creator.setRelatedReference("NONREF");
        }
        //76
        mt196Creator.setAnswers(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        //77a
        String tag77atxt = fremsg.getTag77a();
        mt196Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));
        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt196Creator.setMtDateTag11a(tag11);
        } else {
            mt196Creator.setMtAndDateoftheOriginalMessage(tag11);
        }
        //79
        mt196Creator.setNarrativeDescriptionOriginal(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt196Creator);
    }

    public MtResult buildMt296(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT296Creator mt296Creator = new MT296Creator();
        //收报行bic
        mt296Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt296Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt296Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt296Creator.setRelatedReference(ref);
        }else {
            mt296Creator.setRelatedReference("NONREF");
        }
        //76
        mt296Creator.setAnswers(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        //77a
        String tag77atxt = fremsg.getTag77a();
        mt296Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));
        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt296Creator.setMtDateTag11a(tag11);
        } else {
            mt296Creator.setMTandDateoftheOriginalMessage(tag11);
        }
        //79
        mt296Creator.setNarrativeDescriptionoftheOriginalMessagetoWhichtheAnswerRelates(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt296Creator);
    }


    public MtResult buildMt496(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT496Creator mt496Creator = new MT496Creator();
        //收报行bic
        mt496Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt496Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt496Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt496Creator.setRelatedReference(ref);
        }else {
            mt496Creator.setRelatedReference("NONREF");
        }
        //76
        mt496Creator.setAnswers(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        //77a
        String tag77atxt = fremsg.getTag77a();
        mt496Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));
        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), CR, Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt496Creator.setMtDateTag11a(tag11);
        } else {
            mt496Creator.setMtAndDateoftheOriginalMessage(tag11);
        }
        //79
        mt496Creator.setNarrativeDescriptionOriginal(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt496Creator);
    }

    public MtResult buildMt796(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT796Creator mt796Creator = new MT796Creator();
        //收报行bic
        mt796Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt796Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt796Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt796Creator.setRelatedReference(ref);
        }else {
            mt796Creator.setRelatedReference("NONREF");
        }
        //76
        mt796Creator.setAnswers(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        //77a
        String tag77atxt = fremsg.getTag77a();
        mt796Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));
        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt796Creator.setMtDateTag11a(tag11);
        } else {
            mt796Creator.setMTandDateoftheOriginalMessage(tag11);
        }
        //79
        mt796Creator.setNarrativeDescriptionOriginal(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt796Creator);
    }

    public MtResult buildMt996(PtsptaVo rolPtspta, String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT996Creator mt996Creator = new MT996Creator();
        //收报行bic
        mt996Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt996Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt996Creator.setTransactionReferenceNumber(ownref);
        //21
        if (rolPtspta != null){
            String ref = StringUtils.isEmpty(rolPtspta.getRef()) ? "NONREF" : rolPtspta.getRef();
            mt996Creator.setRelatedReference(ref);
        }else {
            mt996Creator.setRelatedReference("NONREF");
        }
        //76
        mt996Creator.setAnswers(StringUtils.convertBlock(fremsg.getDet(), 6, 35));
        //77a
        String tag77atxt = fremsg.getTag77a();
        mt996Creator.setNarrative(StringUtils.convertBlock(tag77atxt, 20, 35));

        //11a
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        if (fremsg.getReldir().equals("S")) {
            mt996Creator.setMtDateTag11a(tag11);
        } else {
            mt996Creator.setMtAndDateoftheOriginalMessage(tag11);
        }
        //79
        mt996Creator.setNarrativeDescription(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        return MtCreatorManager.packMT(mt996Creator);
    }

    /**
     * source:fremsg.0045.script
     */
    public MtResult swtPrtMT759(DoceotVo doceotVo, String ownref, FremsgVo fremsg) {

        MT759Creator mt759Creator = new MT759Creator();
        //收报行bic
        mt759Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt759Creator.setSendBic(swiftMessageComponent.getSendBic());
        //:27:
        mt759Creator.setSequenceofTotal("1/1");
        //：20：
        if (StringUtils.isNotEmpty(fremsg.getTag20ref())) {
            mt759Creator.setTransactionReferenceNumber(fremsg.getTag20ref());
        } else {
            mt759Creator.setTransactionReferenceNumber(ownref);
        }
        //:21:
        mt759Creator.setRelatedReferenceNumber(swtPrtReference(fremsg,doceotVo.getRole()));
        //:22D:
        mt759Creator.setFormofUndertaking(fremsg.getTag22d());
        //:23:
        mt759Creator.setUndertakingNumber(fremsg.getTag23());
        //52
        if (ptsptaToolComponent.ptsIsRolSet(fremsg.getIss())) {
            ptsptaToolComponent.fillPtsptaAllProperties(fremsg.getIss());
        }
        mt759Creator.setIssuer(swiftMessageComponent.swtPrtAccAdrAD("52", fremsg.getIss()));
        //23H
        mt759Creator.setFunctionofMessage(fremsg.getTag23h());
        //45D
        String tag77utxt999x = "";
        if (fremsg.getSndmt().equals("59")){
            if(!Objects.isNull(fremsg.getTag45d())){
                tag77utxt999x = StringUtils.convertHtmlToText(fremsg.getTag45d());
            }
        }else {
            fremsg.setTag79txt999(fremsg.getPrttitle() + CR + fremsg.getStrinf() + CR + doceotVo.getAddstr());
            tag77utxt999x = fremsg.getTag79txt999();
        }
        mt759Creator.setNarrative(StringUtils.convertBlock(tag77utxt999x, 1200, 65));
        //23X  \TRNMOD\TRNDOC.GetScore23X
        //mt759Creator.setFileIdentification(trndocService.getScore23X(doceotVo));
        return MtCreatorManager.packMT(mt759Creator);
    }

    public MtResult buildMt199(String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT199Creator mt199Creator = new MT199Creator("199");
        //收报行bic
        mt199Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt199Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        if (StringUtils.isNotEmpty(fremsg.getTag20ref())) {
            mt199Creator.setTransactionReferenceNumber(fremsg.getTag20ref());
        } else {
            mt199Creator.setTransactionReferenceNumber(ownref);
        }
        //21
        mt199Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //79
//        fremsg.setTag79txt999(fremsg.getPrttitle() + CR + fremsg.getStrinf() + CR + doceotVo.getAddstr());
        mt199Creator.setNarrative(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        if(MdaUtils.equals(fremsg.getSndmt(),"59")){

        }
        return MtCreatorManager.packMT(mt199Creator);
    }

    public MtResult buildMt299(String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT299Creator mt299Creator = new MT299Creator("299");
        //收报行bic
        mt299Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt299Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        if (StringUtils.isNotEmpty(fremsg.getTag20ref())) {
            mt299Creator.setTransactionReferenceNumber(fremsg.getTag20ref());
        } else {
            mt299Creator.setTransactionReferenceNumber(ownref);
        }
        mt299Creator.setTransactionReferenceNumber(ownref);
        //21
        mt299Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //79
//        fremsg.setTag79txt999(fremsg.getPrttitle() + CR + fremsg.getStrinf() + CR + doceotVo.getAddstr());
        mt299Creator.setNarrative(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));

        return MtCreatorManager.packMT(mt299Creator);
    }

    public MtResult buildMt499(String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT499Creator mt499Creator = new MT499Creator("499");
        //收报行bic
        mt499Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt499Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        if (StringUtils.isNotEmpty(fremsg.getTag20ref())) {
            mt499Creator.setTransactionReferenceNumber(fremsg.getTag20ref());
        } else {
            mt499Creator.setTransactionReferenceNumber(ownref);
        }
        mt499Creator.setTransactionReferenceNumber(ownref);
        //21
        mt499Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //79
        List<String> arrList = new ArrayList<>();
        arrList.add(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));
        mt499Creator.setNarrative(arrList);

        return MtCreatorManager.packMT(mt499Creator);
    }
    public MtResult buildMt799(String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT799Creator mt799Creator = new MT799Creator("799");
        //收报行bic
        mt799Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt799Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        if (StringUtils.isNotEmpty(fremsg.getTag20ref())) {
            mt799Creator.setTransactionReferenceNumber(fremsg.getTag20ref());
        } else {
            mt799Creator.setTransactionReferenceNumber(ownref);
        }
        //21
        mt799Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //79 35*50x 循环
//        fremsg.setTag79txt999(fremsg.getPrttitle() + CR + ("30".equals(fremsg.getSndmt()) ? doceotVo.getAddstr() : fremsg.getStrinf() + CR + doceotVo.getAddstr()));
        List<String> list79 = new LinkedList<>();
        //先截取999行文本
        String tag79txt999 = StringUtils.convertBlock(fremsg.getTag79txt999(), 999, 50);
        //切割行
        String[] lines = tag79txt999.split("\\r?\\n");
        //每35行放一个79
        int cnt = 0 ;
        StringJoiner sj = new StringJoiner("\r\n");
        for (int i = 0; i < lines.length; i++) {
            cnt++;
            sj.add(lines[i]);
            //每35行或者到末尾了，产生一个新的799
            if(cnt == 35 || i == lines.length - 1){
                list79.add(sj.toString());
                cnt = 0;
                sj = new StringJoiner("\r\n");
            }
        }

        mt799Creator.setNarrative(list79);

        return MtCreatorManager.packMT(mt799Creator);
    }
    public MtResult buildMt799c(String ownref, FremsgVo fremsg, DoceotVo doceotVo, Spt spt) {
        MT799Creator mt799Creator = new MT799Creator("799");
        //收报行bic
        mt799Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt799Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        if (StringUtils.isNotEmpty(fremsg.getTag20ref())) {
            mt799Creator.setTransactionReferenceNumber(fremsg.getTag20ref());
        } else {
            mt799Creator.setTransactionReferenceNumber(ownref);
        }
        //21
        mt799Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //79 35*50x 循环
        String copyCc = "This message is a copy of original message sent to " + fremsg.getOthmsg().getPts().getExtkey();
        String fretxt = trndocService.getFretxt(doceotVo,fremsg,spt);
        fremsg.setTag79txt999(copyCc + CR + fretxt + CR + fremsg.getPrttitle() + CR  + ("30".equals(fremsg.getSndmt()) ? doceotVo.getAddstr() : fremsg.getStrinf() + CR + doceotVo.getAddstr()));
        List<String> list79 = new LinkedList<>();
        //先截取999行文本
        String tag79txt999 = StringUtils.convertBlock(fremsg.getTag79txt999(), 999, 50);
        //切割行
        String[] lines = tag79txt999.split("\\r?\\n");
        //每35行放一个79
        int cnt = 0 ;
        StringJoiner sj = new StringJoiner("\r\n");
        for (int i = 0; i < lines.length; i++) {
            cnt++;
            sj.add(lines[i]);
            //每35行或者到末尾了，产生一个新的799
            if(cnt == 35 || i == lines.length - 1){
                list79.add(sj.toString());
                cnt = 0;
                sj = new StringJoiner("\r\n");
            }
        }

        mt799Creator.setNarrative(list79);

        return MtCreatorManager.packMT(mt799Creator);
    }
    public MtResult buildMt730(String ownref, FremsgVo fremsg, DoceotVo doceotVo){
        MT730Creator mt730Creator = new MT730Creator("730");
        //收报行bic
        mt730Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt730Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt730Creator.setSenderReference(ownref);
        //21
        mt730Creator.setReceiverReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //30
        mt730Creator.setDateofMessageBeingAcknowledged(Utils.fmtDateSwift(fremsg.getReldat()));
        ActnoBicAdrTagXXX actnoBicAdrTagXXX = new ActnoBicAdrTagXXX("72","730");
        actnoBicAdrTagXXX.setNarrative(doceotVo.getAddstr());
        //72z
        mt730Creator.setSendertoReceiverInformation(actnoBicAdrTagXXX);
        //79z
//        mt730Creator.setNarrative(fremsg.getStrinf());

        return MtCreatorManager.packMT(mt730Creator);

    }

    public <T> MtResult buildMt410(String ownref, FremsgVo fremsg, DoceotVo doceotVo,T transactionVo,String objTyp){
        String  doccur ="" ;
        BigDecimal docamt = null ;
        Date matdat = null;
        int matpercnt = 0;
        String  matperbeg = "";
        String matpertyp = "";
        String doctypcod = "";
        if(MdaUtils.equals(objTyp,"BC")){
            Bcdgrp bcdgrp =   Utils.getFieldValue(transactionVo,"bcdgrp");
            doccur = bcdgrp.getCbs().getMax().getCur();
            docamt = bcdgrp.getCbs().getMax().getAmt();
            matdat = bcdgrp.getRec().getMatdat();
            matpercnt = bcdgrp.getRec().getMatpercnt();
            matperbeg = bcdgrp.getRec().getMatperbeg();
            matpertyp = bcdgrp.getRec().getMatpertyp();
            doctypcod =bcdgrp.getRec().getDoctypcod();
        }else if (MdaUtils.equals(objTyp,"BO")){
            Bodgrp bodgrp =   Utils.getFieldValue(transactionVo,"bodgrp");
            doccur = bodgrp.getCbs().getMax().getCur();
            docamt = bodgrp.getCbs().getMax().getAmt();
            matdat = bodgrp.getRec().getMatdat();
            matpercnt = bodgrp.getRec().getMatpercnt();
            matperbeg = bodgrp.getRec().getMatperbeg();
            matpertyp = bodgrp.getRec().getMatpertyp();
            doctypcod = bodgrp.getRec().getDoctypcod();
        }
        MT410AbsCreator mt410Creator = null;
        try {
            mt410Creator = new MT410AbsCreator();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //20
        mt410Creator.setSendingBankTRN(ownref);
        //21
        mt410Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //32
        mt410Creator.setAmountAcknowledged(swiftMessageComponent.swtPrtTagAmountABK(
                "32", "ABK",doccur,docamt,matdat,matpercnt,matperbeg,matpertyp,doctypcod));
        List<MT410AbsCreator> mt410AbsCreators = new ArrayList<>();
        mt410AbsCreators.add(mt410Creator);
        MT410Creator mt410Creators = new MT410Creator("410");
        //收报行bic
        mt410Creators.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt410Creators.setSendBic(swiftMessageComponent.getSendBic());
        mt410Creators.setMt410AbsCreators(mt410AbsCreators);
        //72
        if(!MdaUtils.isEmpty(doceotVo.getAddstr())){
            mt410Creators.setSenderToReceiverInformation(doceotVo.getAddstr());
        }
        return MtCreatorManager.packMT(mt410Creators);

    }
    public MtResult buildMt192(String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT192Creator mt192Creator = new MT192Creator("192");
        //收报行bic
        mt192Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt192Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt192Creator.setTransactionReferenceNumber(ownref);
        //21
        mt192Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //11s
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        mt192Creator.setMtandDateoftheOriginalMessage(tag11);
        //79
        mt192Creator.setNarrativeDescriptionoftheOriginalMessage(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));

        return MtCreatorManager.packMT(mt192Creator);
    }

    public MtResult buildMt292(String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT292Creator mt292Creator = new MT292Creator("292");
        //收报行bic
        mt292Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt292Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20 16x
        mt292Creator.setTransactionReferenceNumber(ownref);
        //21 16x
        mt292Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //11S
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        mt292Creator.setMTandDateoftheOriginalMessage(tag11);
        //79 35*50x
        mt292Creator.setNarrativeDescriptionoftheOriginalMessage(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));

        return MtCreatorManager.packMT(mt292Creator);
    }

    public MtResult buildMt792(String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT792Creator mt792Creator = new MT792Creator("792");
        //收报行bic
        mt792Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt792Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt792Creator.setTransactionReferenceNumber(ownref);
        //21
        mt792Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //11s
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        mt792Creator.setMtandDateoftheOriginalMessage(tag11);
        //79
        mt792Creator.setNarrativeDescriptionoftheOriginalMessage(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));

        return MtCreatorManager.packMT(mt792Creator);
    }

    public MtResult buildMt992(String ownref, FremsgVo fremsg, DoceotVo doceotVo) {
        MT992Creator mt992Creator = new MT992Creator("992");
        //收报行bic
        mt992Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt992Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt992Creator.setTransactionReferenceNumber(ownref);
        //21
        mt992Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //11s
        String tag11 = trndocService.xmlGetTxtSepTxt(fremsg.getRelmt(), "\n", Utils.fmtDateSwift(fremsg.getReldat()));
        mt992Creator.setMtandDateoftheOriginalMessage(tag11);
        //79
        mt992Creator.setNarrativeDescriptionoftheOriginalMessage(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));

        return MtCreatorManager.packMT(mt992Creator);
    }

    public MtResult buildMt999(String ownref, FremsgVo fremsg, DoceotVo doceotVo){
        MT999Creator mt999Creator = new MT999Creator("999");
        //收报行bic
        mt999Creator.setRecvBic(doceotVo.getAdrbic());
        //发报行bic
        mt999Creator.setSendBic(swiftMessageComponent.getSendBic());
        //20
        mt999Creator.setTransactionReferenceNumber(ownref);
        //21
        mt999Creator.setRelatedReference(swtPrtReference(fremsg,doceotVo.getRole()));
        //79
        mt999Creator.setNarrative(StringUtils.convertBlock(fremsg.getTag79txt999(), 35, 50));

        return MtCreatorManager.packMT(mt999Creator);
    }
    public String swtPrtReference(FremsgVo fremsg,String argRol) {
        String ref = "";
       if (argRol.toUpperCase().equals("OTHMSG")) {
            ref = fremsg.getOthmsg().getPts().getRef();
        } else if (argRol.toUpperCase().equals("OTHCC")) {
            ref = fremsg.getOthcc().getPts().getRef();
        }
        return StringUtils.isEmpty(ref) ? "NONREF" : ref;
    }

    public TextResultVo buildMt103(Ecm ecm){
        MT103Creator mt103Creator;
        try {
            mt103Creator = new MT103Creator("103");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        mt103Creator.setSendBic(ptyCacheService.getOwnPty().getExtkey());
        mt103Creator.setRecvBic("");
        //20
        mt103Creator.setSendersReference(ecm.getShlsh());
        //23B
        mt103Creator.setBankOperationCode("CRED");
        //32A
        DcDateCurAmtTagXXX dcDateCurAmtTagXXX = new DcDateCurAmtTagXXX("32");
        dcDateCurAmtTagXXX.setCurrency(ecm.getJiyibz());
        dcDateCurAmtTagXXX.setAmount(ecm.getQisuje());
        mt103Creator.setValueDateCurrencyInterbankSettledAmount(dcDateCurAmtTagXXX);
        //50K
        ActnoBicAdrTagXXX actnoBicAdrTagXXX50 = new ActnoBicAdrTagXXX("50");
        actnoBicAdrTagXXX50.setAddress(ecm.getHkrzhh() + CR + ecm.getBranch() + CR + ecm.getEcifno() + CR + ecm.getYiwmch() + CR + ecm.getYiwdzh());
        mt103Creator.setOrderingCustomer(actnoBicAdrTagXXX50);
        //52A
        ActnoBicAdrTagXXX actnoBicAdrTagXXX52 = new ActnoBicAdrTagXXX("52");
        mt103Creator.setOrderingInstitution(actnoBicAdrTagXXX52);
        //56A
        ActnoBicAdrTagXXX actnoBicAdrTagXXX56 = new ActnoBicAdrTagXXX("56");
        if(!MdaUtils.isEmpty(ecm.getSkrdlh())){
            actnoBicAdrTagXXX56.setBic(ecm.getSkrdlh());
        }
        mt103Creator.setIntermediaryInstitution(actnoBicAdrTagXXX56);
        //57A
        ActnoBicAdrTagXXX actnoBicAdrTagXXX57A = new ActnoBicAdrTagXXX("57");
        if(!MdaUtils.isEmpty(ecm.getBicnum())){
            actnoBicAdrTagXXX57A.setBic(ecm.getBicnum());
        }
        mt103Creator.setAccountWithInstitution(actnoBicAdrTagXXX57A);
        //57D
        ActnoBicAdrTagXXX actnoBicAdrTagXXX57D = new ActnoBicAdrTagXXX("57");
        actnoBicAdrTagXXX57D.setAddress(ecm.getQisdam() + CR  + ecm.getSkyhmc() + CR + ecm.getSkyhdz());
        mt103Creator.setAccountWithInstitution(actnoBicAdrTagXXX57D);
        //59
        ActnoBicAdrTagXXX actnoBicAdrTagXXX59 = new ActnoBicAdrTagXXX("59");
        actnoBicAdrTagXXX59.setAddress(ecm.getSkrzhh() + CR + ecm.getSkrmch() + CR + ecm.getSkrdzh());
        mt103Creator.setBeneficiaryCustomer(actnoBicAdrTagXXX59);
        //70
        mt103Creator.setRemittanceInformation(ecm.getFukxix());
        //71A
        mt103Creator.setDetailsofCharges(ecm.getFeychd());
        //72
        mt103Creator.setSendertoReceiverInformation(ecm.getFgsfya());

        MtResult mtResult = MtCreatorManager.packMT(mt103Creator);

        TextResultVo textResultVo = new TextResultVo();
        textResultVo.setData(MtCreatorManager.packMT(mt103Creator).getMessage());
        textResultVo.setFormat(TEXT);
        textResultVo.setExtension("sf2");
        textResultVo.setBinary(mtResult.getMessage().getBytes());
        textResultVo.setAttach(mtResult.getAbsMTCreator());
        return textResultVo;
    }
}
