package com.cim.idm.yard.service;


import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

public class YardMoveInServiceImpI {
	private static Log logger = LogFactory.getLog("YardLocationServiceImpI");
	
	/**
	 * B20堆场搬入口增加
	 * @throws Exception 
	 */
	public  void CreateForMoveIn(String Movein,String Description,String Resperson,String SiteName,EventInfo eventInfo)throws Exception
	{
		logger.info("堆场搬入口管理创建   => " + Movein + "|" + Resperson);
//		查询是否存在主表，不存在就新增
		String querySqlString="SELECT MOVEIN FROM YARD_MOVEIN WHERE SITENAME = :SITENAME AND MOVEIN = :MOVEIN";
		Map<String, String> queryMap = new HashMap<String, String>();
		queryMap.put("SITENAME", SiteName);
		queryMap.put("MOVEIN", Movein); 
		List<Map<String, Object>> returnListStorge = IDMFrameServiceProxy.getSqlTemplate().queryForList(querySqlString, queryMap);
		if(returnListStorge != null && returnListStorge.size() > 0) {
			//throw new Exception("库位名称：" + LocationName + " 已经生成，不用重复操作");
			logger.info("同一工厂相同搬入口已经存在  => " + Movein + "|" + SiteName);
			throw new CustomException(String.valueOf(1), "同一工厂相同搬入口已经存在  => " + Movein + "|" + SiteName);
		}else {
			Map<String, Object> insertMap = new HashMap<String, Object>();
			String insertSql=" INSERT INTO YARD_MOVEIN (SITENAME, MOVEIN, CREATUSER, CREATTIME, DESCRIPTION, RESPERSON, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER, LASTEVENTCOMMENT)VALUES(:SITENAME,:MOVEIN,:CREATUSER,:CREATTIME,:DESCRIPTION,:RESPERSON,:LASTEVENTNAME,:LASTEVENTTIMEKEY,:LASTEVENTTIME,:LASTEVENTUSER,:LASTEVENTCOMMENT)";
			insertMap.put("SITENAME", SiteName);
			insertMap.put("MOVEIN", Movein);
			insertMap.put("CREATUSER", eventInfo.getEventUser());
			insertMap.put("CREATTIME", eventInfo.getEventTime());
			insertMap.put("DESCRIPTION", Description);
			insertMap.put("RESPERSON", Resperson);
			insertMap.put("LASTEVENTNAME", eventInfo.getEventUser());
			insertMap.put("LASTEVENTTIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
			insertMap.put("LASTEVENTTIME", eventInfo.getEventTime());
			insertMap.put("LASTEVENTUSER", eventInfo.getEventUser());
			insertMap.put("LASTEVENTCOMMENT", "Created");
		
			IDMFrameServiceProxy.getSqlTemplate().update(insertSql, insertMap);
		}
	}
	
	/**
	 * B20堆场搬入口修改
	 * @throws Exception 
	 */
	public void ModifyForMoveIn(String sitename,String descprition,String responson, EventInfo eventInfo,String Movein)
	{
		Map<String, Object> insertMap = new HashMap<String, Object>();
		String UpdString="UPDATE YARD_MOVEIN SET DESCRIPTION=:DESCRIPTION ,RESPERSON=:RESPERSON,LASTEVENTNAME=:LASTEVENTNAME,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE SITENAME=:SITENAME AND MOVEIN=:MOVEIN";
		insertMap.put("DESCRIPTION", descprition);
		insertMap.put("RESPERSON", responson);
		insertMap.put("LASTEVENTNAME", eventInfo.getEventUser());
		insertMap.put("LASTEVENTTIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
		insertMap.put("LASTEVENTTIME", eventInfo.getEventTime());
		insertMap.put("LASTEVENTUSER", eventInfo.getEventUser());
		insertMap.put("LASTEVENTCOMMENT", "ModifyYardLocation");
		insertMap.put("SITENAME", sitename);
		insertMap.put("MOVEIN", Movein);
		IDMFrameServiceProxy.getSqlTemplate().update(UpdString, insertMap);
	}
	
	/**
	 * B20堆场库位删除
	 * @throws Exception 
	 */
	public void DelForMoveIn(String sitename,String Movein)
	{
		Map<String, Object> insertMap = new HashMap<String, Object>();
		String delSql = " DELETE FROM YARD_MOVEIN yd WHERE yd.SITENAME = :SITENAME AND yd.MOVEIN = :MOVEIN  ";
		insertMap.put("SITENAME", sitename);
		insertMap.put("MOVEIN", Movein);
		IDMFrameServiceProxy.getSqlTemplate().update(delSql, insertMap);
	}
}
