package com.cim.idm.mwmsextend.materialreceiverequest.service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cim.idm.wmspackage.receiverequestdetail.management.MaterialReceiveRequestDetailService;
import org.jdom.Document;
import org.jdom.Element;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import com.cim.idm.wmsextend.generic.util.ConvertUtil;
import com.cim.idm.wmspackage.materialreceiverequestact.MaterialReceiveRequestActServiceProxy;
import com.cim.idm.wmspackage.materialreceiverequestactdetail.MaterialReceiveRequestActDetailServiceProxy;
import com.cim.idm.wmspackage.materialreceiverequestactdetail.management.info.MaterialReceiveActDetailSetEventInfo;
import com.cim.idm.wmspackage.materialreceivetactdetail.management.data.MaterialReceiveActDetailKey;
import com.cim.idm.wmspackage.materialshiprequestdetail.MaterialShipRequestDetailServiceProxy;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.data.MaterialShipRequestDetail;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.data.MaterialShipRequestDetailKey;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.info.NonPrimaryKeyValueInfo;
import com.cim.idm.wmspackage.materialshiprequesttooem.MaterialShipRequestToOEMServiceProxy;
import com.cim.idm.wmspackage.materialshiprequesttooem.management.data.MaterialShipRequestToOEM;
import com.cim.idm.wmspackage.materialshiprequesttooem.management.data.MaterialShipRequestToOEMKey;
import com.cim.idm.wmspackage.materialshiprequesttooem.management.info.SetMaterialShipRequestToOEMEventInfo;
//import com.cim.idm.wmspackage.productshiprequest.ShipRequestServiceProxy;
import com.cim.idm.wmspackage.receiverequestbatch.management.data.MaterialReceiveActKey;
import com.cim.idm.wmspackage.receiverequestdetail.ReceiveRequestDetailServiceProxy;
import com.cim.idm.wmspackage.receiverequestdetail.management.data.MaterialReceiveRequestDetail;
import com.cim.idm.wmspackage.receiverequestdetail.management.data.MaterialReceiveRequestDetailKey;
import com.cim.idm.wmspackage.receiverequestdetail.management.info.CreateReceiveRequestDetailInfo;
import com.cim.idm.wmspackage.receiverequestdetail.management.info.MakeDetailCompletedInfo;
import com.cim.idm.wmspackage.receiverequestdetail.management.info.MakeDetailConfirmedInfo;
import com.cim.idm.wmspackage.receiverequestdetail.management.info.MakeDetailReceivingInfo;
import com.cim.idm.wmspackage.receiverequestdetail.management.info.RemoveRequestDetailInfo;
import com.cim.idm.wmspackage.receiverequestdetail.management.info.SetEventInfo;

public class MaterialReceiveRequestDetailServiceImpl {
	
	public void create(EventInfo eventInfo, String siteName, String receiveRequestName, String receiveRequestDetailName
			,String pocategory,String receiveRequestDetailType,String materialSpecName,String description,String requestQuantity,
			String receivedQuantity,String erpLocation,String returnFlag,String freeFlag,String forceCompletedFlag,String receiveRequestDetailState,
			String holdState,String iqcFlag,String planDate, String applicant, String charge,String erpFactory) throws ParseException
	{
		MaterialReceiveRequestDetailKey key = new MaterialReceiveRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);
		key.setReceiveRequestDetailName(receiveRequestDetailName);
		
		
		CreateReceiveRequestDetailInfo createInfo = new CreateReceiveRequestDetailInfo();
		
		createInfo.setSiteName(siteName);
		createInfo.setReceiveRequestName(receiveRequestName);		
		createInfo.setReceiveRequestDetailName(receiveRequestDetailName);
		createInfo.setPocategory(pocategory);
		createInfo.setReceiveRequestDetailType(receiveRequestDetailType);
		createInfo.setMaterialSpecName(materialSpecName);
		createInfo.setDescription(description);

		double requestQty = Double.parseDouble(requestQuantity);

		double receivedQty = Double.parseDouble(receivedQuantity);
				
		createInfo.setRequestQuantity(requestQty);
		createInfo.setReceivedQuantity(receivedQty);		
		createInfo.setErpLocation(erpLocation);
		createInfo.setReturnFlag(returnFlag);
		createInfo.setFreeFlag(freeFlag);
		createInfo.setForceCompletedFlag(forceCompletedFlag);
		createInfo.setReceiveRequestDetailState(receiveRequestDetailState);
		createInfo.setHoldState(holdState);
		createInfo.setIqcFlag(iqcFlag);
		
		//Timestamp planDateTime = ConvertUtil.convertToTimeStamp(planDate);
		planDate = planDate + " 00:00:00";		
		try{
				
			Timestamp planDateTime = Timestamp.valueOf(planDate);	
			createInfo.setPlanDate(planDateTime);
		}
		catch(Exception ex){
		}
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("charge",charge);
		bindMap.put("applicant",applicant);
		bindMap.put("erpFactory", erpFactory);
		createInfo.setUserColumns(bindMap);
		
									
		eventInfo.setEventName("Create");		
		
		ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().create(key, eventInfo, createInfo);
	}
	
	
	public void create1(EventInfo eventInfo, String siteName, String receiveRequestName, String receiveRequestDetailName
			,String pocategory,String receiveRequestDetailType,String materialSpecName,String description,String requestQuantity,
			String receivedQuantity,String erpLocation,String returnFlag,String freeFlag,String forceCompletedFlag,String receiveRequestDetailState,
			String holdState,String iqcFlag,String planDate,String erpFactory) throws ParseException
	{
		MaterialReceiveRequestDetailKey key = new MaterialReceiveRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);
		key.setReceiveRequestDetailName(receiveRequestDetailName);
		
		
		CreateReceiveRequestDetailInfo createInfo = new CreateReceiveRequestDetailInfo();
		
		createInfo.setSiteName(siteName);
		createInfo.setReceiveRequestName(receiveRequestName);		
		createInfo.setReceiveRequestDetailName(receiveRequestDetailName);
		createInfo.setPocategory(pocategory);
		createInfo.setReceiveRequestDetailType(receiveRequestDetailType);
		createInfo.setMaterialSpecName(materialSpecName);
		createInfo.setDescription(description);

		double requestQty = Double.parseDouble(requestQuantity);

		double receivedQty = Double.parseDouble(receivedQuantity);
		
		createInfo.setRequestQuantity(requestQty);
		createInfo.setReceivedQuantity(receivedQty);		
		createInfo.setErpLocation(erpLocation);
		createInfo.setReturnFlag(returnFlag);
		createInfo.setFreeFlag(freeFlag);
		createInfo.setForceCompletedFlag(forceCompletedFlag);
		createInfo.setReceiveRequestDetailState(receiveRequestDetailState);
		createInfo.setHoldState(holdState);
		createInfo.setIqcFlag(iqcFlag);
		
		try{
			Timestamp planDateTime = ConvertUtil.convertToTimeStamp(planDate);
			createInfo.setPlanDate(planDateTime);
			}
			catch(Exception e){}
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("erpFactory",erpFactory);
		createInfo.setUserColumns(bindMap);
		
						
		eventInfo.setEventName("Create");		
		
		ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().create(key, eventInfo, createInfo);
	}
	
	public void modify(EventInfo eventInfo, String siteName, String receiveRequestName, String receiveRequestDetailName
			,String pocategory,String receiveRequestDetailType,String materialSpecName,String description,String requestQuantity,
			String receivedQuantity,String erpLocation,String returnFlag,String freeFlag,String forceCompletedFlag,String receiveRequestDetailstate,
			String holdState,String iqcFlag,String planDate, String applicant, String charge)
	{
		MaterialReceiveRequestDetailKey key = new MaterialReceiveRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);
		key.setReceiveRequestDetailName(receiveRequestDetailName);
		
		
		SetEventInfo info = new SetEventInfo();
		
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		
		bindMap.put("pocategory", pocategory);
		bindMap.put("receiveRequestDetailType", receiveRequestDetailType);
		bindMap.put("materialSpecName", materialSpecName);
		bindMap.put("description", description);
		bindMap.put("requestQuantity", Double.parseDouble(requestQuantity));
		bindMap.put("receivedQuantity", Double.parseDouble(receivedQuantity));
		bindMap.put("erpLocation", erpLocation);
		bindMap.put("returnFlag", returnFlag);
		bindMap.put("freeFlag", freeFlag);
		bindMap.put("forceCompletedFlag", forceCompletedFlag);
		bindMap.put("receiveRequestDetailState", receiveRequestDetailstate);
		bindMap.put("holdState", holdState);
		bindMap.put("iqcFlag", iqcFlag);						
		//bindMap.put("planDate", ConvertUtil.convertToTimeStamp(planDate));
		
		try{
		planDate = planDate + " 00:00:00";		
		bindMap.put("planDate", Timestamp.valueOf(planDate));
		}
		catch(Exception e){}
		
		
		bindMap.put("charge",charge);
		bindMap.put("applicant",applicant);		
		
		info.setUserColumns(bindMap);
		
		eventInfo.setEventName("Modify");
			
		ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().setEvent(key, eventInfo, info);
	}
	
	public void modify1(EventInfo eventInfo, String siteName, String receiveRequestName, String receiveRequestDetailName
			,String pocategory,String receiveRequestDetailType,String materialSpecName,String description,String requestQuantity,
			String receivedQuantity,String erpLocation,String returnFlag,String freeFlag,String forceCompletedFlag,String receiveRequestDetailState,
			String holdState,String iqcFlag,String planDate,String erpFactory)
	{
		MaterialReceiveRequestDetailKey key = new MaterialReceiveRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);
		key.setReceiveRequestDetailName(receiveRequestDetailName);
		
		
		SetEventInfo info = new SetEventInfo();
		
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		
		bindMap.put("pocategory", pocategory);
		bindMap.put("receiveRequestDetailType", receiveRequestDetailType);
		bindMap.put("materialSpecName", materialSpecName);
		bindMap.put("description", description);
		bindMap.put("requestQuantity", Double.parseDouble(requestQuantity));
		bindMap.put("receivedQuantity", Double.parseDouble(receivedQuantity));
		bindMap.put("erpLocation", erpLocation);
		bindMap.put("erpFactory", erpFactory);	
		bindMap.put("returnFlag", returnFlag);
		bindMap.put("freeFlag", freeFlag);
		bindMap.put("forceCompletedFlag", forceCompletedFlag);
		bindMap.put("receiveRequestDetailState", receiveRequestDetailState);
		bindMap.put("holdState", holdState);
		bindMap.put("iqcFlag", iqcFlag);				
		try{
		bindMap.put("planDate", ConvertUtil.convertToTimeStamp(planDate));
		}
		catch(Exception e){}
		
		
		info.setUserColumns(bindMap);
		
		eventInfo.setEventName("Modify");
		
			
		ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().setEvent(key, eventInfo, info);
	}
	//增加修改申请人信息
	public void modifyApplicant(EventInfo eventInfo, String siteName, String receiveRequestName, String receiveRequestDetailName
			,String pocategory,String materialSpecName,String description,String requestQuantity,
			String receivedQuantity,String erpLocation,String erpFactory,String planDate,String applicant)
	{
		MaterialReceiveRequestDetailKey key = new MaterialReceiveRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);
		key.setReceiveRequestDetailName(receiveRequestDetailName);
		
		
		SetEventInfo info = new SetEventInfo();
		
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		
		bindMap.put("pocategory", pocategory);
		bindMap.put("materialSpecName", materialSpecName);
		bindMap.put("description", description);
		bindMap.put("requestQuantity", Double.parseDouble(requestQuantity));
		bindMap.put("receivedQuantity", Double.parseDouble(receivedQuantity));
		bindMap.put("erpLocation", erpLocation);
		bindMap.put("erpFactory", erpFactory);
		bindMap.put("applicant", applicant);
		try{
		bindMap.put("planDate", ConvertUtil.convertToTimeStamp(planDate));
		}
		catch(Exception e){}
		
		
		info.setUserColumns(bindMap);
		
		eventInfo.setEventName("Modify");
		
			
		ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().setEvent(key, eventInfo, info);
	}
public void Reserve(EventInfo eventInfo,String siteName,List<String> DetailList, String receiveRequestName, String receiveRequestDetailState){
		
//		String lastEventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
//		Timestamp eventTime = TimeStampUtil.getCurrentTimestamp();	
		
		MakeDetailReceivingInfo makeDetailReceivingInfo = new MakeDetailReceivingInfo();
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("receiveRequestDetailState", receiveRequestDetailState);
		makeDetailReceivingInfo.setUserColumns(bindMap);
		List<MaterialReceiveRequestDetailKey> keyList = new ArrayList<MaterialReceiveRequestDetailKey>();
		for(String detailName : DetailList){
			MaterialReceiveRequestDetailKey keyInfo = new MaterialReceiveRequestDetailKey(siteName, receiveRequestName,detailName);
			keyList.add(keyInfo);
		}
		
		ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().makeReceiving(keyList, eventInfo, makeDetailReceivingInfo);
    }

public void Confirm(EventInfo eventInfo,String siteName,List<String> DetailList, String receiveRequestName, String receiveRequestDetailState){
	
//	String lastEventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
//	Timestamp eventTime = TimeStampUtil.getCurrentTimestamp();	
	
	MakeDetailConfirmedInfo makeDetailConfirmedInfo = new MakeDetailConfirmedInfo();
	Map<String, Object> bindMap = new HashMap<String, Object>();
	 
	bindMap.put("receiveRequestDetailState", receiveRequestDetailState);
	makeDetailConfirmedInfo.setUserColumns(bindMap);
	List<MaterialReceiveRequestDetailKey> keyList = new ArrayList<MaterialReceiveRequestDetailKey>();
	for(String detailName : DetailList){
		MaterialReceiveRequestDetailKey keyInfo = new MaterialReceiveRequestDetailKey(siteName, receiveRequestName,detailName);
		keyList.add(keyInfo);
	}
	
	ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().makeConfirmed(keyList, eventInfo, makeDetailConfirmedInfo);
}

public void Complete(EventInfo eventInfo,String siteName,List<String> DetailList, String receiveRequestName, String receiveRequestDetailState){
	

	MakeDetailCompletedInfo makeDetailCompletedInfo = new MakeDetailCompletedInfo();
	Map<String, Object> bindMap = new HashMap<String, Object>();
	 
	bindMap.put("receiveRequestDetailState", receiveRequestDetailState);
	makeDetailCompletedInfo.setUserColumns(bindMap);
	List<MaterialReceiveRequestDetailKey> keyList = new ArrayList<MaterialReceiveRequestDetailKey>();
	for(String detailName : DetailList){
		MaterialReceiveRequestDetailKey keyInfo = new MaterialReceiveRequestDetailKey(siteName, receiveRequestName,detailName);
		keyList.add(keyInfo);
	}
	
	ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().makeCompleted(keyList, eventInfo, makeDetailCompletedInfo);
}
	
	public void remove(EventInfo eventInfo, String siteName, String receiveRequestName, String receiveRequestDetailName)
	{
		MaterialReceiveRequestDetailKey key = new MaterialReceiveRequestDetailKey();
		
		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);
		key.setReceiveRequestDetailName(receiveRequestDetailName);
		
		RemoveRequestDetailInfo info = new RemoveRequestDetailInfo();
					
		eventInfo.setEventName("Remove");	
		
		ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().remove(key, eventInfo, info);
	}
	
	/*
	* Name : ModifyRequestQtyInfo
	* Desc : This function is ModifyRequestQtyInfo
	* Author : boe cim dev.
	* Date : 2016.03.10
	*/
	public static void ModifyRequestQtyInfo(EventInfo eventInfo,String siteName,
			String receiveRequestName,String receiveRequestDetailName,String receiveQuantity,
			String receiveRequestDetailQty,String makeDate,String receiveActNO,String actDetailQuantity,String iqcFlag) throws Exception
	{
		try
		{
			//修改行项目数量信息
			MaterialReceiveRequestDetailKey  receiveRequestDetailKey = new MaterialReceiveRequestDetailKey();
			MaterialReceiveRequestDetail receiveRequestDetail = new MaterialReceiveRequestDetail();
			SetEventInfo setEventInfo = new SetEventInfo();
			receiveRequestDetailKey.setReceiveRequestDetailName(receiveRequestDetailName);
			receiveRequestDetailKey.setReceiveRequestName(receiveRequestName);
			receiveRequestDetailKey.setSiteName(siteName);
			
			Map<String, Object> bindMap = new HashMap<String, Object>();
			
			bindMap.put("receivedQuantity", Double.parseDouble(receiveQuantity));
			bindMap.put("iqcFlag", iqcFlag);//setEvent接口会把原有的iqcflag置空，需要手动添加进去
			setEventInfo.setUserColumns(bindMap);
			MaterialReceiveRequestDetailService receiveRequestDetailService = ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService();
			ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().setEvent(receiveRequestDetailKey, eventInfo, setEventInfo);
		   receiveRequestDetail=ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().selectByKey(receiveRequestDetailKey);
		   
		   if(receiveRequestDetail.getReceivedQuantity()!=receiveRequestDetail.getRequestQuantity() &&receiveRequestDetail.getReceivedQuantity()<=receiveRequestDetail.getRequestQuantity())
		   {
			   ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().makeReceiving(receiveRequestDetailKey, eventInfo, new MakeDetailReceivingInfo());
		   }
		   
		   //修改批次数量信息
		   MaterialReceiveActDetailKey materialRecieveActDtetailKey = new MaterialReceiveActDetailKey();
		   materialRecieveActDtetailKey.setMakeDate(Timestamp.valueOf(makeDate));
		   materialRecieveActDtetailKey.setReceiveActNO(receiveActNO);
		   materialRecieveActDtetailKey.setReceiveRequestDetailName(receiveRequestDetailName);
		   materialRecieveActDtetailKey.setReceiveRequestName(receiveRequestName);
		   materialRecieveActDtetailKey.setSiteName(siteName);
		   Map<String, Object> bindMap1 = new HashMap<String, Object>();
			MaterialReceiveActDetailSetEventInfo setEventInfo1 = new MaterialReceiveActDetailSetEventInfo();
			
			bindMap1.put("receiveQuantity", Double.parseDouble(actDetailQuantity));
			
			setEventInfo1.setUserColumns(bindMap1);
			
			if(actDetailQuantity.equals("0"))
			{
				MaterialReceiveRequestActDetailServiceProxy.getMaterialReceiveRequestActDetailService().delete(materialRecieveActDtetailKey);
			}else{
			MaterialReceiveRequestActDetailServiceProxy.getMaterialReceiveRequestActDetailService().
			setEvent(materialRecieveActDtetailKey, eventInfo, setEventInfo1);
			}
		}
		catch(Exception e)
		{
			throw e;
		}

	}
	
	public void RemoveReceiveAct(List<String> receiveActList,String siteName,List<String> receiveReuqstNameList){
		String sqlcheck = "select * from materialpacking where receiveactno=:receiveactno and sitename=:sitename";
		
		
		String sql ="delete from materialreceiveactdetail where sitename=:sitename and receiveactno=:receiveactno";
		String sql1 ="delete from materialreceiveact where sitename=:sitename and receiveactno=:receiveactno";
		HashMap<String, Object> bindMap = new HashMap<String, Object>();
		
		List<MaterialReceiveActKey> receiveActKeyList = new ArrayList<MaterialReceiveActKey>();
		for(int i=0;i<receiveActList.size();i++)
		{
			MaterialReceiveActKey receiveActKey = new MaterialReceiveActKey();
			receiveActKey.setReceiveactNO(receiveActList.get(i));
			receiveActKey.setSiteName(siteName);
			receiveActKey.setReceiveRequestName(receiveReuqstNameList.get(i));
			receiveActKeyList.add(receiveActKey);
			bindMap.put("sitename", siteName);
			bindMap.put("receiveactno", receiveActList.get(i));
			List<Map<String, Object>> queryResult = 
					IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlcheck, bindMap);
			if(queryResult==null||queryResult.size()==0){
				IDMFrameServiceProxy.getSqlTemplate().update(sql, bindMap);
				IDMFrameServiceProxy.getSqlTemplate().update(sql1, bindMap);
			}
			
		}
		MaterialReceiveRequestActServiceProxy.getReceiveRequestBatchService().delete(receiveActKeyList);
		
		
	}
	
	/*
	* Name : ModifyShipRequestQtyInfo
	* Desc : This function is ModifyShipRequestQtyInfo
	* Author : boe cim dev.
	* Date : 2016.03.10
	*/
	public static void ModifyShipRequestQtyInfo(EventInfo eventInfo,String siteName,
			String receiveRequestName,String receiveRequestDetailName,String receiveQuantity,
			String receiveRequestDetailQty,String makeDate,String receiveActNO,String actDetailQuantity) throws Exception
	{
		try
		{
			//修改行项目数量信息
			MaterialShipRequestDetailKey  shipRequestDetailKey = new MaterialShipRequestDetailKey();
			MaterialShipRequestDetail shipRequestDetail = new MaterialShipRequestDetail();
			NonPrimaryKeyValueInfo setEventInfo = new NonPrimaryKeyValueInfo();
			shipRequestDetailKey.setShipRequestDetailName(receiveRequestDetailName);
			shipRequestDetailKey.setShipRequestName(receiveRequestName);
			shipRequestDetailKey.setSiteName(siteName);
			
			Map<String, Object> bindMap = new HashMap<String, Object>();
			
			bindMap.put("assignedQuantity", Double.parseDouble(receiveQuantity));
			
			setEventInfo.setUserColumns(bindMap);
			
		   MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().setEvent(shipRequestDetailKey, setEventInfo);
		   
//		   shipRequestDetail=MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().selectByKey(shipRequestDetailKey);
//		   
//		   if(shipRequestDetail.getAssignedQuantity()!=shipRequestDetail.getRequestQuantity() &&shipRequestDetail.getAssignedQuantity()<=shipRequestDetail.getRequestQuantity())
//		   {
//			   MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().setEvent(shipRequestDetailKey, nonPrimaryKeyValueInfo);
//		   }
		   
		   //修改批次数量信息
		   MaterialReceiveActDetailKey materialRecieveActDtetailKey = new MaterialReceiveActDetailKey();
		   materialRecieveActDtetailKey.setMakeDate(Timestamp.valueOf(makeDate));
		   materialRecieveActDtetailKey.setReceiveActNO(receiveActNO);
		   materialRecieveActDtetailKey.setReceiveRequestDetailName(receiveRequestDetailName);
		   materialRecieveActDtetailKey.setReceiveRequestName(receiveRequestName);
		   materialRecieveActDtetailKey.setSiteName(siteName);
		   Map<String, Object> bindMap1 = new HashMap<String, Object>();
			MaterialReceiveActDetailSetEventInfo setEventInfo1 = new MaterialReceiveActDetailSetEventInfo();
			
			bindMap1.put("receiveQuantity", Double.parseDouble(receiveRequestDetailQty));
			
			setEventInfo1.setUserColumns(bindMap1);
			if(actDetailQuantity.equals("0"))
			{
				MaterialReceiveRequestActDetailServiceProxy.getMaterialReceiveRequestActDetailService().delete(materialRecieveActDtetailKey);
			}else{
			MaterialReceiveRequestActDetailServiceProxy.getMaterialReceiveRequestActDetailService().
			setEvent(materialRecieveActDtetailKey, eventInfo, setEventInfo1);
			}
		}
		catch(Exception e)
		{
			throw e;
		}

	}
	
	/*
	* Name : ModifyPOOEMRequestQtyInfo
	* Desc : This function is ModifyPOOEMRequestQtyInfo
	* Author : boe cim dev.
	* Date : 2016.03.10
	*/
	public static void ModifyPOOEMRequestQtyInfo(EventInfo eventInfo,String siteName,
			String receiveRequestName,String receiveRequestDetailName,String receiveQuantity,
			String receiveRequestDetailQty,String makeDate,String receiveActNO,String subMaterialSpecName,String actDetailQuantity) throws Exception
	{
		try
		{
			//修改行项目数量信息
		    MaterialShipRequestToOEMKey	  shipRequestToOEMKey = new MaterialShipRequestToOEMKey();
			MaterialShipRequestToOEM shipRequestToOEM = new MaterialShipRequestToOEM();
			SetMaterialShipRequestToOEMEventInfo setEventInfo = new SetMaterialShipRequestToOEMEventInfo();
			shipRequestToOEMKey.setSiteName(siteName);
			shipRequestToOEMKey.setReceiveRequestName(receiveRequestName);
			shipRequestToOEMKey.setReceiveRequestDetailName(receiveRequestDetailName);
			shipRequestToOEMKey.setSubMaterialSpecName(subMaterialSpecName);
			
			Map<String, Object> bindMap = new HashMap<String, Object>();
			
			bindMap.put("assignedQuantity", Integer.parseInt(receiveRequestDetailQty));
			
			setEventInfo.setUserColumns(bindMap);
			
		   MaterialShipRequestToOEMServiceProxy.getMaterialShipRequestToOEMService().setEvent(shipRequestToOEMKey,eventInfo, setEventInfo);
		   
//		   shipRequestDetail=MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().selectByKey(shipRequestDetailKey);
//		   
//		   if(shipRequestDetail.getAssignedQuantity()!=shipRequestDetail.getRequestQuantity() &&shipRequestDetail.getAssignedQuantity()<=shipRequestDetail.getRequestQuantity())
//		   {
//			   MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().setEvent(shipRequestDetailKey, nonPrimaryKeyValueInfo);
//		   }
		   
		   //修改批次数量信息
		   MaterialReceiveActDetailKey materialRecieveActDtetailKey = new MaterialReceiveActDetailKey();
		   materialRecieveActDtetailKey.setMakeDate(Timestamp.valueOf(makeDate));
		   materialRecieveActDtetailKey.setReceiveActNO(receiveActNO);
		   materialRecieveActDtetailKey.setReceiveRequestDetailName(receiveRequestDetailName+"_"+subMaterialSpecName);
		   materialRecieveActDtetailKey.setReceiveRequestName(receiveRequestName);
		   materialRecieveActDtetailKey.setSiteName(siteName);
		   Map<String, Object> bindMap1 = new HashMap<String, Object>();
			MaterialReceiveActDetailSetEventInfo setEventInfo1 = new MaterialReceiveActDetailSetEventInfo();
			
			bindMap1.put("receiveQuantity", Integer.parseInt(actDetailQuantity));
			
			setEventInfo1.setUserColumns(bindMap1);
			if(actDetailQuantity.equals("0"))
			{
				MaterialReceiveRequestActDetailServiceProxy.getMaterialReceiveRequestActDetailService().delete(materialRecieveActDtetailKey);
			}else{
			MaterialReceiveRequestActDetailServiceProxy.getMaterialReceiveRequestActDetailService().
			setEvent(materialRecieveActDtetailKey, eventInfo, setEventInfo1);
			}
		}
		catch(Exception e)
		{
			throw e;
		}

	}
	
	public void poAssign(EventInfo eventInfo,String erpPoName,String siteName,String receiveRequestName) throws Exception
	{
		try
		{
			String sql = "select * from materialReceiveRequestDetail where siteName = :siteName and receiveRequestName= :receiveRequestName";
			
			Map<String,String> bindMap = new HashMap<String,String>();
			bindMap.put("siteName", siteName);
			bindMap.put("receiveRequestName", receiveRequestName);
						
			List<Map<String, Object>> queryResult = 
					IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
			
			
			String sql1 = "select * from materialReceiveRequestDetail where siteName = :siteName and receiveRequestName= :erpPoName";
			
			Map<String,String> bindMap1 = new HashMap<String,String>();
			bindMap1.put("siteName", siteName);
			bindMap1.put("erpPoName", erpPoName);
						
			List<Map<String, Object>> queryResult1 = 
					IDMFrameServiceProxy.getSqlTemplate().queryForList(sql1, bindMap1);
						

			for(int i=0;i<queryResult.size();i++)
			{
				String erpLocation= queryResult.get(i).get("ERPLOCATION").toString();	
				for(int k=0;k<queryResult1.size();k++)
				{
					String materialSpecName= queryResult.get(i).get("materialSpecName").toString();
					String materialSpecName1= queryResult1.get(k).get("materialSpecName").toString();
					String erpLocation1= queryResult1.get(k).get("ERPLOCATION").toString();
					if(materialSpecName.equals(materialSpecName1)){
						if(erpLocation.equals(erpLocation1)){
						String sql3 =  "select * from materialReceiveRequestDetail where siteName = :siteName and receiveRequestName=:erpPoName and materialSpecName =:materialSpecName";
						
						Map<String,String> bindMap3 = new HashMap<String,String>();
						bindMap3.put("siteName", siteName);
						bindMap3.put("erpPoName", erpPoName);
						bindMap3.put("materialSpecName", materialSpecName);
						
						List<Map<String, Object>> queryResult3 = 
								IDMFrameServiceProxy.getSqlTemplate().queryForList(sql3, bindMap3);
						
						String materialReceiveRequestDetail= queryResult3.get(0).get("RECEIVEREQUESTDETAILNAME").toString();
						
						
						String sql4 =  "select * from materialReceiveRequestDetail where siteName = :siteName and receiveRequestName=:receiveRequestName and materialSpecName =:materialSpecName";
						
						Map<String,String> bindMap6 = new HashMap<String,String>();
						bindMap6.put("siteName", siteName);
						bindMap6.put("receiveRequestName", receiveRequestName);
						bindMap6.put("materialSpecName", materialSpecName);
						
						List<Map<String, Object>> queryResult4 = 
								IDMFrameServiceProxy.getSqlTemplate().queryForList(sql4, bindMap6);
						
						
						String receivedQuantity= queryResult3.get(0).get("RECEIVEDQUANTITY").toString();

							double sumQuantity = Double.parseDouble(queryResult3.get(0).get("REQUESTQUANTITY").toString());
						String requestQuntity= queryResult4.get(0).get("REQUESTQUANTITY").toString();

							double qty = Double.parseDouble(receivedQuantity) + Double.parseDouble(requestQuntity);
						
						if(sumQuantity<qty)	{
							throw new CustomException("QUANTITYFAILED","","");
						}										
						String sql2 = "UPDATE materialReceiveRequestDetail SET ERPPODETAILNAME = :ERPPODETAILNAME , ERPPONAME = :ERPPONAME" +
								" WHERE SITENAME = :SITENAME AND RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND materialSpecName =:materialSpecName";
						Map<String, Object> bindMap4 = new HashMap<String, Object>();
						bindMap4.put("ERPPODETAILNAME", materialReceiveRequestDetail);
						bindMap4.put("ERPPONAME", erpPoName);
						bindMap4.put("SITENAME", siteName);			
						bindMap4.put("RECEIVEREQUESTNAME", receiveRequestName);		
						bindMap4.put("materialSpecName", materialSpecName);	
						
						IDMFrameServiceProxy.getSqlTemplate().update(sql2, bindMap4);
						
						
						String sql5 = "UPDATE materialReceiveRequest SET ERPPONAME = :ERPPONAME " +
								" WHERE SITENAME = :SITENAME AND RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME";
						Map<String,String> bindMap5 = new HashMap<String,String>();
						bindMap5.put("ERPPONAME", erpPoName);
						bindMap5.put("SITENAME", siteName);			
						bindMap5.put("RECEIVEREQUESTNAME", receiveRequestName);
						
						IDMFrameServiceProxy.getSqlTemplate().update(sql5, bindMap5);
						
						String sql6 = "UPDATE materialReceiveRequestDetail SET RECEIVEDQUANTITY = :qty " +
								" WHERE SITENAME = :SITENAME AND RECEIVEREQUESTNAME = :ERPPONAME AND materialSpecName =:materialSpecName";
										
						Map<String, Object> bindMap7 = new HashMap<String, Object>();
						bindMap7.put("qty", qty);
						bindMap7.put("SITENAME", siteName);			
						bindMap7.put("ERPPONAME", erpPoName);
						bindMap7.put("materialSpecName", materialSpecName);	
						
						IDMFrameServiceProxy.getSqlTemplate().update(sql6, bindMap7);
						}
						else{
							throw new CustomException("ERPLOCATIONFAILED","","");
						}
					}
					
				}
				
			}
			
		}
		catch(Exception e)
		{
			throw e;
		}
		
	}

public void poDessign(EventInfo eventInfo,String erpPoName,String siteName,String receiveRequestName) throws Exception
{
	try
	{
		String sql = "select * from materialReceiveRequestDetail where siteName = :siteName and receiveRequestName= :receiveRequestName";
		
		Map<String,String> bindMap = new HashMap<String,String>();
		bindMap.put("siteName", siteName);
		bindMap.put("receiveRequestName", receiveRequestName);
					
		List<Map<String, Object>> queryResult = 
				IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		
		
		String sql1 = "select * from materialReceiveRequestDetail where siteName = :siteName and receiveRequestName= :erpPoName";
		
		Map<String,String> bindMap1 = new HashMap<String,String>();
		bindMap1.put("siteName", siteName);
		bindMap1.put("erpPoName", erpPoName);
					
		List<Map<String, Object>> queryResult1 = 
				IDMFrameServiceProxy.getSqlTemplate().queryForList(sql1, bindMap1);
					

		for(int i=0;i<queryResult.size();i++)
		{
			String erpLocation= queryResult.get(i).get("ERPLOCATION").toString();	
			for(int k=0;k<queryResult1.size();k++)
			{
				String materialSpecName= queryResult.get(i).get("materialSpecName").toString();
				String materialSpecName1= queryResult1.get(k).get("materialSpecName").toString();
				String erpLocation1= queryResult1.get(k).get("ERPLOCATION").toString();
				if(materialSpecName.equals(materialSpecName1)){
					if(erpLocation.equals(erpLocation1)){
					String sql3 =  "select * from materialReceiveRequestDetail where siteName = :siteName and receiveRequestName=:erpPoName and materialSpecName =:materialSpecName";
					
					Map<String,String> bindMap3 = new HashMap<String,String>();
					bindMap3.put("siteName", siteName);
					bindMap3.put("erpPoName", erpPoName);
					bindMap3.put("materialSpecName", materialSpecName);
					
					List<Map<String, Object>> queryResult3 = 
							IDMFrameServiceProxy.getSqlTemplate().queryForList(sql3, bindMap3);
					
					String materialReceiveRequestDetail= queryResult3.get(0).get("RECEIVEREQUESTDETAILNAME").toString();
					
					
					String sql4 =  "select * from materialReceiveRequestDetail where siteName = :siteName and receiveRequestName=:receiveRequestName and materialSpecName =:materialSpecName";
					
					Map<String,String> bindMap6 = new HashMap<String,String>();
					bindMap6.put("siteName", siteName);
					bindMap6.put("receiveRequestName", receiveRequestName);
					bindMap6.put("materialSpecName", materialSpecName);
					
					List<Map<String, Object>> queryResult4 = 
							IDMFrameServiceProxy.getSqlTemplate().queryForList(sql4, bindMap6);
					
					
					String receivedQuantity= queryResult3.get(0).get("RECEIVEDQUANTITY").toString();

						double sumQuantity = Double.parseDouble(queryResult3.get(0).get("REQUESTQUANTITY").toString());
					String requestQuntity= queryResult4.get(0).get("REQUESTQUANTITY").toString();

						double qty = Double.parseDouble(receivedQuantity) - Double.parseDouble(requestQuntity);
					
					if(sumQuantity<qty)	{
						throw new CustomException("QUANTITYFAILED","","");
					}										
					String sql2 = "UPDATE materialReceiveRequestDetail SET ERPPODETAILNAME = '' , ERPPONAME = '' " +
							" WHERE SITENAME = :SITENAME AND RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND materialSpecName =:materialSpecName";
					Map<String, Object> bindMap4 = new HashMap<String, Object>();
					//bindMap4.put("ERPPODETAILNAME", materialReceiveRequestDetail);
					//bindMap4.put("ERPPONAME", erpPoName);
					bindMap4.put("SITENAME", siteName);			
					bindMap4.put("RECEIVEREQUESTNAME", receiveRequestName);		
					bindMap4.put("materialSpecName", materialSpecName);	
					
					IDMFrameServiceProxy.getSqlTemplate().update(sql2, bindMap4);
					
					
					String sql5 = "UPDATE materialReceiveRequest SET ERPPONAME = '' " +
							" WHERE SITENAME = :SITENAME AND RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME";
					Map<String,String> bindMap5 = new HashMap<String,String>();
					//bindMap5.put("ERPPONAME", erpPoName);
					bindMap5.put("SITENAME", siteName);			
					bindMap5.put("RECEIVEREQUESTNAME", receiveRequestName);
					
					IDMFrameServiceProxy.getSqlTemplate().update(sql5, bindMap5);
					
					String sql6 = "UPDATE materialReceiveRequestDetail SET RECEIVEDQUANTITY = :qty " +
							" WHERE SITENAME = :SITENAME AND RECEIVEREQUESTNAME = :ERPPONAME AND materialSpecName =:materialSpecName";
									
					Map<String, Object> bindMap7 = new HashMap<String, Object>();
					bindMap7.put("qty", qty);
					bindMap7.put("SITENAME", siteName);			
					bindMap7.put("ERPPONAME", erpPoName);
					bindMap7.put("materialSpecName", materialSpecName);	
					
					IDMFrameServiceProxy.getSqlTemplate().update(sql6, bindMap7);
					}
					else{
						throw new CustomException("ERPLOCATIONFAILED","","");
					}
				}
				
			}
			
		}
		
	}
	catch(Exception e)
	{
		throw e;
	}
	
}
//B10增加修改PO数量
public void modifyPOReceivedQty(EventInfo eventInfo,String siteName,String receiveRequestName,String receiveRequestDetailName,String materialSpecName,String description,String erpFactory,
		String erpLocation,String requestQuantity,String receivedQuantity)
{
	
	MaterialReceiveRequestDetailKey key = new MaterialReceiveRequestDetailKey();
	key.setSiteName(siteName);
	key.setReceiveRequestName(receiveRequestName);
	key.setReceiveRequestDetailName(receiveRequestDetailName);
	
	SetEventInfo info = new SetEventInfo();
	
	Map<String, Object> bindMap = new HashMap<String, Object>();
	
	bindMap.put("erpLocation", erpLocation);
	bindMap.put("erpFactory", erpFactory);
	bindMap.put("materialSpecName", materialSpecName);
	bindMap.put("description", description);
	bindMap.put("requestQuantity", Double.parseDouble(requestQuantity));
	bindMap.put("receivedQuantity", Double.parseDouble(receivedQuantity));
	
	info.setUserColumns(bindMap);
	
	eventInfo.setEventName("ModifyPOReceivedQty");
	
		
	ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService().setEvent(key, eventInfo, info);
	
}

/**
 * 
 * @param eventInfo
 * @param message
 * @param eventUser
 */
public void modifiedIQCFlag(Document message,String eventUser)
{
	Element root = message.getDocument().getRootElement();
	Element bodyElement = root.getChild("Body");
			
	String receiveRequestname = bodyElement.getChild(
			"RECEIVEREQUESTNAME").getText();
	String siteName = bodyElement.getChild(
			"SITENAME").getText();
	String iqcFlag = bodyElement.getChild(
			"IQCFLAG").getText();
	Map<String, Object> bindMap = new HashMap<String, Object>();
	bindMap.put("RECEIVEREQUESTNAME",receiveRequestname);
	bindMap.put("SITENAME",siteName);
	bindMap.put("IQCFLAG",iqcFlag);
	bindMap.put("LASTEVENTUSER",eventUser);
	String sql = "UPDATE MATERIALRECEIVEREQUESTDETAIL B SET B.IQCFLAG = :IQCFLAG, B.LASTEVENTTIME = SYSDATE,B.LASTEVENTUSER = :LASTEVENTUSER "
			+ "WHERE B.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND B.SITENAME = :SITENAME";
		IDMFrameServiceProxy.getSqlTemplate().update(sql, bindMap);
}

}
