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

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

import com.cim.idm.wmspackage.materialreceiverequestactdetail.management.MaterialReceiveRequestActDetailService;
import org.jdom.Element;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.framework.util.time.TimeUtils;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import com.cim.idm.mwmsextend.generic.util.CommonValidation;
import com.cim.idm.wmsextend.generic.util.ConvertUtil;
import com.cim.idm.wmspackage.invoice.MaterialReceiveRequestServiceProxy;
import com.cim.idm.wmspackage.invoice.management.data.MaterialReceiveRequestKey;
import com.cim.idm.wmspackage.invoice.management.info.CreateReceiveRequestInfo;
import com.cim.idm.wmspackage.invoice.management.info.RemoveReceiveRequestInfo;
import com.cim.idm.wmspackage.invoice.management.info.SetEventInfo;
import com.cim.idm.wmspackage.materialreceiverequestact.MaterialReceiveRequestActServiceProxy;
import com.cim.idm.wmspackage.materialreceiverequestact.management.info.CreateReceiveRequestBatchInfo;
import com.cim.idm.wmspackage.materialreceiverequestactdetail.MaterialReceiveRequestActDetailServiceProxy;
import com.cim.idm.wmspackage.materialreceiverequestactdetail.management.info.ActDetailIncreaseInfo;
import com.cim.idm.wmspackage.materialreceiverequestactdetail.management.info.CreateActDetailInfo;
import com.cim.idm.wmspackage.materialreceivetactdetail.management.data.MaterialReceiveActDetailKey;
import com.cim.idm.wmspackage.materialshiprequestdetail.MaterialShipRequestDetailServiceProxy;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.data.MaterialShipRequestDetailKey;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.info.MakeShipDetailDecreaseAssignedInfo;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.info.MakeShipDetailIncreaseAssignedInfo;
import com.cim.idm.wmspackage.materialshiprequesttooem.MaterialShipRequestToOEMServiceProxy;
import com.cim.idm.wmspackage.materialshiprequesttooem.management.data.MaterialShipRequestToOEMKey;
import com.cim.idm.wmspackage.materialshiprequesttooem.management.info.MakeOEMDecreaseAssignedInfo;
import com.cim.idm.wmspackage.name.NameServiceProxy;
import com.cim.idm.wmspackage.receiverequestbatch.management.data.MaterialReceiveAct;
import com.cim.idm.wmspackage.receiverequestbatch.management.data.MaterialReceiveActKey;
import com.cim.idm.wmspackage.receiverequestdetail.ReceiveRequestDetailServiceProxy;
import com.cim.idm.wmspackage.receiverequestdetail.management.data.MaterialReceiveRequestDetailKey;
import com.cim.idm.wmspackage.receiverequestdetail.management.info.MakeDetailDecreaseReceivedInfo;
import com.cim.idm.wmspackage.receiverequestdetail.management.info.MakeDetailIncreaseReceivedInfo;
import com.cim.idm.wmspackage.supplier.management.data.Supplier;
import com.cim.idm.wmspackage.supplier.management.data.SupplierKey;
import com.cim.idm.wmspackage.supplier.management.impl.SupplierServiceImpl;

public class MaterialReceiveRequestServiceImpl {

	public void create(EventInfo eventInfo, String siteName,
			String receiveRequestName, String purchaseOrderGroup,
			String receiveRequestState, String supplierNO, String systemType,
			String saveReason, String respPerson, String respDepart,
			String solveTime,String passState) {
		MaterialReceiveRequestKey key = new MaterialReceiveRequestKey();
		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);

		CreateReceiveRequestInfo createInfo = new CreateReceiveRequestInfo();

		createInfo.setSiteName(siteName);
		createInfo.setReceiveRequestName(receiveRequestName);
		createInfo.setPurchaseOrderGroup(purchaseOrderGroup);
		createInfo.setReceiveRequestState(receiveRequestState);
		createInfo.setSupplierNO(supplierNO);
		createInfo.setSystemType(systemType);
		Map<String, Object> userColumn = new HashMap<String, Object>();
		userColumn.put("saveReason", saveReason);
		userColumn.put("respPerson", respPerson);
		userColumn.put("respDepart", respDepart);
		userColumn.put("passState", passState);
		if (solveTime == "") {

		} else {
			Timestamp sTime = ConvertUtil.convertToTimeStamp(solveTime);
			userColumn.put("solveTime", sTime);
		}

		createInfo.setUserColumns(userColumn);
		eventInfo.setEventName("Create");

		MaterialReceiveRequestServiceProxy.getMaterialReceiveRequestService()
				.create(eventInfo, createInfo);
		/*if(passState.equals("Completed"))
		{
			com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.GetPOPIReturn("NormalStockIn", eventInfo.getEventUser(), receiveRequestName);
			com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.GetPOPIReturn("PONormalStockIn", eventInfo.getEventUser(), receiveRequestName);
		}*/
	}
	
	/**
	 * 2024.09.26 新增
	 */
	public void create(EventInfo eventInfo, String siteName,
            String receiveRequestName, String purchaseOrderGroup,
            String receiveRequestState, String supplierNO, String systemType,
            String saveReason, String respPerson, String respDepart,
            String solveTime, String passState, String bedat, String receiveRequestType) {
        MaterialReceiveRequestKey key = new MaterialReceiveRequestKey();
        key.setSiteName(siteName);
        key.setReceiveRequestName(receiveRequestName);

        CreateReceiveRequestInfo createInfo = new CreateReceiveRequestInfo();

        createInfo.setSiteName(siteName);
        createInfo.setReceiveRequestName(receiveRequestName);
        createInfo.setPurchaseOrderGroup(purchaseOrderGroup);
        createInfo.setReceiveRequestState(receiveRequestState);
        createInfo.setSupplierNO(supplierNO);
        createInfo.setSystemType(systemType);
        createInfo.setReceiveRequestType(receiveRequestType);
        Map<String, Object> userColumn = new HashMap<String, Object>();
        userColumn.put("saveReason", saveReason);
        userColumn.put("respPerson", respPerson);
        userColumn.put("respDepart", respDepart);
        userColumn.put("passState", passState);
        userColumn.put("bedat", bedat);
        if (solveTime == "") {

        } else {
            Timestamp sTime = ConvertUtil.convertToTimeStamp(solveTime);
            userColumn.put("solveTime", sTime);
        }

        createInfo.setUserColumns(userColumn);
        eventInfo.setEventName("Create");

        MaterialReceiveRequestServiceProxy.getMaterialReceiveRequestService()
                .create(eventInfo, createInfo);
        /*
         * if(passState.equals("Completed"))
         * {
         * com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.
         * GetPOPIReturn("NormalStockIn", eventInfo.getEventUser(), receiveRequestName);
         * com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.
         * GetPOPIReturn("PONormalStockIn", eventInfo.getEventUser(),
         * receiveRequestName);
         * }
         */
    }

    public String createByPDA(EventInfo eventInfo, String siteName,
            String receiveRequestName, String purchaseOrderGroup,
            String receiveRequestState, String supplierNO, String systemType,
            String saveReason, String respPerson, String respDepart,
            String solveTime, String passState, String bedat, String receiveRequestType) throws Exception {

        String invoiceNo = "";
        try {

            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            Date date = new Date(System.currentTimeMillis());
            String dateStr = formatter.format(date);
            invoiceNo = "B4" + dateStr + "_" + eventInfo.getEventUser();
            //log.info("INVOICENO: " + invoiceNo);

            MaterialReceiveRequestServiceImpl receiveRequestImpl = new MaterialReceiveRequestServiceImpl();
            receiveRequestImpl.create(eventInfo, siteName,
                    invoiceNo, purchaseOrderGroup,
                    receiveRequestState, "", "PDA", "", "", "", "", passState, bedat, receiveRequestType);
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return invoiceNo;
    }
	

	public void modify(EventInfo eventInfo, String siteName,
			String receiveRequestName, String purchaseOrderGroup,
			String receiveRequestState, String supplierNO, String systemType,
			String saveReason, String respPerson, String respDepart,
			String solveTime) {
		SetEventInfo info = new SetEventInfo();

		MaterialReceiveRequestKey key = new MaterialReceiveRequestKey();

		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);

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

		bindMap.put("purchaseOrderGroup", purchaseOrderGroup);
		bindMap.put("receiveRequestState", receiveRequestState);
		bindMap.put("supplierNO", supplierNO);
		bindMap.put("systemType", systemType);
		bindMap.put("saveReason", saveReason);
		bindMap.put("respPerson", respPerson);
		bindMap.put("respDepart", respDepart);
		//bindMap.put("passState", passState);
		if (solveTime == "") {

		} else {
			Timestamp sTime = ConvertUtil.convertToTimeStamp(solveTime);
			bindMap.put("solveTime", sTime);
		}
		info.setUserColumns(bindMap);
		eventInfo.setEventName("Modify");
		MaterialReceiveRequestServiceProxy.getMaterialReceiveRequestService()
				.setEvent(key, eventInfo, info);
		/*if(passState.equals("Completed"))
		{
			com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.GetPOPIReturn("NormalStockIn", eventInfo.getEventUser(), receiveRequestName);
			com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.GetPOPIReturn("PONormalStockIn", eventInfo.getEventUser(), receiveRequestName);
		}*/
	}
	
	public void modify(EventInfo eventInfo, String siteName,
			String receiveRequestName, String purchaseOrderGroup,
			String receiveRequestState, String supplierNO, String systemType,
			String saveReason, String respPerson, String respDepart,
			String solveTime,String bedat) {
		SetEventInfo info = new SetEventInfo();

		MaterialReceiveRequestKey key = new MaterialReceiveRequestKey();

		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);

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

		bindMap.put("purchaseOrderGroup", purchaseOrderGroup);
		bindMap.put("receiveRequestState", receiveRequestState);
		bindMap.put("supplierNO", supplierNO);
		bindMap.put("systemType", systemType);
		bindMap.put("saveReason", saveReason);
		bindMap.put("respPerson", respPerson);
		bindMap.put("respDepart", respDepart);
		//bindMap.put("passState", passState);
		bindMap.put("bedat", bedat);
		
		if (solveTime == "") {

		} else {
			Timestamp sTime = ConvertUtil.convertToTimeStamp(solveTime);
			bindMap.put("solveTime", sTime);
		}
		info.setUserColumns(bindMap);
		eventInfo.setEventName("Modify");
		MaterialReceiveRequestServiceProxy.getMaterialReceiveRequestService()
				.setEvent(key, eventInfo, info);
		/*if(passState.equals("Completed"))
		{
			com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.GetPOPIReturn("NormalStockIn", eventInfo.getEventUser(), receiveRequestName);
			com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.GetPOPIReturn("PONormalStockIn", eventInfo.getEventUser(), receiveRequestName);
		}*/
	}
	public void remove(EventInfo eventInfo, String siteName,
			String receiveRequestName) {

		MaterialReceiveRequestKey key = new MaterialReceiveRequestKey();

		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);

		RemoveReceiveRequestInfo info = new RemoveReceiveRequestInfo();

		eventInfo.setEventName("Remove");

		MaterialReceiveRequestServiceProxy.getMaterialReceiveRequestService()
				.remove(key, eventInfo, info);
	}

	/*
	 * Name : CreateMaterialReceiveRequestAct Desc :
	 * CreateMaterialReceiveRequestAct Author : boe cim dev. Date : 2016.02.17
	 */

	public void CreateMaterialReceiveRequestAct(EventInfo eventInfo,
			String siteName, String receiveRequestName,
			String ReceiveRequestDetailName, String makeDate,
			String receiveActNo, String actType, String receiveQuantity,
			String subMaterialSpecName) throws CustomException, ParseException {
		String actType1 = "";
		String newReceiveRequestDetailName = "";
		if (actType.equals("POOEM")) {
			newReceiveRequestDetailName = ReceiveRequestDetailName + "_"
					+ subMaterialSpecName;
		}
		// receiveRequestName="lixiaolongtest1";
		// ReceiveRequestDetailName="lixiaolongtestDetail";
		// makeDate="2016-02-17 20:17:59";
		// receiveActNo="testabc1";

		/*
		 * SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 * Timestamp date = (Timestamp) sdf.parse(makeDate);
		 */

		double ReceiveQuantity = Double.parseDouble(receiveQuantity);

		// receiveActInfo
		MaterialReceiveActDetailKey materialReceiveActDetailKey = new MaterialReceiveActDetailKey();
		// MaterialReceiveActDetail materialReceiveActDetail = new
		// MaterialReceiveActDetail();
		CreateActDetailInfo createActDetailInfo = new CreateActDetailInfo();
		//
		materialReceiveActDetailKey.setSiteName(siteName);
		materialReceiveActDetailKey.setReceiveRequestName(receiveRequestName);
		if (actType.equals("POOEM")) {
			materialReceiveActDetailKey
					.setReceiveRequestDetailName(newReceiveRequestDetailName);
		} else {
			materialReceiveActDetailKey
					.setReceiveRequestDetailName(ReceiveRequestDetailName);
		}

		materialReceiveActDetailKey.setReceiveActNO(receiveActNo);
		materialReceiveActDetailKey.setMakeDate(Timestamp.valueOf(makeDate));

		// materialReceiveActDetail=
		// MaterialReceiveRequestActDetailServiceProxy.getMaterialReceiveRequestActDetailService().selectByKey(materialReceiveActDetailKey);

		String sql = "SELECT * FROM materialreceiveactdetail "
				+ "WHERE sitename = :sitename AND receiveRequestName = :receiveRequestName and ReceiveRequestDetailName=:ReceiveRequestDetailName and receiveActNo=:receiveActNo"
				+ " and makeDate=:makeDate ";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("sitename", siteName);
		bindMap.put("receiveRequestName", receiveRequestName);
		if (actType.equals("POOEM")) {
			bindMap.put("ReceiveRequestDetailName", newReceiveRequestDetailName);
		} else {
			bindMap.put("ReceiveRequestDetailName", ReceiveRequestDetailName);
		}
		bindMap.put("receiveActNo", receiveActNo);
		bindMap.put("makeDate", Timestamp.valueOf(makeDate));

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

		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", receiveRequestName);
		bindMap1.put("receiveActNo", receiveActNo);

		List<Map<String, Object>> queryResult1 = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql1, bindMap1);
		if (queryResult1.size() != 0) {
			actType1 = queryResult1.get(0).get("ACTTYPE").toString();
		}
		if (queryResult1.size() == 0) {
			CreateReceiveRequestBatchInfo createActBatchInfo = new CreateReceiveRequestBatchInfo();
			List<CreateReceiveRequestBatchInfo> createReceiveRequestBatchInfo = new ArrayList<CreateReceiveRequestBatchInfo>();

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

			List<String> materialReceiveRequestAct;
			// String quantity="10";
			String product = "qwe";
			String grade = "w";
			String line = "q";
			String revisionCode = "r";

			createActBatchInfo.setSiteName(siteName);
			createActBatchInfo.setReceiveRequestName(receiveRequestName);
			createActBatchInfo.setReceiveactNO(receiveActNo);
			createActBatchInfo.setReceiveActState("Created");

			bindMap2.put("actType", actType);
			// bindMap2.put("receiveActState", );
			createActBatchInfo.setUserColumns(bindMap2);

			createReceiveRequestBatchInfo.add(createActBatchInfo);

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

		if (queryResult.size() == 0) {
			createActDetailInfo.setReceiveActNO(receiveActNo);
			createActDetailInfo.setReceiveRequestName(receiveRequestName);
			if (actType.equals("POOEM")) {
				createActDetailInfo
						.setReceiveRequestDetailName(newReceiveRequestDetailName);
			} else {
				createActDetailInfo
						.setReceiveRequestDetailName(ReceiveRequestDetailName);
			}
			createActDetailInfo.setSiteName(siteName);

			// Date date1 = new Date();//获取当前时间
			// SimpleDateFormat sdf = new
			// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// String str = sdf.format(date1);//时间存储为字符串
			// System.out.println(str);
			createActDetailInfo.setMakeDate(Timestamp.valueOf(makeDate));
			createActDetailInfo.setReceiveQuantity(ReceiveQuantity);

			Map<String, Object> bindMap3 = new HashMap<String, Object>();
			bindMap3.put("receiveActState", "Created");

			createActDetailInfo.setUserColumns(bindMap3);

			MaterialReceiveRequestActDetailServiceProxy
					.getMaterialReceiveRequestActDetailService().create(
							eventInfo, createActDetailInfo);
		} else {
			ActDetailIncreaseInfo actDetailIncreaseInfo = new ActDetailIncreaseInfo();
			actDetailIncreaseInfo.setReceiveQuantity(ReceiveQuantity);

			MaterialReceiveRequestActDetailServiceProxy
					.getMaterialReceiveRequestActDetailService()
					.increaseReceivedQuantity(materialReceiveActDetailKey,
							eventInfo, actDetailIncreaseInfo);
		}

		if (actType.equals("NoPO") || actType1.equals("NoPO")
				|| actType.equals("Normal") || actType1.equals("Normal")
				|| actType.equals("NormalIDO") || actType1.equals("NormalIDO")) {
			if(!siteName.equals("B7")){
				MakeDetailIncreaseReceivedInfo makeDetailIncreaseReceivedInfo = new MakeDetailIncreaseReceivedInfo();
				MaterialReceiveRequestDetailKey materialReceiveRequestDetailKey = new MaterialReceiveRequestDetailKey();
				materialReceiveRequestDetailKey
						.setReceiveRequestName(receiveRequestName);
				materialReceiveRequestDetailKey.setSiteName(siteName);
				materialReceiveRequestDetailKey
						.setReceiveRequestDetailName(ReceiveRequestDetailName);

				makeDetailIncreaseReceivedInfo.setIncreaseQuantity(ReceiveQuantity);

				ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService()
						.increaseReceivedQuantity(materialReceiveRequestDetailKey,
								eventInfo, makeDetailIncreaseReceivedInfo);
				
			}else{
				//Double型计算精度缺失问题
				Map<String, Object> bindMapF = new HashMap<String, Object>();
				Map<String, Object> bindMapFU = new HashMap<String, Object>();
				String receivedQuantiryF="";
				bindMapF.put("receiveRequestName",receiveRequestName);
				bindMapF.put("ReceiveRequestDetailName", ReceiveRequestDetailName);
				bindMapF.put("sitename", siteName);
				String sqlF="SELECT  RECEIVEDQUANTITY FROM MATERIALRECEIVEREQUESTDETAIL WHERE SITENAME=:sitename AND RECEIVEREQUESTNAME=:receiveRequestName AND RECEIVEREQUESTDETAILNAME=:ReceiveRequestDetailName";
				String sqlFU="UPDATE MATERIALRECEIVEREQUESTDETAIL SET RECEIVEDQUANTITY=:receivedQuantity WHERE SITENAME=:sitename AND RECEIVEREQUESTNAME=:receiveRequestName AND RECEIVEREQUESTDETAILNAME=:ReceiveRequestDetailName";
				List<Map<String, Object>> queryResultF = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlF, bindMapF);
				if(queryResultF.size()!=0){
					 receivedQuantiryF=queryResultF.get(0).get("RECEIVEDQUANTITY").toString();
				}
				BigDecimal receivequantity=new BigDecimal(String.valueOf(ReceiveQuantity));
				BigDecimal receivedQuantiry=new BigDecimal(receivedQuantiryF);
				bindMapFU.put("receivedQuantity", receivequantity.add(receivedQuantiry).doubleValue());
				bindMapFU.put("sitename", siteName);
				bindMapFU.put("receiveRequestName", receiveRequestName);
				bindMapFU.put("ReceiveRequestDetailName", ReceiveRequestDetailName);
				IDMFrameServiceProxy.getSqlTemplate().update(sqlFU, bindMapFU);
			}
			

		}
		// else if(actType.equals("POOEM"))
		// {
		// MakeOEMIncreaseInfo makeOEMIncreaseInfo =new MakeOEMIncreaseInfo();
		// MaterialShipRequestToOEMKey materialShipRequestToOEMKey =new
		// MaterialShipRequestToOEMKey();
		// materialShipRequestToOEMKey.setReceiveRequestName(receiveRequestName);
		// materialShipRequestToOEMKey.setSiteName(siteName);
		// materialShipRequestToOEMKey.setReceiveRequestDetailName(ReceiveRequestDetailName);
		//
		// makeOEMIncreaseInfo.setIncreaseQuantity(ReceiveQuantity);
		//
		// MaterialShipRequestToOEMServiceProxy.getMaterialShipRequestToOEMService().in(materialShipRequestToOEMKey,
		// eventInfo, makeOEMIncreaseInfo);
		// }
		else if (actType.equals("POOEM") || actType1.equals("POOEM")) {

			MakeOEMDecreaseAssignedInfo makeOEMDecreaseAssignedInfo = new MakeOEMDecreaseAssignedInfo();
			MaterialShipRequestToOEMKey materialShipRequestToOEMKey = new MaterialShipRequestToOEMKey();
			materialShipRequestToOEMKey
					.setReceiveRequestName(receiveRequestName);
			materialShipRequestToOEMKey.setSiteName(siteName);
			materialShipRequestToOEMKey
					.setReceiveRequestDetailName(ReceiveRequestDetailName);
			materialShipRequestToOEMKey
					.setSubMaterialSpecName(subMaterialSpecName);
			makeOEMDecreaseAssignedInfo.setAssignedQuantity(ReceiveQuantity);

			MaterialShipRequestToOEMServiceProxy
					.getMaterialShipRequestToOEMService()
					.decreaseAssignedQuantity(materialShipRequestToOEMKey,
							eventInfo, makeOEMDecreaseAssignedInfo);
		}
		// else if(!actType.equals("POOEM")||!actType1.equals("POOEM"))
		else {
			if(!siteName.equals("B7")){
				MakeShipDetailIncreaseAssignedInfo makeShipDetailIncreaseAssignedInfo = new MakeShipDetailIncreaseAssignedInfo();
				MaterialShipRequestDetailKey materialShipRequestDetailKey = new MaterialShipRequestDetailKey();
				materialShipRequestDetailKey.setShipRequestName(receiveRequestName);
				materialShipRequestDetailKey.setSiteName(siteName);
				materialShipRequestDetailKey
						.setShipRequestDetailName(ReceiveRequestDetailName);

				makeShipDetailIncreaseAssignedInfo
						.setAssignedQuantity(ReceiveQuantity);

				MaterialShipRequestDetailServiceProxy
						.getMaterialShipRequestDetailService()
						.increaseAssignedQuantity(materialShipRequestDetailKey,
								makeShipDetailIncreaseAssignedInfo);
			}else{
				//Double型计算精度缺失问题
				Map<String, Object> bindMapF = new HashMap<String, Object>();
				Map<String, Object> bindMapFU = new HashMap<String, Object>();
				String receivedQuantiryF="";
				bindMapF.put("shipRequestName",receiveRequestName);
				bindMapF.put("shipRequestDetailName", ReceiveRequestDetailName);
				bindMapF.put("sitename", siteName);
				String sqlF="SELECT  ASSIGNEDQUANTITY FROM MATERIALSHIPREQUESTDETAIL WHERE SITENAME=:sitename AND SHIPREQUESTNAME=:shipRequestName AND SHIPREQUESTDETAILNAME=:shipRequestDetailName";
				String sqlFU="UPDATE MATERIALSHIPREQUESTDETAIL SET ASSIGNEDQUANTITY=:receivedQuantity WHERE SITENAME=:sitename AND SHIPREQUESTNAME=:shipRequestName AND SHIPREQUESTDETAILNAME=:shipRequestDetailName";
				List<Map<String, Object>> queryResultF = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlF, bindMapF);
				if(queryResultF.size()!=0){
					 receivedQuantiryF=queryResultF.get(0).get("RECEIVEDQUANTITY").toString();
				}
				BigDecimal receivequantity=new BigDecimal(String.valueOf(ReceiveQuantity));
				BigDecimal receivedQuantiry=new BigDecimal(receivedQuantiryF);
				bindMapFU.put("receivedQuantity", receivequantity.add(receivedQuantiry).doubleValue());
				bindMapFU.put("sitename", siteName);
				bindMapFU.put("shipRequestName", receiveRequestName);
				bindMapFU.put("shipRequestDetailName", ReceiveRequestDetailName);
				IDMFrameServiceProxy.getSqlTemplate().update(sqlFU, bindMapFU);
			}	
		}
	}

	public void SetCustomFormInfo(EventInfo eventInfo,String siteName, String receiveRequestName,String ReceiveRequestDetailName, 
			String taxId, String jiezhuandanhao,String guandanhao,String erpLocation,String OMENG) throws Exception {
		
		try {
			String sql_insert = "UPDATE MATERIALRECEIVEREQUESTDETAIL SET ZSQBH=:ZSQBH , ZFPHM=:ZFPHM,ZMXDH=:ZMXDH,ERPLOCATION=:ERPLOCATION,OMENG=:OMENG  WHERE RECEIVEREQUESTNAME=:RECEIVENAME AND RECEIVEREQUESTDETAILNAME=:RECEIVEREQUESTDETAILNAME";
			Map<String, Object> bindMap_insert = new HashMap<String, Object>();
				bindMap_insert.put("ZSQBH",jiezhuandanhao);
				bindMap_insert.put("ZFPHM",taxId);
				bindMap_insert.put("ZMXDH",guandanhao);
				bindMap_insert.put("RECEIVENAME",receiveRequestName);
				bindMap_insert.put("RECEIVEREQUESTDETAILNAME",ReceiveRequestDetailName);
				bindMap_insert.put("ERPLOCATION",erpLocation);
				bindMap_insert.put("OMENG",OMENG);
				IDMFrameServiceProxy.getSqlTemplate()
						
						.update(sql_insert, bindMap_insert);
		} catch (Exception e) {
			throw e;
		}

	}

	public void CreateMaterialReceiveRequestActByInner(EventInfo eventInfo,
			String siteName, String receiveRequestName,
			String ReceiveRequestDetailName, String makeDate,
			String receiveActNo, String actType, String receiveQuantity,
			String subMaterialSpecName, String innerRequest,
			String innerRequestDetail) throws CustomException, ParseException {
		String actType1 = "";
		String newReceiveRequestDetailName = "";

		double ReceiveQuantity = Double.parseDouble(receiveQuantity);

		MaterialReceiveActDetailKey materialReceiveActDetailKey = new MaterialReceiveActDetailKey();
		CreateActDetailInfo createActDetailInfo = new CreateActDetailInfo();
		materialReceiveActDetailKey.setSiteName(siteName);
		materialReceiveActDetailKey.setReceiveRequestName(receiveRequestName);
		materialReceiveActDetailKey
				.setReceiveRequestDetailName(ReceiveRequestDetailName);
		materialReceiveActDetailKey.setReceiveActNO(receiveActNo);
		materialReceiveActDetailKey.setMakeDate(Timestamp.valueOf(makeDate));
		String sql = "SELECT * FROM materialreceiveactdetail "
				+ "WHERE sitename=:sitename and innerrequestname =:innerrequestname AND innerrequestdetailname =:innerrequestdetailname  and receiveActNo=:receiveActNo"
				+ " and makeDate=:makeDate and receiveRequestName=:receiveRequestName and receiveRequestDetailName=:receiveRequestDetailName ";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("sitename", siteName);
		bindMap.put("innerrequestname", innerRequest);
		bindMap.put("innerrequestdetailname", innerRequestDetail);
		bindMap.put("receiveRequestName", receiveRequestName);
		bindMap.put("receiveRequestDetailName", ReceiveRequestDetailName);
		bindMap.put("receiveActNo", receiveActNo);
		bindMap.put("makeDate", Timestamp.valueOf(makeDate));

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

		String sql1 = "SELECT * FROM materialreceiveact "
				+ "WHERE sitename =:sitename AND innerrequestname =:innerrequestname  and receiveActNo=:receiveActNo ";
		Map<String, Object> bindMap1 = new HashMap<String, Object>();
		bindMap1.put("sitename", siteName);
		bindMap1.put("innerrequestname", innerRequest);
		bindMap1.put("receiveActNo", receiveActNo);

		List<Map<String, Object>> queryResult1 = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql1, bindMap1);
		if (queryResult1.size() != 0) {
			actType1 = queryResult1.get(0).get("ACTTYPE").toString();
		}
		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(receiveRequestName);
			createActBatchInfo.setReceiveactNO(receiveActNo);
			createActBatchInfo.setReceiveActState("Created");
			bindMap2.put("actType", actType);	
			bindMap2.put("innerRequestName", innerRequest);			
			createActBatchInfo.setUserColumns(bindMap2);
			createReceiveRequestBatchInfo.add(createActBatchInfo);
			MaterialReceiveRequestActServiceProxy
					.getReceiveRequestBatchService().create(eventInfo,
							createReceiveRequestBatchInfo);
			
		}

		if (queryResult.size() == 0) {
			createActDetailInfo.setReceiveActNO(receiveActNo);
			createActDetailInfo.setReceiveRequestName(receiveRequestName);
			createActDetailInfo.setReceiveRequestDetailName(ReceiveRequestDetailName);
			createActDetailInfo.setSiteName(siteName);
			createActDetailInfo.setMakeDate(Timestamp.valueOf(makeDate));
			createActDetailInfo.setReceiveQuantity(ReceiveQuantity);

			Map<String, Object> bindMap3 = new HashMap<String, Object>();
			bindMap3.put("receiveActState", "Created");
			bindMap3.put("innerRequestName", innerRequest);
			bindMap3.put("innerRequestDetailName", innerRequestDetail);
			createActDetailInfo.setUserColumns(bindMap3);

			MaterialReceiveRequestActDetailServiceProxy
					.getMaterialReceiveRequestActDetailService().create(
							eventInfo, createActDetailInfo);
			
		} else {
			ActDetailIncreaseInfo actDetailIncreaseInfo = new ActDetailIncreaseInfo();
			actDetailIncreaseInfo.setReceiveQuantity(ReceiveQuantity);

			MaterialReceiveRequestActDetailServiceProxy
					.getMaterialReceiveRequestActDetailService()
					.increaseReceivedQuantity(materialReceiveActDetailKey,
							eventInfo, actDetailIncreaseInfo);
			
		}		
//		//内向交货单行项目添加已收数量   added by wh 2016.10.11 
//		String insertQuery="UPDATE INNERREQUESTDETAIL SET RECEIVEDQUANTITY=:RECEIVEDQUANTITY WHERE SITENAME=:SITENAME AND INNERREQUESTNAME=:INNERREQUESTNAME AND INNERREQUESTDETAILNAME=:INNERREQUESTDETAILNAME"+
//							" AND receiveRequestName=:receiveRequestName and receiveRequestDetailName=:receiveRequestDetailName";
//		Map<String,String> insertMap=new HashMap<String, String>();
//		insertMap.put("RECEIVEDQUANTITY", receiveQuantity);
//		insertMap.put("SITENAME", siteName);
//		insertMap.put("INNERREQUESTNAME", innerRequest);
//		insertMap.put("INNERREQUESTDETAILNAME", innerRequestDetail);	
//		insertMap.put("receiveRequestName", receiveRequestName);
//		insertMap.put("receiveRequestDetailName", ReceiveRequestDetailName);
//		IDMFrameServiceProxy.getSqlTemplate().update(insertQuery,insertMap);
		//内向交货单行项目更新已收数量 added by wh 2016.10.11
		String updateQuery="UPDATE INNERREQUESTDETAIL SET RECEIVEDQUANTITY= (to_number(:RECEIVEDQUANTITY) + (SELECT  RECEIVEDQUANTITY from INNERREQUESTDETAIL WHERE SITENAME=:SITENAME AND INNERREQUESTNAME=:INNERREQUESTNAME AND INNERREQUESTDETAILNAME=:INNERREQUESTDETAILNAME)) "+
						   "WHERE SITENAME=:SITENAME AND INNERREQUESTNAME=:INNERREQUESTNAME AND INNERREQUESTDETAILNAME=:INNERREQUESTDETAILNAME";
		Map<String,Object> insertMap=new HashMap<String, Object>();
		insertMap.put("RECEIVEDQUANTITY", receiveQuantity);
		insertMap.put("SITENAME", siteName);
		insertMap.put("INNERREQUESTNAME", innerRequest);
		insertMap.put("INNERREQUESTDETAILNAME", innerRequestDetail);
//		insertMap.put("receiveRequestName", receiveRequestName);
//		insertMap.put("receiveRequestDetailName", ReceiveRequestDetailName);
//		insertMap.put("makeDate", Timestamp.valueOf(makeDate));
		IDMFrameServiceProxy.getSqlTemplate().update(updateQuery,insertMap);
		// 修改PO行项目数量
		MakeDetailIncreaseReceivedInfo makeDetailIncreaseReceivedInfo = new MakeDetailIncreaseReceivedInfo();
		MaterialReceiveRequestDetailKey materialReceiveRequestDetailKey = new MaterialReceiveRequestDetailKey();
		materialReceiveRequestDetailKey
				.setReceiveRequestName(receiveRequestName);
		materialReceiveRequestDetailKey.setSiteName(siteName);
		materialReceiveRequestDetailKey
				.setReceiveRequestDetailName(ReceiveRequestDetailName);

		makeDetailIncreaseReceivedInfo.setIncreaseQuantity(ReceiveQuantity);

		ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService()
				.increaseReceivedQuantity(materialReceiveRequestDetailKey,
						eventInfo, makeDetailIncreaseReceivedInfo);
	}

	// 郝春媚 2016-04-21
	public void CreateMaterialReceiveRequestActUsedInCancel(
			EventInfo eventInfo, String siteName, String receiveRequestName,
			String ReceiveRequestDetailName, Timestamp makeDate,
			String receiveActNo, String actType, double receiveQuantity)
			throws CustomException, ParseException {
		String actType1 = "";
		// receiveRequestName="lixiaolongtest1";
		// ReceiveRequestDetailName="lixiaolongtestDetail";
		// makeDate="2016-02-17 20:17:59";
		// receiveActNo="testabc1";

		/*
		 * SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 * Timestamp date = (Timestamp) sdf.parse(makeDate);
		 */

		double ReceiveQuantity = receiveQuantity;

		// receiveActInfo
		MaterialReceiveActDetailKey materialReceiveActDetailKey = new MaterialReceiveActDetailKey();
		// MaterialReceiveActDetail materialReceiveActDetail = new
		// MaterialReceiveActDetail();
		CreateActDetailInfo createActDetailInfo = new CreateActDetailInfo();
		//
		materialReceiveActDetailKey.setSiteName(siteName);
		materialReceiveActDetailKey.setReceiveRequestName(receiveRequestName);
		materialReceiveActDetailKey
				.setReceiveRequestDetailName(ReceiveRequestDetailName);
		materialReceiveActDetailKey.setReceiveActNO(receiveActNo);
		materialReceiveActDetailKey.setMakeDate(makeDate);

		// materialReceiveActDetail=
		// MaterialReceiveRequestActDetailServiceProxy.getMaterialReceiveRequestActDetailService().selectByKey(materialReceiveActDetailKey);

		String sql = "SELECT * FROM materialreceiveactdetail "
				+ "WHERE sitename = :sitename AND receiveRequestName = :receiveRequestName and ReceiveRequestDetailName=:ReceiveRequestDetailName and receiveActNo=:receiveActNo"
				+ " and makeDate=:makeDate ";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("sitename", siteName);
		bindMap.put("receiveRequestName", receiveRequestName);
		bindMap.put("ReceiveRequestDetailName", ReceiveRequestDetailName);
		bindMap.put("receiveActNo", receiveActNo);
		bindMap.put("makeDate", makeDate);

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

		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", receiveRequestName);
		bindMap1.put("receiveActNo", receiveActNo);

		List<Map<String, Object>> queryResult1 = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql1, bindMap1);
		if (queryResult1.size() != 0) {
			actType1 = queryResult1.get(0).get("ACTTYPE").toString();
		}
		if (queryResult1.size() == 0) {
			CreateReceiveRequestBatchInfo createActBatchInfo = new CreateReceiveRequestBatchInfo();
			List<CreateReceiveRequestBatchInfo> createReceiveRequestBatchInfo = new ArrayList<CreateReceiveRequestBatchInfo>();

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

			List<String> materialReceiveRequestAct;
			// String quantity="10";
			String product = "qwe";
			String grade = "w";
			String line = "q";
			String revisionCode = "r";

			createActBatchInfo.setSiteName(siteName);
			createActBatchInfo.setReceiveRequestName(receiveRequestName);
			createActBatchInfo.setReceiveactNO(receiveActNo);
			createActBatchInfo.setReceiveActState("Created");

			bindMap2.put("actType", actType);
			// bindMap2.put("receiveActState", );
			createActBatchInfo.setUserColumns(bindMap2);

			createReceiveRequestBatchInfo.add(createActBatchInfo);

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

		if (queryResult.size() == 0) {
			createActDetailInfo.setReceiveActNO(receiveActNo);
			createActDetailInfo.setReceiveRequestName(receiveRequestName);
			createActDetailInfo
					.setReceiveRequestDetailName(ReceiveRequestDetailName);
			createActDetailInfo.setSiteName(siteName);

			// Date date1 = new Date();//获取当前时间
			// SimpleDateFormat sdf = new
			// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// String str = sdf.format(date1);//时间存储为字符串
			// System.out.println(str);
			createActDetailInfo.setMakeDate(makeDate);
			createActDetailInfo.setReceiveQuantity(ReceiveQuantity);

			Map<String, Object> bindMap3 = new HashMap<String, Object>();
			bindMap3.put("receiveActState", "Created");

			createActDetailInfo.setUserColumns(bindMap3);

			MaterialReceiveRequestActDetailServiceProxy
					.getMaterialReceiveRequestActDetailService().create(
							eventInfo, createActDetailInfo);
		} else {
			ActDetailIncreaseInfo actDetailIncreaseInfo = new ActDetailIncreaseInfo();
			actDetailIncreaseInfo.setReceiveQuantity(ReceiveQuantity);

			MaterialReceiveRequestActDetailServiceProxy
					.getMaterialReceiveRequestActDetailService()
					.increaseReceivedQuantity(materialReceiveActDetailKey,
							eventInfo, actDetailIncreaseInfo);
		}

	}

	public void createShipRequestAct(EventInfo eventInfo, String siteName,
			String shipReqquestName, String receiveActState,
			String receiveActNo, String actType) {

		CreateActDetailInfo createActDetailInfo = new CreateActDetailInfo();

		MaterialReceiveRequestActDetailServiceProxy
				.getMaterialReceiveRequestActDetailService().create(eventInfo,
						createActDetailInfo);

	}

	public void createSupplier(String supplierNo, String description,
			String address, String city, String fax, String tel,
			String postCode, String nationalNo) {

		String sql = "SELECT * FROM SUPPLIER "
				+ "WHERE SUPPLIERNO = :supplierNo";

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

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

		if (queryResult == null || queryResult.isEmpty()) {
			Supplier s = new Supplier();
			SupplierKey sk = new SupplierKey();
			sk.setSupplierNO(supplierNo);

			s.setAddress(address);
			s.setSupplierName(description);
			s.setDescription(description);
			s.setCity(city);
			s.setTel(tel);
			s.setFax(fax);
			s.setNationalNO(nationalNo);
			s.setPostCode(postCode);
			s.setKey(sk);
			s.setLastEventUser("ERP");
			s.setLastEventTime(TimeStampUtil.getCurrentTimestamp());

			SupplierServiceImpl ss = new SupplierServiceImpl();
			ss.create(s);
		}

	}

	public Boolean checkReceiveRequest(String siteName,
			String receiveRequestName) {

		// MaterialReceiveRequest sr = new MaterialReceiveRequest();

		// MaterialReceiveRequestKey keyInfo = new
		// MaterialReceiveRequestKey(siteName, receiveRequestName);
		// sr=
		// MaterialReceiveRequestServiceProxy.getMaterialReceiveRequestService().selectByKey(keyInfo);

		String sql2 = "SELECT * FROM MATERIALRECEIVEREQUEST "
				+ "WHERE SITENAME = :siteName and RECEIVEREQUESTNAME = :receiveRequestName";

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

		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
	}

	/*
	 * Name : GenerateMaterialReceiveRequestName Desc : This function is
	 * GenerateMaterialReceiveRequestName Author : boe cim dev. Date :
	 * 2016.02.17
	 */
	public static String GenerateMaterialReceiveRequestName(String ruleName,
			String quantity, String siteName, String actType, String line,
			String revisionCode) throws CustomException {
		// 获取系统当前时间
		java.util.Calendar c = java.util.Calendar.getInstance();
		java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
				"yyyyMMdd");

		List<String> args = new ArrayList<String>();
		String actType1 = "";
		if (actType.equals("Normal") || actType.equals("Manual")|| actType.equals("ManualR")
				||actType.equals("BMDTOEM")||actType.equals("B5OEM")
				|| actType.equals("Reserve") ||actType.equals("ERPReserve")|| actType.equals("NormalIDO")
				|| actType.equals("NoPO")||actType.equals("SHIPOEM")) {
			actType1 = "R";
		}else if (actType.equals("RMA") || actType.equals("OEMReturn")) {
			actType1 = "F";
		}
		else if (actType.equals("Scrapped")) {
			actType1 = "B";
		} else if (actType.equals("Refund") || actType.equals("RefundOEM")
				|| actType.equals("WO") || actType.equals("POOEM")
				|| actType.equals("ManualT")||actType.equals("MES")||actType.equals("MESRefund")) {
			actType1 = "T";
		} else if (actType.equals("STCO")) {
			actType1 = "C";
		}else if(actType.equals("PSO"))
		{
			actType1 = "S";
		}
		else if(actType.equals("unUsual"))
		{
			actType1="U";
		}
		if(siteName.equals("B10")){
			siteName="BA";
		}else if(siteName.equals("BMDT")){
			siteName="BM";
		}else if(siteName.equals("B11")){
			siteName="BB";
		}else if(siteName.equals("BMOT")){
			siteName="BO";
		}else if(siteName.equals("B17")){
			siteName="BN";
		} else if ("B12".equals(siteName)) {
			siteName="BC";
		}else if (siteName.equals("B20")) {
			siteName = "BJ";
		}
		args.add(siteName);
		args.add(actType1);
		args.add(f.format(c.getTime()));

		String ReceiveRequestActNo;

		ReceiveRequestActNo = NameServiceProxy.getNameRuleDefService()
				.generateName("ReceiveActNo", args, 1).get(0);
		try {
			return ReceiveRequestActNo;
		} catch (Throwable e) {
			args = new ArrayList<String>();
			args.add(siteName);
			args.add(actType);
			args.add(f.format(c.getTime()));
			ReceiveRequestActNo = NameServiceProxy.getNameRuleDefService()
					.generateName("ReceiveActNo", args, 1).get(0);
			return ReceiveRequestActNo;
		}
	}
	public static String GenerateMaterialReceiveRequestNameSMT (String ruleName,
			String quantity, String siteName, String actType, String line,
			String revisionCode) throws CustomException {

		String actType1 = "";
		if (actType.equals("Normal") || actType.equals("Manual")|| actType.equals("ManualR")
				||actType.equals("BMDTOEM")
				|| actType.equals("Reserve") ||actType.equals("ERPReserve")|| actType.equals("NormalIDO")
				|| actType.equals("NoPO")||actType.equals("SHIPOEM")) {
			actType1 = "R";
		}else if (actType.equals("RMA") || actType.equals("OEMReturn")) {
			actType1 = "F";
		}
		else if (actType.equals("Scrapped")) {
			actType1 = "B";
		} else if (actType.equals("Refund") || actType.equals("RefundOEM")
				|| actType.equals("WO") || actType.equals("POOEM")
				|| actType.equals("ManualT")||actType.equals("MES")) {
			actType1 = "T";
		} else if (actType.equals("STCO")) {
			actType1 = "C";
			
		}else if(actType.equals("PSO"))
		{
			actType1 = "S";
		}

		SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmS");
		String date = df.format(new Date());
		String ReceiveRequestActNo;
		ReceiveRequestActNo=siteName+actType1+System.getProperty("factory")+date;
	
			return ReceiveRequestActNo;
		
	}

	public static String GeneratePalletID(String siteName) throws CustomException {
		// 获取系统当前时间
		java.util.Calendar c = java.util.Calendar.getInstance();
		java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
				"yyyyMMdd");
		List<String> args = new ArrayList<String>();
		String actType1 = "";		
		if(siteName.equals("B10")){
			siteName="BA";
		}else if(siteName.equals("BMDT")){
			siteName="BM";
		}else if(siteName.equals("B11")){
			siteName="BB";
		}else if(siteName.equals("BMOT")){
			siteName="BO";
		}else if(siteName.equals("B17")){
			siteName="BN";
		}else if ("B12".equals(siteName)) {
			siteName="BC";
		}
		args.add(siteName);
		args.add("PL");
		args.add(f.format(c.getTime()));
		String palletID;
		palletID = NameServiceProxy.getNameRuleDefService()
				.generateName("SUPERMATERIALPACKINGNAME", args, 1).get(0);
		try {
			return palletID;
		} catch (Throwable e) {
			args = new ArrayList<String>();
			args.add(siteName);
			args.add("PL");
			args.add(f.format(c.getTime()));
			palletID = NameServiceProxy.getNameRuleDefService()
					.generateName("SUPERMATERIALPACKINGNAME", args, 1).get(0);
			return palletID;
		}
	}	
	public void MaterialReceiveRequestDetailQuantity(EventInfo eventInfo,
			String siteName, String receiveRequestName,
			String ReceiveRequestDetailName, String receiveQuantity, String flag)
			throws CustomException, ParseException {
		MakeDetailIncreaseReceivedInfo makeDetailIncreaseReceivedInfo = new MakeDetailIncreaseReceivedInfo();
		MakeDetailDecreaseReceivedInfo makeDetailDecreaseReceivedInfo = new MakeDetailDecreaseReceivedInfo();
		MaterialReceiveRequestDetailKey materialShipRequestDetailKey = new MaterialReceiveRequestDetailKey();
		materialShipRequestDetailKey.setReceiveRequestName(receiveRequestName);
		materialShipRequestDetailKey.setSiteName(siteName);
		materialShipRequestDetailKey
				.setReceiveRequestDetailName(ReceiveRequestDetailName);
		if (flag.equals("Assign")) {
			makeDetailIncreaseReceivedInfo.setIncreaseQuantity(Double
					.parseDouble(receiveQuantity));
			ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService()
					.increaseReceivedQuantity(materialShipRequestDetailKey,
							eventInfo, makeDetailIncreaseReceivedInfo);
		} else if (flag.equals("Deassign")) {
			makeDetailDecreaseReceivedInfo.setDecreaseQuantity(Double
					.parseDouble(receiveQuantity));
			ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService()
					.decreaseReceivedQuantity(materialShipRequestDetailKey,
							eventInfo, makeDetailDecreaseReceivedInfo);

		}
	}
	public void MaterialShipRequestDetailQuantity(EventInfo eventInfo,
			String siteName, String receiveRequestName,
			String ReceiveRequestDetailName, String receiveQuantity, String flag)
			throws CustomException, ParseException,Exception {
		try{
		MaterialShipRequestDetailKey materialShipRequestDetailKey = new MaterialShipRequestDetailKey();
		materialShipRequestDetailKey.setShipRequestName(receiveRequestName);
		materialShipRequestDetailKey.setSiteName(siteName);
		materialShipRequestDetailKey.setShipRequestDetailName(ReceiveRequestDetailName);
		MakeShipDetailIncreaseAssignedInfo makeDetailIncreaseAssignedInfo = new MakeShipDetailIncreaseAssignedInfo();
		MakeShipDetailDecreaseAssignedInfo makeDetailDecreaseReceivedInfo  = new MakeShipDetailDecreaseAssignedInfo();
		if (flag.equals("Assign")) {
			makeDetailIncreaseAssignedInfo.setAssignedQuantity(Double
					.parseDouble(receiveQuantity));

			MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().increaseAssignedQuantity(materialShipRequestDetailKey,makeDetailIncreaseAssignedInfo);
		} else if (flag.equals("Deassign")) {
			makeDetailDecreaseReceivedInfo.setAssignedQuantity(Double
					.parseDouble(receiveQuantity));
			MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService()
					.decreaseAssignedQuantity(materialShipRequestDetailKey,
							 makeDetailDecreaseReceivedInfo);

		}
		}catch(Exception e)
		{
			throw e;
		}
	}
	public void MaterialReceiveRequestDetailQuantityByUnDoID(EventInfo eventInfo,
			String siteName, String receiveRequestName,
			String ReceiveRequestDetailName, String receiveQuantity, String flag)
			throws CustomException, ParseException {
		String sql2 = "SELECT RECEIVEREQUESTNAME,RECEIVEREQUESTDETAILNAME FROM BS_MATERIALPACKINGUNDOINFO "
				+ "WHERE UNDOID = :receiveRequestName and UNDODETAILNAME = :ReceiveRequestDetailName";

		Map<String, String> bindMap2 = new HashMap<String, String>();
		bindMap2.put("receiveRequestName", receiveRequestName);
		bindMap2.put("ReceiveRequestDetailName", ReceiveRequestDetailName);

		List<Map<String, Object>> queryResult2 = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql2, bindMap2);
		receiveRequestName=queryResult2.get(0).get("RECEIVEREQUESTNAME").toString();
		ReceiveRequestDetailName=queryResult2.get(0).get("RECEIVEREQUESTDETAILNAME").toString();
		MakeDetailIncreaseReceivedInfo makeDetailIncreaseReceivedInfo = new MakeDetailIncreaseReceivedInfo();
		MakeDetailDecreaseReceivedInfo makeDetailDecreaseReceivedInfo = new MakeDetailDecreaseReceivedInfo();
		MaterialReceiveRequestDetailKey materialShipRequestDetailKey = new MaterialReceiveRequestDetailKey();
		materialShipRequestDetailKey.setReceiveRequestName(receiveRequestName);
		materialShipRequestDetailKey.setSiteName(siteName);
		materialShipRequestDetailKey
				.setReceiveRequestDetailName(ReceiveRequestDetailName);
		if (flag.equals("Assign")) {
			makeDetailIncreaseReceivedInfo.setIncreaseQuantity(Double.parseDouble(receiveQuantity));
			ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService()
					.increaseReceivedQuantity(materialShipRequestDetailKey,
							eventInfo, makeDetailIncreaseReceivedInfo);
		} else if (flag.equals("Deassign")) {
			makeDetailDecreaseReceivedInfo.setDecreaseQuantity(Double.parseDouble(receiveQuantity));
			ReceiveRequestDetailServiceProxy.getReceiveRequestDetailService()
					.decreaseReceivedQuantity(materialShipRequestDetailKey,
							eventInfo, makeDetailDecreaseReceivedInfo);

		}
	}
	// 郝春媚 2016-04-21
	public String GenerateMaterialReceiveRequestNameUsedInCancel(
			String siteName, String actType) throws CustomException {
		// 获取系统当前时间
		java.util.Calendar c = java.util.Calendar.getInstance();
		java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
				"yyyyMMdd");

		List<String> args = new ArrayList<String>();
		String actType1 = "";
		if (actType.equals("Normal") || actType.equals("Manual")
				|| actType.equals("Reserve")) {
			actType1 = "R";
		}
		if (actType.equals("RMA") || actType.equals("OEMReturn")) {
			actType1 = "F";
		} else if (actType.equals("Refund") || actType.equals("WO")
				|| actType.equals("POOEM")) {
			actType1 = "T";
		} else if (actType.equals("STCO")) {
			actType1 = "C";
		}
		args.add(siteName);
		args.add(actType1);
		args.add(f.format(c.getTime()));

		String ReceiveRequestActNo;

		ReceiveRequestActNo = NameServiceProxy.getNameRuleDefService()
				.generateName("ReceiveActNo", args, 1).get(0);
		try {
			return ReceiveRequestActNo;
		} catch (Throwable e) {
			args = new ArrayList<String>();
			args.add(siteName);
			args.add(actType);
			args.add(f.format(c.getTime()));
			ReceiveRequestActNo = NameServiceProxy.getNameRuleDefService()
					.generateName("ReceiveActNo", args, 1).get(0);
			return ReceiveRequestActNo;
		}
	}

	// 郝春媚 2016-05-26
	public String GenerateMaterialCancelReceiveRequestNameUsedInCancel(
			String receiverequestname) throws CustomException {

		List<String> args = new ArrayList<String>();

		args.add(receiverequestname);

		String ReceiveRequestActNo;

		ReceiveRequestActNo = NameServiceProxy.getNameRuleDefService()
				.generateName("CancelReceiveActNo", args, 1).get(0);
		try {
			return ReceiveRequestActNo;
		} catch (Throwable e) {
			args = new ArrayList<String>();
			args.add(receiverequestname);
			ReceiveRequestActNo = NameServiceProxy.getNameRuleDefService()
					.generateName("CancelReceiveActNo", args, 1).get(0);
			return ReceiveRequestActNo;
		}
	}

	/*
	 * Name : CreateMaterialRRActByLabel Desc :
	 * CreateMaterialReceiveRequestActByLabel
	 */

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

		Map<String, Object> bindMap3 = new HashMap<String, Object>();
		bindMap3.put("sitename", siteName);
		bindMap3.put("receiveRequestName", receiveRequestName);
		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", receiveRequestName);
		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(receiveRequestName);
			createActBatchInfo.setReceiveactNO(receiveActNo);
			createActBatchInfo.setReceiveActState("Created");
			bindMap2.put("actType", actType);

			createActBatchInfo.setUserColumns(bindMap2);

			createReceiveRequestBatchInfo.add(createActBatchInfo);

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

	}
	public void CreateOEMActByLabel(EventInfo eventInfo,
			String siteName, String receiveRequestName, String receiveActNo,
			String actType) throws CustomException, ParseException {
		String sql1 = "SELECT receiveActNo FROM materialreceiveact "
				+ "WHERE sitename = :sitename AND receiveRequestName = :receiveRequestName  and receiveActNo=:receiveActNo and RECEIVEACTSTATE='Created'";

		Map<String, Object> bindMap1 = new HashMap<String, Object>();
		bindMap1.put("sitename", siteName);
		bindMap1.put("receiveRequestName", receiveRequestName);
		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(receiveRequestName);
			createActBatchInfo.setReceiveactNO(receiveActNo);
			createActBatchInfo.setReceiveActState("Created");
			bindMap2.put("actType", actType);

			createActBatchInfo.setUserColumns(bindMap2);

			createReceiveRequestBatchInfo.add(createActBatchInfo);

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

	}
	public void UpdateIDOForCreateACT(EventInfo eventInfo, String siteName,
			String receiveRequestName, String IDOID, String methodFlag,
			String receiveActNo) {
		if (methodFlag == null) {
			return;
		}
		// if(methodFlag.equals("UpdateIDOForCreateACT")){//内向交货单创建抬头文本时的逻辑
		String sql = " update INBOUNDDO ido set ido.actno =?  where ido.IDOID=? and ido.SITENAME=? and ido.RECEIVEREQUESTNAME=? ";

		IDMFrameServiceProxy.getSqlTemplate()
				.update(sql, receiveActNo, IDOID, siteName, receiveRequestName);
		// }
	}
	public void updateMaterialreceiveactState(EventInfo eventInfo,
			String siteName, String receiveRequestName, String receiveActNo,
			String state) throws Exception 
	{
		try
		{
			MaterialReceiveAct act=new MaterialReceiveAct();
			MaterialReceiveActKey actKey=new MaterialReceiveActKey();
			actKey.setSiteName(siteName);
			actKey.setReceiveactNO(receiveActNo);
			actKey.setReceiveRequestName(receiveRequestName);
			act.setReceiveActState(state);
			act.setKey(actKey);
			MaterialReceiveRequestActServiceProxy.getReceiveRequestBatchService().update(act);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
	}

	public void BMDTcreate(EventInfo eventInfo, String siteName,
			String receiveRequestName, String purchaseOrderGroup,
			String receiveRequestState, String supplierNO, String systemType,
			String saveReason, String respPerson, String respDepart,
			String solveTime,String erpfactory) {
		MaterialReceiveRequestKey key = new MaterialReceiveRequestKey();
		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);

		CreateReceiveRequestInfo createInfo = new CreateReceiveRequestInfo();

		createInfo.setSiteName(siteName);
		createInfo.setOemSiteName(erpfactory);
		createInfo.setReceiveRequestName(receiveRequestName);
		createInfo.setPurchaseOrderGroup(purchaseOrderGroup);
		createInfo.setReceiveRequestState(receiveRequestState);
		createInfo.setSupplierNO(supplierNO);
		createInfo.setSystemType(systemType);
		Map<String, Object> userColumn = new HashMap<String, Object>();
		userColumn.put("saveReason", saveReason);
		userColumn.put("respPerson", respPerson);
		userColumn.put("respDepart", respDepart);
		if (solveTime == "") {

		} else {
			Timestamp sTime = ConvertUtil.convertToTimeStamp(solveTime);
			userColumn.put("solveTime", sTime);
		}

		createInfo.setUserColumns(userColumn);
		eventInfo.setEventName("Create");

		MaterialReceiveRequestServiceProxy.getMaterialReceiveRequestService()
				.create(eventInfo, createInfo);
	}
	//20190424_ltg
	public void MaterialOQAJudge(EventInfo eventInfo, 
			String siteName,
			String materialPackingName, 
			String materialID,
			String materialSpecName, 
			String OQAJudge, 
			String materialQuantity,
			String packingState,
			String stockState, 
			String receiveRequestName,
			String receiveRequestDetailName, 
			String makeDate,
			String receiveActNO, 
			String ERPlocation,
			String ERPFactory,
			String expiringDate,
			String materialType) throws CustomException
	{
		
		String sql4 = "SELECT materialpackingname FROM  materialpacking WHERE materialpackingname =:MATERIALPACKINGNAME AND sitename =:SITENAME AND stockstate IN ( 'Stocked' )AND materialquantity > 0";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("MATERIALPACKINGNAME", materialPackingName);
		bindMap.put("SITENAME", siteName);
		
		List<Map<String, Object>> result1 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql4.toString(), bindMap);
		
		if(result1.size() == 0){
			throw new CustomException("Exception", "BOX不是在库状态，请查询BOX历史记录 ");
		}
		
		String sql = "SELECT MATERIALID FROM MATERIALOQAJUDGE WHERE MATERIALID =:MATERIALID";
		bindMap.put("MATERIALID", materialID);

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

		if (result.size() > 0) {
			String sql1 = "UPDATE MATERIALOQAJUDGE SET " +
					"SITENAME = :SITENAME ,MATERIALPACKINGNAME = :MATERIALPACKINGNAME," +
					"MATERIALSPECNAME = :MATERIALSPECNAME,OQAJUDGE = :OQAJUDGE," +
					"MATERIALQUANTITY = :MATERIALQUANTITY,PACKINGSTATE = :PACKINGSTATE," +
					"STOCKSTATE = :STOCKSTATE,LASTEVENTNAME = :LASTEVENTNAME," +
					"LASTEVENTTIMEKEY = :LASTEVENTTIMEKEY,LASTEVENTTIME = SYSDATE," +
					"LASTEVENTUSER = :LASTEVENTUSER,LASTEVENTCOMMENT = :LASTEVENTCOMMENT,MATERIALTYPE = :MATERIALTYPE," +
					"RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME,RECEIVEREQUESTDETAILNAME = :RECEIVEREQUESTDETAILNAME," +
					"MAKEDATE =(TO_DATE( :MAKEDATE,'YYYY-MM-DD HH24:MI:SS')),RECEIVEACTNO = :RECEIVEACTNO,ERPLOCATION = :ERPLOCATION," +
					"ERPFACTORY = :ERPFACTORY,EXPIRINGDATE = (TO_DATE( :EXPIRINGDATE,'YYYY-MM-DD HH24:MI:SS')) WHERE MATERIALID = :MATERIALID ";
			Map<String, Object> bindMap1 = new HashMap<String, Object>();
			bindMap1.put("SITENAME", siteName);
			bindMap1.put("MATERIALPACKINGNAME", materialPackingName);
			bindMap1.put("MATERIALSPECNAME", materialSpecName);
			bindMap1.put("OQAJUDGE", OQAJudge);
			bindMap1.put("MATERIALQUANTITY", materialQuantity);
			bindMap1.put("PACKINGSTATE", packingState);
			bindMap1.put("STOCKSTATE", stockState);
			bindMap1.put("MATERIALID", materialID);
			bindMap1.put("LASTEVENTNAME",eventInfo.getEventName() );
			bindMap1.put("LASTEVENTTIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
			bindMap1.put("LASTEVENTUSER", eventInfo.getEventUser());
			bindMap1.put("LASTEVENTCOMMENT", eventInfo.getEventComment());
			bindMap1.put("RECEIVEREQUESTNAME", receiveRequestName);
			bindMap1.put("RECEIVEREQUESTDETAILNAME", receiveRequestDetailName);
			bindMap1.put("MAKEDATE", "");
			bindMap1.put("RECEIVEACTNO", receiveActNO);
			bindMap1.put("ERPLOCATION", ERPlocation);
			bindMap1.put("ERPFACTORY", ERPFactory);
			bindMap1.put("EXPIRINGDATE", "");
			bindMap1.put("MATERIALTYPE", materialType);

			

				IDMFrameServiceProxy.getSqlTemplate().update(sql1, bindMap1);

		} else {
			String sql2 = "INSERT INTO MATERIALOQAJUDGE "
			+" (MATERIALID, SITENAME, MATERIALPACKINGNAME, MATERIALSPECNAME, OQAJUDGE, MATERIALQUANTITY, PACKINGSTATE, STOCKSTATE, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER, LASTEVENTCOMMENT, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME, MAKEDATE, RECEIVEACTNO, ERPLOCATION, ERPFACTORY, EXPIRINGDATE,MATERIALTYPE) "
		+" VALUES  "
			+"  (:MATERIALID, :SITENAME, :MATERIALPACKINGNAME, :MATERIALSPECNAME, :OQAJUDGE, :MATERIALQUANTITY, :PACKINGSTATE, :STOCKSTATE, :LASTEVENTNAME, :LASTEVENTTIMEKEY, SYSDATE, :LASTEVENTUSER, :LASTEVENTCOMMENT, :RECEIVEREQUESTNAME, :RECEIVEREQUESTDETAILNAME, (TO_DATE( :MAKEDATE,'YYYY-MM-DD HH24:MI:SS')), :RECEIVEACTNO, :ERPLOCATION, :ERPFACTORY, (TO_DATE( :EXPIRINGDATE,'YYYY-MM-DD HH24:MI:SS')), :MATERIALTYPE) ";
			Map<String, Object> bindMap2 = new HashMap<String, Object>();
			bindMap2.put("SITENAME", siteName);
			bindMap2.put("MATERIALPACKINGNAME", materialPackingName);
			bindMap2.put("MATERIALSPECNAME", materialSpecName);
			bindMap2.put("OQAJUDGE", OQAJudge);
			bindMap2.put("MATERIALID", materialID);
			bindMap2.put("MATERIALQUANTITY", materialQuantity);
			bindMap2.put("PACKINGSTATE", packingState);
			bindMap2.put("STOCKSTATE", stockState);
			bindMap2.put("LASTEVENTNAME", eventInfo.getEventName());
			bindMap2.put("LASTEVENTTIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
			bindMap2.put("LASTEVENTUSER", eventInfo.getEventUser());
			bindMap2.put("LASTEVENTCOMMENT", eventInfo.getEventComment());
			bindMap2.put("RECEIVEREQUESTNAME", receiveRequestName);
			bindMap2.put("RECEIVEREQUESTDETAILNAME", receiveRequestDetailName);
			bindMap2.put("MAKEDATE", "");
			bindMap2.put("RECEIVEACTNO", receiveActNO);
			bindMap2.put("ERPLOCATION", ERPlocation);
			bindMap2.put("ERPFACTORY", ERPFactory);
			bindMap2.put("EXPIRINGDATE", "");
			bindMap2.put("MATERIALTYPE", materialType);

				IDMFrameServiceProxy.getSqlTemplate().update(sql2, bindMap2);

		}

		String sql3 = "INSERT INTO MATERIALOQAJUDGEHISTORY " +
				"(MATERIALID, SITENAME, MATERIALPACKINGNAME, MATERIALSPECNAME, OQAJUDGE, MATERIALQUANTITY, PACKINGSTATE, STOCKSTATE, EVENTNAME, TIMEKEY, EVENTTIME, EVENTUSER, EVENTCOMMENT, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME, MAKEDATE, RECEIVEACTNO, ERPLOCATION, ERPFACTORY, EXPIRINGDATE,MATERIALTYPE) " +
				"VALUES " +
				"(:MATERIALID, :SITENAME, :MATERIALPACKINGNAME, :MATERIALSPECNAME, :OQAJUDGE, :MATERIALQUANTITY, :PACKINGSTATE, :STOCKSTATE, :EVENTNAME, :TIMEKEY, SYSDATE, :EVENTUSER, :EVENTCOMMENT, :RECEIVEREQUESTNAME, :RECEIVEREQUESTDETAILNAME,  (TO_DATE(:MAKEDATE,'YYYY-MM-DD HH24:MI:SS')), :RECEIVEACTNO, :ERPLOCATION, :ERPFACTORY,(TO_DATE( :EXPIRINGDATE,'YYYY-MM-DD HH24:MI:SS')) ,:MATERIALTYPE) ";
		Map<String, Object> bindMap3 = new HashMap<String, Object>();
		bindMap3.put("EVENTUSER", eventInfo.getEventUser());
		bindMap3.put("SITENAME", siteName);
		bindMap3.put("MATERIALPACKINGNAME", materialPackingName);
		bindMap3.put("MATERIALSPECNAME", materialSpecName);
		bindMap3.put("OQAJUDGE", OQAJudge);
		bindMap3.put("MATERIALQUANTITY", materialQuantity);
		bindMap3.put("PACKINGSTATE", packingState);
		bindMap3.put("STOCKSTATE", stockState);
		bindMap3.put("MATERIALID", materialID);
		bindMap3.put("EVENTNAME", eventInfo.getEventName());
		bindMap3.put("TIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
		bindMap3.put("EVENTCOMMENT", eventInfo.getEventComment());
		bindMap3.put("RECEIVEREQUESTNAME", receiveRequestName);
		bindMap3.put("RECEIVEREQUESTDETAILNAME", receiveRequestDetailName);
		bindMap3.put("MAKEDATE","");
		bindMap3.put("RECEIVEACTNO", receiveActNO);
		bindMap3.put("ERPLOCATION", ERPlocation);
		bindMap3.put("ERPFACTORY", ERPFactory);
		bindMap3.put("EXPIRINGDATE","");
		bindMap3.put("MATERIALTYPE", materialType);


			IDMFrameServiceProxy.getSqlTemplate().update(sql3, bindMap3);

			//log.info("INSERT MATERIALOQAJUDGEHISTORY Error Occured");
		}
	//20190514_ltg
	public void MaterialSQEJudge(EventInfo eventInfo, 
			String siteName,
			String materialPackingName, 
			String materialID,
			String materialSpecName, 
			String OQAJudge, 
			String materialQuantity,
			String packingState,
			String stockState, 
			String receiveRequestName,
			String receiveRequestDetailName, 
			String makeDate,
			String receiveActNO, 
			String ERPlocation,
			String ERPFactory,
			String expiringDate) throws CustomException
	{
		
		
		String sql = "INSERT INTO MATERIALOQAJUDGEHISTORY " +
				"(MATERIALID, SITENAME, MATERIALPACKINGNAME, MATERIALSPECNAME, OQAJUDGE, MATERIALQUANTITY, PACKINGSTATE, STOCKSTATE, EVENTNAME, TIMEKEY, EVENTTIME, EVENTUSER, EVENTCOMMENT, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME, MAKEDATE, RECEIVEACTNO, ERPLOCATION, ERPFACTORY, EXPIRINGDATE) " +
				"VALUES " +
				"(:MATERIALID, :SITENAME, :MATERIALPACKINGNAME, :MATERIALSPECNAME, :OQAJUDGE, :MATERIALQUANTITY, :PACKINGSTATE, :STOCKSTATE, :EVENTNAME, :TIMEKEY, SYSDATE, :EVENTUSER, :EVENTCOMMENT, :RECEIVEREQUESTNAME, :RECEIVEREQUESTDETAILNAME,  (TO_DATE(:MAKEDATE,'YYYY-MM-DD HH24:MI:SS')), :RECEIVEACTNO, :ERPLOCATION, :ERPFACTORY,(TO_DATE( :EXPIRINGDATE,'YYYY-MM-DD HH24:MI:SS'))) ";
		Map<String, Object> bindMap3 = new HashMap<String, Object>();
		bindMap3.put("EVENTUSER", eventInfo.getEventUser());
		bindMap3.put("SITENAME", siteName);
		bindMap3.put("MATERIALPACKINGNAME", materialPackingName);
		bindMap3.put("MATERIALSPECNAME", materialSpecName);
		bindMap3.put("OQAJUDGE", OQAJudge);
		bindMap3.put("MATERIALQUANTITY", materialQuantity);
		bindMap3.put("PACKINGSTATE", packingState);
		bindMap3.put("STOCKSTATE", stockState);
		bindMap3.put("MATERIALID", materialID);
		bindMap3.put("EVENTNAME", eventInfo.getEventName());
		bindMap3.put("TIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
		bindMap3.put("EVENTCOMMENT", eventInfo.getEventComment());
		bindMap3.put("RECEIVEREQUESTNAME", receiveRequestName);
		bindMap3.put("RECEIVEREQUESTDETAILNAME", receiveRequestDetailName);
		bindMap3.put("MAKEDATE","");
		bindMap3.put("RECEIVEACTNO", receiveActNO);
		bindMap3.put("ERPLOCATION", ERPlocation);
		bindMap3.put("ERPFACTORY", ERPFactory);
		bindMap3.put("EXPIRINGDATE","");


			IDMFrameServiceProxy.getSqlTemplate().update(sql, bindMap3);

			//log.info("INSERT MATERIALOQAJUDGEHISTORY Error Occured");
		}
	
	/**
	 * 创建oem内部代工PO
	 * @param eventInfo
	 * @param siteName
	 * @param receiveRequestName
	 * @param purchaseOrderGroup
	 * @param receiveRequestState
	 * @param supplierNO
	 * @param systemType
	 * @param saveReason
	 * @param respPerson
	 * @param respDepart
	 * @param solveTime
	 * @param passState
	 */
	public void createOemPO(EventInfo eventInfo, String siteName,
			String receiveRequestName, String purchaseOrderGroup,
			String receiveRequestState, String supplierNO, String systemType,
			String saveReason, String respPerson, String respDepart,
			String solveTime,String passState,String oemERPFactoryName) {
		MaterialReceiveRequestKey key = new MaterialReceiveRequestKey();
		key.setSiteName(siteName);
		key.setReceiveRequestName(receiveRequestName);

		String oemSiteName = new CommonValidation().getSiteName(oemERPFactoryName);
		
		CreateReceiveRequestInfo createInfo = new CreateReceiveRequestInfo();

		createInfo.setSiteName(siteName);
		createInfo.setReceiveRequestName(receiveRequestName);
		createInfo.setPurchaseOrderGroup(purchaseOrderGroup);
		createInfo.setReceiveRequestState(receiveRequestState);
		createInfo.setSupplierNO(supplierNO);
		createInfo.setSystemType(systemType);
		createInfo.setOemSiteName(oemSiteName);
		Map<String, Object> userColumn = new HashMap<String, Object>();
		userColumn.put("saveReason", saveReason);
		userColumn.put("respPerson", respPerson);
		userColumn.put("respDepart", respDepart);
		userColumn.put("passState", passState);
		if (solveTime == "") {

		} else {
			Timestamp sTime = ConvertUtil.convertToTimeStamp(solveTime);
			userColumn.put("solveTime", sTime);
		}

		createInfo.setUserColumns(userColumn);
		eventInfo.setEventName("Create");

		MaterialReceiveRequestServiceProxy.getMaterialReceiveRequestService()
				.create(eventInfo, createInfo);
		/*if(passState.equals("Completed"))
		{
			com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.GetPOPIReturn("NormalStockIn", eventInfo.getEventUser(), receiveRequestName);
			com.cim.idm.mwmsextend.erpinterface.service.ERPInterfaceServiceImpl.GetPOPIReturn("PONormalStockIn", eventInfo.getEventUser(), receiveRequestName);
		}*/
	}
	
	public String selectMaterialReceiveRequestInfo(String siteName, String receiveRequestName) throws CustomException, ParseException {
		String flag="";
		String sql1 = "SELECT RECEIVEREQUESTTYPE FROM MATERIALRECEIVEREQUEST WHERE SITENAME = :sitename AND RECEIVEREQUESTNAME = :receiveRequestName ";
		Map<String, Object> bindMap1 = new HashMap<String, Object>();
		bindMap1.put("sitename", siteName);
		bindMap1.put("receiveRequestName", receiveRequestName);
		
		List<Map<String, Object>> queryResult = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql1, bindMap1);
		if(!queryResult.isEmpty() && queryResult.size() > 0 && null != queryResult.get(0).get("RECEIVEREQUESTTYPE")) {
			return flag = queryResult.get(0).get("RECEIVEREQUESTTYPE") == null ? " ": queryResult.get(0).get("RECEIVEREQUESTTYPE").toString();
		}
		//return GenericServiceProxy.getConstantMap().CancelStockInInterfaceFlag;
		return flag;
	}

	public static void CreateInnerRequestActByLabel(EventInfo eventInfo,
			String siteName, String receiveRequestName, String receiveActNo,
			String actType,List<Element> materialPackingNameList) throws CustomException, ParseException {
		String sql2 = "SELECT * FROM materialpacking "
				+ "WHERE sitename = :sitename AND receiveRequestName = :receiveRequestName  and receiveActNo=:receiveActNo and erpstate='ERPSUCCESS' ";

		Map<String, Object> bindMap3 = new HashMap<String, Object>();
		bindMap3.put("sitename", siteName);
		bindMap3.put("receiveRequestName", receiveRequestName);
		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", receiveRequestName);
		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(receiveRequestName);
			createActBatchInfo.setReceiveactNO(receiveActNo);
			createActBatchInfo.setReceiveActState("Created");
			bindMap2.put("actType", actType);

			createActBatchInfo.setUserColumns(bindMap2);

			createReceiveRequestBatchInfo.add(createActBatchInfo);

			MaterialReceiveRequestActServiceProxy
					.getReceiveRequestBatchService().create(eventInfo,
							createReceiveRequestBatchInfo);
			String sql_update = "update MATERIALPACKING t set t.receiveactno=:receiveActNo where t.RECEIVEREQUESTNAME=:RECEIVEREQUESTNAME";
			bindMap2.put("RECEIVEREQUESTNAME", receiveRequestName);
			bindMap2.put("receiveActNo", receiveActNo);
			//IDMFrameServiceProxy.getSqlTemplate().update(sql_update, bindMap2);
		}

	}
	
	public void InnerRequestDetailQuantity(EventInfo eventInfo,
			String siteName, String innerRequestName,
			String innerRequestDetailName, String receiveQuantity, String flag)
			throws CustomException, ParseException {
		String selectInnerRequestDetailSql = " SELECT INNERREQUESTNAME,INNERREQUESTDETAILNAME,RECEIVEDQUANTITY FROM INNERREQUESTDETAIL WHERE SITENAME = :SITENAME AND INNERREQUESTNAME = :INNERREQUESTNAME AND INNERREQUESTDETAILNAME = :INNERREQUESTDETAILNAME ";
		Map<String, Object> selectInnerRequestDetailMap = new HashMap<String, Object>();
		selectInnerRequestDetailMap.put("SITENAME", siteName);
		selectInnerRequestDetailMap.put("INNERREQUESTNAME", innerRequestName);
		selectInnerRequestDetailMap.put("INNERREQUESTDETAILNAME", innerRequestDetailName);
		List<Map<String, Object>> queryInnerRequestDetailResult = IDMFrameServiceProxy.getSqlTemplate().queryForList(selectInnerRequestDetailSql, selectInnerRequestDetailMap);
		if(!queryInnerRequestDetailResult.isEmpty() && queryInnerRequestDetailResult.size() > 0) {
		    String realityReceivedQuantity = queryInnerRequestDetailResult.get(0).get("RECEIVEDQUANTITY") == null ? "0": queryInnerRequestDetailResult.get(0).get("RECEIVEDQUANTITY").toString();
		    if (flag.equals("Assign")) {
		    	double realityDouble = Double.parseDouble(realityReceivedQuantity);
		        double moveDouble = Double.parseDouble(receiveQuantity);
		        receiveQuantity = Double.toString(realityDouble + moveDouble);
			} else if (flag.equals("Deassign")) {
				double realityDouble = Double.parseDouble(realityReceivedQuantity);
		        double moveDouble = Double.parseDouble(receiveQuantity);
		        receiveQuantity = Double.toString(realityDouble - moveDouble);
			}
			String updateInnerRequestDetailSql = " UPDATE INNERREQUESTDETAIL SET RECEIVEDQUANTITY = :RECEIVEDQUANTITY "
					+ "WHERE SITENAME = :SITENAME AND INNERREQUESTNAME = :INNERREQUESTNAME AND INNERREQUESTDETAILNAME = :INNERREQUESTDETAILNAME ";
			Map<String, Object> updateInnerRequestDetailMap = new HashMap<String, Object>();
			updateInnerRequestDetailMap.put("SITENAME", siteName);
			updateInnerRequestDetailMap.put("INNERREQUESTNAME", innerRequestName);
			updateInnerRequestDetailMap.put("INNERREQUESTDETAILNAME", innerRequestDetailName);
			updateInnerRequestDetailMap.put("RECEIVEDQUANTITY", receiveQuantity);
			IDMFrameServiceProxy.getSqlTemplate().update(updateInnerRequestDetailSql, updateInnerRequestDetailMap);
		}
	}


}
