package flow;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONObject;

/**
 * A basic servlet which allows a user to define their code, generate any
 * output, and to select where to transition to next. Last generated by
 * Orchestration Designer at: 2021年7月30日 下午04时30分47秒
 */
public class ser_workTime extends com.avaya.sce.runtime.BasicServlet {
	private static Logger log = Logger.getLogger(ser_workTime.class);
	// {{START:CLASS:FIELDS
	// }}END:CLASS:FIELDS

	/**
	 * Default constructor Last generated by Orchestration Designer at:
	 * 2021年7月30日 下午04时30分47秒
	 */
	public ser_workTime() {
		// {{START:CLASS:CONSTRUCTOR
		super();
		// }}END:CLASS:CONSTRUCTOR
	}

	/**
	 * This method allows for custom integration with other Java components. You
	 * may use Java for sophisticated logic or to integrate with custom
	 * connectors (i.e. JMS, custom web services, sockets, XML, JAXB, etc.)
	 * 
	 * Any custom code added here should work as efficiently as possible to
	 * prevent delays. It's important to design your callflow so that the voice
	 * browser (Voice Portal/IR) is not waiting too long for a response as this
	 * can lead to a poor caller experience. Additionally, if the response to
	 * the client voice browser exceeds the configured timeout, the platform may
	 * throw an "error.badfetch".
	 * 
	 * Using this method, you have access to all session variables through the
	 * SCESession object.
	 * 
	 * The code generator will *** NOT *** overwrite this method in the future.
	 * Last generated by Orchestration Designer at: 2021年7月30日 下午04时30分47秒
	 */
	public void servletImplementation(
			com.avaya.sce.runtimecommon.SCESession mySession) {
		System.out.println("----------------");
		System.out.println("进入ser_workTime");
		mySession.getVariableField(IProjectVariables.WORK_TIME_TYPE).setValue("白班");
		getWorkTime(mySession);
		String wtt = mySession.getVariableField(IProjectVariables.WORK_TIME_TYPE)
				.getStringValue();
		System.out.println("ser_workTime:workTimeType:" + wtt);
		mySession.getVariableField(IProjectVariables.SER_RESULT).setValue(
				wtt);

	}
	/**
	 * 利用getWorkTime请求的工具类获取工作时间写入变量workTimeType中 分别为白班，夜班
	 * @ClassName: getWorkTime 
	 * @Description: seven
	 * @author Devin <xxx> 
	 * @date 2021年10月22日 下午2:23
	 *  
	 */
	public void getWorkTime (com.avaya.sce.runtimecommon.SCESession mySession){
		//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
		//获取系统时间
		String smsTime = df.format(new Date());
		String format = "HH:mm:ss";
        Date nowTime;
		try {
			nowTime = new SimpleDateFormat(format).parse(smsTime);
			Date startTime = new SimpleDateFormat(format).parse("09:00:00");
	        Date endTime = new SimpleDateFormat(format).parse("20:00:00");
	       boolean effectiveDate = isEffectiveDate(nowTime, startTime, endTime);
	        if(effectiveDate==true){
	        	System.out.println("现工作时间为白班");
	        	mySession.getVariableField(IProjectVariables.WORK_TIME_TYPE).setValue("白班");
	        }else {
	        	System.out.println("现工作时间为夜班");
	        	mySession.getVariableField(IProjectVariables.WORK_TIME_TYPE).setValue("夜班");
			}
		} catch (ParseException e) {
			System.out.println("确认工作时间是出现问题      "+e.getMessage());
		}
        
		
	}
	/**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     * 
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

	/**
	 * Builds the list of branches that are defined for this servlet object.
	 * This list is built automatically by defining Goto nodes in the call flow editor.
	 * It is the programmer's responsibilty to provide at least one enabled Goto.<BR>
	 *
	 * The user should override updateBranches() to determine which Goto that the
	 * framework will activate.  If there is not at least one enabled Goto item, 
	 * the framework will throw a runtime exception.<BR>
	 *
	 * This method is generated automatically and changes to it may
	 * be overwritten next time code is generated.  To modify the list
	 * of branches for the flow item, override:
	 *     <code>updateBranches(Collection branches, SCESession mySession)</code>
	 *
	 * @return a Collection of <code>com.avaya.sce.runtime.Goto</code>
	 * objects that will be evaluated at runtime.  If there are no gotos
	 * defined in the Servlet node, then this returns null.
	 * Last generated by Orchestration Designer at: 2023年4月14日 下午05时14分31秒
	 */
	public java.util.Collection getBranches(com.avaya.sce.runtimecommon.SCESession mySession) {
		java.util.List list = null;
		com.avaya.sce.runtime.Goto aGoto = null;
		list = new java.util.ArrayList(1);

		aGoto = new com.avaya.sce.runtime.Goto("ser_check", 0, true, "Default");
		list.add(aGoto);

		return list;
	}
}
