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

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


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.framework.orm.service.CommonServiceDAO;
import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.framework.util.xml.JdomUtils;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import com.cim.idm.wmsextend.generic.util.ConvertUtil;
import com.cim.idm.wmspackage.customer.management.data.Customer;
import com.cim.idm.wmspackage.customer.management.data.CustomerKey;
import com.cim.idm.wmspackage.customer.management.impl.CustomerServiceImpl;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPackingKey;
import com.cim.idm.wmspackage.materialpacking.management.info.SetEventInfo;
import com.cim.idm.wmspackage.materialreceiverequestact.MaterialReceiveRequestActServiceProxy;
import com.cim.idm.wmspackage.materialreceiverequestact.management.info.CreateReceiveRequestBatchInfo;
import com.cim.idm.wmspackage.materialshiprequest.MaterialShipRequestServiceProxy;
import com.cim.idm.wmspackage.materialshiprequest.management.data.MaterialShipRequest;
import com.cim.idm.wmspackage.materialshiprequest.management.data.MaterialShipRequestKey;
import com.cim.idm.wmspackage.materialshiprequest.management.info.CreateMaterialShipRequestInfo;
import com.cim.idm.wmspackage.materialshiprequest.management.info.MaterialShipRequestMakeConfirmedInfo;
import com.cim.idm.wmspackage.materialshiprequest.management.info.MaterialShipRequestMakeReservedInfo;
import com.cim.idm.wmspackage.materialshiprequest.management.info.MaterialShipRequestMakeUnReservedInfo;
import com.cim.idm.wmspackage.materialshiprequest.management.info.MaterialShipRequestRemoveInfo;
import com.cim.idm.wmspackage.materialshiprequest.management.info.MaterialShipRequestSetEventInfo;
import com.cim.idm.wmspackage.materialshiprequestdetail.MaterialShipRequestDetailServiceProxy;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.info.CreateMaterialShipRequestDetailInfo;

public class MaterialShipRequestServiceImpl extends
		CommonServiceDAO<MaterialShipRequestKey, MaterialShipRequest> {
	
	/**
	 * 2024.09.26
	 */
	public void create4WMS25(EventInfo eventInfo, String siteName,
            String shipRequestName, String shipRequestState,
            String shipRequestType, String shipRequestDetailType,
            String customerNO, String deliveryType, String shipPlanDate,
            String exportNo, String saleReason, String saleReasonDescription,
            String workOrderId, String erpReceiveFactory,
            String erpReceiveLocation, String systemType,
            String erpFactoryName, String customsDeclare, String costCenter, String receiver, String grund,
            String grtxt) {
        CreateMaterialShipRequestInfo info = new CreateMaterialShipRequestInfo();

        info.setSiteName(siteName);
        info.setShipRequestName(shipRequestName);
        info.setShipRequestState(shipRequestState);
        info.setShipRequestType(shipRequestType);
        info.setShipRequestDetailType(shipRequestDetailType);
        info.setCustomerNO(customerNO);
        info.setDeliveryType(deliveryType);
        info.setWorkOrderId(workOrderId);
        info.setSystemType(systemType);

        // Timestamp planShipDateTime =
        // ConvertUtil.convertToTimeStamp(shipPlanDate);

        Timestamp planShipDateTime = new Timestamp(System.currentTimeMillis());
        if (!shipPlanDate.equals("")) {
            shipPlanDate = shipPlanDate + " 00:00:00";
            if (siteName.equalsIgnoreCase("B4"))
                shipPlanDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").format(new Date());
            planShipDateTime = Timestamp.valueOf(shipPlanDate);
        }
        info.setShipPlanDate(planShipDateTime);

        Map<String, Object> bindMap = new HashMap<String, Object>();
        bindMap.put("exportNo", exportNo);
        bindMap.put("saleReason", saleReason);
        bindMap.put("saleReasonDescription", saleReasonDescription);
        bindMap.put("erpFactoryName", erpFactoryName);
        bindMap.put("erpReceiveFactory", erpReceiveFactory);
        bindMap.put("erpReceiveLocation", erpReceiveLocation);
        bindMap.put("customsDeclare", customsDeclare);
        bindMap.put("costCenter", costCenter);
        bindMap.put("receiver", receiver);
        bindMap.put("grund", grund);
        bindMap.put("grtxt", grtxt);
        info.setUserColumns(bindMap);

        eventInfo.setEventName("Create");

        MaterialShipRequestServiceProxy.getMaterialShipRequestService().create(
                eventInfo, info);

    }
	
	// 2024.09.26
	public void create(EventInfo eventInfo, String siteName,
            String shipRequestName, String shipRequestState,
            String shipRequestType, String shipRequestDetailType,
            String customerNO, String deliveryType, String shipPlanDate,
            String exportNo, String saleReason, String saleReasonDescription,
            String workOrderId, String erpReceiveFactory,
            String erpReceiveLocation, String systemType,
            String erpFactoryName, String customsDeclare, String costCenter, String receiver,
            String grund, String zf_State) {
        CreateMaterialShipRequestInfo info = new CreateMaterialShipRequestInfo();

        info.setSiteName(siteName);
        info.setShipRequestName(shipRequestName);
        info.setShipRequestState(shipRequestState);
        info.setShipRequestType(shipRequestType);
        info.setShipRequestDetailType(shipRequestDetailType);
        info.setCustomerNO(customerNO);
        info.setDeliveryType(deliveryType);
        info.setWorkOrderId(workOrderId);
        info.setSystemType(systemType);

        // Timestamp planShipDateTime =
        // ConvertUtil.convertToTimeStamp(shipPlanDate);

        Timestamp planShipDateTime = new Timestamp(System.currentTimeMillis());
        if (!shipPlanDate.equals("")) {
            shipPlanDate = shipPlanDate + " 00:00:00";
            if (siteName.equalsIgnoreCase("B4"))
                shipPlanDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").format(new Date());
            planShipDateTime = Timestamp.valueOf(shipPlanDate);
        }
        info.setShipPlanDate(planShipDateTime);

        Map<String, Object> bindMap = new HashMap<String, Object>();
        bindMap.put("exportNo", exportNo);
        bindMap.put("saleReason", saleReason);
        bindMap.put("saleReasonDescription", saleReasonDescription);
        bindMap.put("erpFactoryName", erpFactoryName);
        bindMap.put("erpReceiveFactory", erpReceiveFactory);
        bindMap.put("erpReceiveLocation", erpReceiveLocation);
        bindMap.put("customsDeclare", customsDeclare);
        bindMap.put("costCenter", costCenter);
        bindMap.put("receiver", receiver);
        bindMap.put("grund", grund);
        bindMap.put("zf_State", zf_State);
        info.setUserColumns(bindMap);

        eventInfo.setEventName("Create");

        MaterialShipRequestServiceProxy.getMaterialShipRequestService().create(
                eventInfo, info);

    }


	public MaterialShipRequest getShipRequest(String siteName, String invoiceNo) {
		MaterialShipRequestKey keyInfo = new MaterialShipRequestKey(siteName,
				invoiceNo);
		MaterialShipRequest selectByKey = selectByKey(keyInfo);
		return selectByKey;
	}
	public String getShipDetailType(String siteName, String invoiceNo) {
		MaterialShipRequestKey keyInfo = new MaterialShipRequestKey(siteName,
				invoiceNo);
		MaterialShipRequest selectByKey = selectByKey(keyInfo);
		return selectByKey.getShipRequestDetailType();
	}
	public void updateReserve(EventInfo eventInfo, String siteName,
			String invoiceNo, String shipRequestState) {

		String lastEventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
		Timestamp eventTime = TimeStampUtil.getCurrentTimestamp();

		MaterialShipRequestKey keyInfo = new MaterialShipRequestKey(siteName,
				invoiceNo);
		MaterialShipRequestMakeReservedInfo makeReservedInfo = new MaterialShipRequestMakeReservedInfo();

		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("shipRequestState", shipRequestState);
		makeReservedInfo.setUserColumns(bindMap);
		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.makeReserved(keyInfo, eventInfo, makeReservedInfo);
	}
	/***
	 * 2020.5.1 对应需求：特殊权限忽略先入先出
	 * @param eventInfo
	 * @param siteName
	 * @param invoiceNo
	 * @param shipRequestState
	 * @param specialFlag
	 * @param specialComment
	 */
	public void updateSpecialReserve(EventInfo eventInfo, String siteName,
			String invoiceNo, String shipRequestState,String specialFlag,String specialComment){

		MaterialShipRequestKey keyInfo = new MaterialShipRequestKey(siteName,
				invoiceNo);
		MaterialShipRequestMakeReservedInfo makeReservedInfo = new MaterialShipRequestMakeReservedInfo();

		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("shipRequestState", shipRequestState);
		bindMap.put("specialFlag", specialFlag);
		bindMap.put("specialComment", specialComment);
		makeReservedInfo.setUserColumns(bindMap);
		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.makeReserved(keyInfo, eventInfo, makeReservedInfo);
		
	}

	public void cancelReserve(EventInfo eventInfo, String siteName,
			String shipRequestName) {
		MaterialShipRequestKey keyInfo = new MaterialShipRequestKey(siteName,
				shipRequestName);
		MaterialShipRequest selectByKey = selectByKey(keyInfo);
		MaterialShipRequestMakeUnReservedInfo makeUnReservedInfo = new MaterialShipRequestMakeUnReservedInfo();
		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.makeUnReserved(keyInfo, eventInfo, makeUnReservedInfo);

	}

	public void updateConfirm(EventInfo eventInfo, String siteName,
			String invoiceNo, String shipRequestState) {
		String lastEventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
		Timestamp eventTime = TimeStampUtil.getCurrentTimestamp();

		MaterialShipRequestKey keyInfo = new MaterialShipRequestKey(siteName,
				invoiceNo);
		MaterialShipRequest selectByKey = selectByKey(keyInfo);
		MaterialShipRequestMakeConfirmedInfo makeConfirmedInfo = new MaterialShipRequestMakeConfirmedInfo();

		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("shipRequestState", shipRequestState);
		makeConfirmedInfo.setUserColumns(bindMap);

		selectByKey.setShipRequestState(shipRequestState);
		selectByKey.setLastEventTime(eventTime);
		selectByKey.setLastEventTimeKey(lastEventTimeKey);

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.makeConfirmed(keyInfo, eventInfo, makeConfirmedInfo);
		MaterialShipRequestServiceProxy.getMaterialShipRequestService().update(selectByKey);
	}
	
	public void updateSending(EventInfo eventInfo, String siteName,
			String invoiceNo, String shipRequestState) {
		String lastEventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
		Timestamp eventTime = TimeStampUtil.getCurrentTimestamp();

		MaterialShipRequestKey keyInfo = new MaterialShipRequestKey(siteName,
				invoiceNo);
		MaterialShipRequest selectByKey = selectByKey(keyInfo);
		MaterialShipRequestMakeConfirmedInfo makeConfirmedInfo = new MaterialShipRequestMakeConfirmedInfo();

		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("shipRequestState", shipRequestState);
		makeConfirmedInfo.setUserColumns(bindMap);

		selectByKey.setShipRequestState(shipRequestState);
		selectByKey.setLastEventTime(eventTime);
		selectByKey.setLastEventTimeKey(lastEventTimeKey);
		
		MaterialShipRequestServiceProxy.getMaterialShipRequestService().update(selectByKey);
	}

	public void MakeCancelConfirm(EventInfo eventInfo, String siteName,
			String invoiceNo, String shipRequestState) {
		String lastEventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
		Timestamp eventTime = TimeStampUtil.getCurrentTimestamp();

		MaterialShipRequestKey keyInfo = new MaterialShipRequestKey(siteName,
				invoiceNo);
		MaterialShipRequest selectByKey = selectByKey(keyInfo);
		//MaterialShipRequestMakeUnConfirmedInfo makeUnConfirmedInfo = new MaterialShipRequestMakeUnConfirmedInfo();
		MaterialShipRequestSetEventInfo setEventInfo = new MaterialShipRequestSetEventInfo();
		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("shipRequestState", shipRequestState);
		//makeUnConfirmedInfo.setUserColumns(bindMap);
		setEventInfo.setUserColumns(bindMap);
		selectByKey.setShipRequestState(shipRequestState);
		selectByKey.setLastEventTime(eventTime);
		selectByKey.setLastEventTimeKey(lastEventTimeKey);

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.setEvent(keyInfo, eventInfo, setEventInfo);
	}

	public void PDAupdateConfirm(EventInfo eventInfo,
			List<String> siteNameList, String invoiceNo, String shipRequestState) throws Exception{
		try
		{
		String lastEventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
		Timestamp eventTime = TimeStampUtil.getCurrentTimestamp();
		String sql="SELECT SHIPREQUESTDETAILNAME,REQUESTQUANTITY,ASSIGNEDQUANTITY FROM MATERIALSHIPREQUESTDETAIL WHERE SHIPREQUESTNAME=:shipRequestName";
		Map<String, String> bindMap1 = new HashMap<String, String>();
		bindMap1.put("shipRequestName", invoiceNo);

		List<Map<String, Object>> queryResult2 = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql, bindMap1);
		for(int i=0;i<queryResult2.size();i++)
		{
			if(!(queryResult2.get(i).get("REQUESTQUANTITY").toString().equals(queryResult2.get(i).get("ASSIGNEDQUANTITY").toString())))
			{
				throw  new CustomException("AssignQuantityError",queryResult2.get(i).get("SHIPREQUESTDETAILNAME").toString(),"");
			}
		}
		MaterialShipRequestKey keyInfo = new MaterialShipRequestKey(
				siteNameList.get(0).toString(), invoiceNo);
		// MaterialShipRequest selectByKey = selectByKey(keyInfo);
		MaterialShipRequestMakeConfirmedInfo makeConfirmedInfo = new MaterialShipRequestMakeConfirmedInfo();
		
		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("shipRequestState", shipRequestState);

		makeConfirmedInfo.setUserColumns(bindMap);

		// selectByKey.setShipRequestState(shipRequestState);
		// selectByKey.setLastEventTime(eventTime);
		// selectByKey.setLastEventTimeKey(lastEventTimeKey);

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.makeConfirmed(keyInfo, eventInfo, makeConfirmedInfo);
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

	public void create(EventInfo eventInfo, String siteName,
			String shipRequestName, String shipRequestState,
			String shipRequestType, String shipRequestDetailType,
			String customerNO, String deliveryType, String shipPlanDate,
			String exportNo, String saleReason, String saleReasonDescription,
			String workOrderId, String erpReceiveFactory,
			String erpReceiveLocation, String systemType,
			String erpFactoryName, String customsDeclare,String costCenter,String receiver) {
		CreateMaterialShipRequestInfo info = new CreateMaterialShipRequestInfo();

		info.setSiteName(siteName);
		info.setShipRequestName(shipRequestName);
		info.setShipRequestState(shipRequestState);
		info.setShipRequestType(shipRequestType);
		info.setShipRequestDetailType(shipRequestDetailType);
		info.setCustomerNO(customerNO);
		info.setDeliveryType(deliveryType);
		info.setWorkOrderId(workOrderId);
		info.setSystemType(systemType);

		// Timestamp planShipDateTime =
		// ConvertUtil.convertToTimeStamp(shipPlanDate);

		Timestamp planShipDateTime = new Timestamp(System.currentTimeMillis());
		if (!shipPlanDate.equals("")) {
			shipPlanDate = shipPlanDate + " 00:00:00";
			if(siteName.equalsIgnoreCase("B4"))
			   shipPlanDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").format(new Date());
			planShipDateTime = Timestamp.valueOf(shipPlanDate);
		}
		info.setShipPlanDate(planShipDateTime);

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("exportNo", exportNo);
		bindMap.put("saleReason", saleReason);
		bindMap.put("saleReasonDescription", saleReasonDescription);
		bindMap.put("erpFactoryName", erpFactoryName);
		bindMap.put("erpReceiveFactory", erpReceiveFactory);
		bindMap.put("erpReceiveLocation", erpReceiveLocation);
		bindMap.put("customsDeclare", customsDeclare);
		bindMap.put("costCenter", costCenter);
		bindMap.put("receiver", receiver);
		info.setUserColumns(bindMap);

		eventInfo.setEventName("Create");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService().create(
				eventInfo, info);

	}

	public void create1(EventInfo eventInfo, String siteName,
			String shipRequestName, String systemType, String shipRequestState,
			String shipRequestType, String shipRequestDetailType,
			String customerNO, String deliveryType, String shipPlanDate,
			String exportNo, String erpFactoryName, String erpReceiveFactory,
			String erpReceiveLocation, String saleReason,
			String saleReasonDescription, String customsDeclare) {
		CreateMaterialShipRequestInfo info = new CreateMaterialShipRequestInfo();

		info.setSiteName(siteName);
		info.setShipRequestName(shipRequestName);
		info.setSystemType(systemType);
		info.setShipRequestState(shipRequestState);
		info.setShipRequestType(shipRequestType);
		info.setShipRequestDetailType(shipRequestDetailType);
		info.setCustomerNO(customerNO);
		info.setDeliveryType(deliveryType);

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("exportNo", exportNo);
		bindMap.put("erpFactoryName", erpFactoryName);
		bindMap.put("erpReceiveFactory", erpReceiveFactory);
		bindMap.put("erpReceiveLocation", erpReceiveLocation);
		bindMap.put("saleReason", saleReason);
		bindMap.put("saleReasonDescription", saleReasonDescription);
		bindMap.put("customsDeclare", customsDeclare);
		info.setUserColumns(bindMap);

		Timestamp planShipDateTime = ConvertUtil
				.convertToTimeStamp(shipPlanDate);
		;
		info.setShipPlanDate(planShipDateTime);

		eventInfo.setEventName("Create");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService().create(
				eventInfo, info);

	}
	public void createForOEM(EventInfo eventInfo, String siteName,
			String shipRequestName, String systemType, String shipRequestState,
			String shipRequestType, String shipRequestDetailType,
			String customerNO, String deliveryType, String shipPlanDate,
			String exportNo, String erpFactoryName, String erpReceiveFactory,
			String erpReceiveLocation, String saleReason,
			String saleReasonDescription, String customsDeclare,String costCenter,String receiver) {
		CreateMaterialShipRequestInfo info = new CreateMaterialShipRequestInfo();

		info.setSiteName(siteName);
		info.setShipRequestName(shipRequestName);
		info.setSystemType(systemType);
		info.setShipRequestState(shipRequestState);
		info.setShipRequestType(shipRequestType);
		info.setShipRequestDetailType(shipRequestDetailType);
		info.setCustomerNO(customerNO);
		info.setDeliveryType(deliveryType);

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("exportNo", exportNo);
		bindMap.put("erpFactoryName", erpFactoryName);
		bindMap.put("erpReceiveFactory", erpReceiveFactory);
		bindMap.put("erpReceiveLocation", erpReceiveLocation);
		bindMap.put("saleReason", saleReason);
		bindMap.put("saleReasonDescription", saleReasonDescription);
		bindMap.put("customsDeclare", customsDeclare);
		bindMap.put("costCenter", costCenter);
		bindMap.put("receiver", receiver);
		info.setUserColumns(bindMap);

		Timestamp planShipDateTime = ConvertUtil
				.convertToTimeStamp(shipPlanDate);
		;
		info.setShipPlanDate(planShipDateTime);

		eventInfo.setEventName("Create");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService().create(
				eventInfo, info);

	}
	public void BMDTcreate(EventInfo eventInfo, String siteName,
			String shipRequestName, String systemType, String shipRequestState,
			String shipRequestDetailType,
			String customerNO, String deliveryType, String shipPlanDate,
			String erpFactoryName, String erpReceiveFactory) throws Exception{
		CreateMaterialShipRequestInfo info = new CreateMaterialShipRequestInfo();

		info.setSiteName(siteName);
		info.setShipRequestName(shipRequestName);
		info.setSystemType(systemType);
		info.setShipRequestState(shipRequestState);
		info.setShipRequestDetailType(shipRequestDetailType);
		info.setCustomerNO(customerNO);
		info.setDeliveryType(deliveryType);

		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("erpFactoryName", erpFactoryName);
		bindMap.put("erpReceiveFactory", erpReceiveFactory);

		info.setUserColumns(bindMap);

		try {
			Timestamp planShipDateTime = ConvertUtil
					.convertToTimeStamp(shipPlanDate);
			info.setShipPlanDate(planShipDateTime);

			eventInfo.setEventName("Create");

			MaterialShipRequestServiceProxy.getMaterialShipRequestService().create(
					eventInfo, info);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void modify(EventInfo eventInfo, String siteName,
			String shipRequestName, String shipRequestState,
			String shipRequestType, String shipRequestDetailType,
			String customerNO, String deliveryType, String shipPlanDate,
			String exportNo) {

		MaterialShipRequestKey key = new MaterialShipRequestKey();

		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);

		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("shipRequestState", shipRequestState);
		bindMap.put("shipRequestType", shipRequestType);
		bindMap.put("shipRequestDetailType", shipRequestDetailType);
		bindMap.put("customerNO", customerNO);
		bindMap.put("deliveryType", deliveryType);
		// bindMap.put("shipPlanDate",
		// ConvertUtil.convertToTimeStamp(shipPlanDate));

		shipPlanDate = shipPlanDate + " 00:00:00";
		bindMap.put("shipPlanDate", Timestamp.valueOf(shipPlanDate));

		bindMap.put("exportNo", exportNo);

		MaterialShipRequestSetEventInfo setEventInfo = new MaterialShipRequestSetEventInfo();
		setEventInfo.setUserColumns(bindMap);

		eventInfo.setEventName("Modify");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.setEvent(key, eventInfo, setEventInfo);

	}
	
	public void modifyReceiver(EventInfo eventInfo, String siteName,
			String shipRequestName,String costCenter, String receiver) {

		MaterialShipRequestKey key = new MaterialShipRequestKey();

		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);

		Map<String, Object> bindMap = new HashMap<String, Object>();

	
		bindMap.put("costCenter", costCenter);
	
		bindMap.put("receiver", receiver);

		MaterialShipRequestSetEventInfo setEventInfo = new MaterialShipRequestSetEventInfo();
		setEventInfo.setUserColumns(bindMap);

		eventInfo.setEventName("Modify");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.setEvent(key, eventInfo, setEventInfo);

	}

	public void modify1(EventInfo eventInfo, String siteName,
			String shipRequestName, String systemType, String shipRequestState,
			String shipRequestType, String shipRequestDetailType,
			String customerNO, String deliveryType, String shipPlanDate,
			String exportNo, String erpFactoryName, String erpReceiveFactory,
			String erpReceiveLocation, String saleReason,
			String saleReasonDescription, String customsDeclare) {

		MaterialShipRequestKey key = new MaterialShipRequestKey();

		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);

		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("shipRequestState", shipRequestState);
		bindMap.put("shipRequestType", shipRequestType);
		// bindMap.put("systemType", systemType);
		bindMap.put("shipRequestDetailType", shipRequestDetailType);
		bindMap.put("customerNO", customerNO);
		bindMap.put("deliveryType", deliveryType);
		bindMap.put("shipPlanDate",
				ConvertUtil.convertToTimeStamp(shipPlanDate));

		bindMap.put("exportNo", exportNo);
		bindMap.put("erpFactoryName", erpFactoryName);
		bindMap.put("erpReceiveFactory", erpReceiveFactory);
		bindMap.put("erpReceiveLocation", erpReceiveLocation);
		bindMap.put("saleReason", saleReason);
		bindMap.put("saleReasonDescription", saleReasonDescription);
		bindMap.put("customsDeclare", customsDeclare);

		MaterialShipRequestSetEventInfo setEventInfo = new MaterialShipRequestSetEventInfo();
		setEventInfo.setUserColumns(bindMap);

		eventInfo.setEventName("Modify");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.setEvent(key, eventInfo, setEventInfo);

	}
	public void modifyForOEM(EventInfo eventInfo, String siteName,
			String shipRequestName, String systemType, String shipRequestState,
			String shipRequestType, String shipRequestDetailType,
			String customerNO, String deliveryType, String shipPlanDate,
			String exportNo, String erpFactoryName, String erpReceiveFactory,
			String erpReceiveLocation, String saleReason,
			String saleReasonDescription, String customsDeclare,String costCenter,String receiver) {

		MaterialShipRequestKey key = new MaterialShipRequestKey();

		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);

		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("shipRequestState", shipRequestState);
		bindMap.put("shipRequestType", shipRequestType);
		// bindMap.put("systemType", systemType);
		bindMap.put("shipRequestDetailType", shipRequestDetailType);
		bindMap.put("customerNO", customerNO);
		bindMap.put("deliveryType", deliveryType);
		bindMap.put("shipPlanDate",
				ConvertUtil.convertToTimeStamp(shipPlanDate));

		bindMap.put("exportNo", exportNo);
		bindMap.put("erpFactoryName", erpFactoryName);
		bindMap.put("erpReceiveFactory", erpReceiveFactory);
		bindMap.put("erpReceiveLocation", erpReceiveLocation);
		bindMap.put("saleReason", saleReason);
		bindMap.put("saleReasonDescription", saleReasonDescription);
		bindMap.put("customsDeclare", customsDeclare);
        bindMap.put("costCenter", costCenter);
        bindMap.put("receiver", receiver);
		MaterialShipRequestSetEventInfo setEventInfo = new MaterialShipRequestSetEventInfo();
		setEventInfo.setUserColumns(bindMap);

		eventInfo.setEventName("Modify");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.setEvent(key, eventInfo, setEventInfo);

	}
	public void BMDTmodify(EventInfo eventInfo, String siteName,
			String shipRequestName,
			String shipRequestDetailType,
			String customerNO, String shipPlanDate,
			String erpFactoryName, String erpReceiveFactory) {

		MaterialShipRequestKey key = new MaterialShipRequestKey();

		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);

		Map<String, Object> bindMap = new HashMap<String, Object>();


		bindMap.put("shipRequestDetailType", shipRequestDetailType);
		bindMap.put("customerNO", customerNO);
		bindMap.put("shipPlanDate",
				ConvertUtil.convertToTimeStamp(shipPlanDate));
		bindMap.put("erpFactoryName", erpFactoryName);
		bindMap.put("erpReceiveFactory", erpReceiveFactory);

		MaterialShipRequestSetEventInfo setEventInfo = new MaterialShipRequestSetEventInfo();
		setEventInfo.setUserColumns(bindMap);

		eventInfo.setEventName("Modify");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.setEvent(key, eventInfo, setEventInfo);

	}

	public void remove(EventInfo eventInfo, String siteName,
			String shipRequestName) {

		MaterialShipRequestKey key = new MaterialShipRequestKey();

		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);

		MaterialShipRequestRemoveInfo removeInfo = new MaterialShipRequestRemoveInfo();

		eventInfo.setEventName("Remove");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService().remove(
				key, eventInfo, removeInfo);

	}
public void updateComplete(EventInfo eventInfo,String siteName,String invoiceNo,String shipRequestState){
		
		String lastEventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
		Timestamp eventTime = TimeStampUtil.getCurrentTimestamp();
		
		MaterialShipRequestKey keyInfo=new MaterialShipRequestKey(siteName, invoiceNo);
		//MaterialShipRequest selectByKey = selectByKey(keyInfo);
		
		MaterialShipRequestSetEventInfo setEventInfo= new MaterialShipRequestSetEventInfo();
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("shipRequestState", shipRequestState);
		setEventInfo.setUserColumns(bindMap);
		
		//selectByKey.setLastEventTime(eventTime);
		MaterialShipRequestServiceProxy.getMaterialShipRequestService().setEvent(keyInfo, eventInfo, setEventInfo);
}
//分行项目过账
public void updateCompleteForPre(EventInfo eventInfo,String siteName,String invoiceNo,String shipRequestState){
	
	String sql="SELECT COUNT(DISTINCT SHIPREQUESTDETAILNAME) FROM MATERIALPACKING WHERE RECEIVEACTNO IN ( " +
     "SELECT  RECEIVEACTNO  FROM  MATERIALRECEIVEACT WHERE RECEIVEACTSTATE='Completed' AND SITENAME=:SITENAME AND RECEIVEREQUESTNAME=:RECEIVEREQUESTNAME  "+
	 " ) AND SHIPREQUESTNAME=:SHIPREQUESTNAME ";
	String detailSql=" SELECT COUNT(DISTINCT SHIPREQUESTDETAILNAME) FROM MATERIALSHIPREQUESTDETAIL WHERE SHIPREQUESTNAME=:SHIPREQUESTNAME AND SITENAME=:SITENAME ";
	
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("SITENAME",siteName);
	map.put("RECEIVEREQUESTNAME",invoiceNo);
	map.put("SHIPREQUESTNAME",invoiceNo);
	int i=IDMFrameServiceProxy.getSqlTemplate()
	.queryForInt(sql, map);
	int j=IDMFrameServiceProxy.getSqlTemplate()
			.queryForInt(detailSql, map);
	MaterialShipRequestKey keyInfo=new MaterialShipRequestKey(siteName, invoiceNo);
	MaterialShipRequestSetEventInfo setEventInfo= new MaterialShipRequestSetEventInfo();
	Map<String, Object> bindMap = new HashMap<String, Object>();
	if(j==i){
	   bindMap.put("shipRequestState", shipRequestState);
	  }
	setEventInfo.setUserColumns(bindMap);
	MaterialShipRequestServiceProxy.getMaterialShipRequestService().setEvent(keyInfo, eventInfo, setEventInfo);
}
public void updateShipRequest(EventInfo eventInfo,String siteName,String invoiceNo,String Undoid ){
	
	String lastEventTimeKey = TimeStampUtil.getCurrentEventTimeKey();
	Timestamp eventTime = TimeStampUtil.getCurrentTimestamp();
	
	MaterialShipRequestKey keyInfo=new MaterialShipRequestKey(siteName, invoiceNo);
	//MaterialShipRequest selectByKey = selectByKey(keyInfo);
	
	MaterialShipRequestSetEventInfo setEventInfo= new MaterialShipRequestSetEventInfo();
	
	Map<String, Object> bindMap = new HashMap<String, Object>();
	bindMap.put("shipRequestState","Confirmed");
	setEventInfo.setUserColumns(bindMap);
	
	//selectByKey.setLastEventTime(eventTime);
	MaterialShipRequestServiceProxy.getMaterialShipRequestService().setEvent(keyInfo, eventInfo, setEventInfo);
	String selectBox = "Select materialpackingname from MATERIALPACKING where sitename =:SITENAME and undoid =:UNDOID ";
	Map<String,Object> map=new HashMap<String, Object>();
	map.put("SITENAME", siteName);
	map.put("UNDOID", Undoid);
	List<Map<String, Object>> result = IDMFrameServiceProxy.getSqlTemplate()
			.queryForList(selectBox, map);
	List<MaterialPackingKey> mpKeyLsit=new ArrayList<MaterialPackingKey>();
	if(result.size()>0)
	{
		for(Map<String, Object> pkName:result){
			MaterialPackingKey mk=new MaterialPackingKey();
			mk.setSiteName(siteName);
			mk.setMaterialPackingName(pkName.get("materialpackingname").toString());
			mpKeyLsit.add(mk);										
		}
		SetEventInfo seteventInfo=new SetEventInfo();
		Map<String,Object> updateMap=new HashMap<String, Object>();
		updateMap.put("stockState","Stocked");
		//updateMap.put("receiveActNo","");
		updateMap.put("erpState","SUCCESS");
		//bindMap.put("shipRequestName", "");
		//bindMap.put("shipRequestDetailName", "");
		//冲销的虚拟库位待指定
		updateMap.put("locationName","");
//		updateMap.put("unDoID",newUnDoList.get(0).getUnDoID());
//		updateMap.put("unDoDetailName",newUnDoList.get(i).getUnDoDetailName());
		seteventInfo.setUserColumns(updateMap);
		MaterialPackingServiceProxy.getMaterialPackingService().setEvent(mpKeyLsit, eventInfo, seteventInfo);
		String updateBox="UPDATE BS_MATERIALPACKINGUNDOINFO SET UNDOIDSTATE='N' WHERE SITENAME=:SITENAME AND UNDOID=:UNDOID ";
		Map<String,Object> bindMap1=new HashMap<String, Object>();
		bindMap1.put("SITENAME", siteName);
		bindMap1.put("UNDOID", Undoid);
		IDMFrameServiceProxy.getSqlTemplate().update(updateBox, bindMap1);
	}
}
	public void createCustomer(String customerNo, String customerName,
			String destination, String address) {

		String sql = "SELECT * FROM CUSTOMER "
				+ "WHERE CUSTOMERNO = :customerNo";

		Map<String, String> bindMap = new HashMap<String, String>();
		bindMap.put("customerNo", customerNo);

		List<Map<String, Object>> queryResult = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql, bindMap);

		if (queryResult == null || queryResult.isEmpty()) {
			Customer c = new Customer();
			CustomerKey ck = new CustomerKey();
			ck.setCustomerNO(customerNo);

			c.setAddress(address);
			c.setDestination(destination);
			c.setCustomerName(customerName);
			c.setKey(ck);
			c.setLastEventTime(TimeStampUtil.getCurrentTimestamp());
			c.setLastEventUser("ERP");

			CustomerServiceImpl cs = new CustomerServiceImpl();
			cs.create(c);
		}

	}

	public Boolean checkShipRequest(String siteName, String shipRequestName) {

		// MaterialShipRequest sr = new MaterialShipRequest();
		// sr = this.getShipRequest(siteName, shipRequestName);

		String sql2 = "SELECT * FROM MATERIALSHIPREQUEST "
				+ "WHERE SITENAME = :siteName and SHIPREQUESTNAME = :shipRequestName";

		Map<String, String> bindMap2 = new HashMap<String, String>();
		bindMap2.put("siteName", siteName);
		bindMap2.put("shipRequestName", shipRequestName);

		List<Map<String, Object>> queryResult2 = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql2, bindMap2);

		if (queryResult2 == null || queryResult2.isEmpty())
			return true;// request not exist
		else
			return false;// request already exit
	}

	public void modifyInterface(EventInfo eventInfo, String siteName,
			String shipRequestName, String exportNo) {

		MaterialShipRequestKey key = new MaterialShipRequestKey();

		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);

		Map<String, Object> bindMap = new HashMap<String, Object>();

		bindMap.put("exportNo", exportNo);

		MaterialShipRequestSetEventInfo setEventInfo = new MaterialShipRequestSetEventInfo();
		setEventInfo.setUserColumns(bindMap);

		eventInfo.setEventName("Modify");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.setEvent(key, eventInfo, setEventInfo);

	}
	public void getMDLReportReply(EventInfo eventInfo, String siteName,
			String shipRequestName,String state) {

		MaterialShipRequestKey key = new MaterialShipRequestKey();

		key.setSiteName(siteName);
		key.setShipRequestName(shipRequestName);

		Map<String, Object> bindMap = new HashMap<String, Object>();
		if(state.equals("0")){
			bindMap.put("mesReceiveFlag", "Y");
		}else{
			bindMap.put("mesReceiveFlag", "N");
		}
		MaterialShipRequestSetEventInfo setEventInfo = new MaterialShipRequestSetEventInfo();
		setEventInfo.setUserColumns(bindMap);

		//eventInfo.setEventName("ShipToMDLMaterialReportReply");

		MaterialShipRequestServiceProxy.getMaterialShipRequestService()
				.setEvent(key, eventInfo, setEventInfo);

	}
	public String getERPLocation(String siteName, String shipRequestName){
		String sql = "SELECT * FROM MATERIALSHIPREQUESTDETAIL "
				+ "WHERE SITENAME = :siteName and SHIPREQUESTNAME = :shipRequestName AND ROWNUM = 1";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("siteName", siteName);
		bindMap.put("shipRequestName", shipRequestName);
		List<Map<String, Object>> queryResult = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		
		String erpLocation = "";
		if(queryResult != null){
			erpLocation = queryResult.get(0).get("ERPLOCATION").toString().substring(0, 3);
		}
		
		return erpLocation;
	}
	
	//SMT过账单据状态更新
	public void updateCompleteForSMT(EventInfo eventInfo,String siteName,String invoiceNo,String shipRequestState){
		
		boolean checkflag=true;
		String sql="SELECT SHIPREQUESTNAME,SHIPREQUESTDETAILNAME,REQUESTQUANTITY,ASSIGNEDQUANTITY FROM MATERIALSHIPREQUESTDETAIL "
				+ " WHERE SITENAME=:SITENAME "
				+ " AND SHIPREQUESTNAME=:SHIPREQUESTNAME "
				+ " ORDER BY SHIPREQUESTDETAILNAME ASC ";
		Map<String, String> bindMap1 = new HashMap<String, String>();
		bindMap1.put("SITENAME", siteName);
		bindMap1.put("SHIPREQUESTNAME", invoiceNo);
		
		List<Map<String, Object>> sqlResult
  			= IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap1);
		for(int i=0;i<sqlResult.size();i++){
			String requestQty=sqlResult.get(i).get("REQUESTQUANTITY").toString();
			String assignedQty=sqlResult.get(i).get("ASSIGNEDQUANTITY").toString();
			if(!requestQty.equals(assignedQty)){
				checkflag = false;
				break;
			}
		}

		MaterialShipRequestKey keyInfo=new MaterialShipRequestKey(siteName, invoiceNo);
		MaterialShipRequestSetEventInfo setEventInfo= new MaterialShipRequestSetEventInfo();
		Map<String, Object> bindMap = new HashMap<String, Object>();
		if(checkflag){
		   bindMap.put("shipRequestState", shipRequestState);
		  }
		setEventInfo.setUserColumns(bindMap);
		MaterialShipRequestServiceProxy.getMaterialShipRequestService().setEvent(keyInfo, eventInfo, setEventInfo);
	}
	
	//B17创建MDL自动叫料单
	public void saveMDLShipRequest(String shipRequestName,String shipRequestDetailName,
			String erpFactory,String erpLocation,EventInfo eventInfo,
			String reasonCodeType,String reasonCode,
			String workOrder,String materialSpecName,String  materialQuantity,String charge,String receiveLocation,String receiver,String shipTime,
		    String	workOrderType,String returnType,String returnTypeDes,String Line,String reMark,String supplierNO,String systemType) throws CustomException
	{
		try
		{
			String siteName=System.getProperty("factory");
			
			String queryERPInfo="SELECT ERPFACTORYNAME,ERPLOCATION FROM BS_MATERIALSHIPDEF WHERE SITENAME=:SITENAME AND MATERIALSPECNAME=:MATERIALSPECNAME";
			Map<String,String> bindMap =new HashMap<String, String>();
			bindMap.put("SITENAME", siteName);	
			bindMap.put("MATERIALSPECNAME", materialSpecName);
			
			if(systemType.equals("MESSHIP")&&returnType.equals("PDNG")){
				erpFactory = "3179";//由于可能会退料Panel避免查不到panel叫料信息
			}
			else{
				
				List<Map<String,Object>> ERPInfo=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryERPInfo, bindMap);
				erpFactory=ERPInfo.get(0).get("ERPFACTORYNAME").toString();
				if(!systemType.equals("MESSHIP")){
					erpLocation=ERPInfo.get(0).get("ERPLOCATION").toString();
				}
			}
			
			
			String queryShip="SELECT SHIPREQUESTNAME FROM MATERIALSHIPREQUEST WHERE SITENAME=:SITENAME AND SHIPREQUESTNAME=:SHIPREQUESTNAME AND SYSTEMTYPE LIKE 'MES%' ";
			Map<String,String> mapShip=new HashMap<String, String>();
			mapShip.put("SITENAME", siteName);
			mapShip.put("SHIPREQUESTNAME", shipRequestName);
			List<Map<String,Object>> shipRequest=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryShip, mapShip);
			//叫料、退库单只发一次
			if(shipRequest.size()==0)
			{	
				//退货单信息
				CreateMaterialShipRequestInfo msInfo=new CreateMaterialShipRequestInfo();
				msInfo.setSiteName(siteName);
				msInfo.setShipRequestName(shipRequestName);
				msInfo.setShipRequestState("Created");
				msInfo.setShipRequestType("Reserve");
				msInfo.setShipRequestDetailType(reasonCodeType);
				msInfo.setDeliveryType("Reserve");			
				msInfo.setSystemType(systemType);	
				msInfo.setShipPlanDate(Timestamp.valueOf(shipTime));
				msInfo.setCustomerNO("MDL");
				Map<String,Object> userColumn=new HashMap<String, Object>();
				userColumn.put("mesReceiveFlag", "N");
				userColumn.put("receiver", receiver);
				shipTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").format(new Date());
				//userColumn.put("shipPlanDate",Timestamp.valueOf(shipTime));
				if(systemType.endsWith("MESSHIP")&&returnType.equals("PDNG")){
					userColumn.put("erpFactoryName", "3179");
					msInfo.setShipRequestDetailType("521");
				}
				else{
					userColumn.put("erpFactoryName", erpFactory);
					if(systemType.endsWith("MESCALL")){
						msInfo.setShipRequestDetailType("311");
					}
					else if(systemType.endsWith("MESSHIP")&&returnType.equals("MTNG")){
						msInfo.setShipRequestDetailType("921");
					}
					else if(systemType.endsWith("MESSHIP")&&returnType.equals("OK")){
						msInfo.setShipRequestDetailType("919");
					}
				}
				msInfo.setUserColumns(userColumn);
				MaterialShipRequestServiceProxy.getMaterialShipRequestService().create(eventInfo, msInfo);								
			}
			String queryDetail="SELECT SHIPREQUESTNAME FROM MATERIALSHIPREQUESTDETAIL WHERE SITENAME=:SITENAME AND SHIPREQUESTNAME=:SHIPREQUESTNAME AND SHIPREQUESTDETAILNAME=:SHIPREQUESTDETAILNAME";
			mapShip.put("SHIPREQUESTDETAILNAME", shipRequestDetailName);			
			List<Map<String,Object>> shipDetail=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryDetail, mapShip);
			//叫料、新增退库单行项目
			if(shipDetail.size()==0 && systemType.equals("MESCALL"))
			{
				
				receiveLocation="230";
				
				CreateMaterialShipRequestDetailInfo mdInfo=new CreateMaterialShipRequestDetailInfo();
				mdInfo.setSiteName(siteName);
				mdInfo.setShipRequestName(shipRequestName);
				mdInfo.setShipRequestDetailName(shipRequestDetailName);
				mdInfo.setMaterialSpecName(materialSpecName);
				mdInfo.setRequestQuantity(Double.parseDouble(materialQuantity));
				mdInfo.setAssignedQuantity(0);			
				
				Map<String,Object> detailMap=new HashMap<String, Object>();
				detailMap.put("erpLocation",erpLocation);
				detailMap.put("erpFactoryName", erpFactory);
				detailMap.put("charge", charge);
				detailMap.put("erpReceiveLocation", receiveLocation);
				detailMap.put("line", Line);
				detailMap.put("reasonCode", reasonCode);
				detailMap.put("reasonCodeType", reasonCodeType);
				detailMap.put("remark", reMark);			
				mdInfo.setUserColumns(detailMap);				
				MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(mdInfo);
			}
			else if(shipDetail.size()==0 && systemType.equals("MESSHIP")){
				if(returnType.equals("PDNG")){
					reasonCodeType = "521";//制损入库移动类型
					erpFactory = "3179";
					if(erpLocation.endsWith("B")){
						receiveLocation = "930B";
					}
					else{
						receiveLocation="930N";//制损退库退到无价值工厂下固定库位
					}
				}
				else{
					//材损、正品退库，ERP工厂取发货的ERP工厂
					String queryERPInfoa = "SELECT ERPFACTORYNAME,ERPLOCATION FROM BS_MATERIALSHIPDEF WHERE SITENAME=:SITENAME AND MATERIALSPECNAME=:MATERIALSPECNAME";
					Map<String,String> bindMapa = new HashMap<String, String>();
					bindMapa.put("SITENAME", siteName);	
					bindMapa.put("MATERIALSPECNAME", materialSpecName);
					List<Map<String,Object>> ERPInfoa = IDMFrameServiceProxy.getSqlTemplate().queryForList(queryERPInfoa, bindMapa);
					erpFactory = ERPInfoa.get(0).get("ERPFACTORYNAME").toString();
					receiveLocation = ERPInfoa.get(0).get("ERPLOCATION").toString()+erpLocation.substring(erpLocation.length()-1);//正品退库退回到原来的发货库位
					reasonCodeType = "919";//正品退库移动类型
				}
				if(returnType.equals("MTNG")){
					reasonCodeType = "921";//材损退库移动类型
					if(erpLocation.endsWith("B")){
						receiveLocation="190B";
					}
					else {
						receiveLocation="190N";//材损库位是固定的
					}
				}
				
				CreateMaterialShipRequestDetailInfo mdInfo=new CreateMaterialShipRequestDetailInfo();
				mdInfo.setSiteName(siteName);
				mdInfo.setShipRequestName(shipRequestName);
				mdInfo.setShipRequestDetailName(shipRequestDetailName);
				mdInfo.setMaterialSpecName(materialSpecName);
				mdInfo.setRequestQuantity(Double.parseDouble(materialQuantity));
				mdInfo.setAssignedQuantity(0);			
				
				Map<String,Object> detailMap=new HashMap<String, Object>();
				detailMap.put("erpLocation",receiveLocation);//退料单receivelocation用于存产线库位
				detailMap.put("erpFactoryName", erpFactory);
				detailMap.put("charge", charge);
				detailMap.put("erpReceiveLocation", erpLocation);//退料单erpLocation用于存仓库库位
				detailMap.put("line", Line);
				detailMap.put("reasonCode", reasonCode);
				detailMap.put("returnType", returnType);
				detailMap.put("reasonCodeType", reasonCodeType);
				detailMap.put("remark", reMark);
				detailMap.put("workOrder", workOrder);
				detailMap.put("workOrderType", workOrderType);
				mdInfo.setUserColumns(detailMap);				
				MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(mdInfo);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
			throw new CustomException("MESRT-001", shipRequestName);
		}
	}
	//保存限制物料号信息
	public void saveLimitedMaterialspec(String materialSpecName,String limitedQuantiry,String receivedQuantity,String isUsed,String siteName,EventInfo eventInfo){
		
		String queryShip="SELECT MATERIALSPECNAME FROM MATERIALLIMITSPEC WHERE SITENAME=:SITENAME AND MATERIALSPECNAME=:MATERIALSPECNAME  ";
		Map<String,String> mapShip=new HashMap<String, String>();
		mapShip.put("SITENAME", siteName);
		mapShip.put("MATERIALSPECNAME", materialSpecName);
		List<Map<String,Object>> materialspec=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryShip, mapShip);
		if(materialspec.size()==0)
		{
			
			String insertSql= "INSERT INTO  MATERIALLIMITSPEC(SITENAME,MATERIALSPECNAME,LIMITEDQUANTITY,RECEIVEDQUANTITY,"
					+ "CREATETIME,CREATEUSER,FLAG)"
					+ "VALUES (?,?,?,?,?,?,?)";
			Object[] bind = new Object[] { siteName, materialSpecName,Integer.parseInt(limitedQuantiry),
					Integer.parseInt(receivedQuantity), new Date(), eventInfo.getEventUser(), isUsed};
			IDMFrameServiceProxy.getSqlTemplate().update(insertSql, bind);
		}else{
			String updateSql= "UPDATE  MATERIALLIMITSPEC SET LIMITEDQUANTITY=:limitedQuantiry,FLAG=:isUsed,CREATEUSER=:user,CREATETIME=:eventTime "
					+ "where SITENAME=:siteName and MATERIALSPECNAME=:materialSpecName ";
			Map<String, Object> bindMap = new HashMap<String, Object>();
			bindMap.put("limitedQuantiry", limitedQuantiry);
			bindMap.put("isUsed", isUsed);
			bindMap.put("user", eventInfo.getEventUser());
			bindMap.put("eventTime", new Date());
			bindMap.put("siteName", siteName);
			bindMap.put("materialSpecName", materialSpecName);
			IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
		}
	}
	//B17人员派单
public void saveWorkerInfo(String siteName,String isPo,String workerID,String invoice,EventInfo eventInfo){
	
	    Map<String, Object> bindMap = new HashMap<String, Object>();
	    String updateSql="";
		if(isPo.equals("Y"))
		{
			updateSql="update materialreceiverequest set WORKER=:workerID where SITENAME=:siteName and RECEIVEREQUESTNAME=:receiveRequestName ";

			bindMap.put("receiveRequestName", invoice);

		}else{
			updateSql="update materialshiprequest set WORKER=:workerID where SITENAME=:siteName and SHIPREQUESTNAME=:shipRequestName ";
			
			bindMap.put("shipRequestName", invoice);
			
		}
		bindMap.put("workerID", workerID);
		bindMap.put("siteName", siteName);
		IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
		//更新派单履历表
		String checkSql="select invoice from INVOICEWORKRECOED where SITENAME=:SITENAME and INVOICE=:INVOICE";
		Map<String, Object> checkMap = new HashMap<String, Object>();
		checkMap.put("SITENAME", siteName);
		checkMap.put("INVOICE", invoice);
		List<Map<String,Object>> checkResult=IDMFrameServiceProxy.getSqlTemplate().queryForList(checkSql, checkMap);
		if(checkResult.size()==0){
			String insertSql= "INSERT INTO  INVOICEWORKRECOED(SITENAME,WORKERID,INVOICE,STATE,"
					+ "STARTTIME,ENDTIME,EVENTCOMMENT,EVENTUSER,EVENTTIME)"
					+ "VALUES (?,?,?,?,?,?,?,?,?)";
			Object[] bind = new Object[] { siteName, workerID,invoice,"ON"
					, new Date(), null,eventInfo.getEventComment(),eventInfo.getEventUser(),new Date() };
			IDMFrameServiceProxy.getSqlTemplate().update(insertSql, bind);
		}{
			String upSql="update INVOICEWORKRECOED set WORKERID=:WORKERID,STARTTIME=:STARTTIME,EVENTUSER=:EVENTUSER,EVENTTIME=:EVENTTIME "
					+ " where SITENAME=:SITENAME and INVOICE=:INVOICE ";
			Map<String, Object> Map = new HashMap<String, Object>();
			Map.put("WORKERID", workerID);
			Map.put("STARTTIME", new Date());
			Map.put("EVENTUSER", eventInfo.getEventUser());
			Map.put("EVENTTIME", new Date());
			Map.put("SITENAME", siteName);
			Map.put("INVOICE", invoice);
			IDMFrameServiceProxy.getSqlTemplate().update(upSql, Map);

		}

	}


//B17删除单据前校验是否已经开始拣配，已开始拣配不允许删除
public String checkMDLShipRequest(EventInfo eventInfo, String siteName,
		String shipRequestName) throws CustomException
{
	String checkResult = "";
	String sql=" SELECT SUM(ASSIGNEDQUANTITY) AS QTY FROM MATERIALSHIPREQUESTDETAIL WHERE SITENAME=:SITENAME AND SHIPREQUESTNAME=:SHIPREQUESTNAME ";
	Map<String,String> bindMap =new HashMap<String, String>();
	bindMap.put("SITENAME", siteName);	
	bindMap.put("SHIPREQUESTNAME", shipRequestName);
	List<Map<String,Object>> sqlResult=IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
	String assignedQuantity = sqlResult.get(0).get("QTY").toString();
	if(assignedQuantity.equals("0")){
		checkResult="Y";
	}
	else{
		checkResult="This ShipRequst Has Been Assigned!! Can Not Delete!!|ERROR";
	}
	return checkResult;
}

	//B17创建FAB MES叫料单
	public void saveFABShipRequest(String shipRequestName,String shipRequestDetailName,String erpFactory,String erpLocation,EventInfo eventInfo,String reasonCodeType,String reasonCode,
			String workOrder,String materialSpecName,String  materialQuantity,String charge,String receiveLocation,String receiver,String shipTime,
		    String	workOrderType,String returnType,String returnTypeDes,String Line,String reMark,String supplierNO,String systemType,
		    String destination,String contactNo) throws CustomException
	{
		try
		{
			String siteName=System.getProperty("factory");
			
			String queryERPInfo="SELECT ERPFACTORYNAME,ERPLOCATION FROM BS_MATERIALSHIPDEF WHERE SITENAME=:SITENAME AND MATERIALSPECNAME=:MATERIALSPECNAME";
			Map<String,String> bindMap =new HashMap<String, String>();
			bindMap.put("SITENAME", siteName);	
			bindMap.put("MATERIALSPECNAME", materialSpecName);
			//FAB MES退库传输了ERPFACTORY字段
			//叫料的出货库位是维护了之后查出来的
			if(systemType.equals("MESCALL")){
				List<Map<String,Object>> ERPInfo=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryERPInfo, bindMap);
				erpFactory=ERPInfo.get(0).get("ERPFACTORYNAME").toString();
			}
			
			String queryShip="SELECT SHIPREQUESTNAME FROM MATERIALSHIPREQUEST WHERE SITENAME=:SITENAME AND SHIPREQUESTNAME=:SHIPREQUESTNAME AND SYSTEMTYPE LIKE 'MES%' ";
			Map<String,String> mapShip=new HashMap<String, String>();
			mapShip.put("SITENAME", siteName);
			mapShip.put("SHIPREQUESTNAME", shipRequestName);
			List<Map<String,Object>> shipRequest=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryShip, mapShip);
			//叫料、退库单只发一次
			if(shipRequest.size()==0)
			{	
				//退货单信息
				CreateMaterialShipRequestInfo msInfo=new CreateMaterialShipRequestInfo();
				msInfo.setSiteName(siteName);
				msInfo.setShipRequestName(shipRequestName);
				msInfo.setShipRequestState("Created");
				msInfo.setShipRequestType("Reserve");
				msInfo.setShipRequestDetailType(reasonCodeType);
				msInfo.setDeliveryType("Reserve");			
				msInfo.setSystemType(systemType);	
				msInfo.setShipPlanDate(Timestamp.valueOf(shipTime));
				msInfo.setCustomerNO("FAB");
				
				
				Map<String,Object> userColumn=new HashMap<String, Object>();
				userColumn.put("mesReceiveFlag", "N");
				//userColumn.put("receiver", "FAB");
				//userColumn.put("shipPlanDate",Timestamp.valueOf(shipTime));
				userColumn.put("erpFactoryName", erpFactory);
				userColumn.put("receiver", receiver);
				userColumn.put("destination", destination);
				userColumn.put("contactNo", contactNo);
				
				if(systemType.endsWith("MESSHIP")&&returnType.equals("PDNG")){
					msInfo.setShipRequestDetailType("521");
				}
				else{
					if(systemType.endsWith("MESCALL")){
						msInfo.setShipRequestDetailType("311");
					}
					else if(systemType.endsWith("MESSHIP")&&returnType.equals("MTNG")){
						msInfo.setShipRequestDetailType("921");
					}
					else if(systemType.endsWith("MESSHIP")&&returnType.equals("OK")){
						msInfo.setShipRequestDetailType("919");
					}
				}
				
				msInfo.setUserColumns(userColumn);
				MaterialShipRequestServiceProxy.getMaterialShipRequestService().create(eventInfo, msInfo);								
			}
			String queryDetail="SELECT SHIPREQUESTNAME FROM MATERIALSHIPREQUESTDETAIL WHERE SITENAME=:SITENAME AND SHIPREQUESTNAME=:SHIPREQUESTNAME AND SHIPREQUESTDETAILNAME=:SHIPREQUESTDETAILNAME";
			mapShip.put("SHIPREQUESTDETAILNAME", shipRequestDetailName);			
			List<Map<String,Object>> shipDetail=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryDetail, mapShip);
			//叫料、新增退库单行项目
			if(shipDetail.size()==0 && systemType.equals("MESCALL"))
			{
				String queryERPInfoA="SELECT ERPFACTORYNAME,ERPLOCATION FROM BS_MATERIALSHIPDEF WHERE SITENAME=:SITENAME AND MATERIALSPECNAME=:MATERIALSPECNAME";
				Map<String,String> bindMapA =new HashMap<String, String>();
				bindMapA.put("SITENAME", siteName);	
				bindMapA.put("MATERIALSPECNAME", materialSpecName);
				List<Map<String,Object>> ERPInfo=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryERPInfoA, bindMapA);
				erpFactory=ERPInfo.get(0).get("ERPFACTORYNAME").toString();
				erpLocation=ERPInfo.get(0).get("ERPLOCATION").toString();
				
				CreateMaterialShipRequestDetailInfo mdInfo=new CreateMaterialShipRequestDetailInfo();
				mdInfo.setSiteName(siteName);
				mdInfo.setShipRequestName(shipRequestName);
				mdInfo.setShipRequestDetailName(shipRequestDetailName);
				mdInfo.setMaterialSpecName(materialSpecName);
				mdInfo.setRequestQuantity(Double.parseDouble(materialQuantity));
				mdInfo.setAssignedQuantity(0);			
				
				Map<String,Object> detailMap=new HashMap<String, Object>();
				detailMap.put("erpLocation",erpLocation);
				detailMap.put("erpFactoryName", erpFactory);
				detailMap.put("charge", charge);
				detailMap.put("erpReceiveLocation", receiveLocation);
				detailMap.put("line", Line);
				detailMap.put("reasonCode", reasonCode);
				detailMap.put("reasonCodeType", reasonCodeType);
				detailMap.put("remark", reMark);			
				mdInfo.setUserColumns(detailMap);				
				MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(mdInfo);
			}
			else if(shipDetail.size()==0 && systemType.equals("MESSHIP")){
				
				if(returnType.equals("PDNG")){
					reasonCodeType = "521";//制损入库移动类型
					erpFactory = "3179";
					if(erpLocation.endsWith("B")){
						receiveLocation = "930B";
					}
					else{
						receiveLocation="930N";//制损退库退到无价值工厂下固定库位
					}
				}
				else{
					//材损、正品退库，ERP工厂取发货的ERP工厂
					String queryERPInfoa = "SELECT ERPFACTORYNAME,ERPLOCATION FROM BS_MATERIALSHIPDEF WHERE SITENAME=:SITENAME AND MATERIALSPECNAME=:MATERIALSPECNAME";
					Map<String,String> bindMapa = new HashMap<String, String>();
					bindMapa.put("SITENAME", siteName);	
					bindMapa.put("MATERIALSPECNAME", materialSpecName);
					List<Map<String,Object>> ERPInfoa = IDMFrameServiceProxy.getSqlTemplate().queryForList(queryERPInfoa, bindMapa);
					erpFactory = ERPInfoa.get(0).get("ERPFACTORYNAME").toString();
					receiveLocation = ERPInfoa.get(0).get("ERPLOCATION").toString()+erpLocation.substring(erpLocation.length()-1);//正品退库退回到原来的发货库位
					reasonCodeType = "919";//正品退库移动类型
				}
				if(returnType.equals("MTNG")){
					reasonCodeType = "921";//材损退库移动类型
					if(erpLocation.endsWith("B")){
						receiveLocation="190B";
					}
					else {
						receiveLocation="190N";//材损库位是固定的
					}
				}
				
				CreateMaterialShipRequestDetailInfo mdInfo=new CreateMaterialShipRequestDetailInfo();
				mdInfo.setSiteName(siteName);
				mdInfo.setShipRequestName(shipRequestName);
				mdInfo.setShipRequestDetailName(shipRequestDetailName);
				mdInfo.setMaterialSpecName(materialSpecName);
				mdInfo.setRequestQuantity(Double.parseDouble(materialQuantity));
				mdInfo.setAssignedQuantity(0);			
				
				Map<String,Object> detailMap=new HashMap<String, Object>();
				detailMap.put("erpLocation",erpLocation);//FAB退料单erpLocation用于存产线库位
				detailMap.put("erpFactoryName", erpFactory);
				detailMap.put("charge", charge);
				detailMap.put("erpReceiveLocation", receiveLocation);//FAB退料单receivelocation用于存仓库库位
				detailMap.put("line", Line);
				detailMap.put("reasonCode", reasonCode);
				detailMap.put("returnType", returnType);
				detailMap.put("reasonCodeType", reasonCodeType);
				detailMap.put("remark", reMark);			
				mdInfo.setUserColumns(detailMap);				
				MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(mdInfo);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
			throw new CustomException("MESRT-001", shipRequestName);
		}
	}

	// B20 MES获取WMS物料库存数量
	public List<Map<String, Object>> getStockedQtyListByMaterialList(List<Map<String, Object>> materialList) {
		List<Map<String, Object>> qtyList = new ArrayList<>();
		for (int i = 0; i < materialList.size(); i++) {
			String getQtySql = "SELECT sum(m.MATERIALQUANTITY) QTY FROM MATERIALPACKING m \r\n" + 
					" WHERE m.MATERIALSPECNAME = :MATERIALSPECNAME \r\n" + 
					" AND m.MATERIALTYPE = :MATERIALTYPE\r\n" + 
					" AND m.STOCKSTATE = 'Stocked'\r\n" + 
					" AND m.PACKINGSTATE = 'Released'\r\n" + 
					" AND m.MATERIALQUANTITY <> 0\r\n" + 
					" AND m.MATERIALPACKINGTYPE = 'Box'";
			log.info(">>>>>>>>>>>>>>>>>>>>>物料号" + materialList.get(i).get("MATERIALSPECNAME"));
			Map<String, Object> bindMap_GetQty = new HashMap<String, Object>();
			bindMap_GetQty.put("MATERIALSPECNAME", materialList.get(i).get("MATERIALSPECNAME"));
			bindMap_GetQty.put("MATERIALTYPE", materialList.get(i).get("MATERIALTYPE"));
			List<Map<String,Object>> result = IDMFrameServiceProxy.getSqlTemplate().queryForList(getQtySql, bindMap_GetQty);
			String materialQuantity = result.get(0).get("QTY").toString();
			Map<String, Object> bindMap = new HashMap<String, Object>();
			bindMap.put("MATERIALSPECNAME", materialList.get(i).get("MATERIALSPECNAME"));
			bindMap.put("QTY", materialQuantity);
			
			qtyList.add(bindMap);
		}
		
		return qtyList;
	}
	
	public List<Map<String, Object>> getStockedQtyByMaterialSpecName(String materialSpecName, String materialType, List<Map<String, Object>> qtyList) {
		String getQtySql = "SELECT sum(m.MATERIALQUANTITY) QTY FROM MATERIALPACKING m \r\n" + 
				" WHERE m.MATERIALSPECNAME = :MATERIALSPECNAME \r\n" + 
				" AND m.MATERIALTYPE = :MATERIALTYPE\r\n" + 
				" AND m.STOCKSTATE = 'Stocked'\r\n" + 
				" AND m.PACKINGSTATE = 'Released'\r\n" + 
				" AND m.MATERIALQUANTITY <> 0\r\n" + 
				" AND m.MATERIALPACKINGTYPE = 'Box'";
		log.info(">>>>>>>>>>>>>>>>>>>>>物料号" + materialSpecName);
		Map<String, Object> bindMap_GetQty = new HashMap<String, Object>();
		bindMap_GetQty.put("MATERIALSPECNAME", materialSpecName);
		bindMap_GetQty.put("MATERIALTYPE", materialType);
		List<Map<String,Object>> result = IDMFrameServiceProxy.getSqlTemplate().queryForList(getQtySql, bindMap_GetQty);
		log.info(">>>>>>>>>>>>>>>>>>>>>result" + result.size());
		if (result != null && !result.get(0).get("QTY").toString().isEmpty()) {
			String materialQuantity = result.get(0).get("QTY").toString();
			
			Map<String, Object> bindMap = new HashMap<String, Object>();
			bindMap.put("MATERIALSPECNAME", materialSpecName);
			bindMap.put("QTY", materialQuantity);
			
			qtyList.add(bindMap);
		}
		
		
		return qtyList;
	}
	
	public Document getStockedQtyListByMaterialSpec(Document message, String siteName) {
		
		Element bodyElement = message.getRootElement().getChild("Body");
		Element headElement = (Element)message.getRootElement().getChild("Header").clone();
		String originalsourcesubjectName = headElement.getChildText("ORIGINALSOURCESUBJECTNAME");
		Element shopNameElement = bodyElement.getChild("SHOPNAME");
		Element materialTypElement = bodyElement.getChild("MATERIALTYPE");
		
		Element materialList = bodyElement.getChild("MATERIALLIST");
		
		String shopNameString = shopNameElement.getText();
		
		Element shopName = new Element("SHOPNAME");
		shopName.setText(shopNameString);

		String materialTypeString = materialTypElement.getText();
		Element materialType = new Element("MATERIALTYPE");
		materialType.setText(materialTypeString);
		
		Element body = new Element("Body");
		
		Element materialSpecForFAB = new Element("MATERIALLIST");
		

		if (materialList != null) {
			for (Iterator<?> iterator = materialList.getChildren().iterator(); iterator.hasNext();) {
				Element materialSpecElement = (Element)iterator.next();
				Element materialElement = new Element("MATERIAL");
				String materialSpec = materialSpecElement.getContent(0).getValue();
				
				String sqlInventoryQuery = " SELECT SUM(MATERIALQUANTITY) AS QTY FROM MATERIALPACKING WHERE MATERIALSPECNAME=:MATERIALSPECNAME "
						+ " AND PACKINGSTATE='Released' "
						+ " AND STOCKSTATE = 'Stocked' "
						+ " AND PACKINGGRADE = 'OK' "
						+ " AND OQARESULTSTATE = 'END' "
						+ " AND SITENAME =:SITENAME";
				
				Map<String, String> bindMap = new HashMap<String, String>();
				bindMap.put("MATERIALSPECNAME", materialSpec);
				bindMap.put("SITENAME", siteName);
				
				List<Map<String,Object>> inventory =IDMFrameServiceProxy.getSqlTemplate()
						.queryForList(sqlInventoryQuery, bindMap);
								
				Element materialName = new Element("MATERIALNAME");
				materialName.setText(materialSpec);
				materialElement.addContent(materialName);

				Element materialQty = new Element("QTY");
				materialQty.setText(inventory.get(0).get("QTY")==null?"0":inventory.get(0).get("QTY").toString());
				materialElement.addContent(materialQty);
				
				materialSpecForFAB.addContent(materialElement);
			}
		}
		body.addContent(shopName);
		body.addContent(materialType);
		body.addContent(materialSpecForFAB);
		message.getRootElement().removeChild("Body");
		message.getRootElement().addContent(body);
		
		headElement.removeChild("MESSAGENAME");
		Element messageNameElement = new Element("MESSAGENAME");
		messageNameElement.setText("CallMaterialQtyReply");
		headElement.addContent(messageNameElement);
		
		Element subJectName = new Element("ORIGINALSOURCESUBJECTNAME");
		subJectName.setText(originalsourcesubjectName);
		headElement.addContent(subJectName);
		
		message.getRootElement().removeChild("Header");
		message.getRootElement().addContent(headElement);
		
		log.info(JdomUtils.toString(message));
		return message;
	}
	//20190710 wj	
	public void updateAssignedAct(String siteName, String invoiceNo,
			String receiveactno) {
		String sql_UndoBoxInfo = "UPDATE MATERIALPACKING SET RECEIVEACTNO=:RECEIVEACTNO WHERE SHIPREQUESTNAME=:SHIPREQUESTNAME AND STOCKSTATE='Stocked' AND PACKINGSTATE='Released' AND SITENAME=:SITENAME";
		Map<String, Object> bindMap_UndoBoxInfo = new HashMap<String, Object>();
		bindMap_UndoBoxInfo.put("SITENAME", siteName);
		bindMap_UndoBoxInfo.put("RECEIVEACTNO", receiveactno);
		bindMap_UndoBoxInfo.put("SHIPREQUESTNAME", invoiceNo);
		IDMFrameServiceProxy.getSqlTemplate()
				.update(sql_UndoBoxInfo, bindMap_UndoBoxInfo);
	}
	

	//B17修改FAB MES叫料单
		public void updateFABShipRequest(String shipRequestName,String shipRequestDetailName,String erpFactory,String erpLocation,EventInfo eventInfo,String reasonCodeType,String reasonCode,
				String workOrder,String materialSpecName,String  materialQuantity,String charge,String receiveLocation,String receiver,String shipTime,
			    String	workOrderType,String returnType,String returnTypeDes,String Line,String reMark,String supplierNO,String systemType,
			    String destination,String contactNo) throws CustomException
		{
			try
			{
				MaterialShipRequestDetailServiceImpl shipRequestDetailImpl = new MaterialShipRequestDetailServiceImpl();
				String siteName=System.getProperty("factory");
				Map<String,String> mapShip=new HashMap<String, String>();
				mapShip.put("SITENAME", siteName);
				mapShip.put("SHIPREQUESTNAME", shipRequestName);
				String queryShip="SELECT SHIPREQUESTNAME,RECEIVER,DESTINATION,CONTACTNO FROM MATERIALSHIPREQUEST WHERE SITENAME=:SITENAME AND SHIPREQUESTNAME=:SHIPREQUESTNAME AND SYSTEMTYPE LIKE 'MES%' ";
				List<Map<String,Object>> shipRequest=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryShip, mapShip);
				//receiver,destination,contactno任意一个不同时，更新shiprequest
				if((!shipRequest.get(0).get("RECEIVER").toString().equals(receiver))||
						(!shipRequest.get(0).get("DESTINATION").toString().equals(destination))||
						(!shipRequest.get(0).get("CONTACTNO").toString().equals(contactNo))) {
					MaterialShipRequest updateInfo =new MaterialShipRequest();
					MaterialShipRequestKey keyInfo = new MaterialShipRequestKey();
					keyInfo.setShipRequestName(shipRequestName);
					keyInfo.setSiteName(siteName);
					updateInfo.setContactNo(contactNo);
					updateInfo.setReceiver(receiver);
					updateInfo.setDestination(destination);
					updateInfo.setKey(keyInfo);
					
					MaterialShipRequestServiceProxy.getMaterialShipRequestService().update(updateInfo);
				}
				String queryDetail="SELECT SHIPREQUESTNAME,ASSIGNEDQUANTITY FROM MATERIALSHIPREQUESTDETAIL WHERE SITENAME=:SITENAME AND SHIPREQUESTNAME=:SHIPREQUESTNAME AND SHIPREQUESTDETAILNAME=:SHIPREQUESTDETAILNAME";
				mapShip.put("SHIPREQUESTDETAILNAME", shipRequestDetailName);			
				List<Map<String,Object>> shipDetail=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryDetail, mapShip);
				//叫料、新增退库单行项目
				if(shipDetail.size()>0 && systemType.equals("MESCALL"))
				{
					String assignedQuantity = shipDetail.get(0).get("ASSIGNEDQUANTITY").toString();
					if(!assignedQuantity.equals("0"))
						throw new CustomException("INT-0007", shipRequestName);
					String queryERPInfo="SELECT ERPFACTORYNAME,ERPLOCATION FROM BS_MATERIALSHIPDEF WHERE SITENAME=:SITENAME AND MATERIALSPECNAME=:MATERIALSPECNAME";
					Map<String,String> bindMap =new HashMap<String, String>();
					bindMap.put("SITENAME", siteName);	
					bindMap.put("MATERIALSPECNAME", materialSpecName);
					List<Map<String,Object>> ERPInfo=IDMFrameServiceProxy.getSqlTemplate().queryForList(queryERPInfo, bindMap);
					erpFactory=ERPInfo.get(0).get("ERPFACTORYNAME").toString();
					erpLocation=ERPInfo.get(0).get("ERPLOCATION").toString();
					
					shipRequestDetailImpl.modify1(eventInfo, siteName,
							shipRequestName, shipRequestDetailName,
							materialSpecName, "", "",
							materialQuantity, assignedQuantity, erpLocation,
							erpFactory, charge);
					
				}
				else if(shipDetail.size()>0 && systemType.equals("MESSHIP")){
					String assignedQuantity = shipDetail.get(0).get("ASSIGNEDQUANTITY").toString();
					if(!assignedQuantity.equals("0"))
						throw new CustomException("INT-0007", shipRequestName);
					shipRequestDetailImpl.modify1(eventInfo, siteName,
							shipRequestName, shipRequestDetailName,
							materialSpecName, "", "",
							materialQuantity, assignedQuantity, erpLocation,
							erpFactory, charge);
				}
			}catch(Exception e)
			{
				e.printStackTrace();
				throw new CustomException("FABMES-002", shipRequestName);
			}
		}
		
		//B17FAB接收材料
		public void FABReceiveMaterialReport(String shipRequestName,String shipRequestDetailName,String materialSpecName,String materialQuantity)
				 throws CustomException{
			try{
				String siteName = System.getProperty("factory");
				String sqlUpdate = " UPDATE MATERIALSHIPREQUESTDETAIL "
						+ " SET REMARK=:REMARK "
						+ " WHERE SITENAME=:SITENAME "
						+ " AND SHIPREQUESTNAME=:SHIPREQUESTNAME "
						+ " AND SHIPREQUESTDETAILNAME=:SHIPREQUESTDETAILNAME ";
				Map<String, String> bindMap = new HashMap<String, String>();
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipRequestName);
				bindMap.put("SHIPREQUESTDETAILNAME", shipRequestDetailName);
				bindMap.put("REMARK", "FABReceived");
				IDMFrameServiceProxy.getSqlTemplate().update(sqlUpdate, bindMap);
				
			}
			catch(Exception e)
			{
				e.printStackTrace();
				throw new CustomException(e);
			}
			
		}
		
		/**
		 * rejectMESbyWarehouse  wsq 2019/11/11
		 * @param eventInfo
		 * @param siteName
		 * @param shipRequestName
		 * @param reMark
		 * @throws CustomException
		 */
		public void rejectMESbyWarehouse(EventInfo eventInfo,String siteName,String shipRequestName,String reMark)throws CustomException{
			try{
				
				String[] shipRequestNameList = shipRequestName.split("\\|");
				for(int i=0;i<shipRequestNameList.length;i++){
					MaterialShipRequestKey key = new MaterialShipRequestKey();

					key.setSiteName(siteName);
					key.setShipRequestName(shipRequestNameList[i].toString());

					Map<String, Object> bindMap = new HashMap<String, Object>();

					bindMap.put("shipRequestState", "Rejecting");
					bindMap.put("customsDeclare", reMark);

					MaterialShipRequestSetEventInfo setEventInfo = new MaterialShipRequestSetEventInfo();
					setEventInfo.setUserColumns(bindMap);

					eventInfo.setEventName("RejectMESbyWarehouse");

					MaterialShipRequestServiceProxy.getMaterialShipRequestService()
							.setEvent(key, eventInfo, setEventInfo);
				}
				
			}
			catch(Exception e)
			{
				e.printStackTrace();
				throw new CustomException(e);
			}
		}
		
		/*
		 * Name : CreateMaterialShipOEMByLabel Desc :
		 * CreateMaterialShipOEMByLabel
		 * 创建物料抬头文本信息
		 */

		public static void CreateMaterialShipPMSByLabel(EventInfo eventInfo,
				String siteName, String shipRequestName, String receiveActNo,
				String actType) throws CustomException, ParseException {
			String sql2 = "SELECT * FROM materialpacking "
					+ "WHERE sitename = :sitename AND SHIPREQUESTNAME = :SHIPREQUESTNAME  and receiveActNo=:receiveActNo and erpstate='ERPSUCCESS' ";

			Map<String, Object> bindMap3 = new HashMap<String, Object>();
			bindMap3.put("sitename", siteName);
			bindMap3.put("SHIPREQUESTNAME", shipRequestName);
			bindMap3.put("receiveActNo", receiveActNo);//抬头文本

			List<Map<String, Object>> queryResult2 = IDMFrameServiceProxy
					.getSqlTemplate()
					.queryForList(sql2, bindMap3);
			if (queryResult2.size() > 0)
			{
				throw new  CustomException("AccoutTimeOut","","");
			}
			
			String sql1 = "SELECT * FROM materialreceiveact "
					+ "WHERE sitename = :sitename AND receiveRequestName = :receiveRequestName  and receiveActNo=:receiveActNo ";

			Map<String, Object> bindMap1 = new HashMap<String, Object>();
			bindMap1.put("sitename", siteName);
			bindMap1.put("receiveRequestName", shipRequestName);
			bindMap1.put("receiveActNo", receiveActNo);

			List<Map<String, Object>> queryResult1 = IDMFrameServiceProxy
					.getSqlTemplate()
					.queryForList(sql1, bindMap1);

			if (queryResult1.size() == 0) {
				CreateReceiveRequestBatchInfo createActBatchInfo = new CreateReceiveRequestBatchInfo();
				List<CreateReceiveRequestBatchInfo> createReceiveRequestBatchInfo = new ArrayList<CreateReceiveRequestBatchInfo>();

				Map<String, Object> bindMap2 = new HashMap<String, Object>();

				createActBatchInfo.setSiteName(siteName);
				createActBatchInfo.setReceiveRequestName(shipRequestName);
				createActBatchInfo.setReceiveactNO(receiveActNo);
				createActBatchInfo.setReceiveActState("Created");
				bindMap2.put("actType", actType);

				createActBatchInfo.setUserColumns(bindMap2);

				createReceiveRequestBatchInfo.add(createActBatchInfo);

				MaterialReceiveRequestActServiceProxy
						.getReceiveRequestBatchService().create(eventInfo,
								createReceiveRequestBatchInfo);
			}
			
		}

}
