//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.impl.ic.batch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import nc.bs.ic.pub.env.ICBSContext;
import nc.impl.pubapp.env.BSContext;
import nc.itf.ic.batch.IBatchCheck;
import nc.itf.scmpub.reference.uap.bd.material.MaterialPubService;
import nc.pubitf.para.SysInitQuery;
import nc.vo.bd.material.MaterialVO;
import nc.vo.ic.general.define.ICBillBodyVO;
import nc.vo.ic.general.define.ICBillHeadVO;
import nc.vo.ic.general.define.ICBillVO;
import nc.vo.ic.material.define.InvCalBodyVO;
import nc.vo.ic.material.define.InvCalbodyKey;
import nc.vo.ic.pub.lang.BatchRes;
import nc.vo.ic.pub.lang.ResBase;
import nc.vo.ic.pub.util.NCBaseTypeUtils;
import nc.vo.ic.pub.util.StringUtil;
import nc.vo.ic.pub.util.ValueCheckUtil;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.pattern.data.ValueUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.AssertUtils;
import nccloud.commons.lang.ArrayUtils;

public class BatchCheckImpl implements IBatchCheck {
    public BatchCheckImpl() {
    }

    public void checkBatchAttribute(ICBillVO[] bills) throws BusinessException {
        try {
            if (ArrayUtils.isEmpty(bills)) {
                return;
            }

            Set<String> keySet = new HashSet();
            List<InvCalbodyKey> keys = new ArrayList();

            for (ICBillVO bill : bills) {
                for (ICBillBodyVO body : bill.getBodys()) {
                    if (body.getDproducedate() != null && body.getDvalidate() != null && body.getDproducedate().compareTo(body.getDvalidate()) > 0) {
                        ExceptionUtils.wrappException(new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4008028_0", "04008028-0067", (String) null, new String[]{"" + body.getVbatchcode()})));
                    }

                    String var10001 = bill.getHead().getPk_org();
                    if (!keySet.contains(var10001 + body.getCmaterialvid()) && body.getStatus() != 3 && body.getNnum() != null) {
                        keys.add(new InvCalbodyKey(bill.getHead().getPk_org(), body.getCmaterialvid()));
                        var10001 = bill.getHead().getPk_org();
                        keySet.add(var10001 + body.getCmaterialvid());
                    }
                }
            }

            if (ValueCheckUtil.isNullORZeroLength(keys)) {
                return;
            }

            Map<InvCalbodyKey, InvCalBodyVO> invInfoMap = (new ICBSContext()).getInvInfo().getInvCalBodyVOs((InvCalbodyKey[]) keys.toArray(new InvCalbodyKey[keys.size()]));
            Map<String, InvCalBodyVO> cInvInfoMap = this.changeRet(invInfoMap);

            for (ICBillVO bill : bills) {
                StringBuilder errStr = new StringBuilder();
                List<String> errRowNos = new ArrayList();
                this.checkBatchcode(bill, cInvInfoMap, errStr, errRowNos, true);
                if (!StringUtil.isSEmptyOrNull(errStr.toString())) {
                    List<String> cpickmbids = this.getCPickMBids(bill, errRowNos);
                    BSContext.getInstance().setSession("ICSessionContainSrcBid", cpickmbids);
                    throw new BusinessException(errStr.toString());
                }
            }
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
        }

    }

    private Map<String, InvCalBodyVO> changeRet(Map<InvCalbodyKey, InvCalBodyVO> invInfoMap) {
        Map<String, InvCalBodyVO> cInvInfoMap = new HashMap();

        for (InvCalBodyVO vo : invInfoMap.values()) {
            cInvInfoMap.put(vo.getPk_org() + vo.getPk_material(), vo);
        }

        return cInvInfoMap;
    }

    public void checkBatchAttribute(ICBillVO bill) throws BusinessException {
        try {
            if (bill == null || bill.getBodys() == null) {
                return;
            }

            String pk_org = bill.getHead().getPk_org();
            List<String> cmaterialVIDs = new ArrayList();

            for (ICBillBodyVO body : bill.getBodys()) {
                if (body.getStatus() != 3 && !cmaterialVIDs.contains(body.getCmaterialvid()) && body.getNnum() != null) {
                    cmaterialVIDs.add(body.getCmaterialvid());
                }
            }

            Map<String, InvCalBodyVO> materialVOs = this.fetchMaterialInfo(pk_org, cmaterialVIDs);
            StringBuilder errStr = new StringBuilder();
            List<String> errRowNos = new ArrayList();
            this.checkBatchcode(bill, materialVOs, errStr, errRowNos, true);
            if (!StringUtil.isSEmptyOrNull(errStr.toString())) {
                List<String> cpickmbids = this.getCPickMBids(bill, errRowNos);
                BSContext.getInstance().setSession("ICSessionContainSrcBid", cpickmbids);
                throw new BusinessException(errStr.toString());
            }
        } catch (BusinessException ex) {
            ExceptionUtils.marsh(ex);
        }

    }

    public void checkBatchAttributeFor4555(ICBillVO bill) throws BusinessException {
        try {
            if (bill == null || bill.getBodys() == null) {
                return;
            }

            String pk_org = bill.getHead().getPk_org();
            List<String> cmaterialVIDs = new ArrayList();

            for (ICBillBodyVO body : bill.getBodys()) {
                if (body.getStatus() != 3 && !cmaterialVIDs.contains(body.getCmaterialvid()) && !NCBaseTypeUtils.isNullOrZero(body.getNnum())) {
                    cmaterialVIDs.add(body.getCmaterialvid());
                }
            }

            Map<String, InvCalBodyVO> materialVOs = this.fetchMaterialInfo(pk_org, cmaterialVIDs);
            StringBuilder errStr = new StringBuilder();
            List<String> errRowNos = new ArrayList();
            this.checkBatchcode(bill, materialVOs, errStr, errRowNos, false);
            if (!StringUtil.isSEmptyOrNull(errStr.toString())) {
                List<String> cpickmbids = this.getCPickMBids(bill, errRowNos);
                BSContext.getInstance().setSession("ICSessionContainSrcBid", cpickmbids);
                throw new BusinessException(errStr.toString());
            }
        } catch (BusinessException ex) {
            ExceptionUtils.marsh(ex);
        }

    }

    private void checkBatchcode(ICBillVO bill, Map<String, InvCalBodyVO> materialVOs, StringBuilder errStr, List<String> errRowNos, boolean checkQualFlag) {
        for (ICBillBodyVO body : bill.getBodys()) {
            if (body.getStatus() != 3 && body.getNnum() != null && !StringUtil.isSEmptyOrNull(body.getCmaterialvid())) {
                String var10001 = bill.getHead().getPk_org();
                InvCalBodyVO materialVO = (InvCalBodyVO) materialVOs.get(var10001 + body.getCmaterialvid());
                AssertUtils.assertValue(materialVO != null, BatchRes.getMaterialStockErr());
                ICBillHeadVO head = bill.getHead();
                boolean isError = this.checkWholeMngInv(materialVO, body, errStr, errRowNos, checkQualFlag, head);
                if (!isError) {
                    this.checkNonWholeMngInv(materialVO, body, errStr, errRowNos);
                }
            }
        }

    }

    private void checkNonWholeMngInv(InvCalBodyVO materialVO, ICBillBodyVO body, StringBuilder errStr, List<String> errRowNos) {
        if (materialVO == null || materialVO.getWholemanaflag() == null || !materialVO.getWholemanaflag().equals(UFBoolean.TRUE)) {
            if (!StringUtil.isSEmptyOrNull(body.getPk_batchcode()) || !StringUtil.isSEmptyOrNull(body.getVbatchcode())) {
                this.showErr(body, errStr, errRowNos, BatchRes.getNonWholeMngErr());
            }

        }
    }

    private boolean checkValidate(ICBillBodyVO body, int qualityUnit, int qualityNum, StringBuilder errStr, List<String> errRowNos) {
        if (body.getDproducedate() != null && body.getDvalidate() != null) {
            UFDate dproduceDate = body.getDproducedate();
            UFDate dvalidateDate = body.getDvalidate();
            if (dproduceDate != null && dvalidateDate != null && dvalidateDate.before(dproduceDate)) {
            }

            return false;
        } else {
            String cmaterialvid = body.getCmaterialvid();
            MaterialVO matVO = MaterialPubService.queryMaterialBaseInfoByPk(cmaterialvid, new String[]{"name", "code"});
            String var10000 = matVO.getCode();
            String matInfo = var10000 + " " + matVO.getName();
            this.showErr(body, errStr, errRowNos, BatchRes.getBatchEmptyErr(matInfo));
            return true;
        }
    }

    private boolean checkWholeMngInv(InvCalBodyVO materialVO, ICBillBodyVO body, StringBuilder errStr, List<String> errRowNos, boolean checkQualFlag, ICBillHeadVO head) {
        if (materialVO != null && materialVO.getWholemanaflag() != null && materialVO.getWholemanaflag().equals(UFBoolean.TRUE)) {
            if (this.isBatchEmpty(body, head)) {
                String cmaterialvid = body.getCmaterialvid();
                MaterialVO matVO = MaterialPubService.queryMaterialBaseInfoByPk(cmaterialvid, new String[]{"name", "code"});
                String var10000 = matVO.getCode();
                String matInfo = var10000 + " " + matVO.getName();
                this.showErr(body, errStr, errRowNos, BatchRes.getBatchEmptyErr(matInfo));
                return true;
            }

            if (checkQualFlag && ValueUtils.getUFBoolean(materialVO.getQualitymanflag()).equals(UFBoolean.TRUE)) {
                return this.checkValidate(body, materialVO.getQualityunit(), materialVO.getQualitynum(), errStr, errRowNos);
            }
        }

        return false;
    }

    private Map<String, InvCalBodyVO> fetchMaterialInfo(String pk_org, List<String> cmaterialVIDs) {
        Map<String, InvCalBodyVO> retMap = new HashMap();
        ICBSContext context = new ICBSContext();
        InvCalBodyVO[] stockVOs = context.getInvInfo().getInvCalBodyVO(pk_org, (String[]) cmaterialVIDs.toArray(new String[cmaterialVIDs.size()]));
        if (stockVOs != null && stockVOs.length != 0) {
            for (InvCalBodyVO vo : stockVOs) {
                retMap.put(vo.getPk_org() + vo.getPk_material(), vo);
            }

            return retMap;
        } else {
            return retMap;
        }
    }

    private List<String> getCPickMBids(ICBillVO bill, List<String> errRowNos) {
        if (errRowNos != null && errRowNos.size() != 0) {
            List<String> cpickmbids = new ArrayList();

            for (String rowNo : errRowNos) {
                for (ICBillBodyVO body : bill.getChildrenVO()) {
                    if (rowNo.equals(body.getCrowno())) {
                        String cpickmbid = (String) body.getAttributeValue("cpickmbid");
                        cpickmbids.add(cpickmbid);
                    }
                }
            }

            return cpickmbids;
        } else {
            return null;
        }
    }

    private boolean isBatchEmpty(ICBillBodyVO body, ICBillHeadVO head) {
        boolean isBatchEmpty = StringUtil.isSEmptyOrNull(body.getVbatchcode());
        int status = head.getStatus();
        if (status == VOStatus.UNCHANGED) {
            // 借料类型的其它入库跳过批次号校验
            String typeCode = "";
            try {
                typeCode = SysInitQuery.getParaString(head.getPk_org(), "ICIN_TYPE");
            } catch (BusinessException e) {
                throw new RuntimeException(e);
            }
            if (typeCode != null && !"".equals(typeCode) && typeCode.equals(head.getVtrantypecode())) {
                isBatchEmpty = false;
            }
        }
        boolean isNumNotEmpty = body.getNnum() != null && !NCBaseTypeUtils.isNullOrZero(body.getNnum());
        return isBatchEmpty && isNumNotEmpty;
    }

    private void showErr(ICBillBodyVO body, StringBuilder errStrSum, List<String> errRowNos, String str) {
        errStrSum.append(str).append("\n").append(ResBase.getCrowno()).append(":").append(body.getCrowno()).append("\n");
        errRowNos.add(body.getCrowno());
    }
}
