package com.cim.idm.service.Impl;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.orm.service.CommonServiceDAO;
import com.cim.idm.framework.util.time.TimeStampUtil;
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.name.NameServiceProxy;
import com.cim.idm.wmspackage.receiverequestbatch.management.data.MaterialReceiveActKey;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 斯迪克库存管理
 * @author li
 *
 */
@Service
public class SDKMaterialPackingServiceImpl extends CommonServiceDAO<MaterialPackingKey, MaterialPacking> {

    private static final String MATERIAL_PACKING_NAME = "MATERIALPACKINGNAME";
    private static final String RECEIVE_ACT_NO = "RECEIVEACTNO";
    private static final String UNDO_ID = "UNDOID";
    private static final String SITE_NAME = "SITENAME";

    /**
     * 创建库存信息
     */
    public void receiveMesShippInfo(EventInfo eventInfo, String siteName, String materialPackingName, String durableName, String user, String palletName,
                                    String areaName) throws CustomException {

        String sql2 = "SELECT * FROM MATERIALPACKING WHERE MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
        Map<String, Object> hashMap2 = new HashMap<>();
        hashMap2.put(MATERIAL_PACKING_NAME, materialPackingName);
        List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql2, hashMap2);
        if (queryForList2 == null || queryForList2.isEmpty()) {
            String sql = buildMaterialPackingQuery();
            Map<String, Object> hashMap = new HashMap<>();
            hashMap.put(MATERIAL_PACKING_NAME, materialPackingName);
            List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
            if (queryForList == null || queryForList.isEmpty()) {
                throw new CustomException("不存在要入库的信息");
            }

            CreateInfo createInfo = new CreateInfo();
            createInfo.setSiteName(siteName);
            createInfo.setMaterialPackingName(materialPackingName);
            createInfo.setMaterialPackingType("Box");
            createInfo.setContentMaterialType("");

            Map<String, Object> bindMap = buildBindMap(queryForList.get(0), palletName, areaName, durableName, eventInfo);
            createInfo.setUserColumns(bindMap);
            MaterialPackingServiceProxy.getMaterialPackingService().create(eventInfo, createInfo);
        } else {
            MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK", materialPackingName);
            SetEventInfo setEventInfo = new SetEventInfo();
            Map<String, Object> hashMap3 = new HashMap<>();
            hashMap3.put("PackingState", "Released");
            hashMap3.put("OldPackingState", "Released");
            hashMap3.put("stockState", "Stocked");
            hashMap3.put("OldStockState", "StockOut");
            hashMap3.put("DurableName", durableName);
            hashMap3.put("AreaName", areaName);
            setEventInfo.setUserColumns(hashMap3);
            MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, eventInfo, setEventInfo);
        }
    }

    private String buildMaterialPackingQuery() {
        return "SELECT T.LOTNAME MATERIALPACKINGNAME, T.QTY MATERIALQUANTITY, T.UNIT, M2.RECEIVEREQUESTDETAILNAME, " +
                "T.REQUESTNAME RECEIVEREQUESTNAME, T.PRODUCTSPECNAME MATERIALSPECNAME, SS.SPECNAME, T.SDK_ID, " +
                "M.ERPLOCATION, M.STOCKORGNO ERPFACTORY, T.AREA, T.SHIPUSER, T.PACKINGGRADE, T.SHIPTIME, " +
                "M3.MATERIALTYPE, T.PHASE FROM BS_MES_SHIPPED T " +
                "LEFT JOIN MATERIALRECEIVEREQUEST M ON T.REQUESTNAME = M.RECEIVEREQUESTNAME " +
                "LEFT JOIN MATERIALRECEIVEREQUESTDETAIL M2 ON T.REQUESTNAME = M2.RECEIVEREQUESTNAME " +
                "AND T.PRODUCTSPECNAME = M2.MATERIALSPECNAME AND T.SDK_ID = M2.SDK_ID " +
                "LEFT JOIN SDK_SPEC SS ON T.SDK_ID = SS.SDK_ID " +
                "LEFT JOIN MATERIALSPEC M3 ON T.PRODUCTSPECNAME = M3.MATERIALSPECNAME " +
                "WHERE (T.RECEIVE_FLAG IS NULL OR T.RECEIVE_FLAG <> 'Y') AND T.LOTNAME = :MATERIALPACKINGNAME";
    }

    private Map<String, Object> buildBindMap(Map<String, Object> queryResult, String palletName, String areaName, String durableName, EventInfo eventInfo) {
        Map<String, Object> bindMap = new HashMap<>();
        bindMap.put("lkMaterialPackingName", palletName);
        bindMap.put("MaterialType", queryResult.get("MATERIALTYPE"));
        bindMap.put("MaterialSpecName", queryResult.get("MATERIALSPECNAME"));
        bindMap.put("PackingGrade", queryResult.get("PACKINGGRADE"));
        bindMap.put("SubPackingQuantity", queryResult.get("MATERIALQUANTITY"));
        bindMap.put("MaterialCreateQuantity", queryResult.get("MATERIALQUANTITY"));
        bindMap.put("MaterialQuantity", queryResult.get("MATERIALQUANTITY"));
        bindMap.put("PackingState", "Released");
        bindMap.put("OldPackingState", "Released");
        bindMap.put("stockState", "Stocked");
        bindMap.put("OldStockState", "Created");
        bindMap.put("HoldState", "N");
        bindMap.put("charge", palletName.split("\\|")[4]);
        bindMap.put("AreaName", areaName);
        bindMap.put("LocationName", "");
        bindMap.put("DurableName", durableName);
        bindMap.put("MaterialProcessGroupName", "");
        bindMap.put("LastEventTimeKey", TimeStampUtil.getCurrentEventTimeKey());
        bindMap.put("ReceiveRequestName", queryResult.get("RECEIVEREQUESTNAME"));
        bindMap.put("ReceiveRequestDetailName", queryResult.get("RECEIVEREQUESTDETAILNAME"));
        bindMap.put("MakeDate", queryResult.get("SHIPTIME"));
        bindMap.put("ReceiveTime", eventInfo.getEventTime());
        bindMap.put("ReceiveUser", eventInfo.getEventUser());
        bindMap.put("ShipTime", queryResult.get("SHIPTIME"));
        bindMap.put("ShipUser", queryResult.get("SHIPUSER"));
        bindMap.put("ErpLocation", queryResult.get("ERPLOCATION"));
        bindMap.put("ErpFactory", queryResult.get("ERPFACTORY"));
        bindMap.put("StockInType", "Normal");
        bindMap.put("SDK_ID", queryResult.get("SDK_ID"));
        bindMap.put("PHASE", queryResult.get("PHASE"));
        return bindMap;
    }

    /**
     * 接收ERP返回消息更新WMS凭证
     * @param receiveActNO
     */
    public void SaveUnDoInfo(String receiveActNO, String undoid,String commitDate) {

        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 RECEIVEACTNO=:receiveActNO AND unDoID=:UNDOID";

        Map<String, Object> bindMap_insert = new HashMap<String, Object>();
        bindMap_insert.put("receiveActNO", receiveActNO);
        bindMap_insert.put("UNDOID", undoid);
        bindMap_insert.put("commitDate", commitDate);
        IDMFrameServiceProxy.getSqlTemplate()
        .update(insertUnDOMaterialPackingInfo, bindMap_insert);
		
		
		String sql = "UPDATE MATERIALRECEIVEACT m  SET m.RECEIVEACTSTATE  = 'Received' WHERE m.RECEIVEACTNO  = :RECEIVEACTNO ";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("RECEIVEACTNO", receiveActNO);
		IDMFrameServiceProxy.getSqlTemplate().update(sql, hashMap);
	}


	/**
	 * 接收ERP返回消息更新WMS凭证
	 * @param undoid
	 */
	public void SaveUnDoInfoByShip(String shipRequest, String undoid) {

		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,TO_CHAR(SYSDATE, 'YYYY-MM-YY HH24:MI:SS') 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 RECEIVEREQUESTNAME=:RECEIVEREQUESTNAME AND unDoID=:UNDOID";

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

	}

	/**
	 * 更新物料凭证
	 * @param receiveActNO
	 * @param eventInfo
	 * @param UNDOID
	 */
	public void updateUndoID(String receiveActNO,EventInfo eventInfo, String UNDOID) {
		String sql = "SELECT SITENAME, MATERIALPACKINGNAME FROM MATERIALPACKING T WHERE T.RECEIVEACTNO = :RECEIVEACTNO ";
		Map<String, Object> hashMap = new HashMap<String, Object> ();
		hashMap.put("RECEIVEACTNO", receiveActNO);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		Map<String, Object> bindMap = new HashMap<String, Object>();
		SetEventInfo setEventInfo = new SetEventInfo();
		for (Map<String, Object> map : queryForList) {
			bindMap.put("unDoID",UNDOID);
			setEventInfo.setUserColumns(bindMap);
			MaterialPackingKey materialPackingKey = new MaterialPackingKey(map.get("SITENAME").toString(), map.get("MATERIALPACKINGNAME").toString());
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, eventInfo, setEventInfo);
		}
	}


	/**
	 * 更新物料凭证
	 * @param shiprequestName
	 * @param eventInfo
	 * @param UNDOID
	 * @throws CustomException 
	 */
	public void updateUndoIDByShipRequest(String shiprequestName,EventInfo eventInfo, String UNDOID) throws CustomException {
		String sql = "SELECT SITENAME, MATERIALPACKINGNAME FROM MATERIALPACKING T WHERE T.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME ";
		Map<String, Object> hashMap = new HashMap<String, Object> ();
		hashMap.put("RECEIVEREQUESTNAME", shiprequestName);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		Map<String, Object> bindMap = new HashMap<String, Object>();
		SetEventInfo setEventInfo = new SetEventInfo();
		if(UNDOID==null)
		{
			throw new CustomException("接口凭证为NULL");
		}
		for (Map<String, Object> map : queryForList) {
			bindMap.put("unDoID",UNDOID);
			setEventInfo.setUserColumns(bindMap);
			MaterialPackingKey materialPackingKey = new MaterialPackingKey(map.get("SITENAME").toString(), map.get("MATERIALPACKINGNAME").toString());
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, eventInfo, setEventInfo);
		}
	}


	/**
	 * 更新库存状态
	 * @param receiveActNO
	 * @param eventInfo
	 * @param UNDOID
	 */
	public void updateStockState(String receiveActNO,EventInfo eventInfo, String UNDOID) {

		String sql = "SELECT SITENAME, MATERIALPACKINGNAME FROM MATERIALPACKING T WHERE T.RECEIVEACTNO = :RECEIVEACTNO AND UNDOID = :UNDOID";
		Map<String, Object> hashMap = new HashMap<String, Object> ();
		hashMap.put("RECEIVEACTNO", receiveActNO);
		hashMap.put("UNDOID", UNDOID);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		Map<String, Object> bindMap = new HashMap<String, Object>();
		SetEventInfo setEventInfo = new SetEventInfo();
		for (Map<String, Object> map : queryForList) {
			bindMap.put("packingState","Released");
			bindMap.put("stockState","Stocked");
			bindMap.put("unDoID",UNDOID);
			setEventInfo.setUserColumns(bindMap);
			MaterialPackingKey materialPackingKey = new MaterialPackingKey(map.get("SITENAME").toString(), map.get("MATERIALPACKINGNAME").toString());
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, eventInfo, setEventInfo);
		}

		MaterialReceiveActKey mrk = new MaterialReceiveActKey();
		String sql2 =" SELECT RECEIVEREQUESTNAME FROM MATERIALRECEIVEACT WHERE SITENAME =:SITENAME AND RECEIVEACTNO =:RECEIVEACTNO ";

		Map<String, Object> bindMap1 = new HashMap<String, Object>();
		bindMap1.put("SITENAME","SDK");
		bindMap1.put("RECEIVEACTNO", receiveActNO);
		List<Map<String, Object>> queryResult = IDMFrameServiceProxy
				.getSqlTemplate()
				.queryForList(sql2, bindMap1);
		mrk.setSiteName("SDK");
		mrk.setReceiveactNO(receiveActNO);
		mrk.setReceiveRequestName(queryResult.get(0).get("RECEIVEREQUESTNAME").toString());
		com.cim.idm.wmspackage.materialreceiverequestact.management.info.SetEventInfo setevent = new com.cim.idm.wmspackage.materialreceiverequestact.management.info.SetEventInfo();
		Map<String, Object> bindMap2 = new HashMap<String, Object>();
		bindMap2.put("receiveActState", "Received");
		setevent.setUserColumns(bindMap2);
		MaterialReceiveRequestActServiceProxy
		.getReceiveRequestBatchService().setEvent(mrk,
				eventInfo, setevent);	
	}

	
	/**
	 * 更新库存状态
	 * @param opCode
	 * @param eventInfo
	 * @param UNDOID
	 */
	public void updateStockState(EventInfo eventInfo, String UNDOID,String opCode) {

		String sql = "SELECT SITENAME, MATERIALPACKINGNAME FROM MATERIALPACKING T WHERE  UNDOID = :UNDOID";
		Map<String, Object> hashMap = new HashMap<String, Object> ();
		hashMap.put("UNDOID", UNDOID);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		Map<String, Object> bindMap = new HashMap<String, Object>();
		SetEventInfo setEventInfo = new SetEventInfo();
		for (Map<String, Object> map : queryForList) {
			bindMap.put("packingState","Released");
			bindMap.put("stockState","Stocked");
			bindMap.put("unDoID",UNDOID);
			bindMap.put("messageId",opCode);
			setEventInfo.setUserColumns(bindMap);
			MaterialPackingKey materialPackingKey = new MaterialPackingKey(map.get("SITENAME").toString(), map.get("MATERIALPACKINGNAME").toString());
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, eventInfo, setEventInfo);
		}
	}

	/**
	 * 模拟NC
	 * @param shipRequestName
	 * @return
	 */
	public String SendNCYK (String shipRequestName) {
		return TimeStampUtil.getCurrentEventTimeKey();
	}


	/**
	 * 更新批次的凭证
	 * @param shipRequestName
	 * @return
	 */
	public void updateUndoId (String shipRequestName,String UNDOID,EventInfo eventInfo) {
		String sql = "SELECT MATERIALPACKINGNAME, SITENAME FROM MATERIALPACKING WHERE SHIPREQUESTNAME = :SHIPREQUESTNAME ";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		for (Map<String, Object> map : queryForList) {
			SetEventInfo setEventInfo = new SetEventInfo();
			Map<String, Object> bindMap = new HashMap<String, Object>();
			bindMap.put("unDoID",UNDOID);
			setEventInfo.setUserColumns(bindMap);
			MaterialPackingKey materialPackingKey = new MaterialPackingKey(map.get("SITENAME").toString(), map.get("MATERIALPACKINGNAME").toString());
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, eventInfo, setEventInfo);
		}

	}
	
	public void updateUndoIdByReceive (String receiveRequestName,String UNDOID,EventInfo eventInfo) {
		String sql = "SELECT MATERIALPACKINGNAME, SITENAME FROM MATERIALPACKING WHERE RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME AND STOCKSTATE = 'Created' ";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("RECEIVEREQUESTNAME", receiveRequestName);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		for (Map<String, Object> map : queryForList) {
			SetEventInfo setEventInfo = new SetEventInfo();
			Map<String, Object> bindMap = new HashMap<String, Object>();
			bindMap.put("unDoID",UNDOID);
			setEventInfo.setUserColumns(bindMap);
			MaterialPackingKey materialPackingKey = new MaterialPackingKey(map.get("SITENAME").toString(), map.get("MATERIALPACKINGNAME").toString());
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, eventInfo, setEventInfo);
		}

	}
	
	
	public void updateUndoIdByList (List<MaterialPacking> list,String UNDOID,EventInfo eventInfo) {
		for (MaterialPacking mp : list) {
			SetEventInfo setEventInfo = new SetEventInfo();
			Map<String, Object> bindMap = new HashMap<String, Object>();
			bindMap.put("unDoID",UNDOID);
			setEventInfo.setUserColumns(bindMap);
			MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK", mp.getMaterialPackingName().toString());
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, eventInfo, setEventInfo);
		}


	}

	public static List<String> generateSDKLot(String ruleName,
			String productSpecName) {

		String sql = "SELECT SQL_TEXT " + "FROM NAMERULEDEF "
				+ "WHERE RULENAME = :ruleName ";


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

		bindMap.put("ruleName", ruleName);

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

		String sSqlText = "";

		if (sqlResult.size() > 0) {
			sSqlText = sqlResult.get(0).get("SQL_TEXT").toString();
		} else {

		}

		bindMap.clear();
		sqlResult.clear();
		bindMap = new HashMap<String, String>();
		bindMap.put("PRODUCTSPECNAME", productSpecName);
		sqlResult = IDMFrameServiceProxy.getSqlTemplate()
				.queryForList(sSqlText, bindMap);

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

		for (int i = 0; i < sqlResult.size(); i++) {

			argSeq.add(sqlResult.get(i).get("ENUMVALUE").toString());
		}

		List<String> oqaLotName = NameServiceProxy
				.getNameRuleDefService().generateName(ruleName,
						argSeq, 1);

		return oqaLotName;
	}

	/**
	 * 接收ERP返回消息更新WMS凭证
	 * @param undoid
	 */
	public void SaveUnDoInfo( String undoid,String commitDate) {

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

	}

}
