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

package nc.bs.pub.action;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.ic.pub.env.ICBSContext;
import nc.bs.ic.pub.util.ICBillVOQuery;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.bs.scmpub.pf.PfParameterUtil;
import nc.itf.ic.m4455.self.ISapplyBillMaintain;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.ic.m4455.entity.SapplyBillBodyVO;
import nc.vo.ic.m4455.entity.SapplyBillVO;
import nc.vo.ic.pub.pf.ICPFParameter;
import nc.vo.ic.pub.util.ValueCheckUtil;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.MathTool;
import nccloud.baseapp.core.log.NCCForUAPLogger;

import java.util.Hashtable;

/**
 * 合并备料计划保存前校验申请数量是否超过计划数量-2005适配2312
 *
 * @author mzr
 * @date 2025/3/26
 */
public class N_4455_WRITE extends AbstractCompiler2 {
    private Hashtable m_keyHas = null;
    private Hashtable m_methodReturnHas = new Hashtable();

    private BaseDAO baseDAO = null;

    public N_4455_WRITE() {
    }

    public Object runComClass(PfParameterVO vo) throws BusinessException {
        try {
            super.m_tmpVo = vo;
            ICBSContext context = new ICBSContext();
            ICPFParameter pfparam = (ICPFParameter) this.getUserObj();
            if (pfparam == null) {
                pfparam = new ICPFParameter();
            }

            if (ValueCheckUtil.isNullORZeroLength(pfparam.getBusinessCheckMap())) {
                pfparam.setBAtpCheckFlag(false);
            }

            context.setICPFParameter(pfparam);
            SapplyBillVO[] outVOs = (SapplyBillVO[]) this.getVos();
            // 保存前校验申请数量是否超过计划数量
            for (SapplyBillVO billVO : outVOs) {
                SapplyBillBodyVO[] bodyVOS = billVO.getBodys();
                for (SapplyBillBodyVO bodyVO : bodyVOS) {
                    // 只判断来源单据是备料计划的出库申请
                    if (!"55A3".equals(bodyVO.getCsourcetype())) {
                        continue;
                    }
                    String csourcebillbid = bodyVO.getCsourcebillbid();
                    // 查询备料计划中的计划出库数量，通过备料计划的表体主键
                    String getPlanNumSql = "SELECT nplanoutastnum "
                            + "FROM mm_pickm_b "
                            + "WHERE NVL(dr,0) = 0 AND cpickm_bid = '[csourcebillbid]'";
                    getPlanNumSql = getPlanNumSql.replace("[csourcebillbid]", csourcebillbid);
                    Object nplanoutastnum = getBaseDAO().executeQuery(getPlanNumSql,
                            new ColumnProcessor("nplanoutastnum"));
                    if (nplanoutastnum == null) {
                        continue;
                    }
                    UFDouble plantNum = new UFDouble(String.valueOf(nplanoutastnum));
                    // 通过备料计划的表体主键查询已申请数量
                    String getApplyNumSql = "SELECT NVL(SUM(nassistnum),0) nassistnum "
                            + "FROM ic_sapply_b "
                            + "WHERE NVL(dr,0) = 0 AND csourcebillbid = '[csourcebillbid]'";
                    getApplyNumSql = getApplyNumSql.replace("[csourcebillbid]", csourcebillbid);
                    // 修改的时候加上子表主键
                    if (null != outVOs[0] && null != outVOs[0].getHead() && null != outVOs[0].getHead().getCgeneralhid()) {
                        getApplyNumSql += " AND cgeneralbid != '" + bodyVO.getPrimaryKey() + "'";
                    }
                    Object nassistnum = getBaseDAO().executeQuery(getApplyNumSql,
                            new ColumnProcessor("nassistnum"));
                    UFDouble applyNum = new UFDouble(String.valueOf(nassistnum));
                    NCCForUAPLogger.debug(String.format(
                            "出库申请保存: 来源单据表体主键 = [%S], 计划出库数量 = [%S], 已申请数量 = [%S], 本次申请数量 = [%S],",
                            csourcebillbid, plantNum, applyNum, bodyVO.getNassistnum()
                    ));
                    UFDouble applyNum1 = applyNum.add(bodyVO.getNassistnum());
                    if (MathTool.compareTo(plantNum, applyNum1) < 0) {
                        throw new BusinessException("申请数量已超出计划数量，请检查！");
                    }
                }
            }
            if (null != outVOs[0] && null != outVOs[0].getHead() && null != outVOs[0].getHead().getCgeneralhid()) {
                PfParameterUtil<SapplyBillVO> util = new PfParameterUtil(this.getPfParameterVO(), outVOs);
                SapplyBillVO[] originBills = (SapplyBillVO[]) util.getOrginBills();
                if (originBills == null) {
                    originBills = (SapplyBillVO[]) (new ICBillVOQuery()).fetchVOWithLoc(outVOs);
                }

                SapplyBillVO[] clientFullBills = (SapplyBillVO[]) util.getClientFullInfoBill();
                return ((ISapplyBillMaintain) NCLocator.getInstance().lookup(ISapplyBillMaintain.class)).update(clientFullBills, originBills);
            } else {
                SapplyBillVO[] retunVos = new SapplyBillVO[outVOs.length];
                ISapplyBillMaintain service = (ISapplyBillMaintain) NCLocator.getInstance().lookup(ISapplyBillMaintain.class);

                for (int i = 0; i < outVOs.length; ++i) {
                    retunVos[i] = service.insert(new SapplyBillVO[]{outVOs[i]})[0];
                }

                return retunVos;
            }
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    public String getCodeRemark() {
        return NCLangRes4VoTransl.getNCLangRes().getStrByID("4008003_0", "04008003-0108");
    }

    private void setParameter(String key, Object val) {
        if (this.m_keyHas == null) {
            this.m_keyHas = new Hashtable();
        }

        if (val != null) {
            this.m_keyHas.put(key, val);
        }

    }

    private BaseDAO getBaseDAO() {
        if (this.baseDAO == null) {
            this.baseDAO = new BaseDAO();
        }
        return this.baseDAO;
    }
}
