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

import  com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.util.ObjectUtils;
import com.brilliance.isc.bo.Umd;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.limmod.vo.LimmodVo;
import com.brilliance.isc.common.umdbnk.service.UmdbnkService;
import com.brilliance.isc.common.umdmod.service.UmdmodService;
import com.brilliance.isc.common.umdmod.vo.CustVo;
import com.brilliance.isc.common.umdmod.vo.TranVo;
import com.brilliance.isc.common.umdmod.vo.UmdmodVo;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.vo.PtsptaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UmdbnkServiceImpl implements UmdbnkService {
    @Autowired
    private UmdmodService umdmodService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    public void addCheckBankByName(UmdmodVo umdmodVo, String argRol, String reltype, String custtype, String custname) {
       PtsptaVo ptsptaVo= umdmodService.getPtsptaFromPtslist(umdmodVo.getPtsptas(),argRol);
       // 有些parties先设置完之后，之后再删除rol可能没有删除，使用ptsIsRolSet会更准确
        if(!ptsptaToolComponent.ptsIsRolSet(ptsptaVo)){
            return;
        }

        List<TranVo> tranVos = umdmodVo.getTRANS();
        if (MdaUtils.isEmpty(tranVos)) {
            tranVos = new ArrayList<>();
        }
        TranVo tranVo = new TranVo();

        tranVo.setREL_TYPE(reltype);
        tranVo.setCUST_TYPE(custtype);
        tranVo.setCUST_NAME(custname==null?"":custname);
        tranVos.add(tranVo);
        umdmodVo.setTRANS(tranVos);
    }

    @Override
    public void addCheckBankByName(UmdmodVo umdmodVo, PtsptaVo ptsptaVo, String reltype, String custtype, String custname) {
        //!
        //!参数：
        //!   -Arggrp : 业务模块
        //!   -ArgRol : 角色
        //!   -RELTYPE : 筛查对象类型 04：对手银行   05：交易涉及的金融机构
        //!   -CUSTTYPE : 客户类型2：实体
        //!   -CUSTNAME : 客户名称
        if (ptsptaToolComponent.ptsIsRolSet(ptsptaVo)) {
            TranVo tranVo = new TranVo();
            tranVo.setREL_TYPE(reltype);
            tranVo.setCUST_TYPE(custtype);
            tranVo.setCUST_NAME(custname);
            umdmodVo.addTran(tranVo);
        }
    }

    public void addCheckBankByRol(UmdmodVo umdmodVo, String argRol, String argType) {
        String extkey = getPtyExtkeyByRol(argRol, umdmodVo.getPtsptas());

        if(StringUtils.isEmpty(extkey)){
            return;
        }
        List<TranVo> tranVos = umdmodVo.getTRANS();
        if (MdaUtils.isEmpty(tranVos)) {
            tranVos = new ArrayList<>();
        }
        TranVo tranVo = new TranVo();
        tranVo.setSWIFT_NO(extkey.split("-")[0]);
        tranVo.setREL_TYPE(argType);
        tranVo.setCUST_TYPE("2");
        tranVo.setCUST_NAME("");
        tranVos.add(tranVo);
        umdmodVo.setTRANS(tranVos);
    }

    @Override
    public void addCheckBankByRol(UmdmodVo umdmodVo, PtsptaVo ptsptaVo, String argType) {
        if (ptsptaToolComponent.ptsIsRolSet(ptsptaVo)) {
            String extkey = ptsptaVo.getExtkey();
            TranVo tranVo = new TranVo();
            tranVo.setSWIFT_NO(extkey.split("-")[0]);
            tranVo.setREL_TYPE(argType);
            tranVo.setCUST_TYPE("2");
            umdmodVo.addTran(tranVo);
        }
    }

    public String getPtyExtkeyByRol(String rol, List<PtsptaVo> pts) {

        for (PtsptaVo ptyvo : pts) {
            if (rol.equals(ptyvo.getRol())) {
                return ptyvo.getExtkey();
            }
        }
        return "";
    }

    public void umdbnkAddCheck(UmdmodVo umdmodVo, String extkey, String argtyp, String argcustyp) {
        if (MdaUtils.isEmpty(extkey)) {
            return;
        }
        List<TranVo> tranVos = umdmodVo.getTRANS();
        if (MdaUtils.isEmpty(tranVos)) {
            tranVos = new ArrayList<>();
        }

        TranVo tranVo = new TranVo();
        tranVo.setSWIFT_NO(extkey.split("-")[0]);
        tranVo.setREL_TYPE(argtyp);
        tranVo.setCUST_TYPE(argcustyp);
        tranVo.setCUST_NAME("");
        tranVos.add(tranVo);
        umdmodVo.setTRANS(tranVos);
    }

    public void umdbnkAddCheckInc(UmdmodVo umdmodVo, String extkey, String argtyp, String argcustyp,String nat, String argcusname) {

        if (MdaUtils.isEmpty(argcusname)&&MdaUtils.isEmpty(extkey)
        &&MdaUtils.isEmpty(nat)) {
            return;
        }
        if (argcusname.length() > 128) {
            argcusname = argcusname.substring(0, 128);
        }


        List<TranVo> tranVos = umdmodVo.getTRANS();
        if (MdaUtils.isEmpty(tranVos)) {
            tranVos = new ArrayList<>();
        }

         if(!StringUtils.isEmpty(extkey)){
             extkey=extkey.split("-")[0];
         }

        TranVo tranVo = new TranVo();
        tranVo.setSWIFT_NO(extkey);
        tranVo.setREL_TYPE(argtyp);
        tranVo.setCUST_TYPE(argcustyp);
        tranVo.setCUST_NAME(argcusname);
        tranVo.setNAT(nat);
        tranVos.add(tranVo);
        umdmodVo.setTRANS(tranVos);
    }

    public void umdbnkAddCheckInc(UmdmodVo umdmodVo, String extkey, String argtyp, String argcustyp,String nat, String argcusname, String celist) {
        if (MdaUtils.isEmpty(argcusname)&&MdaUtils.isEmpty(extkey)
                &&MdaUtils.isEmpty(nat)) {
            return;
        }
        if (argcusname.length() > 128) {
            argcusname = argcusname.substring(0, 128);
        }

        List<TranVo> tranVos = umdmodVo.getTRANS();
        if (MdaUtils.isEmpty(tranVos)) {
            tranVos = new ArrayList<>();
        }

        if(!StringUtils.isEmpty(extkey)){
            extkey=extkey.split("-")[0];
        }

        TranVo tranVo = new TranVo();
        tranVo.setSWIFT_NO(extkey);
        tranVo.setREL_TYPE(argtyp);
        tranVo.setCUST_TYPE(argcustyp);
        tranVo.setCUST_NAME(argcusname);
        tranVo.setNAT(nat);
        tranVo.setCE_LIST(celist);
        tranVos.add(tranVo);
        umdmodVo.setTRANS(tranVos);
    }

   public void umdbnkAddCheckName(UmdmodVo umdmodVo,String extkey,String argtyp,String argcustyp,String argcusname){


       if (MdaUtils.isEmpty(argcusname)) {
           return;
       }
       if (argcusname.length() > 128) {
           argcusname = argcusname.substring(0, 128);
       }


       List<TranVo> tranVos = umdmodVo.getTRANS();
       if (MdaUtils.isEmpty(tranVos)) {
           tranVos = new ArrayList<>();
       }

       TranVo tranVo = new TranVo();
       tranVo.setSWIFT_NO("");
       tranVo.setREL_TYPE(argtyp);
       tranVo.setCUST_TYPE(argcustyp);
       tranVo.setCUST_NAME(argcusname);

       tranVos.add(tranVo);
       umdmodVo.setTRANS(tranVos);
   }
}
