package com.brilliance.isc.common.ptspta.service.impl;

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.*;
import com.brilliance.isc.common.api.bch.service.BchService;
import com.brilliance.isc.common.api.oit.OitApiService;
import com.brilliance.isc.common.component.CipmodComponent;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.counter.service.CounterService;
import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.glemod.service.GlemodService;
import com.brilliance.isc.common.oit.service.OitService;
import com.brilliance.isc.common.oit.vo.NoticeVo;
import com.brilliance.isc.common.ptspta.service.PtsptaService;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.vo.CommonResult;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.*;
import com.brilliance.isc.vo.*;
import com.brilliance.isc.warn.WarnService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class PtsptaServiceImpl implements PtsptaService {

    // 总行机构号
    private final String ZHDZZX = "1000";

    // 银行类型
    private final String BANKTYP = "B";

    // 海关类型
    private final String HAIGUAN = "H";

    // 中文
    private final String ZH = "CN";

    // 英文
    private final String EN = "EN";

    private final String QUANXIALV1 = "A";
    private final String QUANXIALV2 = "B";
    private final String FENHANGLV = "1";
    private final String ZHIHANGLV = "2";

    private final String ALL_PTYTYP = "B,C,F,H";

    @Autowired
    private PtsptaMapper ptsptaMapper;

    @Autowired
    private PtaMapper ptaMapper;

    @Autowired
    private BchMapper bchMapper;

    @Autowired
    private AdrMapper adrMapper;

    @Autowired
    private PtyMapper ptyMapper;

    @Autowired
    private BchService bchService;

    @Resource
    private SettleContext settleContext;

    @Resource
    private CounterService counterService;

    @Autowired
    private OitService oitService;
    @Autowired
    private OitApiService oitApiService;

    @Autowired
    private WarnService warnService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private BnlMapper bnlMapper;

    @Autowired
    private GlemodService glemodService;

    @Autowired
    private CipmodComponent cipmodComponent;

    @Autowired
    private ActMapper actMapper;

    @Override
    public ResponseSet queryPta(PtsptaQueryVo ptsptaVo) {
//        Integer offset = ptsptaVo.getPageNum(), limit=ptsptaVo.getPageSize();
        Integer offset = ptsptaVo.getPageNum(), limit = ptsptaVo.getPageSize();
        offset = offset == null ? 0 : offset;
        limit = limit == null ? 10 : limit;
        ptsptaVo.setExtkey(ptsptaVo.getExtkey().trim());
        //获取参数，ptytype,extkey,bchinr
        // TODO  || StringUtils.isEmpty(ptsptaVo.getBchinr()
        settleContext.simpleLoadSettleSession();
        Bch orgBch = SettleContext.getUserSession().getOrgBch();
        if (orgBch == null) {
            return new ResponseSet("获取当前柜员所属机构失败");
        }
        // 设置机构
        ptsptaVo.setBchinr(orgBch.getBranch());
        if (ZHDZZX.equalsIgnoreCase(orgBch.getBranch())) {
            ptsptaVo.setBchinr("");
        }
        // 入参为类型String,以逗号隔开，"B,C",可能为空，查所有
        if (StringUtils.isNotEmpty(ptsptaVo.getPtytyp())) {
            ptsptaVo.setPtytyplist(Arrays.asList(ptsptaVo.getPtytyp().split(",")));
        } else {
            ptsptaVo.setPtytyplist(Arrays.asList(ALL_PTYTYP));
        }
        // 过滤不进行机构隔离的场景：总行，海关，银行
        List<String> bchlst = new ArrayList<>();
        if (ZHDZZX.equalsIgnoreCase(orgBch.getBranch()) || HAIGUAN.equalsIgnoreCase(ptsptaVo.getPtytyp()) || BANKTYP.equalsIgnoreCase(ptsptaVo.getPtytyp())) {
            bchlst = new ArrayList<>();
        } else {
            // 针对不同机构类型进行判断
            // 全辖时获取所有下辖机构列表
            Bch accbch = bchService.getDirectAccBch(orgBch.getBranch());
            if (accbch != null) {
                List<Bch> bchlist = bchService.listAllChildSubBchByAccBch(accbch.getBranch());
                for (Bch temp: bchlist) {
                    bchlst.add(temp.getInr());
                }
            }
        }
        ptsptaVo.setBchlst(bchlst);
        PageHelper.startPage(offset, limit);
        List<Map<String, Object>> ptaInfo = ptsptaMapper.getPtaInfo(ptsptaVo);
//        PagingData pagingData = commonDAO.page4rest(statement, ptsptaVo, offset, limit);
        // 判断客户类型，为B时为正常sql，不为B时sql中需要加UPPER方法
        PageInfo pageInfo = new PageInfo(ptaInfo);
        return new ResponseSet(pageInfo);
    }

    public void buildPtsptaQueryVo(PtsptaQueryVo ptsptaVo){
        Integer offset = ptsptaVo.getPageNum(), limit = ptsptaVo.getPageSize();
        offset = offset == null ? 0 : offset;
        limit = limit == null ? 10 : limit;
        ptsptaVo.setExtkey(ptsptaVo.getExtkey().trim());
        settleContext.simpleLoadSettleSession();
        Bch orgBch = SettleContext.getUserSession().getOrgBch();
        if (orgBch == null) {
            throw new CommonServiceException("获取当前柜员所属机构失败");
        }
        // 设置机构
        ptsptaVo.setBchinr(orgBch.getBranch());
        if (ZHDZZX.equalsIgnoreCase(orgBch.getBranch())) {
            ptsptaVo.setBchinr("");
        }
        // 入参为类型String,以逗号隔开，"B,C",可能为空，查所有
        if (StringUtils.isNotEmpty(ptsptaVo.getPtytyp())) {
            ptsptaVo.setPtytyplist(Arrays.asList(ptsptaVo.getPtytyp().split(",")));
        } else {
            ptsptaVo.setPtytyplist(Arrays.asList(ALL_PTYTYP));
        }
        // 过滤不进行机构隔离的场景：总行，海关，银行
        List<String> bchlst = new ArrayList<>();
        if (ZHDZZX.equalsIgnoreCase(orgBch.getBranch()) || HAIGUAN.equalsIgnoreCase(ptsptaVo.getPtytyp()) || BANKTYP.equalsIgnoreCase(ptsptaVo.getPtytyp())) {
            bchlst = new ArrayList<>();
        } else {
            // 针对不同机构类型进行判断
            // 全辖时获取所有下辖机构列表
            Bch accbch = bchService.getDirectAccBch(orgBch.getBranch());
            if (accbch != null) {
                List<Bch> bchlist = bchService.listAllChildSubBchByAccBch(accbch.getBranch());
                for (Bch temp: bchlist) {
                    bchlst.add(temp.getInr());
                }
            }
        }
        ptsptaVo.setBchlst(bchlst);
        PageHelper.startPage(offset, limit);
    }

    @Override
    public ResponseSet queryPtas(PtsptaQueryVo ptsptaVo) {
        this.buildPtsptaQueryVo(ptsptaVo);
        List<PtaInfosVo> ptaInfos = ptsptaMapper.getPtaInfos(ptsptaVo);
        PageInfo pageInfo = new PageInfo(ptaInfos);
        return new ResponseSet(pageInfo);
    }

    @Override
    public ResponseSet queryPta5(PtsptaQueryVo ptsptaVo) {
        Integer offset = ptsptaVo.getPageNum(), limit = ptsptaVo.getPageSize();
        offset = offset == null ? 0 : offset;
        limit = limit == null ? 10 : limit;
        ptsptaVo.setFqcyjg(ptsptaVo.getFqcyjg().trim());
        PageHelper.startPage(offset, limit);
        List<PtaInfosVo> ptaInfo = bnlMapper.getBnlByBankno(ptsptaVo);
        // 判断客户类型，为B时为正常sql，不为B时sql中需要加UPPER方法
        PageInfo pageInfo = new PageInfo(ptaInfo);
        return new ResponseSet(pageInfo);
    }

    @Override
    public ResponseSet queryPtsptaInfo5(PtsptaQueryVo ptsptaVo) {
        // 对返回数据处理
        Bnl bnl = bnlMapper.getPtsptaInfo5(ptsptaVo.getFqcyjg());
        if (!MdaUtils.isEmpty(bnl) && StringUtils.isNotEmpty(bnl.getDihdig()) && bnl.getDihdig().length() > 16) {
            bnl.setDihdig(bnl.getDihdig().substring(0, 16));
        }
        return new ResponseSet(bnl);
    }

    @Override
    public ResponseSet queryPtaBic(PtsptaQueryVo ptsptaVo) {
        Integer offset = ptsptaVo.getPageNum(), limit = ptsptaVo.getPageSize();
        offset = offset == null ? 0 : offset;
        limit = limit == null ? 10 : limit;
        ptsptaVo.setExtkey(ptsptaVo.getExtkey().trim());
        PageHelper.startPage(offset, limit);
        List<PtaInfosVo> ptaInfo = ptsptaMapper.queryPtaBicByExtkey(ptsptaVo);
        PageInfo pageInfo = new PageInfo(ptaInfo);
        return new ResponseSet(pageInfo);
    }

    @Override
    public ResponseSet queryPtsptaBic(PtsptaQueryVo ptsptaVo) {
        if (StringUtils.isEmpty(ptsptaVo.getInr())) {
            return new ResponseSet("400", "暂无数据");
        }
        // 对返回数据进行拼接
        Map<String, String> ptsptaInfo = ptsptaMapper.queryPtsptaBicByInr(ptsptaVo.getInr());
        // 进行拼接回显
        // 回显数据：中英文名址，中英文拼接名址
        // 中文名称拼接
        NoticeVo noticeVo=new NoticeVo();
        noticeVo.setObjtyp("pty");
        noticeVo.setObjinr(ptsptaInfo.get("ptyinr"));
        noticeVo.setPath(com.brilliance.isc.utils.StringUtils.stringAppend("ptspta.",ptsptaVo.getRol()));
        oitApiService.registOit(noticeVo);
        // 构筑ptsptavo以及adr，设置ptytyp
        PtsptaVo ptsptaVo1 = new PtsptaVo();
        Adr adr = new Adr();
        adr.setNam1(ptsptaInfo.get("adrnam1"));
        adr.setNam2(ptsptaInfo.get("adrnam2"));
        adr.setStr1(ptsptaInfo.get("adrstr1"));
        adr.setStr2(ptsptaInfo.get("adrstr2"));
        adr.setAdr1(ptsptaInfo.get("adr1"));
        adr.setAdr2(ptsptaInfo.get("adr2"));
        adr.setAdr3(ptsptaInfo.get("adr3"));
        adr.setAdr4(ptsptaInfo.get("adr4"));
        adr.setLoczip(ptsptaInfo.get("loczip"));
        adr.setLoctxt(ptsptaInfo.get("loctxt"));
        ptsptaVo1.setAdr(adr);
        ptsptaVo1.setPtytyp(ptsptaInfo.get("ptytyp"));
        ptsptaVo1.setPtanam1(ptsptaInfo.get("nam1"));
        ptsptaVo1.setNam(ptsptaInfo.get("nam"));
        Map<String, String> enMap = ptsptaToolComponent.ptsModGetAdrBlk501(ptsptaVo1);
        Map<String, String> cnMap = ptsptaToolComponent.ptsModGetAdrBlkForCN(ptsptaVo1);
        ptsptaInfo.put("cnnam", cnMap.get("cnnam"));
        ptsptaInfo.put("cnadr", cnMap.get("cnadr"));
        ptsptaInfo.put("ennam", enMap.get("ennam"));
        ptsptaInfo.put("enadr", enMap.get("enadr"));
        ptsptaInfo.put("cnnamadr", cnMap.get("cnnamadr"));
        ptsptaInfo.put("ennamadr", enMap.get("ennamadr"));
        return new ResponseSet(ptsptaInfo);
    }

    public PtsptaVo queryPtsptaBic2(String ptainr) {
        PtsptaVo ptspta = new PtsptaVo();
        ptspta.reset();
        if (StringUtils.isEmpty(ptainr)) {
            return null;
        }
        // 对返回数据进行拼接
        Map<String, String> ptsptaInfo = ptsptaMapper.queryPtsptaBicByInr(ptainr);
        // 进行拼接回显
        // 回显数据：中英文名址，中英文拼接名址
        // 中文名称拼接
        // 构筑ptsptavo以及adr，设置ptytyp
        PtsptaVo ptsptaVo1 = new PtsptaVo();
        Adr adr = new Adr();
        adr.setNam1(ptsptaInfo.get("adrnam1"));
        adr.setNam2(ptsptaInfo.get("adrnam2"));
        adr.setStr1(ptsptaInfo.get("adrstr1"));
        adr.setStr2(ptsptaInfo.get("adrstr2"));
        adr.setAdr1(ptsptaInfo.get("adr1"));
        adr.setAdr2(ptsptaInfo.get("adr2"));
        adr.setAdr3(ptsptaInfo.get("adr3"));
        adr.setAdr4(ptsptaInfo.get("adr4"));
        adr.setLoczip(ptsptaInfo.get("loczip"));
        adr.setLoctxt(ptsptaInfo.get("loctxt"));
        ptsptaVo1.setAdr(adr);
        ptsptaVo1.setPtytyp(ptsptaInfo.get("ptytyp"));
        ptsptaVo1.setPtanam1(ptsptaInfo.get("nam1"));
        ptsptaVo1.setNam(ptsptaInfo.get("nam"));
        Map<String, String> enMap = ptsptaToolComponent.ptsModGetAdrBlk501(ptsptaVo1);
        Map<String, String> cnMap = ptsptaToolComponent.ptsModGetAdrBlkForCN(ptsptaVo1);
        ptsptaInfo.put("cnnam", cnMap.get("cnnam"));
        ptsptaInfo.put("cnadr", cnMap.get("cnadr"));
        ptsptaInfo.put("ennam", enMap.get("ennam"));
        ptsptaInfo.put("enadr", enMap.get("enadr"));
        ptsptaInfo.put("cnnamadr", cnMap.get("cnnamadr"));
        ptsptaInfo.put("ennamadr", enMap.get("ennamadr"));

        ptspta.getPts().setExtkey(ptsptaInfo.get("objkey"));
        ptspta.getPts().setPtainr(ptsptaInfo.get("ptainr"));
        ptspta.getPts().setPtyinr(ptsptaInfo.get("ptyinr"));
        ptspta.getPts().setNam(ptsptaInfo.get("nam"));
        ptspta.getPts().setCnnam(ptsptaInfo.get("cnnam"));
        ptspta.getPts().setCnadr(ptsptaInfo.get("cnadr"));
        ptspta.getPts().setEnnam(ptsptaInfo.get("ennam"));
        ptspta.getPts().setEnadr(ptsptaInfo.get("enadr"));
        return ptspta;
    }

    /**
     * @description: Ptap12和Ptap13当事人弹窗回填，返回页面使用信息
     */
    @Override
    public ResponseSet queryPtsptaInfo12(PtsptaQueryVo ptsptaVo) {
        //settleContext.simpleLoadSettleSession();
        if (StringUtils.isEmpty(ptsptaVo.getInr())) {
            return new ResponseSet("400", "暂无数据");
        }
        // 对返回数据进行拼接
        Map<String, Object> ptsptaInfoResult =  new HashMap<>();
        Map<String, String> ptsptaInfo = ptsptaMapper.getPtsptaInfo(ptsptaVo.getInr());
        NoticeVo noticeVo=new NoticeVo();
        noticeVo.setObjtyp("pty");
        noticeVo.setObjinr(ptsptaInfo.get("ptyinr"));
        noticeVo.setPath(com.brilliance.isc.utils.StringUtils.stringAppend("ptspta.",ptsptaVo.getRol()));
        oitApiService.registOit(noticeVo);
        // 构筑ptsptavo以及adr，设置ptytyp
        PtsptaVo ptsptaVo1 = new PtsptaVo();
        Adr adr = new Adr();
        adr.setNam1(ptsptaInfo.get("adrnam1"));
        adr.setNam2(ptsptaInfo.get("adrnam2"));
        adr.setStr1(ptsptaInfo.get("adrstr1"));
        adr.setStr2(ptsptaInfo.get("adrstr2"));
        adr.setAdr1(ptsptaInfo.get("adr1"));
        adr.setAdr2(ptsptaInfo.get("adr2"));
        adr.setAdr3(ptsptaInfo.get("adr3"));
        adr.setAdr4(ptsptaInfo.get("adr4"));
        adr.setLoczip(ptsptaInfo.get("loczip"));
        adr.setLoctxt(ptsptaInfo.get("loctxt"));
        ptsptaVo1.setAdr(adr);
        ptsptaVo1.setPtytyp(ptsptaInfo.get("ptytyp"));
        ptsptaVo1.setPtanam1(ptsptaInfo.get("nam1"));
        ptsptaVo1.setNam(ptsptaInfo.get("nam"));
        Map<String, String> enMap = ptsptaToolComponent.ptsModGetAdrBlk501ForPtap12(ptsptaVo1);
        Map<String, String> cnMap = ptsptaToolComponent.ptsModGetAdrBlkForCN(ptsptaVo1);
        ptsptaInfoResult.put("cnnam", cnMap.get("cnnam"));
        ptsptaInfoResult.put("cnadr", cnMap.get("cnadr"));
        ptsptaInfoResult.put("ennam", enMap.get("ennam"));
        ptsptaInfoResult.put("enadr", enMap.get("enadr"));
        ptsptaInfoResult.put("cnnamadr", cnMap.get("cnnamadr"));
        ptsptaInfoResult.put("ennamadr", enMap.get("ennamadr"));
        ptsptaInfoResult.put("ptainr", ptsptaInfo.get("ptainr"));
        ptsptaInfoResult.put("ptyinr", ptsptaInfo.get("ptyinr"));
        String ptyinr = ptsptaInfo.get("ptyinr");
        String cur = ptsptaVo.getCur();
        BigDecimal amt = ptsptaVo.getAmt();
        String orcact = "";
//        if (StringUtils.isNotEmpty(ptyinr) && StringUtils.isNotEmpty(cur)){
//            String actVal = glemodService.getAvailAcc("LO", cur, ptyinr, amt);
//            if (StringUtils.isNotEmpty(actVal)) {
//                Arrays.stream(actVal.split("[\\r\\n]{1,2}")).forEach(account -> {
//                    actSet.add(account);
//                });
//            }
//        }
//        //取actSet集合第一个账号作为账号
//        if(!CollectionUtils.isEmpty(actSet)){
//            Iterator<String> iterator = actSet.iterator();
//            if (iterator.hasNext()){
//                orcact = iterator.next();
//            }
//        }
        ptsptaInfoResult.put("orcact", orcact);
        //增加查询客户下所有账号
        //当前机构为自贸区时（BCH.ZMQFLG == 'X'）才出带NRA、EF、FT、ZMQ的账号
        List<Act> actList = new ArrayList<>();
        if (StringUtils.isNotEmpty(ptyinr)){
            String actVal = glemodService.getAvailAcc("LO", cur, ptyinr, amt);
            if (StringUtils.isNotEmpty(actVal)) {
                Arrays.stream(actVal.split("[\\r\\n]{1,2}")).forEach(account -> {
                    List<Act> acts = actMapper.getActListByExtkey(account);
                    if (acts != null) {
                        actList.addAll(acts);
                    }
                });
            }
        }
        ptsptaInfoResult.put("actList", actList);
        return new ResponseSet(ptsptaInfoResult);
    }

    //Ptap12和Ptap13输入账号显示弹窗
    @Override
    public ResponseSet queryPtaInfoByOrcact(PtsptaQueryVo ptsptaVo) {
        String orcact = ptsptaVo.getOrcact();
        List<String> actList = glemodService.filterAct(Arrays.asList(new String[]{orcact}));
        if (actList.size() == 0){
            return new ResponseSet("账号不属于该机构");
        }
        PtsptaInfoVo ptsptaInfoVo = ptsptaMapper.selectExtkeyByOrcact(ptsptaVo);
        if (MdaUtils.isEmpty(ptsptaInfoVo)){
            return new ResponseSet("该账号国结未维护,请先维护!(内部户除外)");
        }else if (!MdaUtils.isEmpty(ptsptaInfoVo) ){
            if(!"LO".equals(ptsptaInfoVo.getTyp())){
                return new ResponseSet("该账号类型有误!");
            }
        }
        ptsptaVo.setExtkey(ptsptaInfoVo.getExtkey());
        this.buildPtsptaQueryVo(ptsptaVo);
        List<Map<String, Object>> ptaInfo = ptsptaMapper.getPtaInfo(ptsptaVo);
        if (ptaInfo != null){
            ptaInfo.forEach(item -> item.put("orcact", ptsptaVo.getOrcact()));
        }
        PageInfo pageInfo = new PageInfo(ptaInfo);
        return new ResponseSet(pageInfo);
    }

    @Override
    public ResponseSet queryBnm(BnmVo bnmVo) {
//        Integer offset = bnmVo.getPageNum();
//        offset = offset == null ? 0 : offset;
//        Integer limit = bnmVo.getPageSize();
//        limit = limit == null ? 10 : limit;
//        PageHelper.startPage(offset, limit);
        List<BnmVo> bnmList = cipmodComponent.getCyhhAndCyzwmc(bnmVo);
//        PageInfo nidInfo = new PageInfo(bnmList);
        return new ResponseSet(bnmList);
    }

    /**
     * @description: Ptap3组件当事人弹窗回填，返回页面使用信息
     */
    @Override
    public ResponseSet queryPtsptaInfo3(PtsptaQueryVo ptsptaVo) {
        if (StringUtils.isEmpty(ptsptaVo.getInr())) {
            return new ResponseSet("400", "暂无数据");
        }
        // 对返回数据处理
        PtsptaInfoVo ptsptaInfo = ptsptaMapper.getPtsptaInfo3(ptsptaVo.getInr());
        String temp = "";
        //设置中文地址
        if (StringUtils.isEmpty(ptsptaInfo.getCnadr())){
            if (StringUtils.isNotEmpty(ptsptaInfo.getAdr1())) {
                temp += ptsptaInfo.getAdr1();
            }
            if (StringUtils.isNotEmpty(ptsptaInfo.getAdr2())) {
                if (StringUtils.isNotEmpty(temp))
                    temp += "\n";
                temp += ptsptaInfo.getAdr2();
            }
            if (StringUtils.isNotEmpty(ptsptaInfo.getAdr3())) {
                if (StringUtils.isNotEmpty(temp))
                    temp += "\n";
                temp += ptsptaInfo.getAdr3();
            }
            ptsptaInfo.setCnadr(temp);
        }
        if (StringUtils.isEmpty(ptsptaInfo.getCnnam())){
            ptsptaInfo.setCnnam(ptsptaInfo.getPtanam1());
        }
        // 进行拼接回显
        // 回显数据：中英文名址，中英文拼接名址
//        registOit(ptsptaInfo.get("ptyinr"), "PTY", ptsptaVo.getRol() + "_PTS_EXTKEY");
        NoticeVo noticeVo=new NoticeVo();
        noticeVo.setObjtyp("pty");
        noticeVo.setObjinr(ptsptaInfo.getPtyinr());
        noticeVo.setPath(com.brilliance.isc.utils.StringUtils.stringAppend("ptspta.",ptsptaVo.getRol()));
        oitApiService.registOit(noticeVo);
        return new ResponseSet(ptsptaInfo);
    }

    /**
     * @description: 当事人弹窗回填，返回页面使用信息
     */
    @Override
    public ResponseSet queryPtsptaInfo(PtsptaQueryVo ptsptaVo) {
        if (StringUtils.isEmpty(ptsptaVo.getInr())) {
            return new ResponseSet("400", "暂无数据");
        }
        // 对返回数据进行拼接
        Map<String, String> ptsptaInfo = ptsptaMapper.getPtsptaInfo(ptsptaVo.getInr());
        // 进行拼接回显
        // 回显数据：中英文名址，中英文拼接名址
        // 中文名称拼接
//        registOit(ptsptaInfo.get("ptyinr"), "PTY", ptsptaVo.getRol() + "_PTS_EXTKEY");
        NoticeVo noticeVo=new NoticeVo();
        noticeVo.setObjtyp("pty");
        noticeVo.setObjinr(ptsptaInfo.get("ptyinr"));
        noticeVo.setPath(com.brilliance.isc.utils.StringUtils.stringAppend("ptspta.",ptsptaVo.getRol()));
        oitApiService.registOit(noticeVo);
        // 构筑ptsptavo以及adr，设置ptytyp
        PtsptaVo ptsptaVo1 = new PtsptaVo();
        Adr adr = new Adr();
        adr.setNam1(ptsptaInfo.get("adrnam1"));
        adr.setNam2(ptsptaInfo.get("adrnam2"));
        adr.setStr1(ptsptaInfo.get("adrstr1"));
        adr.setStr2(ptsptaInfo.get("adrstr2"));
        adr.setAdr1(ptsptaInfo.get("adr1"));
        adr.setAdr2(ptsptaInfo.get("adr2"));
        adr.setAdr3(ptsptaInfo.get("adr3"));
        adr.setAdr4(ptsptaInfo.get("adr4"));
        adr.setLoczip(ptsptaInfo.get("loczip"));
        adr.setLoctxt(ptsptaInfo.get("loctxt"));
        ptsptaVo1.setAdr(adr);
        ptsptaVo1.setPtytyp(ptsptaInfo.get("ptytyp"));
        ptsptaVo1.setPtanam1(ptsptaInfo.get("nam1"));
        ptsptaVo1.setNam(ptsptaInfo.get("nam"));
        Map<String, String> enMap = ptsptaToolComponent.ptsModGetAdrBlk501(ptsptaVo1);
        Map<String, String> cnMap = ptsptaToolComponent.ptsModGetAdrBlkForCN(ptsptaVo1);
        ptsptaInfo.put("cnnam", cnMap.get("cnnam"));
        ptsptaInfo.put("cnadr", cnMap.get("cnadr"));
        ptsptaInfo.put("ennam", enMap.get("ennam"));
        ptsptaInfo.put("enadr", enMap.get("enadr"));
        ptsptaInfo.put("cnnamadr", cnMap.get("cnnamadr"));
        ptsptaInfo.put("ennamadr", enMap.get("ennamadr"));
        return new ResponseSet(ptsptaInfo);
    }

    /**
     * 生成临时客户
     *
     * @return ResponseSet
     */
    @Override
    public Map<String,Object> ptsTmpAdrAddAuto(PtsptaVo ptsptaVo) {
        Map<String,Object> result = new HashMap<>();
        // TODO 触发pty,pta,adr的更新操作
        // TODO 中文状态下的英文栏取值
        // 对adr进行赋值
        Adr adr = ptsTmpAdrDefault(ptsptaVo);
        // 插入adr
        Pty pty = new Pty();
        Pta pta = new Pta();
        // 判断ptainr是否有值，如果有值则进行更新操作，否则正常生成临时名址
        // 判断是否有值
        if (StringUtils.isEmpty(ptsptaVo.getAdrblk()) && StringUtils.isEmpty(ptsptaVo.getNamelc())) {
            result.put("errorMsg","名称不能为空");
        }
        if (StringUtils.isNotEmpty(ptsptaVo.getPtainr())) {
            // 判断是否为名址分开
            // 更新pta
            pta = ptaMapper.selectByInr(ptsptaVo.getPtainr());
            pty = ptyMapper.selectByPtyinr(pta.getPtyinr());
            if (isCnNameAdress(ptsptaVo)) {
                if (ptsptaVo.getNamelc().length() > 150) {
                    pta.setNam1(ptsptaVo.getNamelc().substring(0,150));
                } else {
                    pta.setNam1(ptsptaVo.getNamelc());
                }
            } else {
                pta.setNam(adr.getNam1());
            }
            ptaMapper.updateByPrimaryKey(pta);
            // 只有客户号不以Temp开头时会做pty的更新操作
            if (!pty.getExtkey().startsWith("Temp")) {
                if (isCnNameAdress(ptsptaVo)) {
                    if (ptsptaVo.getNamelc().length() > 150) {
                        pty.setNam1(ptsptaVo.getNamelc().substring(0,150));
                    } else {
                        pty.setNam1(ptsptaVo.getNamelc());
                    }
                } else {
                    pty.setNam(adr.getNam1());
                }
                ptyMapper.updateByPrimaryKey(pty);
            }
            adr.setInr(pta.getObjinr());
            adr.setExtkey(pta.getObjkey());
            adrMapper.updateByPrimaryKey(adr);
            ptsptaVo.setAdr(adr);
        } else {
            Bch orgBch = SettleContext.getUserSession().getOrgBch();
            Bch bck = SettleContext.getUserSession().getBch();
//        if (ptsptaVo.getAdr() == null) {
            adr.setInr(counterService.dbCounterFormat("ADR"));
            adrMapper.insert(adr);
            ptsptaVo.setAdr(adr);
//        }
//        else {
//            adr.setInr(ptsptaVo.getAdr().getInr());
//            adrMapper.updateByPrimaryKey(adr);
//        }
            // 对pty赋值
            if (isCnNameAdress(ptsptaVo)) {
                if (ptsptaVo.getNamelc().length() > 150) {
                    pty.setNam1(ptsptaVo.getNamelc().substring(0,150));
                } else {
                    pty.setNam1(ptsptaVo.getNamelc());
                }
            } else {
                pty.setNam(adr.getNam1());
            }
            pty.setAdrsta("T");
            pty.setExtkey(adr.getExtkey());
            if ("B".equalsIgnoreCase(ptsptaVo.getTypflg())) {
                pty.setPtytyp("B");
            } else {
                pty.setPtytyp("F");
            }
            if (ptsptaVo.isFlg()) {
                pty.setInr(counterService.dbCounterFormat("PTY"));
                ptyMapper.insert(pty);
            } else {
                // 点击否的时候执行的代码
                // 根据extkey查询pty
                List<String> ptyList = ptyMapper.selectInrListByExtkey(adr.getExtkey());
                // 不存在临时名址的ptyinr时，自动插入
                if (CollectionUtils.isEmpty(ptyList)) {
                    pty.setInr(counterService.dbCounterFormat("PTY"));
                    ptyMapper.insert(pty);
                } else {
                    pty.setInr(ptyList.get(0));
                }
//            }
            }
//        if (workPtspta.getPta() == null) {
            // 根据pty生成pta数据
            pta.setInr(counterService.dbCounterFormat("PTA"));
            if (StringUtils.isEmpty(pty.getInr())) {
                pta.setPtyinr(ptyMapper.selectInrByExtkey(pty.getExtkey()));
            } else {
                pta.setPtyinr(pty.getInr());
            }
            pta.setPtytyp(pty.getPtytyp());
            pta.setPtyextkey(pty.getExtkey());
            pta.setAdrsta(pty.getAdrsta());
            // 根据adr生成pta数据
            pta.setObjtyp("ADR");
            pta.setObjinr(adr.getInr());
            pta.setObjkey(adr.getExtkey());
            if (isCnNameAdress(ptsptaVo)) {
                if (ptsptaVo.getNamelc().length() > 150) {
                    pta.setNam1(ptsptaVo.getNamelc().substring(0,150));
                } else {
                    pta.setNam1(ptsptaVo.getNamelc());
                }
            } else {
                pta.setNam(adr.getNam1());
            }
            pta.setBic(adr.getBic());
            pta.setTid(adr.getTid());
            // 填充剩余字段
            pta.setUsg("MAA");
            if (ZHDZZX.equalsIgnoreCase(orgBch.getBranch())) {
                String zhInr = bchService.getByBranch(ZHDZZX).getInr();
                pta.setBchkeyinr(zhInr);
                pta.setBranchinr(zhInr);
            } else {
                pta.setBchkeyinr(bck.getInr());
                pta.setBranchinr(bck.getInr());
            }
            ptaMapper.insert(pta);
//        } else {
//            workPtspta.getPta().setInr(ptsptaVo.getPtainr());
//            ptaMapper.updateByPrimaryKeySelective(workPtspta.getPta());
//        }
        }
        // 对返回进行处理
        ptsptaVo.setExtkey(adr.getExtkey());
        ptsptaVo.setPtainr(pta.getInr());
        ptsptaVo.setPtyinr(pty.getInr());
        if (StringUtils.isNotEmpty(pta.getNam())) {
            ptsptaVo.setNam(pta.getNam());
        } else if (StringUtils.isNotEmpty(pta.getNam1())) {
            ptsptaVo.setNam(pta.getNam1());
        }
        ptsptaVo.setPtanam1(pta.getNam1());
        Pts pts = new Pts();
        ptsptaVo.setPts(pts);
        pts.setPtainr(ptsptaVo.getPtainr());
        pts.setPtyinr(ptsptaVo.getPtyinr());
        ptsptaVo.setPtytyp(pta.getPtytyp());
        if (isCnNameAdress(ptsptaVo)) {
            Map<String, String> cnMaps = ptsptaToolComponent.ptsModGetAdrBlkForCN(ptsptaVo);
            if (cnMaps != null) {
                pts.setCnnam(cnMaps.get("cnnam"));
                pts.setCnadr(cnMaps.get("cnadr"));
                pts.setAdrblk(cnMaps.get("cnnamadr"));
            }
        } else {
            Map<String, String> enMaps = ptsptaToolComponent.ptsModGetAdrBlk501(ptsptaVo);
            if (enMaps != null) {
                pts.setEnnam(enMaps.get("ennam"));
                pts.setEnadr(enMaps.get("enadr"));
                pts.setAdrblk(enMaps.get("ennamadr"));
            }
        }
        result.put("ptsptaVo",ptsptaVo);
        return result;
    }

    /**
     * Ptap13组件生成临时客户
     *
     * @return ResponseSet
     */

    @Override
    public Map<String,Object> ptsTmpAdrAdd(PtsptaVo ptsptaVo) {
        Map<String,Object> result = new HashMap<>();
        // 对adr进行赋值
        Adr adr = ptsTmpAdrDefault1(ptsptaVo);
        // 插入adr
        Pty pty = new Pty();
        Pta pta = new Pta();
        // 判断ptainr是否有值，如果有值则进行更新操作，否则正常生成临时名址
        // 判断是否有值
        if (ZH.equalsIgnoreCase(ptsptaVo.getLanguage()) && StringUtils.isEmpty(ptsptaVo.getCnnam())) {
            result.put("errorMsg","中文名称不能为空");
        }else if (EN.equalsIgnoreCase(ptsptaVo.getLanguage()) && StringUtils.isEmpty(ptsptaVo.getEnnam())){
            result.put("errorMsg","英文名称不能为空");
        }
        if (StringUtils.isNotEmpty(ptsptaVo.getPtainr())) {
            // 判断是否为名址分开
            // 更新pta
            pta = ptaMapper.selectByInr(ptsptaVo.getPtainr());
            pty = ptyMapper.selectByPtyinr(pta.getPtyinr());
            if (isCnNameAdress(ptsptaVo)) {
                if (ptsptaVo.getCnnam().length() > 150) {
                    pta.setNam1(ptsptaVo.getCnnam().substring(0,150));
                } else {
                    pta.setNam1(ptsptaVo.getCnnam());
                }
            } else {
                pta.setNam(adr.getNam1());
            }
            ptaMapper.updateByPrimaryKey(pta);
            // 只有客户号不以Temp开头时会做pty的更新操作
            if (!pty.getExtkey().startsWith("Temp")) {
                if (isCnNameAdress(ptsptaVo)) {
                    if (ptsptaVo.getCnnam().length() > 150) {
                        pty.setNam1(ptsptaVo.getCnnam().substring(0,150));
                    } else {
                        pty.setNam1(ptsptaVo.getCnnam());
                    }
                } else {
                    pty.setNam(adr.getNam1());
                }
                ptyMapper.updateByPrimaryKey(pty);
            }
            adr.setInr(pta.getObjinr());
            adr.setExtkey(pta.getObjkey());
            adrMapper.updateByPrimaryKey(adr);
            ptsptaVo.setAdr(adr);
        } else {
            Bch orgBch = SettleContext.getUserSession().getOrgBch();
            Bch bck = SettleContext.getUserSession().getBch();
            adr.setInr(counterService.dbCounterFormat("ADR"));
            adrMapper.insert(adr);
            ptsptaVo.setAdr(adr);
            // 对pty赋值
            if (isCnNameAdress(ptsptaVo)) {
                if (ptsptaVo.getCnnam().length() > 150) {
                    pty.setNam1(ptsptaVo.getCnnam().substring(0,150));
                } else {
                    pty.setNam1(ptsptaVo.getCnnam());
                }
            } else {
                pty.setNam(adr.getNam1());
            }
            pty.setAdrsta("T");
            pty.setExtkey(adr.getExtkey());
            if ("B".equalsIgnoreCase(ptsptaVo.getTypflg())) {
                pty.setPtytyp("B");
            } else {
                pty.setPtytyp("F");
            }
            if (ptsptaVo.isFlg()) {
                pty.setInr(counterService.dbCounterFormat("PTY"));
                ptyMapper.insert(pty);
            } else {
                // 点击否的时候执行的代码
                // 根据extkey查询pty
                List<String> ptyList = ptyMapper.selectInrListByExtkey(adr.getExtkey());
                // 不存在临时名址的ptyinr时，自动插入
                if (CollectionUtils.isEmpty(ptyList)) {
                    pty.setInr(counterService.dbCounterFormat("PTY"));
                    ptyMapper.insert(pty);
                } else {
                    pty.setInr(ptyList.get(0));
                }
            }
            // 根据pty生成pta数据
            pta.setInr(counterService.dbCounterFormat("PTA"));
            if (StringUtils.isEmpty(pty.getInr())) {
                pta.setPtyinr(ptyMapper.selectInrByExtkey(pty.getExtkey()));
            } else {
                pta.setPtyinr(pty.getInr());
            }
            pta.setPtytyp(pty.getPtytyp());
            pta.setPtyextkey(pty.getExtkey());
            pta.setAdrsta(pty.getAdrsta());
            // 根据adr生成pta数据
            pta.setObjtyp("ADR");
            pta.setObjinr(adr.getInr());
            pta.setObjkey(adr.getExtkey());
            if (isCnNameAdress(ptsptaVo)) {
                if (ptsptaVo.getCnnam().length() > 150) {
                    pta.setNam1(ptsptaVo.getCnnam().substring(0,150));
                } else {
                    pta.setNam1(ptsptaVo.getCnnam());
                }
            } else {
                pta.setNam(adr.getNam1());
            }
            pta.setBic(adr.getBic());
            pta.setTid(adr.getTid());
            // 填充剩余字段
            pta.setUsg("MAA");
            if (ZHDZZX.equalsIgnoreCase(orgBch.getBranch())) {
                String zhInr = bchService.getByBranch(ZHDZZX).getInr();
                pta.setBchkeyinr(zhInr);
                pta.setBranchinr(zhInr);
            } else {
                pta.setBchkeyinr(bck.getInr());
                pta.setBranchinr(bck.getInr());
            }
            ptaMapper.insert(pta);
        }
        // 对返回进行处理
        ptsptaVo.setExtkey(adr.getExtkey());
        ptsptaVo.setPtainr(pta.getInr());
        ptsptaVo.setPtyinr(pty.getInr());
        if (StringUtils.isNotEmpty(pta.getNam())) {
            ptsptaVo.setNam(pta.getNam());
        } else if (StringUtils.isNotEmpty(pta.getNam1())) {
            ptsptaVo.setNam(pta.getNam1());
        }
        ptsptaVo.setPtanam1(pta.getNam1());
        Pts pts = new Pts();
        ptsptaVo.setPts(pts);
        pts.setPtainr(ptsptaVo.getPtainr());
        pts.setPtyinr(ptsptaVo.getPtyinr());
        ptsptaVo.setPtytyp(pta.getPtytyp());
        Map<String, String> cnMaps1 = ptsptaToolComponent.ptsModGetAdrBlkForCN(ptsptaVo);
        if (cnMaps1 != null) {
            pts.setCnnam(cnMaps1.get("cnnam"));
            pts.setCnadr(cnMaps1.get("cnadr"));
            pts.setAdrblk(cnMaps1.get("cnnamadr"));
        }
        Map<String, String> enMaps2 = ptsptaToolComponent.ptsModGetAdrBlk501(ptsptaVo);
        if (enMaps2 != null) {
            pts.setEnnam(enMaps2.get("ennam"));
            pts.setEnadr(enMaps2.get("enadr"));
            ptsptaVo.setAdrelc(enMaps2.get("ennamadr"));
        }
        result.put("ptsptaVo",ptsptaVo);
        return result;
    }

    @Override
    public ResponseSet getPtsptaInfo(PtsptaQueryVo ptsptaVo) {
        if (StringUtils.isEmpty(ptsptaVo.getBchbranch())) {
            return new ResponseSet("400", "请求参数为空");
        }
        Bch directAccBch = bchService.getDirectAccBch(ptsptaVo.getBchbranch());
        if (directAccBch == null) {
            return new ResponseSet("400", "响应结果为空");
        }
        // 对返回数据进行拼接
        Map<String, String> ptsptaInfo = ptsptaMapper.getPtsptaInfoByExtkey(directAccBch.getSwfcod());
        // 进行拼接回显
        // 回显数据：中英文名址，中英文拼接名址
        // 中文名称拼接
//        registOit(ptsptaInfo.get("ptyinr"), "PTY", ptsptaVo.getRol() + "_PTS_EXTKEY");
        NoticeVo noticeVo=new NoticeVo();
        noticeVo.setObjtyp("pty");
        noticeVo.setObjinr(ptsptaInfo.get("ptyinr"));
        oitApiService.registOit(noticeVo);
        PtsptaVo ptsptaVo1 = new PtsptaVo();
        Adr adr = new Adr();
        adr.setNam1(ptsptaInfo.get("adrnam1"));
        adr.setNam2(ptsptaInfo.get("adrnam2"));
        adr.setStr1(ptsptaInfo.get("adrstr1"));
        adr.setStr2(ptsptaInfo.get("adrstr2"));
        adr.setAdr1(ptsptaInfo.get("adr1"));
        adr.setAdr2(ptsptaInfo.get("adr2"));
        adr.setAdr3(ptsptaInfo.get("adr3"));
        adr.setAdr4(ptsptaInfo.get("adr4"));
        adr.setLoczip(ptsptaInfo.get("loczip"));
        adr.setLoctxt(ptsptaInfo.get("loctxt"));
        ptsptaVo1.setAdr(adr);
        ptsptaVo1.setPtytyp(ptsptaInfo.get("ptytyp"));
        Map<String, String> enMap = ptsptaToolComponent.ptsModGetAdrBlk501(ptsptaVo1);
        Map<String, String> cnMap = ptsptaToolComponent.ptsModGetAdrBlkForCN(ptsptaVo1);
        ptsptaInfo.put("cnnam", cnMap.get("cnnam"));
        ptsptaInfo.put("cnadr", cnMap.get("cnadr"));
        ptsptaInfo.put("ennam", enMap.get("ennam"));
        ptsptaInfo.put("enadr", enMap.get("enadr"));
        ptsptaInfo.put("cnnamadr", cnMap.get("cnnamadr"));
        ptsptaInfo.put("ennamadr", enMap.get("ennamadr"));
        return new ResponseSet(ptsptaInfo);
    }

    @Override
    public CommonResult queryPtyByExk(PtsptaQueryVo ptsptaVo) {
        settleContext.simpleLoadSettleSession();
        Bch orgBch = SettleContext.getUserSession().getOrgBch();
        List<Bch> bchList = bchService.listAllChildAndItselfBch(orgBch.getBranch());
        PageHelper.startPage(ptsptaVo.getPageNum(), ptsptaVo.getPageSize());
        List<Pty> ptyList = ptsptaMapper.queryPtyByExtkey(ptsptaVo.getExtkey(), bchList);
        CommonResult result = CommonResult.restPage(ptyList);
        return result;
    }

    @Override
    public ResponseSet queryPtyInfos(PtsptaQueryVo ptsptaVo) {
        List<PtyInfosVo> ptyInfosVos = ptsptaMapper.queryCusInfoByExtkey(ptsptaVo);
        if (MdaUtils.isEmpty(ptyInfosVos)){
            return new ResponseSet("暂无数据");
        }
        PageInfo pageInfo = new PageInfo(ptyInfosVos);
        return new ResponseSet(pageInfo);
    }

    /**
     * 生成临时地址
     *
     * @param ptsptaVo 当前执行的数据容器
     */
    private Adr ptsTmpAdrDefault(PtsptaVo ptsptaVo) {
        String rol = ptsptaVo.getRol();
        Adr temp = new Adr();
        // 生成临时地址的extkey （更新不生成新的编号）
        if (StringUtils.isEmpty(ptsptaVo.getPtainr())) {
            if (ptsptaVo.isFlg()) {
                temp.setExtkey("9999" + counterService.dbCounterFormat("TMPADR", 8));
            } else {
                temp.setExtkey("Temp" + rol.toUpperCase());
            }
        } else {
            temp = adrMapper.selectByPtaInr(ptsptaVo.getPtainr());
        }
        /*if (ptsptaVo.getAdrblk() != null) {
            String[] splitline1 =  ptsptaVo.getAdrblk().split("\\n");
            if (splitline1[0].length() > 35) {
                temp.setNam(splitline1[0].substring(0, 35));
            } else {
                temp.setNam(splitline1[0]);
            }
        }*/

        if (isCnNameAdress(ptsptaVo)) {
            //中文地址
            if (StringUtils.isNotEmpty(ptsptaVo.getAdrblk())) {
                //名称地址没有拆
                String adrblk = ptsptaVo.getAdrblk();
                adrblk = StringUtils.convertBlock(adrblk, 100, 35);
                String[] adrblks = adrblk.split("\r?\n");
                if (adrblks.length == 1) {
                    temp.setNam(adrblks[0]);
                    temp.setAdr1("");
                    temp.setAdr2("");
                    temp.setAdr3("");
                    temp.setAdr4("");
                } else if (adrblks.length == 2) {
                    temp.setNam(adrblks[0]);
                    temp.setAdr1(adrblks[1]);
                    temp.setAdr2("");
                    temp.setAdr3("");
                    temp.setAdr4("");
                } else if (adrblks.length == 3) {
                    temp.setNam(adrblks[0]);
                    temp.setAdr1(adrblks[1]);
                    temp.setAdr2(adrblks[2]);
                    temp.setAdr3("");
                    temp.setAdr4("");
                } else if (adrblks.length == 4) {
                    temp.setNam(adrblks[0]);
                    temp.setAdr1(adrblks[1]);
                    temp.setAdr2(adrblks[2]);
                    temp.setAdr3(adrblks[3]);
                    temp.setAdr4("");
                } else {
                    temp.setNam(adrblks[0]);
                    temp.setAdr1(adrblks[1]);
                    temp.setAdr2(adrblks[2]);
                    temp.setAdr3(adrblks[3]);
                    temp.setAdr4(adrblks[4]);
                }
            } else {
                //名称地址拆了
                String namelc = ptsptaVo.getNamelc();
                String adrelc = ptsptaVo.getAdrelc();
                adrelc = StringUtils.convertBlock(adrelc, 100, 40);
                temp.setNam(namelc);
                if (StringUtils.isNotEmpty(adrelc)) {
                    adrelc = StringUtils.convertBlock(adrelc, 100, 40);
                    String[] adrelcs = adrelc.split("\r?\n");
                    if (adrelcs.length == 1) {
                        temp.setAdr1(adrelcs[0]);
                        temp.setAdr2("");
                        temp.setAdr3("");
                        temp.setAdr4("");
                    } else if (adrelcs.length == 2) {
                        temp.setAdr1(adrelcs[0]);
                        temp.setAdr2(adrelcs[1]);
                        temp.setAdr3("");
                        temp.setAdr4("");
                    } else if (adrelcs.length == 3) {
                        temp.setAdr1(adrelcs[0]);
                        temp.setAdr2(adrelcs[1]);
                        temp.setAdr3(adrelcs[2]);
                        temp.setAdr4("");
                    } else {
                        temp.setAdr1(adrelcs[0]);
                        temp.setAdr2(adrelcs[1]);
                        temp.setAdr3(adrelcs[2]);
                        temp.setAdr4(adrelcs[3]);
                    }
                } else if (StringUtils.isNotEmpty(namelc)){
                    temp.setAdr1("");
                    temp.setAdr2("");
                    temp.setAdr3("");
                    temp.setAdr4("");
                }
            }
        } else /*if (EN.equalsIgnoreCase(ptsptaVo.getLanguage()))*/ {
            //英文地址
            if (StringUtils.isNotEmpty(ptsptaVo.getAdrblk())) {
                //名称地址没有拆
                String adrblk = ptsptaVo.getAdrblk();
                adrblk = StringUtils.convertBlock(adrblk, 100, 35);
                String[] adrblks = adrblk.split("\r?\n");
                if (adrblks.length == 1) {
                    temp.setNam1(adrblks[0]);
                    temp.setNam2("");
                    temp.setStr1("");
                    temp.setStr2("");
                    temp.setLoczip("");
                    temp.setLoctxt("");
                } else if (adrblks.length == 2) {
                    temp.setNam1(adrblks[0]);
                    temp.setNam2(adrblks[1]);
                    temp.setStr1("");
                    temp.setStr2("");
                    temp.setLoczip("");
                    temp.setLoctxt("");
                } else if (adrblks.length == 3) {
                    temp.setNam1(adrblks[0]);
                    temp.setNam2(adrblks[1]);
                    temp.setStr1(adrblks[2]);
                    temp.setStr2("");
                    temp.setLoczip("");
                    temp.setLoctxt("");
                } else {
                    temp.setNam1(adrblks[0]);
                    temp.setNam2(adrblks[1]);
                    temp.setStr1(adrblks[2]);
                    int line = adrblks[3].length();
                    if (line < 11) {
                        temp.setLoczip(adrblks[3]);
                        temp.setLoctxt("");
                    } else {
                        temp.setLoczip(adrblks[3].substring(0, 10));
                        temp.setLoctxt(adrblks[3].substring(10));
                    }
                }
            } else {
                //名称地址拆了
                String namelc = ptsptaVo.getNamelc();
                String adrelc = ptsptaVo.getAdrelc();
                namelc = StringUtils.convertBlock(namelc, 100, 35);
                adrelc = StringUtils.convertBlock(adrelc, 100, 35);
                if (StringUtils.isNotEmpty(namelc)) {
                    String[] namelcs = namelc.split("\r?\n");
                    if (namelcs.length == 1) {
                        temp.setNam1(namelcs[0]);
                        temp.setNam2("");
                    } else {
                        temp.setNam1(namelcs[0]);
                        temp.setNam2(namelcs[1]);
                    }
                }
                if (StringUtils.isNotEmpty(adrelc)) {
                    adrelc = StringUtils.convertBlock(adrelc, 100, 35);
                    String[] adrelcs = adrelc.split("\r?\n");
                    if (adrelcs.length == 1) {
                        temp.setStr1(adrelcs[0]);
                        temp.setStr2("");
                        temp.setLoczip("");
                        temp.setLoctxt("");
                    } else {
                        temp.setStr1(adrelcs[0]);
                        int line = adrelcs[1].length();
                        if (line < 11) {
                            temp.setLoczip(adrelcs[1]);
                            temp.setLoctxt("");
                        } else {
                            temp.setLoczip(adrelcs[1].substring(0, 10));
                            temp.setLoctxt(adrelcs[1].substring(10));
                        }
                    }
                }
            }
        }
        return temp;
    }

    /**
     * 同时生成中英文临时地址，Ptap13组件用
     *
     * @param ptsptaVo 当前执行的数据容器
     */
    private Adr ptsTmpAdrDefault1(PtsptaVo ptsptaVo) {
        String rol = ptsptaVo.getRol();
        Adr temp = new Adr();
        // 生成临时地址的extkey （更新不生成新的编号）
        if (StringUtils.isEmpty(ptsptaVo.getPtainr())) {
            if (ptsptaVo.isFlg()) {
                temp.setExtkey("9999" + counterService.dbCounterFormat("TMPADR", 8));
            } else {
                temp.setExtkey("Temp" + rol.toUpperCase());
            }
        } else {
            temp = adrMapper.selectByPtaInr(ptsptaVo.getPtainr());
        }
        //中文地址
        //名称地址拆了
        String cnnam = ptsptaVo.getCnnam();
        String cnadr = ptsptaVo.getCnadr();
        temp.setNam(cnnam);
        if (StringUtils.isNotEmpty(cnadr)) {
            cnadr = StringUtils.convertBlock(cnadr, 100, 40);
            String[] cnadrs = cnadr.split("\r?\n");
            if (cnadrs.length == 1) {
                temp.setAdr1(cnadrs[0]);
                temp.setAdr2("");
                temp.setAdr3("");
                temp.setAdr4("");
            } else if (cnadrs.length == 2) {
                temp.setAdr1(cnadrs[0]);
                temp.setAdr2(cnadrs[1]);
                temp.setAdr3("");
                temp.setAdr4("");
            } else if (cnadrs.length == 3) {
                temp.setAdr1(cnadrs[0]);
                temp.setAdr2(cnadrs[1]);
                temp.setAdr3(cnadrs[2]);
                temp.setAdr4("");
            } else {
                temp.setAdr1(cnadrs[0]);
                temp.setAdr2(cnadrs[1]);
                temp.setAdr3(cnadrs[2]);
                temp.setAdr4(cnadrs[3]);
            }
        } else if (StringUtils.isNotEmpty(cnnam)) {
            temp.setAdr1("");
            temp.setAdr2("");
            temp.setAdr3("");
            temp.setAdr4("");
        }
        //英文地址
        //名称地址拆了
        String ennam = ptsptaVo.getEnnam();
        String enadr = ptsptaVo.getEnadr();
        if (StringUtils.isNotEmpty(ennam)) {
            ennam = StringUtils.convertBlock(ennam, 100, 35);
            String[] ennams = ennam.split("\r?\n");
            if (ennams.length == 1) {
                temp.setNam1(ennams[0]);
                temp.setNam2("");
            } else if (ennams.length == 2) {
                temp.setNam1(ennams[0] + "\n" + ennams[1]);
                temp.setNam2("");
            } else if (ennams.length == 3) {
                temp.setNam1(ennams[0] + "\n" + ennams[1]);
                temp.setNam2(ennams[2]);
            } else if (ennams.length > 3) {
                temp.setNam1(ennams[0] + "\n" + ennams[1]);
                temp.setNam2(ennams[2] + "\n" + ennams[3]);
            } else {
                temp.setNam1("");
                temp.setNam2("");
            }
        }
        if (StringUtils.isNotEmpty(enadr)) {
            enadr = StringUtils.convertBlock(enadr, 100, 35);
            String[] enadrs = enadr.split("\r?\n");
            if (enadrs.length == 1) {
                temp.setStr1(enadrs[0]);
                temp.setStr2("");
                temp.setLoczip("");
                temp.setLoctxt("");
            } else {
                temp.setStr1(enadrs[0]);
                int line = enadrs[1].length();
                if (line < 11) {
                    temp.setLoczip(enadrs[1]);
                    temp.setLoctxt("");
                } else {
                    temp.setLoczip(enadrs[1].substring(0, 10));
                    temp.setLoctxt(enadrs[1].substring(10));
                }
            }
        } else if (StringUtils.isNotEmpty(ennam)) {
            temp.setStr1("");
            temp.setStr2("");
            temp.setLoczip("");
            temp.setLoctxt("");
        }
        return temp;
    }

    /**
     * Ptap组件oit警告
     *
     * @param objinr
     * @param objtyp
     * @param path
     */
    private void registOit(String objinr, String objtyp, String path) {
        Map<String, List<Oit>> maps = oitService.filterOit(objinr, objtyp);
        String message = "";
        if (maps != null && maps.containsKey("WARN")) {
            List<Oit> warnOits = maps.get("WARN");
            for (int i = 0; i < warnOits.size(); i++) {
                if (i == 0) {
                    message += warnOits.get(i).getInftxt();
                } else {
                    message += "\r\n" + warnOits.get(i).getInftxt();
                }
            }
        }
        List<WarnVo> warnList = new ArrayList<>();
        WarnVo warnVo = new WarnVo();
        warnVo.setId(path);
        warnVo.setSubId("PTYWARNING");
        warnVo.setLevel(WarnVo.LEVEL_GENERAL);
        warnVo.setSource(WarnVo.SOURCE_BACK);
        warnVo.setMessage(message);
        warnList.add(warnVo);
        //warnService.generalWarn(path, "PTYWARNING", WarnVo.SOURCE_BACK, message);
        message = "";
        if (maps != null && maps.containsKey("INFO")) {
            List<Oit> warnOits = maps.get("INFO");
            for (int i = 0; i < warnOits.size(); i++) {
                if (i == 0) {
                    message += warnOits.get(i).getInftxt();
                } else {
                    message += "\r\n" + warnOits.get(i).getInftxt();
                }
            }
        }
        warnVo = new WarnVo();
        warnVo.setId(path);
        warnVo.setSubId("PTYINFO");
        warnVo.setLevel(WarnVo.LEVEL_TIPS);
        warnVo.setSource(WarnVo.SOURCE_BACK);
        warnVo.setMessage(message);
        warnList.add(warnVo);
        warnService.putWarnList(warnList);
    }

    /**
     * 只有uil是cn且名称地址分开的时候  返回true 取中文的名称地址
     * 否则取英文的名称地址
     *
     * @param ptsptaVo
     * @return
     */
    private boolean isCnNameAdress(PtsptaVo ptsptaVo) {
        boolean flag = false;
        if (ZH.equalsIgnoreCase(ptsptaVo.getLanguage()) && StringUtils.isEmpty(ptsptaVo.getAdrblk())) {
            flag = true;
        }
        return flag;
    }
}
