package com.cim.idm.mwmsextend.timer.smt;

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

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.cim.idm.framework.IDMFrameServiceProxy;

public class SMTTimer implements Job{

	@SuppressWarnings("static-access")
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		// TODO Auto-generated method stub
		System.out.println("do SMTInventory Sumary ...");
		try
		{	 
			IDMFrameServiceProxy.getTransactionManager().beginTransaction();
			String sql;
			List<Map<String, Object>> rs;		
			//判断是否已经有进程在执行自动计算功能了				
			sql = "SELECT CONSTANTVALUE FROM CONSTANTDEF WHERE CONSTANTNAME='WMSAutoSMTInventoryStart' for update";			
			rs = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, new Object[]{});			
			if(rs.equals(null) || rs.size()==0)//rs[0][0].equals(""))
			{							
				try {			
					//首次未有SPCAutoCalculateCLStart值时，插入该值
					sql="INSERT INTO CONSTANTDEF(CONSTANTNAME,DESCRIPTION,CONSTANTVALUE,CACHEFLAG) "
							+ "VALUES('WMSAutoSMTInventoryStart','One of wms Server has started SMT ','N','N')";
					IDMFrameServiceProxy.getSqlTemplate().update(sql, new Object[]{});	
				} catch (Exception e )
				{					
					//首次未有SPCAutoCalculateCLStart值时，第一个线程已经完成插入，因此后续线程退出计算
					System.out.println("Insert value failed for another process has started.");
					IDMFrameServiceProxy.getTransactionManager().commitTransaction();
					return;	
				}		
			}
			else
			{
				System.out.println("WMSAutoSMTInventoryStart = "+rs.get(0).get("CONSTANTVALUE").toString());
				if(rs.get(0).get("CONSTANTVALUE").toString().equals("Y"))//已经有进程在自动计算，退出等待下次执行
				{
					IDMFrameServiceProxy.getTransactionManager().commitTransaction();	
					System.out.println("Another process has started.");					
					return;
				}		
			}
				
			//无进程在自动计算，本进程开始自动计算。一下是

			sql = "UPDATE CONSTANTDEF SET CONSTANTVALUE='Y'  WHERE CONSTANTNAME='WMSAutoSMTInventoryStart'";	
			IDMFrameServiceProxy.getSqlTemplate().update(sql, new Object[]{});			
			IDMFrameServiceProxy.getTransactionManager().commitTransaction();	

			/***End*****/
			
			IDMFrameServiceProxy.getTransactionManager().beginTransaction();
			
			String sqlinsert=" INSERT INTO SMT_BOEINVENTORY (SITENAME,BOESITENAME,MATERIALSPECNAME,MATERIALQUANTITY,EVENTTIME)  "
					+ " SELECT SITENAME,BOESITENAME,MATERIALSPECNAME,SUM (MATERIALQUANTITY) AS MATERIALQUANTITY,SYSDATE "
					+ " FROM MATERIALPACKINGSMT "
					+ " WHERE STOCKSTATE='Stocked' "
					+ " GROUP BY SITENAME,BOESITENAME,MATERIALSPECNAME,SYSDATE ";
			IDMFrameServiceProxy.getSqlTemplate().update(sqlinsert, new Object[]{});	
			System.out.println(sqlinsert+"  completed!");
			String endsql = "UPDATE CONSTANTDEF SET CONSTANTVALUE='N'  WHERE CONSTANTNAME='WMSAutoSMTInventoryStart'";	
			IDMFrameServiceProxy.getSqlTemplate().update(endsql, new Object[]{});
			IDMFrameServiceProxy.getTransactionManager().commitTransaction();	
			
		    //Calendar  currentTimeC = Calendar.getInstance();
		    //currentTimeC.setTime(currentTime);
		}
		catch (Exception e )
		{	
			IDMFrameServiceProxy.getTransactionManager().rollbackAllTransactions();			
			//首次未有SPCAutoCalculateCLStart值时，第一个线程已经完成插入，因此后续线程退出计算
			System.out.println("Insert value failed for another process has started.");
			String endsql = "UPDATE CONSTANTDEF SET CONSTANTVALUE='N'  WHERE CONSTANTNAME='WMSAutoSMTInventoryStart'";	
			IDMFrameServiceProxy.getSqlTemplate().update(endsql, new Object[]{});	
			return;	
		}	
		finally{
		String endsql = "UPDATE CONSTANTDEF SET CONSTANTVALUE='N'  WHERE CONSTANTNAME='WMSAutoSMTInventoryStart'";	
		IDMFrameServiceProxy.getSqlTemplate().update(endsql, new Object[]{});	
		IDMFrameServiceProxy.getTransactionManager().commitTransaction();	
	}
		
		

	}

}
