package wypu;

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

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.NCLocator;
import nc.itf.pu.invoke.IMatrialFp;
import nc.itf.pu.invoke.IMatrilFpService;
import nc.itf.pu.query.IQuery;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.pu.m21.entity.OrderItemVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;

public class MatrilFpService implements IMatrilFpService{
	
	public void matrialFp(OrderItemVO[] BVOs) {
		List<String> listMaterial = new ArrayList<String>();
		// for(OrderItemVO BVO : BVOs ) {
//			listMaterial.add(BVO.getPk_material());
//		}
//		int sizeMaterial = listMaterial.size(); 
//		String[] materials = (String[])listMaterial.toArray(new String[sizeMaterial]);
		Map<String, String[]> targets = new HashMap<String, String[]>();
		for (OrderItemVO BVO : BVOs) {
//			List<String> listAll = new ArrayList<String>();
			String pk_material = BVO.getPk_material();
			String pk_arrvstoorg = BVO.getPk_arrvstoorg();
			String pk_psfinanceorg = BVO.getPk_psfinanceorg();
			List<String> targetIds = new ArrayList<String>();
			List<String> listLastVersion = new ArrayList<String>();
			try {
				if (pk_arrvstoorg.equals(pk_psfinanceorg)) {
					targetIds = Arrays.asList(getTargetId(pk_arrvstoorg));
					listLastVersion.add(pk_arrvstoorg);
//					targetIds.add(pk_arrvstoorg);
				} else {
					targetIds = Arrays.asList(getTargetId(pk_arrvstoorg, pk_psfinanceorg));
					listLastVersion.add(pk_arrvstoorg);
//					listLastVersion.add(pk_psfinanceorg);
//					targetIds.add(pk_arrvstoorg);
//					targetIds.add(pk_psfinanceorg);
				}
			} catch (Exception e) {
				System.out.println(e);
			}
			listMaterial.add(pk_material);
			listLastVersion.addAll(targetIds);
			String[] targetIdstr = (String[]) listLastVersion.toArray(new String[targetIds.size()]);
			targets.put(BVO.getPk_material(), targetIdstr);
		}
//		int sizeTargetID = listTarget.size(); 
//		String[] targetID = (String[])listTarget.toArray(new String[sizeTargetID]);
		for (String material : listMaterial) {
			String[] materials = new String[1];
			materials[0] = material;
			String[] targetId = targets.get(material);
			try {
				IMatrialFp bgyToService = null;

				// 由于这个类事在前端的,所以要在这里改造下,通过ServiceLocator去调用本模块的接口和实现类,然后再
				// 实现类中 通过以下方式调用:

				/*
				 * if (!com.yonyou.cloud.ncc.NCCEnv.isMerged()) { bgyToService =
				 * IrisNCLocator.getInstance().lookup(IMatrialFp.class); } else { bgyToService =
				 * NCLocator.getInstance().lookup(IMatrialFp.class); }
				 */
				bgyToService = NCLocator.getInstance().lookup(IMatrialFp.class);
//				 
//			bgyToService = ServiceLocator.find(IInvoke.class);
				bgyToService.invokeassignMaterialByPks(materials, targetId);
				
				updateState(targetId,material);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
	
	private String[] getTargetId(String... params)
			throws nc.bs.framework.json.core.exception.BusinessException, DAOException {
		IQuery queryService = (IQuery) NCLocator.getInstance().lookup(IQuery.class);
		String[] rtnValue = queryService.queryCostregion(params);
		return rtnValue;
	}
	
	 private Object updateState(String[] targetID,String pk_material) throws BusinessException {
		  BaseDAO baseDao = new BaseDAO();
		  //更新库存信息与成本信息
		  for(String pk:targetID) {
		   String sql1 = "update bd_materialstock set wholemanaflag = ? where pk_material = ? and pk_org = ?";
		   SQLParameter param1 = new SQLParameter();
		   param1.addParam(new UFBoolean(true));
		   param1.addParam(pk_material);
		   param1.addParam(pk);
		   baseDao.executeUpdate(sql1,param1);
		   
		   String sql2 = "update bd_materialcostmod set batchcost = ? where pk_material = ? and pk_costregion = ?";
		   SQLParameter param2 = new SQLParameter();
		   param2.addParam(new UFBoolean(true));
		   param2.addParam(pk_material);
		   param2.addParam(pk);
		   baseDao.executeUpdate(sql2, param2);
		   
		 //update 实收物料价值管控模式 begin
		 //判断编码
		   String sql3 = "select t.code from bd_marbasclass t left join bd_material p on t.pk_marbasclass = p.pk_marbasclass where p.pk_material = ?";
		   SQLParameter param3 = new SQLParameter();
		   param3.addParam(pk_material);
		   String code = (String) baseDao.executeQuery(sql3, param3,new ColumnProcessor());
		   //物料基本信息主键对应的编码
		   if("19".equals(code)) {
			    String sql4 = "update bd_materialfi set materialvaluemgt='2' where pk_material=?";
			    SQLParameter param4 = new SQLParameter();
			    param4.addParam(pk_material);
			    baseDao.executeUpdate(sql4,param4);
		    }
		  }
		  return null;
	 }
}
