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


import com.brilliance.isc.bo.Ded;
import com.brilliance.isc.bo.Did;
import com.brilliance.isc.bo.Imguse;
import com.brilliance.isc.bo.Invuse;
import com.brilliance.isc.bo.model.Bddgrp;
import com.brilliance.isc.bo.model.Bfdgrp;
import com.brilliance.isc.bo.model.Bpdgrp;
import com.brilliance.isc.bo.model.ImgInvmod;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.invchk.vo.InvlstVo;
import com.brilliance.isc.common.transaction.newcheck.AbstractModuleValidator;
import com.brilliance.isc.common.transaction.newcheck.Warning;
import com.brilliance.isc.common.transaction.newcheck.WarningResult;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.mda.dao.DedMapper;
import com.brilliance.isc.mda.dao.DidMapper;
import com.brilliance.isc.mda.dao.InvuseMapper;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ImginvmodValidator extends AbstractModuleValidator<ImgInvmod> {

    @Autowired
    private InvuseMapper invuseMapper;

    @Autowired
    private DedMapper dedMapper;

    @Autowired
    private DidMapper didMapper;

    @Warning(path = "imgInvmod.fpdj")
    public WarningResult warningFpdj(ImgInvmod vo) {
        Boolean elcflg = false;
        switch (SettleContext.getTransName()) {
            case "BFTSND":
            case "BFTDRV":
            case "BFTINV":
                Bfdgrp bfdgrp = (Bfdgrp) getParent();
                Ded ded = new Ded();
                if (!MdaUtils.isEmpty(bfdgrp) && "DED".equalsIgnoreCase(bfdgrp.getRec().getPnttyp())){
                    ded.setInr(bfdgrp.getRec().getPntinr());
                    ded = dedMapper.selectByPrimaryKey(ded);
                    if (!MdaUtils.isEmpty(ded)){
                        if ("Y".equalsIgnoreCase(ded.getElcflg())) {
                            elcflg = true;
                        } else {
                            elcflg = false;
                        }
                    }
                }
                break;
            default:
                elcflg = false;
        }
        Boolean flg = false;
        if (CollectionUtils.isNotEmpty(vo.getInvlst())) {
            for (InvlstVo item : vo.getInvlst()) {
                if ("BDTSET".equalsIgnoreCase(SettleContext.getTransName())) {
                    if (("ECOM".equalsIgnoreCase(item.getInvtyp()) || "ESPE".equalsIgnoreCase(item.getInvtyp())) && StringUtils.isNotEmpty(item.getIsfnc())) {
                        flg = true;
                        break;
                    }
                } else if ("BFTSND".equalsIgnoreCase(SettleContext.getTransName()) || "BFTDRV".equalsIgnoreCase(SettleContext.getTransName()) || "BFTINV".equalsIgnoreCase(SettleContext.getTransName())) {
                    if (("ECOM".equalsIgnoreCase(item.getInvtyp()) || "ESPE".equalsIgnoreCase(item.getInvtyp())) && StringUtils.isNotEmpty(item.getIsfnc()) && !"DJCG".equalsIgnoreCase(item.getRegsta())) {
                        flg = true;
                        break;
                    }
                }
            }
        }
        if (!elcflg && flg) {
            String txt = "BDTSET".equalsIgnoreCase(SettleContext.getTransName()) ? "电子发票需要进行发票登记" : "非电证的电子发票需要进行发票登记";
            return WarningResult.build("SubCheckAddRegFlg", txt);
        }
        return WarningResult.buildEmpty("SubCheckAddRegFlg");
    }

    @Warning(path = "imgInvmod.fpdj")
    public WarningResult warningIsRe(ImgInvmod vo) {
        if (CollectionUtils.isNotEmpty(vo.getInvlst())) {
            for (InvlstVo item : vo.getInvlst()) {
                if (("ECOM".equalsIgnoreCase(item.getInvtyp()) || "ESPE".equalsIgnoreCase(item.getInvtyp())) && StringUtils.isEmpty(item.getIsfnc())) {
                    return WarningResult.build("SubCheckIsRe", "电子发票需要进行人行重复性校验");
                }
            }
        }
        return WarningResult.buildEmpty("SubCheckIsRe");
    }

    @Warning(path = "imgInvmod.uploadFlg")
    public WarningResult warningUploadflg(ImgInvmod vo) {
        Boolean elcFlg = false;
        switch (SettleContext.getTransName()) {
            case "DITOPN":
            case "DITAME":
            case "DITAMC":
            case "DITDCK":
            case "BDTDCK":
            case "BDTINV":
            case "BDTSET":
                Bddgrp bddgrp = (Bddgrp) getParent();
                Did did = new Did();
                if (!MdaUtils.isEmpty(bddgrp) && "DID".equalsIgnoreCase(bddgrp.getRec().getPnttyp())){
                    did.setInr(bddgrp.getRec().getPntinr());
                    did = didMapper.selectByPrimaryKey(did);
                    if (!MdaUtils.isEmpty(did)){
                        if ("Y".equals(did.getElcflg())) {
                            elcFlg = true;
                        } else {
                            elcFlg = false;
                        }
                    }
                }
                break;
            case "BFTSND":
            case "BFTDRV":
            case "BFTINV":
                Bfdgrp bfdgrp = (Bfdgrp) getParent();
                Ded ded = new Ded();
                if (!MdaUtils.isEmpty(bfdgrp) && "DED".equalsIgnoreCase(bfdgrp.getRec().getPnttyp())){
                    ded.setInr(bfdgrp.getRec().getPntinr());
                    ded = dedMapper.selectByPrimaryKey(ded);
                    if (!MdaUtils.isEmpty(ded)){
                        if ("Y".equals(ded.getElcflg())) {
                            elcFlg = true;
                        } else {
                            elcFlg = false;
                        }
                    }
                }
                break;
            default:
                elcFlg = false;
        }

        Boolean isUpload = false;
        if (CollectionUtils.isEmpty(vo.getImguseList())) {
            isUpload = false;
        } else {
            for (Imguse item : vo.getImguseList()) {
                if (!"Y".equals(item.getSndsta())) {
                    isUpload = true;
                    break;
                }
            }
        }

        if (elcFlg && isUpload && !vo.getUploadFlg()) {
            return WarningResult.build("SubCheckUploadflg", "电证影像需要上传人行");
        }
        return WarningResult.buildEmpty("SubCheckUploadflg");
    }

    @Warning(path = "imgInvmod.ownref")
    public WarningResult warningCheckother(ImgInvmod vo) {
        switch (SettleContext.getTransName()){
            case "BFTINV":
            case "BDTINV":
            case "BPTINV":
                if (CollectionUtils.isNotEmpty(vo.getInvlst())) {
                    for (InvlstVo item : vo.getInvlst()) {
                        if (Integer.parseInt(StringUtils.isEmpty(item.getCheckother()) ? "0" : item.getCheckother()) >= 1) {
                            return WarningResult.build("SubCheckother", "本批次发票中存在他行核验过的发票");
                        }
                    }
                }
                if (CollectionUtils.isNotEmpty(vo.getOldInvlst())) {
                    for (InvlstVo item : vo.getOldInvlst()) {
                        if (Integer.parseInt(StringUtils.isEmpty(item.getCheckother()) ? "0" : item.getCheckother()) >= 1) {
                            return WarningResult.build("SubCheckother", "本批次发票中存在他行核验过的发票");
                        }
                    }
                }
            default:
                return WarningResult.buildEmpty("SubCheckother");
        }
    }

    @Warning(path = "imgInvmod.pntref")
    public WarningResult warningAbnormal(ImgInvmod vo) {
        String ownref = null;
        switch (SettleContext.getTransName()){
            case "BFTSND":
            case "BFTDRV":
                Bfdgrp bfdgrp = (Bfdgrp) getParent();
                if (!MdaUtils.isEmpty(bfdgrp)){
                    ownref = bfdgrp.getRec().getOwnref();
                }
                break;
            case "DITDCK":
            case "BDTDCK":
            case "BDTSET":
                Bddgrp bddgrp = (Bddgrp) getParent();
                if (!MdaUtils.isEmpty(bddgrp)){
                    ownref = bddgrp.getRec().getOwnref();
                }
                break;
            case "BPTOPN":
            case "BPTSET":
                Bpdgrp bpdgrp = (Bpdgrp) getParent();
                if (!MdaUtils.isEmpty(bpdgrp)){
                    ownref = bpdgrp.getRec().getOwnref();
                }
                break;
            default:
                ownref = "";
        }
        if (StringUtils.isNotEmpty(ownref)){
            List<Invuse> abnormalInvlst = invuseMapper.getAbnormalInvByOwnref(ownref);
            if (CollectionUtils.isNotEmpty(abnormalInvlst)){
                return WarningResult.build("SubCheckAbnormal", "该单据存在异常发票，请到发票修改交易对异常发票进行替换或删除");
            }
        }
        return WarningResult.buildEmpty("SubCheckAbnormal");
    }
}
