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

package nc.bs.pu.m20.maintain.rule.delete;

import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.EventDispatcher;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.trade.business.HYPubBO;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.uif.pub.exception.UifException;
import nc.vo.pu.m20.entity.PraybillItemVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pu.pub.constant.PUMDValue;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.commons.lang.ArrayUtils;

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

/**
 * 请购单删除时回写备料计划是否下达字段
 */
public class UpdatePickmRule implements IRule<PraybillVO> {
    public UpdatePickmRule() {
    }

    public void process(PraybillVO[] vos) {
        if (!ArrayUtils.isEmpty(vos)) {
            try {
                List<String> pk=  new ArrayList<String>();
                List<Map<String,Object>> updateList = new ArrayList<>();
                for (PraybillVO vo : vos) {
                    PraybillItemVO[] praybillItemVOS=vo.getBVO();
                    for (PraybillItemVO praybillItemVO : praybillItemVOS) {
                        if("55A3".equals(praybillItemVO.getCsourcetypecode())){
                            Map<String,Object> map = new HashMap<>();
                            map.put("num",praybillItemVO.getNastnum());
                            map.put("pk",praybillItemVO.getCfirstbid());
                            pk.add(praybillItemVO.getCsourcebid());
                            updateList.add(map);
                        }
                    }
                }

                if(!updateList.isEmpty()){
//                回写已经下达数量
                    updetaPmoNum(updateList);
                }
                if(!pk.isEmpty()){
                    //回写是否下方为是
                    updetaPmo(pk);
                }


            } catch (BusinessException e) {
                try {
                    throw new BusinessException(e.getMessage());
                } catch (BusinessException ex) {
                    throw new RuntimeException(ex);
                }
            }

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


//		OrgVO orgvo = (OrgVO)hy.queryByPrimaryKey(OrgVO.class, );

                Object cpickm_bid = hy.findColValue("mm_pickm_b", "cpickmid", "cpickm_bid  = '"+arrayList.get(i)+"'  ");
                if(null == cpickm_bid){
                    continue;
                }
                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 vbdef30='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, UifException {
        BaseDAO dao = new BaseDAO();
        for (Map<String, Object> updateMap : updateList) {
            HYPubBO hy = new HYPubBO();
//		OrgVO orgvo = (OrgVO)hy.queryByPrimaryKey(OrgVO.class, );

            Object cpickm_bid = hy.findColValue("mm_pickm_b", "cpickmid", "cpickm_bid  = '"+updateMap.get("pk")+"'  ");
            if(null == cpickm_bid){
                continue;
            }

            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);
        }


    }
}
