package flow;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Date;
import java.text.SimpleDateFormat;

import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONArray;
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: 2017年4月5日 下午03时12分42秒
 */
public class SetDataToDB extends com.avaya.sce.runtime.BasicServlet {
	private static final boolean List = false;
	private static Logger log = Logger.getLogger(SetDataToDB.class);
	// {{START:CLASS:FIELDS
	// }}END:CLASS:FIELDS

	/**
	 * Default constructor Last generated by Orchestration Designer at:
	 * 2017年4月5日 下午03时12分42秒
	 */
	public SetDataToDB() {
		// {{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: 2017年4月5日 下午03时12分42秒
	 */
	public void servletImplementation(com.avaya.sce.runtimecommon.SCESession mySession) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		log.info("SetDataToDB开始时间=======" + df.format(new Date()));
		String sys_source="AVAYA";
		String option1 = mySession.getVariableField(IProjectVariables.SOLVE_MENU,IProjectVariables.SOLVE_MENU_FIELD_VALUE).getStringValue();
		String option2 = mySession.getVariableField(IProjectVariables.SATISFACTION_MENU,IProjectVariables.SATISFACTION_MENU_FIELD_VALUE).getStringValue();
		String option3 = mySession.getVariableField(IProjectVariables.ZT,IProjectVariables.ZT_FIELD_VALUE).getStringValue();
		String ucid = mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_UCID).getStringValue();
		String ani = mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_CALLERNO).getStringValue();
		String time = mySession.getVariableField(IProjectVariables.UUIGROUP, IProjectVariables.UUIGROUP_FIELD_SVCTIME).getStringValue();
		time=df.format(new Date());
		String remark=null;
		log.info("SetDataToDB   来电号码=======" + ani);
		try {
			PostHttp cf = new PostHttp();
			//String param ="sys_source="+sys_source+"&callid="+ucid+"&time="+time+"&option1="+option1+"&option2="+option2+"&option3="+option3;
			//log.info("IsVIPServlet   param=======" + param);
			
			JSONObject jsonObject = new JSONObject();
			
			jsonObject.put("sys_source", sys_source);
			
			
			
			JSONObject jsonObject1 = new JSONObject();
			
			jsonObject1.put("recguid", ucid);
			jsonObject1.put("time", df.format(new Date()));
			jsonObject1.put("option1", option1);
			jsonObject1.put("option2", option2);
			jsonObject1.put("option3", option3);
			jsonObject1.put("remark", "");
			
			JSONArray array = new JSONArray();
			JSONObject jsonObject2 = new JSONObject();
			array.put(jsonObject1);
			//array.put(jsonObject2);
			jsonObject.put("data", array);
			
			
			System.out.println(jsonObject.toString());
			
			//String post = cf.send("http://172.17.144.163:8080/EliteGeneralHttpTask/api/api_rm_statisfaction",jsonObject);// (jsonObject1,
			//String post = cf.send("http://172.17.144.165:8080/EliteGeneralHttpTask/api/api_rm_statisfaction",jsonObject);// (jsonObject1,
			//String post = cf.send("http://172.17.144.165:8080/EliteGeneralHttpTask/api/api_rm_statisfaction",jsonObject);// (jsonObject1,
			//String post = cf.send("https://callcenter.realmeservice.com/EliteGeneralHttpTask/api/api_rm_statisfaction",jsonObject);// (jsonObject1,
			String post = HttpClientUtil.doPost("https://callcenter.realmeservice.com/EliteGeneralHttpTask/api/api_rm_statisfaction", jsonObject1, "utf-8");
			String resultcode = null;
			String message = null;
			JSONObject jo = new JSONObject(post);
			
			try {

				resultcode = (String) jo.get("resultcode");
				log.info("IsVIPServlet  来电号码是:        "+ucid+"       返回值resultcode是     :"+resultcode);
				//message = (String) jo.get("message");
				

			} catch (Exception e) {
				log.info("SetDataToDB出现问题    "+e.getMessage());
				//log.debug("IsVIPServlet1   ")+e.getMessage();
			}
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
			log.info("SetDataToDB出现问题2       "+e.getMessage());
		}
		/*try {
			PostHttp cf = new PostHttp();
			String param ="sys_source="+sys_source+"&callid="+ucid+"&time="+time+"&option1="+option1+"&option2="+option2+"&option3="+option3;
			log.info("IsVIPServlet   param=======" + param);
			
			String post = cf.get("http://172.17.144.163:8080/EliteGeneralHttpTask/api/api_rm_statisfaction",param);// (jsonObject1,
			
			String code = null;
			String message = null;
			JSONObject jo = new JSONObject(post);
			
			try {

				code = (String) jo.get("code");
				log.info("IsVIPServlet  来电号码是:        "+ani+"       返回值code是     :"+code);
				message = (String) jo.get("message");
				

			} catch (Exception e) {
				log.info("IsVIPServlet1    "+e.getMessage());
				//log.debug("IsVIPServlet1   ")+e.getMessage();
			}
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
			log.info("IsVIPServlet       "+e.getMessage());
		}*/
		// TODO: Add your code here!
	}

	/**
	 * 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: 2021年12月23日 下午05时43分08秒
	 */
	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("Thanks", 0, true, "Default");
		list.add(aGoto);

		return list;
	}
}
