Demo entry 6762046

111

   

Submitted by 1111 on Oct 10, 2018 at 05:13
Language: Java. Code size: 8.2 kB.

package com.haier.hevwms.sapinterface;

import io.terminus.pampas.common.UserUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;

import com.haier.hevwms.po.dao.StgPoDownDAO;
import com.haier.hevwms.sapinterface.config.SapConnection;
import com.haier.hevwms.security.dao.OperationLogDAO;
import com.haier.hevwms.security.domain.OperationLogSaveModel;
import com.haier.hevwms.util.CommonTool;
import com.haier.openplatform.util.SpringApplicationContextHolder;
import com.haier.openplatform.wms.po.dto.StgPoDownDTO;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;

public class DownloadPoFromSap {
	
	String rfc_id = "ZMM_AEV_INT_PURCHASEOD"; // RFC函数名
	String system_id = "HEVWMS"; 
	String beginTime;
	String endTime; 
	String poNo; 
	private StgPoDownDAO stgPoDownDAO;
	private OperationLogDAO operationLogDAO;
	
	Logger logger = Logger.getLogger(DownloadPoFromSap.class);
	
	public DownloadPoFromSap(String poNo, String beginTime, String endTime){
		this.poNo = poNo;
		this.beginTime = beginTime;
		this.endTime = endTime;
		stgPoDownDAO = SpringApplicationContextHolder.getBean("stgPoDownDAO");
		operationLogDAO = SpringApplicationContextHolder.getBean("operationLogDAO");
	}
	
	public String exchangeWithSap() {
		logger.info("Exchange with sap start------------------------");
		String result = "S";
		OperationLogSaveModel log = new OperationLogSaveModel();
		log.setUserId(UserUtil.getCurrentUser().getId());
		log.setUserName(UserUtil.getCurrentUser().getName());
		log.setAppName("DownloadPoFromSap");

		try {
			SapConnection.initSapConn();
			logger.info("SAP connection successfully");
		} catch (Exception ex) {
			result = "E";
			log.setDescription("Sap Connection Failed:"+ex.getMessage().substring(0, 1000));
			operationLogDAO.save(log);
		}
		
		try{
			JCoDestination destination = JCoDestinationManager.getDestination(SapConnection.ABAP_MS);
			JCoFunction function = destination.getRepository().getFunction(rfc_id);

			// 输入日期参数
			if (beginTime != null) {
				if (endTime != null) {
					if (beginTime.equals(endTime)) {
						function.getImportParameterList().setValue("AEDAT_B", beginTime);
					} else {
						function.getImportParameterList().setValue("AEDAT_B", beginTime);
						function.getImportParameterList().setValue("AEDAT_E", endTime);
					}
				} else {
					function.getImportParameterList().setValue("AEDAT_B", beginTime);
				}
			}  
			
			// 输入公司参数
			function.getImportParameterList().setValue("AEDAT_B", "65C0");
			
			// 输入凭证参数
			if (poNo != null) {
				function.getImportParameterList().setValue("EBELN_B", poNo);
			}
			logger.info("INPUT Parameters=========>" + function.getImportParameterList());	
			function.execute(destination);
			
			JCoTable codes = function.getTableParameterList().getTable("OUTPUT");
			logger.info("OUTPUT Table=========>" + codes);
			
			
			if (codes != null) {
				//PO No: EBELN    PO item No: EBELP
				Map<String, Set<String>> newOrder = new HashMap<String, Set<String>>();
				Map<String, Set<String>> oldOrder = new HashMap<String, Set<String>>();
				for (int i = 0; i < codes.getNumRows(); i++){
					codes.setRow(i);
					Set<String> newItem = new HashSet<String>();
					if(newOrder.get(codes.getString("EBELN".toUpperCase()))==null){
						newItem.add(codes.getString("EBELP".toUpperCase()));
						newOrder.put(codes.getString("EBELN".toUpperCase()), newItem);
					} else {
						newItem = newOrder.get(codes.getString("EBELN".toUpperCase()));
						newItem.add(codes.getString("EBELP".toUpperCase()));
					}
					
					if (oldOrder.get(codes.getString("EBELN".toUpperCase()))==null){
						StgPoDownDTO oldTemp = new StgPoDownDTO();
						oldTemp.setPoNo(codes.getString("EBELN".toUpperCase()));
						List<String> oldPoItems = stgPoDownDAO.getPoItemsByPoNo(oldTemp);
						Set<String> oldItem = new HashSet<String>(oldPoItems);
						oldOrder.put(codes.getString("EBELN".toUpperCase()), oldItem);
					}
				}
				
				Set<Entry<String, Set<String>>> entrySet = oldOrder.entrySet();
				for (Entry<String, Set<String>> oldPo:entrySet){
					Set<String> oldItems = oldPo.getValue();
					Set<String> newItems = newOrder.get(oldPo.getKey());
					for (String exsitedItem:oldItems){
						boolean deleteFlag = false;
						for (String checkItem:newItems){
							if(checkItem.equals(exsitedItem)){
								deleteFlag = true;
								break;
							}
						}
						if (!deleteFlag){
							StgPoDownDTO checkTemp = new StgPoDownDTO();
							checkTemp.setPoNo(oldPo.getKey());
							checkTemp.setPoItemNo(exsitedItem);
							Long ifStart = stgPoDownDAO.ifScanningStart(checkTemp);
							if (ifStart == 0){
								stgPoDownDAO.bankupDeletedItems(checkTemp);
								stgPoDownDAO.deleteByPoItems(checkTemp);
							}
						}
						
					}
				}
				for (int j = 0; j < codes.getNumRows(); j++) {
					codes.setRow(j);
					StgPoDownDTO po = new StgPoDownDTO();
					po.setPoNo(codes.getString("EBELN".toUpperCase()));
					po.setPoItemNo(codes.getString("EBELP".toUpperCase()));
					po.setPoType(codes.getString("BSART".toUpperCase()));
					po.setPlant(codes.getString("WERKS".toUpperCase()));
					po.setPoLocation(codes.getString("LGORT".toUpperCase()));
					po.setMaterialNo(codes.getString("MATNR".toUpperCase()));
					
					String qty = codes.getString("MENGE1".toUpperCase());
					if ("".equals(qty) || qty == null) {
						qty = "0";
					}
					po.setQty(Long.valueOf(qty));
					po.setUnit(codes.getString("MENGE1".toUpperCase()));  //基本单位
					
					String materialDesp = codes.getString("TXZ01".toUpperCase());
					if (!"".equals(materialDesp) && materialDesp != null) {
						materialDesp = CommonTool.replace(materialDesp, "'", " ");
						materialDesp = CommonTool.replace(materialDesp, "\"", "");
					}
					po.setMaterialDesp(materialDesp);
					String poClose = codes.getString("ELIKZ".toUpperCase()); // 订单状态
					if("".equals(poClose) ||poClose == null){
						poClose = "0";
					}
					po.setPoClose(poClose);  //X-不能继续收货. 空-可以继续收货
					String itemDelete = codes.getString("LOEKZ".toUpperCase()); // 删除标记
					//判断是否为空
					if("".equals(itemDelete) ||itemDelete == null){
						itemDelete = "0";
					}
					po.setItemDeltet(itemDelete);
					po.setOrderUnit(codes.getString("MEINS".toUpperCase()));
					po.setPoCreateBy(codes.getString("ERNAM".toUpperCase()));
					po.setPoLastModifyDate(codes.getString("AEDAT".toUpperCase()));
					po.setVendorCode(codes.getString("LIFNR".toUpperCase()));
					po.setVendorName(codes.getString("NAME1".toUpperCase()));
					
					List<StgPoDownDTO> poList = stgPoDownDAO.getExistingPoItem(po);
					if (poList == null || poList.size() == 0){
						po.setCreateBy(UserUtil.getCurrentUser().getName());
						po.setCreateDate(new Date());
						stgPoDownDAO.save(po);
					} else {
						
						Long ifStart = stgPoDownDAO.ifScanningStart(po);
						if (ifStart == 0){
							po.setModifyBy(UserUtil.getCurrentUser().getName());
							po.setModifyDate(new Date());
							stgPoDownDAO.updateByPoItem(po);
						} else {
							StgPoDownDTO dto = new StgPoDownDTO();
							dto.setPoNo(po.getPoNo());
							dto.setPoItemNo(po.getPoItemNo());
							po.setModifyBy(UserUtil.getCurrentUser().getName());
							po.setModifyDate(new Date());
							stgPoDownDAO.updateByPoItem(po);
						}
					}
					
				}
			} else {
				log.setDescription("No data is downloaded!");
				operationLogDAO.save(log);
			}	
		} catch (Exception e) {
			result = "E";
			logger.error("Catch Exception when download PO: " + e.getMessage());
			log.setDescription("Execute Sap Function failed!"+e.getMessage().substring(0, 1000));
			operationLogDAO.save(log);
		}
		logger.info("Download Po Success!");
		return result;
	}

}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).