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

import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.entmod.service.EntmodService;
import com.brilliance.isc.common.entmod.vo.EntmodVo;
import com.brilliance.isc.common.transaction.newcheck.*;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class EntmodValidator extends AbstractModuleValidator<EntmodVo> {

    @Autowired
    private EntmodService entmodService;

    @Autowired
    private PtyCacheService ptyCacheService;

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

    /**
     *   source:gedmod.0001.script
     */
    @Validate(path="entmod.rtyflg")
    public ValidResult checkRtyflgN1000(EntmodVo vo){
        //String open = Inis.getIniStr("intace.ini","TM01","open0");
        if(entmodService.tm01Opn()
                && "X".equals(vo.getVisflg())
                && (StringUtils.isEmpty(vo.getRtyflg()) || StringUtils.isEmpty(vo.getChkflg()))) {
            return ValidResult.build(false, "请点击校验企业名录信息");
        }
        return null;
    }

    @Warning(path="entmod.entinf.code", level = "3")
    public WarningResult warnEntinfCode(EntmodVo vo) {
        if ("X".equals(vo.getVisflg())) {
            if(!MdaUtils.isEmpty(vo.getEntinf().getCode())) {
                if ("00000".equals(vo.getEntinf().getCode())) {
                    if ("A".equals(vo.getEntinf().getEntrank())) {
                        return WarningResult.build("ENTCHKMSG", "企业名录查询信息提示：" + vo.getEntinf().getEntrank() + "类企业<br/>" + vo.getEntinf().getChkmsg());
                    }
                } else {
                    return WarningResult.build("ENTCHKMSG", "企业名录查询信息提示：" + vo.getEntinf().getCode() + "-" + vo.getEntinf().getDetail());
                }
            }
            if(MdaUtils.compareTo(vo.getRtyflg(),"X") == 0
                    && MdaUtils.isEmpty(vo.getEntinf().getCode())) {
                return WarningResult.build("ENTCHKMSG","企业名录查询信息提示：" + vo.getEntinf().getDetail());
            }
        }
        return WarningResult.buildEmpty("ENTCHKMSG");
    }

    @Warning(path="entmod.entinf.code")
    public WarningResult warnEntinfCodeA(EntmodVo vo) {
        if ("X".equals(vo.getVisflg())) {
            if(!MdaUtils.isEmpty(vo.getEntinf().getCode())) {
                if ("00000".equals(vo.getEntinf().getCode())) {
                    if (!"A".equals(vo.getEntinf().getEntrank())) {
                        return WarningResult.build("ENTCHKMSG1", "企业名录查询信息提示：" + vo.getEntinf().getEntrank() + "类企业<br/>" + vo.getEntinf().getChkmsg());
                    }
                }
            }
        }
        return WarningResult.buildEmpty("ENTCHKMSG1");
    }

    @Warning(path="entmod.entinf.corpname", level = "3")
    public WarningResult warnEntinfCorpname(EntmodVo vo) {
        if ("X".equals(vo.getVisflg())) {
            String ptyinr = vo.getPtyinr();
            String corpname = vo.getEntinf().getCorpname();
            Pty pty = ptyCacheService.getPtyById(ptyinr);
            if (pty != null && StringUtils.isNotEmpty(corpname) && !corpname.equals(pty.getNam1())) {
                return WarningResult.build("SUBCORPNAME", "客户名称【"+pty.getNam1()+"】与企业名录中企业名称【"+corpname+"】不一致,请确认");
            }
        }
        return WarningResult.buildEmpty("SUBCORPNAME");
    }
}
