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

package nccloud.pubimpl.ic.outbound.service;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.impl.pubapp.pattern.database.IDQueryBuilder;
import nc.itf.scmpub.reference.uap.pf.PfServiceScmUtil;
import nc.itf.uap.pf.IPfExchangeService;
import nc.vo.ic.general.deal.TransBillBaseProcess;
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.m4455.entity.SapplyBillVO;
import nc.vo.ic.m4c.deal.Bill4CFor45Process;
import nc.vo.ic.m4c.entity.SaleOutVO;
import nc.vo.ic.material.query.InvInfoQuery;
import nc.vo.ic.org.OrgInfoQuery;
import nc.vo.ic.param.ICSysParam;
import nc.vo.ic.pub.calc.BusiCalculator;
import nc.vo.ic.pub.define.ICContext;
import nc.vo.ic.pub.pf.billtype.BillTypeInfoQuery;
import nc.vo.ic.pub.util.StringUtil;
import nc.vo.ic.pub.util.VOEntityUtil;
import nc.vo.ic.pub.util.ValueCheckUtil;
import nc.vo.ic.warehouse.WarehouseInfoQuery;
import nc.vo.ml.LanguageVO;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pickm.entity.PickmHeadVO;
import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.pfflow01.BillbusinessVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.scale.ScaleUtils;
import nc.vo.pubapp.util.VORowNoUtils;
import nc.vo.scmpub.res.billtype.ICBillType;
import nc.vo.scmpub.res.billtype.SOBillType;
import nccloud.dto.ic.pub.entity.TransVoInfo;
import nccloud.dto.ic.pub.entity.TransferInfo;
import nccloud.pubitf.ic.outbound.service.IOutboundTransferVoService1;

import java.util.ArrayList;
import java.util.List;

public class OutboundTransferVoServiceImpl1 implements IOutboundTransferVoService1 {
    public static final Integer ZERO_INTEGER = 0;
    private ICContext context = new ICContext() {
        public String getUserID() {
            return InvocationInfoProxy.getInstance().getUserId();
        }

        public UFDateTime getServerDateTime() {
            return null;
        }

        public String getPk_group() {
            return InvocationInfoProxy.getInstance().getGroupId();
        }

        public String getModuleEnableDate() {
            return null;
        }

        public LanguageVO getCurrentLanguage() {
            return null;
        }

        public UFDateTime getBizDateTime() {
            return new UFDateTime(InvocationInfoProxy.getInstance().getBizDateTime());
        }

        public UFDate getBizDate() {
            return new UFDate(InvocationInfoProxy.getInstance().getBizDateTime());
        }

        public String getBDDocMultLangName(String bddocname) {
            return null;
        }
    };

    public SapplyBillVO[] transPickmTo4455(TransferInfo info) {
        if (null == info) {
            return null;
        } else {
            TransVoInfo[] voInfos = info.getInfo();
            if (voInfos.length < 1) {
                return null;
            } else {
                VOQuery<PickmHeadVO> query = new VOQuery(PickmHeadVO.class);
                String[] hids = info.getInfo()[0].getHids();
                PickmHeadVO[] hvos = (PickmHeadVO[]) query.query(hids);
                VOQuery<PickmItemVO> queryb = new VOQuery(PickmItemVO.class);
                String[] bids = info.getInfo()[0].getBids();
                PickmItemVO[] bvos = (PickmItemVO[]) queryb.query(bids);
                /**
                 * 查询数量的限制来自于 nc.impl.pubapp.pattern.data.table.TableIDQueryCondition#TableIDQueryCondition(String[] ids)
                 */
                if ((hvos == null || hvos.length == 0) && hids.length > IDQueryBuilder.getMaxInCount()) {
                    ExceptionUtils.wrappBusinessException("选择的备料计划数量不能大于最大限制" + IDQueryBuilder.getMaxInCount());
                    return null;
                }
                if ((bvos == null || bvos.length == 0) && bids.length > IDQueryBuilder.getMaxInCount()) {
                    ExceptionUtils.wrappBusinessException("选择的子表数量不能大于最大限制" + IDQueryBuilder.getMaxInCount());
                    return null;
                }

                AggPickmVO[] volist = new AggPickmVO[hvos.length];

                for (int i = 0; i < hvos.length; ++i) {
                    AggPickmVO vo = new AggPickmVO();
                    vo.setParentVO(hvos[i]);
                    List<PickmItemVO> bodyvo = new ArrayList();

                    for (int j = 0; j < bvos.length; ++j) {
                        if (bvos[j].getCpickmid().equals(hvos[i].getCpickmid())) {
                            bodyvo.add(bvos[j]);
                        }
                    }

                    vo.setChildrenVO((CircularlyAccessibleValueObject[]) bodyvo.toArray(new PickmItemVO[0]));
                    volist[i] = vo;
                }

                IPfExchangeService service = (IPfExchangeService) NCLocator.getInstance().lookup(IPfExchangeService.class);
                AggregatedValueObject[] vochange = new AggregatedValueObject[0];

                try {
                    String cbilltype = info.getInfo()[0].getCbilltype();
                    vochange = service.runChangeDataAryNeedClassify(cbilltype, ICBillType.SapplyBill.getCode(), volist, (PfParameterVO) null, -1);
                } catch (BusinessException e) {
                    ExceptionUtils.wrappBusinessException(e.getMessage());
                }

                vochange = this.addFromSource(vochange);
                return (SapplyBillVO[]) ((SapplyBillVO[]) vochange);
            }
        }
    }


    private AggregatedValueObject[] addFromSource(AggregatedValueObject[] retvos) {
        if (retvos.length == 0) {
            return retvos;
        }
        this.processRowNO(this.processAfterVoChange(retvos));
        if (retvos instanceof SaleOutVO[]) {
            Bill4CFor45Process proc = new Bill4CFor45Process(false);
            proc.setBPushSave(false);
            proc.setBusiCalc(BusiCalculator.getBusiCalculatorAtBS());
            proc.setIcSysParam(new ICSysParam(InvocationInfoProxy.getInstance().getGroupId()));
            proc.setInvQuery(new InvInfoQuery());
            proc.setOrgInfoQry(new OrgInfoQuery());
            proc.setScale(ScaleUtils.getScaleUtilAtBS());
            proc.setWhQuery(new WarehouseInfoQuery());
            proc.setBilltypeQuery(new BillTypeInfoQuery());
            proc.setContext(this.context);
            retvos = proc.changeToSaleOutVO((SaleOutVO[]) retvos);
            this.checkCbiz((SaleOutVO[]) retvos);
        }

        return retvos;
    }

    protected AggregatedValueObject[] processAfterVoChange(AggregatedValueObject[] retvos) {
        this.setClientDbilldate(retvos);
        TransBillBaseProcess<ICBillVO, ICBillHeadVO, ICBillBodyVO> process = new TransBillBaseProcess(false, false);
        process.setBusiCalc(BusiCalculator.getBusiCalculatorAtBS());
        process.setIcSysParam(new ICSysParam(InvocationInfoProxy.getInstance().getGroupId()));
        process.setInvQuery(new InvInfoQuery());
        process.setOrgInfoQry(new OrgInfoQuery());
        process.setScale(ScaleUtils.getScaleUtilAtBS());
        process.setWhQuery(new WarehouseInfoQuery());
        process.setBilltypeQuery(new BillTypeInfoQuery());
        process.setContext(this.context);
        return process.processBillVOs((ICBillVO[]) retvos);
    }

    protected void setClientDbilldate(AggregatedValueObject[] retvos) {
        long time = InvocationInfoProxy.getInstance().getBizDateTime();
        UFDate date = new UFDate(time);

        for (AggregatedValueObject retvo : retvos) {
            ICBillVO billvo = (ICBillVO) retvo;
            billvo.getHead().setDbilldate(date);
        }

    }

    protected AggregatedValueObject[] processRowNO(AggregatedValueObject[] retvos) {
        if (ValueCheckUtil.isNullORZeroLength(retvos)) {
            return retvos;
        } else {
            long time = InvocationInfoProxy.getInstance().getBizDateTime();
            UFDate date = new UFDate(time);

            for (AggregatedValueObject bill : retvos) {
                CircularlyAccessibleValueObject[] bodys = bill.getChildrenVO();
                if (!ValueCheckUtil.isNullORZeroLength(bodys)) {
                    for (CircularlyAccessibleValueObject body : bodys) {
                        body.setAttributeValue("crowno", (Object) null);
                        if (body.getAttributeValue("nnum") != null && body.getAttributeValue("dbizdate") == null) {
                            body.setAttributeValue("dbizdate", date);
                        }
                    }

                    VORowNoUtils.setVOsRowNoByRule(bodys, "crowno");
                }
            }

            return retvos;
        }
    }

    private void checkCbiz(SaleOutVO[] vos) {
        String[] cbiztypes = VOEntityUtil.getVOsNotRepeatValue(VOEntityUtil.getHeadVOs(vos), "cbiztype");
        if (!ValueCheckUtil.isNullORZeroLength(cbiztypes)) {
            String pk_group = InvocationInfoProxy.getInstance().getGroupId();
            String arriveCode = SOBillType.Delivery.getCode();

            for (String biztype : cbiztypes) {
                BillbusinessVO[] businessVOs = PfServiceScmUtil.findBillbusinessVOs(biztype, pk_group);
                if (!ValueCheckUtil.isNullORZeroLength(businessVOs)) {
                    for (BillbusinessVO vo : businessVOs) {
                        if (StringUtil.isStringEqual(vo.getPk_billtype(), arriveCode)) {
                            ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4008003_0", "04008003-0251"));
                        }
                    }
                }
            }

        }
    }

}
