package com.brilliance.isc.common.transaction.newcheck.bopset.rmbbop;

import com.brilliance.isc.bo.Cnycty;
import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.rmbbop.entity.Tbk;
import com.brilliance.isc.common.rmbbop.vo.Rmb2111;
import com.brilliance.isc.common.rmbbop.vo.RmbbopRequest;
import com.brilliance.isc.common.transaction.newcheck.AbstractModuleValidator;
import com.brilliance.isc.common.transaction.newcheck.ValidResult;
import com.brilliance.isc.common.transaction.newcheck.Validate;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.CnyctyMapper;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;

@Component
public class Rmb2111Validator extends AbstractModuleValidator<Rmb2111> {

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private PtyCacheService ptyCacheService;

    @Autowired
    private CnyctyMapper cnyctyMapper;

    @Autowired
    private TbkValidator tbkValidator;

    @Override
    @PostConstruct
    public void register() {
        super.register();
        tbkValidator.register();
    }

    @Override
    public Map<String, String> validate(Rmb2111 vo) {
        Map<String, String> maps = new HashMap<>(super.validate(vo));
        //验证备忘录条目
        List<Tbk> tbk = vo.getRecgrp().getTbk();
        for (int i = 0; i < tbk.size(); i++) {
            maps.putAll(tbkValidator.validate(tbk.get(i), i));
        }
        return maps;
    }

    @Validate(path = "rmbbop.rmb2111.recgrp.ads.bankorgcode")
    public ValidResult checkrmb2111VoRecgrpAdsBankorgcode(Rmb2111 rmb2111) {
        if (MdaUtils.isEmpty(rmb2111.getRecgrp().getAds().getBankorgcode())) {
            return ValidResult.build(false, "必输项");
        }
        return null;
    }


    @Validate(path = "rmbbop.rmb2111.recgrp.ads.payerattr")
    public ValidResult checkRecgrpAdsPayeeattrN1001(Rmb2111 rmb2111) {
        String tranRol = NULLSTR;
        String fieldVal2 = NULLSTR;
        if (!MdaUtils.isEmpty(rmb2111.getRecgrp().getAds().getPayerattr())) {

            switch (SettleContext.getTransName().toUpperCase()) {
                case "GITSET":
                    tranRol = "APL";
                    break;
                case "GETSET":
                    tranRol = "BEN";
                    break;
            }

            RmbbopRequest rmbbopRequest = (RmbbopRequest) getParent();

            PtsptaVo apl = ptsptaToolComponent.getPtsFromListByRol(rmbbopRequest.getPtsList(), "APL");
            if (MdaUtils.isEmpty(apl)) {
                apl = new PtsptaVo();
                apl.reset();
            }
            PtsptaVo ben = ptsptaToolComponent.getPtsFromListByRol(rmbbopRequest.getPtsList(), "BEN");
            if (MdaUtils.isEmpty(ben)) {
                ben = new PtsptaVo();
                ben.reset();
            }


            Pty pty = new Pty();
            if (MdaUtils.compareTo(tranRol, "APL") == 0) {
                if (!MdaUtils.isEmpty(apl.getPtyinr())) {
                    pty = ptyCacheService.getPtyById(apl.getPtyinr());
                }
            }

            if (MdaUtils.compareTo(tranRol, "BEN") == 0) {
                if (!MdaUtils.isEmpty(ben.getPtyinr())) {
                    pty = ptyCacheService.getPtyById(ben.getPtyinr());
                }
            }

            if (!MdaUtils.isEmpty(pty)) {
                fieldVal2 = pty.getIdtype();
            }

            if (MdaUtils.compareTo(fieldVal2, "2") == 0) {
                if (!MdaUtils.isEmpty(rmb2111.getRecgrp().getAds().getPayerattr())) {
                    if (MdaUtils.compareTo(rmb2111.getRecgrp().getAds().getPayerattr(), "2001") == 0 || MdaUtils.compareTo(rmb2111.getRecgrp().getAds().getPayerattr(), "2002") == 0) {
                        return null;
                    } else {
                        return ValidResult.build(false, "付款人属性错误，请修改");
                    }
                }
            } else {
                if (MdaUtils.compareTo(fieldVal2, "1") == 0) {
                    if (MdaUtils.compareTo(rmb2111.getRecgrp().getAds().getPayerattr(), "2003") == 0) {
                        return null;
                    } else {
                        return ValidResult.build(false, "付款人属性错误，请修改");
                    }
                }
            }
        }

        return null;
    }

//    @Validate(path="rmb2111.rmbflg")
//    public ValidResult checkRmbflgN1000(rmb2111 rmb2111){
//        switch( SettleContext.getTransName().toUpperCase() )
//        {
//            case  "GITSET":
//                if( MdaUtils.isEmpty(rmb2111.getRmbflg()) )
//                {
//                    return ValidResult.build(false, "必输项");
//                }
//                break;
//        }
//
//        return null;
//    }


    @Validate(path = "rmbbop.rmb2111.recgrp.ads.payeecountrycode")
    public ValidResult checkpayeecountrycodeN1000(Rmb2111 rmb2111) {
        String countryCod = rmb2111.getRecgrp().getAds().getPayeecountrycode();
        if (StringUtils.isEmpty(countryCod)) {
            return errorMandatory();
        } else {
            Cnycty cnycty = cnyctyMapper.getCnyctyByCode(countryCod);
            if (MdaUtils.isEmpty(cnycty)) {
                return ValidResult.build(false, "请输入正确的收款人常驻国家（地区）代码cod");
            }
            return null;
        }

    }

}
