package com.brilliance.isc.common.component;

import com.brilliance.isc.bo.Bnl;
import com.brilliance.isc.bo.Bnm;
import com.brilliance.isc.bo.Dga;
import com.brilliance.isc.mda.dao.BnlMapper;
import com.brilliance.isc.mda.dao.BnmMapper;
import com.brilliance.isc.mda.dao.DgaMapper;
import com.brilliance.isc.vo.BnmVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component("CipmodComponent")
public class CipmodComponent {

    private static final String BANKY = "Y";
    private static final String BANKN = "N";

    @Autowired
    private BnmMapper bnmMapper;

    @Autowired
    private BnlMapper bnlMapper;

    @Autowired
    private DgaMapper dgaMapper;

    /**
     * 获取联行行号和参与方中文名称，入参详情见BnmVo
     * 当前支持三种参与方：收款间接参与方，付款直接参与方，付款间接参与方
     */
    public List<BnmVo> getCyhhAndCyzwmc(BnmVo bnmVo) {
        List<BnmVo> bnmVos = new ArrayList<>();
        if ("F".equals(bnmVo.getBanktyp())) {
            //fxps获取行号
            if (StringUtils.isEmpty(bnmVo.getBch()) && StringUtils.isEmpty(bnmVo.getBchname())) {
                return bnmVos;
            }
            List<Dga> dgaList = dgaMapper.selectByBiccodAndJigomc(bnmVo.getBch(), bnmVo.getBchname());
            if (dgaList != null && dgaList.size() > 0) {
                dgaList.forEach(dga -> {
                    BnmVo temp = new BnmVo();
                    temp.setBch(dga.getBiccod());
                    temp.setBchname(dga.getJigomc());
                    bnmVos.add(temp);
                });
            }
            return bnmVos;
        } else {
            if (StringUtils.isBlank(bnmVo.getBch()) && bnmVo.getBchname().length() < 4) {
                return bnmVos;
            }
            //邢希新要求二期汇款交易cips渠道行号从6位改成4位
            if (StringUtils.isBlank(bnmVo.getBchname()) && bnmVo.getBch().length() < 4) {
                return bnmVos;
            }

            // 确认参数 cyzgbs值是否正确
            if (StringUtils.isBlank(bnmVo.getCyzgbs()) ) {
                return bnmVos;
            }
            List<Bnm> bnmList = bnmMapper.getCyhhCyzwmc(bnmVo);
            if (bnmList != null && bnmList.size() > 0) {
                for (Bnm bnm : bnmList) {
                    BnmVo temp = new BnmVo();
                    temp.setBch(bnm.getKjcyhh());
                    temp.setBchname(bnm.getCyzwmc());
                    bnmVos.add(temp);
                }
                return bnmVos;
            } else {
                if (BANKN.equalsIgnoreCase(bnmVo.getBanktyp())) {
                    Bnl bnl = new Bnl();
                    bnl.setFqcyjg(bnmVo.getBch());
                    bnl.setFukrhm(bnmVo.getBchname());
                    List<Bnl> bnls = bnlMapper.selectByHhMc(bnl);
                    if (bnls != null && bnls.size() > 0) {
                        for (Bnl bnl1 : bnls) {
                            BnmVo temp = new BnmVo();
                            temp.setBch(bnl1.getFqcyjg());
                            temp.setBchname(bnl1.getFukrhm());
                            bnmVos.add(temp);
                        }
                    }
                }
                return bnmVos;
            }
        }
    }
}
