/*
 * Created on 2005-11-29
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package weaver.system;

import java.sql.Timestamp;
import java.util.*;

import weaver.conn.RecordSet;
import weaver.general.Util;
import ln.LN;
/**
 * @author Samurai
 *
 * TODO To change the template for this generated tyipe comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ChkLicenseTimer implements ThreadWork{
	public ChkLicenseTimer(){
	}
	public void doThreadWork() {
		if(isRemind(false) == 1){
			isRemind(true);
		}
	}
	private int isRemind(boolean remind){
		LN license = new LN();
		license.CkHrmnum();
		String expireDate = license.getExpiredate();
		
		RecordSet rs = new RecordSet();
		rs.writeLog("expiresDate in license file is :"+expireDate);
		if("".equals(expireDate))
		{
			rs.executeSql("select expireDate from license");
			if(rs.next())
			{
				expireDate = rs.getString("expireDate");
				rs.writeLog("expiresDate in database is :"+expireDate);
			}
		}
		rs.executeProc("SystemSet_Select","");
		rs.next();
		String licenseRemind = Util.null2String(rs.getString("licenseRemind"));
		String remindUsers = Util.null2String(rs.getString("remindUsers"));
		int remindDays= Util.getIntValue(rs.getString("remindDays"),0);
		String lastRemindDate = Util.null2String(rs.getString("lastRemindDate"));//上一次提醒工作流程触发的日期
		
		if(licenseRemind.equals("1")&&!remindUsers.equals("")&&remindDays>0&&!expireDate.equals("9999-99-99")){
	      Date newdate = new Date();
	      long datetime = newdate.getTime();
			Timestamp timestamp = new Timestamp(datetime);
			String currentDate = (timestamp.toString()).substring(0, 4) + "-" + (timestamp.toString()).substring(5, 7) + "-" + (timestamp.toString()).substring(8, 10);
			int days = Util.dayDiff(currentDate,expireDate);
			
			if(remindDays>=days&&!lastRemindDate.equals(currentDate)&&days>=0){
				if(!remind){
					license.removeLicenseComInfo();
					return 1;
				}
				rs.writeLog("ChkLicenseTimer.java----------remindDays::"+remindDays+"=====days::"+days+"=====currentDate::"+currentDate+"====expireDate:::"+expireDate);
				SysRemindWorkflow srw = new SysRemindWorkflow();
				String title = "License"+weaver.systeminfo.SystemEnv.getHtmlLabelName(10004072,weaver.general.ThreadVarLanguage.getLang())+"";
				int resource = 1;
				String remark = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10004073,weaver.general.ThreadVarLanguage.getLang())+"License("+license.getLicensecode()+")"+weaver.systeminfo.SystemEnv.getHtmlLabelName(10004074,weaver.general.ThreadVarLanguage.getLang())+""+days+""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10004075,weaver.general.ThreadVarLanguage.getLang())+"<a href=/system/licenseInfo.jsp>"+weaver.systeminfo.SystemEnv.getHtmlLabelName(22012,weaver.general.ThreadVarLanguage.getLang())+"</a>"+weaver.systeminfo.SystemEnv.getHtmlLabelName(10004076,weaver.general.ThreadVarLanguage.getLang())+"";
				try {
					srw.setSysRemindInfo(title,0,0,0,0,resource,remindUsers,remark);
					String sql = "update SystemSet set lastRemindDate='"+currentDate+"'";
					rs.executeSql(sql);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 2;
			}
		}
		return 0;
	}

}
