package com.brilliance.isc.doc.convert;

import com.brilliance.mda.runtime.mda.impl.Argument;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.brilliance.isc.exception.SSTFException;
import com.brilliance.isc.swift.objmtconvert.manager.MtCreatorManager;
import com.brilliance.isc.swift.objmtconvert.model.AbsMTCreator;
import com.brilliance.isc.swift.objmtconvert.model.mt7xx.*;
import com.brilliance.isc.swift.objmtconvert.result.MtResult;
import com.google.common.collect.Lists;
import com.prowidesoftware.swift.model.field.*;
import com.prowidesoftware.swift.model.mt.mt7xx.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Component
public class MTCreatorSplitor {

    private static final Logger LOGGER = LoggerFactory.getLogger(MTCreatorSplitor.class);

    //SWIFT 换行符必须为\r\n
    private String CR = "\r\n";

    private static final int MAX_LINE_150 = 150;

    /**
     * 701报文拆分域的最大行数均为100行
     */
    private static final int MT701_MAX_LINE_100 = 100;

    private static final int MAX_TOTAL_LEN = 9000;

    private static final int MAX_SPLIT_CNT = 7;

    public List<MT761Creator> split760(MT760Creator mt760Creator) throws SSTFException {
        int totalLen = mt760Creator.getAbstractMT().message().length();
        MT760 mt760 = (MT760) mt760Creator.getAbstractMT();
        Field77U field77U = mt760.getField77U();
        Field77L field77L = mt760.getField77L();
        int lineCnt77U = 0;
        int lineCnt77L = 0;
        if (field77U != null) {
            lineCnt77U = field77U.getLines().size();
        }
        if (field77L != null) {
            lineCnt77L = field77L.getLines().size();
        }
        // 不需要拆分时直接退出
        if (totalLen < MAX_TOTAL_LEN && lineCnt77U < MAX_LINE_150 && lineCnt77L < MAX_LINE_150) {
            return new ArrayList<>();
        }
        List<String> lines77U = new ArrayList<>();
        if (field77U != null) {
            lines77U = field77U.getLines();
            // 先将77U清空
            mt760Creator.setUndertakingTermsandConditionsOfB("");
        }
        List<String> lines77L = new ArrayList<>();
        if (field77L != null) {
            lines77L = field77L.getLines();
            // 先将77L清空
            mt760Creator.setRequestedLocalUndertakingTermsandConditionsOfC("");
        }
        // 计算剩余字符数
        // swift最大长度为9000
        String sndbic = mt760Creator.getSendBic();
        String rcvbic = mt760Creator.getRecvBic();
        invoke(mt760Creator, "initAbstractMT");
        mt760Creator.setSendBic(sndbic);
        mt760Creator.setRecvBic(rcvbic);
        MtResult mtResult = MtCreatorManager.packMT(mt760Creator);
        String message = mtResult.getMessage();
        int lenWithout77 = message.length();
        // 剩余字符数,减10是77U和77L的tag长度
        int remainLen = MAX_TOTAL_LEN - lenWithout77 - 10;
        int maxTagLines = MAX_LINE_150;
        List<String> newLines77U = new ArrayList<>();
        List<String> newLines77L = new ArrayList<>();
        Argument<String> splitFieldCod = new Argument<>("", "");
        if (CollectionUtils.isNotEmpty(lines77U)) {
            remainLen = this.split77uOr77l(newLines77U, maxTagLines, splitFieldCod, lines77U, remainLen);
            //lines77U已被更新
            mt760Creator.setUndertakingTermsandConditionsOfB(org.apache.commons.lang.StringUtils.join(newLines77U, CR));
        }
        if (CollectionUtils.isNotEmpty(lines77L)) {
            this.split77uOr77l(newLines77L, maxTagLines, splitFieldCod, lines77L, remainLen);
            //lines77L已被更新
            mt760Creator.setRequestedLocalUndertakingTermsandConditionsOfC(org.apache.commons.lang.StringUtils.join(newLines77L, CR));
        }
        // 组装761报文
        List<MT761Creator> mt761List = this.make761(mt760Creator, lines77U, lines77L);
        if (mt761List.size() > MAX_SPLIT_CNT) {
            throw new SSTFException("S00001", "分片超过最大数量");
        }
        int maxCnt = mt761List.size() + 1;
        mt760Creator.setSequenceofTotalOfA("1/" + maxCnt);
        int curCnt = 2;
        for (MT761Creator creator : mt761List) {
            creator.setSequenceofTotal(curCnt + "/" + maxCnt);
            curCnt++;
        }
        return mt761List;
    }

    private void invoke(Object obj, String methodName) {
        try {
            Method method = AbsMTCreator.class.getDeclaredMethod(methodName);
            method.setAccessible(true);
            method.invoke(obj);
        } catch (Exception ex) {
            LOGGER.error("执行方法异常：" + methodName, ex);
        }
    }

    private List<MT761Creator> make761(MT760Creator mt760Creator, List<String> overLineList77U, List<String> overLineList77L) throws SSTFException {
        MT761Creator tmpMt761Creator = this.buildCreator761(mt760Creator);
        String message = MtCreatorManager.packMT(tmpMt761Creator).getMessage();
        int maxTagLines = MAX_LINE_150;
        // 减10是77U和77L的tag长度
        int remainMaxLen = MAX_TOTAL_LEN - message.length() - 10;
        boolean splitFlg = true;
        List<MT761Creator> overMtList = new ArrayList<>();
        while (splitFlg) {
            int remainLen = remainMaxLen;
            splitFlg = false;
            Argument<String> splitFieldCod = new Argument<>("", "");
            MT761Creator mt761Creator = this.buildCreator761(mt760Creator);
            List<String> newLines77U = new ArrayList<>();
            List<String> newLines77L = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(overLineList77U)) {
                splitFlg = true;
                remainLen = this.split77uOr77l(newLines77U, maxTagLines, splitFieldCod, overLineList77U, remainLen);
                String t77u = StringUtils.join(newLines77U, CR);
                mt761Creator.setUndertakingTermsandConditions(t77u);
            }
            if (CollectionUtils.isNotEmpty(overLineList77L)) {
                splitFlg = true;
                this.split77uOr77l(newLines77L, maxTagLines, splitFieldCod, overLineList77L, remainLen);
                String t77l = StringUtils.join(newLines77L, CR);
                mt761Creator.setRequestedLocalUndertakingTermsandConditions(t77l);
            }
            if (CollectionUtils.isNotEmpty(newLines77U) || CollectionUtils.isNotEmpty(newLines77L)) {
                overMtList.add(mt761Creator);
            }
        }
        return overMtList;
    }

    private MT761Creator buildCreator761(MT760Creator mt760Creator) {
        MT761Creator mt761Creator = new MT761Creator();
        mt761Creator.setRecvBic(mt760Creator.getRecvBic());
        mt761Creator.setSendBic(mt760Creator.getSendBic());
        mt761Creator.setSequenceofTotal("1/1");
        mt761Creator.setUndertakingNumber(mt760Creator.getUndertakingNumberOfB());
        mt761Creator.setIssuer(mt760Creator.getIssuerOfB());
        return mt761Creator;
    }

    private int split77uOr77l(List<String> newLines77, int maxTagLines, Argument<String> splitFieldCod, List<String> overLineList, int remainLen) {
        List<String> newLineList = new ArrayList<>();
        for (int idx2 = 0; idx2 < overLineList.size(); idx2++) {
            String line = overLineList.get(idx2);
            // 换行符长度为2
            int tmpRemainLen = remainLen - line.length() - 2;
            if (tmpRemainLen > 0 && idx2 < maxTagLines) {
                // 未超长
                newLineList.add(line);
                remainLen = tmpRemainLen;
            } else {
                // 超长了
                break;
            }
        }
        // 此处不能removeAll，只能sublist
        newLines77.addAll(newLineList);
        List<String> empList = new ArrayList<>(overLineList.subList(newLineList.size(), overLineList.size()));
        overLineList.clear();
        overLineList.addAll(empList);
        String cod = "";
        for (int idx = 0; idx < newLineList.size(); idx++) {
            String line = newLineList.get(idx);
            if (idx == 0) {
                if (line.indexOf("/") == 5) {
                    int len = line.indexOf("/", 6);
                    if (len > 0) {
                        cod = line.substring(5, len + 1);
                    }
                }
            } else if (line.startsWith("/AD")) {
                cod = "/ADD/";
            } else if (line.startsWith("/RE")) {
                cod = "/REPALL/";
            } else if (line.startsWith("/DE")) {
                cod = "/DELETE/";
            }
        }
        splitFieldCod.value = cod;
        return remainLen;
    }


    public List<MT775Creator> split767(MT767Creator mt767Creator) throws SSTFException {
        int totalLen = mt767Creator.getAbstractMT().message().length();
        MT767 mt767 = (MT767) mt767Creator.getAbstractMT();
        Field77U field77U = mt767.getField77U();
        Field77L field77L = mt767.getField77L();
        int lineCnt77U = 0;
        int lineCnt77L = 0;
        if (field77U != null) {
            lineCnt77U = field77U.getLines().size();
        }
        if (field77L != null) {
            lineCnt77L = field77L.getLines().size();
        }
        // 不需要拆分时直接退出
        if (totalLen < MAX_TOTAL_LEN && lineCnt77U < MAX_LINE_150 && lineCnt77L < MAX_LINE_150) {
            return new ArrayList<>();
        }
        List<String> lines77U = new ArrayList<>();
        if (field77U != null) {
            lines77U = field77U.getLines();
            // 先将77U清空
            mt767Creator.setOtherAmendmentsToUndertakingOfB("");
        }
        List<String> lines77L = new ArrayList<>();
        if (field77L != null) {
            lines77L = field77L.getLines();
            mt767Creator.setOtherAmendmentsToLocalUndertakingOfC("");
        }
        String sndbic = mt767Creator.getSendBic();
        String rcvbic = mt767Creator.getRecvBic();
        invoke(mt767Creator, "initAbstractMT");
        mt767Creator.setSendBic(sndbic);
        mt767Creator.setRecvBic(rcvbic);
        MtResult mtResult = MtCreatorManager.packMT(mt767Creator);
        String message = mtResult.getMessage();
        int lenWithout77 = message.length();
        // 剩余字符数,减10是77U和77L的tag长度
        int remainLen = MAX_TOTAL_LEN - lenWithout77 - 10;
        int maxTagLines = MAX_LINE_150;
        List<String> newLines77U = new ArrayList<>();
        List<String> newLines77L = new ArrayList<>();
        Argument<String> splitFieldCod = new Argument<>("", "");
        if (CollectionUtils.isNotEmpty(lines77U)) {
            remainLen = this.split77uOr77l(newLines77U, maxTagLines, splitFieldCod, lines77U, remainLen);
            mt767Creator.setOtherAmendmentsToUndertakingOfB(org.apache.commons.lang.StringUtils.join(newLines77U, CR));
        }
        if (CollectionUtils.isNotEmpty(lines77L)) {
            this.split77uOr77l(newLines77L, maxTagLines, splitFieldCod, lines77L, remainLen);
            mt767Creator.setOtherAmendmentsToLocalUndertakingOfC(org.apache.commons.lang.StringUtils.join(newLines77L, CR));
        }
        // 组装775报文
        List<MT775Creator> mt775List = this.make775(mt767Creator, lines77U, lines77L);
        if (mt775List.size() > MAX_SPLIT_CNT) {
            throw new SSTFException("S00001", "分片超过最大数量");
        }
        int maxCnt = mt775List.size() + 1;
        mt767Creator.setSequenceOfTotalOfA("1/" + maxCnt);
        int curCnt = 2;
        for (MT775Creator creator : mt775List) {
            creator.setSequenceofTotal(curCnt + "/" + maxCnt);
            curCnt++;
        }
        return mt775List;
    }

    private List<MT775Creator> make775(MT767Creator mt767Creator, List<String> overLineList77U, List<String> overLineList77L) throws SSTFException {
        MT775Creator tmpMt775Creator = this.buildCreator775(mt767Creator);
        String message = MtCreatorManager.packMT(tmpMt775Creator).getMessage();
        int maxTagLines = MAX_LINE_150;
        // 减10是77U和77L的tag长度
        int remainMaxLen = MAX_TOTAL_LEN - message.length() - 10;
        boolean splitFlg = true;
        List<MT775Creator> overMtList = new ArrayList<>();
        while (splitFlg) {
            int remainLen = remainMaxLen;
            splitFlg = false;
            Argument<String> splitFieldCod = new Argument<>("", "");
            MT775Creator mt775Creator = this.buildCreator775(mt767Creator);
            List<String> newLines77U = new ArrayList<>();
            List<String> newLines77L = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(overLineList77U)) {
                splitFlg = true;
                remainLen = this.split77uOr77l(newLines77U, maxTagLines, splitFieldCod, overLineList77U, remainLen);
                String t77u = StringUtils.join(newLines77U, CR);
                mt775Creator.setOtherAmendmentstoUndertaking(t77u);
            }
            if (CollectionUtils.isNotEmpty(overLineList77L)) {
                splitFlg = true;
                this.split77uOr77l(newLines77L, maxTagLines, splitFieldCod, overLineList77L, remainLen);
                String t77l = StringUtils.join(newLines77L, CR);
                mt775Creator.setOtherAmendmentstoLocalUndertaking(t77l);
            }
            if (CollectionUtils.isNotEmpty(newLines77U) || CollectionUtils.isNotEmpty(newLines77L)) {
                overMtList.add(mt775Creator);
            }
        }
        return overMtList;
    }

    private MT775Creator buildCreator775(MT767Creator mt767Creator) {
        MT775Creator mt775Creator = new MT775Creator();
        mt775Creator.setSendBic(mt767Creator.getSendBic());
        mt775Creator.setRecvBic(mt767Creator.getRecvBic());
        mt775Creator.setSequenceofTotal("1/1");
        mt775Creator.setUndertakingNumber(mt767Creator.getUndertakingNumberOfB());
        mt775Creator.setRelatedReference(mt767Creator.getRelatedReferenceOfA());
        mt775Creator.setNumberofAmendment(mt767Creator.getNumberOfAmendmentOfB());
        mt775Creator.setIssuer(mt767Creator.getIssuerOfB());
        return mt775Creator;
    }


    public List<MT759Creator> split759(MT759Creator mt759Creator) throws SSTFException {
        MT759 mt759 = (MT759) mt759Creator.getAbstractMT();
        Field45D field45D = mt759.getField45D();
        List<String> t45dList = field45D.getLines();
        List<String> lineList = new ArrayList<>();
        List<String> overLineList = new ArrayList<>();
        if (t45dList.size() > MAX_LINE_150) {
            List<String> newLineList = new ArrayList<>(t45dList.subList(0, MAX_LINE_150));
            overLineList = t45dList.subList(MAX_LINE_150, t45dList.size());
            lineList.addAll(newLineList);
        }
        //更新当前759
        mt759Creator.setNarrative(StringUtils.join(lineList, "\r\n"));
        // 组装759拆分报文
        List<MT759Creator> mt759List = this.make759(mt759Creator, overLineList);
        return mt759List;
    }

    private List<MT759Creator> make759(MT759Creator mt759, List<String> overLineList) throws SSTFException {
        List<MT759Creator> overMtList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(overLineList)) {
            List<List<String>> partitionList = Lists.partition(overLineList, MAX_LINE_150);
            int msgCnt = partitionList.size() + 1;
            int curCnt = 2;
            mt759.setSequenceofTotal("1/" + msgCnt);
            for (List<String> list : partitionList) {
                MT759Creator mt759Creator = new MT759Creator();
                mt759Creator.setSendBic(mt759.getSendBic());
                mt759Creator.setRecvBic(mt759.getRecvBic());
                String t27 = curCnt + "/" + msgCnt;
                mt759Creator.setSequenceofTotal(t27);

                String t45d = StringUtils.join(list, "\r\n");

                mt759Creator.setSequenceofTotal(mt759.getSequenceofTotal());
                mt759Creator.setTransactionReferenceNumber(mt759.getTransactionReferenceNumber());
                mt759Creator.setRelatedReferenceNumber(mt759.getRelatedReferenceNumber());
                mt759Creator.setFormofUndertaking(mt759.getFormofUndertaking());
                mt759Creator.setFunctionofMessage(mt759.getFunctionofMessage());
                mt759Creator.setNarrative(t45d);
                overMtList.add(mt759Creator);
                curCnt++;
            }
        }
        return overMtList;
    }

    /**
     * mt700报文拆分
     *
     * @param mt700Creator 入参
     * @return List
     * @throws SSTFException 异常
     */
    private List<MT701Creator> split700(MT700Creator mt700Creator) throws SSTFException {
        int totalLen = mt700Creator.getAbstractMT().message().length();
        MT700 mt700 = (MT700) mt700Creator.getAbstractMT();
        Field45A field45A = mt700.getField45A();
        Field46A field46A = mt700.getField46A();
        Field47A field47A = mt700.getField47A();
        Field49H field49H = mt700.getField49H();
        Field49G field49G = mt700.getField49G();
        int lineCnt45A = 0;
        int lineCnt46A = 0;
        int lineCnt47A = 0;
        int lineCnt49H = 0;
        int lineCnt49G = 0;
        if (field45A != null) {
            lineCnt45A = field45A.getLines().size();
        }
        if (field46A != null) {
            lineCnt46A = field46A.getLines().size();
        }
        if (field47A != null) {
            lineCnt47A = field47A.getLines().size();
        }
        if (field49H != null) {
            lineCnt49H = field49H.getLines().size();
        }
        if (field49G != null) {
            lineCnt49G = field49G.getLines().size();
        }
        // 不需要拆分时直接退出
        if (totalLen < MAX_TOTAL_LEN && lineCnt45A < MT701_MAX_LINE_100 && lineCnt46A < MT701_MAX_LINE_100
                && lineCnt47A < MT701_MAX_LINE_100 && lineCnt49H < MT701_MAX_LINE_100 && lineCnt49G < MT701_MAX_LINE_100) {
            return new ArrayList<>();
        }
        List<String> lines45A = new ArrayList<>();
        List<String> lines46A = new ArrayList<>();
        List<String> lines47A = new ArrayList<>();
        List<String> lines49G = new ArrayList<>();
        List<String> lines49H = new ArrayList<>();
        if (field45A != null) {
            lines45A = field45A.getLines();
            // 先将45A清空
            mt700Creator.setDescOfGoodsAndServ("");
        }
        if (field46A != null) {
            lines46A = field46A.getLines();
            // 先将46A清空
            mt700Creator.setDocumentsRequired("");
        }
        if (field47A != null) {
            lines47A = field47A.getLines();
            // 先将47A清空
            mt700Creator.setAdditionalConditions("");
        }
        if (field49H != null) {
            lines49H = field49H.getLines();
            // 先将49H清空
            mt700Creator.setSpecPaymCndiForRecvBank("");
        }
        if (field49G != null) {
            lines49G = field49G.getLines();
            // 先将49G清空
            mt700Creator.setSpecPaymentCondForBenefi("");
        }
        // 计算剩余字符数
        // swift最大长度为9000
        String sndbic = mt700Creator.getSendBic();
        String rcvbic = mt700Creator.getRecvBic();
        invoke(mt700Creator, "initAbstractMT");
        mt700Creator.setSendBic(sndbic);
        mt700Creator.setRecvBic(rcvbic);
        MtResult mtResult = MtCreatorManager.packMT(mt700Creator);
        String message = mtResult.getMessage();
        int lenWithoutSplitField = message.length();
        // 剩余字符数,减25是5个tag的长度
        int remainLen = MAX_TOTAL_LEN - lenWithoutSplitField - 25;
        int maxTagLines = MT701_MAX_LINE_100;
        // 5个tag逐个拆分
        // 1.拆分45A
        List<String> newLines45A = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines45A)) {
            remainLen = this.splitTag(newLines45A, maxTagLines, lines45A, remainLen);
            // lines45A已被更新
            mt700Creator.setDescOfGoodsAndServ(org.apache.commons.lang.StringUtils.join(newLines45A, CR));
        }
        // 2.拆分46A
        List<String> newLines46A = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines46A)) {
            remainLen = this.splitTag(newLines46A, maxTagLines, lines46A, remainLen);
            // lines46A已被更新
            mt700Creator.setDocumentsRequired(org.apache.commons.lang.StringUtils.join(newLines46A, CR));
        }
        // 3.拆分47A
        List<String> newLines47A = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines47A)) {
            remainLen = this.splitTag(newLines47A, maxTagLines, lines47A, remainLen);
            // lines46A已被更新
            mt700Creator.setAdditionalConditions(org.apache.commons.lang.StringUtils.join(newLines47A, CR));
        }
        // 4.拆分49G
        List<String> newLines49G = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines49G)) {
            remainLen = this.splitTag(newLines49G, maxTagLines, lines49G, remainLen);
            // lines49G已被更新
            mt700Creator.setSpecPaymentCondForBenefi(org.apache.commons.lang.StringUtils.join(newLines49G, CR));
        }
        // 5.拆分49H
        List<String> newLines49H = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines49H)) {
            this.splitTag(newLines49H, maxTagLines, lines49H, remainLen);
            // lines49H已被更新
            mt700Creator.setSpecPaymCndiForRecvBank(org.apache.commons.lang.StringUtils.join(newLines49H, CR));
        }

        // 组装701报文
        List<MT701Creator> mt701List = this.make701(mt700Creator, lines45A, lines46A, lines47A, lines49G, lines49H);
        if (mt701List.size() > MAX_SPLIT_CNT) {
            throw new SSTFException("S00001", "分片超过最大数量");
        }
        int maxCnt = mt701List.size() + 1;
        mt700Creator.setSequenceofTotal("1/" + maxCnt);
        int curCnt = 2;
        for (MT701Creator creator : mt701List) {
            creator.setSequenceofTotal(curCnt + "/" + maxCnt);
            curCnt++;
        }
        return mt701List;
    }

    private List<MT701Creator> make701(
            MT700Creator mt700Creator, List<String> lines45A, List<String> lines46A, List<String> lines47A,
            List<String> lines49G, List<String> lines49H) throws SSTFException {
        MT701Creator tmpMt701Creator = this.buildCreator701(mt700Creator);
        String message = MtCreatorManager.packMT(tmpMt701Creator).getMessage();
        int maxTagLines = MT701_MAX_LINE_100;
        // 减25是5个tag长度
        int remainMaxLen = MAX_TOTAL_LEN - message.length() - 25;
        boolean splitFlg = true;
        List<MT701Creator> overMtList = new ArrayList<>();
        while (splitFlg) {
            int remainLen = remainMaxLen;
            splitFlg = false;
            MT701Creator mt701Creator = this.buildCreator701(mt700Creator);
            // 5个tag逐个拆分
            // 1.拆分45A
            List<String> newLines45A = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines45A)) {
                splitFlg = true;
                remainLen = this.splitTag(newLines45A, maxTagLines, lines45A, remainLen);
                // lines45A已被更新
                mt701Creator.setDescriptionofGoodsandorServices(org.apache.commons.lang.StringUtils.join(newLines45A, CR));
            }
            // 2.拆分46A
            List<String> newLines46A = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines46A)) {
                splitFlg = true;
                remainLen = this.splitTag(newLines46A, maxTagLines, lines46A, remainLen);
                // lines46A已被更新
                mt701Creator.setDocumentsRequired(org.apache.commons.lang.StringUtils.join(newLines46A, CR));
            }
            // 3.拆分47A
            List<String> newLines47A = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines47A)) {
                splitFlg = true;
                remainLen = this.splitTag(newLines47A, maxTagLines, lines47A, remainLen);
                // lines47A已被更新
                mt701Creator.setAdditionalConditions(org.apache.commons.lang.StringUtils.join(newLines47A, CR));
            }
            // 4.拆分49G
            List<String> newLines49G = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines49G)) {
                splitFlg = true;
                remainLen = this.splitTag(newLines49G, maxTagLines, lines49G, remainLen);
                // lines49G已被更新
                mt701Creator.setSpecialPaymentConditionsforBeneficiary(org.apache.commons.lang.StringUtils.join(newLines49G, CR));
            }
            // 5.拆分49H
            List<String> newLines49H = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines49H)) {
                splitFlg = true;
                this.splitTag(newLines49H, maxTagLines, lines49H, remainLen);
                // lines49H已被更新
                mt701Creator.setSpecialPaymentConditionsforBankOnly(org.apache.commons.lang.StringUtils.join(newLines49H, CR));
            }
            if (CollectionUtils.isNotEmpty(newLines45A) || CollectionUtils.isNotEmpty(newLines46A) || CollectionUtils.isNotEmpty(newLines47A)
                    || CollectionUtils.isNotEmpty(newLines49G) || CollectionUtils.isNotEmpty(newLines49H)) {
                overMtList.add(mt701Creator);
            }
        }
        return overMtList;
    }

    private MT701Creator buildCreator701(MT700Creator mt700Creator) {
        MT701Creator mt701Creator = new MT701Creator();
        mt701Creator.setRecvBic(mt700Creator.getRecvBic());
        mt701Creator.setSendBic(mt700Creator.getSendBic());
        mt701Creator.setSequenceofTotal("1/1");
        mt701Creator.setDocumentaryCreditNumber(mt700Creator.getDocumentaryCreditNumber());
        return mt701Creator;
    }

    /**
     * mt707报文拆分
     *
     * @param mt707Creator 入参
     * @return List
     * @throws SSTFException 异常
     */
    private List<MT708Creator> split707(MT707Creator mt707Creator) throws SSTFException {
        int totalLen = mt707Creator.getAbstractMT().message().length();
        MT707 mt707 = (MT707) mt707Creator.getAbstractMT();
        Field45B field45B = mt707.getField45B();
        Field46B field46B = mt707.getField46B();
        Field47B field47B = mt707.getField47B();
        Field49M field49M = mt707.getField49M();
        Field49N field49N = mt707.getField49N();
        int lineCnt45B = 0;
        int lineCnt46B = 0;
        int lineCnt47B = 0;
        int lineCnt49M = 0;
        int lineCnt49N = 0;
        if (field45B != null) {
            lineCnt45B = field45B.getLines().size();
        }
        if (field46B != null) {
            lineCnt46B = field46B.getLines().size();
        }
        if (field47B != null) {
            lineCnt47B = field47B.getLines().size();
        }
        if (field49M != null) {
            lineCnt49M = field49M.getLines().size();
        }
        if (field49N != null) {
            lineCnt49N = field49N.getLines().size();
        }
        // 不需要拆分时直接退出
        if (totalLen < MAX_TOTAL_LEN && lineCnt45B < MT701_MAX_LINE_100 && lineCnt46B < MT701_MAX_LINE_100
                && lineCnt47B < MT701_MAX_LINE_100 && lineCnt49M < MT701_MAX_LINE_100 && lineCnt49N < MT701_MAX_LINE_100) {
            return new ArrayList<>();
        }
        List<String> lines45B = new ArrayList<>();
        List<String> lines46B = new ArrayList<>();
        List<String> lines47B = new ArrayList<>();
        List<String> lines49M = new ArrayList<>();
        List<String> lines49N = new ArrayList<>();
        if (field45B != null) {
            lines45B = field45B.getLines();
            // 先将45B清空
            mt707Creator.setDescriptionOfGoodsAndOS("");
        }
        if (field46B != null) {
            lines46B = field46B.getLines();
            // 先将46B清空
            mt707Creator.setDocumentsRequired("");
        }
        if (field47B != null) {
            lines47B = field47B.getLines();
            // 先将47B清空
            mt707Creator.setAdditionalConditions("");
        }
        if (field49M != null) {
            lines49M = field49M.getLines();
            // 先将49M清空
            mt707Creator.setSpecialPaymentConditionsForBeneficiary("");
        }
        if (field49N != null) {
            lines49N = field49N.getLines();
            // 先将49N清空
            mt707Creator.setSpecialPaymentConditionsForBankOnly("");
        }
        // 计算剩余字符数
        // swift最大长度为9000
        String sndbic = mt707Creator.getSendBic();
        String rcvbic = mt707Creator.getRecvBic();
        invoke(mt707Creator, "initAbstractMT");
        mt707Creator.setSendBic(sndbic);
        mt707Creator.setRecvBic(rcvbic);
        MtResult mtResult = MtCreatorManager.packMT(mt707Creator);
        String message = mtResult.getMessage();
        int lenWithoutSplitField = message.length();
        // 剩余字符数,减25是5个tag的长度
        int remainLen = MAX_TOTAL_LEN - lenWithoutSplitField - 25;
        int maxTagLines = MT701_MAX_LINE_100;
        // 5个tag逐个拆分
        // 1.拆分45A
        List<String> newLines45B = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines45B)) {
            remainLen = this.splitTag(newLines45B, maxTagLines, lines45B, remainLen);
            // lines45B已被更新
            mt707Creator.setDescriptionOfGoodsAndOS(org.apache.commons.lang.StringUtils.join(newLines45B, CR));
        }
        // 2.拆分46B
        List<String> newLines46B = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines46B)) {
            remainLen = this.splitTag(newLines46B, maxTagLines, lines46B, remainLen);
            // lines46B已被更新
            mt707Creator.setDocumentsRequired(org.apache.commons.lang.StringUtils.join(newLines46B, CR));
        }
        // 3.拆分47B
        List<String> newLines47B = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines47B)) {
            remainLen = this.splitTag(newLines47B, maxTagLines, lines47B, remainLen);
            // lines46B已被更新
            mt707Creator.setAdditionalConditions(org.apache.commons.lang.StringUtils.join(newLines47B, CR));
        }
        // 4.拆分49M
        List<String> newLines49M = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines49M)) {
            remainLen = this.splitTag(newLines49M, maxTagLines, lines49M, remainLen);
            // lines49M已被更新
            mt707Creator.setSpecialPaymentConditionsForBeneficiary(org.apache.commons.lang.StringUtils.join(newLines49M, CR));
        }
        // 5.拆分49N
        List<String> newLines49N = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines49N)) {
            this.splitTag(newLines49N, maxTagLines, lines49N, remainLen);
            // lines49N已被更新
            mt707Creator.setSpecialPaymentConditionsForBankOnly(org.apache.commons.lang.StringUtils.join(newLines49N, CR));
        }

        // 组装708报文
        List<MT708Creator> mt708List = this.make708(mt707Creator, lines45B, lines46B, lines47B, lines49M, lines49N);
        if (mt708List.size() > MAX_SPLIT_CNT) {
            throw new SSTFException("S00001", "分片超过最大数量");
        }
        int maxCnt = mt708List.size() + 1;
        mt707Creator.setSequenceOfTotal("1/" + maxCnt);
        int curCnt = 2;
        for (MT708Creator creator : mt708List) {
            creator.setSequenceofTotal(curCnt + "/" + maxCnt);
            curCnt++;
        }
        return mt708List;
    }

    private List<MT708Creator> make708(
            MT707Creator mt707Creator, List<String> lines45B, List<String> lines46B, List<String> lines47B,
            List<String> lines49M, List<String> lines49N) throws SSTFException {
        MT708Creator tmpMt708Creator = this.buildCreator708(mt707Creator);
        String message = MtCreatorManager.packMT(tmpMt708Creator).getMessage();
        int maxTagLines = MT701_MAX_LINE_100;
        // 减25是5个tag长度
        int remainMaxLen = MAX_TOTAL_LEN - message.length() - 25;
        boolean splitFlg = true;
        List<MT708Creator> overMtList = new ArrayList<>();
        while (splitFlg) {
            int remainLen = remainMaxLen;
            splitFlg = false;
            MT708Creator mt708Creator = this.buildCreator708(mt707Creator);
            // 5个tag逐个拆分
            // 1.拆分45B
            List<String> newLines45B = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines45B)) {
                // 需要加/REPALL/字符
                lines45B.add(0, "/REPALL/");
                splitFlg = true;
                remainLen = this.splitTag(newLines45B, maxTagLines, lines45B, remainLen);
                // lines45A已被更新
                mt708Creator.setDescriptionofGoodsServices(org.apache.commons.lang.StringUtils.join(newLines45B, CR));
            }
            // 2.拆分46B
            List<String> newLines46B = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines46B)) {
                lines46B.add(0, "/REPALL/");
                splitFlg = true;
                remainLen = this.splitTag(newLines46B, maxTagLines, lines46B, remainLen);
                // lines46B已被更新
                mt708Creator.setDocumentsRequired(org.apache.commons.lang.StringUtils.join(newLines46B, CR));
            }
            // 3.拆分47B
            List<String> newLines47B = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines47B)) {
                lines47B.add(0, "/REPALL/");
                splitFlg = true;
                remainLen = this.splitTag(newLines47B, maxTagLines, lines47B, remainLen);
                // lines47B已被更新
                mt708Creator.setAdditionalConditions(org.apache.commons.lang.StringUtils.join(newLines47B, CR));
            }
            // 4.拆分49M
            List<String> newLines49M = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines49M)) {
                lines49M.add(0, "/REPALL/");
                splitFlg = true;
                remainLen = this.splitTag(newLines49M, maxTagLines, lines49M, remainLen);
                // lines49M已被更新
                mt708Creator.setSpecialPaymentConditionsforBeneficiary(org.apache.commons.lang.StringUtils.join(newLines49M, CR));
            }
            // 5.拆分49N
            List<String> newLines49N = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines49N)) {
                lines49N.add(0, "/REPALL/");
                splitFlg = true;
                this.splitTag(newLines49N, maxTagLines, lines49N, remainLen);
                // lines49H已被更新
                mt708Creator.setSpecialPaymentConditionsforBankOnly(org.apache.commons.lang.StringUtils.join(newLines49N, CR));
            }
            if (CollectionUtils.isNotEmpty(newLines45B) || CollectionUtils.isNotEmpty(newLines46B) || CollectionUtils.isNotEmpty(newLines47B)
                    || CollectionUtils.isNotEmpty(newLines49M) || CollectionUtils.isNotEmpty(newLines49N)) {
                overMtList.add(mt708Creator);
            }
        }
        return overMtList;
    }

    private MT708Creator buildCreator708(MT707Creator mt707Creator) {
        MT708Creator mt708Creator = new MT708Creator();
        mt708Creator.setRecvBic(mt707Creator.getRecvBic());
        mt708Creator.setSendBic(mt707Creator.getSendBic());
        mt708Creator.setSequenceofTotal("1/1");
        mt708Creator.setSenderReference(mt707Creator.getSendersReference());
        mt708Creator.setReceiverReference(mt707Creator.getReceiversReference());
        mt708Creator.setIssuingBankReference(mt707Creator.getIssuingBanksReference());
        mt708Creator.setNumberofAmendment(mt707Creator.getNumberOfAmendment());
        mt708Creator.setDateofAmendment(mt707Creator.getDateOfAmendment());
        return mt708Creator;
    }

    /**
     * mt720报文拆分
     *
     * @param mt720Creator 入参
     * @return List
     * @throws SSTFException 异常
     */
    private List<MT721Creator> split720(MT720Creator mt720Creator) throws SSTFException {
        int totalLen = mt720Creator.getAbstractMT().message().length();
        MT720 mt720 = (MT720) mt720Creator.getAbstractMT();
        Field45A field45A = mt720.getField45A();
        Field46A field46A = mt720.getField46A();
        Field47A field47A = mt720.getField47A();
        Field49G field49G = mt720.getField49G();
        Field49H field49H = mt720.getField49H();
        int lineCnt45A = 0;
        int lineCnt46A = 0;
        int lineCnt47A = 0;
        int lineCnt49G = 0;
        int lineCnt49H = 0;
        if (field45A != null) {
            lineCnt45A = field45A.getLines().size();
        }
        if (field46A != null) {
            lineCnt46A = field46A.getLines().size();
        }
        if (field47A != null) {
            lineCnt47A = field47A.getLines().size();
        }
        if (field49G != null) {
            lineCnt49G = field49G.getLines().size();
        }
        if (field49H != null) {
            lineCnt49H = field49H.getLines().size();
        }
        // 不需要拆分时直接退出
        if (totalLen < MAX_TOTAL_LEN && lineCnt45A < MT701_MAX_LINE_100 && lineCnt46A < MT701_MAX_LINE_100
                && lineCnt47A < MT701_MAX_LINE_100 && lineCnt49G < MT701_MAX_LINE_100 && lineCnt49H < MT701_MAX_LINE_100) {
            return new ArrayList<>();
        }
        List<String> lines45A = new ArrayList<>();
        List<String> lines46A = new ArrayList<>();
        List<String> lines47A = new ArrayList<>();
        List<String> lines49G = new ArrayList<>();
        List<String> lines49H = new ArrayList<>();
        if (field45A != null) {
            lines45A = field45A.getLines();
            // 先将45A清空
            mt720Creator.setDescriptionofGoodsServices("");
        }
        if (field46A != null) {
            lines46A = field46A.getLines();
            // 先将46A清空
            mt720Creator.setDocumentsRequired("");
        }
        if (field47A != null) {
            lines47A = field47A.getLines();
            // 先将47A清空
            mt720Creator.setAdditionalConditions("");
        }
        if (field49G != null) {
            lines49G = field49G.getLines();
            // 先将49G清空
            mt720Creator.setSpecialPaymentConditionsforBeneficiary("");
        }
        if (field49H != null) {
            lines49H = field49H.getLines();
            // 先将49H清空
            mt720Creator.setSpecialPaymentConditionsforBankOnly("");
        }
        // 计算剩余字符数
        // swift最大长度为9000
        String sndbic = mt720Creator.getSendBic();
        String rcvbic = mt720Creator.getRecvBic();
        invoke(mt720Creator, "initAbstractMT");
        mt720Creator.setSendBic(sndbic);
        mt720Creator.setRecvBic(rcvbic);
        MtResult mtResult = MtCreatorManager.packMT(mt720Creator);
        String message = mtResult.getMessage();
        int lenWithoutSplitField = message.length();
        // 剩余字符数,减25是5个tag的长度
        int remainLen = MAX_TOTAL_LEN - lenWithoutSplitField - 25;
        int maxTagLines = MT701_MAX_LINE_100;
        // 5个tag逐个拆分
        // 1.拆分45A
        List<String> newLines45B = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines45A)) {
            remainLen = this.splitTag(newLines45B, maxTagLines, lines45A, remainLen);
            // lines45A已被更新
            mt720Creator.setDescriptionofGoodsServices(org.apache.commons.lang.StringUtils.join(newLines45B, CR));
        }
        // 2.拆分46A
        List<String> newLines46A = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines46A)) {
            remainLen = this.splitTag(newLines46A, maxTagLines, lines46A, remainLen);
            // lines46A已被更新
            mt720Creator.setDocumentsRequired(org.apache.commons.lang.StringUtils.join(newLines46A, CR));
        }
        // 3.拆分47A
        List<String> newLines47A = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines47A)) {
            remainLen = this.splitTag(newLines47A, maxTagLines, lines47A, remainLen);
            // lines46A已被更新
            mt720Creator.setAdditionalConditions(org.apache.commons.lang.StringUtils.join(newLines47A, CR));
        }
        // 4.拆分49G
        List<String> newLines49G = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines49G)) {
            remainLen = this.splitTag(newLines49G, maxTagLines, lines49G, remainLen);
            // lines49G已被更新
            mt720Creator.setSpecialPaymentConditionsforBeneficiary(org.apache.commons.lang.StringUtils.join(newLines49G, CR));
        }
        // 5.拆分49H
        List<String> newLines49H = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines49H)) {
            this.splitTag(newLines49H, maxTagLines, lines49H, remainLen);
            // lines49H已被更新
            mt720Creator.setSpecialPaymentConditionsforBankOnly(org.apache.commons.lang.StringUtils.join(newLines49H, CR));
        }

        // 组装721报文
        List<MT721Creator> mt721List = this.make721(mt720Creator, lines45A, lines46A, lines47A, lines49G, lines49H);
        if (mt721List.size() > MAX_SPLIT_CNT) {
            throw new SSTFException("S00001", "分片超过最大数量");
        }
        int maxCnt = mt721List.size() + 1;
        mt720Creator.setSequenceofTotal("1/" + maxCnt);
        int curCnt = 2;
        for (MT721Creator creator : mt721List) {
            creator.setSequenceofTotal(curCnt + "/" + maxCnt);
            curCnt++;
        }
        return mt721List;
    }

    private List<MT721Creator> make721(
            MT720Creator mt720Creator, List<String> lines45A, List<String> lines46A, List<String> lines47A,
            List<String> lines49G, List<String> lines49H) throws SSTFException {
        MT721Creator tmpMt721Creator = this.buildCreator721(mt720Creator);
        String message = MtCreatorManager.packMT(tmpMt721Creator).getMessage();
        int maxTagLines = MT701_MAX_LINE_100;
        // 减25是5个tag长度
        int remainMaxLen = MAX_TOTAL_LEN - message.length() - 25;
        boolean splitFlg = true;
        List<MT721Creator> overMtList = new ArrayList<>();
        while (splitFlg) {
            int remainLen = remainMaxLen;
            splitFlg = false;
            MT721Creator mt721Creator = this.buildCreator721(mt720Creator);
            // 5个tag逐个拆分
            // 1.拆分45A
            List<String> newLines45A = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines45A)) {
                // 需要加/REPALL/字符
                lines45A.add(0, "/REPALL/");
                splitFlg = true;
                remainLen = this.splitTag(newLines45A, maxTagLines, lines45A, remainLen);
                // lines45A已被更新
                mt721Creator.setDescriptionofGoodsServices(org.apache.commons.lang.StringUtils.join(newLines45A, CR));
            }
            // 2.拆分46A
            List<String> newLines46A = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines46A)) {
                lines46A.add(0, "/REPALL/");
                splitFlg = true;
                remainLen = this.splitTag(newLines46A, maxTagLines, lines46A, remainLen);
                // lines46A已被更新
                mt721Creator.setDocumentsRequired(org.apache.commons.lang.StringUtils.join(newLines46A, CR));
            }
            // 3.拆分47A
            List<String> newLines47A = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines47A)) {
                lines47A.add(0, "/REPALL/");
                splitFlg = true;
                remainLen = this.splitTag(newLines47A, maxTagLines, lines47A, remainLen);
                // lines47A已被更新
                mt721Creator.setAdditionalConditions(org.apache.commons.lang.StringUtils.join(newLines47A, CR));
            }
            // 4.拆分49G
            List<String> newLines49G = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines49G)) {
                lines49G.add(0, "/REPALL/");
                splitFlg = true;
                remainLen = this.splitTag(newLines49G, maxTagLines, lines49G, remainLen);
                // lines49G已被更新
                mt721Creator.setSpecialPaymentConditionsforBeneficiary(org.apache.commons.lang.StringUtils.join(newLines49G, CR));
            }
            // 5.拆分49H
            List<String> newLines49H = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines49H)) {
                lines49H.add(0, "/REPALL/");
                splitFlg = true;
                this.splitTag(newLines49H, maxTagLines, lines49H, remainLen);
                // lines49H已被更新
                mt721Creator.setSpecialPaymentConditionsforBankOnly(org.apache.commons.lang.StringUtils.join(newLines49H, CR));
            }
            if (CollectionUtils.isNotEmpty(newLines45A) || CollectionUtils.isNotEmpty(newLines46A) || CollectionUtils.isNotEmpty(newLines47A)
                    || CollectionUtils.isNotEmpty(newLines49G) || CollectionUtils.isNotEmpty(newLines49H)) {
                overMtList.add(mt721Creator);
            }
        }
        return overMtList;
    }

    private MT721Creator buildCreator721(MT720Creator mt720Creator) {
        MT721Creator mt721Creator = new MT721Creator();
        mt721Creator.setRecvBic(mt720Creator.getRecvBic());
        mt721Creator.setSendBic(mt720Creator.getSendBic());
        mt721Creator.setSequenceofTotal("1/1");
        mt721Creator.setTransferringBankReference(mt720Creator.getTransferringBankReference());
        mt721Creator.setDocumentaryCreditNumber(mt720Creator.getDocumentaryCreditNumber());
        return mt721Creator;
    }


    private int splitTag(List<String> newLines, int maxTagLines, List<String> overLineList, int remainLen) {
        List<String> newLineList = new ArrayList<>();
        for (int idx2 = 0; idx2 < overLineList.size(); idx2++) {
            String line = overLineList.get(idx2);
            // 换行符长度为2
            int tmpRemainLen = remainLen - line.length() - 2;
            if (tmpRemainLen > 0 && idx2 < maxTagLines) {
                // 未超长
                newLineList.add(line);
                remainLen = tmpRemainLen;
            } else {
                // 超长了
                break;
            }
        }
        // 此处不能removeAll，只能sublist
        newLines.addAll(newLineList);
        List<String> empList = new ArrayList<>(overLineList.subList(newLineList.size(), overLineList.size()));
        overLineList.clear();
        overLineList.addAll(empList);
        return remainLen;
    }

    /**
     * mt710报文拆分
     *
     * @param mt710Creator 入参
     * @return List
     * @throws SSTFException 异常
     */
    private List<MT711Creator> split710(MT710Creator mt710Creator) throws SSTFException {
        int totalLen = mt710Creator.getAbstractMT().message().length();
        MT710 mt710 = (MT710) mt710Creator.getAbstractMT();
        Field45A field45A = mt710.getField45A();
        Field46A field46A = mt710.getField46A();
        Field47A field47A = mt710.getField47A();
        Field49H field49H = mt710.getField49H();
        Field49G field49G = mt710.getField49G();
        int lineCnt45A = 0;
        int lineCnt46A = 0;
        int lineCnt47A = 0;
        int lineCnt49H = 0;
        int lineCnt49G = 0;
        if (field45A != null) {
            lineCnt45A = field45A.getLines().size();
        }
        if (field46A != null) {
            lineCnt46A = field46A.getLines().size();
        }
        if (field47A != null) {
            lineCnt47A = field47A.getLines().size();
        }
        if (field49H != null) {
            lineCnt49H = field49H.getLines().size();
        }
        if (field49G != null) {
            lineCnt49G = field49G.getLines().size();
        }
        // 不需要拆分时直接退出
        if (totalLen < MAX_TOTAL_LEN && lineCnt45A < MT701_MAX_LINE_100 && lineCnt46A < MT701_MAX_LINE_100
                && lineCnt47A < MT701_MAX_LINE_100 && lineCnt49H < MT701_MAX_LINE_100 && lineCnt49G < MT701_MAX_LINE_100) {
            return new ArrayList<>();
        }
        List<String> lines45A = new ArrayList<>();
        List<String> lines46A = new ArrayList<>();
        List<String> lines47A = new ArrayList<>();
        List<String> lines49G = new ArrayList<>();
        List<String> lines49H = new ArrayList<>();
        if (field45A != null) {
            lines45A = field45A.getLines();
            // 先将45A清空
            mt710Creator.setDescriptionofGoodsandorServices("");
        }
        if (field46A != null) {
            lines46A = field46A.getLines();
            // 先将46A清空
            mt710Creator.setDocumentsRequired("");
        }
        if (field47A != null) {
            lines47A = field47A.getLines();
            // 先将47A清空
            mt710Creator.setAdditionalConditions("");
        }
        if (field49H != null) {
            lines49H = field49H.getLines();
            // 先将49H清空
            mt710Creator.setSpecialPaymentConditionsforBankOnly("");
        }
        if (field49G != null) {
            lines49G = field49G.getLines();
            // 先将49G清空
            mt710Creator.setSpecialPaymentConditionsforBeneficiary("");
        }
        // 计算剩余字符数
        // swift最大长度为9000
        String sndbic = mt710Creator.getSendBic();
        String rcvbic = mt710Creator.getRecvBic();
        invoke(mt710Creator, "initAbstractMT");
        mt710Creator.setSendBic(sndbic);
        mt710Creator.setRecvBic(rcvbic);
        MtResult mtResult = MtCreatorManager.packMT(mt710Creator);
        String message = mtResult.getMessage();
        int lenWithoutSplitField = message.length();
        // 剩余字符数,减25是5个tag的长度
        int remainLen = MAX_TOTAL_LEN - lenWithoutSplitField - 25;
        int maxTagLines = MT701_MAX_LINE_100;
        // 5个tag逐个拆分
        // 1.拆分45A
        List<String> newLines45A = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines45A)) {
            remainLen = this.splitTag(newLines45A, maxTagLines, lines45A, remainLen);
            // lines45A已被更新
            mt710Creator.setDescriptionofGoodsandorServices(org.apache.commons.lang.StringUtils.join(newLines45A, CR));
        }
        // 2.拆分46A
        List<String> newLines46A = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines46A)) {
            remainLen = this.splitTag(newLines46A, maxTagLines, lines46A, remainLen);
            // lines46A已被更新
            mt710Creator.setDocumentsRequired(org.apache.commons.lang.StringUtils.join(newLines46A, CR));
        }
        // 3.拆分47A
        List<String> newLines47A = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines47A)) {
            remainLen = this.splitTag(newLines47A, maxTagLines, lines47A, remainLen);
            // lines46A已被更新
            mt710Creator.setAdditionalConditions(org.apache.commons.lang.StringUtils.join(newLines47A, CR));
        }
        // 4.拆分49G
        List<String> newLines49G = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines49G)) {
            remainLen = this.splitTag(newLines49G, maxTagLines, lines49G, remainLen);
            // lines49G已被更新
            mt710Creator.setSpecialPaymentConditionsforBeneficiary(org.apache.commons.lang.StringUtils.join(newLines49G, CR));
        }
        // 5.拆分49H
        List<String> newLines49H = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lines49H)) {
            this.splitTag(newLines49H, maxTagLines, lines49H, remainLen);
            // lines49H已被更新
            mt710Creator.setSpecialPaymentConditionsforBankOnly(org.apache.commons.lang.StringUtils.join(newLines49H, CR));
        }

        // 组装711报文
        List<MT711Creator> mt711List = this.make711(mt710Creator, lines45A, lines46A, lines47A, lines49G, lines49H);
        if (mt711List.size() > MAX_SPLIT_CNT) {
            throw new SSTFException("S00001", "分片超过最大数量");
        }
        int maxCnt = mt711List.size() + 1;
        mt710Creator.setSequenceofTotal("1/" + maxCnt);
        int curCnt = 2;
        for (MT711Creator creator : mt711List) {
            creator.setSequenceofTotal(curCnt + "/" + maxCnt);
            curCnt++;
        }
        return mt711List;
    }

    private List<MT711Creator> make711(
            MT710Creator mt710Creator, List<String> lines45A, List<String> lines46A, List<String> lines47A,
            List<String> lines49G, List<String> lines49H) throws SSTFException {
        MT711Creator tmpMt701Creator = this.buildCreator711(mt710Creator);
        String message = MtCreatorManager.packMT(tmpMt701Creator).getMessage();
        int maxTagLines = MT701_MAX_LINE_100;
        // 减25是5个tag长度
        int remainMaxLen = MAX_TOTAL_LEN - message.length() - 25;
        boolean splitFlg = true;
        List<MT711Creator> overMtList = new ArrayList<>();
        while (splitFlg) {
            int remainLen = remainMaxLen;
            splitFlg = false;
            MT711Creator mt711Creator = this.buildCreator711(mt710Creator);
            // 5个tag逐个拆分
            // 1.拆分45A
            List<String> newLines45A = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines45A)) {
                splitFlg = true;
                remainLen = this.splitTag(newLines45A, maxTagLines, lines45A, remainLen);
                // lines45A已被更新
                mt711Creator.setDescriptionofGoodsandorServices(org.apache.commons.lang.StringUtils.join(newLines45A, CR));
            }
            // 2.拆分46A
            List<String> newLines46A = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines46A)) {
                splitFlg = true;
                remainLen = this.splitTag(newLines46A, maxTagLines, lines46A, remainLen);
                // lines46A已被更新
                mt711Creator.setDocumentsRequired(org.apache.commons.lang.StringUtils.join(newLines46A, CR));
            }
            // 3.拆分47A
            List<String> newLines47A = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines47A)) {
                splitFlg = true;
                remainLen = this.splitTag(newLines47A, maxTagLines, lines47A, remainLen);
                // lines47A已被更新
                mt711Creator.setAdditionalConditions(org.apache.commons.lang.StringUtils.join(newLines47A, CR));
            }
            // 4.拆分49G
            List<String> newLines49G = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines49G)) {
                splitFlg = true;
                remainLen = this.splitTag(newLines49G, maxTagLines, lines49G, remainLen);
                // lines49G已被更新
                mt711Creator.setSpecialPaymentConditionsforBeneficiary(org.apache.commons.lang.StringUtils.join(newLines49G, CR));
            }
            // 5.拆分49H
            List<String> newLines49H = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(lines49H)) {
                splitFlg = true;
                this.splitTag(newLines49H, maxTagLines, lines49H, remainLen);
                // lines49H已被更新
                mt711Creator.setSpecialPaymentConditionsforBankOnly(org.apache.commons.lang.StringUtils.join(newLines49H, CR));
            }
            if (CollectionUtils.isNotEmpty(newLines45A) || CollectionUtils.isNotEmpty(newLines46A) || CollectionUtils.isNotEmpty(newLines47A)
                    || CollectionUtils.isNotEmpty(newLines49G) || CollectionUtils.isNotEmpty(newLines49H)) {
                overMtList.add(mt711Creator);
            }
        }
        return overMtList;
    }

    private MT711Creator buildCreator711(MT710Creator mt710Creator) {
        MT711Creator mt711Creator = new MT711Creator("711");
        mt711Creator.setRecvBic(mt710Creator.getRecvBic());
        mt711Creator.setSendBic(mt710Creator.getSendBic());
        mt711Creator.setSendersReference(mt710Creator.getSendersReference());
        mt711Creator.setSequenceofTotal("1/1");
        mt711Creator.setDocumentaryCreditNumber(mt710Creator.getDocumentaryCreditNumber());
        return mt711Creator;
    }


    public List<AbsMTCreator> splitMT(AbsMTCreator absMTCreator) {
        List<AbsMTCreator> absMTCreators = new ArrayList<>();
        absMTCreators.add(absMTCreator);
        if (absMTCreator instanceof MT760Creator) {
            List<MT761Creator> mt761Creators = split760((MT760Creator) absMTCreator);
            absMTCreators.addAll(mt761Creators);
        } else if (absMTCreator instanceof MT767Creator) {
            List<MT775Creator> mt775Creators = split767((MT767Creator) absMTCreator);
            absMTCreators.addAll(mt775Creators);
        } else if (absMTCreator instanceof MT759Creator) {
            List<MT759Creator> mt759Creators = split759((MT759Creator) absMTCreator);
            absMTCreators.addAll(mt759Creators);
        } else if (absMTCreator instanceof MT700Creator) {
            List<MT701Creator> mt701Creators = split700((MT700Creator) absMTCreator);
            absMTCreators.addAll(mt701Creators);
        } else if (absMTCreator instanceof MT707Creator) {
            List<MT708Creator> mt708Creators = split707((MT707Creator) absMTCreator);
            absMTCreators.addAll(mt708Creators);
        }else if (absMTCreator instanceof MT720Creator) {
            List<MT721Creator> mt721Creators = split720((MT720Creator) absMTCreator);
            absMTCreators.addAll(mt721Creators);
        }else if (absMTCreator instanceof MT710Creator) {
            List<MT711Creator> mt711Creators = split710((MT710Creator) absMTCreator);
            absMTCreators.addAll(mt711Creators);
        }
        return absMTCreators;
    }

}
