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