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

package nc.pubimpl.so.m30.so.withdraw;

import java.util.HashMap;
import java.util.Map;
import nc.bs.so.m30.plugin.ServicePlugInPoint;
import nc.bs.so.m30.rule.maintainprocess.NullRule;
import nc.bs.so.m30.rule.maintainprocess.RewriteReturnNumRule;
import nc.bs.so.m30.rule.rewrite.m30.CheckWithdrawNumRule;
import nc.impl.pubapp.env.BSContext;
import nc.impl.pubapp.pattern.data.view.ViewQuery;
import nc.impl.pubapp.pattern.data.view.ViewUpdate;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.impl.pubapp.pattern.rule.processer.AroundProcesser;
import nc.pubimpl.so.rule.SyncClmNumRule;
import nc.pubitf.so.m30.so.withdraw.IRewriteSaleOrderByWithdraw;
import nc.pubitf.so.m30.so.withdraw.Rewrite30Para;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.log.TimeLog;
import nc.vo.pubapp.pattern.pub.MathTool;
import nc.vo.so.m30.entity.SaleOrderBVO;
import nc.vo.so.m30.entity.SaleOrderViewVO;

public class RewriteSaleOrderByWitdrawImpl implements IRewriteSaleOrderByWithdraw {
    public RewriteSaleOrderByWitdrawImpl() {
    }

    public void rewrite30NumForWithdraw(Rewrite30Para[] paras) throws BusinessException {
        try {
            this.rewrite(paras);
        } catch (RuntimeException ex) {
            ExceptionUtils.marsh(ex);
        }

    }

    private void addAfterRule(AroundProcesser<SaleOrderViewVO> processer) {
        processer.addAfterRule(new NullRule());
        processer.addAfterRule(new RewriteReturnNumRule());
        // 同步CLM合同销售订单-累计退货主数量
        processer.addAfterRule(new SyncClmNumRule("ntotalreturnnum"));
    }

    private void addBeforeRule(AroundProcesser<SaleOrderViewVO> processer) {
        IRule<SaleOrderViewVO> rule = new CheckWithdrawNumRule();
        processer.addBeforeRule(rule);
    }

    private Map<String, Rewrite30Para> prepareParams(Rewrite30Para[] paras) {
        Map<String, Rewrite30Para> index = new HashMap();

        for(Rewrite30Para para : paras) {
            index.put(para.getCsaleorderbid(), para);
        }

        return index;
    }

    private SaleOrderViewVO[] query(Map<String, Rewrite30Para> index) {
        String[] ids = (String[])index.keySet().toArray(new String[0]);
        ViewQuery<SaleOrderViewVO> bo = new ViewQuery(SaleOrderViewVO.class);
        bo.setSharedHead(true);
        SaleOrderViewVO[] views = (SaleOrderViewVO[])bo.query(ids);
        if (views.length != index.size()) {
            String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("4006011_0", "04006011-0171");
            ExceptionUtils.wrappBusinessException(message);
        }

        return views;
    }

    private void rewrite(Rewrite30Para[] paras) {
        TimeLog.logStart();
        Map<String, Rewrite30Para> index = this.prepareParams(paras);
        TimeLog.logStart();
        SaleOrderViewVO[] views = this.query(index);
        TimeLog.info("查询销售订单表体");
        BSContext.getInstance().setSession(Rewrite30Para.class.getName(), index);
        AroundProcesser<SaleOrderViewVO> processer = new AroundProcesser(ServicePlugInPoint.rewrite30NumForWithdraw);

        for(SaleOrderViewVO viewvo : views) {
            this.setRewiteNum(viewvo, index);
        }

        TimeLog.logStart();
        this.addBeforeRule(processer);
        processer.before(views);
        TimeLog.info("调用回写动作前插入点");
        TimeLog.logStart();
        String[] names = new String[]{"ntotalreturnnum"};
        ViewUpdate<SaleOrderViewVO> bo = new ViewUpdate();
        views = (SaleOrderViewVO[])bo.update(views, SaleOrderBVO.class, names);
        TimeLog.info("更新数据库");
        TimeLog.logStart();
        this.addAfterRule(processer);
        processer.after(views);
        TimeLog.info("调用回写动作后插入点");
        BSContext.getInstance().removeSession(Rewrite30Para.class.getName());
    }

    private void setRewiteNum(SaleOrderViewVO vo, Map<String, Rewrite30Para> index) {
        SaleOrderBVO body = vo.getBody();
        Rewrite30Para para = (Rewrite30Para)index.get(body.getCsaleorderbid());
        UFDouble ntotalreturnnum = body.getNtotalreturnnum();
        ntotalreturnnum = MathTool.sub(ntotalreturnnum, para.getNnum());
        body.setNtotalreturnnum(ntotalreturnnum);
    }
}
