package nc.bs.mmpac.pickm.bp.rule;


import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Log;
import nc.bs.mmpac.pmo.pac0002.bp.rule.util.SyncEpicMesUtil;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.itf.mmpac.pmo.pac0002.IPMOQueryService;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;
import nc.vo.pub.BusinessException;

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

/**
 * 备料计划更改后更新mes
 *
 * @author lh
 * @date 2025/7/15
 */
public class AfterupdateSyncEpicMesRule implements IRule<AggPickmVO> {

    private static final String LOG_INFO_NAME = "dldzlog";

    private static final Log logDl = Log.getInstance(LOG_INFO_NAME);

    @Override
    public void process(AggPickmVO[] aggPickmVOS) {
        if (MMValueCheck.isEmpty(aggPickmVOS)) {
            return;
        }

        for (AggPickmVO aggPickmVO : aggPickmVOS) {

            IPMOQueryService queryService= NCLocator.getInstance().lookup(IPMOQueryService.class);
            List<PMOAggVO> list = new ArrayList<PMOAggVO>();
            if(null!=aggPickmVO.getParentVO().getVfirstbilltype() && aggPickmVO.getParentVO().getVfirstbilltype().indexOf("55A2")!=-1){
                try {
                    PMOAggVO[] pmoAggVO = queryService.queryPMOAggVOByBid( new String[]{aggPickmVO.getParentVO().getCsourcebillid()});
                    if(null!=pmoAggVO){
                        for (PMOAggVO pmoAggVO1 : pmoAggVO) {
                            if (1 == pmoAggVO1.getParentVO().getFbillstatus()){
                                list.add(pmoAggVO1);
                            }
                        }
                    }

                } catch (BusinessException e) {
                    throw new RuntimeException(e);
                }
            }
            if (list.size() > 0) {
                SyncEpicMesUtil mesUtil = new SyncEpicMesUtil();
//               备料重算后触发审核
                String n = "Y";
                try {
                    mesUtil.processData((PMOAggVO[])list.toArray(new PMOAggVO[list.size()]),n);
                } catch (BusinessException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}
