package com.brilliance.isc.common.transaction.newcheck.umdmod;

import com.brilliance.isc.common.context.SettleContext;
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.umdmod.service.UmdmodService;
import com.brilliance.isc.common.umdmod.vo.UmdmodVo;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class UmdmodValidator extends AbstractModuleValidator<UmdmodVo> {
    @Autowired(required = false)
    private UmdmodService umdmodService;

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

    @Override
    public Map<String, String> validate(UmdmodVo vo) {
        return super.validate(vo);
    }

    @Validate(path = "umdmod.bennam")
    public ValidResult checkBennam(UmdmodVo vo) {
        if (umdmodService.tymdOpn()) {
            //String transName = SettleContext.getTransName();
            String transName = SettleContext.getDirectTransName();
            switch (transName) {
                case "GITOPN":
                case "GITPOP":
                case "NITOPN":
                case "NITPOP":
                case "GITAME":
                case "GITPAM":
                case "NITAME":
                case "NITPAM":
                case "GITSET":
                case "GITPSE":
                case "NITSET":
                case "NITPSE":
                    if (StringUtils.isEmpty(vo.getBennam())) {
                        //保函只有对外担保才校验黑名单与高风险国家
                        String fingua = Utils.getFieldValue(vo.getRec(), "fingua");
                        if ("Y".equalsIgnoreCase(fingua)) {
                            return ValidResult.build(false, "该字段是必填项");
                        }
                    }
                    break;
                case "LITOPN":
                case "LITAME":
                case "LITDCK":
                case "BCTDAV":
                case "BCTAME":
                case "BRTSET":
                case "BCTSET":
                case "LETOPN":
                case "LETAME":
                case "LETRSV":
                case "LETDRW":
                case "LETDRV":
                case "BOTDAV":
                case "BOTAME":
                case "CCTDAV":
                case "CLTDAV":
                case "LTTOPN":
                case "LTTAME":
                case "LTTDCK":
                case "GETOPN":
                case "NETOPN":
                case "GETAME":
                case "NETAME":
                    if (StringUtils.isEmpty(vo.getBennam())) {
                        return errorMandatory();
                    }
                    break;
                case "CPTOPN":
                case "CPTECO":
                case "CPTADV":
                case "CPTECI":
                    if (StringUtils.isEmpty(vo.getBennam())) {
                        String swftyp = Utils.getFieldValue(vo.getRec(), "swftyp");
                        if (Strings.pos("-200-103-202-", swftyp) > 0) {
                            return errorMandatory();
                        }
                    }
                    break;
                default:
            }
        }
        return null;
    }


}
