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 YardDepartMentServiceImpI {
	private static Log logger = LogFactory.getLog("YardLocationServiceImpI");
	
	/**
	 * B20堆场部门增加
	 * @throws Exception 
	 */
	public  void CreateForYardDepartMent(String Departmentnum,String Description,String Departmentname,String SiteName, String SectionNum, String SectionName, EventInfo eventInfo)throws Exception
	{
		logger.info("堆场设备部门管理创建   => " + Departmentnum + "|" + Departmentname);
         //		查询是否存在主表，不存在就新增
		String querySqlString="SELECT Departmentnum FROM YARD_DEPARTMENT WHERE SITENAME = :SITENAME AND DEPARTMENTNUM = :DEPARTMENTNUM AND DEPARTMENTNAME=:DEPARTMENTNAME AND SECTIONNUM = :SECTIONNUM";
		Map<String, String> queryMap = new HashMap<String, String>();
		queryMap.put("SITENAME", SiteName);
		queryMap.put("DEPARTMENTNUM", Departmentnum); 
		queryMap.put("DEPARTMENTNAME", Departmentname); 
		queryMap.put("SECTIONNUM", SectionName); 
		List<Map<String, Object>> returnListStorge = IDMFrameServiceProxy.getSqlTemplate().queryForList(querySqlString, queryMap);
		if(returnListStorge != null && returnListStorge.size() > 0) {
			logger.info("部门单号：" + Departmentnum + " 已经生成，不用重复操作");
			throw new CustomException(String.valueOf(1), "部门科室：" + Departmentnum + " 已经生成，不用重复操作");
		}else {
			Map<String, Object> insertMap = new HashMap<String, Object>();
			String insertSql=" INSERT INTO YARD_DEPARTMENT(SITENAME, DEPARTMENTNUM, DEPARTMENTNAME, SECTIONNUM, SECTIONNAME, CREATUSER, CREATTIME, DESCRIPTION, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER, LASTEVENTCOMMENT)VALUES " + 
					" (:SITENAME,:DEPARTMENTNUM,:DEPARTMENTNAME, :SECTIONNUM, :SECTIONNAME, :CREATUSER,:CREATTIME,:DESCRIPTION,:LASTEVENTNAME,:LASTEVENTTIMEKEY,:LASTEVENTTIME,:LASTEVENTUSER,:LASTEVENTCOMMENT)";
			insertMap.put("SITENAME", SiteName);
			insertMap.put("DEPARTMENTNUM", Departmentnum);
			insertMap.put("DEPARTMENTNAME", Departmentname);
			insertMap.put("SECTIONNUM", SectionNum);
			insertMap.put("SECTIONNAME", SectionName);
			insertMap.put("CREATUSER", eventInfo.getEventUser());
			insertMap.put("CREATTIME", eventInfo.getEventTime());
			
			insertMap.put("DESCRIPTION", Description);
			insertMap.put("LASTEVENTNAME", "Created");
			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 ModifyForYardDepartMent(String sitename,String descprition,String Departmentname, EventInfo eventInfo,String Departmentnum, String SectionNum, String SectionName)
	{
		Map<String, Object> insertMap = new HashMap<String, Object>();
		String UpdString="UPDATE YARD_DEPARTMENT SET DESCRIPTION=:DESCRIPTION ,DEPARTMENTNAME=:DEPARTMENTNAME, SECTIONNUM = :SECTIONNUM, SECTIONNAME = :SECTIONNAME, LASTEVENTNAME=:LASTEVENTNAME,LASTEVENTTIMEKEY=:LASTEVENTTIMEKEY,LASTEVENTTIME=:LASTEVENTTIME,LASTEVENTUSER=:LASTEVENTUSER,LASTEVENTCOMMENT=:LASTEVENTCOMMENT WHERE SITENAME=:SITENAME AND DEPARTMENTNUM=:DEPARTMENTNUM";
		insertMap.put("DESCRIPTION", descprition);
		insertMap.put("DEPARTMENTNAME", Departmentname);
		insertMap.put("LASTEVENTNAME", eventInfo.getEventUser());
		insertMap.put("SECTIONNUM", SectionNum);
		insertMap.put("SECTIONNAME", SectionName);
		insertMap.put("LASTEVENTTIMEKEY", TimeStampUtil.getCurrentEventTimeKey());
		insertMap.put("LASTEVENTTIME", eventInfo.getEventTime());
		insertMap.put("LASTEVENTUSER", eventInfo.getEventUser());
		insertMap.put("LASTEVENTCOMMENT", "ModifyYardDepartMent");
		insertMap.put("SITENAME", sitename);
		insertMap.put("DEPARTMENTNUM", Departmentnum);
		IDMFrameServiceProxy.getSqlTemplate().update(UpdString, insertMap);
	}
	
	/**
	 * B20堆场部门删除
	 * @throws Exception 
	 */
	public void DelForYardDepartMent(String sitename,String Departmentnum, String SectionNum)
	{
		Map<String, Object> insertMap = new HashMap<String, Object>();
		String delSql = " DELETE FROM YARD_DEPARTMENT yd WHERE yd.SITENAME = :SITENAME AND yd.DEPARTMENTNUM = :DEPARTMENTNUM AND yd.SECTIONNUM = :SECTIONNUM";
		insertMap.put("SITENAME", sitename);
		insertMap.put("DEPARTMENTNUM", Departmentnum);
		insertMap.put("SECTIONNUM", SectionNum);
		IDMFrameServiceProxy.getSqlTemplate().update(delSql, insertMap);
	}
}
