package com.dev.swms.server.interfaces;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.httpclient.HttpClientError;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;

import com.dev.energy.server.dao.CommonDao;
import com.dev.swms.server.model.interfaceLog.InterfaceLog;
import com.dev.swms.server.model.interfaceLog.InterfaceLogConstant;
import com.dev.swms.server.service.interfaceLog.InterfaceItemManager;
import com.dev.swms.server.utils.DateUtil;

/**
 * @author dev
 * @version $Id: AdapterLauncher.java,v 1.2 2010/06/01 09:14:16 peng.lei Exp $
 */

public class ItemJobLauncher implements ApplicationListener {
	protected final Log logger = LogFactory.getLog(this.getClass());

	/** 执行间隔时间(ms) */
	protected int sleepInterval = 5000;
	/** 服务是否启动 */
	private boolean isApplicationAvailable = false;
	/** 是否允许运行，用于客户端对其进行控制 */
	private boolean allowed = true;
	/** 二进制回退算法重置的阈值(ms，默认是１０分钟) */
	protected int thresholdForResetValue = 600000;
	/** 本次要发送的日志数 */
	private int numOfLogsToBeSent;
	/** 已发送过的日志，包括成功和失败的 */
	private int numOfLogsSent;
	protected CommonDao commonDao;
	protected InterfaceItemManager itemManager;

	public ItemJobLauncher( CommonDao commonDao,InterfaceItemManager itemManager) {
		this.itemManager = itemManager;
		this.commonDao=commonDao;
	}

	public boolean isAllowed() {
		return allowed;
	}

	public void setAllowed(boolean allowed) {
		this.allowed = allowed;
	}

	public int getSleepInterval() {
		return sleepInterval;
	}

	public void setSleepInterval(int sleepInterval) {
		this.sleepInterval = sleepInterval;
	}

	public int getThresholdForResetValue() {
		return thresholdForResetValue;
	}

	public void setThresholdForResetValue(int thresholdForResetValue) {
		this.thresholdForResetValue = thresholdForResetValue;
	}

	public int getNumOfLogsToBeSent() {
		return numOfLogsToBeSent;
	}

	public void setNumOfLogsToBeSent(int numOfLogsToBeSent) {
		this.numOfLogsToBeSent = numOfLogsToBeSent;
	}

	public int getNumOfLogsSent() {
		return numOfLogsSent;
	}

	public void setNumOfLogsSent(int numOfLogsSent) {
		this.numOfLogsSent = numOfLogsSent;
	}

	/**
	 * 从数据库中获取InterfaceLog信息并根据其业务类型进行相应处理
	 */
	public void dealReceivedLogs() {
		logger.info("application[dispatchXMLToWebMethods] started....................");
		while(allowed) {
			List types = new ArrayList<String>();
			types.add(InterfaceLogConstant.QUERY_INVENTORY);
			types.add(InterfaceLogConstant.AVAILABLE_ORDER_PO);
			types.add(InterfaceLogConstant.AVAILABLE_ORDER_SO);
			List<InterfaceLog> logs = itemManager.getLogsByConditions(types, InterfaceLogConstant.RECEIVE);
			dealLog(logs);
			try {
				Thread.sleep(sleepInterval);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(!isApplicationAvailable) {
				logger.info("application[dealReceivedLogs] closed....................");
				break;
			}
		}
	}

	private void dealLog(List<InterfaceLog> logs) {
		logger.info("log size:" + logs.size());
		for(InterfaceLog log : logs) {
			//按照log业务类型进行具体业务处理，处理完毕后更新log状态为FINISH
			//----(如果需要转发消息，则转发时设置状态为PROCESS，发送成功后改为FINISH)
			if (InterfaceLogConstant.QUERY_INVENTORY.equals(log.getType())) {
				//库存审核反馈处理
				try {
					itemManager.inventoryResponse(log);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (InterfaceLogConstant.AVAILABLE_ORDER_PO.equals(log.getType()) 
					|| InterfaceLogConstant.AVAILABLE_ORDER_SO.equals(log.getType())) {
				//订单生效反馈处理
				try {
					itemManager.availableOrderResponse(log);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 从数据库中获取InterfaceLog信息并根据其业务类型进行相应处理
	 */
	public void batchSendLogs() {
		logger.info("application[batchSendLogs] started....................");
		while (allowed) {
			// 对库存审核task批量处理，调用http接口发送数据
			List types = new ArrayList<String>();
			types.add(InterfaceLogConstant.RECEIVE_REPORT);
			types.add(InterfaceLogConstant.PUTAWAY_REPORT);
			types.add(InterfaceLogConstant.PICKING_REPORT);
			types.add(InterfaceLogConstant.SHIPPING_REPORT);
			List<InterfaceLog> logs = itemManager.getLogsByConditions(types, InterfaceLogConstant.READY);
			for (InterfaceLog log : logs) {
				itemManager.send(log, InterfaceLogConstant.REQUEST);
			}
			try {
				Thread.sleep(sleepInterval);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (!isApplicationAvailable) {
				logger.info("application[dealReceivedLogs] closed....................");
				break;
			}
		}
	}

	/**
	 * 从数据库中获取Item信息并根据其业务类型进行相应处理
	 */
	public void dealItem() {
		logger.info("application[batchSendLogs] started....................");
//		while (true) {
		String wsdl13 = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH13Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ShowF57WEH13Element>");
		sb.append("<java:MaxReturnedRows>20</java:MaxReturnedRows>");
		sb.append("<java:Identifier2ndItem/>");
		sb.append("<java:CostCenter>10908</java:CostCenter>");
		sb.append("<java:YesOrNoEntry>N</java:YesOrNoEntry>");
		sb.append("</orac:ShowF57WEH13Element>");
		String managerName = "JP57WE13";

		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl13);
			httpPost.setHeader("SOAPAction", "http://oracle.e1.bssv.JP57WE13//ShowF57WEH13");
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			// System.out.println(new String(EntityUtils.toByteArray(entity), "UTF8"));
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealItem==================" + xml);
//				Thread.sleep(sleepInterval);
//				SAXReader reader = new SAXReader();
//				Document document = reader.read(new File("/Users/houjianlan/Documents/aa.xml"));
//				System.out.println(document.asXML());

			itemManager.storeItem(xml);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealItem] closed....................");
//				break;
		}
//		}
	}

	public void dealAddress() {
		logger.info("application[batchlAddress] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH14Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ShowF57WEH14Element>");
		sb.append("<java:MaxReturnedRows>20</java:MaxReturnedRows>");
		sb.append("<java:AddressNumber/>");
//				 sb.append("<java:dateUpdated>2017-11-08CST11:00:09</java:dateUpdated>");
		sb.append("<java:YesOrNoEntry>N</java:YesOrNoEntry>");
		sb.append(" <java:SessionID/>");
		sb.append("</orac:ShowF57WEH14Element>");
		String managerName = "JP57WE14";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "ShowF57WEH14";

		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealAddress========" + xml);

			// itemManager.storeAddress(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealAddress] closed....................");
		}
	}

	public void dealUnit() {
		logger.info("application[batchlUnit] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH15Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ShowF57WEH15Element>");
		sb.append("<java:MaxReturnedRows>20</java:MaxReturnedRows>");
		sb.append("<java:Identifier2ndItem/>");
		sb.append("<java:UnitOfMeasure/>");
		sb.append("<java:RelatedUnitOfMeasure/>");
//				 sb.append("<java:dateUpdated>2017-11-08CST11:00:09</java:dateUpdated>");
		sb.append("<java:YesOrNoEntry>Y</java:YesOrNoEntry>");
		sb.append(" <java:SessionID/>");
		sb.append("</orac:ShowF57WEH15Element>");
		String managerName = "JP57WE15";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "ShowF57WEH15";

		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealUnit========" + xml);

			// itemManager.storeUnit(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealUnit] closed....................");
		}
	}

	/** 临时销售单Out信息 */
	public void dealOut() {
		logger.info("application[batchlOut] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH01Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ShowF57WEH01Element>");
		sb.append("<java:CompanyKeyOrderNo/>");
		sb.append("<java:OrderType></java:OrderType>");
		sb.append("<java:DocumentOrderInvoiceE>18000469</java:DocumentOrderInvoiceE>");
		sb.append("<java:LineNumber/>");
		sb.append("<java:YesOrNoEntry>Y</java:YesOrNoEntry>");
		sb.append("<java:MaxReturnedRows>500</java:MaxReturnedRows>");
		sb.append("<java:CostCenter>10908</java:CostCenter>");
//				 sb.append("<java:dateUpdated>2017-11-08CST11:00:09</java:dateUpdated>");
		sb.append("</orac:ShowF57WEH01Element>");
		String managerName = "JP57WEH1";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "ShowF57WEH01";
		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealOut========" + xml);
			 itemManager.storeOut(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealOut] closed....................");
		}
	}

	/*
	 * public void dealOutReturn() {
	 * logger.info("application[batchlOutReturn] started...................."); //
	 * while (true) { String wsdl =
	 * "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH02Manager?WSDL";
	 * StringBuffer sb = new StringBuffer("");
	 * sb.append("<orac:howF57WEH02Element>");
	 * sb.append("<java:CompanyKeyOrderNo/>"); sb.append("<java:OrderType/>");
	 * sb.append("<java:DocumentOrderInvoiceE/>"); sb.append("<java:LineNumber/>");
	 * sb.append("<java:YesOrNoEntry>Y</java:YesOrNoEntry>");
	 * sb.append("<java:MaxReturnedRows/>"); sb.append("<java:CostCenter/>"); //
	 * sb.append("<java:dateUpdated>2017-11-08CST11:00:09</java:dateUpdated>");
	 * sb.append("</orac:howF57WEH02Element>"); String managerName="JP57WEH2";
	 * String method="http://oracle.e1.bssv."+managerName+"//"+"howF57WEH02"; String
	 * xmlContent = createXML(managerName,sb.toString()); //
	 * 对库存审核task批量处理，调用http接口发送数据 try { HttpClient httpclient = new
	 * DefaultHttpClient(); HttpPost httpPost = new HttpPost(wsdl);
	 * httpPost.setHeader("SOAPAction",method); StringEntity stringEntity = new
	 * StringEntity(xmlContent, "text/xml", "UTF8");
	 * stringEntity.setContentType("text/xml"); httpPost.setEntity(stringEntity);
	 * HttpResponse response = httpclient.execute(httpPost);
	 * System.out.println(response.getStatusLine().getStatusCode()); HttpEntity
	 * entity = response.getEntity(); String xml=new
	 * String(EntityUtils.toByteArray(entity), "UTF8");
	 * System.out.println("dealOutReturn========"+xml);
	 * //itemManager.storeOutReturn(xml); }catch (Exception e) {
	 * e.printStackTrace(); } if (!isApplicationAvailable) { logger
	 * .info("application[dealOutReturn] closed...................."); } }
	 */

	/** 临时销售退货单OutReturn信息 */
	public void dealOutReturn() {
		logger.info("application[batchlOutReturn] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH02Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ShowF57WEH02Element>");
		sb.append("<java:CompanyKeyOrderNo/>");
		sb.append("<java:OrderType/>");
		sb.append("<java:DocumentOrderInvoiceE/>");
		sb.append("<java:LineNumber/>");
		sb.append("<java:YesOrNoEntry>N</java:YesOrNoEntry>");
		sb.append("<java:MaxReturnedRows/>");
		sb.append("<java:CostCenter/>");
//				 sb.append("<java:dateUpdated>2017-11-08CST11:00:09</java:dateUpdated>");
		sb.append("</orac:ShowF57WEH02Element>");
		String managerName = "JP57WEH2";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "howF57WEH02";
		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealOutReturn========" + xml);
			// itemManager.storeOutReturn(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealOutReturn] closed....................");
		}
	}

	/** 临时采购单Purchase信息 */
	public void dealPurchase() {
		logger.info("application[batchlPurchase] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH07Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ShowF57WEH07Element>");
		sb.append("<java:CompanyKeyOrderNo/>");
		sb.append("<java:DocumentOrderInvoiceE/>");
		sb.append("<java:OrderType/>");
		sb.append("<java:YesOrNoEntry>Y</java:YesOrNoEntry>");
		sb.append("<java:OrderSuffix/>");
		sb.append("<java:LineNumber/>");
		sb.append("<java:NextStauts></java:NextStauts>");
		sb.append("<java:CostCenter>10908</java:CostCenter>");
		sb.append("<java:MaxReturnedRows>200</java:MaxReturnedRows>");
		sb.append("</orac:ShowF57WEH07Element>");
		String managerName = "JP57WEH7";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "ShowF57WEH07";

		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealPurchase========" + xml);

			 itemManager.storePurchase(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealPurchase] closed....................");
		}
	}

	/** 临时采购单Return信息 */
	public void dealReturn() {
		logger.info("application[batchlReturn] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH08Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ShowF57WEH08Element>");
		sb.append("<java:CompanyKeyOrderNo/>");
		sb.append("<java:DocumentOrderInvoiceE/>");
		sb.append("<java:OrderType/>");
		sb.append("<java:OrderSuffix/>");
		sb.append("<java:LineNumber/>");
		sb.append("<java:NextStauts/>");
		sb.append("<java:CostCenter>10908</java:CostCenter>");
		sb.append("<java:MaxReturnedRows>20</java:MaxReturnedRows>");
		sb.append("</orac:ShowF57WEH08Element>");
		String managerName = "JP57WEH8";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "ShowF57WEH08";

		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealReturn========" + xml);

			// itemManager.storeReturn(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealReturn] closed....................");
		}
	}

	/** 临时项目Project信息 */
	public void dealProject() {
		logger.info("application[batchlProject] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH11Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ShowF57WEH11Element>");
		sb.append("<java:Identifier2ndItem/>");
		sb.append("<java:YesOrNoEntry/>");
		sb.append("<java:MaxReturnedRows>20</java:MaxReturnedRows>");
		sb.append("</orac:ShowF57WEH11Element>");
		String managerName = "JP57WE11";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "ShowF57WEH11";

		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealProject========" + xml);

			itemManager.storeProject(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealProject] closed....................");
		}
	}

	/** 临时仓库主数据Warehouse信息 */
	public void dealWarehouse() {
		logger.info("application[batchlWarehouse] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEH12Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ShowF57WEH12Element>");
		sb.append("<java:CostCenter>10908</java:CostCenter>");
		sb.append("<java:YesOrNoEntry>Y</java:YesOrNoEntry>");
		sb.append("<java:MaxReturnedRows>20</java:MaxReturnedRows>");
		sb.append("</orac:ShowF57WEH12Element>");
		String managerName = "JP57WE12";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "ShowF57WEH12";

		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealWarehouse========" + xml);

			// itemManager.storeWarehouse(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealWarehouse] closed....................");
		}
	}

	/** 定时处理王品对接Download下载信息 */
	public void dealDownload() {
		logger.info("application[batchlDownload] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCShowF57WEHB2Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		String date=DateUtil.formatDateYMD01ToStr(new Date());
		sb.append("<orac:ShowF57WEHB2Element>");
	 	sb.append("<java:BarCodeString1></java:BarCodeString1>");
	 	sb.append("<java:Identifier2ndItem></java:Identifier2ndItem>");
	 	sb.append("<java:DateTransactionJulian>"+date+"</java:DateTransactionJulian>");//2018/09/27
		sb.append("<java:SessionID></java:SessionID>");
	 	sb.append("<java:MaxReturnedRows>2000000</java:MaxReturnedRows>");
		sb.append("<java:TimeFrom>000000</java:TimeFrom>");
		sb.append("<java:TimeTo>235959</java:TimeTo>");
		 sb.append("</orac:ShowF57WEHB2Element>");
		String managerName = "JP57WEB2";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "ShowF57WEHB2";

		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			//System.out.println("dealDownload========" + xml);

			itemManager.storeDownload(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealDownload] closed....................");
		}
	}

	/** 定时处理王品对接Diary日记账信息 */
	public void dealDiary() {
		logger.info("application[batchlDiary] started....................");
//		while (true) {
		String wsdl = "https://bssv.wowprime.com.cn:8016/PD910/BCProcessF57WEHB3Manager?WSDL";
		StringBuffer sb = new StringBuffer("");
		sb.append("<orac:ProcessF57WEHB3Element>");
		sb.append("<java:BarCodeString1/>");
		sb.append("<java:NdItemNumber/>");
		sb.append("<java:OrderCompanyOrderNumber/>");
		sb.append("<java:OrderType/>");
		sb.append("<java:DocumentOrderNoInvoiceetc/>");
		sb.append("<java:LineNumber/>");
		sb.append("<java:OrderSuffix/>");
		sb.append("<java:WMSOrderID/>");
		sb.append("<java:WMSOrderType/>");
		sb.append("<java:WMSLineNumber/>");
		sb.append("<java:JDEnterpriseOneEventPoint01/>");
		sb.append("<java:JDEnterpriseOneEventPoint02/>");
		sb.append("<java:JDEnterpriseOnePoint03/>");
		sb.append("<java:Description11/>");
		sb.append("<java:Description12/>");
		sb.append("<java:Description13/>");
		sb.append("<java:Description14/>");
		sb.append("<java:Description15/>");
		sb.append("<java:UserID/>");
		sb.append("<java:ProgramID/>");
		sb.append("<java:WorkStationID/>");
		/*
		 * sb.append("<java:YesOrNoEntry>Y</java:YesOrNoEntry>");
		 * sb.append("<java:MaxReturnedRows>20</java:MaxReturnedRows>");
		 */
		sb.append("</orac:ProcessF57WEHB3Element>");
		String managerName = "JP57WEB3";
		String method = "http://oracle.e1.bssv." + managerName + "//" + "ShowF57WEHB3";

		String xmlContent = createXML(managerName, sb.toString());
		// 对库存审核task批量处理，调用http接口发送数据
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(wsdl);
			httpPost.setHeader("SOAPAction", method);
			StringEntity stringEntity = new StringEntity(xmlContent, "text/xml", "UTF8");
			stringEntity.setContentType("text/xml");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			String xml = new String(EntityUtils.toByteArray(entity), "UTF8");
			System.out.println("dealDiary========" + xml);

			// itemManager.storeDiary(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!isApplicationAvailable) {
			logger.info("application[dealDiary] closed....................");
		}
	}

	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof ContextClosedEvent) {
			logger.info("Context closing......................");
			isApplicationAvailable = false;
		} else if (event instanceof ContextRefreshedEvent) {
			logger.info("Context starting......................");
			isApplicationAvailable = true;
		}
	}

	public static String createXML(String managerName, String body) {
		StringBuffer sb = new StringBuffer("");
		sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
		sb.append("xmlns:orac=\"http://oracle.e1.bssv." + managerName + "/\" xmlns:java=\"java:oracle.e1.bssv."
				+ managerName + ".valueobject\"> ");
		sb.append("<soapenv:Header> ");
		sb.append("<wsse:Security soapenv:mustUnderstand=\"1\" ");
		sb.append("xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" ");
		sb.append("xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" ");
		sb.append("xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"> ");
		sb.append("<wsse:UsernameToken> ");
		sb.append("<wsse:Username>0001001</wsse:Username> ");
		sb.append(
				"<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">1q2w3e4r!</wsse:Password>");
		sb.append("</wsse:UsernameToken>");
		sb.append("</wsse:Security>");
		sb.append("</soapenv:Header>");

		sb.append("<soapenv:Body>");
		sb.append(body);
		sb.append("</soapenv:Body>");
		sb.append("</soapenv:Envelope>");
		return sb.toString();
	}

}
