package com.cim.idm.yard.service;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class YardDeviceReceiveQuestServiceImpl {

	private static Log logger = LogFactory.getLog("YardDeviceReceiveQuestServiceImpl");
	
	/**
	 * 生成提货单号
	 * @throws Exception 
	 */
	public String CreateDeviceShipeQuest(Document message,EventInfo eventInfo)
	{
		Object DeviceShipId=System.currentTimeMillis();
		String returnId=DeviceShipId.toString();
		//下面是Device表插入方式。
		Element root = message.getDocument().getRootElement();//获取其根元素
		Element bodyElement = root.getChild("Body");//获取body元素
		Element deviceListElement = bodyElement.getChild("SaveItemList");//获取YARD_DEVICE元素
		//用于暂时存储的变量
		String SITENAME="";
		String CONTAINER="";
		String DEVICE="";
		String MOVEDATE="";
		String MOVEENTRANCE="";
		String MOVEORDER="";
		String TRANSPORTREQUIRE="";
		String CaseNo="";
		//遍历YARD_DEVICE，获取每个元素的值
		for (Iterator<?> iterator = deviceListElement.getChildren().iterator(); iterator.hasNext();) {
			try {
			Element deviceElement = (Element) iterator.next();
			SITENAME = deviceElement.getChild("SITENAME").getText();
			CONTAINER = deviceElement.getChild("CONTAINER").getText();
			DEVICE = deviceElement.getChild("DEVICE").getText();
			MOVEDATE = deviceElement.getChild("MOVEDATE").getText();
			MOVEENTRANCE=deviceElement.getChild("MOVEENTRANCE").getText();
			MOVEORDER=deviceElement.getChild("MOVEORDER").getText();
			TRANSPORTREQUIRE=deviceElement.getChild("TRANSPORTREQUIRE").getText();
			CaseNo=deviceElement.getChild("CASENO").getText();
			logger.info("添加Yard_Device中Move相关数据   ");
			String UpdateDevicMove="UPDATE YARD_DEVICE SET STOCKSTATE=:STOCKSTATE,MOVEDATE=:MOVEDATE ,MOVEENTRANCE=:MOVEENTRANCE,MOVEORDER=:MOVEORDER,TRANSPORTREQUIRE=:TRANSPORTREQUIRE,SHIPREQUESTNAME=:SHIPREQUESTNAME,SHIPUSER=:SHIPUSER,SHIPTIME=:SHIPTIME,"+
					"LASTEVENTNAME=:LASTEVENTNAME,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE SITENAME =:SITENAME AND CASENO=:CASENO AND DEVICE=:DEVICE";
			Map<String, Object> insertMap = new HashMap<String, Object>();
			SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
			Date date = ft.parse(MOVEDATE);
			insertMap.put("STOCKSTATE", "Stocked");
			insertMap.put("MOVEDATE", date);
			insertMap.put("MOVEENTRANCE", MOVEENTRANCE);
			insertMap.put("MOVEORDER", MOVEORDER);
			insertMap.put("TRANSPORTREQUIRE", TRANSPORTREQUIRE);
			insertMap.put("SHIPREQUESTNAME",returnId);
			insertMap.put("SHIPUSER",eventInfo.getEventUser());
			insertMap.put("SHIPTIME",eventInfo.getEventTime());
			insertMap.put("LASTEVENTNAME","Prestockout");
			insertMap.put("LASTEVENTTIMEKEY",System.currentTimeMillis());
			insertMap.put("LASTEVENTTIME",new Date());
			insertMap.put("LASTEVENTUSER",eventInfo.getEventUser());
			insertMap.put("LASTEVENTCOMMENT","Prestockout");
			insertMap.put("SITENAME", SITENAME);
			insertMap.put("CASENO",CaseNo);
			insertMap.put("DEVICE",DEVICE);
			IDMFrameServiceProxy.getSqlTemplate().update(UpdateDevicMove, insertMap);
			
			String Insertsql="insert into "+ 
					" YARD_DEVICEHISTORY "+ 
					"select * from YARD_DEVICE t WHERE t.SITENAME =:SITENAME AND t.CASENO=:CASENO AND t.DEVICE=:DEVICE";
			Map<String, Object> insertMap2 = new HashMap<String, Object>();
			insertMap2.put("SITENAME", SITENAME);
			insertMap2.put("CASENO",CaseNo );
			insertMap2.put("DEVICE", DEVICE);
			IDMFrameServiceProxy.getSqlTemplate().update(Insertsql, insertMap2);
		}catch (ParseException e) {
		    e.printStackTrace(); // 或者你可以进行其他的异常处理操作
		}
		}
		
		return returnId;
	}
	
	
	/**
	 * 生成退库单号
	 * @throws Exception 
	 */
	public String CreateDeviceRTRequest(EventInfo eventInfo, String siteName, String ShipQuantity)
	{
		Object DeviceReturnId=System.currentTimeMillis();
		String returnId = "RT" + DeviceReturnId.toString();
		try {
			String Insertsql2="INSERT INTO YARD_DEVICERECEIVEREQUEST(SITENAME,DEVICERECEIVEREQUEST,RECEIVEDQUANTITY,CREATUSER,CREATTIME,LASTEVENTNAME,LASTEVENTTIMEKEY,LASTEVENTTIME,LASTEVENTUSER,LASTEVENTCOMMENT,RECEIVEQUESTSTATE)"+
					"VALUES(:SITENAME,:DEVICERECEIVEREQUEST,:RECEIVEDQUANTITY,:CREATUSER,:CREATTIME,:LASTEVENTNAME,:LASTEVENTTIMEKEY,:LASTEVENTTIME,:LASTEVENTUSER,:LASTEVENTCOMMENT,'Created')";
			Map<String, Object> InsertMap = new HashMap<String, Object>();
			InsertMap.put("SITENAME", siteName);
			InsertMap.put("DEVICERECEIVEREQUEST", returnId);
			InsertMap.put("RECEIVEDQUANTITY", ShipQuantity);
			InsertMap.put("CREATUSER", eventInfo.getEventUser());
			InsertMap.put("CREATTIME",eventInfo.getEventTime());
			InsertMap.put("LASTEVENTNAME", "CreateRTRequest");
			InsertMap.put("LASTEVENTTIMEKEY", System.currentTimeMillis());
			InsertMap.put("LASTEVENTTIME", new Date());
			InsertMap.put("LASTEVENTUSER", eventInfo.getEventUser());
			InsertMap.put("LASTEVENTCOMMENT","CreateRTRequest");
			IDMFrameServiceProxy.getSqlTemplate().update(Insertsql2, InsertMap);	
		}catch (Exception e) {
		    e.printStackTrace(); // 或者你可以进行其他的异常处理操作
		}
		return returnId;
	}
	
	
	/**
	 * 绑定退库单号
	 * @throws Exception 
	 */
	public void BindDeviceRTRequest(Document message,EventInfo eventInfo, String returnId)
	{
		//下面是Device表插入方式。
		Element root = message.getDocument().getRootElement();//获取其根元素
		Element bodyElement = root.getChild("Body");//获取body元素
		Element deviceListElement = bodyElement.getChild("SaveItemList");//获取YARD_DEVICE元素
		//用于暂时存储的变量
		String SITENAME="";		
		String DEVICE="";
		String CaseNo="";
		//遍历YARD_DEVICE，获取每个元素的值
		for (Iterator<?> iterator = deviceListElement.getChildren().iterator(); iterator.hasNext();) {
			try {
			Element deviceElement = (Element) iterator.next();
			SITENAME = deviceElement.getChild("SITENAME").getText();			
			DEVICE = deviceElement.getChild("DEVICE").getText();			
			CaseNo = deviceElement.getChild("CASENO").getText();
			logger.info("添加Yard_Device中Move相关数据   ");
			String UpdateDevicMove="UPDATE YARD_DEVICE SET RECEIVEREQUESTNAME=:RECEIVEREQUESTNAME,RECEIVEUSER=:RECEIVEUSER,RECEIVETIME=:RECEIVETIME,"+
					"LASTEVENTNAME=:LASTEVENTNAME,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE SITENAME =:SITENAME AND CASENO=:CASENO AND DEVICE=:DEVICE";
			Map<String, Object> insertMap = new HashMap<String, Object>();		
			//insertMap.put("STOCKSTATE", "Stocked");
			insertMap.put("RECEIVEREQUESTNAME",returnId);
			insertMap.put("RECEIVEUSER",eventInfo.getEventUser());
			insertMap.put("RECEIVETIME",eventInfo.getEventTime());
			insertMap.put("LASTEVENTNAME","ReturnStockIn");
			insertMap.put("LASTEVENTTIMEKEY",System.currentTimeMillis());
			insertMap.put("LASTEVENTTIME",new Date());
			insertMap.put("LASTEVENTUSER",eventInfo.getEventUser());
			insertMap.put("LASTEVENTCOMMENT","ReturnStockIn");
			insertMap.put("SITENAME", SITENAME);
			insertMap.put("CASENO",CaseNo);
			insertMap.put("DEVICE",DEVICE);
			IDMFrameServiceProxy.getSqlTemplate().update(UpdateDevicMove, insertMap);
			
			/*
			 * String Insertsql="insert into "+ " YARD_DEVICEHISTORY "+
			 * "select * from YARD_DEVICE t WHERE t.SITENAME =:SITENAME AND t.CASENO=:CASENO AND t.DEVICE=:DEVICE"
			 * ; Map<String, Object> insertMap2 = new HashMap<String, Object>();
			 * insertMap2.put("SITENAME", SITENAME); insertMap2.put("CASENO",CaseNo );
			 * insertMap2.put("DEVICE", DEVICE);
			 * IDMFrameServiceProxy.getSqlTemplate().update(Insertsql, insertMap2);
			 */
		}catch (Exception e) {
		    e.printStackTrace(); // 或者你可以进行其他的异常处理操作
		}
		}
	}
	
	/**
	 * 直搬出库
	 * @param message
	 * @param eventInfo
	 * @return
	 */
	public String CreateDirectDeviceShipeQuest(Document message,EventInfo eventInfo)
	{
		Object DeviceShipId=System.currentTimeMillis();
		String returnId=DeviceShipId.toString();
		//下面是Device表插入方式。
		Element root = message.getDocument().getRootElement();//获取其根元素
		Element bodyElement = root.getChild("Body");//获取body元素
		Element deviceListElement = bodyElement.getChild("SaveItemList");//获取YARD_DEVICE元素
		//用于暂时存储的变量
		String SITENAME="";
		String CONTAINER="";
		String DEVICE="";
		String MOVEDATE="";
		String MOVEENTRANCE="";
		String MOVEORDER="";
		String TRANSPORTREQUIRE="";
		String CaseNo="";
		//遍历YARD_DEVICE，获取每个元素的值
		for (Iterator<?> iterator = deviceListElement.getChildren().iterator(); iterator.hasNext();) {
			try {
			Element deviceElement = (Element) iterator.next();
			SITENAME = deviceElement.getChild("SITENAME").getText();
			CONTAINER = deviceElement.getChild("CONTAINER").getText();
			DEVICE = deviceElement.getChild("DEVICE").getText();
			MOVEDATE = deviceElement.getChild("MOVEDATE").getText();
			MOVEENTRANCE=deviceElement.getChild("MOVEENTRANCE").getText();
			MOVEORDER=deviceElement.getChild("MOVEORDER").getText();
			TRANSPORTREQUIRE=deviceElement.getChild("TRANSPORTREQUIRE").getText();
			CaseNo=deviceElement.getChild("CASENO").getText();
			logger.info("添加Yard_Device中Move相关数据   ");
			String UpdateDevicMove="UPDATE YARD_DEVICE SET STOCKSTATE=:STOCKSTATE,MOVEDATE=:MOVEDATE ,MOVEENTRANCE=:MOVEENTRANCE,MOVEORDER=:MOVEORDER,TRANSPORTREQUIRE=:TRANSPORTREQUIRE,SHIPREQUESTNAME=:SHIPREQUESTNAME,SHIPUSER=:SHIPUSER,SHIPTIME=:SHIPTIME,"+
					"LASTEVENTNAME=:LASTEVENTNAME,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE SITENAME =:SITENAME AND CASENO=:CASENO AND DEVICE=:DEVICE";
			Map<String, Object> insertMap = new HashMap<String, Object>();
			SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
			Date date = ft.parse(MOVEDATE);
			insertMap.put("STOCKSTATE", "Stocked");
			insertMap.put("MOVEDATE", date);
			insertMap.put("MOVEENTRANCE", MOVEENTRANCE);
			insertMap.put("MOVEORDER", MOVEORDER);
			insertMap.put("TRANSPORTREQUIRE", TRANSPORTREQUIRE);
			insertMap.put("SHIPREQUESTNAME",returnId);
			insertMap.put("SHIPUSER",eventInfo.getEventUser());
			insertMap.put("SHIPTIME",eventInfo.getEventTime());
			insertMap.put("LASTEVENTNAME","Prestockout");
			insertMap.put("LASTEVENTTIMEKEY",System.currentTimeMillis());
			insertMap.put("LASTEVENTTIME",new Date());
			insertMap.put("LASTEVENTUSER",eventInfo.getEventUser());
			insertMap.put("LASTEVENTCOMMENT","Prestockout");
			insertMap.put("SITENAME", SITENAME);
			insertMap.put("CASENO",CaseNo);
			insertMap.put("DEVICE",DEVICE);
			IDMFrameServiceProxy.getSqlTemplate().update(UpdateDevicMove, insertMap);
			
			String Insertsql="insert into "+ 
					" YARD_DEVICEHISTORY "+ 
					"select * from YARD_DEVICE t WHERE t.SITENAME =:SITENAME AND t.CASENO=:CASENO AND t.DEVICE=:DEVICE";
			Map<String, Object> insertMap2 = new HashMap<String, Object>();
			insertMap2.put("SITENAME", SITENAME);
			insertMap2.put("CASENO",CaseNo );
			insertMap2.put("DEVICE", DEVICE);
			IDMFrameServiceProxy.getSqlTemplate().update(Insertsql, insertMap2);
		}catch (ParseException e) {
		    e.printStackTrace(); // 或者你可以进行其他的异常处理操作
		}
		}
		
		return returnId;
	}
	
	
	
	
	/**
	 * B20 堆场预约拣配
	 * @throws Exception 
	 */
	public void createYardDeviceQuest(EventInfo eventInfo, String siteName,
			String ShipQuantity,String DeviceShipId) {
		logger.info(" 堆场预约拣配->创建入库单管理 ");
		
		String Insertsql="INSERT INTO YARD_DEVICESHIPREQUEST(SITENAME,DEVICESHIREQUEST,SHIPQUANTITY,CREATUSER,CREATTIME,LASTEVENTNAME,LASTEVENTTIMEKEY,LASTEVENTTIME,LASTEVENTUSER,LASTEVENTCOMMENT,SHIPSTATE)"+
		"VALUES(:SITENAME,:DEVICESHIREQUEST,:SHIPQUANTITY,:CREATUSER,:CREATTIME,:LASTEVENTNAME,:LASTEVENTTIMEKEY,:LASTEVENTTIME,:LASTEVENTUSER,:LASTEVENTCOMMENT,'Reserve')";
		Map<String, Object> InsertMap = new HashMap<String, Object>();
		InsertMap.put("SITENAME", siteName);
		InsertMap.put("DEVICESHIREQUEST", DeviceShipId);
		InsertMap.put("SHIPQUANTITY", ShipQuantity);
		InsertMap.put("CREATUSER", eventInfo.getEventUser());
		InsertMap.put("CREATTIME",eventInfo.getEventTime());
		InsertMap.put("LASTEVENTNAME", "Create");
		InsertMap.put("LASTEVENTTIMEKEY", System.currentTimeMillis());
		InsertMap.put("LASTEVENTTIME", new Date());
		InsertMap.put("LASTEVENTUSER", eventInfo.getEventUser());
		InsertMap.put("LASTEVENTCOMMENT","Create");
		IDMFrameServiceProxy.getSqlTemplate().update(Insertsql, InsertMap);	
	}
	
	/**
	 * B20 堆场出库
	 * @throws Exception 
	 */
	public void ShipYardStockOut(EventInfo eventInfo,String DeviceShipId,String PONO,String Invoiceno,String Container,String Device,String sitename,String caseno)throws Exception
	{
		
		logger.info(" 先判断是否能出库！ ");
		String selectShipQuerySql = "SELECT SHIPQUANTITY FROM YARD_DEVICESHIPREQUEST WHERE DEVICESHIREQUEST=:DEVICESHIREQUEST AND SHIPSTATE = 'Reserve' ";
		Map<String, Object> selectMap = new HashMap<>();
		selectMap.put("DEVICESHIREQUEST", DeviceShipId);
		List<Map<String, Object>> returnListStorge = IDMFrameServiceProxy.getSqlTemplate().queryForList(selectShipQuerySql, selectMap);
		if (returnListStorge == null || returnListStorge.size() <= 0) {
			throw new CustomException(String.valueOf(3),"单号不存在或者单据状态不为已预约");
		}
		Map<String, Object> firstMap = returnListStorge.get(0);
		int shipQuantityNumber = Integer.parseInt(firstMap.get("SHIPQUANTITY").toString());

		String selectShipQuerySql2 = "SELECT RECEIVEDQUANTITY,SHIPQUANTITY,QUANTITY FROM YARD_DEVICERECEIVEREQUEST WHERE DEVICERECEIVEREQUEST=:DEVICERECEIVEREQUEST";
		Map<String, Object> selectMap2 = new HashMap<>();
		selectMap2.put("DEVICERECEIVEREQUEST", PONO);
		List<Map<String, Object>> returnListStorge2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(selectShipQuerySql2, selectMap2);
		if (returnListStorge2 != null && returnListStorge2.size() > 0) {
			Map<String, Object> firstMap2 = returnListStorge2.get(0);
			Object shipQuantity = firstMap2.get("SHIPQUANTITY");
			if(shipQuantity==null)
			{
				shipQuantity="0";
			}
			int shipQuantityNumber2 = Integer.parseInt(shipQuantity.toString());
		    if(Integer.parseInt(firstMap2.get("RECEIVEDQUANTITY").toString())<(shipQuantityNumber+shipQuantityNumber2)||(Integer.parseInt(firstMap2.get("QUANTITY").toString())<(shipQuantityNumber+shipQuantityNumber2)))
		    {
		    	throw new CustomException(String.valueOf(1), "出库数量大于库中数量   "  + "|" + shipQuantityNumber+shipQuantityNumber2);
		    }
			String UpdateYARDDEVICERECEIVEREQUEST="UPDATE YARD_DEVICERECEIVEREQUEST SET SHIPQUANTITY=:SHIPQUANTITY,LASTEVENTNAME=:LASTEVENTNAME,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE DEVICERECEIVEREQUEST=:DEVICERECEIVEREQUEST";
			Map<String, Object> UpdateYARDDEVICERECEIVEREQUESTMap = new HashMap<String, Object>();
			UpdateYARDDEVICERECEIVEREQUESTMap.put("SHIPQUANTITY",shipQuantityNumber+shipQuantityNumber2 );
			UpdateYARDDEVICERECEIVEREQUESTMap.put("LASTEVENTNAME", "StockOut");
			UpdateYARDDEVICERECEIVEREQUESTMap.put("LASTEVENTTIMEKEY", System.currentTimeMillis());
			UpdateYARDDEVICERECEIVEREQUESTMap.put("LASTEVENTTIME", new Date());
			UpdateYARDDEVICERECEIVEREQUESTMap.put("LASTEVENTUSER", eventInfo.getEventUser());
			UpdateYARDDEVICERECEIVEREQUESTMap.put("LASTEVENTCOMMENT","StockOut");
			UpdateYARDDEVICERECEIVEREQUESTMap.put("DEVICERECEIVEREQUEST",PONO);
			IDMFrameServiceProxy.getSqlTemplate().update(UpdateYARDDEVICERECEIVEREQUEST, UpdateYARDDEVICERECEIVEREQUESTMap);
		}
		
		
		logger.info(" 堆场设备出库 ");
		String UpdateShipState="UPDATE YARD_DEVICE SET STOCKSTATE=:STOCKSTATE,SHIPUSER=:SHIPUSER,SHIPTIME=:SHIPTIME,"+
				" LASTEVENTNAME=:LASTEVENTNAME,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE SHIPREQUESTNAME=:SHIPREQUESTNAME AND  SITENAME =:SITENAME ";
		Map<String, Object> InsertMap = new HashMap<String, Object>();
		InsertMap.put("STOCKSTATE", "StockOut");
		InsertMap.put("SHIPUSER", eventInfo.getEventUser());
		InsertMap.put("SHIPTIME",eventInfo.getEventTime());
		InsertMap.put("LASTEVENTNAME", "StockOut");
		InsertMap.put("LASTEVENTTIMEKEY", System.currentTimeMillis());
		InsertMap.put("LASTEVENTTIME", new Date());
		InsertMap.put("LASTEVENTUSER", eventInfo.getEventUser());
		InsertMap.put("LASTEVENTCOMMENT","StockOut");
		InsertMap.put("SHIPREQUESTNAME",DeviceShipId);
		InsertMap.put("SITENAME", sitename);
		InsertMap.put("CASENO", caseno);
		InsertMap.put("DEVICE",Device);
		IDMFrameServiceProxy.getSqlTemplate().update(UpdateShipState, InsertMap);
		 
		String Selectsql="SELECT PONO,INVOICENO FROM YARD_DEVICE WHERE SHIPREQUESTNAME=:SHIPREQUESTNAME ";
		Map<String, Object> InsertMap2 = new HashMap<String, Object>();
		InsertMap2.put("SHIPREQUESTNAME",DeviceShipId);
		List<Map<String, Object>> returnList = IDMFrameServiceProxy.getSqlTemplate().queryForList(Selectsql, InsertMap2);
		Map<String, Object> map = returnList.get(0);
		String PONOID = (String) map.get("PONO");		
		logger.info(" 堆场设备变更Device表历史记录 "+PONOID+","+Invoiceno);
		    String Insertsql="insert into "+ 
					" YARD_DEVICEHISTORY "+ 
					"select * from YARD_DEVICE t where t.SHIPREQUESTNAME=:SHIPREQUESTNAME";
		Map<String, Object> insertMap3 = new HashMap<String, Object>();
		insertMap3.put("SITENAME", sitename);
		insertMap3.put("CASENO", caseno);
		insertMap3.put("DEVICE", Device);
		insertMap3.put("SHIPREQUESTNAME",DeviceShipId);
		IDMFrameServiceProxy.getSqlTemplate().update(Insertsql, insertMap3);
		
		String UpdateShipSql="UPDATE YARD_DEVICESHIPREQUEST SET LASTEVENTNAME=:LASTEVENTNAME,SHIPSTATE = :SHIPSTATE ,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE DEVICESHIREQUEST=:DEVICESHIREQUEST";
		Map<String, Object> UpdateMap = new HashMap<String, Object>();
		UpdateMap.put("LASTEVENTNAME", "StockOut");
		UpdateMap.put("LASTEVENTTIMEKEY", System.currentTimeMillis());
		UpdateMap.put("LASTEVENTTIME", new Date());
		UpdateMap.put("LASTEVENTUSER", eventInfo.getEventUser());
		UpdateMap.put("LASTEVENTCOMMENT","StockOut");
		UpdateMap.put("DEVICESHIREQUEST",DeviceShipId);
		UpdateMap.put("SHIPSTATE","Completed");
		IDMFrameServiceProxy.getSqlTemplate().update(UpdateShipSql, UpdateMap);
		
		
	}

	/**
	 * 取消预约拣配
	 * @throws Exception 
	 */
   public void RevokePreStockOutMethod(String DeviceShipId,EventInfo eventInfo)
   {
	   
		logger.info(" 堆场设备取消预约拣配删除出货单 ");
		String delSql = " DELETE FROM YARD_DEVICESHIPREQUEST  WHERE DEVICESHIREQUEST = :DEVICESHIREQUEST ";
		Map<String,Object> bindMap = new HashMap<String,Object>();
		bindMap.put("DEVICESHIREQUEST", DeviceShipId);
		IDMFrameServiceProxy.getSqlTemplate().update(delSql, bindMap);
				
		//logger.info(4," 堆场设备Device历史表。 ");
		String Selectsql="SELECT SITENAME,CONTAINER,DEVICE,PONO,INVOICENO,CASENO FROM YARD_DEVICE WHERE SHIPREQUESTNAME=:SHIPREQUESTNAME ";
		Map<String, Object> InsertMap2 = new HashMap<String, Object>();
		InsertMap2.put("SHIPREQUESTNAME",DeviceShipId);
		List<Map<String, Object>> returnList = IDMFrameServiceProxy.getSqlTemplate().queryForList(Selectsql, InsertMap2);
		for(int i=0;i<returnList.size();i++)
		{
			Map<String, Object> map = returnList.get(i);
		    String SITENAME = (String) map.get("SITENAME");
		    String caseNo=(String) map.get("CASENO");
		    String DEVICE=(String) map.get("DEVICE");
		    
			logger.info(" 堆场设备取消预约拣配修改Device表 ");
			//String UPdateSql="UPDATE YARD_DEVICE SET STOCKSTATE=:STOCKSTATE, STOCKINTYPE= '冲销入库', SHIPREQUESTNAME=:SHIPREQUESTNAME,SHIPUSER=:SHIPUSER,SHIPTIME=:SHIPTIME,LASTEVENTNAME=:LASTEVENTNAME,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE SITENAME =:SITENAME AND CASENO=:CASENO AND DEVICE=:DEVICE ";
			String UPdateSql="UPDATE YARD_DEVICE SET SHIPREQUESTNAME=:SHIPREQUESTNAME,SHIPUSER=:SHIPUSER,SHIPTIME=:SHIPTIME,LASTEVENTNAME=:LASTEVENTNAME,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE SITENAME =:SITENAME AND CASENO=:CASENO AND DEVICE=:DEVICE ";

			Map<String, Object> UpdatetMap = new HashMap<String, Object>();
			//UpdatetMap.put("STOCKSTATE", "Stocked");
			UpdatetMap.put("SHIPREQUESTNAME", "");
			UpdatetMap.put("SHIPUSER", "");
			UpdatetMap.put("SHIPTIME", "");
		    UpdatetMap.put("LASTEVENTNAME", "CancelReserveShipRequest");
			UpdatetMap.put("LASTEVENTTIMEKEY", System.currentTimeMillis());
			UpdatetMap.put("LASTEVENTTIME", new Date());
			UpdatetMap.put("LASTEVENTUSER", eventInfo.getEventUser());
			UpdatetMap.put("LASTEVENTCOMMENT","CancelReserveShipRequest");
			UpdatetMap.put("SITENAME", SITENAME);
			UpdatetMap.put("CASENO", caseNo);
			UpdatetMap.put("DEVICE", DEVICE);
			IDMFrameServiceProxy.getSqlTemplate().update(UPdateSql, UpdatetMap);
			
				 
		    logger.info(" 堆场设备变更Device表历史记录 "+SITENAME+","+caseNo+","+DEVICE);
		    String Insertsql="insert into "+ 
					" YARD_DEVICEHISTORY "+ 
					"select * from YARD_DEVICE t where t.SITENAME =:SITENAME AND t.CASENO=:CASENO AND t.DEVICE=:DEVICE";
			Map<String, Object> insertMap2 = new HashMap<String, Object>();
			insertMap2.put("SITENAME", SITENAME);
			insertMap2.put("CASENO",caseNo );
			insertMap2.put("DEVICE", DEVICE);
			IDMFrameServiceProxy.getSqlTemplate().update(Insertsql, insertMap2);
		}
   }
   
   // B20 生成盘点单	
	public static void createCheckNoByDevice(EventInfo eventInfo, Document doc) throws CustomException {
			Element root = doc.getDocument().getRootElement();
			Element bodyElement = root.getChild("Body");
			// 获取单据明细行
			List<Element> detail = root.getChild("Body").getChild("DETAILS").getChildren();
		
			String siteName = bodyElement.getChildText("SITENAME");
	         
			// 单据号
			String checkNo = bodyElement.getChildText("CHECKNO");
			// 盘点类型
			String checkType = bodyElement.getChildText("CHECKTYPE");
			
			String createUser = eventInfo.getEventUser();
			Date createTime = eventInfo.getEventTime();
			
			String checkState = "Created";
			
			String createSql = "INSERT INTO YARD_CHECK (SITENAME, CHECKNO, CHECKTYPE, CHECKSTATE, CREATEUSER, CREATETIME) "
					+ "VALUES(:SITENAME, :CHECKNO, :CHECKTYPE, :CHECKSTATE, :CREATEUSER, :CREATETIME)";
			Map<String, Object> insertMap = new HashMap<String, Object>();
			
			insertMap.put("SITENAME", siteName);
			insertMap.put("CHECKNO", checkNo);
			insertMap.put("CHECKTYPE", checkType);
			insertMap.put("CHECKSTATE", checkState);
			insertMap.put("CREATEUSER", createUser);
			insertMap.put("CREATETIME", createTime);
			
			IDMFrameServiceProxy.getSqlTemplate().update(createSql, insertMap);
			
			for (Element element : detail) {
				String caseNo = element.getChildText("CASENO");
				String device = element.getChildText("DEVICE");
				
				String createDetailSql = "INSERT INTO YARD_CHECK_DETAIL (SITENAME, CHECKNO, CASENO, DEVICE, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER) "
						+ "VALUES(:SITENAME, :CHECKNO, :CASENO, :DEVICE, :LASTEVENTNAME, :LASTEVENTTIMEKEY, :LASTEVENTTIME, :LASTEVENTUSER)";
				Map<String, Object> insertDetailMap = new HashMap<String, Object>();
				
				insertDetailMap.put("SITENAME", siteName);
				insertDetailMap.put("CHECKNO", checkNo);
				insertDetailMap.put("CASENO", caseNo);
				insertDetailMap.put("DEVICE", device);
				insertDetailMap.put("LASTEVENTNAME", "CreateCheckNo");
				insertDetailMap.put("LASTEVENTTIMEKEY", System.currentTimeMillis());
				insertDetailMap.put("LASTEVENTTIME", new Date());
				insertDetailMap.put("LASTEVENTUSER", eventInfo.getEventUser());
				IDMFrameServiceProxy.getSqlTemplate().update(createDetailSql, insertDetailMap);
			}
	
				
	}
}
