package flow;

import java.util.ArrayList;
import java.util.HashMap;

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日 下午06时45分29秒
 */
public class ser_sendSMS extends com.avaya.sce.runtime.BasicServlet {

	//{{START:CLASS:FIELDS
	//}}END:CLASS:FIELDS

	/**
	 * Default constructor
	 * Last generated by Orchestration Designer at: 2021年7月30日 下午06时45分29秒
	 */
	public ser_sendSMS() {
		//{{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日 下午06时45分29秒
	 */
	public void servletImplementation(com.avaya.sce.runtimecommon.SCESession mySession) {
		//IVR_MenuConfig_heytai,IVR_MenuItemConfig_heytai
		System.out.println("进入通用短信发送流程");
		
		mySession.getVariableField(IProjectVariables.CURRENT_MENU_NAME).getStringValue();
		String SendSMSResult=mySession.getVariableField(IProjectVariables.CURRENT_MENU_NAME)
				.getStringValue();
		String NextNodeID = mySession.getVariableField(
				IProjectVariables.NEXT_MENU).getStringValue();
		HashMap<Integer, Object> hMapAll = (HashMap<Integer, Object>) mySession
				.getVariableField(IProjectVariables.MENU_ALL).getObjectValue();
		HashMap<Integer, ArrayList<CMenuItemConfig>> hMapChild = (HashMap<Integer, ArrayList<CMenuItemConfig>>) mySession
				.getVariableField(IProjectVariables.MENU_CHILD)
				.getObjectValue();
		HashMap<Integer, CMenuConfig> hMapParent = (HashMap<Integer, CMenuConfig>) mySession
				.getVariableField(IProjectVariables.MENU_PARENT)
				.getObjectValue();
		String ANI= mySession.getVariableField(IProjectVariables.SESSION,IProjectVariables.SESSION_FIELD_ANI).getStringValue();
		String SendPhone=ANI;
		String OtherPhone=mySession.getVariableField(IProjectVariables.OTHER_PHONE)
				.getStringValue();
		if (!OtherPhone.equals("")) {
			SendPhone=OtherPhone;
		}
		
		mySession.getVariableField(IProjectVariables.SER_RESULT).setValue(SendSMSResult);
		System.out.println("短信SendSMSResult:"+SendSMSResult);
		String SMSTemplate=mySession.getVariableField(IProjectVariables.SMSTEMPLATE_ID)
		.getStringValue();
		if(!SMSTemplate.equals("")||!SMSTemplate.isEmpty()){
			System.out.println("最终短信调用是以菜单传输的SSM编码呼叫的");
		}else {
			System.out.println("最终短信调用是以SSM配置的编码呼叫的传输的SSM编码呼叫的");
			if (hMapChild.containsKey(NextNodeID)) {
				mySession.getVariableField(IProjectVariables.MENU_CHILD)
						.getObjectValue();
				ArrayList<CMenuItemConfig> list = hMapChild.get(NextNodeID);
				for (CMenuItemConfig cMenuItemConfig : list) {
					
						String NextID = cMenuItemConfig.getNextMenu();
						String ItemMenuName = cMenuItemConfig.getItemMenuName();
						if (ItemMenuName!="") {
							mySession.getVariableField(IProjectVariables.CURRENT_MENU_NAME)
							.setValue(ItemMenuName);
						}
						mySession.getVariableField(IProjectVariables.SER_RESULT).setValue(cMenuItemConfig.getRemark());
						System.out.println("ser_getMenuResult,设置Next_Menu="
								+ cMenuItemConfig);
						String smstemplate=cMenuItemConfig.getSMSTemplate();
						System.out.println(smstemplate);
						SMSTemplate=smstemplate;
						if (smstemplate!=null&&!smstemplate.equals("")) {
							mySession.getVariableField(IProjectVariables.SMSTEMPLATE_ID)
							.setValue(smstemplate);
							System.out.println("ser_getMenuResult,设置SMSTemplate="
									+ smstemplate);
						}
						
						break;
					
				}
			}
		}
		
		try {
			//给其他手机号码短信编码复制
			mySession.getVariableField(IProjectVariables.OTHER_PHONE_SM).setValue(SMSTemplate);
			//短信接口
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("phone", SendPhone);
			jsonObject1.put("smsCode", SMSTemplate);
			String ip = mySession.getVariableField(IProjectVariables.HTTP,IProjectVariables.HTTP_FIELD_IP).getStringValue();
			String url = mySession.getVariableField(IProjectVariables.HTTP,IProjectVariables.HTTP_FIELD_URL_3).getStringValue();
			url=ip+url;
			String post = HttpClientUtil.doPost(url, jsonObject1, "utf-8");// (jsonObject1,
			System.out.println("短信返回值:"+post);
		}catch (Exception e) {
			System.out.println("短信接口出现问题:" + e.getMessage());
		}

		
		mySession.getVariableField(IProjectVariables.OTHER_PHONE).setValue(ANI);
		System.out.println("短信接收号码为："+SendPhone+" ;短信模板，SMSTEMPLATE_ID:"+SMSTemplate);
		System.out.println("短信发送完毕");
		
		//mySession.getVariableField(IProjectVariables.NEXT_MENU).getStringValue();
		//mySession.getVariableField(IProjectVariables.SER_RESULT).();
		mySession.getVariableField(IProjectVariables.SMSTEMPLATE_ID).setValue("");
	}
	/**
	 * 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("ser_check", 0, true, "Default");
		list.add(aGoto);

		return list;
	}
}
