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

package nc.bs.arap.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import nc.bs.arap.bill.ArapBillPubUtil;
import nc.bs.arap.billact.IValidatorFactory;
import nc.bs.arap.billact.UpdateBatchBSAction;
import nc.bs.arap.busireg.ArapBillUpdateChecker;
import nc.bs.arap.busireg.BillUpdateChecker;
import nc.bs.arap.util.ArapBillVOUtils;
import nc.bs.arap.util.ArapBusiLogUtils;
import nc.bs.arap.util.ArapVOUtils;
import nc.bs.arap.util.BillAccountCalendarUtils;
import nc.bs.arap.util.BillDateUtils;
import nc.bs.arap.util.BillStatusUtils;
import nc.bs.arap.util.BillTermUtils;
import nc.bs.arap.validator.CrossRuleCheckValidator;
import nc.bs.arap.validator.ValidatorFactory;
import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.EventDispatcher;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.impl.pubapp.env.BSContext;
import nc.itf.imag.lightbus.IImagPubService;
import nc.md.MDBaseQueryFacade;
import nc.md.model.IAttribute;
import nc.md.model.IBean;
import nc.pubitf.para.SysInitQuery;
import nc.vo.arap.basebill.BaseAggVO;
import nc.vo.arap.basebill.BaseBillVO;
import nc.vo.arap.basebill.BaseItemVO;
import nc.vo.arap.pub.ArapConstant;
import nc.vo.arap.pub.BillEnumCollection.BillSatus;
import nc.vo.arap.pub.BillEnumCollection.FromSystem;
import nc.vo.arap.utils.ARAPModuleEnableUtil;
import nc.vo.arap.utils.StringUtil;
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;

public abstract class BillUpdateBatchBSAction extends UpdateBatchBSAction {
    protected static int TEMP_2_TEMP = 0;
    protected static int TEMP_2_SAVE = 1;
    protected static int SAVE_2_TEMP = 2;
    protected static int SAVE_2_SAVE = 3;
    protected Map<String, Integer> childvostatmap;

    public BillUpdateBatchBSAction() {
        this.validatorCode.add(77);
        this.validatorCode.add(84);
        this.validatorCode.add(66);
        this.validatorCode.add(15);
        this.validatorCode.add(8);
        this.validatorCode.add(44);
        this.validatorCode.add(39);
        this.validatorCode.add(26);
        this.validatorCode.add(49);
        this.validatorCode.add(78);
        this.validatorCode.add(79);
        this.validatorCode.add(81);
        this.childvostatmap = null;
    }

    protected IValidatorFactory getValidatorFactory() {
        ValidatorFactory vf = new ValidatorFactory();
        return vf;
    }

    protected void doAfterFireEvent(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) throws BusinessException {
        int updateType = this.getUpdateType(bills, orginBills);
        if (updateType == TEMP_2_SAVE) {
            EventDispatcher.fireEvent(new BusinessEvent((String)this.tmpMap.get("beanid"), "1002", bills));
        } else if (updateType == SAVE_2_TEMP) {
            EventDispatcher.fireEvent(new BusinessEvent((String)this.tmpMap.get("beanid"), "1006", orginBills));
        } else if (updateType == SAVE_2_SAVE) {
            this.afterEventSave2Save(bills, orginBills);
        }

    }

    protected void afterEventSave2Save(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) throws BusinessException {
        EventDispatcher.fireEvent(new BdUpdateEvent((String)this.tmpMap.get("beanid"), "1004", orginBills, bills));
    }

    protected void doBeforeFireEvent(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) throws BusinessException {
        int updateType = this.getUpdateType(bills, orginBills);
        ArapConstant constant = new ArapConstant();
        BSContext.getInstance().setSession(ArapConstant.ARAP_TBB_ACTIONNAME, constant.ARAP_NTB_EDIT_KEY);
        if (bills != null && bills.length > 0 && ((BaseAggVO)bills[0]).isAlarmPassed()) {
            BSContext.getInstance().setSession(ArapConstant.ARAP_TBB_FLAG, ArapConstant.ARAP_TBB_NOCHECK);
        }

        if (updateType == TEMP_2_SAVE) {
            EventDispatcher.fireEvent(new BusinessEvent((String)this.tmpMap.get("beanid"), "1005", orginBills));
            EventDispatcher.fireEvent(new BusinessEvent((String)this.tmpMap.get("beanid"), "1006", orginBills));
            EventDispatcher.fireEvent(new BusinessEvent((String)this.tmpMap.get("beanid"), "1001", bills));
        } else if (updateType == SAVE_2_TEMP) {
            EventDispatcher.fireEvent(new BusinessEvent((String)this.tmpMap.get("beanid"), "1005", bills));
        } else if (updateType == SAVE_2_SAVE) {
            this.beforeEventSave2Save(bills, orginBills);
        }

    }

    protected void beforeEventSave2Save(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) throws BusinessException {
        EventDispatcher.fireEvent(new BdUpdateEvent((String)this.tmpMap.get("beanid"), "1003", orginBills, bills));
    }

    public int getUpdateType(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) {
        int oldStatus = ((BaseBillVO)orginBills[0].getParentVO()).getBillstatus();
        int newStatus = ((BaseBillVO)bills[0].getParentVO()).getBillstatus();
        if (oldStatus != BillSatus.TEMPEORARY.VALUE && oldStatus != BillSatus.UNCOMFIRM.VALUE) {
            return newStatus != BillSatus.TEMPEORARY.VALUE && newStatus != BillSatus.UNCOMFIRM.VALUE ? SAVE_2_SAVE : SAVE_2_TEMP;
        } else {
            return newStatus != BillSatus.TEMPEORARY.VALUE && newStatus != BillSatus.UNCOMFIRM.VALUE ? TEMP_2_SAVE : TEMP_2_TEMP;
        }
    }

    protected void doAfterUpdate(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) throws BusinessException {
        BillTermUtils.dealBillTerm(bills, this.childvostatmap, orginBills);
        BillTermUtils.retBodyTermVOS(bills);
        ArapBusiLogUtils.insertSmartBusiLogs(ArapConstant.ARAP_ACTION_EDIT, (BaseAggVO)bills[0], (BaseAggVO)orginBills[0], ArapConstant.SYS_NAME);

        for(AggregatedValueObject bill : bills) {
            if (bill.getParentVO().getAttributeValue("coordflag") != null && "1".equals(bill.getParentVO().getAttributeValue("coordflag").toString()) && ARAPModuleEnableUtil.isImagEnabled(InvocationInfoProxy.getInstance().getGroupId())) {
                IImagPubService imagService = (IImagPubService)NCLocator.getInstance().lookup(IImagPubService.class);
                imagService.updateBillNo(bill.getParentVO().getPrimaryKey(), (String)bill.getParentVO().getAttributeValue("billno"));
            }
        }

    }

    protected void doBeforeUpdate(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) throws BusinessException {
        ArapBillPubUtil.fillTradeTypeInfo(bills);

        for(AggregatedValueObject bill : bills) {
            ArapBillPubUtil.processMoneyOnlySum(bill);
        }

        ArapBillVOUtils.prepareDefaultInfo(bills, true);
        BillDateUtils.setBillDateByNow(bills);
        int updateType = this.getUpdateType(bills, orginBills);

        for(AggregatedValueObject bill : bills) {
            bill.getParentVO().setStatus(1);
        }

        if (updateType == TEMP_2_SAVE || updateType == SAVE_2_SAVE) {
            for(AggregatedValueObject bill : bills) {
                for(CircularlyAccessibleValueObject item : bill.getChildrenVO()) {
                    if (item.getStatus() != 2 && item.getStatus() != 3) {
                        item.setStatus(1);
                    }
                }
            }
        }

        if (updateType == TEMP_2_SAVE || updateType == SAVE_2_SAVE) {
            BillAccountCalendarUtils.setAccperiodYearMonth(bills);
        }

        this.checkIsCorrdBillMoneyControl(bills, orginBills);
        this.checkOtherSystemBill(bills, orginBills);
        (new CrossRuleCheckValidator()).validate(bills);
    }

    protected void checkOtherSystemBill(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) throws BusinessException {
        ArapBillUpdateChecker defaultCheck = new ArapBillUpdateChecker();

        try {
            int i = 0;

            for(AggregatedValueObject newbill : bills) {
                BaseAggVO bill = (BaseAggVO)newbill;
                BaseAggVO oriBill = (BaseAggVO)orginBills[i++];
                BaseItemVO[] vosOld = (BaseItemVO[])oriBill.getChildrenVO();
                String top_billtype = vosOld[0].getTop_billtype();
                List<BillUpdateChecker> executeCheckers = new ArrayList();
                executeCheckers.add(defaultCheck);
                Map<String, List<BaseItemVO>> rowsChangeMap = this.getBillRowsChangeMap(bill, oriBill);
                List<BaseItemVO> addlines = (List)rowsChangeMap.get("bodyAddvos");
                List<BaseItemVO> deletelines = (List)rowsChangeMap.get("bodyDeletevos");
                Map<String, Map<String, Object[]>> bodyfieldchange = this.getBodyFieldValueChangeMap(bill, oriBill);
                Map<String, Object[]> headfieldchange = this.getHeadFieldValueChangeMap(bill, oriBill);

                for(BillUpdateChecker billUpdateChecker : executeCheckers) {
                    if (!billUpdateChecker.canUpdateBillWithoutCtrl(bill)) {
                        if (!addlines.isEmpty()) {
                            String errmsg = billUpdateChecker.canAddLine(top_billtype, addlines);
                            if (!StringUtil.isEmpty(errmsg)) {
                                throw new BusinessException(errmsg);
                            }
                        }

                        if (!deletelines.isEmpty()) {
                            String errmsg = billUpdateChecker.canDeleteLine(top_billtype, deletelines);
                            if (!StringUtil.isEmpty(errmsg)) {
                                throw new BusinessException(errmsg);
                            }
                        }

                        if (!bodyfieldchange.isEmpty()) {
                            String errmsg = billUpdateChecker.canUpdateBodyFieldValue(bodyfieldchange, bill, oriBill);
                            if (!StringUtil.isEmpty(errmsg)) {
                                throw new BusinessException(errmsg);
                            }
                        }

                        if (!headfieldchange.isEmpty()) {
                            String errmsg = billUpdateChecker.canUpdateHeadFieldValue(headfieldchange, bill, oriBill);
                            if (!StringUtil.isEmpty(errmsg)) {
                                throw new BusinessException(errmsg);
                            }
                        }
                    }
                }
            }

        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    private Map<String, Object[]> getHeadFieldValueChangeMap(BaseAggVO bill, BaseAggVO oriBill) throws BusinessException {
        Map<String, Object[]> headfieldchange = new HashMap();
        BaseBillVO pvo = (BaseBillVO)bill.getParentVO();
        BaseBillVO pvoold = (BaseBillVO)oriBill.getParentVO();
        IBean bean = MDBaseQueryFacade.getInstance().getBeanByFullClassName(pvo.getClass().getName());

        for(IAttribute attr : bean.getAttributesOfModel()) {
            if (!attr.isCalculation()) {
                String fieldcode = attr.getName();
                if (!BaseBillVO.UPDATEIGNORE_LIST.contains(fieldcode)) {
                    Object value = pvo.getAttributeValue(fieldcode);
                    Object oldvalue = pvoold.getAttributeValue(fieldcode);
                    if (!this.simpEquals(value, oldvalue)) {
                        headfieldchange.put(fieldcode, new Object[]{value, oldvalue});
                    }
                }
            }
        }

        return headfieldchange;
    }

    private Map<String, Map<String, Object[]>> getBodyFieldValueChangeMap(BaseAggVO bill, BaseAggVO oriBill) throws BusinessException {
        Map<String, Map<String, Object[]>> map = new HashMap();
        BaseItemVO[] vosNew = (BaseItemVO[])bill.getChildrenVO();
        BaseItemVO[] vosOld = (BaseItemVO[])oriBill.getChildrenVO();
        Map<String, BaseItemVO> mapOld = new HashMap();

        for(BaseItemVO vo : vosOld) {
            mapOld.put(vo.getPrimaryKey(), vo);
        }

        IBean bean = MDBaseQueryFacade.getInstance().getBeanByFullClassName(vosNew[0].getClass().getName());
        List<IAttribute> attrs = bean.getAttributesOfModel();

        for(BaseItemVO itemvo : vosNew) {
            if (itemvo.getPrimaryKey() != null) {
                Map<String, Object[]> bodyfieldchange = new HashMap();
                map.put(itemvo.getPrimaryKey(), bodyfieldchange);
                BaseItemVO itemvoOld = (BaseItemVO)mapOld.get(itemvo.getPrimaryKey());

                for(IAttribute attr : attrs) {
                    if (!attr.isCalculation()) {
                        String fieldcode = attr.getName();
                        Object oldvalue = itemvoOld.getAttributeValue(fieldcode);
                        Object value = itemvo.getAttributeValue(fieldcode);
                        if (!this.simpEquals(value, oldvalue)) {
                            bodyfieldchange.put(fieldcode, new Object[]{value, oldvalue});
                        }
                    }
                }
            }
        }

        return map;
    }

    private Map<String, List<BaseItemVO>> getBillRowsChangeMap(AggregatedValueObject bill, AggregatedValueObject orginBill) throws BusinessException {
        Map<String, List<BaseItemVO>> map = new HashMap();
        List<BaseItemVO> addlist = new ArrayList();
        List<BaseItemVO> deletelist = new ArrayList();
        map.put("bodyAddvos", addlist);
        map.put("bodyDeletevos", deletelist);
        BaseItemVO[] vosOld = (BaseItemVO[])orginBill.getChildrenVO();
        Map<String, BaseItemVO> mapOld = new HashMap();
        BaseItemVO[] vosNew = (BaseItemVO[])bill.getChildrenVO();

        for(BaseItemVO vo : vosOld) {
            mapOld.put(vo.getPrimaryKey(), vo);
        }

        for(BaseItemVO vo : vosNew) {
            if (vo.getPrimaryKey() == null) {
                addlist.add(vo);
            } else {
                mapOld.remove(vo.getPrimaryKey());
            }
        }

        if (mapOld.size() > 0) {
            for(Map.Entry<String, BaseItemVO> deletentry : mapOld.entrySet()) {
                deletelist.add((BaseItemVO)deletentry.getValue());
            }
        }

        return map;
    }

    private boolean simpEquals(Object s1, Object s2) {
        if (s1 == null && s2 == null) {
            return true;
        } else {
            return s1 != null && s2 != null ? s1.equals(s2) : false;
        }
    }

    protected void checkIsCorrdBillMoneyControl(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) throws BusinessException {
        try {
            int i = 0;

            for(AggregatedValueObject bill : bills) {
                AggregatedValueObject oriBill = orginBills[i++];
                BaseBillVO billVO = (BaseBillVO)bill.getParentVO();
                BaseBillVO billOriVO = (BaseBillVO)oriBill.getParentVO();
                if (billVO.getSrc_syscode() == FromSystem.XTDJ.VALUE && this.isCorrdBillMoneyControl(billVO)) {
                    if (!billVO.getMoney().equals(billOriVO.getMoney())) {
                        throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("2006pub_0", "02006pub-0434"));
                    }

                    CircularlyAccessibleValueObject[] childrenVO = bill.getChildrenVO();
                    CircularlyAccessibleValueObject[] orichildrenVO = oriBill.getChildrenVO();

                    for(int j = 0; j < Math.min(childrenVO.length, orichildrenVO.length); ++j) {
                        if (!Objects.equals(childrenVO[j].getAttributeValue("pk_currtype"), orichildrenVO[j].getAttributeValue("pk_currtype"))) {
                            throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("2006arappub0316_0", "02006arappub0316-0029"));
                        }
                    }
                }
            }

        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    private boolean isCorrdBillMoneyControl(BaseBillVO headVO) throws BusinessException {
        UFBoolean flag = UFBoolean.FALSE;
        boolean exp = headVO.getSyscode() == FromSystem.AR.VALUE || ArapBillPubUtil.isARSysBilltype(headVO.getPk_billtype());
        flag = SysInitQuery.getParaBoolean(headVO.getPk_org(), exp ? "AR18" : "AP18");
        return flag.booleanValue();
    }

    protected AggregatedValueObject[] doUpdate(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) throws BusinessException {
        this.childvostatmap = BillStatusUtils.enUpdateVOStauts(bills, orginBills);
        this.beforeUpdateDefValue(bills, orginBills);
        ArapVOUtils.resetMoneyBal(bills);
        return super.doUpdate(bills, orginBills);
    }

    protected void beforeUpdateDefValue(AggregatedValueObject[] bills, AggregatedValueObject[] orginBills) {
        BillStatusUtils.enCUDVODefVals(bills, true, orginBills);
    }
}
