package flow;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.apache.log4j.Logger;

/**
 * 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月31日 下午06时59分25秒
 */
public class ser_transfer extends com.avaya.sce.runtime.BasicServlet {
	private static Logger log = Logger.getLogger(ser_transfer.class);
	// {{START:CLASS:FIELDS
	// }}END:CLASS:FIELDS

	/**
	 * Default constructor Last generated by Orchestration Designer at:
	 * 2021年7月31日 下午06时59分25秒
	 */
	public ser_transfer() {
		// {{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月31日 下午06时59分25秒
	 */
	public void servletImplementation(
			com.avaya.sce.runtimecommon.SCESession mySession) {
		System.out.println("-----------------");
		System.out.println("进入ser_transfer");
		String CurrentMenuName=mySession.getVariableField(IProjectVariables.CURRENT_MENU_NAME)
				.getStringValue();
		System.out.println("ser_transfer：CurrentMenuName:"+CurrentMenuName);
		/*String ani="15694343651";
		GetVND getVND = new GetVND();
		getVND.getVDN(mySession);*/
		/*String Dnis=getTranDnis();*/
		String Dnis = mySession.getVariableField(IProjectVariables.SESSION,IProjectVariables.SESSION_FIELD_DNIS).getStringValue();
		String ANI = mySession.getVariableField(IProjectVariables.SESSION,IProjectVariables.SESSION_FIELD_ANI).getStringValue();
		String TransDnis = mySession.getVariableField(IProjectVariables.TRANS_DNIS).getStringValue();
		//VIP等级
		String vipGrade =mySession.getVariableField(IProjectVariables.VIPGRADE).getStringValue();
		String uui=vipGrade+"&"+ANI+"&"+Dnis+"&"+TransDnis;
		System.out.println("UUI:"+uui);
		log.debug("-----转人工VDN号码是："+TransDnis+"-----");
		//mySession.getVariableField(IProjectVariables.TRANS_DNIS).setValue(Dnis);
		mySession.getVariableField(IProjectVariables.UUI).setValue(uui);
		mySession.getVariableField(IProjectVariables.SER_RESULT).setValue(CurrentMenuName);
		
		String NextNodeID = mySession.getVariableField(
				IProjectVariables.NEXT_MENU).getStringValue();
		String next_node = "end";
		System.out.println("NextNodeID:"+NextNodeID);
		String Result=mySession.getVariableField(IProjectVariables.SER_RESULT).getStringValue();
		System.out.println("Result:"+Result);
		HashMap<String, Object>  hMapAll = (HashMap<String, Object>) mySession
				.getVariableField(IProjectVariables.MENU_ALL)
				.getObjectValue();
		HashMap<String, ArrayList<CMenuItemConfig>> hMapChild = (HashMap<String, ArrayList<CMenuItemConfig>>) mySession
				.getVariableField(IProjectVariables.MENU_CHILD)
				.getObjectValue();
		HashMap<String, CMenuConfig> hMapParent = (HashMap<String, CMenuConfig>) mySession
				.getVariableField(IProjectVariables.MENU_PARENT)
				.getObjectValue();
		
		if (hMapAll.containsKey(NextNodeID)) {
			CMenuConfig CMenuConfig = (CMenuConfig) hMapAll.get(NextNodeID);
			System.out.println("setannnext:"+CMenuConfig);
					mySession.getVariableField(
							IProjectVariables.NEXT_MENU).setValue(CMenuConfig.getNextMenu());
		}
		//upNode(mySession);
		System.out.println(mySession.getVariableField(IProjectVariables.SL_CALL_IN_TYPE).getStringValue());
		System.out.println(mySession.getVariableField(IProjectVariables.SL_AREA).getStringValue());
		System.out.println(mySession.getVariableField(IProjectVariables.SL_SERV_NAME).getStringValue());
		log.debug(mySession.getVariableField(IProjectVariables.SL_CALL_IN_TYPE).getStringValue());
		log.debug(mySession.getVariableField(IProjectVariables.SL_AREA).getStringValue());
		log.debug(mySession.getVariableField(IProjectVariables.SL_SERV_NAME).getStringValue());
		LogEntity logEntity = (LogEntity)mySession.getVariableField(IProjectVariables.IVR__LOG__FOUNDATION).getObjectValue();
		logEntity.setTO_AGENT_TYPE(mySession.getVariableField(IProjectVariables.SL_SERV_NAME).getStringValue());
		logEntity.setTO_AGENT_IF("1");
		mySession.getVariableField(IProjectVariables.IVR__LOG__FOUNDATION).setValue(logEntity);
	}
	
    private String getTranDnis(){
    	
    	return "90001";
    	
    }
    
    /**
	 * upNode方法主要是  nodeRecordList （所有节点集合）最后一位转人工状态
	 * @ClassName: ser_setNext 
	 * @Description: seven
	 * @author 判断nodeRecordList集合节点是否为空，不为空是修改最后节点转人工状态
	 * @date 2022年2月12日 下午2:23
	 *  
	 */
	public void upNode(com.avaya.sce.runtimecommon.SCESession mySession){
		try {
			ArrayList<IVR_NodeRecord> ivrNRL = (ArrayList<IVR_NodeRecord>)mySession.getVariableField(IProjectVariables.NODE_RECORD_LIST).getObjectValue();
			if(ivrNRL.size()>0){
				ivrNRL.get(ivrNRL.size()-1).setTransferYorN(1);
				String transDnis = mySession.getVariableField(IProjectVariables.TRANS_DNIS).getStringValue();
				ivrNRL.get(ivrNRL.size()-1).setTransferNumber(transDnis);
			}
			mySession.getVariableField(IProjectVariables.NODE_RECORD_LIST).setValue(ivrNRL);
		} catch (Exception e) {
			log.debug("-----ser_transfer的upNode方法出现错误， 错误内容是："+e.getMessage()+"-----");
			e.printStackTrace();
		}
		
	}
	
	public void ivrLogFoundatin(com.avaya.sce.runtimecommon.SCESession mySession){
		LogEntity logEntity = (LogEntity)mySession.getVariableField(IProjectVariables.IVR__LOG__FOUNDATION).getObjectValue();
		logEntity.setTO_AGENT_IF("1");
		logEntity.setTO_AGENT_TYPE("0");
		logEntity.setTO_AGENT_IF_SUCCESS("0");
		logEntity.setFAIL_IF_DEAL("0");
		mySession.getVariableField(IProjectVariables.IVR__LOG__FOUNDATION).setValue(logEntity);
		
	}
	/**
	 * 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分30秒
	 */
	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("setVDUdata", 0, true, "Default");
		list.add(aGoto);

		return list;
	}
}
