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

package nc.bs.mmpac.pmo.pac0002.bp.rule;

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

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.mmpac.pmo.pac0002.bp.service.PMOSNServiceAdapter;
import nc.bs.mmpac.pmo.pac0002.bp.util.PMOBPSNUtil;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO;
import nc.vo.mmpac.wr.param.WrSerialCodeMarkParamForMO;
import nc.vo.pu.m20.entity.PraybillItemVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.commons.lang.ArrayUtils;

/**
 * 流程生成订单删除时回写备料计划是否下达字段
 */
public class WithUpdatePickmRule implements IRule<PMOAggVO> {
    public WithUpdatePickmRule() {
    }

    public void process(PMOAggVO[] vos) {
        if (!ArrayUtils.isEmpty(vos)) {
            try {
                List<String> pk=  new ArrayList<String>();
                List<Map<String,Object>> updateList = new ArrayList<>();
                for (PMOAggVO vo : vos) {
                    PMOItemVO[] praybillItemVOS=vo.getChildrenVO();
                    for (PMOItemVO praybillItemVO : praybillItemVOS) {
                        if("55A3".equals(praybillItemVO.getVsrctype())){
                            pk.add(praybillItemVO.getVsrcbid());
                        }
                    }
                }
                if(!updateList.isEmpty()){
//                回写已经下达数量
                    updetaPmoNum(updateList);
                }
                if(!pk.isEmpty()){
                    //回写是否下达为否
                    updetaPmo(pk);
                }


            } catch (Exception e) {
                ExceptionUtils.wrappException(e);
            }

        }
    }
    private void updetaPmo(List<String> arrayList) throws DAOException {
        String result = "";
        StringBuilder sb = new StringBuilder();
        if (arrayList.size() > 1) {
            for (int i = 0; i < arrayList.size(); i++) {


                sb.append("'").append(arrayList.get(i)).append("'");
                if (i < arrayList.size() - 1) {
                    sb.append(",");
                }
            }

            result = sb.toString();
        } else {
            result = "'" + (String) arrayList.get(0) + "'";
        }
        String sql = " update  mm_pickm_b set vbdef13='N'  where mm_pickm_b.cpickm_bid in(" + result + ") and mm_pickm_b.vbdef16='0'  ";

        BaseDAO dao = new BaseDAO();
        dao.executeUpdate(sql);
    }
    private void updetaPmoNum(List<Map<String, Object>> updateList) throws DAOException {
        BaseDAO dao = new BaseDAO();
        for (Map<String, Object> updateMap : updateList) {
//            String sql = " update  mm_pickm_b set vbdef16 = TO_CHAR(TO_NUMBER(vbdef16) -"+updateMap.get("num")+")  where mm_pickm_b.cpickm_bid ='" + updateMap.get("pk") + "'";
            String sql = " update  mm_pickm_b set vbdef16 =  TO_CHAR(TO_NUMBER(vbdef16) -TO_NUMBER('"+((UFDouble)updateMap.get("num")).toString()+"'))  where mm_pickm_b.cpickm_bid ='" + updateMap.get("pk") + "'";

            dao.executeUpdate(sql);
        }


    }
}
