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

package nc.pubimpl.so.sobalance.arap.listener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.ml.NCLangResOnserver;
import nc.impl.pubapp.bill.convertor.ViewToBillConvertor;
import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.itf.arap.forso.IDataFromF2ForM30;
import nc.pubitf.arap.pub.GetSODataByArapUtils;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.so.m30.entity.SaleOrderHVO;
import nc.vo.so.m30.sobalance.entity.SoBalanceBVO;
import nc.vo.so.m30.sobalance.entity.SoBalanceHVO;
import nc.vo.so.m30.sobalance.entity.SoBalanceVO;
import nc.vo.so.m30.sobalance.entity.SoBalanceViewVO;
import nc.vo.so.m30.sobalance.enumeration.SoBalanceType;

public class GatheringUpdateBeforeListener implements IBusinessListener {
    public GatheringUpdateBeforeListener() {
    }

    private Map<String, IDataFromF2ForM30> organizeDataMap(IDataFromF2ForM30[] datas) {
        Map<String, IDataFromF2ForM30> retMap = new HashMap();

        for(IDataFromF2ForM30 data : datas) {
            retMap.put(data.getPayBillRowID(), data);
        }

        return retMap;
    }

    private Map<String, SoBalanceVO> organizeBalanceMap(SoBalanceVO[] vos) {
        Map<String, SoBalanceVO> balanceMap = new HashMap();
        if (vos != null && vos.length > 0) {
            for(SoBalanceVO vo : vos) {
                SoBalanceBVO[] bodys = vo.getChildrenVO();

                for(SoBalanceBVO body : bodys) {
                    balanceMap.put(body.getCpaybillrowid(), vo);
                }
            }
        }

        return balanceMap;
    }

    public void doAction(IBusinessEvent event) throws BusinessException {
        IDataFromF2ForM30[] datas = (new GetSODataByArapUtils()).getIDataFromF2ForM30(event);
        Map<String, IDataFromF2ForM30> dataMap = this.organizeDataMap(datas);
        String[] payBillRowIDs = (String[])dataMap.keySet().toArray(new String[dataMap.keySet().size()]);
        if (payBillRowIDs != null && payBillRowIDs.length != 0) {
            SoBalanceViewVO[] views = ArListenerUtils.getInstance().querySoBalanceViewByGatheringBillBodyIDs(payBillRowIDs);
            ViewToBillConvertor<SoBalanceViewVO, SoBalanceVO> convert = new ViewToBillConvertor(SoBalanceVO.class);
            SoBalanceVO[] vos = (SoBalanceVO[])convert.convert(views);
            if (vos != null && vos.length != 0) {
                Map<String, SoBalanceVO> balanceMap = this.organizeBalanceMap(vos);
                this.checkDatas(datas, balanceMap);
            }
        }
    }

    private void checkDatas(IDataFromF2ForM30[] datas, Map<String, SoBalanceVO> balanceMap) throws BusinessException {
        StringBuffer errMsg = new StringBuffer();
        List<IDataFromF2ForM30> deleteList = new ArrayList();
        List<IDataFromF2ForM30> updateList = new ArrayList();

        for(IDataFromF2ForM30 data : datas) {
            if (3 == data.getRowStatus()) {
                deleteList.add(data);
            } else if (1 == data.getRowStatus()) {
                updateList.add(data);
            }
        }

        if (deleteList.size() > 0) {
            IDataFromF2ForM30[] deleteDatas = (IDataFromF2ForM30[])deleteList.toArray(new IDataFromF2ForM30[deleteList.size()]);

            for(IDataFromF2ForM30 data : deleteDatas) {
                if (balanceMap.containsKey(data.getPayBillRowID())) {
                    String payBillNo = data.getPayBillNo();
                    String billRowID = ((SoBalanceVO)balanceMap.get(data.getPayBillRowID())).getParentVO().getVbillcode();
                    errMsg.append(NCLangResOnserver.getInstance().getStrByID("4006011_0", "04006011-0363", (String)null, new String[]{payBillNo, billRowID}));
                    errMsg.append("\n");
                }
            }
        }

        if (updateList.size() > 0) {
            IDataFromF2ForM30[] updateDatas = (IDataFromF2ForM30[])updateList.toArray(new IDataFromF2ForM30[updateList.size()]);
            List<String> list = new ArrayList();
            Map<String, String> map = new HashMap();
            Map<String, String> map2 = new HashMap();
            Map<String, String> map3 = new HashMap();

            for(String payBillRowid : balanceMap.keySet()) {
                map.put(((SoBalanceVO)balanceMap.get(payBillRowid)).getParentVO().getCsobalanceid(), ((SoBalanceVO)balanceMap.get(payBillRowid)).getParentVO().getCsaleorderid());
                list.add(((SoBalanceVO)balanceMap.get(payBillRowid)).getParentVO().getCsaleorderid());
            }

            VOQuery<SaleOrderHVO> voQuery = new VOQuery(SaleOrderHVO.class);
            SaleOrderHVO[] heads = (SaleOrderHVO[])voQuery.query((String[])list.toArray(new String[list.size()]));
            if (heads == null || heads.length == 0) {
                return;
            }

            for(int i = 0; i < heads.length; ++i) {
                map2.put(heads[i].getCsaleorderid(), heads[i].getVbillcode());
            }

            for(String string : map.keySet()) {
                map3.put(string, (String)map2.get(map.get(string)));
            }

            for(IDataFromF2ForM30 data : updateDatas) {
                if (balanceMap.containsKey(data.getPayBillRowID())) {
                    SoBalanceVO sbVO = (SoBalanceVO)balanceMap.get(data.getPayBillRowID());
                    this.checkGatheringbillAndSoBalanceConsistent(data, sbVO, map3);
                }
            }
        }

        if (errMsg.length() > 0) {
            throw new BusinessException(errMsg.toString());
        }
    }

    private void checkGatheringbillAndSoBalanceConsistent(IDataFromF2ForM30 data, SoBalanceVO sbVO, Map<String, String> map) throws BusinessException {
        Map<String, UFDouble> balancePaybillMap = new HashMap();
        SoBalanceHVO head = sbVO.getParentVO();
        String ccustomerid = head.getCcustomerid();
        String cinvoicecustid = head.getCinvoicecustid();
        String corigcurrencyid = head.getCorigcurrencyid();
        String carorgid = head.getCarorgid();
        SoBalanceBVO[] bodys = sbVO.getChildrenVO();
        int fibaltype = 1;

        for(SoBalanceBVO body : bodys) {
            balancePaybillMap.put(body.getCpaybillrowid(), body.getNorigarmny());
            if (body.getCpaybillrowid().equals(data.getPayBillRowID())) {
                fibaltype = body.getFibaltype();
                break;
            }
        }

        if (SoBalanceType.SOBALANCE_ORDERBAL.getIntValue() == fibaltype) {
            if (!ccustomerid.equals(data.getPayBillOrderCust())) {
                if(!(data.getFirstBillType().equals("30")&&head.getMetaData().getEntityName().equals("so.so_balance")) ) {
                    throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4006011_0", "04006011-0193", (String) null, new String[]{(String) map.get(head.getCsobalanceid())}));
                }
            }

            if (!cinvoicecustid.equals(data.getPayBillCustomer())) {
                if(!(data.getFirstBillType().equals("30")&&head.getMetaData().getEntityName().equals("so.so_balance"))) {
                throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4006011_0", "04006011-0194", (String)null, new String[]{(String)map.get(head.getCsobalanceid())}));
                }
            }

            if (!corigcurrencyid.equals(data.getPayBillCurID())) {
                throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4006011_0", "04006011-0195", (String)null, new String[]{(String)map.get(head.getCsobalanceid())}));
            }

            if (!carorgid.equals(data.getPayBillOrg())) {
                throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4006011_0", "04006011-0196", (String)null, new String[]{(String)map.get(head.getCsobalanceid())}));
            }

            if (!((UFDouble)balancePaybillMap.get(data.getPayBillRowID())).equals(data.getPayBillmny())) {
                if(!(data.getFirstBillType().equals("30")&&head.getMetaData().getEntityName().equals("so.so_balance")) ) {
                    throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4006011_0", "04006011-0612", (String)null, new String[]{(String)map.get(head.getCsobalanceid())}));
                }
            }
        }

    }
}
