package com.cim.idm.service.Impl;

import com.cim.idm.exception.GlobalException;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.service.TransferInService;
import com.cim.idm.service.impl.QMSServiceImpl;
import com.cim.idm.service.impl.ToSAPServiceImpl;
import com.cim.idm.utils.EventInfoUtil;
import com.cim.idm.wmsextend.generic.GenericServiceProxy;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPackingKey;
import com.cim.idm.wmspackage.materialpacking.management.info.SetEventInfo;
import com.cim.idm.wmspackage.storage.StorageServiceProxy;
import com.cim.idm.wmspackage.storage.management.data.StorageSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("TransferIn")
public class TransferInServiceImpl implements TransferInService {
	
	@Autowired
	private TransferInServiceImpl transferInServiceImpl;
	
	private EventInfo makeEventInfo;

	@Autowired
	private ToSAPServiceImpl toSAPServiceImpl;
	
	private QMSServiceImpl qMSServiceImpl = new QMSServiceImpl();

	/**
	 * 调拨入库绑定条码u
	 */
	@Transactional
	@Override
	public boolean BindBarCodeToTransferIn(String materialPackingName,String charge,String materialspecName,String user,
			String shipRequestName,String shipRequestDetailName,String locationName) throws GlobalException {
		//解析条码,根据远条码信息更新目标条码数量和库存状态
		String condition="WHERE CHARGE=? AND MATERIALSPECNAME=? ";
		String[] bindSet={charge,materialspecName};
		List<MaterialPacking> MaterialPackingList = MaterialPackingServiceProxy.getMaterialPackingService().select(condition, bindSet);
		SetEventInfo setEventInfo = new SetEventInfo();
		EventInfoUtil eventInfoUtil = new EventInfoUtil();
		makeEventInfo = eventInfoUtil.makeEventInfo("BarCodeToTransferIn", user, "BarCodeToTransferIn", "", "");
		String sql = "select * from STORAGESPEC WHERE STORAGENAME = :STORAGENAME" ;
		Map<String, Object> hashMap2 = new HashMap<String,Object> ();
		hashMap2.put("STORAGENAME", locationName) ;
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap2);
		if (queryForList == null || queryForList.size() < 1) {
			throw new GlobalException(locationName + "库位不存在！");
		}
		//根据货位找到ERP库位和组织
		condition="WHERE STORAGENAME=? ";
		String[] indSet={locationName};
		List<StorageSpec> select = StorageServiceProxy.getStorageSpecService().select(condition, indSet);
		String erpFactory = "";
		String erpLocation = "";
		for (StorageSpec storageSpec : select) {
			erpLocation = storageSpec.getErpLocation();
			erpFactory = storageSpec.getErpFactory();
		}
		if ("".equals(erpLocation) || "".equals(erpFactory)) {
			throw new GlobalException(locationName + "库位不存在！");
		}
		String[] split = materialPackingName.split("\\|");
		for (MaterialPacking materialPacking : MaterialPackingList) {
			//更新批次，数量，库位状态
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("materialPackingName", materialPackingName);
			hashMap.put("materialQuantity", split[7]);
			hashMap.put("materialCreateQuantity", split[7]);
			hashMap.put("shipRequestName", shipRequestName);
			hashMap.put("shipRequestDetailName", shipRequestDetailName);
			hashMap.put("erpLocation", erpLocation);
			hashMap.put("erpFactory", erpFactory);
			hashMap.put("locationName", locationName);
			setEventInfo.setUserColumns(hashMap);
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
			
			String sql2 = "UPDATE MATERIALSHIPREQUESTDETAIL T SET T.ASSIGNEDQUANTITY = T.ASSIGNEDQUANTITY + :materialQuantity "
					+ "WHERE T.SHIPREQUESTNAME = :shipRequestName AND T.SHIPREQUESTDETAILNAME  = :shipRequestDetailName ";
			IDMFrameServiceProxy.getSqlTemplate().update(sql2, hashMap);
			
		}
		

		return true;
	}
	

	/**
	 * 调拨入库绑定条码  无单据 有组织 仓库 货位 
	 */
	@Transactional
	@Override
	public boolean BindBarCodeToTransfer(String receiveRequestName,String receiveRequestDetailName,String materialPackingName,String charge,String materialspecName,String user,
			String shipRequestName,String shipRequestDetailName,String locationName, String erpLocation, String erpFactory) throws GlobalException {
		//解析条码,根据远条码信息更新目标条码数量和库存状态
		String condition="WHERE CHARGE=? AND MATERIALSPECNAME=? ";
		String[] bindSet={charge,materialspecName};
		List<MaterialPacking> MaterialPackingList = MaterialPackingServiceProxy.getMaterialPackingService().select(condition, bindSet);
		SetEventInfo setEventInfo = new SetEventInfo();
		EventInfoUtil eventInfoUtil = new EventInfoUtil();
		makeEventInfo = eventInfoUtil.makeEventInfo("BarCodeToTransferIn", user, "BarCodeToTransferIn", "", "");
		String sql = "select * from STORAGESPEC WHERE STORAGENAME = :STORAGENAME" ;
		Map<String, Object> hashMap2 = new HashMap<String,Object> ();
		hashMap2.put("STORAGENAME", locationName) ;
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap2);
		if (queryForList == null || queryForList.size() < 1) {
			throw new GlobalException(locationName + "库位不存在！");
		}
		String[] split = materialPackingName.split("\\|");
		for (MaterialPacking materialPacking : MaterialPackingList) {
			//更新批次，数量，库位状态
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			Double materialQuantity = materialPacking.getMaterialQuantity();
			
			// 根据undoid 查询单据
			String orderSql2 = "SELECT DISTINCT MD.SHIPREQUESTNAME,MD.SHIPREQUESTDETAILNAME,MD.REQUESTQUANTITY,MD.ASSIGNEDQUANTITY, MD.ERPRECEIVEFACTORY FROM MATERIALSHIPREQUESTDETAIL "
					+ "MD LEFT JOIN MATERIALSHIPREQUEST MT ON MT.SHIPREQUESTNAME = MD.SHIPREQUESTNAME "
					+ "WHERE MD.SHIPREQUESTNAME = :SHIPREQUESTNAME AND MT.SHIPREQUESTTYPE = '4E' AND MD.SDK_ID  = :SDK_ID "
					+ "AND MD.MATERIALSPECNAME = :MATERIALSPECNAME AND MD.PHASE = :PHASE AND MD.REQUESTQUANTITY - MD.ASSIGNEDQUANTITY >= " + materialQuantity;
			
			Map<String, Object> orderMap2 = new HashMap<String, Object>();
			orderMap2.put("SHIPREQUESTNAME",materialPacking.getUnDoID()); 
			orderMap2.put("SDK_ID",materialPacking.getSDK_ID() ); 
			orderMap2.put("PHASE",materialPacking.getPHASE() ); 
			orderMap2.put("MATERIALSPECNAME",materialPacking.getMaterialSpecName()); 
			List<Map<String,Object>> orderList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(orderSql2, orderMap2);
			
			if(orderList2.size() < 1) {
				throw new GlobalException("未找到条码对应的单据!");
			}
			
			String shipRequestName2 = (String) orderList2.get(0).get("SHIPREQUESTNAME");
			String shipRequestDetailName2 = (String) orderList2.get(0).get("SHIPREQUESTDETAILNAME");
			
//			materialPacking.getUnDoID().equals(receiveRequestName)
				hashMap.put("shipRequestName", shipRequestName2);
				hashMap.put("shipRequestDetailName", shipRequestDetailName2);
				hashMap.put("erpLocation", erpLocation);
				hashMap.put("erpFactory", erpFactory);
				hashMap.put("locationName", locationName);
				hashMap.put("materialQuantity", materialPacking.getMaterialQuantity());
				setEventInfo.setUserColumns(hashMap);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
				String sql2 = "UPDATE MATERIALSHIPREQUESTDETAIL T SET T.ASSIGNEDQUANTITY = T.ASSIGNEDQUANTITY + :materialQuantity "
						+ "WHERE T.SHIPREQUESTNAME = :shipRequestName AND T.SHIPREQUESTDETAILNAME  = :shipRequestDetailName ";
				IDMFrameServiceProxy.getSqlTemplate().update(sql2, hashMap);
		}
		

		return true;
	}
	
	@Override
	@Transactional
	public boolean CommitTransfer(List<com.cim.idm. model.MaterialPacking> boxList,String user, String erpFactory,
			String erpLocation, String locationName,String commitDate,String opCode) throws Exception {
		String pc = "(";
		for (int i = 0; i < boxList.size(); i++) {
			com.cim.idm. model.MaterialPacking box = boxList.get(i);
			EventInfoUtil eventInfoUtil = new EventInfoUtil();
			makeEventInfo = eventInfoUtil.makeEventInfo("CommitTransfer", user, "CommitTransfer", "", "");
			SetEventInfo setEventInfo = new SetEventInfo();
//			String shipRequestName = boxList.get(i).getShipRequestName();
//			String shipRequestDetailName = boxList.get(i).getShipRequestDetailName();
			String siteName = boxList.get(i).getSiteName();
			String materialPackingName = boxList.get(i).getMaterialPackingName();
			String charge = boxList.get(i).getCharge();
			String materialSpecName = boxList.get(i).getMaterialSpecName();
			String receiveRequestName = boxList.get(i).getReceiveRequestName();
			String receiveRequestDetailName = boxList.get(i).getReceiveRequestDetailName();
//			String locationName = boxList.get(i).getLocationName();
//			String erpFactory = boxList.get(i).getErpFactory();
			
			MaterialPacking materialPacking = MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(new MaterialPackingKey("SDK",box.getMaterialPackingName()));
			String shipRequestName = receiveRequestName;
			String shipRequestDetailName = receiveRequestDetailName;
			String orderSql2 = "SELECT DISTINCT MD.SHIPREQUESTNAME,MD.SHIPREQUESTDETAILNAME,MD.REQUESTQUANTITY,MD.ASSIGNEDQUANTITY, MD.ERPRECEIVEFACTORY FROM MATERIALSHIPREQUESTDETAIL "
					+ "MD LEFT JOIN MATERIALSHIPREQUEST MT ON MT.SHIPREQUESTNAME = MD.SHIPREQUESTNAME "
					+ "WHERE MD.SHIPREQUESTNAME = :SHIPREQUESTNAME AND MT.SHIPREQUESTTYPE = '4E' AND NVL(MD.SDK_ID,'~')  = NVL(:SDK_ID,'~') "
					+ "AND MD.MATERIALSPECNAME = :MATERIALSPECNAME AND NVL(MD.PHASE,'~') = NVL(:PHASE,'~') AND MD.CHARGE = :CHARGE AND MD.REQUESTQUANTITY - MD.ASSIGNEDQUANTITY = " + materialPacking.getMaterialQuantity();
			pc +=  "'" + materialPacking.getUnDoID() + "',";
			Map<String, Object> orderMap2 = new HashMap<String, Object>();
			orderMap2.put("SHIPREQUESTNAME",materialPacking.getUnDoID()); 
			orderMap2.put("SDK_ID",materialPacking.getSDK_ID() ); 
			orderMap2.put("PHASE",materialPacking.getPHASE() ); 
			orderMap2.put("MATERIALSPECNAME",materialPacking.getMaterialSpecName());
			orderMap2.put("CHARGE",charge);
			List<Map<String,Object>> orderList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(orderSql2, orderMap2);
			
			if(orderList2.size() < 1) {
				throw new GlobalException("未找到条码对应的单据!");
			}
			
			String shipRequestName2 = (String) orderList2.get(0).get("SHIPREQUESTNAME");
			String shipRequestDetailName2 = (String) orderList2.get(0).get("SHIPREQUESTDETAILNAME");
				Map<String, Object> hashMap = new HashMap<String,Object> ();
//			materialPacking.getUnDoID().equals(receiveRequestName)
				hashMap.put("shipRequestName", shipRequestName2);
				hashMap.put("shipRequestDetailName", shipRequestDetailName2);
				hashMap.put("erpLocation", erpLocation);
				hashMap.put("erpFactory", erpFactory);
				hashMap.put("locationName", locationName);
				hashMap.put("materialQuantity", materialPacking.getMaterialQuantity());
				setEventInfo.setUserColumns(hashMap);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
				String sql2 = "UPDATE MATERIALSHIPREQUESTDETAIL T SET T.ASSIGNEDQUANTITY = T.ASSIGNEDQUANTITY + :materialQuantity "
						+ "WHERE T.SHIPREQUESTNAME = :shipRequestName AND T.SHIPREQUESTDETAILNAME  = :shipRequestDetailName ";
				IDMFrameServiceProxy.getSqlTemplate().update(sql2, hashMap);
		}
		pc += "'')";
		
		
		String sql = "SELECT DISTINCT SHIPREQUESTNAME FROM MATERIALSHIPREQUEST WHERE  SHIPREQUESTNAME IN " +  pc;
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, new HashMap<String,Object> ());
		for (Map<String, Object> map : queryForList) {
			String shiprequestName = map.get("SHIPREQUESTNAME").toString();
			String billCode = toSAPServiceImpl.AlloctionStockIn(shiprequestName, "SDK", user, erpLocation);
//			String billCode = UUID.randomUUID().toString();
			//判断是否获得物料凭证
			if (billCode == null || "".equals(billCode)) {
				throw new GlobalException("ERP过账失败！");
			}
			SetEventInfo setEventInfo = new SetEventInfo();
			EventInfoUtil eventInfoUtil = new EventInfoUtil();
			String condition1="WHERE SHIPREQUESTNAME = ?   AND STOCKSTATE='StockOut' AND ERPLOCATION = ? ";
			String[] bindSet2 = {shiprequestName, erpLocation};
			List<MaterialPacking> MaterialPackingList = MaterialPackingServiceProxy.getMaterialPackingService().select(condition1, bindSet2);
			for (MaterialPacking materialPacking : MaterialPackingList) {
				//更新批次，数量，库位状态
				Map<String, Object> hashMap = new HashMap<String,Object> ();
				hashMap.put("stockState", "Stocked");
				hashMap.put("shipRequestName", "");
				hashMap.put("shipRequestDetailName", "");
//				hashMap.put("commitDate",commitDate);
				hashMap.put("unDoID", billCode);
				setEventInfo.setUserColumns(hashMap);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
			}
			SaveUnDoInfo( billCode,commitDate);
			String sql3 = "UPDATE MATERIALSHIPREQUEST T SET T.SHIPREQUESTSTATE = 'Completed' WHERE T.SHIPREQUESTNAME = :SHIPREQUESTNAME ";
			IDMFrameServiceProxy.getSqlTemplate().update(sql3, new HashMap<String,Object> () {{
				this.put("SHIPREQUESTNAME", shiprequestName);
			}});
		}

		

		
		return true;
	}
	
	
	@Transactional
	public boolean CommitTransfer(List<com.cim.idm. model.MaterialPacking> boxList,String user, String erpFactory,
			String erpLocation, String locationName,String commitDate,String opCode,String oqaresultstate) throws Exception {
		String pc = "(";
		for (int i = 0; i < boxList.size(); i++) {
			com.cim.idm. model.MaterialPacking box = boxList.get(i);
			EventInfoUtil eventInfoUtil = new EventInfoUtil();
			makeEventInfo = eventInfoUtil.makeEventInfo("CommitTransfer", user, "CommitTransfer", "", "");
			SetEventInfo setEventInfo = new SetEventInfo();
//			String shipRequestName = boxList.get(i).getShipRequestName();
//			String shipRequestDetailName = boxList.get(i).getShipRequestDetailName();
			String siteName = boxList.get(i).getSiteName();
			String materialPackingName = boxList.get(i).getMaterialPackingName();
			String charge = boxList.get(i).getCharge();
			String materialSpecName = boxList.get(i).getMaterialSpecName();
			String receiveRequestName = boxList.get(i).getReceiveRequestName();
			String receiveRequestDetailName = boxList.get(i).getReceiveRequestDetailName();
//			String locationName = boxList.get(i).getLocationName();
//			String erpFactory = boxList.get(i).getErpFactory();
			
			MaterialPacking materialPacking = MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(new MaterialPackingKey("SDK",box.getMaterialPackingName()));
			String shipRequestName = receiveRequestName;
			String shipRequestDetailName = receiveRequestDetailName;
			double mqty = materialPacking.getMaterialQuantity2() == 0L ? materialPacking.getMaterialQuantity() : materialPacking.getMaterialQuantity2();
//			String sdk_id = "".equals(materialPacking.getSDK_ID()) ? null : materialPacking.getSDK_ID();
//			String orderSql2 = "SELECT DISTINCT MD.SHIPREQUESTNAME,MD.SHIPREQUESTDETAILNAME,MD.REQUESTQUANTITY,MD.ASSIGNEDQUANTITY, MD.ERPRECEIVEFACTORY FROM MATERIALSHIPREQUESTDETAIL "
//					+ "MD LEFT JOIN MATERIALSHIPREQUEST MT ON MT.SHIPREQUESTNAME = MD.SHIPREQUESTNAME "
//					+ "WHERE MD.SHIPREQUESTNAME = :SHIPREQUESTNAME AND MT.SHIPREQUESTTYPE = '4E' AND MD.SDK_ID  = :SDK_ID "
//					+ "AND MD.MATERIALSPECNAME = :MATERIALSPECNAME AND MD.PHASE = :PHASE AND MD.REQUESTQUANTITY - MD.ASSIGNEDQUANTITY = " + materialPacking.getMaterialQuantity();
			
			String orderSql2 = "SELECT DISTINCT MD.SHIPREQUESTNAME,MD.SHIPREQUESTDETAILNAME,MD.REQUESTQUANTITY,MD.ASSIGNEDQUANTITY, MD.ERPRECEIVEFACTORY FROM MATERIALSHIPREQUESTDETAIL "
					+ "MD LEFT JOIN MATERIALSHIPREQUEST MT ON MT.SHIPREQUESTNAME = MD.SHIPREQUESTNAME "
					+ "WHERE MD.SHIPREQUESTNAME = :SHIPREQUESTNAME AND MT.SHIPREQUESTTYPE = '4E' AND NVL(MD.SDK_ID,'~')  = NVL(:SDK_ID,'~')"
					+ "AND MD.MATERIALSPECNAME = :MATERIALSPECNAME AND NVL(MD.PHASE,'~') = NVL(:PHASE,'~') AND (MD.CHARGE = :CHARGE OR MD.CHARGE IS NULL) AND MD.REQUESTQUANTITY - MD.ASSIGNEDQUANTITY = " + mqty;
			
			pc +=  "'" + materialPacking.getUnDoID() + "',";
			Map<String, Object> orderMap2 = new HashMap<String, Object>();
			orderMap2.put("SHIPREQUESTNAME",materialPacking.getUnDoID()); 
			orderMap2.put("SDK_ID",materialPacking.getSDK_ID() ); 
			orderMap2.put("PHASE",materialPacking.getPHASE() ); 
			orderMap2.put("MATERIALSPECNAME",materialPacking.getMaterialSpecName());
			orderMap2.put("CHARGE",charge);
			List<Map<String,Object>> orderList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(orderSql2, orderMap2);
			
//			if(orderList2.size() < 1) {
//				throw new GlobalException("未找到条码对应的单据!");
//			}
			
			if(orderList2.size() < 1) {
				//当一个批次多个行项时，查出当前批次的所有行项求和
				String orderSql3 = "SELECT DISTINCT MD.SHIPREQUESTNAME,MD.SHIPREQUESTDETAILNAME,MD.REQUESTQUANTITY,MD.ASSIGNEDQUANTITY, MD.ERPRECEIVEFACTORY FROM MATERIALSHIPREQUESTDETAIL "
						+ "MD LEFT JOIN MATERIALSHIPREQUEST MT ON MT.SHIPREQUESTNAME = MD.SHIPREQUESTNAME "
						+ "WHERE MD.SHIPREQUESTNAME = :SHIPREQUESTNAME AND MT.SHIPREQUESTTYPE = '4E' AND NVL(MD.SDK_ID,'~')  = NVL(:SDK_ID,'~') "
						+ "AND MD.MATERIALSPECNAME = :MATERIALSPECNAME AND NVL(MD.PHASE,'~') = NVL(:PHASE,'~') AND (MD.CHARGE = :CHARGE OR MD.CHARGE IS NULL) ";
				orderMap2.put("CHARGE",materialPacking.getCharge() ); 
				List<Map<String,Object>> orderList3 = IDMFrameServiceProxy.getSqlTemplate().queryForList(orderSql3, orderMap2);
				
				if(orderList3.size() > 0) {
					
//					 double requestQuantityAll = 0;
//					 double assignedQuantityAll = 0;
					BigDecimal requestQuantityAll = new BigDecimal(0);
					BigDecimal assignedQuantityAll = new BigDecimal(0);
					BigDecimal assignedQuantitysub = new BigDecimal(0);
					BigDecimal mqNty = BigDecimal.valueOf(mqty);
					for (int j = 0; j < orderList3.size(); j++) {
//						requestQuantityAll = requestQuantityAll + Double.parseDouble(orderList3.get(j).get("REQUESTQUANTITY").toString());
//						assignedQuantityAll = assignedQuantityAll + Double.parseDouble(orderList3.get(j).get("ASSIGNEDQUANTITY").toString());
						requestQuantityAll = requestQuantityAll.add(new BigDecimal(orderList3.get(j).get("REQUESTQUANTITY").toString()));
						assignedQuantityAll = assignedQuantityAll.add(new BigDecimal(orderList3.get(j).get("ASSIGNEDQUANTITY").toString()));
					}
					// 如果相等 说明一个批次多个行项存在
					assignedQuantitysub = requestQuantityAll.subtract(assignedQuantityAll);
//					if(requestQuantityAll - assignedQuantityAll == mqty) {
//					System.out.println(mqNty);
//					System.out.println(assignedQuantitysub);
//					System.out.println(mqNty.compareTo(assignedQuantitysub) == 0);
					if(mqNty.compareTo(assignedQuantitysub) == 0) {
						for (int jk = 0; jk < orderList3.size(); jk++) {
							String shipRequestName3 = (String) orderList3.get(jk).get("SHIPREQUESTNAME");
							String shipRequestDetailName3 = (String) orderList3.get(jk).get("SHIPREQUESTDETAILNAME");
								Map<String, Object> hashMapD = new HashMap<String,Object> ();
//							materialPacking.getUnDoID().equals(receiveRequestName)
								hashMapD.put("shipRequestName", shipRequestName3);
								hashMapD.put("shipRequestDetailName", shipRequestDetailName3);
								hashMapD.put("erpLocation", erpLocation);
								hashMapD.put("erpFactory", erpFactory);
								hashMapD.put("locationName", locationName);
//								hashMapD.put("materialQuantity", materialPacking.getMaterialQuantity());
//								由于一个批次多个行项，一个标签里的数量是多个行项的汇总数量，所以总数量相等时，只能单独给每个行项自己给自己赋值
								hashMapD.put("materialQuantity", mqty);
								setEventInfo.setUserColumns(hashMapD);
							    hashMapD.put("ASSIGNEDQUANTITY", orderList3.get(jk).get("REQUESTQUANTITY"));
								MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
								String sql3 = "UPDATE MATERIALSHIPREQUESTDETAIL T SET T.ASSIGNEDQUANTITY = T.ASSIGNEDQUANTITY + :ASSIGNEDQUANTITY "
										+ "WHERE T.SHIPREQUESTNAME = :shipRequestName AND T.SHIPREQUESTDETAILNAME  = :shipRequestDetailName ";
								IDMFrameServiceProxy.getSqlTemplate().update(sql3, hashMapD);
						}
					}
//					else {
//						throw new GlobalException("未找到条码对应的单据且不存在一个批次多个行项!");
//					}
					
					
				}else {
					throw new GlobalException("未找到条码对应的单据!");
				}
				
				
			}else {
				
				String shipRequestName2 = (String) orderList2.get(0).get("SHIPREQUESTNAME");
				String shipRequestDetailName2 = (String) orderList2.get(0).get("SHIPREQUESTDETAILNAME");
					Map<String, Object> hashMap = new HashMap<String,Object> ();
//				materialPacking.getUnDoID().equals(receiveRequestName)
					hashMap.put("shipRequestName", shipRequestName2);
					hashMap.put("shipRequestDetailName", shipRequestDetailName2);
					hashMap.put("erpLocation", erpLocation);
					hashMap.put("erpFactory", erpFactory);
					hashMap.put("locationName", locationName);
					hashMap.put("materialQuantity", materialPacking.getMaterialQuantity());
					setEventInfo.setUserColumns(hashMap);
					MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
					String sql2 = "UPDATE MATERIALSHIPREQUESTDETAIL T SET T.ASSIGNEDQUANTITY = T.ASSIGNEDQUANTITY + :materialQuantity "
							+ "WHERE T.SHIPREQUESTNAME = :shipRequestName AND T.SHIPREQUESTDETAILNAME  = :shipRequestDetailName ";
					IDMFrameServiceProxy.getSqlTemplate().update(sql2, hashMap);
				
			}
			
			
			
		}
		pc += "'')";
		
		
		String sql = "SELECT DISTINCT SHIPREQUESTNAME FROM MATERIALSHIPREQUEST WHERE  SHIPREQUESTNAME IN " +  pc;
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, new HashMap<String,Object> ());
		for (Map<String, Object> map : queryForList) {
			String shiprequestName = map.get("SHIPREQUESTNAME").toString();
			String billCode = toSAPServiceImpl.AlloctionStockIn(shiprequestName, "SDK", user, erpLocation);
//			String billCode = UUID.randomUUID().toString();
			//判断是否获得物料凭证
			if (billCode == null || "".equals(billCode)) {
				throw new GlobalException("ERP过账失败！");
			}

			EventInfoUtil eventInfoUtil = new EventInfoUtil();
			String condition1="WHERE SHIPREQUESTNAME = ?   AND STOCKSTATE='StockOut' AND ERPLOCATION = ? ";
			String[] bindSet2 = {shiprequestName, erpLocation};
			List<MaterialPacking> MaterialPackingList = MaterialPackingServiceProxy.getMaterialPackingService().select(condition1, bindSet2);
			for (MaterialPacking materialPacking : MaterialPackingList) {
				SetEventInfo setEventInfo = new SetEventInfo();
				//更新批次，数量，库位状态
				Map<String, Object> hashMap = new HashMap<String,Object> ();
				hashMap.put("stockState", "Stocked");
				hashMap.put("shipRequestName", "");
				hashMap.put("shipRequestDetailName", "");
//				hashMap.put("commitDate",commitDate);
				hashMap.put("unDoID", billCode);
				hashMap.put("messageId",opCode);
				if ("OK".equals(oqaresultstate)) {
					hashMap.put("oqaResultState", GenericServiceProxy.getConstantMap().OQA_State_End);
					hashMap.put("oqaResult","OK");
					hashMap.put("packingGrade", "OK");
				} else {
					hashMap.put("oqaResultState",GenericServiceProxy.getConstantMap().OQA_State_Pre);
					hashMap.put("oqaResult","");
					hashMap.put("packingGrade", "");
				}
				setEventInfo.setUserColumns(hashMap);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
			}
			SaveUnDoInfo( billCode,commitDate);
			String sql3 = "UPDATE MATERIALSHIPREQUEST T SET T.SHIPREQUESTSTATE = 'Completed' WHERE T.SHIPREQUESTNAME = :SHIPREQUESTNAME ";
			IDMFrameServiceProxy.getSqlTemplate().update(sql3, new HashMap<String,Object> () {{
				this.put("SHIPREQUESTNAME", shiprequestName);
			}});
			
			//发送品质质检 根据OPCODE来发送质检
			try {
				qMSServiceImpl.TCDBIQCInfoSend( opCode,shiprequestName,erpFactory);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			
		}

		

		
		return true;
	}
	
	/**
	 * 接收ERP返回消息更新WMS凭证
	 * @param
	 */
	public void SaveUnDoInfo( String undoid,String commitDate) {

		String insertUnDOMaterialPackingInfo = "insert into BS_MATERIALPACKINGUNDOINFO (unDoID,preundoid,UNDODETAILNAME,undoidstate,UNDOIDDATE,"
				+ "SITENAME, MATERIALPACKINGNAME, MATERIALPACKINGTYPE, CONTENTMATERIALTYPE,"
				+ "MATERIALTYPE, MATERIALSPECNAME, SUPERMATERIALPACKINGNAME, PACKINGGRADE, SUBPACKINGQUANTITY, MATERIALCREATEQUANTITY,"
				+ "MATERIALQUANTITY, PACKINGSTATE, OLDPACKINGSTATE, STOCKSTATE, OLDSTOCKSTATE, HOLDSTATE, AREANAME, LOCATIONNAME,"
				+ "DURABLENAME, MATERIALPROCESSGROUPNAME, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER,"
				+ "LASTEVENTCOMMENT, CREATETIME, CREATEUSER, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME,"
				+ "MAKEDATE, RECEIVEACTNO, STOCKINTYPE, RECEIVETIME, RECEIVEUSER, SHIPREQUESTNAME, SHIPREQUESTDETAILNAME, SHIPTIME,"
				+ "SHIPUSER, REASONCODETYPE, OQARESULTSTATE, OQARESULT, ERPLOCATION, ERPFACTORY, REASONCODE, CHARGE, SUPPLIERNAME,ETCSTATE,ERPEVENTTIME,NPFLAG)"
				+ "select unDoID,'' as preundoid,UNDODETAILNAME,'Completed' as undoidstate,:commitDate AS UNDOIDDATE," //
				+ "SITENAME, MATERIALPACKINGNAME, MATERIALPACKINGTYPE, CONTENTMATERIALTYPE,"
				+ "MATERIALTYPE, MATERIALSPECNAME, SUPERMATERIALPACKINGNAME, PACKINGGRADE, SUBPACKINGQUANTITY, MATERIALCREATEQUANTITY,"
				+ "MATERIALQUANTITY, PACKINGSTATE, OLDPACKINGSTATE, STOCKSTATE, OLDSTOCKSTATE, HOLDSTATE, AREANAME, LOCATIONNAME,"
				+ "DURABLENAME, MATERIALPROCESSGROUPNAME, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER,"
				+ "LASTEVENTCOMMENT, CREATETIME, CREATEUSER, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME,"
				+ "MAKEDATE, RECEIVEACTNO, STOCKINTYPE, RECEIVETIME, RECEIVEUSER, SHIPREQUESTNAME, SHIPREQUESTDETAILNAME, SHIPTIME,"
				+ "SHIPUSER, REASONCODETYPE, OQARESULTSTATE, OQARESULT, ERPLOCATION, ERPFACTORY, REASONCODE, CHARGE, SUPPLIERNAME,ETCSTATE,ERPEVENTTIME,NPFLAG"
				+ " from MATERIALPACKING  "
				+ " where unDoID=:UNDOID";

		Map<String, Object> bindMap_insert = new HashMap<String, Object>();
		bindMap_insert.put("UNDOID", undoid);
		bindMap_insert.put("commitDate", commitDate);
		IDMFrameServiceProxy.getSqlTemplate()
				.update(insertUnDOMaterialPackingInfo, bindMap_insert);

	}






	
	
}
