package com.cim.idm.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cim.idm.data.ErpMessageLog;
import com.cim.idm.exception.GlobalException;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.exception.IDMFrameDBErrorSignal;
import com.cim.idm.framework.util.sys.SystemPropHelper;
import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.mwmsextend.generic.util.CommonValidation;
import com.cim.idm.mwmsextend.materialpacking.service.MaterialPackingServiceImpl;
import com.cim.idm.mwmsextend.materialpacking.service.MaterialPackingServiceUtil;
import com.cim.idm.mwmsextend.materialshiprequest.service.MaterialShipRequestServiceImpl;
import com.cim.idm.service.ChargeSplitService;
import com.cim.idm.service.InvoiceService;
import com.cim.idm.service.QMSService;
import com.cim.idm.service.impl.MESServiceImpl;
import com.cim.idm.service.impl.ToSAPServiceImpl;
import com.cim.idm.util.MessageLogUtil;
import com.cim.idm.util.NCHttpUtil;
import com.cim.idm.util.ToSAPMessageUtil;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.utils.CodeGenerator;
import com.cim.idm.utils.CommonUtils;
import com.cim.idm.utils.EventInfoUtil;
import com.cim.idm.wmsextend.generic.GenericServiceProxy;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPackingKey;
import com.cim.idm.wmspackage.materialpacking.management.info.CreateInfo;
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.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.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import groovy.lang.Lazy;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.commons.collections4.MapUtils;
import org.quartz.SimpleTrigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class InvoiceServiceImpl implements InvoiceService {

	@Autowired
	private ToSAPMessageUtil toSAPMessageUtil;

	@Autowired
	private ToSAPServiceImpl toSAPService;
	@Autowired
	private QMSService qmsService;
	@Autowired
	@Lazy
	private ChargeSplitService chargeSplitService;

	private EventInfo makeEventInfo;

	private MaterialShipRequestServiceImpl ms = new MaterialShipRequestServiceImpl();

	private MaterialPackingServiceImpl mp = new MaterialPackingServiceImpl();

	private CommonValidation comm = new CommonValidation ();
	private CommonUtils untils=new CommonUtils();

	public InvoiceServiceImpl() {
	}

	/**
	 * 创建入库单
	 * @param eventInfo
	 * @param siteName
	 * @param receiveRequestName
	 * @param ReceiveRequestDetailName
	 * @param makeDate
	 * @param receiveActNo
	 * @param actType
	 * @param receiveQuantity
	 * @param subMaterialSpecName
	 * @throws CustomException
	 * @throws ParseException
	 */
	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 = "";

		float ReceiveQuantity = Float.parseFloat(receiveQuantity);

		// receiveActInfo
		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 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", 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>();


			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);

			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);

			//			因为float精度问题，再更新一次
			try {
				String updateSql= " UPDATE MATERIALRECEIVEACTDETAIL SET RECEIVEQUANTITY = :QTY WHERE RECEIVEACTNO = :RECEIVEACTNO"
						+ " AND RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND RECEIVEREQUESTDETAILNAME = :RECEIVEREQUESTDETAILNAME";
				Map<String, Object> updateMap = new HashMap<String, Object>();
				updateMap.put("QTY", receiveQuantity);
				updateMap.put("RECEIVEACTNO", receiveActNo);
				updateMap.put("RECEIVEREQUESTNAME", receiveRequestName);
				updateMap.put("RECEIVEREQUESTDETAILNAME", createActDetailInfo.getReceiveRequestDetailName());
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, updateMap);
			} catch (IDMFrameDBErrorSignal e) {
			}

		} else {
			ActDetailIncreaseInfo actDetailIncreaseInfo = new ActDetailIncreaseInfo();
			actDetailIncreaseInfo.setReceiveQuantity(ReceiveQuantity);
			MaterialReceiveRequestActDetailServiceProxy
			.getMaterialReceiveRequestActDetailService()
			.increaseReceivedQuantity(materialReceiveActDetailKey,
					eventInfo, actDetailIncreaseInfo);
		}
		//float型计算精度缺失问题
		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);
	}


	@Transactional
	public String SendERPStockOut20240802(String shipRequestName, String siteName, String eventUser,
										  List<MaterialPacking> list , String commitDate, String opCode) throws Exception {
		//判断单据类型调用不同的NC接口
		String sql = "SELECT m.SHIPREQUESTTYPE,m.SHIPREQUESTDETAILTYPE FROM MATERIALSHIPREQUEST m WHERE m.SHIPREQUESTNAME = :SHIPREQUESTNAME";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		List<String> materialPackingKeyList = new ArrayList<> ();
		List<MaterialPackingKey> arrayList = new ArrayList<MaterialPackingKey> ();
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		String shipRequestType = queryForList.get(0).get("SHIPREQUESTTYPE").toString();
		String detailype = queryForList.get(0).get("SHIPREQUESTDETAILTYPE").toString();
		if ("4C".equals(shipRequestType)) {
			String sql222 = "SELECT SUM(m.REQUESTQUANTITY - m.ASSIGNEDQUANTITY)  QTY,MATERIALSPECNAME FROM MATERIALSHIPREQUESTDETAIL m WHERE m.SHIPREQUESTNAME  = :SHIPREQUESTNAME GROUP BY MATERIALSPECNAME";
			List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql222, hashMap);
			for (int i = 0; i < queryForList2.size(); i++) {
				String string = queryForList2.get(i).get("QTY").toString();
				String MATERIALSPECNAME = queryForList2.get(i).get("MATERIALSPECNAME").toString();
				BigDecimal bigDecimal = new BigDecimal(string);
				if (bigDecimal.compareTo(BigDecimal.ZERO) < 0 ) {
					throw new CustomException( MATERIALSPECNAME + "销售不允许超发");
				}
			}
		}
		//备份过账条码表的信息，并删除
		String bfSql = "INSERT\r\n" + 
				"	INTO\r\n" + 
				"	MATERIALPACKINGSUBHISTORY T (T.MATERIALPACKINGNAME,\r\n" + 
				"	T.RECEIVEREQUESTNAME,\r\n" + 
				"	T.RECEIVEREQUESTDETAILNAME,\r\n" + 
				"	T.QTY)\r\n" + 
				"SELECT\r\n" + 
				"	M.MATERIALPACKINGNAME,\r\n" + 
				"	M.RECEIVEREQUESTNAME,\r\n" + 
				"	M.RECEIVEREQUESTDETAILNAME,\r\n" + 
				"	M.QTY\r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKINGSUB M\r\n" + 
				"WHERE\r\n" + 
				"	M.RECEIVEREQUESTNAME = :SHIPREQUESTNAME";
		String scSql = "DELETE FROM MATERIALPACKINGSUB WHERE RECEIVEREQUESTNAME  = :SHIPREQUESTNAME";
		IDMFrameServiceProxy.getSqlTemplate().update(bfSql, hashMap);
		IDMFrameServiceProxy.getSqlTemplate().update(scSql, hashMap);
		EventInfoUtil eventInfoUtil = new EventInfoUtil();
		EventInfo makeEventInfo = eventInfoUtil.makeEventInfo("CompletedShipRequest", eventUser, "CompletedShipRequest");
		//循环过账批次
		for (int i = 0; i < list.size(); ++ i) {
			String materialPackingName = list.get(i).getMaterialPackingName();
			String sdk_ID = list.get(i).getSDK_ID();
			String phase = list.get(i).getPHASE();
			String materialSpecName = list.get(i).getMaterialSpecName();
			//			float materialQuantity = list.get(i).getMaterialQuantity();
			//重新获取下数量，通过sql查询取数
			String SLsQL = "SELECT m.MATERIALPACKINGNAME,m.MATERIALQUANTITY,"
					+ "DECODE(NVL(m.MATERIALQUANTITY2,0), 0 ,m.MATERIALQUANTITY,m.MATERIALQUANTITY2) MATERIALQUANTITY2  FROM MATERIALPACKING m"
					+ " WHERE m.SHIPREQUESTNAME = :SHIPRQEUESTNAME AND MATERIALPACKINGNAME = :MATERIALPACKINGNAME ";
			Map<String, Object> hashMap2 = new HashMap<String,Object> ();
			hashMap2.put("SHIPRQEUESTNAME", shipRequestName);
			hashMap2.put("MATERIALPACKINGNAME", materialPackingName);
			String materialQuantity2 = list.get(i).getMaterialQuantity2() + "";
			List<Map<String, Object>> queryForList3 = IDMFrameServiceProxy.getSqlTemplate().queryForList(SLsQL, hashMap2);
			if (queryForList3 != null && queryForList3.size() > 0) {
				materialQuantity2 = queryForList3.get(0).get("MATERIALQUANTITY2").toString();
			} else {
				throw new CustomException("单据" + shipRequestName + "库存"  + materialPackingName  + "绑定关系不存在");
			}
			//循环单据查询过帐表
			String pcSql = "SELECT SHIPREQUESTNAME, SHIPREQUESTDETAILNAME,\r\n" + 
					"	m.MATERIALSPECNAME ,\r\n" + 
					"	m.PHASE ,\r\n" + 
					"	m.SDK_ID ,\r\n" + 
					"	m.REQUESTQUANTITY ,\r\n" + 
					"	m.ASSIGNEDQUANTITY ,\r\n" + 
					"	(m.REQUESTQUANTITY - nvl(m.SALEOUTQUANTITY, 0) ) SALEOUTQUANTITY\r\n" + 
					"FROM\r\n" + 
					"	MATERIALSHIPREQUESTDETAIL m\r\n" + 
					"WHERE\r\n" + 
					"	m.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" + 
//					"	AND (:SDK_ID IS NULL\r\n" +
//					"		OR SDK_ID = :SDK_ID)\r\n" +
//					"	AND (:PHASE IS NULL\r\n" +
//					"		OR PHASE = :PHASE)\r\n" +
					"	AND SHIPREQUESTNAME = :SHIPREQUESTNAME  ORDER BY TO_NUMBER(SHIPREQUESTDETAILNAME) ASC ";
			//AND (m.REQUESTQUANTITY - nvl(m.SALEOUTQUANTITY, 0)) > 0
			hashMap.put("MATERIALSPECNAME", materialSpecName);
			hashMap.put("SDK_ID", sdk_ID);
			hashMap.put("PHASE", phase);
			List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(pcSql, hashMap);
			BigDecimal pcQty = new BigDecimal(materialQuantity2);//批次数量
			BigDecimal temp;
			for (int j = 0; j < queryForList2.size(); j++) {
				String sq = queryForList2.get(j).get("SALEOUTQUANTITY").toString();//剩余发货数量
				String shipRequestDetailName = queryForList2.get(j).get("SHIPREQUESTDETAILNAME").toString();//行项
				BigDecimal saleQty = new BigDecimal(sq);//需要发货数量
				//修复Bug:发货刚好满足数量时，如有超发则不会写入SUB表.zd.2024.9.26
				//bugfix:销售出库  lxh 20240927
				if (pcQty.compareTo(BigDecimal.ZERO) == 0 
						 || (saleQty.compareTo(BigDecimal.ZERO) == 0  && (j != queryForList2.size() - 1) )
						) {
					continue;
				}
				int compareTo = saleQty.compareTo(pcQty);//大于等于0则批次全发,小于0则需要拆分
				int compareToZero=saleQty.compareTo(BigDecimal.ZERO);
				if (compareTo >= 0) {
					untils.fahuoSplit(materialPackingName, shipRequestName, shipRequestDetailName, pcQty);
					break;
				} else {//需要拆行
					if( (j != queryForList2.size() - 1) &&  compareToZero>0) {//判断是否是最后一行，不是最后一行就不允许超发，发货数量等于请求数量即拆
						untils.fahuoSplit(materialPackingName, shipRequestName, shipRequestDetailName, saleQty);
						pcQty = pcQty.subtract(saleQty);
						continue;
					} else {
						untils.fahuoSplit(materialPackingName, shipRequestName, shipRequestDetailName, pcQty);
						break;
					}
				}
			}
			materialPackingKeyList.add(materialPackingName);
//			MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",materialPackingName);
//			arrayList.add(materialPackingKey);
		}
		//判断会有多少个仓库发货，不同仓库需要分开走账
		StringBuffer cklx = new StringBuffer();
//		cklx.append("SELECT DISTINCT m.ERPLOCATION FROM MATERIALPACKING m WHERE m.MATERIALPACKINGNAME IN ");
//		cklx.append("SELECT m.MATERIALQUANTITY,m.MATERIALQUANTITY2,m.MATERIALPACKINGNAME,m.CHARGE,m.SITENAME FROM MATERIALPACKING m WHERE m.MATERIALPACKINGNAME IN ");
		cklx.append("SELECT m.MATERIALPACKINGNAME,m.SITENAME FROM MATERIALPACKING m WHERE m.MATERIALPACKINGNAME IN ");
		String str = "";
		str += "(";
		for (String materialPacking : materialPackingKeyList) {
			str += "'" + materialPacking + "',";
		}
		str += "'')";
//		Object[] bindSet = {};
//		List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(cklx.toString() + str, hashMap);

		// 查询MATERIALQUANTITY 与 MATERIALQUANTITY2是否相等，如果相等则未修改数量，不相等则修改数量
		List<Map<String, Object>> packingQueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(cklx.toString() + str, hashMap);
//		List<MaterialPacking> qtyQueryForList = MaterialPackingServiceProxy.getMaterialPackingService().select(cklx.toString() + str, bindSet,MaterialPacking.class);

		String billCode = "";
		switch (shipRequestType) {
			case "NLCC" : //STO调拨出库

				billCode=TransferStockOut_NLCC(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
				//makeEventInfo = eventInfoUtil.makeEventInfo("CompleteShipRequest", eventUser, "CompleteShipRequest:"+shipRequestType);
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;

			case "01"://生产领料
			case "02"://生产补料
				// 判断是否是现场仓
				String IsSql = "SELECT m.ERPRECEIVELOCATION,m.ERPLOCATION,m.ERPRECEIVEFACTORY FROM MATERIALSHIPREQUESTDETAIL m WHERE  SHIPREQUESTNAME = :SHIPREQUESTNAME";
				Map<String,Object> bpc=new HashMap<>();
				bpc.put("SHIPREQUESTNAME", shipRequestName);
				List<Map<String,Object>> receiveList = IDMFrameServiceProxy.getSqlTemplate().queryForList(IsSql, bpc);
				String ERPRECEIVELOCATION = receiveList.get(0).get("ERPRECEIVELOCATION") == null ? "" : receiveList.get(0).get("ERPRECEIVELOCATION").toString();
				String ERPLOCATION = receiveList.get(0).get("ERPLOCATION") == null ? "" : receiveList.get(0).get("ERPLOCATION").toString();
				String ERPRECEIVEFACTORY = receiveList.get(0).get("ERPRECEIVEFACTORY") == null ? "" : receiveList.get(0).get("ERPRECEIVEFACTORY").toString();

				//判断是否为MES需要二次接收的物料
				String sqlc=" SELECT e.ENUMVALUE FROM MES_ENUMDEFVALUE e \r\n" +
						" WHERE e.ENUMNAME ='ConfirmReceiveArea'\r\n" +
						" AND e.ENUMVALUE in(\r\n" +
						" SELECT p.AREANAME FROM MES_PRODUCTREQUEST p \r\n" +
						" WHERE p.PRODUCTREQUESTNAME =(SELECT m.RECEIVEPRODUCTIONORDERNUMBER FROM MATERIALSHIPREQUESTDETAIL m \r\n" +
						" WHERE m.SHIPREQUESTNAME=:SHIPREQUESTNAME AND RECEIVEPRODUCTIONORDERNUMBER IS NOT NULL AND ROWNUM=1))";

				List<Map<String,Object>> src=IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlc, bpc);


				//MES需要接收，则只抛送数据。 或如果是现场仓,先移到现场仓
				if(src.size()>0 || !ERPRECEIVELOCATION.isEmpty()) {

//					 拆条码，查询MATERIALQUANTITY 与 MATERIALQUANTITY2是否相等，如果相等则未修改了数量，不相等则修改了数量
//					 如果已修改数量，则进行拆条码
//					for (int z = 0; z < packingQueryForList.size(); z++) {
//						String MATERIALPACKINGNAME = packingQueryForList.get(z).get("MATERIALPACKINGNAME").toString();
//						Object[] bindSet = {MATERIALPACKINGNAME};
//						String qtysql = "WHERE MATERIALPACKINGNAME = ?";
//						List<MaterialPacking> qtyQueryForList = MaterialPackingServiceProxy.getMaterialPackingService().select(qtysql, bindSet);
//
//						String qty = String.valueOf(qtyQueryForList.get(z).getMaterialQuantity());
//
//						String qty2 = new Double(qtyQueryForList.get(z).getMaterialQuantity2()) == null ? "0" : String.valueOf(qtyQueryForList.get(z).getMaterialQuantity2());
//						String materialPackingName = qtyQueryForList.get(z).getMaterialPackingName();
//						String charge = qtyQueryForList.get(z).getCharge();
//						BigDecimal bigQty = new BigDecimal(qty);
//						// 已拆数量
//						BigDecimal bigQty2 = new BigDecimal(qty2);
//
//						// 如果已拆数量不等于原数量，并且已拆数量为0(未拆分数量)
//						if(bigQty.compareTo(bigQty2) != 0 && !"0".equals(qty2)) {
//							// 剩余数量
//							BigDecimal restQty = bigQty.subtract(bigQty2);
//
//							// 生成新的拆分条码
//							String stringBigQty2 = bigQty2.toString();
//							String newPalletNo = CodeGenerator.packingGenerateCode(materialPackingName,stringBigQty2,charge);
//
//							// 写入拆分的新条码，更新原数据库
//							List<MaterialPacking> boxList = new ArrayList<MaterialPacking>();
////							MaterialPackingKey materialpackingKey = new MaterialPackingKey();
//							MaterialPacking materialpackingNew = new MaterialPacking();
////							materialpackingKey.setSiteName("SDK");
////							materialpackingKey.setMaterialPackingName(newPalletNo);
////							materialpackingNew.setKey(materialpackingKey);
//
//							// 复制一个当前的原标签数据
//							materialpackingNew.setKey(qtyQueryForList.get(z).getKey());
////							MaterialPacking materialpackingNew = qtyQueryForList.get(z);
//							// 设置新为标签和数量
//							materialpackingNew.setMaterialPackingName(newPalletNo);
//							// bigQty2 转double类型
//							materialpackingNew.setMaterialQuantity(bigQty2.doubleValue());
//							boxList.add(qtyQueryForList.get(z));
//							boxList.add(materialpackingNew);
//							boolean moveTransformOut = packingChargeSplit(boxList, eventUser);
//							if (moveTransformOut != true) {
//								throw new RuntimeException("修改数量拆分失败");
//							}
//
//							// 原条码扣减拆出的数量，用剩余数量更新原条码，条码不变，数量更新
//							SetEventInfo setEventInfo = new SetEventInfo();
//							Map<String, Object> pakHashMap = new HashMap<>();
//							pakHashMap.put("materialQuantity", restQty.doubleValue());
//
//							EventInfo eventInfo = new EventInfoUtil().makeEventInfo("备货出库拆分修改数量拆分条码", eventUser, "修改数量拆分条码");
//							setEventInfo.setUserColumns(pakHashMap);
//							MaterialPackingServiceProxy.getMaterialPackingService().setEvent(new MaterialPackingKey( queryForList.get(z).get("SITENAME").toString(),
//									queryForList.get(z).get("MATERIALPACKINGNAME").toString()), eventInfo, setEventInfo);
//
//							// 删除被拆分的原条码数据，用拆分出的新条码覆盖，进行后面的过账，现场仓过账只过账已拆分出的数量，剩余数量的原条码不过账
//							boolean deletePacking = materialPackingKeyList.remove(MATERIALPACKINGNAME);
//							if (deletePacking) {
//								materialPackingKeyList.add(newPalletNo);
//							}
//
//						}
//
//					}


					//new MESServiceImpl().NeedReceiveMaterialInfoSend_NEW(shipRequestName, "SDK", materialPackingKeyList);
					billCode="Y";
					String sqlZK=" SELECT\r\n" +
							"	t.MATERIALSPECNAME,m.MATERIALPACKINGNAME,\r\n" +
							"	t.ERPFACTORY,\r\n" +
							"	t.ERPLOCATION,\r\n" +
							"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
							"	t.UNIT,\r\n" +
							"	m1.COSTCENTER,\r\n" +
							"	m1.SHIPREQUESTNAME,\r\n" +
							"	m1.SHIPREQUESTDETAILNAME,\r\n" +
							"	m1.ERPRECEIVELOCATION,\r\n" +
							"	m1.ERPRECEIVEFACTORY,\r\n" +
							"	m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
							"	CASE WHEN m1.SENDSALESORDER IS NOT NULL THEN 'E' ELSE '' END SOBKZ,\r\n" +
							"	(\r\n" +
							"	SELECT\r\n" +
							"		RECEIVEPRODUCTIONORDERNUMBER\r\n" +
							"	FROM\r\n" +
							"		MATERIALSHIPREQUESTDETAIL m5\r\n" +
							"	WHERE\r\n" +
							"		m5.SHIPREQUESTNAME =:SHIPREQUESTNAME\r\n" +
							"		AND m5.MATERIALSPECNAME = t.MATERIALSPECNAME\r\n" +
							"	ORDER BY\r\n" +
							"		m5.USEDATE,\r\n" +
							"		m5.SHIPREQUESTDETAILNAME FETCH FIRST ROW ONLY)RECEIVEPRODUCTIONORDERNUMBER,\r\n" +
							"	SUM(m.QTY) AS MATERIALQUANTITY\r\n" +
							" FROM \r\n" +
							"		 MATERIALPACKINGSUB m\r\n" +
							" LEFT JOIN MATERIALPACKING T ON \r\n" +
							"		 m.MATERIALPACKINGNAME = T.MATERIALPACKINGNAME\r\n" +
							"	AND m.RECEIVEREQUESTNAME = T.SHIPREQUESTNAME\r\n" +
							" LEFT JOIN MATERIALSHIPREQUESTDETAIL M1  \r\n" +
							"		  ON \r\n" +
							"		 m.RECEIVEREQUESTNAME = M1.SHIPREQUESTNAME\r\n" +
							"	AND m.RECEIVEREQUESTDETAILNAME = M1.SHIPREQUESTDETAILNAME\r\n" +
							" LEFT JOIN MATERIALSHIPREQUEST M2  \r\n" +
							"		  ON \r\n" +
							"		 M1.SHIPREQUESTNAME = M2.SHIPREQUESTNAME\r\n" +
							" WHERE \r\n" +
							"		 T.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" +
							"	AND T.SITENAME = :SITENAME\r\n" +
							"	AND T.STOCKSTATE = 'Stocked'\r\n" +
							"	AND T.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
							" GROUP BY\r\n" +
							"	t.MATERIALSPECNAME,\r\n" +
							"	t.ERPFACTORY,\r\n" +
							"	t.ERPLOCATION,\r\n" +
							"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
							"	t.UNIT,\r\n" +
							"	m1.COSTCENTER,\r\n" +
							"	m1.SHIPREQUESTNAME,m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
							"	m1.SHIPREQUESTDETAILNAME,\r\n" +
							"	m1.ERPRECEIVELOCATION,\r\n" +
							"	m1.ERPRECEIVEFACTORY,m.MATERIALPACKINGNAME";

					// 转库到线边仓
//					String sqlZK = "SELECT b.MATERIALPACKINGNAME FROM MATERIALPACKINGSUB b WHERE b.MATERIALPACKINGNAME IN (:BOXLIST)";
					Map<String, Object> hashMapZK = new HashMap<String, Object>();
					hashMapZK.put("BOXLIST", materialPackingKeyList);
					hashMapZK.put("SHIPREQUESTNAME", shipRequestName);
					hashMapZK.put("SITENAME", siteName);
					List<Map<String, Object>> ZKqueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlZK.toString(), hashMapZK);

					for (int z = 0; z < ZKqueryForList.size(); z++) {
						String materialPackingName = ZKqueryForList.get(z).get("MATERIALPACKINGNAME").toString();
						materialPackingKeyList.add(materialPackingName);
						MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",materialPackingName);
						arrayList.add(materialPackingKey);
					}
					billCode = UnSourcedChangeLocation(arrayList,ERPRECEIVEFACTORY,
							ERPRECEIVELOCATION, "", eventUser, commitDate );

					List<MaterialPackingKey> mpKeyList = new ArrayList<MaterialPackingKey> ();
					SetEventInfo setEventInfo = new SetEventInfo();
					for (MaterialPackingKey materialPacking : arrayList) {
						MaterialPackingKey key = materialPacking;
						mpKeyList.add(key);
					}

					Map<String, Object> hashMapPack = new HashMap<String,Object> ();
					hashMapPack.put("erpLocation", ERPRECEIVELOCATION == null  ? "" : ERPRECEIVELOCATION);
					hashMapPack.put("erpFactory",  ERPRECEIVEFACTORY == null  ? "" : ERPRECEIVEFACTORY);
					hashMapPack.put("locationName", "");
					hashMapPack.put("lastEventUser", eventUser);
					hashMapPack.put("unDoID", billCode);
					setEventInfo.setUserColumns(hashMapPack);
					MaterialPackingServiceProxy.getMaterialPackingService().setEvent(mpKeyList, makeEventInfo, setEventInfo);
					//抛送MES数据
					untils.sendMaterialInfoToMES_NEW2(shipRequestName, "", materialPackingKeyList,billCode,eventUser);

					WlySaveUnDoInfo( materialPackingKeyList,billCode,commitDate);
				}
				else
				{

					billCode=ProductionStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
					if (billCode.isEmpty()) {
						throw new CustomException("SAP过账异常！");
					}
//					billCode=toSAPService.ProductionStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList);
					//抛送MES数据
					untils.sendMaterialInfoToMES_NEW(shipRequestName, "", materialPackingKeyList,billCode,eventUser);
					//billCode = NCWServiceImpl.SaleOut2(shipRequestName, "SDK", eventUser,materialPackingKeyList,"");
					untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				}
				break;

			case "06"://成本中心领料
				//				billCode=toSAPService.orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList);
				billCode=orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
				//抛送MES数据
				untils.sendMaterialInfoToMES_NEW(shipRequestName, "", materialPackingKeyList,billCode,eventUser);
				//更新MES管芯数据
				new MESServiceImpl().updateDurable_ByCostcenter(shipRequestName, "SDK", eventUser, materialPackingKeyList);
				//billCode = NCWServiceImpl.SaleOut2(shipRequestName, "SDK", eventUser,materialPackingKeyList,"");
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			case "09"://研发领料
//				billCode=toSAPService.orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList);
//				billCode=orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList);
//				//更新MES管芯数据
//				new MESServiceImpl().updateDurable_ByCostcenter(shipRequestName, "SDK", eventUser, materialPackingKeyList);
//				//billCode = NCWServiceImpl.SaleOut2(shipRequestName, "SDK", eventUser,materialPackingKeyList,"");
//				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			case "ZLF0"://销售出库确认 （单据类型ZLF0~ZLF8） SD023(销售回传)
			case "ZLF1":
			case "ZLF2":
			case "ZLF3":
			case "ZLF4":
			case "ZLF5":
			case "ZLF6":
			case "ZLF7":
			case "ZLF8":
//				billCode=toSAPService.saleStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList);
				billCode=saleStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			case "08":    // 委外
			case "OEM":  // 委外
//				billCode=toSAPService.oemStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList);
				billCode=oemStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList, commitDate);
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			default :
				break;
		}
		return billCode;
	}

	public boolean packingChargeSplit(List<MaterialPacking> boxList, String user) {

		String condition="";
		String newBoxId1 = boxList.get(1).getMaterialPackingName();
//		String pc = "('" + materialpackingname + "')";
		String sql = "SELECT 1 FROM MATERIALPACKING WHERE MATERIALPACKINGNAME IN (:MATERIALPACKINGNAME1)";
		Map<String, Object> hashMap2 = new HashMap<String,Object> ();
		hashMap2.put("MATERIALPACKINGNAME1", newBoxId1);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap2);
		List<MaterialPacking> MaterialPackingList = null;
		if (queryForList != null && queryForList.size() > 0) {
			String[] bindSet={newBoxId1};
			condition="WHERE MATERIALPACKINGNAME IN (?)";
			MaterialPackingList = MaterialPackingServiceProxy.getMaterialPackingService().select(condition, bindSet);
		}
		//解析条码,根据远条码信息更新目标条码数量和库存状态
		SetEventInfo setEventInfo = new SetEventInfo();
		EventInfoUtil eventInfoUtil = new EventInfoUtil();
		makeEventInfo = eventInfoUtil.makeEventInfo("ChargeSplit", user, "ChargeSplit" );
		//找到原来标签的相关信息
//		String receiveTime = "";
//		String makeDate = "";
//		String expireDate = "";
		String holdState = "";
		String oqaResult = "";
		String oqaResultState = "";
		String packingGrade = "";
		String trueGg = "";
		String erpFactory = "";
		String erpLocation = "";
		String locationName = "";
		String shelfName = "";
		String areaName = "";
		String durableName = "";;
		String CAREER_ASSISTANCE = "";
		String SALESPERSON = "";
		String CUSTOMNO = "";
		String DEVIATION = "";
		String DIAMETER = "";
		String WIDTH = "";
		String WEIGHT = "";
		String PRODUCTLINE = "";
		String UNIT = "";
		String MJPC = "";
		String BUSINESS_UNIT = "";

		String MATERIALSPECNAME = "";
//	    String SUBPACKINGQUANTITY = "";
//	    String MATERIALCREATEQUANTITY = "";
		String CHARGE = "";
		String SDK_ID = "";
		String PHASE = "";
		String RECEIVEREQUESTNAME = "";
		String RECEIVEREQUESTDETAILNAME = "";
		String REMARK = "";
		String SHIPREQUESTNAME = "";
		String SHIPREQUESTDETAILNAME = "";
		String MATERIALTYPE = "";
		String RECEIVEACTNO = "";
		String UNDOID = "";
		String	FIFOFLAG = "";

		String ypcSql = "SELECT M.RECEIVETIME,M.MAKEDATE,M.EXPIRINGDATE,M.HOLDSTATE,M.OQARESULTSTATE ,\r\n" +
				"		M.OQARESULT ,M.PACKINGGRADE ,M.TRUEGG ,M.ERPFACTORY ,M.ERPLOCATION ,M.LOCATIONNAME ,\r\n" +
				"		M.SHELFNAME ,M.MATERIALTYPE,M.AREANAME ,M.DURABLENAME ,M.CAREER_ASSISTANCE ,M.SALESPERSON ,M.CUSTOMNO,M.SDK_ID,M.PHASE,M.SHIPREQUESTNAME,M.SHIPREQUESTDETAILNAME,M.RECEIVEREQUESTNAME,M.RECEIVEREQUESTDETAILNAME,"
				+ "     M.DEVIATION ,M.DIAMETER ,M.WIDTH ,M.WEIGHT ,M.PRODUCTLINE,UNIT ,MJPC,BUSINESS_UNIT,M.MATERIALSPECNAME, M.SUBPACKINGQUANTITY, M.MATERIALCREATEQUANTITY, M.MATERIALQUANTITY, M.CHARGE, M.REMARK" +
				"		FROM MATERIALPACKING M WHERE M.MATERIALPACKINGNAME  = :MATERIALPACKINGNAME AND M.CHARGE  = :CHARGE ";
		Map<String, Object> hashMap4 = new HashMap<String,Object> ();
		hashMap4.put("MATERIALPACKINGNAME", boxList.get(0).getMaterialPackingName());
		hashMap4.put("CHARGE", boxList.get(0).getCharge());
		List<Map<String, Object>> queryForList3 = IDMFrameServiceProxy.getSqlTemplate().queryForList(ypcSql, hashMap4);

		if (queryForList3 != null && queryForList3.size() > 0) {
			Map<String, Object> map = queryForList3.get(0);
			holdState = map.get("HOLDSTATE") == null ? "N" : map.get("HOLDSTATE").toString();
			oqaResult = map.get("OQARESULT") == null ? "" : map.get("OQARESULT").toString();
			oqaResultState = map.get("OQARESULTSTATE") == null ? "END" : map.get("OQARESULTSTATE").toString();
			packingGrade = map.get("PACKINGGRADE") == null ? "" : map.get("PACKINGGRADE").toString();
			trueGg = map.get("TRUEGG") == null ? "" : map.get("TRUEGG").toString();
			erpFactory = map.get("ERPFACTORY") == null ? "" : map.get("ERPFACTORY").toString();
			erpLocation = map.get("ERPLOCATION") == null ? "" : map.get("ERPLOCATION").toString();
			locationName = map.get("LOCATIONNAME") == null ? "" : map.get("LOCATIONNAME").toString();
			shelfName = map.get("SHELFNAME") == null ? "" : map.get("SHELFNAME").toString();
			areaName = map.get("AREANAME") == null ? "" : map.get("AREANAME").toString();
			durableName = map.get("DURABLENAME") == null ? "" : map.get("DURABLENAME").toString();
			CAREER_ASSISTANCE = map.get("CAREER_ASSISTANCE") == null ? "" : map.get("CAREER_ASSISTANCE").toString();
			SALESPERSON = map.get("SALESPERSON") == null ? "" : map.get("SALESPERSON").toString();
			CUSTOMNO = map.get("CUSTOMNO") == null ? "" : map.get("CUSTOMNO").toString();
			DEVIATION = map.get("DEVIATION") == null ? "" : map.get("DEVIATION").toString();
			DIAMETER = map.get("DIAMETER") == null ? "" : map.get("DIAMETER").toString();
			WIDTH = map.get("WIDTH") == null ? "" : map.get("WIDTH").toString();
			WEIGHT = map.get("WEIGHT") == null ? "" : map.get("WEIGHT").toString();
			PRODUCTLINE = map.get("PRODUCTLINE") == null ? "" : map.get("PRODUCTLINE").toString();
			UNIT = map.get("UNIT") == null ? "" : map.get("UNIT").toString();
			MJPC = map.get("MJPC") == null ? "" : map.get("MJPC").toString();
			BUSINESS_UNIT = map.get("BUSINESS_UNIT") == null ? "" : map.get("BUSINESS_UNIT").toString();
			MATERIALSPECNAME = map.get("MATERIALSPECNAME") == null ? "" : map.get("MATERIALSPECNAME").toString();
//	         SUBPACKINGQUANTITY = map.get("SUBPACKINGQUANTITY") == null ? "" : map.get("SUBPACKINGQUANTITY").toString();
//	         MATERIALCREATEQUANTITY = map.get("MATERIALCREATEQUANTITY") == null ? "" : map.get("MATERIALCREATEQUANTITY").toString();
			CHARGE = map.get("CHARGE") == null ? "" : map.get("CHARGE").toString();
			SDK_ID = map.get("SDK_ID") == null ? "" : map.get("SDK_ID").toString();
			PHASE = map.get("PHASE") == null ? "" : map.get("PHASE").toString();
			REMARK = map.get("REMARK") == null ? "" : map.get("REMARK").toString();
			SHIPREQUESTNAME = map.get("SHIPREQUESTNAME") == null ? "" : map.get("SHIPREQUESTNAME").toString();
			SHIPREQUESTDETAILNAME = map.get("SHIPREQUESTDETAILNAME") == null ? "" : map.get("SHIPREQUESTDETAILNAME").toString();

			MATERIALTYPE = map.get("MATERIALTYPE") == null ? "" : map.get("MATERIALTYPE").toString();
			RECEIVEACTNO = map.get("RECEIVEACTNO") == null ? "" : map.get("RECEIVEACTNO").toString();
			UNDOID = map.get("UNDOID") == null ? "" : map.get("UNDOID").toString();
			FIFOFLAG = map.get("FIFOFLAG") == null ? "" : map.get("FIFOFLAG").toString();

		}

		if (MaterialPackingList != null && MaterialPackingList.size() > 0) {
			throw new RuntimeException("拆分批次的标签已存在！");
		}else {
//			创建标签1
			CreateInfo createInfo1 = new CreateInfo();
			createInfo1.setSiteName("SDK");
			createInfo1.setMaterialPackingName(newBoxId1);// 新生成的box id
			createInfo1.setMaterialPackingType("Box");
			createInfo1.setContentMaterialType("");

			Map<String, Object> bindMap = new HashMap<String, Object>();
			bindMap.put("MaterialSpecName", MATERIALSPECNAME);
			bindMap.put("PackingGrade", packingGrade);
			bindMap.put("oqaResult", oqaResult);
			bindMap.put("oqaResultState", oqaResultState);
			bindMap.put("SubPackingQuantity",boxList.get(1).getMaterialQuantity());
			bindMap.put("MaterialCreateQuantity",boxList.get(1).getMaterialQuantity());
			bindMap.put("MaterialQuantity", boxList.get(1).getMaterialQuantity());// 数量
			bindMap.put("PackingState", "Released");
			bindMap.put("OldPackingState", "Released");
			bindMap.put("stockState", "Stocked");
			bindMap.put("OldStockState", "Created");
			bindMap.put("HoldState", holdState);
			bindMap.put("LocationName", locationName);
			bindMap.put("charge",CHARGE);
			bindMap.put("MaterialProcessGroupName","");
			bindMap.put("LastEventTimeKey",TimeStampUtil.getCurrentEventTimeKey());

			bindMap.put("MakeDate", queryForList3.get(0).get("MAKEDATE"));
			bindMap.put("expiringDate", queryForList3.get(0).get("EXPIRINGDATE"));
			bindMap.put("ReceiveTime", queryForList3.get(0).get("RECEIVETIME"));
			bindMap.put("unit", queryForList3.get(0).get("UNIT"));
			bindMap.put("CUSTOMNO", queryForList3.get(0).get("CUSTOMNO"));

			bindMap.put("ReceiveUser", makeEventInfo.getEventUser());
			bindMap.put("ErpLocation", erpLocation);
			bindMap.put("ErpFactory", erpFactory);
			bindMap.put("locationName", locationName);
			bindMap.put("StockInType", "Normal");
			bindMap.put("SDK_ID", SDK_ID);
			bindMap.put("PHASE", PHASE);
			bindMap.put("remark", REMARK);

			bindMap.put("shipRequestName", SHIPREQUESTNAME);
			bindMap.put("shipRequestDetailName",SHIPREQUESTDETAILNAME);

			bindMap.put("ReceiveRequestName", RECEIVEREQUESTNAME);
			bindMap.put("ReceiveRequestDetailName",RECEIVEREQUESTDETAILNAME);
			bindMap.put("materialType", MATERIALTYPE);
			bindMap.put("receiveActNo", RECEIVEACTNO);
			bindMap.put("unDoID", UNDOID);
			bindMap.put("fifoFlag", FIFOFLAG);

			bindMap.put("CAREER_ASSISTANCE", CAREER_ASSISTANCE);//业助信息
			bindMap.put("BUSINESS_UNIT", BUSINESS_UNIT);//业务
			bindMap.put("SALESPERSON", SALESPERSON);//事业部
			bindMap.put("MJPC", MJPC);//母卷批次
			bindMap.put("truegg", trueGg);
			bindMap.put("durableName", durableName);
			bindMap.put("areaName", areaName);
			bindMap.put("PRODUCTLINE", PRODUCTLINE);
			bindMap.put("WIDTH", WIDTH);
			bindMap.put("DEVIATION", DEVIATION);
			bindMap.put("WEIGHT", WEIGHT);
			bindMap.put("DIAMETER", DIAMETER);
			bindMap.put("shelfName", shelfName);
			bindMap.put("split", "Y");
			createInfo1.setUserColumns(bindMap);

			MaterialPackingServiceProxy.getMaterialPackingService().create(makeEventInfo, createInfo1);

			//更新原标签批次的库位状态为出库
//			MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",boxList.get(0).getMaterialPackingName());
//			Map<String, Object> hashMap3 = new HashMap<String,Object> ();
//			hashMap3.put("stockState", "StockOut");
//			setEventInfo.setUserColumns(hashMap3);
//			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
		}
		return true;
	}

	//无来源转库过账-转现场仓 （311）
	public String UnSourcedChangeLocation(List<MaterialPackingKey> all,
										 String aimFactory,
										 String aimERPlocation,
										 String aimLocationName,
										 String user, String commitDate) throws Exception {
		String rcode;
		String undoId = "";
		String rmsg = null;
		StringBuffer sql = new StringBuffer();
		sql
				.append("  	SELECT   ").append(SystemPropHelper.CR)
				.append("       MP.MATERIALSPECNAME,   ").append(SystemPropHelper.CR)
				.append("       MP.ERPFACTORY ,   ").append(SystemPropHelper.CR)
				.append("       :AIMFACTORY AS UMWRK_RECIEVEFACTORY,   ").append(SystemPropHelper.CR)
				.append("       MP.ERPLOCATION,   ").append(SystemPropHelper.CR)
				.append("       :AIMERPLOCATION AS UMLGO_RECIEVEERPLOCATION,   ").append(SystemPropHelper.CR)
				.append("       MP.MATERIALQUANTITY ,   ").append(SystemPropHelper.CR)
				.append("       MP.UNIT,   ").append(SystemPropHelper.CR)
				.append("       :AIMLOCATIONNAME AS NULL_RECIVELOCAITONNAME   ").append(SystemPropHelper.CR)
				.append("   FROM   ").append(SystemPropHelper.CR)
				.append("  	    MATERIALPACKING MP   ").append(SystemPropHelper.CR)
				.append("   WHERE   ").append(SystemPropHelper.CR)
				.append("  	    MP.MATERIALPACKINGNAME IN    ").append(SystemPropHelper.CR);
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("AIMFACTORY", aimFactory);
		hashMap.put("AIMLOCATIONNAME", aimLocationName);
		hashMap.put("AIMERPLOCATION", aimERPlocation);
		String str = "";
		str += "(";
		for (MaterialPackingKey materialPackingKey : all) {
			str += "'" + materialPackingKey.getMaterialPackingName() + "',";
		}
		str += "'')";
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql.toString() + str, hashMap);
		if (queryForList == null || queryForList.size() < 1) {
			return "";
		}
		try {

			JSONObject sendData = new JSONObject(true);
			JSONObject header = new JSONObject(true);
			sendData.put("HEAD", header);
			JSONObject body = new JSONObject(true);
			sendData.put("BODY", body);
			JSONArray itemArray = new JSONArray();

            /*
             HEAD
             */
			header.put("INTF_ID", "MM067");
			String uniqueID = UUID.randomUUID().toString();
			header.put("REQ_KEYID", uniqueID);
			header.put("SRC_MSGID", uniqueID);
			header.put("SRC_SYSTEM", "WMS");
			header.put("DEST_SYSTEM", "SAP");
			header.put("REQUSER", user);

            /*
             BODY
             */
			body.put("ITEM", itemArray);
			body.put("ITEMID", uniqueID);
			//过账日期
			SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
			Date date = inputFormat.parse(commitDate);
			String outputDate = outputFormat.format(date);
			body.put("BUDAT", outputDate);
			//凭证日期
			body.put("BLDAT", outputDate);
			//凭证抬头文本
			body.put("BKTXT", uniqueID);
			//用户名
			body.put("USNAM", user);

			for (Map<String, Object> mm : queryForList) {

				JSONObject item = new JSONObject(true);
				// MATNR 发货物料号
				item.put("MATNR", mm.get("MATERIALSPECNAME"));
				//UMMAT 收货物料
				item.put("UMMAT", mm.get("MATERIALSPECNAME"));
				// WERKS 发货工厂
				item.put("WERKS", mm.get("ERPFACTORY"));
				// LGORT 发货库存地点
				item.put("LGORT", mm.get("ERPLOCATION"));
				//UMWRK 收货工厂
				item.put("UMWRK", mm.get("UMWRK_RECIEVEFACTORY"));
				//UMLGO 收货库存地点
				item.put("UMLGO", mm.get("UMLGO_RECIEVEERPLOCATION"));
				//MENGE 数量
				item.put("MENGE", mm.get("MATERIALQUANTITY"));
				//MENGE 单位
				item.put("MEINS", mm.get("UNIT"));
				//BWART 移动类型
				item.put("BWART", "311");
//                //ZLLORDER 自定义领料单号
//                item.put("ZLLORDER", mm.get("SHIPREQUESTNAME"));
//                //ZLLITEM 自定义领料单行号
//                item.put("ZLLITEM", mm.get("SHIPREQUESTDETAILNAME"));

				itemArray.add(item);

			}
//			log.info("SendTOSAP >>>>" + sendData);

			String sapreturn = toSAPMessageUtil.sendHttpPost(toSAPMessageUtil.materialChangeLocationUrl, "", sendData.toJSONString());
			org.json.JSONObject receiveJsonObject = new org.json.JSONObject(sapreturn);
			org.json.JSONObject returnJsonObject = (org.json.JSONObject) receiveJsonObject.get("RETURN");
			rcode = returnJsonObject.get("STATUS").toString();
			rmsg = returnJsonObject.get("MSGTXT").toString();

			if ("S".equals(rcode)) {
				undoId = returnJsonObject.get("MBLNR").toString()+"_"+returnJsonObject.get("MJAHR").toString();//将物料凭证号与凭证年度拼在一起
			}
			//将log写到表里
			UUID uuid = UUID.randomUUID();
			ErpMessageLog erplog = new ErpMessageLog();
			erplog.setEventUser("");
			erplog.setServerName("WmsToErp");
			erplog.setEventName("无来源转库-311");
			erplog.setId(uuid.toString());
			erplog.setInterfaceTime(TimeStampUtil.getCurrentTime(TimeStampUtil.FORMAT_DEFAULT));
			erplog.setMessageId(UUID.randomUUID().toString());
			erplog.setSendMsg(sendData.toJSONString());
			erplog.setSendMsg2(sendData.toJSONString());
			erplog.setReturnMsg2(sapreturn);
			erplog.setResultCode(rcode);
			MessageLogUtil.writeMessageLog(erplog);
		} catch (Exception e) {
//			log.info(e.getMessage(), e);
			throw new RuntimeException("SAP返回" + rmsg);
		}
		if (!"S".equals(rcode)) {
			throw new RuntimeException("SAP返回" + rmsg);
		}
		return undoId;
	}

	public void WlySaveUnDoInfo( List<String> list,String undoid,String commitDate) {

		// 更新事件名
		// 此SQL语句用于更新物料包装的最后事件名称、评论和时间
		String sqlu="UPDATE MATERIALPACKING m SET m.LASTEVENTNAME=:LASTEVENTNAME,m.LASTEVENTCOMMENT =:LASTEVENTCOMMENT,m.LASTEVENTTIME =SYSDATE \r\n" +
				" WHERE m.SITENAME =:SITENAME AND m.MATERIALPACKINGNAME in (:BOXLIST)";
		Map<String,Object> bpu = new HashMap<String,Object> ();
		bpu.put("LASTEVENTNAME", makeEventInfo.getEventName());
		bpu.put("LASTEVENTCOMMENT", makeEventInfo.getEventComment());
		bpu.put("SITENAME", "SDK");
		bpu.put("BOXLIST", list);
		IDMFrameServiceProxy.getSqlTemplate().update(sqlu, bpu);

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

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

	}


	/**
	 * 备份并清除特定发货请求的物料包装子记录
	 *
	 * 此方法首先将与特定发货请求关联的物料包装子记录备份到历史记录表中，
	 * 然后从当前表中删除这些记录，为新的过账操作做准备
	 *
	 * @param shipRequestName 发货请求的名称，用于标识需要备份和清除的记录
	 */
	private void backupMaterialPackingSub(String shipRequestName) {
	    // 备份过账条码表的信息，并删除
	    String bfSql = "INSERT\r\n" +
	            "    INTO\r\n" +
	            "    MATERIALPACKINGSUBHISTORY T (T.MATERIALPACKINGNAME,\r\n" +
	            "    T.RECEIVEREQUESTNAME,\r\n" +
	            "    T.RECEIVEREQUESTDETAILNAME,\r\n" +
	            "    T.QTY)\r\n" +
	            "SELECT\r\n" +
	            "    M.MATERIALPACKINGNAME,\r\n" +
	            "    M.RECEIVEREQUESTNAME,\r\n" +
	            "    M.RECEIVEREQUESTDETAILNAME,\r\n" +
	            "    M.QTY\r\n" +
	            "FROM\r\n" +
	            "    MATERIALPACKINGSUB M\r\n" +
	            "WHERE\r\n" +
	            "    M.RECEIVEREQUESTNAME = :SHIPREQUESTNAME";
	    String scSql = "DELETE FROM MATERIALPACKINGSUB WHERE RECEIVEREQUESTNAME  = :SHIPREQUESTNAME";
	    HashMap<String, Object> hashMap = new HashMap<>();
	    hashMap.put("SHIPREQUESTNAME", shipRequestName);
	    IDMFrameServiceProxy.getSqlTemplate().update(bfSql, hashMap);
	    IDMFrameServiceProxy.getSqlTemplate().update(scSql, hashMap);
	}
	//重写STO业务销售交货单过账。zd.2024.8.14
	public String TransferStockOut_NLCC(String shipRequestName, String siteName, String user,List<String> boxList, String commitDate) throws Exception {
		String rcode;
		String undoId = "";
		String rmsg = null;
		String sql="    SELECT    \r\n" +
				"            count(*) AS ZROL,    \r\n" +
				"            m1.SHIPREQUESTDETAILNAME ,    \r\n" +
				"            t.ERPLOCATION,          \r\n" +
				"            sum(m.QTY)MATERIALQUANTITY          \r\n" +
				"            FROM \r\n" +
				"		 MATERIALPACKINGSUB m \r\n" +
				"		 LEFT JOIN MATERIALPACKING T ON \r\n" +
				"		 m.MATERIALPACKINGNAME = T.MATERIALPACKINGNAME AND m.RECEIVEREQUESTNAME = T.SHIPREQUESTNAME \r\n" +
				"		 LEFT JOIN MATERIALSHIPREQUESTDETAIL M1  \r\n" +
				"		  ON \r\n" +
				"		 m.RECEIVEREQUESTNAME = M1.SHIPREQUESTNAME \r\n" +
				"		 AND m.RECEIVEREQUESTDETAILNAME = M1.SHIPREQUESTDETAILNAME \r\n" +
				"		 LEFT JOIN MATERIALSHIPREQUEST M2  \r\n" +
				"		  ON \r\n" +
				"		 M1.SHIPREQUESTNAME = M2.SHIPREQUESTNAME \r\n" +
				"		 WHERE \r\n" +
				"		 T.SHIPREQUESTNAME = :SHIPREQUESTNAME \r\n" +
				"		 AND T.SITENAME = :SITENAME  \r\n" +
				"		  AND T.STOCKSTATE = 'Stocked' AND   T.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
				"		  GROUP BY m1.SHIPREQUESTDETAILNAME ,    t.ERPLOCATION";

		Map<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		hashMap.put("SITENAME", siteName);
		hashMap.put("BOXLIST", boxList);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql.toString(), hashMap);
		try {

			JSONObject sendData = new JSONObject(true);
			JSONObject header = new JSONObject(true);
			sendData.put("HEAD", header);
			JSONObject body = new JSONObject(true);
			sendData.put("BODY", body);
			JSONArray itemArray = new JSONArray();

            /*
             HEAD
             */
			header.put("DEST_SYSTEM", "SAP");
			header.put("INTF_ID", "SD023");
			header.put("SRC_SYSTEM", "WMS");
			String uniqueID = UUID.randomUUID().toString();
			header.put("SRC_MSGID", uniqueID);
			header.put("BACKUP1", uniqueID);
			header.put("BACKUP2", uniqueID);



            /*
             BODY
             */
			//交货单号
			body.put("VBELN", shipRequestName);

			SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
			Date date = inputFormat.parse(commitDate);
			String outputDate = outputFormat.format(date);
			//实际过账日期
			body.put("WADAT_IST", outputDate);//yyyyMMdd
			//发送日期
			body.put("Z_SENDDATE", outputDate);
			//操作类型（过账）
			body.put("Z_OPTION", "1");

			if (queryForList.size() >= 1) {
				for (Map<String, Object> mm : queryForList) {
					JSONObject item = new JSONObject(true);

					//交货单行号
					item.put("POSNR", mm.get("SHIPREQUESTDETAILNAME"));
					//交货数量
					item.put("PIKMG", mm.get("MATERIALQUANTITY"));
					//库存地点
					item.put("LGORT", mm.get("ERPLOCATION"));
					//实际发货卷数
					item.put("Z_ROL", mm.get("ZROL"));
					//实际发货平方米
					item.put("Z_SQUE", mm.get("MATERIALQUANTITY"));

					itemArray.add(item);
				}
				body.put("ITEMS", itemArray);
			}else {
				for (Map<String, Object> mm : queryForList) {

					//交货单行号
					body.put("POSNR", mm.get("SHIPREQUESTDETAILNAME"));
					//交货数量
					body.put("PIKMG", mm.get("MATERIALQUANTITY")); //SUM
					//库存地点
					body.put("LGORT", mm.get("ERPLOCATION"));
					//实际发货卷数
					body.put("Z_ROL", mm.get("ZROL"));   //COUNT
					//实际发货平方米
					body.put("Z_SQUE", mm.get("MATERIALQUANTITY"));

				}
			}

//			log.info("SendTOSAP >>>>" + sendData);
			String sapreturn = toSAPMessageUtil.sendHttpPost(toSAPMessageUtil.DNUrl, "", sendData.toJSONString());
			org.json.JSONObject receiveJsonObject = new org.json.JSONObject(sapreturn);

			//销售交货单过账不返回物料凭证,且RETURN为JsonArray
			org.json.JSONObject returnJsonObject = (org.json.JSONObject) receiveJsonObject.get("RETURN");
			rcode = returnJsonObject.get("STATUS").toString();
			rmsg = returnJsonObject.get("MSGTXT").toString();

			if ("S".equals(rcode)) {
				undoId = "S";
			}
			//将log写到表里
			ErpMessageLog erplog = new ErpMessageLog();
			erplog.setEventUser("");
			erplog.setServerName("WmsToErp");
			erplog.setEventName("调拨出库(NLCC)");
			erplog.setInterfaceTime(TimeStampUtil.getCurrentTime(TimeStampUtil.FORMAT_DEFAULT));
			erplog.setMessageId(UUID.randomUUID().toString());
			erplog.setSendMsg(sendData.toJSONString());
			erplog.setSendMsg2(sendData.toJSONString());
			erplog.setReturnMsg2(sapreturn);
			erplog.setResultCode(rcode);
			MessageLogUtil.writeMessageLog(erplog);
		} catch (Exception e) {
//			log.info(e.getMessage(), e);
			throw new RuntimeException("SAP返回" + rmsg);
		}
		if (!"S".equals(rcode)) {
			throw new RuntimeException("SAP返回" + rmsg);
		}
		return undoId;
	}

	/**
	 * 
	 * @param list
	 * @param undoid
	 * @param makeEventInfo
	 */
	public void SaveUnDoInfo(List<String> list,  String undoid, EventInfo makeEventInfo,String shipReuqestType,String erpLocations) {
		String updateUndoInfo = "UPDATE MATERIALPACKING T SET T.unDoID = :unDoID WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME AND T.ERPLOCATION = :ERPLOCATION";
		for (String s : list) {
			Map<String,Object> map = new HashMap<String,Object> ();
			map.put("MATERIALPACKINGNAME", s);
			map.put("ERPLOCATION", erpLocations);
			map.put("unDoID", undoid);
			IDMFrameServiceProxy.getSqlTemplate()
			.update(updateUndoInfo, map);
		}
		
		//更新Box状态，如果数量发货数量和条码数量相同则全部出货，小于则更新条码数量，取消单据解绑
		for (String s : list) {
			String insertUnDOMaterialPackingInfo = "insert into BS_MATERIALPACKINGUNDOINFO (unDoID,preundoid,UNDODETAILNAME,undoidstate,UNDOIDDATE,"
					+ "SITENAME, MATERIALPACKINGNAME, MATERIALPACKINGTYPE, CONTENTMATERIALTYPE,"
					+ "MATERIALTYPE, MATERIALSPECNAME, SUPERMATERIALPACKINGNAME, PACKINGGRADE, SUBPACKINGQUANTITY, MATERIALCREATEQUANTITY,"
					+ "MATERIALQUANTITY, PACKINGSTATE, OLDPACKINGSTATE, STOCKSTATE, OLDSTOCKSTATE, HOLDSTATE, AREANAME, LOCATIONNAME,"
					+ "DURABLENAME, MATERIALPROCESSGROUPNAME, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER,"
					+ "LASTEVENTCOMMENT, CREATETIME, CREATEUSER, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME,"
					+ "MAKEDATE, RECEIVEACTNO, STOCKINTYPE, RECEIVETIME, RECEIVEUSER, SHIPREQUESTNAME, SHIPREQUESTDETAILNAME, SHIPTIME,"
					+ "SHIPUSER, REASONCODETYPE, OQARESULTSTATE, OQARESULT, ERPLOCATION, ERPFACTORY, REASONCODE, CHARGE, SUPPLIERNAME,ETCSTATE,ERPEVENTTIME,NPFLAG)"
					+ "select unDoID,'' as preundoid,UNDODETAILNAME,'Created' as undoidstate,:UNDOIDDATE AS UNDOIDDATE," //
					+ "SITENAME, MATERIALPACKINGNAME, MATERIALPACKINGTYPE, CONTENTMATERIALTYPE,"
					+ "MATERIALTYPE, MATERIALSPECNAME, SUPERMATERIALPACKINGNAME, PACKINGGRADE, SUBPACKINGQUANTITY, MATERIALCREATEQUANTITY,"
					+ "CASE WHEN MATERIALQUANTITY2 > 0 THEN  MATERIALQUANTITY2 ELSE MATERIALQUANTITY END, PACKINGSTATE, OLDPACKINGSTATE, STOCKSTATE, OLDSTOCKSTATE, HOLDSTATE, AREANAME, LOCATIONNAME,"
					+ "DURABLENAME, MATERIALPROCESSGROUPNAME, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER,"
					+ "LASTEVENTCOMMENT, CREATETIME, CREATEUSER, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME,"
					+ "MAKEDATE, RECEIVEACTNO, STOCKINTYPE, RECEIVETIME, RECEIVEUSER, SHIPREQUESTNAME, SHIPREQUESTDETAILNAME, SHIPTIME,"
					+ "SHIPUSER, REASONCODETYPE, OQARESULTSTATE, OQARESULT, ERPLOCATION, ERPFACTORY, REASONCODE, CHARGE, SUPPLIERNAME,ETCSTATE,ERPEVENTTIME,NPFLAG"
					+ " from MATERIALPACKING  "
					+ " where unDoID=:UNDOID AND MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> bindMap_insert = new HashMap<String, Object>();
			bindMap_insert.put("UNDOIDDATE", "");
			bindMap_insert.put("UNDOID", undoid);
			bindMap_insert.put("MATERIALPACKINGNAME", s);
			IDMFrameServiceProxy.getSqlTemplate()
			.update(insertUnDOMaterialPackingInfo, bindMap_insert);
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", s);
			hashMap.put("ERPLOCATION", erpLocations);
			hashMap.put("unDoID", undoid);
			if ("4Y".equals(shipReuqestType)) {
				String insertSql = "INSERT\r\n" + 
						"							INTO\r\n" + 
						"							MATERIALPACKING T (SITENAME , \r\n" + 
						"							MATERIALPACKINGNAME , \r\n" + 
						"							MATERIALPACKINGTYPE , \r\n" + 
						"							CONTENTMATERIALTYPE , \r\n" + 
						"							MATERIALTYPE , \r\n" + 
						"							MATERIALSPECNAME , \r\n" + 
						"							SUPERMATERIALPACKINGNAME, \r\n" + 
						"							PACKINGGRADE , \r\n" + 
						"							SUBPACKINGQUANTITY, \r\n" + 
						"							MATERIALCREATEQUANTITY, \r\n" + 
						"							PACKINGSTATE , \r\n" + 
						"							OLDPACKINGSTATE , \r\n" + 
						"							STOCKSTATE , \r\n" + 
						"							OLDSTOCKSTATE , \r\n" + 
						"							HOLDSTATE , \r\n" + 
						"							AREANAME , \r\n" + 
						"							LOCATIONNAME , \r\n" + 
						"							MATERIALPROCESSGROUPNAME , \r\n" + 
						"							LASTEVENTNAME , \r\n" + 
						"							LASTEVENTTIMEKEY , \r\n" + 
						"							LASTEVENTTIME, \r\n" + 
						"							LASTEVENTUSER , \r\n" + 
						"							LASTEVENTCOMMENT, \r\n" + 
						"							CREATETIME, \r\n" + 
						"							CREATEUSER , \r\n" + 
						"							RECEIVEREQUESTNAME , \r\n" + 
						"							RECEIVEREQUESTDETAILNAME , \r\n" + 
						"							MAKEDATE, \r\n" + 
						"							RECEIVEACTNO , \r\n" + 
						"							RECEIVETIME, \r\n" + 
						"							RECEIVEUSER , \r\n" + 
						"							SHIPREQUESTNAME , \r\n" + 
						"							SHIPREQUESTDETAILNAME , \r\n" + 
						"							SHIPTIME, \r\n" + 
						"							SHIPUSER , \r\n" + 
						"							REASONCODETYPE , \r\n" + 
						"							REASONCODE , \r\n" + 
						"							ERPLOCATION , \r\n" + 
						"							ETCSTATE , \r\n" + 
						"							ERPFACTORY , \r\n" + 
						"							STOCKINTYPE , \r\n" + 
						"							OQARESULTSTATE , \r\n" + 
						"							OQARESULT , \r\n" + 
						"							CHARGE , \r\n" + 
						"							SUPPLIERNAME , \r\n" + 
						"							UNDOID, \r\n" + 
						"							UNDODETAILNAME, \r\n" + 
						"							ERPEVENTTIME, \r\n" + 
						"							NPFLAG , \r\n" + 
						"							ERPSTATE, \r\n" + 
						"							MESSAGEID, \r\n" + 
						"							OQAMETHOD , \r\n" + 
						"							REMARK, \r\n" + 
						"							IMPROVMENTFLAG , \r\n" + 
						"							EXPIRINGDATE, \r\n" + 
						"							PROCESSSTATE , \r\n" + 
						"							ETCOUTQUANTITY, \r\n" + 
						"							MESRECEIVEFLAG , \r\n" + 
						"							MESRECEIVETIME, \r\n" + 
						"							RECEIPTOR , \r\n" + 
						"							DURABLENAME, \r\n" + 
						"							OEMOQAFLAG , \r\n" + 
						"							MATERIALQUANTITY, \r\n" + 
						"							FIFOFLAG , \r\n" + 
						"							SUPPLIERCHARGE, \r\n" + 
						"							SDK_ID , \r\n" + 
						"							SDKTRUEID , \r\n" + 
						"							MATURATIONINTIME, \r\n" + 
						"							MATURATIONOUTTIME, \r\n" + 
						"							PHASE, \r\n" + 
						"							ORG , \r\n" + 
						"							LKMATERIALPACKINGNAME, \r\n" + 
						"							SPECIALSTATE, \r\n" + 
						"							UNIT, \r\n" + 
						"							SHOPNAME, \r\n" + 
						"							MATURATIONTIME, \r\n" + 
						"							MATURATIONCYCLE, \r\n" + 
						"							SHELFNAME, \r\n" + 
						"							EXCEPTIONHANDLING, \r\n" + 
						"							ISCHECK, \r\n" + 
						"							PREERPLOCATION, \r\n" + 
						"							FUNIT, \r\n" + 
						"							FQTY, \r\n" + 
						"							TRUEGG, \r\n" + 
						"							BUSINESS_UNIT, \r\n" + 
						"							CAREER_ASSISTANCE, \r\n" + 
						"							SALESPERSON, \r\n" + 
						"							CUSTOMNO, \r\n" + 
						"							CUSTOMCHARGE, \r\n" + 
						"							PRELOCATIONNAME, \r\n" + 
						"							MATERIALQUANTITY2, \r\n" + 
						"							DURABLETYPE)\r\n" + 
						"						SELECT\r\n" + 
						"							m.SITENAME , \r\n" + 
						"							SYS_GUID() , \r\n" + 
						"							MATERIALPACKINGTYPE , \r\n" + 
						"							CONTENTMATERIALTYPE , \r\n" + 
						"							MATERIALTYPE , \r\n" + 
						"							m.MATERIALSPECNAME , \r\n" + 
						"							SUPERMATERIALPACKINGNAME, \r\n" + 
						"							PACKINGGRADE , \r\n" + 
						"							SUBPACKINGQUANTITY, \r\n" + 
						"							MATERIALCREATEQUANTITY, \r\n" + 
						"							PACKINGSTATE , \r\n" + 
						"							OLDPACKINGSTATE , \r\n" + 
						"							STOCKSTATE , \r\n" + 
						"							OLDSTOCKSTATE , \r\n" + 
						"							HOLDSTATE , \r\n" + 
						"							AREANAME , \r\n" + 
						"							'' , \r\n" + 
						"							MATERIALPROCESSGROUPNAME , \r\n" + 
						"							LASTEVENTNAME , \r\n" + 
						"							LASTEVENTTIMEKEY , \r\n" + 
						"							LASTEVENTTIME, \r\n" + 
						"							LASTEVENTUSER , \r\n" + 
						"							LASTEVENTCOMMENT, \r\n" + 
						"							CREATETIME, \r\n" + 
						"							CREATEUSER , \r\n" + 
						"							RECEIVEREQUESTNAME , \r\n" + 
						"							RECEIVEREQUESTDETAILNAME , \r\n" + 
						"							MAKEDATE, \r\n" + 
						"							RECEIVEACTNO , \r\n" + 
						"							RECEIVETIME, \r\n" + 
						"							RECEIVEUSER , \r\n" + 
						"							'' , \r\n" + 
						"							'' , \r\n" + 
						"							SHIPTIME, \r\n" + 
						"							SHIPUSER , \r\n" + 
						"							m.REASONCODETYPE , \r\n" + 
						"							m.REASONCODE , \r\n" + 
						"							m2.ERPRECEIVELOCATION , \r\n" + 
						"							ETCSTATE , \r\n" + 
						"							m2.ERPRECEIVEFACTORY , \r\n" + 
						"							STOCKINTYPE , \r\n" + 
						"							OQARESULTSTATE , \r\n" + 
						"							OQARESULT , \r\n" + 
						"							m.CHARGE , \r\n" + 
						"							SUPPLIERNAME , \r\n" + 
						"							UNDOID, \r\n" + 
						"							UNDODETAILNAME, \r\n" + 
						"							ERPEVENTTIME, \r\n" + 
						"							NPFLAG , \r\n" + 
						"							ERPSTATE, \r\n" + 
						"							MESSAGEID, \r\n" + 
						"							OQAMETHOD , \r\n" + 
						"							m.REMARK, \r\n" + 
						"							IMPROVMENTFLAG , \r\n" + 
						"							EXPIRINGDATE, \r\n" + 
						"							PROCESSSTATE , \r\n" + 
						"							ETCOUTQUANTITY, \r\n" + 
						"							MESRECEIVEFLAG , \r\n" + 
						"							MESRECEIVETIME, \r\n" + 
						"							RECEIPTOR , \r\n" + 
						"							DURABLENAME, \r\n" + 
						"							OEMOQAFLAG , \r\n" + 
						"							MATERIALQUANTITY2, \r\n" + 
						"							FIFOFLAG , \r\n" + 
						"							SUPPLIERCHARGE, \r\n" + 
						"							m.SDK_ID , \r\n" + 
						"							SDKTRUEID , \r\n" + 
						"							MATURATIONINTIME, \r\n" + 
						"							MATURATIONOUTTIME, \r\n" + 
						"							m.PHASE, \r\n" + 
						"							ORG , \r\n" + 
						"							LKMATERIALPACKINGNAME, \r\n" + 
						"							SPECIALSTATE, \r\n" + 
						"							m.UNIT, \r\n" + 
						"							SHOPNAME, \r\n" + 
						"							MATURATIONTIME, \r\n" + 
						"							MATURATIONCYCLE, \r\n" + 
						"							SHELFNAME, \r\n" + 
						"							EXCEPTIONHANDLING, \r\n" + 
						"							ISCHECK, \r\n" + 
						"							PREERPLOCATION, \r\n" + 
						"							FUNIT, \r\n" + 
						"							FQTY, \r\n" + 
						"							TRUEGG, \r\n" + 
						"							m.BUSINESS_UNIT, \r\n" + 
						"							m.CAREER_ASSISTANCE, \r\n" + 
						"							m.SALESPERSON, \r\n" + 
						"							CUSTOMNO, \r\n" + 
						"							CUSTOMCHARGE, \r\n" + 
						"							PRELOCATIONNAME, \r\n" + 
						"							0, \r\n" + 
						"							DURABLETYPE\r\n" + 
						"						FROM\r\n" + 
						"							MATERIALPACKING m\r\n" + 
						"						LEFT JOIN MATERIALSHIPREQUESTDETAIL m2 ON\r\n" + 
						"							m.SHIPREQUESTNAME = m2.SHIPREQUESTNAME\r\n" + 
						"							AND m.MATERIALSPECNAME = m2.MATERIALSPECNAME\r\n" + 
						"						WHERE\r\n" + 
						"							m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME AND m.ERPLOCATION = :ERPLOCATION";
				IDMFrameServiceProxy.getSqlTemplate().update(insertSql, hashMap);
			}
			
			MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",s);
			MaterialPacking selectByKey = MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(materialPackingKey);
			Double materialQuantity = selectByKey.getMaterialQuantity();
			Double materialQuantity2 = selectByKey.getMaterialQuantity2();
			String erpLocation2 = selectByKey.getErpLocation();
			if (erpLocations.equals(erpLocation2)) {
				BigDecimal fmaterialQuantity = new BigDecimal(materialQuantity);
				BigDecimal fmaterialQuantity2 = new BigDecimal(materialQuantity2);
				if (fmaterialQuantity.compareTo(fmaterialQuantity2) > 0) {
					Map<String, Object> hashMap2 = new HashMap<String,Object> ();
					hashMap2.put("materialQuantity", fmaterialQuantity.subtract(fmaterialQuantity2) );
					hashMap2.put("materialQuantity2", 0);
					hashMap2.put("shipRequestName", "");
					hashMap2.put("shipRequestDetailName", "");
					SetEventInfo setEventInfo = new SetEventInfo();
					setEventInfo.setUserColumns(hashMap2);
					MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				} else {
					Map<String, Object> hashMap2 = new HashMap<String,Object> ();
					hashMap2.put("stockState", "StockOut");
					//				hashMap.put("shipRequestName", "");
					//				hashMap.put("shipRequestDetailName", "");
					SetEventInfo setEventInfo = new SetEventInfo();
					setEventInfo.setUserColumns(hashMap2);
					MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				}
			}
		}

	}
	
//	public static void main(String[] args) {
//		BigDecimal fmaterialQuantity = new BigDecimal("1063.1");
//		BigDecimal fmaterialQuantity2 = new BigDecimal("500");
//		System.out.println(fmaterialQuantity.subtract(fmaterialQuantity2) + "");
//	}

	/**
	 * 备货方法
	 * @param materialPacking
	 * @param shipReuqestName
	 * @param sdk_id
	 * @param type
	 * @param siteName
	 * @param user
	 * @throws CustomException
	 */
	@Transactional
	public void Reserve (MaterialPacking materialPacking, String shipReuqestName,
						 String materialSpecName, String sdk_id, String phase,
						 String type, String siteName, String user, String materialQuantity2, String fifoFlag) throws CustomException {
		String materialPackingName = materialPacking.getMaterialPackingName();//条码
		Double materialQuantity = materialPacking.getMaterialQuantity();
		//			float materialQuantity2 = materialPacking.getMaterialQuantity2();
		String charge = materialPacking.getCharge();//批次
		EventInfo makeEventInfo;
		//1.绑定
		//校验Box有没有被其他订单绑过
		if ("Assign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("AssiginShipRequest", user, "AssiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(invoice) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				} else if (!"".equals(invoice)  && invoice.equals(shipReuqestName)) {//备过货的不做处理
					return;
				}
				
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
				SetEventInfo setEventInfo = new SetEventInfo();
				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
				//更新实际实际出库数量到库存表
				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
				hashMap2.put("shipRequestName", shipReuqestName);
				hashMap2.put("fifoFlag", fifoFlag);
				setEventInfo.setUserColumns(hashMap2);
				hashMap2.put("fifoFlag", fifoFlag);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY + :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ " AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND ROWNUM = 1  ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", materialQuantity);
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}

		} else 	if ("Dessign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("DessiginShipRequest", user, "DessiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(shipReuqestName) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
				SetEventInfo setEventInfo = new SetEventInfo();
				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
				//更新实际实际出库数量到库存表
				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
				hashMap2.put("shipRequestName", "");
				hashMap2.put("fifoFlag", "");
				hashMap2.put("shipRequestDetailName", "");
				hashMap2.put("materialQuantity2", "0");
				setEventInfo.setUserColumns(hashMap2);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				//更新备货数量
				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY - :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ "AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND ROWNUM = 1  ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", materialQuantity2);
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}
		}
	}
	
	
	@Transactional
	public void ReserveByPallet (MaterialPacking materialPacking, String shipReuqestName,
								 String materialSpecName, String sdk_id, String phase,
								 String type, String siteName, String user, String materialQuantity2, String fifoFlag) throws CustomException {
		String materialPackingName = materialPacking.getMaterialPackingName();//条码
		String materialQuantity = materialQuantity2;
		//			float materialQuantity2 = materialPacking.getMaterialQuantity2();
		String charge = materialPacking.getCharge();//批次
		EventInfo makeEventInfo;
		//1.绑定
		//校验Box有没有被其他订单绑过
		if ("Assign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("AssiginShipRequest", user, "AssiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(invoice) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				} else if (!"".equals(invoice)  && invoice.equals(shipReuqestName)) {//备过货的不做处理
					return;
				}
				
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
				SetEventInfo setEventInfo = new SetEventInfo();
				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
				//更新实际实际出库数量到库存表
				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
				hashMap2.put("shipRequestName", shipReuqestName);
				hashMap2.put("fifoFlag", fifoFlag);
				hashMap2.put("materialQuantity2", materialQuantity);
				setEventInfo.setUserColumns(hashMap2);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY + :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ " AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND ROWNUM = 1  ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", materialQuantity);
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}

		} else 	if ("Dessign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("DessiginShipRequest", user, "DessiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(shipReuqestName) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
				SetEventInfo setEventInfo = new SetEventInfo();
				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
				//更新实际实际出库数量到库存表
				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
				hashMap2.put("shipRequestName", "");
				hashMap2.put("fifoFlag", "");
				hashMap2.put("shipRequestDetailName", "");
				hashMap2.put("materialQuantity2", "0");
				setEventInfo.setUserColumns(hashMap2);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				//更新备货数量
				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY - :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ "AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND ROWNUM = 1  ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", materialQuantity2);
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}
		}
	}
	
	/**
	 * 备货方法3 MATERIALRECEIVEREQUEST表
	 * @param materialPacking
	 * @param sdk_id
	 * @param phase
	 * @param type
	 * @param siteName
	 * @param user
	 * @throws CustomException
	 */
	@Transactional
	public void Reserve3 (MaterialPacking materialPacking, String receiveRequestName, String receiveRequestDetailName,
						  String materialSpecName, String sdk_id, String phase,
						  String type, String siteName, String user, String materialQuantity2, String fifoFlag) throws CustomException {
		String materialPackingName = materialPacking.getMaterialPackingName();//条码
		Double materialQuantity = materialPacking.getMaterialQuantity();
		//			float materialQuantity2 = materialPacking.getMaterialQuantity2();
		String charge = materialPacking.getCharge();//批次
		EventInfo makeEventInfo;
		//1.绑定
		//校验Box有没有被其他订单绑过
		if ("Assign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("AssiginShipRequest", user, "AssiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(invoice) && !invoice.equals(receiveRequestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
				SetEventInfo setEventInfo = new SetEventInfo();
				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
				//更新实际实际出库数量到库存表
				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
				hashMap2.put("shipRequestName", receiveRequestName);
				hashMap2.put("shipRequestDetailName", receiveRequestDetailName);
				hashMap2.put("fifoFlag", fifoFlag);
				setEventInfo.setUserColumns(hashMap2);
				hashMap2.put("fifoFlag", fifoFlag);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				String updateSql= " UPDATE MATERIALRECEIVEREQUESTDETAIL m SET m.RECEIVEDQUANTITY = m.RECEIVEDQUANTITY + :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ " AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND ROWNUM = 1  ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", materialQuantity);
				bindMap.put("SITENAME", siteName);
				bindMap.put("RECEIVEREQUESTNAME", receiveRequestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}

		} else 	if ("Dessign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("DessiginShipRequest", user, "DessiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(receiveRequestName) && !invoice.equals(receiveRequestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
				SetEventInfo setEventInfo = new SetEventInfo();
				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
				//更新实际实际出库数量到库存表
				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
				hashMap2.put("shipRequestName", "");
				hashMap2.put("fifoFlag", "");
				hashMap2.put("shipRequestDetailName", "");
				hashMap2.put("materialQuantity2", "0");
				setEventInfo.setUserColumns(hashMap2);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				//更新备货数量
				String updateSql= " UPDATE MATERIALRECEIVEREQUESTDETAIL m SET m.RECEIVEDQUANTITY = m.RECEIVEDQUANTITY - :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ "AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND ROWNUM = 1  ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", materialQuantity2);
				bindMap.put("SITENAME", siteName);
				bindMap.put("RECEIVEREQUESTNAME", receiveRequestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}
		}
	}

	/**
	 * 备货方法2.为不影响其他功能，新复制原Reserve方法进行修改。zd.2024.9.10
	 * @param materialPacking
	 * @param shipReuqestName
	 * @param sdk_id
	 * @param type
	 * @param siteName
	 * @param user
	 * @throws CustomException
	 */
	@Transactional
	public void Reserve2 (MaterialPacking materialPacking, String shipReuqestName,
						  String materialSpecName, String sdk_id, String phase,
						  String type, String siteName, String user, String materialQuantity2, String fifoFlag) throws CustomException {
		String materialPackingName = materialPacking.getMaterialPackingName();//条码
//		float materialQuantity = materialPacking.getMaterialQuantity();
//		float mqty2 = materialPacking.getMaterialQuantity2();
//		String charge = materialPacking.getCharge();//批次
		EventInfo makeEventInfo;
		//1.绑定
		//校验Box有没有被其他订单绑过
		if ("Assign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("AssiginShipRequest", user, "AssiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME,MATERIALQUANTITY,SDK_ID,PHASE FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(invoice) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}
				phase = queryForList.get(0).get("PHASE") == null ? "" :queryForList.get(0).get("PHASE").toString();
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
//				SetEventInfo setEventInfo = new SetEventInfo();
//				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
//				//更新实际实际出库数量到库存表
//				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
//				hashMap2.put("shipRequestName", shipReuqestName);
//				hashMap2.put("fifoFlag", fifoFlag);
//				hashMap2.put("materialQuantity2", queryForList.get(0).get("MATERIALQUANTITY").toString());//先将Box数量记录至materialQuantity2
//				setEventInfo.setUserColumns(hashMap2);
//				hashMap2.put("fifoFlag", fifoFlag);
//				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				hashMap.put("materialQuantity2", queryForList.get(0).get("MATERIALQUANTITY").toString());
				hashMap.put("fifoFlag", fifoFlag);
				hashMap.put("shipRequestName", shipReuqestName);
				hashMap.put("SITENAME", siteName);
				
				String upMkSql = "UPDATE\r\n" + 
						"	MATERIALPACKING m\r\n" + 
						"SET\r\n" + 
						"	m.MATERIALQUANTITY2 = :materialQuantity2,\r\n" + 
						"	m.FIFOFLAG = :fifoFlag,\r\n" + 
						"	m.SHIPREQUESTNAME = :shipRequestName\r\n" + 
						"WHERE\r\n" + 
						"	m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME\r\n" + 
						"	AND m.SITENAME = :SITENAME";
				
				IDMFrameServiceProxy.getSqlTemplate().update(upMkSql, hashMap);
				
				
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY + :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
//						+ " AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) " +
						+ "AND (:PHASE IS NULL OR PHASE = :PHASE) " +
						"AND ROWNUM = 1  ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", queryForList.get(0).get("MATERIALQUANTITY").toString());
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
//				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}

		} else 	if ("Dessign".equals(type)) {
			//解绑根据查询出的materil信息拿到SDKID和阶段
//			sdk_id = materialPacking.getSDK_ID();
//			phase = materialPacking.getPHASE();
			makeEventInfo = new EventInfoUtil().makeEventInfo("DessiginShipRequest", user, "DessiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME,CASE WHEN MATERIALQUANTITY2 > 0 THEN MATERIALQUANTITY2 ELSE MATERIALQUANTITY END MATERIALQUANTITY2,SDK_ID,PHASE FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				
				sdk_id = queryForList.get(0).get("SDK_ID") == null ? "" : queryForList.get(0).get("SDK_ID").toString();
				phase = queryForList.get(0).get("PHASE") == null ? "" : queryForList.get(0).get("PHASE") .toString();
				
				
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(shipReuqestName) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}
							
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
//				SetEventInfo setEventInfo = new SetEventInfo();
//				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
//				//更新实际实际出库数量到库存表
//				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
//				hashMap2.put("shipRequestName", "");
//				hashMap2.put("fifoFlag", "");
//				hashMap2.put("shipRequestDetailName", "");
//				hashMap2.put("materialQuantity2", "0");
//				setEventInfo.setUserColumns(hashMap2);
//				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				hashMap.put("shipRequestName", "");
				hashMap.put("fifoFlag", "");
				hashMap.put("shipRequestDetailName", "");
				hashMap.put("materialQuantity2", "0");
				hashMap.put("SITENAME", siteName);
				String upMkSql = "UPDATE\r\n" + 
						"	MATERIALPACKING m\r\n" + 
						"SET\r\n" + 
						"	m.MATERIALQUANTITY2 = :materialQuantity2,\r\n" + 
						"	m.FIFOFLAG = :fifoFlag,\r\n" + 
						"	m.SHIPREQUESTNAME = :shipRequestName,\r\n" + 
						"	m.SHIPREQUESTDETAILNAME = :shipRequestDetailName\r\n" + 
						"WHERE\r\n" + 
						"	m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME\r\n" + 
						"	AND m.SITENAME = :SITENAME";
				IDMFrameServiceProxy.getSqlTemplate().update(upMkSql, hashMap);
				
				
				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY - :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
//						+ "AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) " +
						+ "AND (:PHASE IS NULL OR PHASE = :PHASE)" +
						"  AND ROWNUM = 1 ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				//bindMap.put("QTY", materialQuantity2);
				bindMap.put("QTY", queryForList.get(0).get("MATERIALQUANTITY2").toString());
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
//				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}
		}
	}
	
	/**
	 * 辅材备货
	 * @param materialPacking
	 * @param shipReuqestName
	 * @param shipRequestDetailName
	 * @param materialSpecName
	 * @param sdk_id
	 * @param phase
	 * @param type
	 * @param siteName
	 * @param user
	 * @param materialQuantity2
	 * @throws CustomException
	 */
	@Transactional
	public void AuxiliaryReserve (MaterialPacking materialPacking, String shipReuqestName, String shipRequestDetailName,
								  String materialSpecName, String sdk_id, String phase,
								  String type, String siteName, String user, String materialQuantity2) throws CustomException {
		String materialPackingName = materialPacking.getMaterialPackingName();//条码
		Double materialQuantity = materialPacking.getMaterialQuantity();
		if(!"".equals(materialQuantity2)) {
			materialQuantity = Double.parseDouble(materialQuantity2);
		}
		String charge = materialPacking.getCharge();//批次
		EventInfo makeEventInfo;
		//1.绑定
		//校验Box有没有被其他订单绑过
		if ("Assign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("AssiginShipRequest", user, "AssiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(invoice) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
				SetEventInfo setEventInfo = new SetEventInfo();
				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
				//更新实际实际出库数量到库存表
				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
				hashMap2.put("shipRequestName", shipReuqestName);
				hashMap2.put("materialQuantity2", materialQuantity);
				setEventInfo.setUserColumns(hashMap2);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY + :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ " AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND m.SHIPREQUESTDETAILNAME = :RECEIVEREQUESTDETAILNAME  ";
				String updateTh = "UPDATE MATERIALRECEIVEREQUESTDETAIL m SET m.RECEIVEDQUANTITY = m.RECEIVEDQUANTITY + :QTY WHERE m.SITENAME = :SITENAME \r\n" + 
						"						  AND m.RECEIVEREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME \r\n" + 
						"						  AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND m.RECEIVEREQUESTDETAILNAME = :RECEIVEREQUESTDETAILNAME ";
				//增加过账条码表记录
				String insertSql = "INSERT INTO MATERIALPACKINGSUB t (t.MATERIALPACKINGNAME,t.RECEIVEREQUESTNAME,t.RECEIVEREQUESTDETAILNAME,t.QTY)\n" +
						"VALUES (:MATERIALPACKINGNAME,:RECEIVEREQUESTNAME,:RECEIVEREQUESTDETAILNAME,:QTY)";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", materialQuantity);
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("RECEIVEREQUESTNAME", shipReuqestName);
				bindMap.put("RECEIVEREQUESTDETAILNAME", shipRequestDetailName);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
				IDMFrameServiceProxy.getSqlTemplate().update(updateTh, bindMap);
				IDMFrameServiceProxy.getSqlTemplate().update(insertSql, bindMap);
			}

		} else 	if ("Dessign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("DessiginShipRequest", user, "DessiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(shipReuqestName) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
				SetEventInfo setEventInfo = new SetEventInfo();
				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
				//更新实际实际出库数量到库存表
				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
				hashMap2.put("shipRequestName", "");
				hashMap2.put("shipRequestDetailName", "");
				hashMap2.put("materialQuantity2", "0");
				setEventInfo.setUserColumns(hashMap2);
				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				//更新备货数量
				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY - :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ "AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND m.SHIPREQUESTDETAILNAME = :RECEIVEREQUESTDETAILNAME  ";
				String updateTh = "UPDATE MATERIALRECEIVEREQUESTDETAIL m SET m.RECEIVEDQUANTITY = m.RECEIVEDQUANTITY - :QTY WHERE m.SITENAME = :SITENAME \r\n" + 
						"						  AND m.RECEIVEREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME \r\n" + 
						"						  AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) AND (:PHASE IS NULL OR PHASE = :PHASE) AND m.RECEIVEREQUESTDETAILNAME = :RECEIVEREQUESTDETAILNAME ";
				String deleteSql = "DELETE FROM MATERIALPACKINGSUB t WHERE t.MATERIALPACKINGNAME = :MATERIALPACKINGNAME AND RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND " +
						"RECEIVEREQUESTDETAILNAME = :RECEIVEREQUESTDETAILNAME";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", materialQuantity2);
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				bindMap.put("RECEIVEREQUESTNAME", shipReuqestName);
				bindMap.put("RECEIVEREQUESTDETAILNAME", shipRequestDetailName);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
				IDMFrameServiceProxy.getSqlTemplate().update(updateTh, bindMap);
				IDMFrameServiceProxy.getSqlTemplate().update(deleteSql, bindMap);
			}
		}

	}
	
	@Transactional
	public  List<Map<String,Object>> ZDBH (String shiprequestname,String user,String erpfactory ) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 将前端传入的参数和存储过程中的参数对应起来
		map.put("V_SHIPREQUESTNAME",shiprequestname);
		map.put("V_ERPFACTORY",erpfactory);
		map.put("V_RESULT","");
		String procedureName = "CALL  GetFifoInventoryForOrder('" + shiprequestname + "', '" + erpfactory + "' )";
		try {
			// 调用存储过程返回参数
			 IDMFrameServiceProxy.getSqlTemplate().getJdbcTemplate().execute(procedureName);
			//查询中间临时表的数据
			String sql = "SELECT T.MATERIALPACKINGNAME,T.MATERIALSPECNAME,T.MATERIALQUANTITY,T.MATERIALQUANTITY2,T.AREANAME ,T.SHELFNAME ,T.SDK_ID ,T.SPECNAME,T.PHASE   ,T.RECEIVETIME ,T.ERPLOCATION, \r\n" + 
					"T.LOCATIONNAME ,T.CHARGE ,T.BHFLG ,T.IS_CHECK \r\n" + 
					"FROM TEMP_FIFOTABLE T WHERE T.MATERIALPACKINGNAME NOT IN (SELECT MATERIALPACKINGNAME FROM MATERIALPACKING M WHERE M.SHIPREQUESTNAME = :SHIPREQUESTNAME)\r\n" + 
					"UNION\r\n" + 
					"(SELECT\r\n" + 
					"	A.MATERIALPACKINGNAME,\r\n" + 
					"	A.MATERIALSPECNAME,\r\n" + 
					"	A.MATERIALQUANTITY,\r\n" + 
					"	CASE\r\n" + 
					"		WHEN MATERIALQUANTITY2 > 0 THEN\r\n" + 
					"                    A.MATERIALQUANTITY2\r\n" + 
					"		ELSE\r\n" + 
					"                    A.MATERIALQUANTITY\r\n" + 
					"	END MATERIALQUANTITY2,\r\n" + 
					"	A.AREANAME,\r\n" + 
					"	A.SHELFNAME,\r\n" + 
					"	A.SDK_ID,\r\n" + 
					"	SS.SPECNAME,\r\n" + 
					"	A.PHASE,\r\n" + 
					"	TO_CHAR(A.MAKEDATE, 'YYYY-MM-DD') RECEIVETIME,\r\n" + 
					"	be.DESCRIPTION ERPLOCATION,\r\n" + 
					"	s.DESCRIPTION LOCATIONNAME,\r\n" + 
					"	A.CHARGE,\r\n" + 
					"	'Y' BHFLG,\r\n" + 
					"	'N' IS_CHECK\r\n" + 
					"FROM\r\n" + 
					"	MATERIALPACKING A\r\n" +
					"	LEFT JOIN MATERIALSHIPREQUESTDETAIL mdl ON\r\n" +
					"	mdl.SHIPREQUESTNAME= A.SHIPREQUESTNAME\r\n" +
					"	AND mdl.MATERIALSPECNAME = A.MATERIALSPECNAME\r\n" +
					"LEFT JOIN SDK_SPEC SS ON\r\n" + 
					"	A.SDK_ID = SS.SDK_ID\r\n" + 
					"LEFT JOIN \r\n" + 
					"	MATERIALSPEC B ON\r\n" + 
					"	A.MATERIALSPECNAME = B.MATERIALSPECNAME\r\n" + 
					"LEFT JOIN BS_ERPLOCATION be ON\r\n" + 
					"	A.ERPLOCATION = be.ERPLOCATIONNAME\r\n" + 
					"LEFT JOIN STORAGESPEC s ON\r\n" + 
					"	A.LOCATIONNAME = s.STORAGENAME\r\n" + 
					"	AND A.ERPLOCATION = s.ERPLOCATION\r\n" + 
					"	AND A.ERPFACTORY = s.ERPFACTORY\r\n" + 
					"WHERE\r\n" + 
					"	1 = 1\r\n" + 
					"	AND A.MATERIALSPECNAME = B.MATERIALSPECNAME\r\n" + 
					"	AND A.SITENAME = B.SITENAME\r\n" + 
					"	AND A.SITENAME =:SITENAME\r\n" + 
					"	AND A.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
					"	AND A.STOCKSTATE = 'Stocked'\r\n" + 
					"	AND A.PACKINGSTATE = 'Released'\r\n" + 
					"	AND A.HOLDSTATE = 'N'\r\n" + 
					"	AND A.PACKINGGRADE IN ('OK', 'OKEY')\r\n" + 
					"	AND A.MATERIALQUANTITY <> '0'\r\n" + 
					"	AND (A.NPFLAG <> 'Y'\r\n" + 
					"		OR A.NPFLAG IS NULL)\r\n" + 
					"	AND (A.ETCSTATE IS NULL\r\n" + 
					"		OR A.ETCSTATE = 'EtcIn')\r\n" + 
					"	AND A.OQARESULTSTATE <> 'PREOQA'\r\n" +
					"	AND (A.ERPLOCATION <> mdl.ERPRECEIVELOCATION OR mdl.ERPRECEIVELOCATION IS NULL)\r\n" +
					"	AND to_char(A.EXPIRINGDATE, 'yyyymmdd') >= to_char(SYSDATE, 'yyyymmdd'))";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("SITENAME", "SDK");
			hashMap.put("SHIPREQUESTNAME", shiprequestname);
			List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);

			//如果是需要判定E库存的话，则进行判定
			queryForList = untils.checkEStore(queryForList,  shiprequestname);

			return queryForList;
		}catch(Throwable e) {
			throw e;
		}
		
	}
	public String ProductionStockOut_NEW(String shipRequestName, String siteName, String user,List<String> boxList,String commitDate) throws Exception {

		String rcode;
		String undoId = "";
		String rmsg = null;
		String sql=" SELECT\r\n" +
				"	t.MATERIALSPECNAME,\r\n" +
				"	t.ERPFACTORY,\r\n" +
				"	t.ERPLOCATION,\r\n" +
				"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
				"	t.UNIT,\r\n" +
				"	m1.COSTCENTER,\r\n" +
				"	m1.SHIPREQUESTNAME,\r\n" +
				"	m1.SHIPREQUESTDETAILNAME,\r\n" +
				"	m1.ERPRECEIVELOCATION,\r\n" +
				"	m1.ERPRECEIVEFACTORY,\r\n" +
				"	m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
				"	CASE WHEN m1.SENDSALESORDER IS NOT NULL THEN 'E' ELSE '' END SOBKZ,\r\n" +
				"	(\r\n" +
				"	SELECT\r\n" +
				"		RECEIVEPRODUCTIONORDERNUMBER\r\n" +
				"	FROM\r\n" +
				"		MATERIALSHIPREQUESTDETAIL m5\r\n" +
				"	WHERE\r\n" +
				"		m5.SHIPREQUESTNAME =:SHIPREQUESTNAME\r\n" +
				"		AND m5.MATERIALSPECNAME = t.MATERIALSPECNAME\r\n" +
				"	ORDER BY\r\n" +
				"		m5.USEDATE,\r\n" +
				"		m5.SHIPREQUESTDETAILNAME FETCH FIRST ROW ONLY)RECEIVEPRODUCTIONORDERNUMBER,\r\n" +
				"	SUM(m.QTY) AS MATERIALQUANTITY\r\n" +
				" FROM \r\n" +
				"		 MATERIALPACKINGSUB m\r\n" +
				" LEFT JOIN MATERIALPACKING T ON \r\n" +
				"		 m.MATERIALPACKINGNAME = T.MATERIALPACKINGNAME\r\n" +
				"	AND m.RECEIVEREQUESTNAME = T.SHIPREQUESTNAME\r\n" +
				" LEFT JOIN MATERIALSHIPREQUESTDETAIL M1  \r\n" +
				"		  ON \r\n" +
				"		 m.RECEIVEREQUESTNAME = M1.SHIPREQUESTNAME\r\n" +
				"	AND m.RECEIVEREQUESTDETAILNAME = M1.SHIPREQUESTDETAILNAME\r\n" +
				" LEFT JOIN MATERIALSHIPREQUEST M2  \r\n" +
				"		  ON \r\n" +
				"		 M1.SHIPREQUESTNAME = M2.SHIPREQUESTNAME\r\n" +
				" WHERE \r\n" +
				"		 T.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" +
				"	AND T.SITENAME = :SITENAME\r\n" +
				"	AND T.STOCKSTATE = 'Stocked'\r\n" +
				"	AND T.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
				" GROUP BY\r\n" +
				"	t.MATERIALSPECNAME,\r\n" +
				"	t.ERPFACTORY,\r\n" +
				"	t.ERPLOCATION,\r\n" +
				"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
				"	t.UNIT,\r\n" +
				"	m1.COSTCENTER,\r\n" +
				"	m1.SHIPREQUESTNAME,m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
				"	m1.SHIPREQUESTDETAILNAME,\r\n" +
				"	m1.ERPRECEIVELOCATION,\r\n" +
				"	m1.ERPRECEIVEFACTORY";
		Map<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		hashMap.put("SITENAME", siteName);
		hashMap.put("BOXLIST", boxList);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql.toString(), hashMap);

		String sqlGetBWART = "SELECT M.SHIPREQUESTDETAILTYPE FROM MATERIALSHIPREQUEST M " +
				"WHERE M.SHIPREQUESTNAME = :SHIPREQUESTNAME AND M.SITENAME = :SITENAME";

		List<Map<String, Object>> checkResultList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlGetBWART, hashMap);
		String bwart = "";
		for(Map<String, Object> map : checkResultList) {
			bwart = MapUtils.getString(map, "SHIPREQUESTDETAILTYPE");
		}
		if(bwart == null || bwart == "") {
			throw new CustomException("ERROR","下发单据时，请务必填入移动类型");
		}

		try {

			JSONObject sendData = new JSONObject(true);
			JSONObject header = new JSONObject(true);
			sendData.put("HEAD", header);
			JSONObject body = new JSONObject(true);
			sendData.put("BODY", body);
			JSONArray itemArray = new JSONArray();

            /*
             HEAD
             */
			header.put("INTF_ID", "MM067");
			String uniqueID = UUID.randomUUID().toString();
			header.put("REQ_KEYID", uniqueID);
			header.put("SRC_MSGID", uniqueID);
			header.put("SRC_SYSTEM", "WMS");
			header.put("DEST_SYSTEM", "SAP");
			header.put("REQUSER", user);

            /*
             BODY
             */
			body.put("ITEM", itemArray);
			body.put("ITEMID", uniqueID);
			SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
			Date date = inputFormat.parse(commitDate);
			String outputDate = outputFormat.format(date);
			//过账日期
			body.put("BUDAT", outputDate);
			//凭证日期
			body.put("BLDAT", outputDate);
			//凭证抬头文本
			body.put("BKTXT", uniqueID);
			//用户名
			body.put("USNAM", user);

			for (Map<String, Object> mm : queryForList) {

				JSONObject item = new JSONObject(true);
				// MATNR 发货物料号
				item.put("MATNR", mm.get("MATERIALSPECNAME"));
				// WERKS 收货工厂
				item.put("WERKS", mm.get("ERPFACTORY"));
				// LGORT 发货库存地点
				item.put("LGORT", mm.get("ERPLOCATION"));
				//MENGE 数量
				item.put("MENGE", mm.get("MATERIALQUANTITY"));
				//MENGE 单位
				item.put("MEINS", mm.get("UNIT"));
//                //KOSTL 成本中心
//                item.put("KOSTL", mm.get("COSTCENTER"));
//                //GRUND 移动原因 (201,202回传)
//                item.put("GRUND", mm.get("SHIPREQUESTDETAILTYPE"));

				if(bwart.equals("261")) {
					//BWART 移动类型
					item.put("BWART", mm.get("SHIPREQUESTDETAILTYPE"));
					//ZLLORDER 自定义领料单号
					item.put("ZLLORDER", mm.get("SHIPREQUESTNAME"));
					//ZLLITEM 自定义领料单行号
					item.put("ZLLITEM", mm.get("SHIPREQUESTDETAILNAME"));
					//AUFNR 订单号
					item.put("AUFNR", mm.get("RECEIVEPRODUCTIONORDERNUMBER"));
				}
				if(bwart.equals("311")) {
					//BWART 移动类型
					item.put("BWART", mm.get("SHIPREQUESTDETAILTYPE"));
					//UMMAT 收货物料
					item.put("UMMAT", mm.get("MATERIALSPECNAME"));//传什么
					//UMLGO 收货库存地点
					item.put("UMLGO", mm.get("ERPRECEIVELOCATION"));
					//UMWRK 收货工厂
					item.put("UMWRK", mm.get("ERPRECEIVEFACTORY"));
				}
				item.put("SOBKZ", mm.get("SOBKZ"));//特殊库存标识
				item.put("MAT_KDAUF", mm.get("SENDSALESORDER"));//发货销售订单号
				item.put("MAT_KDPOS", mm.get("SENDSALESORDERDETAILNO"));//发货销售订单行项

				itemArray.add(item);

			}
//			log.info("SendTOSAP >>>>" + sendData);
			String dataString = sendData.toJSONString();
			String sapreturn = toSAPMessageUtil.sendHttpPost(toSAPMessageUtil.materialChangeLocationUrl, "", sendData.toJSONString());
			org.json.JSONObject receiveJsonObject = new org.json.JSONObject(sapreturn);
			org.json.JSONObject returnJsonObject = (org.json.JSONObject) receiveJsonObject.get("RETURN");
			rcode = returnJsonObject.get("STATUS").toString();
			rmsg = returnJsonObject.get("MSGTXT").toString();

			if ("S".equals(rcode)) {
				undoId = returnJsonObject.get("MBLNR").toString()+"_"+returnJsonObject.get("MJAHR").toString();//将物料凭证号与凭证年度拼在一起
			}
			//将log写到表里
			ErpMessageLog erplog = new ErpMessageLog();
			erplog.setEventUser("");
			erplog.setServerName("WmsToErp");
			erplog.setEventName("生产领料出库261/311");
			erplog.setInterfaceTime(TimeStampUtil.getCurrentTime(TimeStampUtil.FORMAT_DEFAULT));
			erplog.setMessageId(UUID.randomUUID().toString());
			erplog.setSendMsg(sendData.toJSONString());
			erplog.setSendMsg2(sendData.toJSONString());
			erplog.setReturnMsg2(sapreturn);
			erplog.setResultCode(rcode);
			MessageLogUtil.writeMessageLog(erplog);
		} catch (Exception e) {
//			log.info(e.getMessage(), e);
			throw new GlobalException("SAP返回" + rmsg);
		}
		if (!"S".equals(rcode)) {
			throw new GlobalException("SAP返回" + rmsg);
//			throw new CustomException("SAP返回" + rmsg);
		}
		return undoId;
	}

	public String orderStockOut(String shipRequestName, String siteName, String user,List<String> boxList,String commitDate) throws Exception {

		String rcode;
		String undoId = "";
		String rmsg = null;
		String sql=" SELECT      MP.MATERIALSPECNAME,       \r\n" +
				"        			MP.ERPFACTORY,       \r\n" +
				"        			MP.ERPLOCATION,       \r\n" +
				"        			MS.SHIPREQUESTDETAILTYPE,       \r\n" +
				"        			MP.UNIT,       \r\n" +
				"        			MD.COSTCENTER,       \r\n" +
				"        			MD.RECEIVEPRODUCTIONORDERNUMBER,       \r\n" +
				"        			MP.SHIPREQUESTNAME,       \r\n" +
				"        			MD.SHIPREQUESTDETAILNAME,       \r\n" +
				"        			MD.ERPRECEIVELOCATION,       \r\n" +
				"        			MD.ERPRECEIVEFACTORY,       \r\n" +
				"        			MD.CAUSEOFMOVEMENT,       \r\n" +
				"        			SUM(t.QTY) AS MATERIALQUANTITY       \r\n" +
				"        		FROM       \r\n" +
				"        			MATERIALPACKING MP \r\n" +
				"        		LEFT JOIN MATERIALPACKINGSUB T\r\n" +
				"        		ON mp.MATERIALPACKINGNAME =t.MATERIALPACKINGNAME \r\n" +
				"        		AND mp.SHIPREQUESTNAME =t.RECEIVEREQUESTNAME \r\n" +
				"        		LEFT JOIN MATERIALSHIPREQUESTDETAIL MD        \r\n" +
				"        				 ON       \r\n" +
				"        			MP.SHIPREQUESTNAME = MD.SHIPREQUESTNAME       \r\n" +
				"        			AND MP.SITENAME = MD.SITENAME       \r\n" +
				"        			AND t.RECEIVEREQUESTDETAILNAME = MD.SHIPREQUESTDETAILNAME       \r\n" +
				"        		LEFT JOIN MATERIALSHIPREQUEST MS        \r\n" +
				"        				 ON       \r\n" +
				"        			MD.SHIPREQUESTNAME = MS.SHIPREQUESTNAME       \r\n" +
				"        		WHERE       \r\n" +
				"        			MP.SHIPREQUESTNAME = :SHIPREQUESTNAME       \r\n" +
				"        			AND MP.SITENAME = :SITENAME       \r\n" +
				"        			AND MP.STOCKSTATE = 'Stocked'\r\n" +
				"        			AND MP.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
				"        		GROUP BY        \r\n" +
				"        			MP.MATERIALSPECNAME,       \r\n" +
				"        			MP.ERPFACTORY,       \r\n" +
				"        			MP.ERPLOCATION,       \r\n" +
				"        			MS.SHIPREQUESTDETAILTYPE,       \r\n" +
				"        			MP.UNIT,       \r\n" +
				"        			MD.COSTCENTER,       \r\n" +
				"        			MD.RECEIVEPRODUCTIONORDERNUMBER,       \r\n" +
				"        			MP.SHIPREQUESTNAME, \r\n" +
				"        			MD.SHIPREQUESTDETAILNAME,\r\n" +
				"        			MD.ERPRECEIVELOCATION,       \r\n" +
				"        			MD.ERPRECEIVEFACTORY,       \r\n" +
				"        			MD.CAUSEOFMOVEMENT       ";
		Map<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		hashMap.put("SITENAME", siteName);
		hashMap.put("BOXLIST", boxList);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql.toString(), hashMap);


		try {

			JSONObject sendData = new JSONObject(true);
			JSONObject header = new JSONObject(true);
			sendData.put("HEAD", header);
			JSONObject body = new JSONObject(true);
			sendData.put("BODY", body);
			JSONArray itemArray = new JSONArray();

            /*
             HEAD
             */
			header.put("INTF_ID", "MM067");
			String uniqueID = UUID.randomUUID().toString();
			header.put("REQ_KEYID", uniqueID);
			header.put("SRC_MSGID", uniqueID);
			header.put("SRC_SYSTEM", "WMS");
			header.put("DEST_SYSTEM", "SAP");
			header.put("REQUSER", user);

            /*
             BODY
             */
			body.put("ITEM", itemArray);
			body.put("ITEMID", uniqueID);

			SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
			Date date = inputFormat.parse(commitDate);
			String outputDate = outputFormat.format(date);
			//过账日期
			body.put("BUDAT",outputDate);
			//凭证日期
			body.put("BLDAT", outputDate);
			//凭证抬头文本
			body.put("BKTXT", uniqueID);
			//用户名
			body.put("USNAM", user);

			for (Map<String, Object> mm : queryForList) {

				JSONObject item = new JSONObject(true);
				//BWART 移动类型
				item.put("BWART",  mm.get("SHIPREQUESTDETAILTYPE"));
				// MATNR 发货物料号
				item.put("MATNR", mm.get("MATERIALSPECNAME"));
				// WERKS 收货工厂
				item.put("WERKS", mm.get("ERPFACTORY"));
				// LGORT 发货库存地点
				item.put("LGORT", mm.get("ERPLOCATION"));
				//MENGE 数量
				item.put("MENGE", mm.get("MATERIALQUANTITY"));
				//MENGE 单位
				item.put("MEINS", mm.get("UNIT"));
				//KOSTL 成本中心
				item.put("KOSTL", mm.get("COSTCENTER"));
				//AUFNR 订单号
				item.put("AUFNR", mm.get("RECEIVEPRODUCTIONORDERNUMBER"));
				//GRUND 移动原因 (201,202回传)
				item.put("GRUND", mm.get("CAUSEOFMOVEMENT"));
				//ZLLORDER 自定义领料单号
				item.put("ZLLORDER", mm.get("SHIPREQUESTNAME"));
				//ZLLITEM 自定义领料单行号
				item.put("ZLLITEM", mm.get("SHIPREQUESTDETAILNAME"));

				itemArray.add(item);

			}
//			log.info("SendTOSAP >>>>" + sendData);
			String sapreturn = toSAPMessageUtil.sendHttpPost(toSAPMessageUtil.materialChangeLocationUrl, "", sendData.toJSONString());
			org.json.JSONObject receiveJsonObject = new org.json.JSONObject(sapreturn);
			org.json.JSONObject returnJsonObject = (org.json.JSONObject) receiveJsonObject.get("RETURN");
			rcode = returnJsonObject.get("STATUS").toString();
			rmsg = returnJsonObject.get("MSGTXT").toString();

			if ("S".equals(rcode)) {
				undoId = returnJsonObject.get("MBLNR").toString()+"_"+returnJsonObject.get("MJAHR").toString();//将物料凭证号与凭证年度拼在一起
			}
			//将log写到表里
			ErpMessageLog erplog = new ErpMessageLog();
			erplog.setEventUser("");
			erplog.setServerName("WmsToErp");
			erplog.setEventName("成本中心/研发领料");
			erplog.setInterfaceTime(TimeStampUtil.getCurrentTime(TimeStampUtil.FORMAT_DEFAULT));
			erplog.setMessageId(UUID.randomUUID().toString());
			erplog.setSendMsg(sendData.toJSONString());
			erplog.setSendMsg2(sendData.toJSONString());
			erplog.setReturnMsg2(sapreturn);
			erplog.setResultCode(rcode);
			MessageLogUtil.writeMessageLog(erplog);
		} catch (Exception e) {
//			log.info(e.getMessage(), e);
			throw new RuntimeException("SAP返回" + rmsg);
		}
		if (!"S".equals(rcode)) {
			throw new RuntimeException("SAP返回" + rmsg);
		}
		return undoId;
	}

	//重写销售交货单过账。zd.2024.8.14
	public String saleStockOut_NEW(String shipRequestName, String siteName, String user,List<String> boxList,String commitDate) throws Exception {
		String rcode;
		String undoId = "";
		String rmsg = null;
		String sql="    SELECT    \r\n" +
				"            count(*) AS ZROL,    \r\n" +
				"            m1.SHIPREQUESTDETAILNAME ,    \r\n" +
				"            t.ERPLOCATION,          \r\n" +
				"            sum(m.QTY)MATERIALQUANTITY          \r\n" +
				"            FROM \r\n" +
				"		 MATERIALPACKINGSUB m \r\n" +
				"		 LEFT JOIN MATERIALPACKING T ON \r\n" +
				"		 m.MATERIALPACKINGNAME = T.MATERIALPACKINGNAME AND m.RECEIVEREQUESTNAME = T.SHIPREQUESTNAME \r\n" +
				"		 LEFT JOIN MATERIALSHIPREQUESTDETAIL M1  \r\n" +
				"		  ON \r\n" +
				"		 m.RECEIVEREQUESTNAME = M1.SHIPREQUESTNAME \r\n" +
				"		 AND m.RECEIVEREQUESTDETAILNAME = M1.SHIPREQUESTDETAILNAME \r\n" +
				"		 LEFT JOIN MATERIALSHIPREQUEST M2  \r\n" +
				"		  ON \r\n" +
				"		 M1.SHIPREQUESTNAME = M2.SHIPREQUESTNAME \r\n" +
				"		 WHERE \r\n" +
				"		 T.SHIPREQUESTNAME = :SHIPREQUESTNAME \r\n" +
				"		 AND T.SITENAME = :SITENAME  \r\n" +
				"		  AND T.STOCKSTATE = 'Stocked' AND   T.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
				"		  GROUP BY m1.SHIPREQUESTDETAILNAME ,    t.ERPLOCATION";

		Map<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		hashMap.put("SITENAME", siteName);
		hashMap.put("BOXLIST", boxList);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql.toString(), hashMap);
		try {

			JSONObject sendData = new JSONObject(true);
			JSONObject header = new JSONObject(true);
			sendData.put("HEAD", header);
			JSONObject body = new JSONObject(true);
			sendData.put("BODY", body);
			JSONArray itemArray = new JSONArray();

            /*
             HEAD
             */
			header.put("DEST_SYSTEM", "SAP");
			header.put("INTF_ID", "SD023");
			header.put("SRC_SYSTEM", "WMS");
			String uniqueID = UUID.randomUUID().toString();
			header.put("SRC_MSGID", uniqueID);
			header.put("BACKUP1", uniqueID);
			header.put("BACKUP2", uniqueID);



            /*
             BODY
             */
			//交货单号
			body.put("VBELN", shipRequestName);

			SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
			Date date = inputFormat.parse(commitDate);
			String outputDate = outputFormat.format(date);
			//实际过账日期
			body.put("WADAT_IST", outputDate);//yyyyMMddhhmmss
			//发送日期
			body.put("Z_SENDDATE", outputDate);
			//操作类型（过账）
			body.put("Z_OPTION", "1");

			if (queryForList.size() >= 1) {
				for (Map<String, Object> mm : queryForList) {
					JSONObject item = new JSONObject(true);

					//交货单行号
					item.put("POSNR", mm.get("SHIPREQUESTDETAILNAME"));
					//交货数量
					item.put("PIKMG", mm.get("MATERIALQUANTITY"));
					//库存地点
					item.put("LGORT", mm.get("ERPLOCATION"));
					//实际发货卷数
					item.put("Z_ROL", mm.get("ZROL"));
					//实际发货平方米
					item.put("Z_SQUE", mm.get("MATERIALQUANTITY"));

					itemArray.add(item);
				}
				body.put("ITEMS", itemArray);//销售过账需放ITEMS
			}else {
				for (Map<String, Object> mm : queryForList) {

					//交货单行号
					body.put("POSNR", mm.get("SHIPREQUESTDETAILNAME"));
					//交货数量
					body.put("PIKMG", mm.get("MATERIALQUANTITY")); //SUM
					//库存地点
					body.put("LGORT", mm.get("ERPLOCATION"));
					//实际发货卷数
					body.put("Z_ROL", mm.get("ZROL"));   //COUNT
					//实际发货平方米
					body.put("Z_SQUE", mm.get("MATERIALQUANTITY"));

				}
			}

//			log.info("SendTOSAP >>>>" + sendData);
			String sapreturn = toSAPMessageUtil.sendHttpPost(toSAPMessageUtil.DNUrl, "", sendData.toJSONString());
			org.json.JSONObject receiveJsonObject = new org.json.JSONObject(sapreturn);

			//销售交货单过账不返回物料凭证,且RETURN为JsonArray
			org.json.JSONObject returnJsonObject = (org.json.JSONObject) receiveJsonObject.get("RETURN");
			rcode = returnJsonObject.get("STATUS").toString();
			rmsg = returnJsonObject.get("MSGTXT").toString();

			if ("S".equals(rcode)) {
				undoId = "S";
			}
			//将log写到表里
			ErpMessageLog erplog = new ErpMessageLog();
			erplog.setEventUser("");
			erplog.setServerName("WmsToErp");
			erplog.setEventName("调拨出库(NLCC)");
			erplog.setInterfaceTime(TimeStampUtil.getCurrentTime(TimeStampUtil.FORMAT_DEFAULT));
			erplog.setMessageId(UUID.randomUUID().toString());
			erplog.setSendMsg(sendData.toJSONString());
			erplog.setSendMsg2(sendData.toJSONString());
			erplog.setReturnMsg2(sapreturn);
			erplog.setResultCode(rcode);
			MessageLogUtil.writeMessageLog(erplog);
		} catch (Exception e) {
//			log.info(e.getMessage(), e);
			throw new RuntimeException("SAP返回" + rmsg);
		}
		if (!"S".equals(rcode)) {
			throw new RuntimeException("SAP返回" + rmsg);
		}
		return undoId;
	}

	//重写委外发料。zd.2024.8.17
	public String oemStockOut_NEW(String shipRequestName, String siteName, String user,List<String> boxList, String commitDate) throws Exception
	{

		String rcode;
		String undoId = "";
		String rmsg = null;
		String sql="SELECT       \r\n" +
//				"        			MS.DELIVERYADDRESS,       \r\n" +
				"        			MD.DELIVERYADDRESS,       \r\n" + // 取详情
				"        			MP.MATERIALSPECNAME,       \r\n" +
				"        			MP.ERPFACTORY,       \r\n" +
				"        			MP.ERPLOCATION,       \r\n" +
				"        			MS.SHIPREQUESTDETAILTYPE,       \r\n" +
				"        			MP.UNIT,       \r\n" +
				"        			MD.COSTCENTER,       \r\n" +
				"        			MD.RECEIVEPRODUCTIONORDERNUMBER,       \r\n" +
				"        			MP.SHIPREQUESTNAME,       \r\n" +
				"        			MP.SHIPREQUESTDETAILNAME,       \r\n" +
				"        			MD.ERPRECEIVELOCATION,       \r\n" +
				"        			MD.ERPRECEIVEFACTORY,       \r\n" +
				"        			SUM(M.QTY) AS MATERIALQUANTITY       \r\n" +
				"        		FROM       \r\n" +
				"        			MATERIALPACKING MP  \r\n" +
				"        		LEFT JOIN MATERIALPACKINGSUB m \r\n" +
				"        		ON mp.MATERIALPACKINGNAME =m.MATERIALPACKINGNAME \r\n" +
				"        		AND mp.SHIPREQUESTNAME =m.RECEIVEREQUESTNAME \r\n" +
				"        		LEFT JOIN MATERIALSHIPREQUESTDETAIL MD        \r\n" +
				"        				 ON       \r\n" +
				"        			MP.SHIPREQUESTNAME = MD.SHIPREQUESTNAME       \r\n" +
				"        			AND MP.SITENAME = MD.SITENAME \r\n" +
				"        			AND m.RECEIVEREQUESTDETAILNAME =md.SHIPREQUESTDETAILNAME \r\n" +
				"        		LEFT JOIN MATERIALSHIPREQUEST MS        \r\n" +
				"        				 ON       \r\n" +
				"        			MD.SHIPREQUESTNAME = MS.SHIPREQUESTNAME \r\n" +
				"        		WHERE       \r\n" +
				"        			MP.SHIPREQUESTNAME = :SHIPREQUESTNAME       \r\n" +
				"        			AND MP.SITENAME = :SITENAME       \r\n" +
				"        			AND MP.STOCKSTATE = 'Stocked'\r\n" +
				"        			AND mp.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
				"        		GROUP BY        \r\n" +
				"        			MD.DELIVERYADDRESS,       \r\n" +
				"        			MP.MATERIALSPECNAME,       \r\n" +
				"        			MP.ERPFACTORY,       \r\n" +
				"        			MP.ERPLOCATION,       \r\n" +
				"        			MS.SHIPREQUESTDETAILTYPE,       \r\n" +
				"        			MP.UNIT,       \r\n" +
				"        			MD.COSTCENTER,       \r\n" +
				"        			MD.RECEIVEPRODUCTIONORDERNUMBER,       \r\n" +
				"        			MP.SHIPREQUESTNAME,       \r\n" +
				"        			MP.SHIPREQUESTDETAILNAME,     \r\n" +
				"        			MD.ERPRECEIVELOCATION,       \r\n" +
				"        			MD.ERPRECEIVEFACTORY       ";


		Map<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		hashMap.put("SITENAME", siteName);
		hashMap.put("BOXLIST", boxList);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql.toString(), hashMap);



		try {

			JSONObject sendData = new JSONObject(true);
			JSONObject header = new JSONObject(true);
			sendData.put("HEAD", header);
			JSONObject body = new JSONObject(true);
			sendData.put("BODY", body);
			JSONArray itemArray = new JSONArray();

            /*
             HEAD
             */
			header.put("INTF_ID", "MM067");
			String uniqueID = UUID.randomUUID().toString();
			header.put("REQ_KEYID", uniqueID);
			header.put("SRC_MSGID", uniqueID);
			header.put("SRC_SYSTEM", "WMS");
			header.put("DEST_SYSTEM", "SAP");
			header.put("REQUSER", user);

            /*
             BODY
             */
			body.put("ITEM", itemArray);
			body.put("ITEMID", uniqueID);

			SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
			Date date = inputFormat.parse(commitDate);
			String outputDate = outputFormat.format(date);
			//过账日期
			body.put("BUDAT", outputDate);
			//凭证日期
			body.put("BLDAT", outputDate);
			//凭证抬头文本
			body.put("BKTXT", uniqueID);
			//用户名
			body.put("USNAM", user);

			for (Map<String, Object> mm : queryForList) {

				JSONObject item = new JSONObject(true);

				// MATNR 发货物料号
				item.put("MATNR", mm.get("MATERIALSPECNAME"));
				// WERKS 收货工厂
				item.put("WERKS", mm.get("ERPFACTORY"));
				// LGORT 发货库存地点
				item.put("LGORT", mm.get("ERPLOCATION"));
				//BWART 移动类型
				item.put("BWART", "541O");
				//SOBKZ 特殊库存标识
//                item.put("SOBKZ", "O");
				//LIFNR 供应商编码
				item.put("LIFNR", mm.get("DELIVERYADDRESS"));
				//MENGE 数量
				item.put("MENGE", mm.get("MATERIALQUANTITY"));
				//MENGE 单位
				item.put("MEINS", mm.get("UNIT"));
				//ZLLITEM 自定义领料单行号
				item.put("ZLLORDER", mm.get("SHIPREQUESTNAME"));
				//行项
				item.put("ZLLITEM", mm.get("SHIPREQUESTDETAILNAME"));

				itemArray.add(item);

				updateOEMINVENTORY(mm.get("ERPFACTORY") == null ?  "" : mm.get("ERPFACTORY").toString(),mm.get("DELIVERYADDRESS") == null ?  "" : mm.get("DELIVERYADDRESS").toString(),
						mm.get("MATERIALSPECNAME") == null ?  "" : mm.get("MATERIALSPECNAME").toString(),mm.get("MATERIALQUANTITY") == null ?  "" : mm.get("MATERIALQUANTITY").toString());

			}
//			log.info("SendTOSAP >>>>" + sendData);
			String sapreturn = toSAPMessageUtil.sendHttpPost(toSAPMessageUtil.materialChangeLocationUrl, "", sendData.toJSONString());
			org.json.JSONObject receiveJsonObject = new org.json.JSONObject(sapreturn);
			org.json.JSONObject returnJsonObject = (org.json.JSONObject) receiveJsonObject.get("RETURN");
			rcode = returnJsonObject.get("STATUS").toString();
			rmsg = returnJsonObject.get("MSGTXT").toString();

			if ("S".equals(rcode)) {
				undoId = returnJsonObject.get("MBLNR").toString()+"_"+returnJsonObject.get("MJAHR").toString();//将物料凭证号与凭证年度拼在一起
			}
			//将log写到表里
			UUID uuid = UUID.randomUUID();
			ErpMessageLog erplog = new ErpMessageLog();
			erplog.setEventUser("");
			erplog.setServerName("WmsToErp");
			erplog.setEventName("委外发料541O");
			erplog.setId(uuid.toString());
			erplog.setInterfaceTime(TimeStampUtil.getCurrentTime(TimeStampUtil.FORMAT_DEFAULT));
			erplog.setMessageId(UUID.randomUUID().toString());
			erplog.setSendMsg(sendData.toJSONString());
			erplog.setSendMsg2(sendData.toJSONString());
			erplog.setReturnMsg2(sapreturn);
			erplog.setResultCode(rcode);
			MessageLogUtil.writeMessageLog(erplog);
		} catch (Exception e) {
//			log.info(e.getMessage(), e);
			throw new RuntimeException("SAP返回" + rmsg);
		}
		if (!"S".equals(rcode)) {
			throw new RuntimeException("SAP返回" + rmsg);
		}
		return undoId;
	}

	//更新OEMINVENTORY。zd.2024.8.7
	public void updateOEMINVENTORY(String erpFactory,String supplierNo,String materialSpecName,String qty) throws Exception
	{
		String sqls="SELECT o.MATERIALSPECNAME FROM OEMINVENTORY o \r\n" +
				" WHERE o.ERPFACTORY =:ERPFACTORY\r\n" +
				" AND o.MATERIALSPECNAME =:MATERIALSPECNAME\r\n" ;
//				" AND o.SUPPLIERNO =:SUPPLIERNO";
		Map<String, Object> bp=new HashMap<>();
		bp.put("ERPFACTORY", erpFactory);
		bp.put("MATERIALSPECNAME", materialSpecName);
		bp.put("SUPPLIERNO", supplierNo);
		bp.put("QTY", qty);
		List<Map<String, Object>> sr=IDMFrameServiceProxy.getSqlTemplate().queryForList(sqls, bp);
		if(sr.size()>0)
		{
			String sqlu="UPDATE OEMINVENTORY o SET o.QTY =o.QTY + :QTY\r\n" +
					" WHERE o.ERPFACTORY =:ERPFACTORY\r\n" +
					" AND o.MATERIALSPECNAME =:MATERIALSPECNAME\r\n" ;
//					" AND o.SUPPLIERNO =:SUPPLIERNO";
			IDMFrameServiceProxy.getSqlTemplate().update(sqlu, bp);
		}
		else
		{
			String sqli="INSERT INTO OEMINVENTORY(ERPFACTORY,MATERIALSPECNAME,SUPPLIERNO,QTY)\r\n" +
					" VALUES (:ERPFACTORY,:MATERIALSPECNAME,:SUPPLIERNO,:QTY)";
			IDMFrameServiceProxy.getSqlTemplate().update(sqli, bp);
		}

	}

	@Transactional
	public void Reserve5 (MaterialPacking materialPacking, String shipReuqestName,
						  String materialSpecName, String sdk_id, String phase,
						  String type, String siteName, String user, String materialQuantity2, String fifoFlag) throws CustomException {
		String materialPackingName = materialPacking.getMaterialPackingName();//条码
//		float materialQuantity = materialPacking.getMaterialQuantity();
//		float mqty2 = materialPacking.getMaterialQuantity2();
//		String charge = materialPacking.getCharge();//批次
		EventInfo makeEventInfo;
		//1.绑定
		//校验Box有没有被其他订单绑过
		if ("Assign".equals(type)) {
			makeEventInfo = new EventInfoUtil().makeEventInfo("AssiginShipRequest", user, "AssiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME,MATERIALQUANTITY,SDK_ID,PHASE FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {
				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(invoice) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}
				phase = queryForList.get(0).get("PHASE") == null ? "" :queryForList.get(0).get("PHASE").toString();
				//更新Box绑定关系、Box发货数量、拆分到条码明细表
//				SetEventInfo setEventInfo = new SetEventInfo();
//				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
//				//更新实际实际出库数量到库存表
//				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
//				hashMap2.put("shipRequestName", shipReuqestName);
//				hashMap2.put("fifoFlag", fifoFlag);
//				hashMap2.put("materialQuantity2", queryForList.get(0).get("MATERIALQUANTITY").toString());//先将Box数量记录至materialQuantity2
//				setEventInfo.setUserColumns(hashMap2);
//				hashMap2.put("fifoFlag", fifoFlag);
//				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				hashMap.put("materialQuantity2", queryForList.get(0).get("MATERIALQUANTITY").toString());
				hashMap.put("fifoFlag", fifoFlag);
				hashMap.put("shipRequestName", shipReuqestName);
				hashMap.put("SITENAME", siteName);

				String upMkSql = "UPDATE\r\n" +
						"	MATERIALPACKING m\r\n" +
						"SET\r\n" +
						"	m.MATERIALQUANTITY2 = :materialQuantity2,\r\n" +
						"	m.FIFOFLAG = :fifoFlag,\r\n" +
						"	m.SHIPREQUESTNAME = :shipRequestName\r\n" +
						"WHERE\r\n" +
						"	m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME\r\n" +
						"	AND m.SITENAME = :SITENAME";

				IDMFrameServiceProxy.getSqlTemplate().update(upMkSql, hashMap);


				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY + :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ " AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) " +
//						"AND (:PHASE IS NULL OR PHASE = :PHASE) " +
						"AND ROWNUM = 1  ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				bindMap.put("QTY", queryForList.get(0).get("MATERIALQUANTITY").toString());
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}

		} else 	if ("Dessign".equals(type)) {
			//解绑根据查询出的materil信息拿到SDKID和阶段
//			sdk_id = materialPacking.getSDK_ID();
//			phase = materialPacking.getPHASE();
			makeEventInfo = new EventInfoUtil().makeEventInfo("DessiginShipRequest", user, "DessiginShipRequest");
			String sql = "SELECT SHIPREQUESTNAME,CASE WHEN MATERIALQUANTITY2 > 0 THEN MATERIALQUANTITY2 ELSE MATERIALQUANTITY END MATERIALQUANTITY2,SDK_ID,PHASE FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
			if (queryForList == null || queryForList.size() < 1) {
				throw new CustomException("条码在系统中不存在");
			} else {

				sdk_id = queryForList.get(0).get("SDK_ID") == null ? "" : queryForList.get(0).get("SDK_ID").toString();
				phase = queryForList.get(0).get("PHASE") == null ? "" : queryForList.get(0).get("PHASE") .toString();


				String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
				if (!"".equals(shipReuqestName) && !invoice.equals(shipReuqestName)) {
					throw new CustomException("条码已被" + invoice + "备货");
				}

				//更新Box绑定关系、Box发货数量、拆分到条码明细表
//				SetEventInfo setEventInfo = new SetEventInfo();
//				Map<String, Object> hashMap2 = new HashMap<String,Object> ();
//				//更新实际实际出库数量到库存表
//				MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
//				hashMap2.put("shipRequestName", "");
//				hashMap2.put("fifoFlag", "");
//				hashMap2.put("shipRequestDetailName", "");
//				hashMap2.put("materialQuantity2", "0");
//				setEventInfo.setUserColumns(hashMap2);
//				MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
				//更新备货数量
				//					BigDecimal QTY = new BigDecimal(materialQuantity);
				hashMap.put("shipRequestName", "");
				hashMap.put("fifoFlag", "");
				hashMap.put("shipRequestDetailName", "");
				hashMap.put("materialQuantity2", "0");
				hashMap.put("SITENAME", siteName);
				String upMkSql = "UPDATE\r\n" +
						"	MATERIALPACKING m\r\n" +
						"SET\r\n" +
						"	m.MATERIALQUANTITY2 = :materialQuantity2,\r\n" +
						"	m.FIFOFLAG = :fifoFlag,\r\n" +
						"	m.SHIPREQUESTNAME = :shipRequestName,\r\n" +
						"	m.SHIPREQUESTDETAILNAME = :shipRequestDetailName\r\n" +
						"WHERE\r\n" +
						"	m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME\r\n" +
						"	AND m.SITENAME = :SITENAME";
				IDMFrameServiceProxy.getSqlTemplate().update(upMkSql, hashMap);


				String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY - :QTY WHERE m.SITENAME = :SITENAME"
						+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.MATERIALSPECNAME = :MATERIALSPECNAME "
						+ "AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID) " +
//						"AND (:PHASE IS NULL OR PHASE = :PHASE)" +
						"  AND ROWNUM = 1 ";
				Map<String, Object> bindMap = new HashMap<String, Object>();
				//bindMap.put("QTY", materialQuantity2);
				bindMap.put("QTY", queryForList.get(0).get("MATERIALQUANTITY2").toString());
				bindMap.put("SITENAME", siteName);
				bindMap.put("SHIPREQUESTNAME", shipReuqestName);
				bindMap.put("MATERIALPACKINGNAME", materialPackingName);
				bindMap.put("MATERIALSPECNAME", materialSpecName);
				bindMap.put("SDK_ID", sdk_id);
				bindMap.put("PHASE", phase);
				IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
			}
		}
	}
	@Transactional
	public String StockUpOutSendSAPOrMES(String shipRequestName, String siteName, String eventUser,
										  List<MaterialPacking> list , String commitDate, String opCode) throws Exception {
		//判断单据类型调用不同的NC接口
		String sql = "SELECT m.SHIPREQUESTTYPE,m.SHIPREQUESTDETAILTYPE FROM MATERIALSHIPREQUEST m WHERE m.SHIPREQUESTNAME = :SHIPREQUESTNAME";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		List<String> materialPackingKeyList = new ArrayList<> ();
		List<MaterialPackingKey> arrayList = new ArrayList<MaterialPackingKey> ();
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		String shipRequestType = queryForList.get(0).get("SHIPREQUESTTYPE").toString();
		String detailype = queryForList.get(0).get("SHIPREQUESTDETAILTYPE").toString();
		if ("4C".equals(shipRequestType)) {
			String sql222 = "SELECT SUM(m.REQUESTQUANTITY - m.ASSIGNEDQUANTITY)  QTY,MATERIALSPECNAME FROM MATERIALSHIPREQUESTDETAIL m WHERE m.SHIPREQUESTNAME  = :SHIPREQUESTNAME GROUP BY MATERIALSPECNAME";
			List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql222, hashMap);
			for (int i = 0; i < queryForList2.size(); i++) {
				String string = queryForList2.get(i).get("QTY").toString();
				String MATERIALSPECNAME = queryForList2.get(i).get("MATERIALSPECNAME").toString();
				BigDecimal bigDecimal = new BigDecimal(string);
				if (bigDecimal.compareTo(BigDecimal.ZERO) < 0 ) {
					throw new CustomException( MATERIALSPECNAME + "销售不允许超发");
				}
			}
		}
		//备份过账条码表的信息，并删除
		String bfSql = "INSERT\r\n" +
				"	INTO\r\n" +
				"	MATERIALPACKINGSUBHISTORY T (T.MATERIALPACKINGNAME,\r\n" +
				"	T.RECEIVEREQUESTNAME,\r\n" +
				"	T.RECEIVEREQUESTDETAILNAME,\r\n" +
				"	T.QTY)\r\n" +
				"SELECT\r\n" +
				"	M.MATERIALPACKINGNAME,\r\n" +
				"	M.RECEIVEREQUESTNAME,\r\n" +
				"	M.RECEIVEREQUESTDETAILNAME,\r\n" +
				"	M.QTY\r\n" +
				"FROM\r\n" +
				"	MATERIALPACKINGSUB M\r\n" +
				"WHERE\r\n" +
				"	M.RECEIVEREQUESTNAME = :SHIPREQUESTNAME";
		String scSql = "DELETE FROM MATERIALPACKINGSUB WHERE RECEIVEREQUESTNAME  = :SHIPREQUESTNAME";
		IDMFrameServiceProxy.getSqlTemplate().update(bfSql, hashMap);
		IDMFrameServiceProxy.getSqlTemplate().update(scSql, hashMap);
		EventInfoUtil eventInfoUtil = new EventInfoUtil();
		EventInfo makeEventInfo = eventInfoUtil.makeEventInfo("CompletedShipRequest", eventUser, "CompletedShipRequest");
		//循环过账批次
		for (int i = 0; i < list.size(); ++ i) {
			String materialPackingName = list.get(i).getMaterialPackingName();
			String sdk_ID = list.get(i).getSDK_ID();
			String phase = list.get(i).getPHASE();
			String materialSpecName = list.get(i).getMaterialSpecName();
			//			float materialQuantity = list.get(i).getMaterialQuantity();
			//重新获取下数量，通过sql查询取数
			String SLsQL = "SELECT m.MATERIALPACKINGNAME,m.MATERIALQUANTITY,"
					+ "DECODE(NVL(m.MATERIALQUANTITY2,0), 0 ,m.MATERIALQUANTITY,m.MATERIALQUANTITY2) MATERIALQUANTITY2  FROM MATERIALPACKING m"
					+ " WHERE m.SHIPREQUESTNAME = :SHIPRQEUESTNAME AND MATERIALPACKINGNAME = :MATERIALPACKINGNAME ";
			Map<String, Object> hashMap2 = new HashMap<String,Object> ();
			hashMap2.put("SHIPRQEUESTNAME", shipRequestName);
			hashMap2.put("MATERIALPACKINGNAME", materialPackingName);
			String materialQuantity2 = list.get(i).getMaterialQuantity2() + "";
			List<Map<String, Object>> queryForList3 = IDMFrameServiceProxy.getSqlTemplate().queryForList(SLsQL, hashMap2);
			if (queryForList3 != null && queryForList3.size() > 0) {
				materialQuantity2 = queryForList3.get(0).get("MATERIALQUANTITY2").toString();
			} else {
				throw new CustomException("单据" + shipRequestName + "库存"  + materialPackingName  + "绑定关系不存在");
			}
			//循环单据查询过帐表
			String pcSql = "SELECT SHIPREQUESTNAME, SHIPREQUESTDETAILNAME,\r\n" +
					"	m.MATERIALSPECNAME ,\r\n" +
					"	m.PHASE ,\r\n" +
					"	m.SDK_ID ,\r\n" +
					"	m.REQUESTQUANTITY ,\r\n" +
					"	m.ASSIGNEDQUANTITY ,\r\n" +
					"	(m.REQUESTQUANTITY - nvl(m.SALEOUTQUANTITY, 0) ) SALEOUTQUANTITY\r\n" +
					"FROM\r\n" +
					"	MATERIALSHIPREQUESTDETAIL m\r\n" +
					"WHERE\r\n" +
					"	m.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" +
					"	AND SHIPREQUESTNAME = :SHIPREQUESTNAME  ORDER BY TO_NUMBER(SHIPREQUESTDETAILNAME) ASC ";
			hashMap.put("MATERIALSPECNAME", materialSpecName);
			hashMap.put("SDK_ID", sdk_ID);
			hashMap.put("PHASE", phase);
			List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(pcSql, hashMap);
			BigDecimal pcQty = new BigDecimal(materialQuantity2);//批次数量
			BigDecimal temp;
			for (int j = 0; j < queryForList2.size(); j++) {
				String sq = queryForList2.get(j).get("SALEOUTQUANTITY").toString();//剩余发货数量
				String shipRequestDetailName = queryForList2.get(j).get("SHIPREQUESTDETAILNAME").toString();//行项
				BigDecimal saleQty = new BigDecimal(sq);//需要发货数量
				if (pcQty.compareTo(BigDecimal.ZERO) == 0
						|| (saleQty.compareTo(BigDecimal.ZERO) == 0  && (j != queryForList2.size() - 1) )
				) {
					continue;
				}
				int compareTo = saleQty.compareTo(pcQty);//大于等于0则批次全发,小于0则需要拆分
				int compareToZero=saleQty.compareTo(BigDecimal.ZERO);
				if (compareTo >= 0) {
					untils.fahuoSplit(materialPackingName, shipRequestName, shipRequestDetailName, pcQty);
					break;
				} else {//需要拆行
					if( (j != queryForList2.size() - 1) &&  compareToZero>0) {//判断是否是最后一行，不是最后一行就不允许超发，发货数量等于请求数量即拆
						untils.fahuoSplit(materialPackingName, shipRequestName, shipRequestDetailName, saleQty);
						pcQty = pcQty.subtract(saleQty);
						continue;
					} else {
						untils.fahuoSplit(materialPackingName, shipRequestName, shipRequestDetailName, pcQty);
						break;
					}
				}
			}
			materialPackingKeyList.add(materialPackingName);
		}
		//判断会有多少个仓库发货，不同仓库需要分开走账
		StringBuffer cklx = new StringBuffer();
		cklx.append("SELECT m.MATERIALPACKINGNAME,m.SITENAME FROM MATERIALPACKING m WHERE m.MATERIALPACKINGNAME IN ");
		String str = "";
		str += "(";
		for (String materialPacking : materialPackingKeyList) {
			str += "'" + materialPacking + "',";
		}
		str += "'')";
		// 查询MATERIALQUANTITY 与 MATERIALQUANTITY2是否相等，如果相等则未修改数量，不相等则修改数量
		List<Map<String, Object>> packingQueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(cklx.toString() + str, hashMap);

		String billCode = "";
		switch (shipRequestType) {
			case "NLCC" : //STO调拨出库

				//	拆条码，查询MATERIALQUANTITY 与 MATERIALQUANTITY2是否相等，如果相等则未修改了数量，不相等则修改了数量
				//如果已修改数量，则进行拆条码
				for (int z = 0; z < packingQueryForList.size(); z++) {
					String MATERIALPACKINGNAME = packingQueryForList.get(z).get("MATERIALPACKINGNAME").toString();
					Object[] bindSet = {MATERIALPACKINGNAME};
					String qtysql = "WHERE MATERIALPACKINGNAME = ?";
					List<MaterialPacking> qtyQueryForList = MaterialPackingServiceProxy.getMaterialPackingService().select(qtysql, bindSet);

					String qty = new Double(qtyQueryForList.get(0).getMaterialQuantity()) == null ? "0" : String.valueOf(qtyQueryForList.get(0).getMaterialQuantity());

					String qty2 = new Double(qtyQueryForList.get(0).getMaterialQuantity2()) == null ? "0" : String.valueOf(qtyQueryForList.get(0).getMaterialQuantity2());
					String materialPackingName = qtyQueryForList.get(0).getMaterialPackingName();
					String charge = qtyQueryForList.get(0).getCharge();
					BigDecimal bigQty = new BigDecimal(qty);
					// 已拆数量
					BigDecimal bigQty2 = new BigDecimal(qty2);

					// 如果已拆数量不等于原数量，并且已拆数量为0(未拆分数量)
					if(bigQty.compareTo(bigQty2) != 0 && !"0".equals(qty2)) {
						// 剩余数量
						BigDecimal restQty = bigQty.subtract(bigQty2);

						// 生成新的拆分条码
						String stringBigQty2 = bigQty2.toString();
						String newPalletNo = CodeGenerator.packingGenerateCode(materialPackingName,stringBigQty2,charge);

						// 写入拆分的新条码，更新原数据库
						List<MaterialPacking> boxList = new ArrayList<MaterialPacking>();
//							MaterialPackingKey materialpackingKey = new MaterialPackingKey();
						MaterialPacking materialpackingNew = new MaterialPacking();
//							materialpackingKey.setSiteName("SDK");
//							materialpackingKey.setMaterialPackingName(newPalletNo);
//							materialpackingNew.setKey(materialpackingKey);

						// 复制一个当前的原标签数据
						materialpackingNew.setKey(qtyQueryForList.get(0).getKey());
//							MaterialPacking materialpackingNew = qtyQueryForList.get(z);
						// 设置新为标签和数量
						materialpackingNew.setMaterialPackingName(newPalletNo);
						// bigQty2 转double类型
						materialpackingNew.setMaterialQuantity(bigQty2.doubleValue());
						boxList.add(qtyQueryForList.get(0));
						boxList.add(materialpackingNew);
						boolean moveTransformOut = packingChargeSplit(boxList, eventUser);
						if (moveTransformOut != true) {
							throw new RuntimeException("修改数量拆分失败");
						}

						//记录到拆分表
						String subsql = "SELECT RECEIVEREQUESTDETAILNAME FROM MATERIALPACKINGSUB WHERE RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND MATERIALPACKINGNAME = :MATERIALPACKINGNAME\n";
						Map<String, Object> subHashMap = new HashMap<String, Object>();
						subHashMap.put("RECEIVEREQUESTNAME", shipRequestName);
						subHashMap.put("MATERIALPACKINGNAME", MATERIALPACKINGNAME);
						List<Map<String, Object>> ZKqueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(subsql, subHashMap);
						String preReceiverequestdetailname = ZKqueryForList.get(0).get("RECEIVEREQUESTDETAILNAME").toString();

						String shipRequestDetailName = qtyQueryForList.get(0).getShipRequestDetailName() == null ? "" : qtyQueryForList.get(0).getShipRequestDetailName().toString();
						untils.fahuoSplit(newPalletNo, shipRequestName, preReceiverequestdetailname, bigQty2);

						// 原条码扣减拆出的数量，用剩余数量更新原条码，条码不变，数量更新
						SetEventInfo setEventInfo = new SetEventInfo();
						Map<String, Object> pakHashMap = new HashMap<>();
						pakHashMap.put("materialQuantity", restQty.doubleValue());
						pakHashMap.put("materialQuantity2", 0);
						pakHashMap.put("shipRequestName", "");
						pakHashMap.put("shipRequestDetailName", "");

						EventInfo eventInfo = new EventInfoUtil().makeEventInfo("备货出库调拨出拆分条码", eventUser, "拆分条码");
						setEventInfo.setUserColumns(pakHashMap);
						MaterialPackingServiceProxy.getMaterialPackingService().setEvent(new MaterialPackingKey( "SDK", MATERIALPACKINGNAME), eventInfo, setEventInfo);

						// 删除被拆分的原条码数据，用拆分出的新条码覆盖，进行后面的过账，现场仓过账只过账已拆分出的数量，剩余数量的原条码不过账
						boolean deletePacking = materialPackingKeyList.remove(MATERIALPACKINGNAME);
						if (deletePacking) {
							materialPackingKeyList.add(newPalletNo);
						}

					}

				}

				makeEventInfo.setEventName("调拨出库");
				makeEventInfo.setEventComment("备货调拨出库");
				billCode=TransferStockOut_NLCC(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;

			case "01"://生产领料
			case "02"://生产补料
				if ("01".equals(shipRequestType)){
					makeEventInfo.setEventName("CompletedShipRequestLL");
					makeEventInfo.setEventComment("CompletedShipRequestLL");
				}else {
					makeEventInfo.setEventName("CompletedShipRequestBL");
					makeEventInfo.setEventComment("CompletedShipRequestBL");
				}

				// 判断是否是现场仓
				String IsSql = "SELECT m.ERPRECEIVELOCATION,m.ERPLOCATION,m.ERPRECEIVEFACTORY FROM MATERIALSHIPREQUESTDETAIL m WHERE  SHIPREQUESTNAME = :SHIPREQUESTNAME";
				Map<String,Object> bpc=new HashMap<>();
				bpc.put("SHIPREQUESTNAME", shipRequestName);
				List<Map<String,Object>> receiveList = IDMFrameServiceProxy.getSqlTemplate().queryForList(IsSql, bpc);
				String ERPRECEIVELOCATION = receiveList.get(0).get("ERPRECEIVELOCATION") == null ? "" : receiveList.get(0).get("ERPRECEIVELOCATION").toString();
				String ERPLOCATION = receiveList.get(0).get("ERPLOCATION") == null ? "" : receiveList.get(0).get("ERPLOCATION").toString();
				String ERPRECEIVEFACTORY = receiveList.get(0).get("ERPRECEIVEFACTORY") == null ? "" : receiveList.get(0).get("ERPRECEIVEFACTORY").toString();

				//判断是否为MES需要二次接收的物料
				/*String sqlc=" SELECT e.ENUMVALUE FROM MES_ENUMDEFVALUE e \r\n" +
						" WHERE e.ENUMNAME ='ConfirmReceiveArea'\r\n" +
						" AND e.ENUMVALUE in(\r\n" +
						" SELECT p.AREANAME FROM MES_PRODUCTREQUEST p \r\n" +
						" WHERE p.PRODUCTREQUESTNAME =(SELECT m.RECEIVEPRODUCTIONORDERNUMBER FROM MATERIALSHIPREQUESTDETAIL m \r\n" +
						" WHERE m.SHIPREQUESTNAME=:SHIPREQUESTNAME AND RECEIVEPRODUCTIONORDERNUMBER IS NOT NULL AND ROWNUM=1))";*/
				List<Map<String,Object>> src = new ArrayList<Map<String,Object>>();
						String sqlQuery = " SELECT m.RECEIVEPRODUCTIONORDERNUMBER FROM MATERIALSHIPREQUESTDETAIL m " +
						" WHERE m.SHIPREQUESTNAME=:SHIPREQUESTNAME AND RECEIVEPRODUCTIONORDERNUMBER IS NOT NULL AND ROWNUM=1";
				List<Map<String,Object>> materialShipRequestDetailList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlQuery, bpc);
				if (null != materialShipRequestDetailList && !materialShipRequestDetailList.isEmpty()){
					String productRequestName = materialShipRequestDetailList.get(0).get("RECEIVEPRODUCTIONORDERNUMBER") == null ? "" : materialShipRequestDetailList.get(0).get("RECEIVEPRODUCTIONORDERNUMBER").toString().substring(4);
					String sqlc = " SELECT e.ENUMVALUE FROM ENUMDEFVALUE@MES_PRODUCTREQUEST e \r\n" +
							" WHERE e.ENUMNAME ='ConfirmReceiveArea'\r\n" +
							" AND e.ENUMVALUE in(\r\n" +
							" SELECT p.AREANAME FROM PRODUCTREQUEST@MES_PRODUCTREQUEST p \r\n" +
							" WHERE p.PRODUCTREQUESTNAME =:PRODUCTREQUESTNAME)";
					bpc.put("PRODUCTREQUESTNAME", productRequestName);
					src = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlc, bpc);
				}
				//MES需要接收，则只抛送数据。 或如果是现场仓,先移到现场仓
				if (src.size()>0){
					billCode="Y";
					String sqlZK=" SELECT\r\n" +
							"	t.MATERIALSPECNAME,m.MATERIALPACKINGNAME,\r\n" +
							"	t.ERPFACTORY,\r\n" +
							"	t.ERPLOCATION,\r\n" +
							"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
							"	t.UNIT,\r\n" +
							"	m1.COSTCENTER,\r\n" +
							"	m1.SHIPREQUESTNAME,\r\n" +
							"	m1.SHIPREQUESTDETAILNAME,\r\n" +
							"	m1.ERPRECEIVELOCATION,\r\n" +
							"	m1.ERPRECEIVEFACTORY,\r\n" +
							"	m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
							"	CASE WHEN m1.SENDSALESORDER IS NOT NULL THEN 'E' ELSE '' END SOBKZ,\r\n" +
							"	(\r\n" +
							"	SELECT\r\n" +
							"		RECEIVEPRODUCTIONORDERNUMBER\r\n" +
							"	FROM\r\n" +
							"		MATERIALSHIPREQUESTDETAIL m5\r\n" +
							"	WHERE\r\n" +
							"		m5.SHIPREQUESTNAME =:SHIPREQUESTNAME\r\n" +
							"		AND m5.MATERIALSPECNAME = t.MATERIALSPECNAME\r\n" +
							"	ORDER BY\r\n" +
							"		m5.USEDATE,\r\n" +
							"		m5.SHIPREQUESTDETAILNAME FETCH FIRST ROW ONLY)RECEIVEPRODUCTIONORDERNUMBER,\r\n" +
							"	SUM(m.QTY) AS MATERIALQUANTITY\r\n" +
							" FROM \r\n" +
							"		 MATERIALPACKINGSUB m\r\n" +
							" LEFT JOIN MATERIALPACKING T ON \r\n" +
							"		 m.MATERIALPACKINGNAME = T.MATERIALPACKINGNAME\r\n" +
							"	AND m.RECEIVEREQUESTNAME = T.SHIPREQUESTNAME\r\n" +
							" LEFT JOIN MATERIALSHIPREQUESTDETAIL M1  \r\n" +
							"		  ON \r\n" +
							"		 m.RECEIVEREQUESTNAME = M1.SHIPREQUESTNAME\r\n" +
							"	AND m.RECEIVEREQUESTDETAILNAME = M1.SHIPREQUESTDETAILNAME\r\n" +
							" LEFT JOIN MATERIALSHIPREQUEST M2  \r\n" +
							"		  ON \r\n" +
							"		 M1.SHIPREQUESTNAME = M2.SHIPREQUESTNAME\r\n" +
							" WHERE \r\n" +
							"		 T.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" +
							"	AND T.SITENAME = :SITENAME\r\n" +
							"	AND T.STOCKSTATE = 'Stocked'\r\n" +
							"	AND T.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
							" GROUP BY\r\n" +
							"	t.MATERIALSPECNAME,\r\n" +
							"	t.ERPFACTORY,\r\n" +
							"	t.ERPLOCATION,\r\n" +
							"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
							"	t.UNIT,\r\n" +
							"	m1.COSTCENTER,\r\n" +
							"	m1.SHIPREQUESTNAME,m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
							"	m1.SHIPREQUESTDETAILNAME,\r\n" +
							"	m1.ERPRECEIVELOCATION,\r\n" +
							"	m1.ERPRECEIVEFACTORY,m.MATERIALPACKINGNAME";

					// 转库到线边仓
					Map<String, Object> hashMapZK = new HashMap<String, Object>();
					hashMapZK.put("BOXLIST", materialPackingKeyList);
					hashMapZK.put("SHIPREQUESTNAME", shipRequestName);
					hashMapZK.put("SITENAME", siteName);
					List<Map<String, Object>> ZKqueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlZK.toString(), hashMapZK);

					for (int z = 0; z < ZKqueryForList.size(); z++) {
						String materialPackingName = ZKqueryForList.get(z).get("MATERIALPACKINGNAME").toString();
						materialPackingKeyList.add(materialPackingName);
						MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",materialPackingName);
						arrayList.add(materialPackingKey);
					}
					/*List<MaterialPackingKey> mpKeyList = new ArrayList<MaterialPackingKey> ();
					SetEventInfo setEventInfo = new SetEventInfo();
					for (MaterialPackingKey materialPacking : arrayList) {
						MaterialPackingKey key = materialPacking;
						mpKeyList.add(key);
					}

					Map<String, Object> hashMapPack = new HashMap<String,Object> ();
					hashMapPack.put("erpLocation", ERPRECEIVELOCATION == null  ? "" : ERPRECEIVELOCATION);
					hashMapPack.put("erpFactory",  ERPRECEIVEFACTORY == null  ? "" : ERPRECEIVEFACTORY);
					hashMapPack.put("locationName", "");
					hashMapPack.put("lastEventUser", eventUser);
					hashMapPack.put("unDoID", billCode);
					setEventInfo.setUserColumns(hashMapPack);
					MaterialPackingServiceProxy.getMaterialPackingService().setEvent(mpKeyList, makeEventInfo, setEventInfo);
					*/
					//抛送MES数据
					//untils.sendMaterialInfoToMES_NEW2(shipRequestName, "", materialPackingKeyList);
					untils.StockOutSendMaterialInfoToMES(shipRequestName, "", materialPackingKeyList);
					//更新BOX状态
					EventInfo mesEventInfo = eventInfoUtil.makeEventInfo("CompletedShipRequestMES", eventUser, "CompletedShipRequestMES");
					SetEventInfo setEventInfo = new SetEventInfo();
					Map<String, Object> hashMapPack = new HashMap<String,Object> ();
					hashMapPack.put("stockState", GenericServiceProxy.getConstantMap().STOCKSTATE_STOCKPENDING);
					hashMapPack.put("oldStockState", GenericServiceProxy.getConstantMap().STOCKSTATE_STOCKED);
					setEventInfo.setUserColumns(hashMapPack);
					MaterialPackingServiceProxy.getMaterialPackingService().setEvent(arrayList, mesEventInfo, setEventInfo);
				} else {
					if(!ERPRECEIVELOCATION.isEmpty()) {

	//					 拆条码，查询MATERIALQUANTITY 与 MATERIALQUANTITY2是否相等，如果相等则未修改了数量，不相等则修改了数量
	//					 如果已修改数量，则进行拆条码
						for (int z = 0; z < packingQueryForList.size(); z++) {
							String MATERIALPACKINGNAME = packingQueryForList.get(z).get("MATERIALPACKINGNAME").toString();
							Object[] bindSet = {MATERIALPACKINGNAME};
							String qtysql = "WHERE MATERIALPACKINGNAME = ?";
							List<MaterialPacking> qtyQueryForList = MaterialPackingServiceProxy.getMaterialPackingService().select(qtysql, bindSet);

							String qty = new Double(qtyQueryForList.get(0).getMaterialQuantity()) == null ? "0" : String.valueOf(qtyQueryForList.get(0).getMaterialQuantity());

							String qty2 = new Double(qtyQueryForList.get(0).getMaterialQuantity2()) == null ? "0" : String.valueOf(qtyQueryForList.get(0).getMaterialQuantity2());
							String materialPackingName = qtyQueryForList.get(0).getMaterialPackingName();
							String charge = qtyQueryForList.get(0).getCharge();
							BigDecimal bigQty = new BigDecimal(qty);
							// 已拆数量
							BigDecimal bigQty2 = new BigDecimal(qty2);

							// 如果已拆数量不等于原数量，并且已拆数量为0(未拆分数量)
							if(bigQty.compareTo(bigQty2) != 0 && !"0".equals(qty2)) {
								// 剩余数量
								BigDecimal restQty = bigQty.subtract(bigQty2);

								// 生成新的拆分条码
								String stringBigQty2 = bigQty2.toString();
								String newPalletNo = CodeGenerator.packingGenerateCode(materialPackingName,stringBigQty2,charge);

								// 写入拆分的新条码，更新原数据库
								List<MaterialPacking> boxList = new ArrayList<MaterialPacking>();
	//							MaterialPackingKey materialpackingKey = new MaterialPackingKey();
								MaterialPacking materialpackingNew = new MaterialPacking();
	//							materialpackingKey.setSiteName("SDK");
	//							materialpackingKey.setMaterialPackingName(newPalletNo);
	//							materialpackingNew.setKey(materialpackingKey);

								// 复制一个当前的原标签数据
								materialpackingNew.setKey(qtyQueryForList.get(0).getKey());
	//							MaterialPacking materialpackingNew = qtyQueryForList.get(z);
								// 设置新为标签和数量
								materialpackingNew.setMaterialPackingName(newPalletNo);
								// bigQty2 转double类型
								materialpackingNew.setMaterialQuantity(bigQty2.doubleValue());
								boxList.add(qtyQueryForList.get(0));
								boxList.add(materialpackingNew);
								boolean moveTransformOut = packingChargeSplit(boxList, eventUser);
								if (moveTransformOut != true) {
									throw new RuntimeException("修改数量拆分失败");
								}

								//记录到拆分表
								String subsql = "SELECT RECEIVEREQUESTDETAILNAME FROM MATERIALPACKINGSUB WHERE RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND MATERIALPACKINGNAME = :MATERIALPACKINGNAME\n";
								Map<String, Object> subHashMap = new HashMap<String, Object>();
								subHashMap.put("RECEIVEREQUESTNAME", shipRequestName);
								subHashMap.put("MATERIALPACKINGNAME", MATERIALPACKINGNAME);
								List<Map<String, Object>> ZKqueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(subsql, subHashMap);
								String preReceiverequestdetailname = ZKqueryForList.get(0).get("RECEIVEREQUESTDETAILNAME").toString();

								String shipRequestDetailName = qtyQueryForList.get(0).getShipRequestDetailName() == null ? "" : qtyQueryForList.get(0).getShipRequestDetailName().toString();
								untils.fahuoSplit(newPalletNo, shipRequestName, preReceiverequestdetailname, bigQty2);

								// 原条码扣减拆出的数量，用剩余数量更新原条码，条码不变，数量更新
								SetEventInfo setEventInfo = new SetEventInfo();
								Map<String, Object> pakHashMap = new HashMap<>();
								pakHashMap.put("materialQuantity", restQty.doubleValue());
								pakHashMap.put("materialQuantity2", 0);
								pakHashMap.put("shipRequestName", "");
								pakHashMap.put("shipRequestDetailName", "");

								EventInfo eventInfo = new EventInfoUtil().makeEventInfo("拆分条码", eventUser, "备货出库拆分条码");
								setEventInfo.setUserColumns(pakHashMap);
								MaterialPackingServiceProxy.getMaterialPackingService().setEvent(new MaterialPackingKey( "SDK", MATERIALPACKINGNAME), eventInfo, setEventInfo);

								// 删除被拆分的原条码数据，用拆分出的新条码覆盖，进行后面的过账，现场仓过账只过账已拆分出的数量，剩余数量的原条码不过账
								boolean deletePacking = materialPackingKeyList.remove(MATERIALPACKINGNAME);
								if (deletePacking) {
									materialPackingKeyList.add(newPalletNo);
								}

							}

						}


						//new MESServiceImpl().NeedReceiveMaterialInfoSend_NEW(shipRequestName, "SDK", materialPackingKeyList);
						billCode="Y";
						String sqlZK=" SELECT\r\n" +
								"	t.MATERIALSPECNAME,m.MATERIALPACKINGNAME,\r\n" +
								"	t.ERPFACTORY,\r\n" +
								"	t.ERPLOCATION,\r\n" +
								"	t.LOCATIONNAME,\r\n" +
								"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
								"	t.UNIT,\r\n" +
								"	m1.COSTCENTER,\r\n" +
								"	m1.SHIPREQUESTNAME,\r\n" +
								"	m1.SHIPREQUESTDETAILNAME,\r\n" +
								"	m1.ERPRECEIVELOCATION,\r\n" +
								"	m1.ERPRECEIVEFACTORY,\r\n" +
								"	m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
								"	CASE WHEN m1.SENDSALESORDER IS NOT NULL THEN 'E' ELSE '' END SOBKZ,\r\n" +
								"	(\r\n" +
								"	SELECT\r\n" +
								"		RECEIVEPRODUCTIONORDERNUMBER\r\n" +
								"	FROM\r\n" +
								"		MATERIALSHIPREQUESTDETAIL m5\r\n" +
								"	WHERE\r\n" +
								"		m5.SHIPREQUESTNAME =:SHIPREQUESTNAME\r\n" +
								"		AND m5.MATERIALSPECNAME = t.MATERIALSPECNAME\r\n" +
								"	ORDER BY\r\n" +
								"		m5.USEDATE,\r\n" +
								"		m5.SHIPREQUESTDETAILNAME FETCH FIRST ROW ONLY)RECEIVEPRODUCTIONORDERNUMBER,\r\n" +
								"	SUM(m.QTY) AS MATERIALQUANTITY\r\n" +
								" FROM \r\n" +
								"		 MATERIALPACKINGSUB m\r\n" +
								" LEFT JOIN MATERIALPACKING T ON \r\n" +
								"		 m.MATERIALPACKINGNAME = T.MATERIALPACKINGNAME\r\n" +
								"	AND m.RECEIVEREQUESTNAME = T.SHIPREQUESTNAME\r\n" +
								" LEFT JOIN MATERIALSHIPREQUESTDETAIL M1  \r\n" +
								"		  ON \r\n" +
								"		 m.RECEIVEREQUESTNAME = M1.SHIPREQUESTNAME\r\n" +
								"	AND m.RECEIVEREQUESTDETAILNAME = M1.SHIPREQUESTDETAILNAME\r\n" +
								" LEFT JOIN MATERIALSHIPREQUEST M2  \r\n" +
								"		  ON \r\n" +
								"		 M1.SHIPREQUESTNAME = M2.SHIPREQUESTNAME\r\n" +
								" WHERE \r\n" +
								"		 T.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" +
								"	AND T.SITENAME = :SITENAME\r\n" +
								"	AND T.STOCKSTATE = 'Stocked'\r\n" +
								"	AND T.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
								" GROUP BY\r\n" +
								"	t.MATERIALSPECNAME,\r\n" +
								"	t.ERPFACTORY,\r\n" +
								"	t.ERPLOCATION,\r\n" +
								"	t.LOCATIONNAME,\r\n" +
								"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
								"	t.UNIT,\r\n" +
								"	m1.COSTCENTER,\r\n" +
								"	m1.SHIPREQUESTNAME,m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
								"	m1.SHIPREQUESTDETAILNAME,\r\n" +
								"	m1.ERPRECEIVELOCATION,\r\n" +
								"	m1.ERPRECEIVEFACTORY,m.MATERIALPACKINGNAME";

						// 转库到线边仓
//					String sqlZK = "SELECT b.MATERIALPACKINGNAME FROM MATERIALPACKINGSUB b WHERE b.MATERIALPACKINGNAME IN (:BOXLIST)";
						Map<String, Object> hashMapZK = new HashMap<String, Object>();
						hashMapZK.put("BOXLIST", materialPackingKeyList);
						hashMapZK.put("SHIPREQUESTNAME", shipRequestName);
						hashMapZK.put("SITENAME", siteName);
						List<Map<String, Object>> ZKqueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlZK.toString(), hashMapZK);
						List<String> SUBList = new ArrayList<> ();
						for (int z = 0; z < ZKqueryForList.size(); z++) {
							String materialPackingName = ZKqueryForList.get(z).get("MATERIALPACKINGNAME").toString();
							SUBList.add(materialPackingName);
							MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",materialPackingName);
							arrayList.add(materialPackingKey);

							// 更新MATERIALPACKING表前仓库货位为现在货位和仓库数据,保存现仓库货位
							Map<String, Object> row = ZKqueryForList.get(z);
							String erpLocation = row.get("ERPLOCATION") == null ? "" : row.get("ERPLOCATION").toString();
							String locationName = row.get("LOCATIONNAME") == null ? "" : row.get("LOCATIONNAME").toString();
							String updSql = "UPDATE MATERIALPACKING SET PREERPLOCATION = :PREERPLOCATION, PRELOCATIONNAME = :PRELOCATIONNAME WHERE MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
							Map<String, Object> updHashMap = new HashMap<>();
							updHashMap.put("PREERPLOCATION", erpLocation);
							updHashMap.put("PRELOCATIONNAME", locationName);
							updHashMap.put("MATERIALPACKINGNAME", materialPackingName);
							try {
								IDMFrameServiceProxy.getSqlTemplate().update(updSql, updHashMap);
							} catch (Exception e) {
								// 日志记录并抛出自定义异常，便于调试和事务回滚
								throw new CustomException("更新仓库货位信息失败: " + materialPackingName, e);
							}
						}
						billCode = UnSourcedChangeLocation(arrayList,ERPRECEIVEFACTORY,
								ERPRECEIVELOCATION, "", eventUser, commitDate );

						List<MaterialPackingKey> mpKeyList = new ArrayList<MaterialPackingKey> ();
						SetEventInfo setEventInfo = new SetEventInfo();
						for (MaterialPackingKey materialPacking : arrayList) {
							MaterialPackingKey key = materialPacking;
							mpKeyList.add(key);
						}

						Map<String, Object> hashMapPack = new HashMap<String,Object> ();
						hashMapPack.put("erpLocation", ERPRECEIVELOCATION == null  ? "" : ERPRECEIVELOCATION);
						hashMapPack.put("erpFactory",  ERPRECEIVEFACTORY == null  ? "" : ERPRECEIVEFACTORY);
						hashMapPack.put("locationName", "");
						hashMapPack.put("lastEventUser", eventUser);
						hashMapPack.put("unDoID", billCode);
						setEventInfo.setUserColumns(hashMapPack);
						MaterialPackingServiceProxy.getMaterialPackingService().setEvent(mpKeyList, makeEventInfo, setEventInfo);
						//抛送MES数据
						Boolean success = untils.sendMaterialInfoToMES_NEW2(shipRequestName, "", SUBList,billCode,eventUser);

						if (!success) {
							String undo = cancelShipInter(billCode, eventUser);
							if (undo == null || undo.isEmpty()) {
								throw new GlobalException("下发mes失败后冲销失败！");
							}else {
								throw new GlobalException("发送到mes失败！");
							}
						}
						makeEventInfo.setEventName("备货出库无来源转库");
						makeEventInfo.setEventComment("备货出库无来源转库");
						WlySaveUnDoInfo( materialPackingKeyList,billCode,commitDate);
//						untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
					}
					else
					{
//						billCode=toSAPService.ProductionStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList);

						billCode=ProductionStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
						if (billCode.isEmpty()) {
							throw new CustomException("SAP过账异常！");
						}
//
						//抛送MES数据
						Boolean success = untils.sendMaterialInfoToMES_NEW(shipRequestName, "", materialPackingKeyList,billCode,eventUser);
						//billCode = NCWServiceImpl.SaleOut2(shipRequestName, "SDK", eventUser,materialPackingKeyList,"");
						// 如果mes发送失败，则进行sap过账冲销
						if (!success) {
							String undo = cancelShipInter(billCode, eventUser);
							if (undo == null || undo.isEmpty()) {
								throw new GlobalException("下发mes失败后冲销失败！");
							}else {
								throw new GlobalException("发送到mes失败！");
							}
					     }
						makeEventInfo.setEventName("备货出库生产领料出库");
						makeEventInfo.setEventComment("备货出库生产领料出库");
						untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
					}
				}
				break;

			case "06"://成本中心领料
				//				billCode=toSAPService.orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList);
				billCode=orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
				//抛送MES数据
				Boolean success = untils.sendMaterialInfoToMES_NEW(shipRequestName, "", materialPackingKeyList,billCode,eventUser);

				if (!success) {
					String undo = cancelShipInter(billCode, eventUser);
					if (undo == null || undo.isEmpty()) {
						throw new GlobalException("下发mes失败后冲销失败！");
					}else {
						throw new GlobalException("发送到mes失败！");
					}
				}

				//更新MES管芯数据
				new MESServiceImpl().updateDurable_ByCostcenter(shipRequestName, "SDK", eventUser, materialPackingKeyList);
				//billCode = NCWServiceImpl.SaleOut2(shipRequestName, "SDK", eventUser,materialPackingKeyList,"");
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			case "09"://研发领料
//				billCode=toSAPService.orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList);
//				billCode=orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList);
//				//更新MES管芯数据
//				new MESServiceImpl().updateDurable_ByCostcenter(shipRequestName, "SDK", eventUser, materialPackingKeyList);
//				//billCode = NCWServiceImpl.SaleOut2(shipRequestName, "SDK", eventUser,materialPackingKeyList,"");
//				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			case "ZLF0"://销售出库确认 （单据类型ZLF0~ZLF8） SD023(销售回传)
			case "ZLF1":
			case "ZLF2":
			case "ZLF3":
			case "ZLF4":
			case "ZLF5":
			case "ZLF6":
			case "ZLF7":
			case "ZLF8":
				makeEventInfo.setEventName("CompletedShipRequestZL");
				makeEventInfo.setEventComment("CompletedShipRequestZL");
//				billCode=toSAPService.saleStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList);
				billCode=saleStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				//单据类型等于ZLF0-ZLF9，物料编码以7开头都属于成品胶水需要调用QMS质检接口
				String message = checkShipRequestTypeSendIQC(commitDate, siteName ,shipRequestName, makeEventInfo, materialPackingKeyList);
				if (StringUtils.isNotEmpty(message)) {
					throw new CustomException(message);
				}
				break;
			case "08":    // 委外
			case "OEM":  // 委外
//				billCode=toSAPService.oemStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList);
				billCode=oemStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList, commitDate);
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			default :
				break;
		}
		return billCode;
	}

	public String cancelShipInter(String undoId, String user) throws Exception {

		String makeFGStockIn = matailInReverse(user, undoId);
		String sapreturn = "";
		String rcode = "";
		String rmsg = "";
		String undoId2 = "";


		try {
			sapreturn = toSAPMessageUtil.sendHttpPost(toSAPMessageUtil.materilUndoUrl, "",
					makeFGStockIn);
			org.json.JSONObject receiveJsonObject = new org.json.JSONObject(sapreturn);

			org.json.JSONObject returnJsonObject = (org.json.JSONObject) receiveJsonObject.get("RETURN");
			rcode = returnJsonObject.get("STATUS").toString();
			rmsg = returnJsonObject.get("MSGTXT").toString();
			if ("S".equals(rcode)) {
				String MBLNR1 = returnJsonObject.get("MBLNR1") == null ? "" : returnJsonObject.get("MBLNR1").toString();
				String MJAHR1 = returnJsonObject.get("MJAHR1") == null ? "" : returnJsonObject.get("MJAHR1").toString();
				undoId2 = MBLNR1+"_"+MJAHR1;//将物料凭证号与凭证年度拼在一起
			}
		} catch (Exception e) {
//			log.error("WMS请求SAP异常:" + e.getMessage(), e);
			throw new RuntimeException("WMS请求SAP异常:" + rmsg);
		} finally {
			// 将消息记录log表
			ErpMessageLog erplog = new ErpMessageLog();
			erplog.setEventUser(user);
			erplog.setServerName("WmsToSAP");
			erplog.setEventName("冲销");
			erplog.setInterfaceTime(TimeStampUtil.getCurrentTime(TimeStampUtil.FORMAT_DEFAULT));
//            erplog.setMessageId(undoId);
			erplog.setMessageId(undoId);
			erplog.setSendMsg(makeFGStockIn);
			erplog.setSendMsg2(makeFGStockIn);
			erplog.setSendMsg(makeFGStockIn);
			erplog.setReturnMsg2(sapreturn);
			erplog.setResultCode(rcode);
			MessageLogUtil.writeMessageLog(erplog);
		}
		if (!"S".equals(rcode)) {
			throw new RuntimeException("SAP返回" + rmsg);
		}
		return undoId2;
	}
	public String matailInReverse(String user, String undoId) throws JsonMappingException, JsonProcessingException {

//        if (list == null || list.size() < 1) {
//            return "";
//        }
//
//        Map<String,Object> bodyData = new HashMap<>();
//        List<Map<String,Object>> itemsData = new ArrayList<>();
//
//        for (Map<String, Object> mm : list) {
//            Map<String,Object> item = new HashMap<>();
//            //UMWRK 收货工厂
//            //LGORT 发货库存地点
//            //UMLOG 收货库存地点
//            //MATNR 发货物料号
//            //UMMAT 收货物料
//
//            item.put("MATNR", mm.get("MATERIALSPECNAME"));
//            item.put("LGORT", mm.get("ERPLOCATION"));
//            item.put("WERKS", mm.get("ERPFACTORY"));
//            if("FG".equals((String)mm.get("MATERIALSPECTYPE")) ) {
//                item.put("BWART", "101"); //产成品，移动类型 101
//            }else if("BFG".equals((String)mm.get("MATERIALSPECTYPE")) ) {
//                item.put("BWART", "531"); //副产品， 移动类型 531
//            }else if("FGDEV".equals((String)mm.get("MATERIALSPECTYPE")) ) {
//                item.put("BWART", "511"); //研发品， 移动类型 511
//            }else {
//                item.put("BWART", "101"); //为了兼容之前的未提到的成品，统一暂用 101
//            }
//
//            item.put("MENGE", mm.get("MATERIALQUANTITY"));
//            item.put("MEINS", mm.get("UNIT"));
//            item.put("AUFNR", mm.get("PRODUCTORDER"));
//            item.put("BPMNG", "");
//            item.put("BPRME", "");
//            item.put("SGTXT", mm.get("REMARK"));
//            String insmk = "";
//            if (StringUtils.isEmpty((String)mm.get("PACKINGGRADE"))) {
//                insmk = "2";
//            }else if ("NG".equals((String)mm.get("PACKINGGRADE"))) {
//                insmk = "3";
//            }
////            String insmk = StringUtils.equals(mm.get("OQARESULTSTATE").toString(),"END")?"":"X";
//            item.put("INSMK", insmk);
//            itemsData.add(item);
//
//        }
//String sendData = ToSAPMessageUtil.mm067(eventUser, bodyData, itemsData);
		String[] parts = undoId.split("_");
		String MBLNR = parts[0];
		String MJAHR = parts[1];
		String uniqueID = UUID.randomUUID().toString();
		JSONObject MainData = new JSONObject(true);
		Map<String,String> headJSonData = new HashMap<>();
		Map<String,String> bodyJSonData = new HashMap<>();
		MainData.put("HEAD",headJSonData);
		MainData.put("BODY",bodyJSonData);

		headJSonData.put("INTF_ID","MM068");
		headJSonData.put("SRC_SYSTEM","WMS");
		headJSonData.put("DEST_SYSTEM","SAP");
		headJSonData.put("SRC_MSGID",uniqueID);
		headJSonData.put("BACKUP1","");
		headJSonData.put("BACKUP2","");

		bodyJSonData.put("MBLNR", MBLNR);
		bodyJSonData.put("MJAHR", MJAHR);

		//过账日期
		bodyJSonData.put("BUDAT", "20250430");
		//凭证日期
		bodyJSonData.put("BLDAT", "20250430");

		String sendData = MainData.toJSONString();
		return sendData;
	}

	public String checkShipRequestTypeSendIQC(String shippingDate,String siteName, String shipRequestName, EventInfo eventInfo, List<String> materialPackingNameList) throws Exception {
		String message = "";
		JSONArray jsonArray = new JSONArray();
		Map<String, JSONObject> jsonObjectMap = new HashMap<>();
				String sql=" SELECT \n" +
				"MP.SITENAME,\n" +
				"MP.SHIPREQUESTNAME,\n" +
				"MP.MATERIALSPECNAME,\n" +
				"MP.MATERIALPACKINGNAME,\n" +
				"MP.REMARK,\n" +
				"MP.CHARGE,\n" +
				"MP.SDKTRUEID,\n" +
				"MS.DESC_CN\n" +
				"FROM\n" +
				"MATERIALPACKING MP \n" +
				"LEFT JOIN MATERIALSPEC MS ON  MP.SITENAME = MS.SITENAME AND MP.MATERIALSPECNAME = MS.MATERIALSPECNAME\n" +
				"WHERE\n" +
				"MP.SITENAME =:SITENAME\n" +
				"AND MP.SHIPREQUESTNAME =:SHIPREQUESTNAME\n" +
				"AND MP.MATERIALPACKINGNAME IN (:BOXLIST)\n" +
				"AND MP.MATERIALSPECNAME LIKE '7%' ";

		Map<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		hashMap.put("SITENAME", siteName);
		hashMap.put("BOXLIST", materialPackingNameList);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql.toString(), hashMap);
		if (!queryForList.isEmpty()){

			for (int i = 0; i < queryForList.size(); i++) {
				String source = "wms";
				String deliveryNumber = queryForList.get(i).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(i).get("SHIPREQUESTNAME").toString();
				String materialCode = queryForList.get(i).get("MATERIALSPECNAME") == null ? "" : queryForList.get(i).get("MATERIALSPECNAME").toString();
				String materialName = queryForList.get(i).get("DESC_CN") == null ? "" : queryForList.get(i).get("DESC_CN").toString();
				String specification = queryForList.get(i).get("SDKTRUEID") == null ? "" : queryForList.get(i).get("SDKTRUEID").toString();
				String materialPackingName = queryForList.get(i).get("MATERIALPACKINGNAME") == null ? "" : queryForList.get(i).get("MATERIALPACKINGNAME").toString();
				if (jsonObjectMap.containsKey(materialName)){
					JSONObject jsonObject = jsonObjectMap.get(materialName);
					String shipmentBatch = jsonObject.getString("shipmentBatch");
					jsonObject.put("shipmentBatch",shipmentBatch + "," + materialPackingName);
				}else {
					JSONObject jsonObject = new JSONObject(true);
					//必传字段
					jsonObject.put("source", source);
					//出货日期,取页面上的过账日期
					jsonObject.put("shippingDate", shippingDate);
//			发货单号
					jsonObject.put("deliveryNumber", deliveryNumber);
//			料号
					jsonObject.put("materialCode", materialCode);
//			发货品名
					jsonObject.put("materialName", materialName);
//			规格
					jsonObject.put("specification", specification);
					//发货批次号
					jsonObject.put("shipmentBatch", materialPackingName);
					jsonObjectMap.put(materialName, jsonObject);
					//非必传字段
					/**
					 * 			 String inspectionNumber = queryForList.get(i).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(i).get("SHIPREQUESTNAME").toString();
					 * 			 String quantity = queryForList.get(i).get("MATERIALQUANTITY") == null ? "" : queryForList.get(i).get("MATERIALQUANTITY").toString();
					 * 			 String inspectBatch = queryForList.get(i).get("CHARGE") == null ? "" : queryForList.get(i).get("CHARGE").toString();
					 * 			 String samplingQuantity = queryForList.get(i).get("") == null ? "" : queryForList.get(i).get("").toString();
					 * 			 String samplingSpecification = queryForList.get(i).get("") == null ? "" : queryForList.get(i).get("").toString();
					 * 			 String remark = queryForList.get(i).get("REMARK") == null ? "" : queryForList.get(i).get("REMARK").toString();
					 * 			 String materialGroup = queryForList.get(i).get("MATERIALPROCESSGROUPNAME") == null ? "" : queryForList.get(i).get("MATERIALPROCESSGROUPNAME").toString();
					 //			发货数量
					 jsonObject.put("quantity", quantity);
					 //			客户名称
					 jsonObject.put("customName", customName);
					 //			客户代码
					 jsonObject.put("customCode", customCode);
					 //			检验单号
					 jsonObject.put("inspectionNumber", inspectionNumber);
					 //检验批号
					 jsonObject.put("inspectBatch", inspectBatch);

					 //			抽检数量
					 jsonObject.put("samplingQuantity", samplingQuantity);
					 //			抽检规格
					 jsonObject.put("samplingSpecification", samplingSpecification);
					 //			备注
					 jsonObject.put("remark", remark);
					 //			物料组
					 jsonObject.put("materialGroup", materialGroup);
					 */
				}
			}
			jsonArray.addAll(jsonObjectMap.values());
			String jsonArrayJSONString = jsonArray.toJSONString();
			message = qmsService.sendQMSByUrlJson(jsonArrayJSONString, "/opc/addShipmentInspection",eventInfo);
		}
		return message;
	}

	@Transactional
	public String RepairStockUpOutSendSAPOrMES(String shipRequestName, String siteName, String eventUser,
										 List<MaterialPacking> list , String commitDate, String opCode) throws Exception {
		//判断单据类型调用不同的NC接口
		String sql = "SELECT m.SHIPREQUESTTYPE,m.SHIPREQUESTDETAILTYPE FROM MATERIALSHIPREQUEST m WHERE m.SHIPREQUESTNAME = :SHIPREQUESTNAME";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		List<String> materialPackingKeyList = new ArrayList<> ();
		List<MaterialPackingKey> arrayList = new ArrayList<MaterialPackingKey> ();
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		String shipRequestType = queryForList.get(0).get("SHIPREQUESTTYPE").toString();
		String detailype = queryForList.get(0).get("SHIPREQUESTDETAILTYPE").toString();
		if ("4C".equals(shipRequestType)) {
			String sql222 = "SELECT SUM(m.REQUESTQUANTITY - m.ASSIGNEDQUANTITY)  QTY,MATERIALSPECNAME FROM MATERIALSHIPREQUESTDETAIL m WHERE m.SHIPREQUESTNAME  = :SHIPREQUESTNAME GROUP BY MATERIALSPECNAME";
			List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql222, hashMap);
			for (int i = 0; i < queryForList2.size(); i++) {
				String string = queryForList2.get(i).get("QTY").toString();
				String MATERIALSPECNAME = queryForList2.get(i).get("MATERIALSPECNAME").toString();
				BigDecimal bigDecimal = new BigDecimal(string);
				if (bigDecimal.compareTo(BigDecimal.ZERO) < 0 ) {
					throw new CustomException( MATERIALSPECNAME + "销售不允许超发");
				}
			}
		}
		//备份过账条码表的信息，并删除
		String bfSql = "INSERT\r\n" +
				"	INTO\r\n" +
				"	MATERIALPACKINGSUBHISTORY T (T.MATERIALPACKINGNAME,\r\n" +
				"	T.RECEIVEREQUESTNAME,\r\n" +
				"	T.RECEIVEREQUESTDETAILNAME,\r\n" +
				"	T.QTY)\r\n" +
				"SELECT\r\n" +
				"	M.MATERIALPACKINGNAME,\r\n" +
				"	M.RECEIVEREQUESTNAME,\r\n" +
				"	M.RECEIVEREQUESTDETAILNAME,\r\n" +
				"	M.QTY\r\n" +
				"FROM\r\n" +
				"	MATERIALPACKINGSUB M\r\n" +
				"WHERE\r\n" +
				"	M.RECEIVEREQUESTNAME = :SHIPREQUESTNAME";
		String scSql = "DELETE FROM MATERIALPACKINGSUB WHERE RECEIVEREQUESTNAME  = :SHIPREQUESTNAME";
		IDMFrameServiceProxy.getSqlTemplate().update(bfSql, hashMap);
		IDMFrameServiceProxy.getSqlTemplate().update(scSql, hashMap);
		EventInfoUtil eventInfoUtil = new EventInfoUtil();
		EventInfo makeEventInfo = eventInfoUtil.makeEventInfo("CompletedShipRequest", eventUser, "CompletedShipRequest");
		//循环过账批次
		for (int i = 0; i < list.size(); ++ i) {
			String materialPackingName = list.get(i).getMaterialPackingName();
			String sdk_ID = list.get(i).getSDK_ID();
			String phase = list.get(i).getPHASE();
			String materialSpecName = list.get(i).getMaterialSpecName();
			//			float materialQuantity = list.get(i).getMaterialQuantity();
			//重新获取下数量，通过sql查询取数
			String SLsQL = "SELECT m.MATERIALPACKINGNAME,m.MATERIALQUANTITY,"
					+ "DECODE(NVL(m.MATERIALQUANTITY2,0), 0 ,m.MATERIALQUANTITY,m.MATERIALQUANTITY2) MATERIALQUANTITY2  FROM MATERIALPACKING m"
					+ " WHERE m.SHIPREQUESTNAME = :SHIPRQEUESTNAME AND MATERIALPACKINGNAME = :MATERIALPACKINGNAME ";
			Map<String, Object> hashMap2 = new HashMap<String,Object> ();
			hashMap2.put("SHIPRQEUESTNAME", shipRequestName);
			hashMap2.put("MATERIALPACKINGNAME", materialPackingName);
			String materialQuantity2 = list.get(i).getMaterialQuantity2() + "";
			List<Map<String, Object>> queryForList3 = IDMFrameServiceProxy.getSqlTemplate().queryForList(SLsQL, hashMap2);
			if (queryForList3 != null && queryForList3.size() > 0) {
				materialQuantity2 = queryForList3.get(0).get("MATERIALQUANTITY2").toString();
			} else {
				throw new CustomException("单据" + shipRequestName + "库存"  + materialPackingName  + "绑定关系不存在");
			}
			//循环单据查询过帐表
			String pcSql = "SELECT SHIPREQUESTNAME, SHIPREQUESTDETAILNAME,\r\n" +
					"	m.MATERIALSPECNAME ,\r\n" +
					"	m.PHASE ,\r\n" +
					"	m.SDK_ID ,\r\n" +
					"	m.REQUESTQUANTITY ,\r\n" +
					"	m.ASSIGNEDQUANTITY ,\r\n" +
					"	(m.REQUESTQUANTITY - nvl(m.SALEOUTQUANTITY, 0) ) SALEOUTQUANTITY\r\n" +
					"FROM\r\n" +
					"	MATERIALSHIPREQUESTDETAIL m\r\n" +
					"WHERE\r\n" +
					"	m.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" +
					"	AND SHIPREQUESTNAME = :SHIPREQUESTNAME  ORDER BY TO_NUMBER(SHIPREQUESTDETAILNAME) ASC ";
			hashMap.put("MATERIALSPECNAME", materialSpecName);
			hashMap.put("SDK_ID", sdk_ID);
			hashMap.put("PHASE", phase);
			List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(pcSql, hashMap);
			BigDecimal pcQty = new BigDecimal(materialQuantity2);//批次数量
			BigDecimal temp;
			for (int j = 0; j < queryForList2.size(); j++) {
				String sq = queryForList2.get(j).get("SALEOUTQUANTITY").toString();//剩余发货数量
				String shipRequestDetailName = queryForList2.get(j).get("SHIPREQUESTDETAILNAME").toString();//行项
				BigDecimal saleQty = new BigDecimal(sq);//需要发货数量
				if (pcQty.compareTo(BigDecimal.ZERO) == 0
						|| (saleQty.compareTo(BigDecimal.ZERO) == 0  && (j != queryForList2.size() - 1) )
				) {
					continue;
				}
				int compareTo = saleQty.compareTo(pcQty);//大于等于0则批次全发,小于0则需要拆分
				int compareToZero=saleQty.compareTo(BigDecimal.ZERO);
				if (compareTo >= 0) {
					untils.fahuoSplit(materialPackingName, shipRequestName, shipRequestDetailName, pcQty);
					break;
				} else {//需要拆行
					if( (j != queryForList2.size() - 1) &&  compareToZero>0) {//判断是否是最后一行，不是最后一行就不允许超发，发货数量等于请求数量即拆
						untils.fahuoSplit(materialPackingName, shipRequestName, shipRequestDetailName, saleQty);
						pcQty = pcQty.subtract(saleQty);
						continue;
					} else {
						untils.fahuoSplit(materialPackingName, shipRequestName, shipRequestDetailName, pcQty);
						break;
					}
				}
			}
			materialPackingKeyList.add(materialPackingName);
		}
		//判断会有多少个仓库发货，不同仓库需要分开走账
		StringBuffer cklx = new StringBuffer();
		cklx.append("SELECT m.MATERIALPACKINGNAME,m.SITENAME FROM MATERIALPACKING m WHERE m.MATERIALPACKINGNAME IN ");
		String str = "";
		str += "(";
		for (String materialPacking : materialPackingKeyList) {
			str += "'" + materialPacking + "',";
		}
		str += "'')";
		// 查询MATERIALQUANTITY 与 MATERIALQUANTITY2是否相等，如果相等则未修改数量，不相等则修改数量
		List<Map<String, Object>> packingQueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(cklx.toString() + str, hashMap);

		String billCode = "";
		switch (shipRequestType) {
			case "NLCC" : //STO调拨出库

				//	拆条码，查询MATERIALQUANTITY 与 MATERIALQUANTITY2是否相等，如果相等则未修改了数量，不相等则修改了数量
				//如果已修改数量，则进行拆条码
				for (int z = 0; z < packingQueryForList.size(); z++) {
					String MATERIALPACKINGNAME = packingQueryForList.get(z).get("MATERIALPACKINGNAME").toString();
					Object[] bindSet = {MATERIALPACKINGNAME};
					String qtysql = "WHERE MATERIALPACKINGNAME = ?";
					List<MaterialPacking> qtyQueryForList = MaterialPackingServiceProxy.getMaterialPackingService().select(qtysql, bindSet);

					String qty = new Double(qtyQueryForList.get(0).getMaterialQuantity()) == null ? "0" : String.valueOf(qtyQueryForList.get(0).getMaterialQuantity());

					String qty2 = new Double(qtyQueryForList.get(0).getMaterialQuantity2()) == null ? "0" : String.valueOf(qtyQueryForList.get(0).getMaterialQuantity2());
					String materialPackingName = qtyQueryForList.get(0).getMaterialPackingName();
					String charge = qtyQueryForList.get(0).getCharge();
					BigDecimal bigQty = new BigDecimal(qty);
					// 已拆数量
					BigDecimal bigQty2 = new BigDecimal(qty2);

					// 如果已拆数量不等于原数量，并且已拆数量为0(未拆分数量)
					if(bigQty.compareTo(bigQty2) != 0 && !"0.0".equals(qty2)) {
						// 剩余数量
						BigDecimal restQty = bigQty.subtract(bigQty2);

						// 生成新的拆分条码
						String stringBigQty2 = bigQty2.toString();
						String newPalletNo = CodeGenerator.packingGenerateCode(materialPackingName,stringBigQty2,charge);

						// 写入拆分的新条码，更新原数据库
						List<MaterialPacking> boxList = new ArrayList<MaterialPacking>();
//							MaterialPackingKey materialpackingKey = new MaterialPackingKey();
						MaterialPacking materialpackingNew = new MaterialPacking();
//							materialpackingKey.setSiteName("SDK");
//							materialpackingKey.setMaterialPackingName(newPalletNo);
//							materialpackingNew.setKey(materialpackingKey);

						// 复制一个当前的原标签数据
						materialpackingNew.setKey(qtyQueryForList.get(0).getKey());
//							MaterialPacking materialpackingNew = qtyQueryForList.get(z);
						// 设置新为标签和数量
						materialpackingNew.setMaterialPackingName(newPalletNo);
						// bigQty2 转double类型
						materialpackingNew.setMaterialQuantity(bigQty2.doubleValue());
						boxList.add(qtyQueryForList.get(0));
						boxList.add(materialpackingNew);
						boolean moveTransformOut = packingChargeSplit(boxList, eventUser);
						if (moveTransformOut != true) {
							throw new RuntimeException("修改数量拆分失败");
						}

						//记录到拆分表
						String subsql = "SELECT RECEIVEREQUESTDETAILNAME FROM MATERIALPACKINGSUB WHERE RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND MATERIALPACKINGNAME = :MATERIALPACKINGNAME\n";
						Map<String, Object> subHashMap = new HashMap<String, Object>();
						subHashMap.put("RECEIVEREQUESTNAME", shipRequestName);
						subHashMap.put("MATERIALPACKINGNAME", MATERIALPACKINGNAME);
						List<Map<String, Object>> ZKqueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(subsql, subHashMap);
						String preReceiverequestdetailname = ZKqueryForList.get(0).get("RECEIVEREQUESTDETAILNAME").toString();

						String shipRequestDetailName = qtyQueryForList.get(0).getShipRequestDetailName() == null ? "" : qtyQueryForList.get(0).getShipRequestDetailName().toString();
						untils.fahuoSplit(newPalletNo, shipRequestName, preReceiverequestdetailname, bigQty2);

						// 原条码扣减拆出的数量，用剩余数量更新原条码，条码不变，数量更新
						SetEventInfo setEventInfo = new SetEventInfo();
						Map<String, Object> pakHashMap = new HashMap<>();
						pakHashMap.put("materialQuantity", restQty.doubleValue());
						pakHashMap.put("materialQuantity2", 0);
						pakHashMap.put("shipRequestName", "");
						pakHashMap.put("shipRequestDetailName", "");

						EventInfo eventInfo = new EventInfoUtil().makeEventInfo("备货出库调拨出拆分条码", eventUser, "拆分条码");
						setEventInfo.setUserColumns(pakHashMap);
						MaterialPackingServiceProxy.getMaterialPackingService().setEvent(new MaterialPackingKey( "SDK", MATERIALPACKINGNAME), eventInfo, setEventInfo);

						// 删除被拆分的原条码数据，用拆分出的新条码覆盖，进行后面的过账，现场仓过账只过账已拆分出的数量，剩余数量的原条码不过账
						boolean deletePacking = materialPackingKeyList.remove(MATERIALPACKINGNAME);
						if (deletePacking) {
							materialPackingKeyList.add(newPalletNo);
						}

					}

				}

				makeEventInfo.setEventName("调拨出库");
				makeEventInfo.setEventComment("备货调拨出库");
				billCode=TransferStockOut_NLCC(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;

			case "01"://生产领料
			case "02"://生产补料
				if ("01".equals(shipRequestType)){
					makeEventInfo.setEventName("CompletedShipRequestLL");
					makeEventInfo.setEventComment("CompletedShipRequestLL");
				}else {
					makeEventInfo.setEventName("CompletedShipRequestBL");
					makeEventInfo.setEventComment("CompletedShipRequestBL");
				}

				// 判断是否是现场仓
				String IsSql = "SELECT m.ERPRECEIVELOCATION,m.ERPLOCATION,m.ERPRECEIVEFACTORY FROM MATERIALSHIPREQUESTDETAIL m WHERE  SHIPREQUESTNAME = :SHIPREQUESTNAME";
				Map<String,Object> bpc=new HashMap<>();
				bpc.put("SHIPREQUESTNAME", shipRequestName);
				List<Map<String,Object>> receiveList = IDMFrameServiceProxy.getSqlTemplate().queryForList(IsSql, bpc);
				String ERPRECEIVELOCATION = receiveList.get(0).get("ERPRECEIVELOCATION") == null ? "" : receiveList.get(0).get("ERPRECEIVELOCATION").toString();
				String ERPLOCATION = receiveList.get(0).get("ERPLOCATION") == null ? "" : receiveList.get(0).get("ERPLOCATION").toString();
				String ERPRECEIVEFACTORY = receiveList.get(0).get("ERPRECEIVEFACTORY") == null ? "" : receiveList.get(0).get("ERPRECEIVEFACTORY").toString();

				//判断是否为MES需要二次接收的物料
				/*String sqlc=" SELECT e.ENUMVALUE FROM MES_ENUMDEFVALUE e \r\n" +
						" WHERE e.ENUMNAME ='ConfirmReceiveArea'\r\n" +
						" AND e.ENUMVALUE in(\r\n" +
						" SELECT p.AREANAME FROM MES_PRODUCTREQUEST p \r\n" +
						" WHERE p.PRODUCTREQUESTNAME =(SELECT m.RECEIVEPRODUCTIONORDERNUMBER FROM MATERIALSHIPREQUESTDETAIL m \r\n" +
						" WHERE m.SHIPREQUESTNAME=:SHIPREQUESTNAME AND RECEIVEPRODUCTIONORDERNUMBER IS NOT NULL AND ROWNUM=1))";*/
				List<Map<String,Object>> src = new ArrayList<Map<String,Object>>();
				String sqlQuery = " SELECT m.RECEIVEPRODUCTIONORDERNUMBER FROM MATERIALSHIPREQUESTDETAIL m " +
						" WHERE m.SHIPREQUESTNAME=:SHIPREQUESTNAME AND RECEIVEPRODUCTIONORDERNUMBER IS NOT NULL AND ROWNUM=1";
				List<Map<String,Object>> materialShipRequestDetailList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlQuery, bpc);
				if (null != materialShipRequestDetailList && !materialShipRequestDetailList.isEmpty()){
					String productRequestName = materialShipRequestDetailList.get(0).get("RECEIVEPRODUCTIONORDERNUMBER") == null ? "" : materialShipRequestDetailList.get(0).get("RECEIVEPRODUCTIONORDERNUMBER").toString().substring(4);
					String sqlc = " SELECT e.ENUMVALUE FROM ENUMDEFVALUE@MES_PRODUCTREQUEST e \r\n" +
							" WHERE e.ENUMNAME ='ConfirmReceiveArea'\r\n" +
							" AND e.ENUMVALUE in(\r\n" +
							" SELECT p.AREANAME FROM PRODUCTREQUEST@MES_PRODUCTREQUEST p \r\n" +
							" WHERE p.PRODUCTREQUESTNAME =:PRODUCTREQUESTNAME)";
					bpc.put("PRODUCTREQUESTNAME", productRequestName);
					src = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlc, bpc);
				}
				//MES需要接收，则只抛送数据。 或如果是现场仓,先移到现场仓
				if (src.size()>0){
					billCode="Y";
					String sqlZK=" SELECT\r\n" +
							"	t.MATERIALSPECNAME,m.MATERIALPACKINGNAME,\r\n" +
							"	t.ERPFACTORY,\r\n" +
							"	t.ERPLOCATION,\r\n" +
							"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
							"	t.UNIT,\r\n" +
							"	m1.COSTCENTER,\r\n" +
							"	m1.SHIPREQUESTNAME,\r\n" +
							"	m1.SHIPREQUESTDETAILNAME,\r\n" +
							"	m1.ERPRECEIVELOCATION,\r\n" +
							"	m1.ERPRECEIVEFACTORY,\r\n" +
							"	m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
							"	CASE WHEN m1.SENDSALESORDER IS NOT NULL THEN 'E' ELSE '' END SOBKZ,\r\n" +
							"	(\r\n" +
							"	SELECT\r\n" +
							"		RECEIVEPRODUCTIONORDERNUMBER\r\n" +
							"	FROM\r\n" +
							"		MATERIALSHIPREQUESTDETAIL m5\r\n" +
							"	WHERE\r\n" +
							"		m5.SHIPREQUESTNAME =:SHIPREQUESTNAME\r\n" +
							"		AND m5.MATERIALSPECNAME = t.MATERIALSPECNAME\r\n" +
							"	ORDER BY\r\n" +
							"		m5.USEDATE,\r\n" +
							"		m5.SHIPREQUESTDETAILNAME FETCH FIRST ROW ONLY)RECEIVEPRODUCTIONORDERNUMBER,\r\n" +
							"	SUM(m.QTY) AS MATERIALQUANTITY\r\n" +
							" FROM \r\n" +
							"		 MATERIALPACKINGSUB m\r\n" +
							" LEFT JOIN MATERIALPACKING T ON \r\n" +
							"		 m.MATERIALPACKINGNAME = T.MATERIALPACKINGNAME\r\n" +
							"	AND m.RECEIVEREQUESTNAME = T.SHIPREQUESTNAME\r\n" +
							" LEFT JOIN MATERIALSHIPREQUESTDETAIL M1  \r\n" +
							"		  ON \r\n" +
							"		 m.RECEIVEREQUESTNAME = M1.SHIPREQUESTNAME\r\n" +
							"	AND m.RECEIVEREQUESTDETAILNAME = M1.SHIPREQUESTDETAILNAME\r\n" +
							" LEFT JOIN MATERIALSHIPREQUEST M2  \r\n" +
							"		  ON \r\n" +
							"		 M1.SHIPREQUESTNAME = M2.SHIPREQUESTNAME\r\n" +
							" WHERE \r\n" +
							"		 T.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" +
							"	AND T.SITENAME = :SITENAME\r\n" +
							"	AND T.STOCKSTATE = 'Stocked'\r\n" +
							"	AND T.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
							" GROUP BY\r\n" +
							"	t.MATERIALSPECNAME,\r\n" +
							"	t.ERPFACTORY,\r\n" +
							"	t.ERPLOCATION,\r\n" +
							"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
							"	t.UNIT,\r\n" +
							"	m1.COSTCENTER,\r\n" +
							"	m1.SHIPREQUESTNAME,m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
							"	m1.SHIPREQUESTDETAILNAME,\r\n" +
							"	m1.ERPRECEIVELOCATION,\r\n" +
							"	m1.ERPRECEIVEFACTORY,m.MATERIALPACKINGNAME";

					// 转库到线边仓
					Map<String, Object> hashMapZK = new HashMap<String, Object>();
					hashMapZK.put("BOXLIST", materialPackingKeyList);
					hashMapZK.put("SHIPREQUESTNAME", shipRequestName);
					hashMapZK.put("SITENAME", siteName);
					List<Map<String, Object>> ZKqueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlZK.toString(), hashMapZK);

					for (int z = 0; z < ZKqueryForList.size(); z++) {
						String materialPackingName = ZKqueryForList.get(z).get("MATERIALPACKINGNAME").toString();
						materialPackingKeyList.add(materialPackingName);
						MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",materialPackingName);
						arrayList.add(materialPackingKey);
					}
					/*List<MaterialPackingKey> mpKeyList = new ArrayList<MaterialPackingKey> ();
					SetEventInfo setEventInfo = new SetEventInfo();
					for (MaterialPackingKey materialPacking : arrayList) {
						MaterialPackingKey key = materialPacking;
						mpKeyList.add(key);
					}

					Map<String, Object> hashMapPack = new HashMap<String,Object> ();
					hashMapPack.put("erpLocation", ERPRECEIVELOCATION == null  ? "" : ERPRECEIVELOCATION);
					hashMapPack.put("erpFactory",  ERPRECEIVEFACTORY == null  ? "" : ERPRECEIVEFACTORY);
					hashMapPack.put("locationName", "");
					hashMapPack.put("lastEventUser", eventUser);
					hashMapPack.put("unDoID", billCode);
					setEventInfo.setUserColumns(hashMapPack);
					MaterialPackingServiceProxy.getMaterialPackingService().setEvent(mpKeyList, makeEventInfo, setEventInfo);
					*/
					//抛送MES数据
					//untils.sendMaterialInfoToMES_NEW2(shipRequestName, "", materialPackingKeyList);
					untils.StockOutSendMaterialInfoToMES(shipRequestName, "", materialPackingKeyList);
					//更新BOX状态
					EventInfo mesEventInfo = eventInfoUtil.makeEventInfo("CompletedShipRequestMES", eventUser, "CompletedShipRequestMES");
					SetEventInfo setEventInfo = new SetEventInfo();
					Map<String, Object> hashMapPack = new HashMap<String,Object> ();
					hashMapPack.put("stockState", GenericServiceProxy.getConstantMap().STOCKSTATE_STOCKPENDING);
					hashMapPack.put("oldStockState", GenericServiceProxy.getConstantMap().STOCKSTATE_STOCKED);
					setEventInfo.setUserColumns(hashMapPack);
					MaterialPackingServiceProxy.getMaterialPackingService().setEvent(arrayList, mesEventInfo, setEventInfo);
				} else {
					if(!ERPRECEIVELOCATION.isEmpty()) {

						//					 拆条码，查询MATERIALQUANTITY 与 MATERIALQUANTITY2是否相等，如果相等则未修改了数量，不相等则修改了数量
						//					 如果已修改数量，则进行拆条码
						for (int z = 0; z < packingQueryForList.size(); z++) {
							String MATERIALPACKINGNAME = packingQueryForList.get(z).get("MATERIALPACKINGNAME").toString();
							Object[] bindSet = {MATERIALPACKINGNAME};
							String qtysql = "WHERE MATERIALPACKINGNAME = ?";
							List<MaterialPacking> qtyQueryForList = MaterialPackingServiceProxy.getMaterialPackingService().select(qtysql, bindSet);

							String qty = new Double(qtyQueryForList.get(0).getMaterialQuantity()) == null ? "0" : String.valueOf(qtyQueryForList.get(0).getMaterialQuantity());

							String qty2 = new Double(qtyQueryForList.get(0).getMaterialQuantity2()) == null ? "0" : String.valueOf(qtyQueryForList.get(0).getMaterialQuantity2());
							String materialPackingName = qtyQueryForList.get(0).getMaterialPackingName();
							String charge = qtyQueryForList.get(0).getCharge();
							BigDecimal bigQty = new BigDecimal(qty);
							// 已拆数量
							BigDecimal bigQty2 = new BigDecimal(qty2);

							// 如果已拆数量不等于原数量，并且已拆数量为0(未拆分数量)
							if(bigQty.compareTo(bigQty2) != 0 && !"0.0".equals(qty2)) {
								// 剩余数量
								BigDecimal restQty = bigQty.subtract(bigQty2);

								// 生成新的拆分条码
								String stringBigQty2 = bigQty2.toString();
								String newPalletNo = CodeGenerator.packingGenerateCode(materialPackingName,stringBigQty2,charge);

								// 写入拆分的新条码，更新原数据库
								List<MaterialPacking> boxList = new ArrayList<MaterialPacking>();
								//							MaterialPackingKey materialpackingKey = new MaterialPackingKey();
								MaterialPacking materialpackingNew = new MaterialPacking();
								//							materialpackingKey.setSiteName("SDK");
								//							materialpackingKey.setMaterialPackingName(newPalletNo);
								//							materialpackingNew.setKey(materialpackingKey);

								// 复制一个当前的原标签数据
								materialpackingNew.setKey(qtyQueryForList.get(0).getKey());
								//							MaterialPacking materialpackingNew = qtyQueryForList.get(z);
								// 设置新为标签和数量
								materialpackingNew.setMaterialPackingName(newPalletNo);
								// bigQty2 转double类型
								materialpackingNew.setMaterialQuantity(bigQty2.doubleValue());
								boxList.add(qtyQueryForList.get(0));
								boxList.add(materialpackingNew);
								boolean moveTransformOut = packingChargeSplit(boxList, eventUser);
								if (moveTransformOut != true) {
									throw new RuntimeException("修改数量拆分失败");
								}

								//记录到拆分表
								String subsql = "SELECT RECEIVEREQUESTDETAILNAME FROM MATERIALPACKINGSUB WHERE RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND MATERIALPACKINGNAME = :MATERIALPACKINGNAME\n";
								Map<String, Object> subHashMap = new HashMap<String, Object>();
								subHashMap.put("RECEIVEREQUESTNAME", shipRequestName);
								subHashMap.put("MATERIALPACKINGNAME", MATERIALPACKINGNAME);
								List<Map<String, Object>> ZKqueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(subsql, subHashMap);
								String preReceiverequestdetailname = ZKqueryForList.get(0).get("RECEIVEREQUESTDETAILNAME").toString();

								String shipRequestDetailName = qtyQueryForList.get(0).getShipRequestDetailName() == null ? "" : qtyQueryForList.get(0).getShipRequestDetailName().toString();
								untils.fahuoSplit(newPalletNo, shipRequestName, preReceiverequestdetailname, bigQty2);

								// 原条码扣减拆出的数量，用剩余数量更新原条码，条码不变，数量更新
								SetEventInfo setEventInfo = new SetEventInfo();
								Map<String, Object> pakHashMap = new HashMap<>();
								pakHashMap.put("materialQuantity", restQty.doubleValue());
								pakHashMap.put("materialQuantity2", 0);
								pakHashMap.put("shipRequestName", "");
								pakHashMap.put("shipRequestDetailName", "");

								EventInfo eventInfo = new EventInfoUtil().makeEventInfo("拆分条码", eventUser, "备货出库拆分条码");
								setEventInfo.setUserColumns(pakHashMap);
								MaterialPackingServiceProxy.getMaterialPackingService().setEvent(new MaterialPackingKey( "SDK", MATERIALPACKINGNAME), eventInfo, setEventInfo);

								// 删除被拆分的原条码数据，用拆分出的新条码覆盖，进行后面的过账，现场仓过账只过账已拆分出的数量，剩余数量的原条码不过账
								boolean deletePacking = materialPackingKeyList.remove(MATERIALPACKINGNAME);
								if (deletePacking) {
									materialPackingKeyList.add(newPalletNo);
								}

							}

						}


						//new MESServiceImpl().NeedReceiveMaterialInfoSend_NEW(shipRequestName, "SDK", materialPackingKeyList);
						billCode="Y";
						String sqlZK=" SELECT\r\n" +
								"	t.MATERIALSPECNAME,m.MATERIALPACKINGNAME,\r\n" +
								"	t.ERPFACTORY,\r\n" +
								"	t.ERPLOCATION,\r\n" +
								"	t.LOCATIONNAME,\r\n" +
								"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
								"	t.UNIT,\r\n" +
								"	m1.COSTCENTER,\r\n" +
								"	m1.SHIPREQUESTNAME,\r\n" +
								"	m1.SHIPREQUESTDETAILNAME,\r\n" +
								"	m1.ERPRECEIVELOCATION,\r\n" +
								"	m1.ERPRECEIVEFACTORY,\r\n" +
								"	m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
								"	CASE WHEN m1.SENDSALESORDER IS NOT NULL THEN 'E' ELSE '' END SOBKZ,\r\n" +
								"	(\r\n" +
								"	SELECT\r\n" +
								"		RECEIVEPRODUCTIONORDERNUMBER\r\n" +
								"	FROM\r\n" +
								"		MATERIALSHIPREQUESTDETAIL m5\r\n" +
								"	WHERE\r\n" +
								"		m5.SHIPREQUESTNAME =:SHIPREQUESTNAME\r\n" +
								"		AND m5.MATERIALSPECNAME = t.MATERIALSPECNAME\r\n" +
								"	ORDER BY\r\n" +
								"		m5.USEDATE,\r\n" +
								"		m5.SHIPREQUESTDETAILNAME FETCH FIRST ROW ONLY)RECEIVEPRODUCTIONORDERNUMBER,\r\n" +
								"	SUM(m.QTY) AS MATERIALQUANTITY\r\n" +
								" FROM \r\n" +
								"		 MATERIALPACKINGSUB m\r\n" +
								" LEFT JOIN MATERIALPACKING T ON \r\n" +
								"		 m.MATERIALPACKINGNAME = T.MATERIALPACKINGNAME\r\n" +
								"	AND m.RECEIVEREQUESTNAME = T.SHIPREQUESTNAME\r\n" +
								" LEFT JOIN MATERIALSHIPREQUESTDETAIL M1  \r\n" +
								"		  ON \r\n" +
								"		 m.RECEIVEREQUESTNAME = M1.SHIPREQUESTNAME\r\n" +
								"	AND m.RECEIVEREQUESTDETAILNAME = M1.SHIPREQUESTDETAILNAME\r\n" +
								" LEFT JOIN MATERIALSHIPREQUEST M2  \r\n" +
								"		  ON \r\n" +
								"		 M1.SHIPREQUESTNAME = M2.SHIPREQUESTNAME\r\n" +
								" WHERE \r\n" +
								"		 T.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" +
								"	AND T.SITENAME = :SITENAME\r\n" +
								"	AND T.STOCKSTATE = 'Stocked'\r\n" +
								"	AND T.MATERIALPACKINGNAME IN (:BOXLIST)\r\n" +
								" GROUP BY\r\n" +
								"	t.MATERIALSPECNAME,\r\n" +
								"	t.ERPFACTORY,\r\n" +
								"	t.ERPLOCATION,\r\n" +
								"	t.LOCATIONNAME,\r\n" +
								"	m2.SHIPREQUESTDETAILTYPE,\r\n" +
								"	t.UNIT,\r\n" +
								"	m1.COSTCENTER,\r\n" +
								"	m1.SHIPREQUESTNAME,m1.SENDSALESORDER ,m1.SENDSALESORDERDETAILNO,\r\n" +
								"	m1.SHIPREQUESTDETAILNAME,\r\n" +
								"	m1.ERPRECEIVELOCATION,\r\n" +
								"	m1.ERPRECEIVEFACTORY,m.MATERIALPACKINGNAME";

						// 转库到线边仓
//					String sqlZK = "SELECT b.MATERIALPACKINGNAME FROM MATERIALPACKINGSUB b WHERE b.MATERIALPACKINGNAME IN (:BOXLIST)";
						Map<String, Object> hashMapZK = new HashMap<String, Object>();
						hashMapZK.put("BOXLIST", materialPackingKeyList);
						hashMapZK.put("SHIPREQUESTNAME", shipRequestName);
						hashMapZK.put("SITENAME", siteName);
						List<Map<String, Object>> ZKqueryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlZK.toString(), hashMapZK);
						List<String> SUBList = new ArrayList<> ();
						for (int z = 0; z < ZKqueryForList.size(); z++) {
							String materialPackingName = ZKqueryForList.get(z).get("MATERIALPACKINGNAME").toString();
							SUBList.add(materialPackingName);
							MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",materialPackingName);
							arrayList.add(materialPackingKey);

							// 更新MATERIALPACKING表前仓库货位为现在货位和仓库数据,保存现仓库货位
							Map<String, Object> row = ZKqueryForList.get(z);
							String erpLocation = row.get("ERPLOCATION") == null ? "" : row.get("ERPLOCATION").toString();
							String locationName = row.get("LOCATIONNAME") == null ? "" : row.get("LOCATIONNAME").toString();
							String updSql = "UPDATE MATERIALPACKING SET PREERPLOCATION = :PREERPLOCATION, PRELOCATIONNAME = :PRELOCATIONNAME WHERE MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
							Map<String, Object> updHashMap = new HashMap<>();
							updHashMap.put("PREERPLOCATION", erpLocation);
							updHashMap.put("PRELOCATIONNAME", locationName);
							updHashMap.put("MATERIALPACKINGNAME", materialPackingName);
							try {
								IDMFrameServiceProxy.getSqlTemplate().update(updSql, updHashMap);
							} catch (Exception e) {
								// 日志记录并抛出自定义异常，便于调试和事务回滚
								throw new CustomException("更新仓库货位信息失败: " + materialPackingName, e);
							}
						}
						billCode = UnSourcedChangeLocation(arrayList,ERPRECEIVEFACTORY,
								ERPRECEIVELOCATION, "", eventUser, commitDate );

						List<MaterialPackingKey> mpKeyList = new ArrayList<MaterialPackingKey> ();
						SetEventInfo setEventInfo = new SetEventInfo();
						for (MaterialPackingKey materialPacking : arrayList) {
							MaterialPackingKey key = materialPacking;
							mpKeyList.add(key);
						}

						Map<String, Object> hashMapPack = new HashMap<String,Object> ();
						hashMapPack.put("erpLocation", ERPRECEIVELOCATION == null  ? "" : ERPRECEIVELOCATION);
						hashMapPack.put("erpFactory",  ERPRECEIVEFACTORY == null  ? "" : ERPRECEIVEFACTORY);
						hashMapPack.put("locationName", "");
						hashMapPack.put("lastEventUser", eventUser);
						hashMapPack.put("unDoID", billCode);
						setEventInfo.setUserColumns(hashMapPack);
						MaterialPackingServiceProxy.getMaterialPackingService().setEvent(mpKeyList, makeEventInfo, setEventInfo);
						//抛送MES数据
						Boolean success = untils.sendMaterialInfoToMES_NEW2(shipRequestName, "", SUBList,billCode,eventUser);

						if (!success) {
							String undo = cancelShipInter(billCode, eventUser);
							if (undo == null || undo.isEmpty()) {
								throw new GlobalException("下发mes失败后冲销失败！");
							}else {
								throw new GlobalException("发送到mes失败！");
							}
						}
						makeEventInfo.setEventName("备货出库无来源转库");
						makeEventInfo.setEventComment("备货出库无来源转库");
//						WlySaveUnDoInfo( billCode,commitDate);
						untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
					}
					else
					{
//						billCode=toSAPService.ProductionStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList);

						billCode=ProductionStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
						if (billCode.isEmpty()) {
							throw new CustomException("SAP过账异常！");
						}
//
						//抛送MES数据
						Boolean success = untils.sendMaterialInfoToMES_NEW(shipRequestName, "", materialPackingKeyList,billCode,eventUser);
						//billCode = NCWServiceImpl.SaleOut2(shipRequestName, "SDK", eventUser,materialPackingKeyList,"");
						// 如果mes发送失败，则进行sap过账冲销
						if (!success) {
							String undo = cancelShipInter(billCode, eventUser);
							if (undo == null || undo.isEmpty()) {
								throw new GlobalException("下发mes失败后冲销失败！");
							}else {
								throw new GlobalException("发送到mes失败！");
							}
						}

						untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
					}
				}
				break;

			case "06"://成本中心领料
				//				billCode=toSAPService.orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList);
				billCode=orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
				//抛送MES数据
				Boolean success = untils.sendMaterialInfoToMES_NEW(shipRequestName, "", materialPackingKeyList,billCode,eventUser);

				if (!success) {
					String undo = cancelShipInter(billCode, eventUser);
					if (undo == null || undo.isEmpty()) {
						throw new GlobalException("下发mes失败后冲销失败！");
					}else {
						throw new GlobalException("发送到mes失败！");
					}
				}

				//更新MES管芯数据
				new MESServiceImpl().updateDurable_ByCostcenter(shipRequestName, "SDK", eventUser, materialPackingKeyList);
				//billCode = NCWServiceImpl.SaleOut2(shipRequestName, "SDK", eventUser,materialPackingKeyList,"");
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			case "09"://研发领料
//				billCode=toSAPService.orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList);
//				billCode=orderStockOut(shipRequestName, "SDK", eventUser,materialPackingKeyList);
//				//更新MES管芯数据
//				new MESServiceImpl().updateDurable_ByCostcenter(shipRequestName, "SDK", eventUser, materialPackingKeyList);
//				//billCode = NCWServiceImpl.SaleOut2(shipRequestName, "SDK", eventUser,materialPackingKeyList,"");
//				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			case "ZLF0"://销售出库确认 （单据类型ZLF0~ZLF8） SD023(销售回传)
			case "ZLF1":
			case "ZLF2":
			case "ZLF3":
			case "ZLF4":
			case "ZLF5":
			case "ZLF6":
			case "ZLF7":
			case "ZLF8":
				makeEventInfo.setEventName("CompletedShipRequestZL");
				makeEventInfo.setEventComment("CompletedShipRequestZL");
//				billCode=toSAPService.saleStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList);
				billCode=saleStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList,commitDate);
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				//单据类型等于ZLF0-ZLF9，物料编码以7开头都属于成品胶水需要调用QMS质检接口
				String message = checkShipRequestTypeSendIQC(commitDate, siteName ,shipRequestName, makeEventInfo, materialPackingKeyList);
				if (StringUtils.isNotEmpty(message)) {
					throw new CustomException(message);
				}
				break;
			case "08":    // 委外
			case "OEM":  // 委外
//				billCode=toSAPService.oemStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList);
				billCode=oemStockOut_NEW(shipRequestName, "SDK", eventUser,materialPackingKeyList, commitDate);
				untils.SaveUnDoInfo_ForSap(materialPackingKeyList, billCode, makeEventInfo);
				break;
			default :
				break;
		}
		return billCode;
	}
}
