package nc.bs.mmpac.wr.rule.prodin;

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

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.mmpac.wr.adapter.WrptServiceAdapter;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.util.mmf.busi.service.OrgUnitPubService;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.wr.entity.AggWrVO;
import nc.vo.mmpac.wr.entity.WrItemVO;
import nc.vo.mmpac.wr.entity.WrQualityVO;
import nc.vo.org.StockConsignVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class WrProdInSetInStockOrgRule implements IRule<AggWrVO> {
    @Override
    public void process(AggWrVO[] vos) {

        // 判空
        if (MMValueCheck.isEmpty(vos)) {
            return;
        }

        // 取得完工入库委托关系
        Map<String, String> inStockMap = new HashMap<String, String>();
        try {
            inStockMap = this.getInStockOrg(vos);
        }
        catch (BusinessException e) {
            ExceptionUtils.wrappException(e);
        }
        List<String> orgIds = new ArrayList<String>();
        for (AggWrVO aggVO : vos) {
            for (WrItemVO wrItemVO : (WrItemVO[]) aggVO.getChildren(WrItemVO.class)) {
            	 String org = inStockMap.get(wrItemVO.getPk_org() + wrItemVO.getCbmaterialvid());
            	 orgIds.add(org);
            }
        }
        Map<String, String> map = new HashMap<String,String>();
        if(MMValueCheck.isNotEmpty(orgIds)) {
        	try {
        		map = OrgUnitPubService.getNewVIDSByOrgIDS(orgIds.toArray(new String[0]));
        	}catch (BusinessException e) {
                ExceptionUtils.wrappException(e);
            }
        }
        for (AggWrVO aggVO : vos) {

            for (WrItemVO wrItemVO : (WrItemVO[]) aggVO.getChildren(WrItemVO.class)) {

                String org = inStockMap.get(wrItemVO.getPk_org() + wrItemVO.getCbmaterialvid());

                if (MMValueCheck.isEmpty(org)) {

                    this.setInStockOrg(wrItemVO, wrItemVO.getPk_org(), wrItemVO.getPk_org_v());
                }
                else {
                    String orgV = map.get(org);
                    this.setInStockOrg(wrItemVO, org, orgV);
                }
            }
        }
    }

    /**
     * 获取完工入库委托关系
     * 
     * @param vos
     * @return
     * @throws BusinessException
     */
    private Map<String, String> getInStockOrg(AggWrVO[] vos) throws BusinessException {

        List<StockConsignVO> scvos = new ArrayList<StockConsignVO>();
        StockConsignVO scvo = null;

        for (AggWrVO aggVO : vos) {

            for (WrItemVO itemVO : (WrItemVO[]) aggVO.getChildren(WrItemVO.class)) {

                scvo = new StockConsignVO();
                scvo.setPk_factory(itemVO.getPk_org());
                scvo.setPk_material(itemVO.getCbmaterialvid());
                scvos.add(scvo);
            }
        }

        return WrptServiceAdapter.getInStockConsignPubService().queryStockPkByFactoryPKsAndMaterialPKs(
                scvos.toArray(new StockConsignVO[0]));
    }

    /**
     * 设定入库库存组织
     * 
     * @param wrItem
     * @param rOrg
     * @param rOrgVid
     */
    private void setInStockOrg(WrItemVO wrItemVO, String rOrg, String rOrgVid) {

        // 判空
        if (MMValueCheck.isEmpty(wrItemVO)) {
            return;
        }
        String userCode = InvocationInfoProxy.getInstance().getUserCode();
        for (WrQualityVO wrQualityVO : wrItemVO.getQualityvos()) {

            // 设定委外关系库存组织
            if (!MMValueCheck.isEmpty(rOrg) && !MMValueCheck.isEmpty(rOrgVid)) {

                wrQualityVO.setCgdepositorgid(rOrg);
                wrQualityVO.setCgdepositorgvid(rOrgVid);
            }
            else {
                wrQualityVO.setCgdepositorgid(wrItemVO.getPk_org());
                wrQualityVO.setCgdepositorgvid(wrItemVO.getPk_org_v());
            }


            //2025年5月30日08点45分 --外部系统推送接口时填了仓库，此处不清空
            if("rzmes".equals(userCode)||"MES".equals(userCode)){

            }else{
                // 清空入库仓库
                wrQualityVO.setCgwarehouseid(null);
            }
        }
    }
}
