package com.kingdee.eas.custom.erp.util;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.util.SHRSyssettingUtil;

public class SynPublicUtils {

	private  static  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	 // 创建SimpleDateFormat实例来格式化日期  
	private  static   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	
	private static Properties properties = null;
	/**
	 * 
	 * 更新时间记录表为当前时间
	 * @throws BOSException 
	 */
	public static void updateSynTimeTable(Context ctx,String evenName) throws BOSException{
		String sql = "update T_HR_SynTimeTable set event_timestamp = now() where event_name = '"+evenName+"'";
		DbUtil.execute(ctx, sql.toString());
	}
	
	
	/**
	 * 获取上次同步结束的时间
	 * @param ctx
	 * @param evenName
	 * @throws BOSException 
	 * @throws SQLException 
	 */
	public static String getLastUpdateSynTableTimeStrByEvenName(Context ctx,String evenName) throws BOSException, SQLException{
		String sql = "select event_timestamp as evenTimeStamp from T_HR_SynTimeTable where event_name = '"+evenName+"'";
		 IRowSet executeQuery = DbUtil.executeQuery(ctx, sql);	 
		 Date evenTimeStamp = null;
		 if(executeQuery.size()>0){
			while(executeQuery.next()){
				evenTimeStamp = executeQuery.getDate("evenTimeStamp");
			}
		 }
		 String lastUpdateTimeStr = "";
		 if(evenTimeStamp != null){
			 lastUpdateTimeStr =  dateFormat.format(evenTimeStamp);
		 }
		 return lastUpdateTimeStr;
	}
	
	 /**
	  * 获取当前时间的字符串
	  * @return
	  */
	 public static String getNowTimeStr(){
		 // 获取当前时间  
	        Date now = new Date();  
	          
	        // 创建SimpleDateFormat实例来格式化日期  
	        // 这里使用"yyyy-MM-dd HH:mm:ss"作为日期时间格式  
	          
	        // 格式化日期并获取字符串  
	        String formattedDateTime = dateFormat.format(now);  
		 
		return formattedDateTime;
	 }
	
	 /**
		 * 获取前一天的00:00:00时间 
		 * @return
		 */
		 public static String  getLastDateZeroTime(){
			   // 获取当前日期的Calendar实例  
		        Calendar calendar = Calendar.getInstance();  
		          
		        // 计算前一天  
		        calendar.add(Calendar.DAY_OF_MONTH, -1);  
		          
		        // 设置时间为当天的开始（对于前一天来说，也是00:00:00）  
		        calendar.set(Calendar.HOUR_OF_DAY, 0);  
		        calendar.set(Calendar.MINUTE, 0);  
		        calendar.set(Calendar.SECOND, 0);  
		        calendar.set(Calendar.MILLISECOND, 0);  
		        // 格式化日期并获取字符串  
		        String formattedDateTime = dateFormat.format(calendar.getTime());  
		          
			 return formattedDateTime;
		 }
	 
	 
		 public static Properties getConfig() {
		        if (properties != null) {
		            return properties;
		        } else {
		            StringBuffer sb = new StringBuffer();
		            sb.append(SHRSyssettingUtil.getShrPropertiesPath())
		                    .append(File.separator).append("shr-xhClient.properties");
		            Properties properties = null;
		            try {
		                properties = SHRSyssettingUtil.getPropertiesFileByPath(sb.toString());
		            } catch (IOException e) {
		                e.printStackTrace();
		                return null;
		            }

		            return properties;
		        }
		    }
		 
		 
	 
}
