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

package nc.bs.arap.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import nc.bs.arap.bill.ArapBillPubUtil;
import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Log;
import nc.bs.logging.Logger;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.pubitf.uapbd.IMaterialPubService;
import nc.pubitf.uapbd.MeasdocUtil;
import nc.ui.dbcache.DBCacheFacade;
import nc.vo.arap.basebill.BaseAggVO;
import nc.vo.arap.basebill.BaseBillVO;
import nc.vo.arap.basebill.BaseItemVO;
import nc.vo.arap.djlx.DjLXVO;
import nc.vo.arap.global.ArapBillDealVOConsts;
import nc.vo.arap.pub.ArapConstant;
import nc.vo.arap.pub.BillEnumCollection.FromSystem;
import nc.vo.arap.utils.ArrayUtil;
import nc.vo.arap.utils.StringUtil;
import nc.vo.arap.verify.AggverifyVO;
import nc.vo.arap.verify.VerifyDetailVO;
import nc.vo.bd.material.MaterialVO;
import nc.vo.fipub.exception.ExceptionHandler;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;

public final class ArapVOUtils {
    private static final String ZB = "zb.";
    private static Map<Object, Integer> materialDecMap = new HashMap();

    public ArapVOUtils() {
    }

    public static Map<String, Integer> getDecimalFromSource(List<String> pks) {
        Map<String, Integer> result = new HashMap();

        for(String pk : pks) {
            if (null != materialDecMap.get(pk)) {
                result.put(pk, (Integer)materialDecMap.get(pk));
            }
        }

        try {
            Map<String, MaterialVO> vo = ((IMaterialPubService)NCLocator.getInstance().lookup(IMaterialPubService.class)).queryMaterialBaseInfoByPks((String[])pks.toArray(new String[0]), new String[]{"pk_measdoc"});

            for(String pk : vo.keySet()) {
                MaterialVO materialVO = (MaterialVO)vo.get(pk);
                Integer[] num = MeasdocUtil.getInstance().getPrecisionByPks(new String[]{materialVO.getPk_measdoc()});
                if (!StringUtil.isEmptyArry(num)) {
                    materialDecMap.put(pk, num[0]);
                    result.put(pk, num[0]);
                } else {
                    materialDecMap.put(pk, 2);
                    result.put(pk, 2);
                }
            }
        } catch (Exception e) {
            Logger.debug(e.getMessage());
        }

        return result;
    }

    public static int getDecimalFromSource(Object pk_material) {
        try {
            if ("~".equals(pk_material) || null == pk_material) {
                return 2;
            }

            if (null != materialDecMap.get(pk_material)) {
                return (Integer)materialDecMap.get(pk_material);
            }

            String pkMeasdoc = "";

            try {
                Vector<?> fromDBCache = DBCacheFacade.matchPK("select pk_measdoc from bd_material where pk_material in(?)", new String[]{pk_material.toString()}, "pk_measdoc");
                pkMeasdoc = (String)((Vector)fromDBCache.get(0)).get(0);
            } catch (Exception var5) {
                Map<String, MaterialVO> vo = ((IMaterialPubService)NCLocator.getInstance().lookup(IMaterialPubService.class)).queryMaterialBaseInfoByPks(new String[]{pk_material.toString()}, new String[]{"pk_measdoc"});
                pkMeasdoc = ((MaterialVO)vo.get(pk_material.toString())).getPk_measdoc();
            }

            Integer[] num = MeasdocUtil.getInstance().getPrecisionByPks(new String[]{pkMeasdoc});
            if (!StringUtil.isEmptyArry(num)) {
                materialDecMap.put(pk_material, num[0]);
                return num[0];
            }
        } catch (Exception e) {
            Logger.debug(e.getMessage());
        }

        return 0;
    }

    public static void validateVoCopyRed(BaseAggVO vo) throws BusinessException {
        CircularlyAccessibleValueObject[] childrenVOs = vo.getChildrenVO();
        BaseBillVO bill = (BaseBillVO)vo.getParentVO();

        for(CircularlyAccessibleValueObject children : childrenVOs) {
            BaseItemVO item = (BaseItemVO)children;
            if (bill.getBillclass().equals(ArapConstant.ARAP_FK_BILLCLASS) && item.getObjtype() != null && item.getObjtype() == 0 && bill.getIsrefund() != null && bill.getIsrefund().booleanValue()) {
                throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("2006pub_0", "02006pub-0971"));
            }

            if (bill.getBillclass().equals(ArapConstant.ARAP_AP_BILLCLASS) && item.getObjtype() != null && item.getObjtype() == 0 && bill.getIsrefund() != null && bill.getIsrefund().booleanValue()) {
                throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("2006pub_0", "02006pub-0972"));
            }

            if (bill.getBillclass().equals(ArapConstant.ARAP_SK_BILLCLASS) && item.getObjtype() != null && item.getObjtype() == 1 && bill.getIsrefund() != null && bill.getIsrefund().booleanValue()) {
                throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("2006pub_0", "02006pub-0973"));
            }
        }

        checkBill(new BaseAggVO[]{vo}, "add");
    }

    public static ArapVOData[] getOtherSystemVerifyVOData(IBusinessEvent event, String top_billtype, String idfield, String[] valuefields, boolean srcBillIdMustNull, boolean srcItemIdMustNull) {
        ArrayList<AggverifyVO> agglist = (ArrayList)((BusinessEvent)event).getObject();
        List<ArapVOData> billList = new ArrayList();
        if (agglist == null) {
            return (ArapVOData[])billList.toArray(new ArapVOData[0]);
        } else {
            for(AggverifyVO agg : agglist) {
                VerifyDetailVO[] childrenVO = (VerifyDetailVO[])agg.getChildrenVO();
                CircularlyAccessibleValueObject headVO = agg.getParentVO();

                for(VerifyDetailVO child : childrenVO) {
                    if (!child.getBusiflag().equals(ArapBillDealVOConsts.BACKONVERIFY_FLAG) && StringUtils.isEmpty(child.getBusidata().getSrc_billid()) == srcBillIdMustNull && StringUtils.isEmpty(child.getBusidata().getSrc_itemid()) == srcItemIdMustNull && child.getBusidata().getTop_billtype() != null && child.getBusidata().getTop_billtype().trim().equals(top_billtype)) {
                        ArapVOData vodata = new ArapVOData();
                        vodata.setSrcid((String)child.getBusidata().getAttributeValue(idfield));

                        for(String valueField : valuefields) {
                            if (valueField.startsWith("zb.")) {
                                vodata.getHeadData().put(valueField, headVO.getAttributeValue(valueField.substring("zb.".length())));
                            } else {
                                vodata.getHeadData().put(valueField, child.getAttributeValue(valueField));
                            }
                        }

                        billList.add(vodata);
                    }
                }
            }

            return (ArapVOData[])billList.toArray(new ArapVOData[0]);
        }
    }

    public static ArapVOData[] getOtherSystemVerifyVOData(IBusinessEvent event, String top_billtype, String idfield, String[] valuefields, String flag) {
        ArrayList<AggverifyVO> agglist = (ArrayList)((BusinessEvent)event).getObject();
        List<ArapVOData> billList = new ArrayList();
        if (agglist == null) {
            return (ArapVOData[])billList.toArray(new ArapVOData[0]);
        } else {
            for(AggverifyVO agg : agglist) {
                VerifyDetailVO[] childrenVO = (VerifyDetailVO[])agg.getChildrenVO();
                CircularlyAccessibleValueObject headVO = agg.getParentVO();

                for(VerifyDetailVO child : childrenVO) {
                    if (!child.getBusiflag().equals(ArapBillDealVOConsts.RBVERIFY_FLAG) && !child.getBusiflag().equals(ArapBillDealVOConsts.BACKONVERIFY_FLAG) && child.getBusidata().getTop_billtype() != null && top_billtype.trim().equals(child.getBusidata().getTop_billtype().trim())) {
                        if ("0".equals(flag)) {
                            if (child.getBusidata().getInsurance() != null && child.getBusidata().getInsurance().booleanValue()) {
                                ArapVOData vodata = new ArapVOData();
                                vodata.setSrcid((String)child.getBusidata().getAttributeValue(idfield));

                                for(String valueField : valuefields) {
                                    if (valueField.startsWith("zb.")) {
                                        vodata.getHeadData().put(valueField, headVO.getAttributeValue(valueField.substring("zb.".length())));
                                    } else {
                                        vodata.getHeadData().put(valueField, child.getAttributeValue(valueField));
                                    }
                                }

                                billList.add(vodata);
                            }
                        } else if ("1".equals(flag)) {
                            if (child.getBusidata().getPrepay() != null && child.getBusidata().getPrepay() == 1) {
                                ArapVOData vodata = new ArapVOData();
                                vodata.setSrcid((String)child.getBusidata().getAttributeValue(idfield));

                                for(String valueField : valuefields) {
                                    if (valueField.startsWith("zb.")) {
                                        vodata.getHeadData().put(valueField, headVO.getAttributeValue(valueField.substring("zb.".length())));
                                    } else {
                                        vodata.getHeadData().put(valueField, child.getAttributeValue(valueField));
                                    }
                                }

                                billList.add(vodata);
                            }
                        } else if ("2".equals(flag) && child.getBusidata().getPrepay() != null && child.getBusidata().getPrepay() == 0 && child.getBusidata().getInsurance() != null && !child.getBusidata().getInsurance().booleanValue()) {
                            ArapVOData vodata = new ArapVOData();
                            vodata.setSrcid((String)child.getBusidata().getAttributeValue(idfield));

                            for(String valueField : valuefields) {
                                if (valueField.startsWith("zb.")) {
                                    vodata.getHeadData().put(valueField, headVO.getAttributeValue(valueField.substring("zb.".length())));
                                } else {
                                    vodata.getHeadData().put(valueField, child.getAttributeValue(valueField));
                                }
                            }

                            billList.add(vodata);
                        }
                    }
                }
            }

            return (ArapVOData[])billList.toArray(new ArapVOData[0]);
        }
    }

    public static ArapVOData[] getOtherSystemRBVerifyVOData(IBusinessEvent event, String top_billtype, String idfield, String[] valuefields, String flag) {
        ArrayList<AggverifyVO> agglist = (ArrayList)((BusinessEvent)event).getObject();
        List<ArapVOData> billList = new ArrayList();
        if (agglist == null) {
            return (ArapVOData[])billList.toArray(new ArapVOData[0]);
        } else {
            for(AggverifyVO agg : agglist) {
                VerifyDetailVO[] childrenVO = (VerifyDetailVO[])agg.getChildrenVO();
                CircularlyAccessibleValueObject headVO = agg.getParentVO();

                for(VerifyDetailVO child : childrenVO) {
                    if (!child.getBusiflag().equals(ArapBillDealVOConsts.BACKONVERIFY_FLAG) && child.getBusiflag().equals(ArapBillDealVOConsts.RBVERIFY_FLAG) && child.getBusidata().getTop_billtype() != null && top_billtype.trim().equals(child.getBusidata().getTop_billtype().trim())) {
                        if ("0".equals(flag)) {
                            if (child.getBusidata().getInsurance() != null && child.getBusidata().getInsurance().booleanValue()) {
                                ArapVOData vodata = new ArapVOData();
                                vodata.setSrcid((String)child.getBusidata().getAttributeValue(idfield));

                                for(String valueField : valuefields) {
                                    if (valueField.startsWith("zb.")) {
                                        vodata.getHeadData().put(valueField, headVO.getAttributeValue(valueField.substring("zb.".length())));
                                    } else {
                                        vodata.getHeadData().put(valueField, child.getAttributeValue(valueField));
                                    }
                                }

                                billList.add(vodata);
                            }
                        } else if ("1".equals(flag)) {
                            if (child.getBusidata().getPrepay() == 1) {
                                ArapVOData vodata = new ArapVOData();
                                vodata.setSrcid((String)child.getBusidata().getAttributeValue(idfield));

                                for(String valueField : valuefields) {
                                    if (valueField.startsWith("zb.")) {
                                        vodata.getHeadData().put(valueField, headVO.getAttributeValue(valueField.substring("zb.".length())));
                                    } else {
                                        vodata.getHeadData().put(valueField, child.getAttributeValue(valueField));
                                    }
                                }

                                billList.add(vodata);
                            }
                        } else if ("2".equals(flag) && child.getBusidata().getPrepay() == 0 && child.getBusidata().getInsurance() != null && !child.getBusidata().getInsurance().booleanValue()) {
                            ArapVOData vodata = new ArapVOData();
                            vodata.setSrcid((String)child.getBusidata().getAttributeValue(idfield));

                            for(String valueField : valuefields) {
                                if (valueField.startsWith("zb.")) {
                                    vodata.getHeadData().put(valueField, headVO.getAttributeValue(valueField.substring("zb.".length())));
                                } else {
                                    vodata.getHeadData().put(valueField, child.getAttributeValue(valueField));
                                }
                            }

                            billList.add(vodata);
                        }
                    }
                }
            }

            return (ArapVOData[])billList.toArray(new ArapVOData[0]);
        }
    }

    public static ArapVOData[] getOtherSystemBillVOData(IBusinessEvent event, String src_syscode, String top_billtype, String idfield, String[] valuefields) {
        BaseAggVO[] otherSystemBills = getOtherSystemBills(event, src_syscode, top_billtype);
        List<ArapVOData> billList = new ArrayList();
        if (ArrayUtils.isEmpty(otherSystemBills)) {
            return (ArapVOData[])billList.toArray(new ArapVOData[0]);
        } else {
            for(BaseAggVO agg : otherSystemBills) {
                BaseBillVO headVO = agg.getHeadVO();
                BaseItemVO[] items = agg.getItems();

                for(BaseItemVO item : items) {
                    if (StringUtils.isNotEmpty((String)item.getAttributeValue(idfield))) {
                        ArapVOData vodata = new ArapVOData();
                        vodata.setSrcid((String)item.getAttributeValue(idfield));

                        for(String valueField : valuefields) {
                            if (valueField.startsWith("zb.")) {
                                vodata.getHeadData().put(valueField, headVO.getAttributeValue(valueField.substring("zb.".length())));
                            } else {
                                vodata.getHeadData().put(valueField, item.getAttributeValue(valueField));
                            }
                        }

                        billList.add(vodata);
                    }
                }
            }

            return (ArapVOData[])billList.toArray(new ArapVOData[0]);
        }
    }

    public static BaseAggVO[] getOtherSystemBills(IBusinessEvent event, String src_syscode, String top_billtype) {
        Object value = null;
        if (event instanceof BusinessEvent) {
            value = ((BusinessEvent)event).getObject();
        } else if (event instanceof BdUpdateEvent) {
            value = ((BdUpdateEvent)event).getNewObject();
        }

        List<BaseAggVO> billList = new ArrayList();
        BaseAggVO[] bills = new BaseAggVO[0];
        if (null != value) {
            if (value.getClass().isArray()) {
                Object[] objs = (Object[]) value;
                bills = objs instanceof BaseAggVO[] ? (BaseAggVO[])objs : (BaseAggVO[])ArrayUtil.convertSupers2Subs((AggregatedValueObject[])objs, BaseAggVO.class);
            } else {
                bills = new BaseAggVO[]{(BaseAggVO)value};
            }
        }

        for(BaseAggVO vo : bills) {
            BaseBillVO headVO = vo.getHeadVO();
            Integer billSrcSyscode = headVO.getSrc_syscode();
            if (src_syscode == null || billSrcSyscode != null && src_syscode.equals(billSrcSyscode == null ? "" : billSrcSyscode.toString())) {
                BaseItemVO[] items = vo.getItems();
                boolean notMatch = false;

                for(BaseItemVO item : items) {
                    if (top_billtype != null) {
                        String billTopBilltype = item.getTop_billtype();
                        if (billTopBilltype == null || !top_billtype.equals(billTopBilltype)) {
                            notMatch = true;
                            break;
                        }
                    }
                }

                if (!notMatch) {
                    billList.add(vo);
                }
            }
        }

        return (BaseAggVO[])billList.toArray(new BaseAggVO[0]);
    }

    public static BaseAggVO[] getBills(IBusinessEvent event) {
        Object value = null;
        if (event instanceof BusinessEvent) {
            value = ((BusinessEvent)event).getObject();
        } else if (event instanceof BdUpdateEvent) {
            value = ((BdUpdateEvent)event).getNewObject();
        }

        BaseAggVO[] bills = new BaseAggVO[0];
        if (null != value) {
            if (value.getClass().isArray()) {
                Object[] objs = (Object[]) value;
                bills = objs instanceof BaseAggVO[] ? (BaseAggVO[])objs : (BaseAggVO[])ArrayUtil.convertSupers2Subs((AggregatedValueObject[])objs, BaseAggVO.class);
            } else {
                bills = new BaseAggVO[]{(BaseAggVO)value};
            }
        }

        return bills;
    }

    public static boolean isOtherSystemPushBill(BaseBillVO billvo, String topBilltype) {
        Integer srcSyscode = billvo.getSrc_syscode();
        UFBoolean isflowbill = billvo.getIsflowbill();
        if (isflowbill == null) {
            isflowbill = UFBoolean.FALSE;
        }

        if (srcSyscode == null) {
            Log.getInstance("ArapVOUtils").error("srcSyscode is null");
            return false;
        } else if (StringUtils.isNotEmpty(topBilltype) && topBilltype.trim().startsWith("4A")) {
            return true;
        } else if (StringUtils.isNotEmpty(topBilltype) && "36J3".equals(topBilltype)) {
            return true;
        } else if (isflowbill.booleanValue()) {
            return false;
        } else if (srcSyscode == FromSystem.SO.VALUE) {
            return false;
        } else if (srcSyscode != FromSystem.AR.VALUE && srcSyscode != FromSystem.AP.VALUE && srcSyscode != FromSystem.WBJHPT.VALUE && srcSyscode != FromSystem.XTDJ.VALUE && srcSyscode != FromSystem.FTS.VALUE) {
            return true;
        } else {
            Log.getInstance("ArapVOUtils").error(srcSyscode);
            return false;
        }
    }

    public static boolean isArapRedBill(BaseBillVO billvo, String topBilltype) {
        boolean isRed = false;
        String pk_billtype = billvo.getPk_billtype();
        if (!StringUtil.isEmptyWithTrim(pk_billtype) && pk_billtype.equals(topBilltype)) {
            isRed = true;
        }

        return isRed;
    }

    public static void resetMoneyBal(AggregatedValueObject[] bills) {
        String pk_billtype = (String)bills[0].getParentVO().getAttributeValue("pk_billtype");
        if ("F2".equals(pk_billtype) || "F3".equals(pk_billtype)) {
            List<String> result = null;

            try {
                result = (List)(new BaseDAO()).executeQuery("select pk_recpaytype from fi_recpaytype where dr=0 and isverification<>'Y'", new ColumnListProcessor());
            } catch (DAOException e) {
                ExceptionHandler.consume(e);
            }

            for(AggregatedValueObject bill : bills) {
                CircularlyAccessibleValueObject[] bodyVOs = bill.getChildrenVO();

                for(CircularlyAccessibleValueObject obj : bodyVOs) {
                    BaseItemVO bodyVO = (BaseItemVO)obj;
                    if (result.contains(bodyVO.getPk_recpaytype())) {
                        bodyVO.setOccupationmny(UFDouble.ZERO_DBL);
                        bodyVO.setMoney_bal(UFDouble.ZERO_DBL);
                        bodyVO.setLocal_money_bal(UFDouble.ZERO_DBL);
                        bodyVO.setGroupbalance(UFDouble.ZERO_DBL);
                        bodyVO.setGlobalbalance(UFDouble.ZERO_DBL);
                    }
                }
            }
        }

    }

    public static void resetMoneyBal(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) {
        String pk_billtype = (String)bills[0].getParentVO().getAttributeValue("pk_billtype");
        Map<String, BaseItemVO> orgingitemMap = new HashMap();
        BaseItemVO[] items = (BaseItemVO[])orginBills[0].getChildrenVO();

        for(BaseItemVO item : items) {
            orgingitemMap.put(item.getPrimaryKey(), item);
        }

        if ("F2".equals(pk_billtype) || "F3".equals(pk_billtype)) {
            List<String> result = null;

            try {
                result = (List)(new BaseDAO()).executeQuery("select pk_recpaytype from fi_recpaytype where dr=0 and isverification<>'Y'", new ColumnListProcessor());
            } catch (DAOException e) {
                ExceptionHandler.consume(e);
            }

            for(AggregatedValueObject bill : bills) {
                CircularlyAccessibleValueObject[] bodyVOs = bill.getChildrenVO();

                for(CircularlyAccessibleValueObject obj : bodyVOs) {
                    BaseItemVO bodyVO = (BaseItemVO)obj;
                    if (result.contains(bodyVO.getPk_recpaytype())) {
                        bodyVO.setOccupationmny(UFDouble.ZERO_DBL);
                        bodyVO.setMoney_bal(UFDouble.ZERO_DBL);
                        bodyVO.setLocal_money_bal(UFDouble.ZERO_DBL);
                        bodyVO.setGroupbalance(UFDouble.ZERO_DBL);
                        bodyVO.setGlobalbalance(UFDouble.ZERO_DBL);
                    } else if (orgingitemMap.get(bodyVO.getPrimaryKey()) != null && result.contains(((BaseItemVO)orgingitemMap.get(bodyVO.getPrimaryKey())).getPk_recpaytype()) && !result.contains(bodyVO.getPk_recpaytype())) {
                        if ("F2".equals(pk_billtype)) {
                            bodyVO.setOccupationmny(bodyVO.getMoney_cr());
                            bodyVO.setMoney_bal(bodyVO.getMoney_cr());
                            bodyVO.setLocal_money_bal(bodyVO.getMoney_cr());
                            bodyVO.setGroupbalance(bodyVO.getMoney_cr());
                            bodyVO.setGlobalbalance(bodyVO.getMoney_cr());
                        } else if ("F3".equals(pk_billtype)) {
                            bodyVO.setOccupationmny(bodyVO.getMoney_de());
                            bodyVO.setMoney_bal(bodyVO.getMoney_de());
                            bodyVO.setLocal_money_bal(bodyVO.getMoney_de());
                            bodyVO.setGroupbalance(bodyVO.getMoney_de());
                            bodyVO.setGlobalbalance(bodyVO.getMoney_de());
                        }
                    }
                }
            }
        }

    }

    public static void checkBill(BaseAggVO[] aggvos, String actionType) throws BusinessException {
        if (aggvos != null && aggvos.length != 0) {
            for(BaseAggVO aggvo : aggvos) {
                if (aggvo != null) {
                    BaseBillVO bill = (BaseBillVO)aggvo.getParentVO();
                    String pk_tradetype = (String)bill.getAttributeValue("pk_tradetype");
                    String pk_billtype = (String)bill.getAttributeValue("pk_billtype");
                    if (bill.getBillclass().equals(ArapConstant.ARAP_ZF_BILLCLASS) && !StringUtil.isEmpty(pk_tradetype)) {
                        DjLXVO[] vos = ArapBillPubUtil.queryTradetypeBycodes(pk_billtype, pk_tradetype);
                        if ("add".equals(actionType) && (vos == null || vos.length == 0)) {
                            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("2006pub_0", "02006pub-1555", (String)null, new String[]{pk_tradetype}));
                        }
                    }
                }
            }

        }
    }
}
