package flow;

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

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

	/**
	 * Default constructor
	 * Last generated by Orchestration Designer at: 2021年7月20日 下午06时06分37秒
	 */
	public StrategySMS() {
		//{{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月20日 下午06时06分37秒
	 */
	public void servletImplementation(com.avaya.sce.runtimecommon.SCESession mySession) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		String sj = df.format(new Date());
		log.info("StrategySMS---发送短信接口");
		System.out.println("StrategySMS---发送短信接口");
		String phonedata = mySession.getVariableField(IProjectVariables.SESSION,IProjectVariables.SESSION_FIELD_ANI).getStringValue();
		log.info("StrategySMS---发送短信接口号码："+phonedata);
		System.out.println("StrategySMS---发送短信接口号码："+phonedata);
		String dxbm = mySession.getVariableField(IProjectVariables.DXBM).getStringValue();
		//String dhtype = mySession.getVariableField(IProjectVariables.DHTYPE).getStringValue();
		//String quhao = mySession.getVariableField(IProjectVariables.QUHAO).getStringValue();
		if(phonedata.length()==11&&!dxbm.equals("0")){
			try {
				JSONObject jsonObject1 = new JSONObject();
				jsonObject1.put("phone", phonedata);
				jsonObject1.put("smsCode", dxbm);
				
				String ip = mySession.getVariableField(IProjectVariables.HTTP,IProjectVariables.HTTP_FIELD_IP).getStringValue();
				String method = mySession.getVariableField(IProjectVariables.HTTP,IProjectVariables.HTTP_FIELD_URL_3).getStringValue();
				String post = HttpClientUtil.doPost(ip+method, jsonObject1, "utf-8");// (jsonObject1,
				//String post = cf.send( url+"/innerapi/IVR/SendIVRSms.ashx",jsonObject1);
				log.info("StrategySMS---接口返回值是： "+post);
				JSONObject jo = new JSONObject(post);
				String status = (String) jo.get("status");
				String message = (String) jo.get("message");
				String ani = mySession.getVariableField(IProjectVariables.SESSION,IProjectVariables.SESSION_FIELD_ANI).getStringValue();
				String dnis = mySession.getVariableField(IProjectVariables.SESSION,IProjectVariables.SESSION_FIELD_DNIS).getStringValue();
				String ucid = mySession.getVariableField(IProjectVariables.SESSION,IProjectVariables.SESSION_FIELD_UCID).getStringValue();
				Interfacee ifee = new Interfacee();
				String js = df.format(new Date());
				ifee.setTRADE_CODE("SMSTemplate");
				try {
					String[] split = sj.split("\\ ");
					ifee.setID(ucid+split[1]);
				} catch (Exception e) {
					log.info(e.getMessage());
				}
				ifee.setARG2(dxbm);
				ifee.setEND_TIME(js);
				ifee.setANI(ani);
				ifee.setDNIS(dnis);
				ifee.setCALLID(ucid);
				ifee.setBEGIN_TIME(sj);
				ifee.setARG1(phonedata);
				ifee.setRESPONSE_CODE(status);				
				long between = 0;
				try {            
					 java.util.Date begin = df.parse(ifee.getBEGIN_TIME());           
				     java.util.Date end = df.parse(ifee.getEND_TIME());           
				     between = (end.getTime() - begin.getTime());// 得到两者的毫秒数        
				     System.out.println(between);
				 } catch (Exception ex) {           
					 log.info(ex.getMessage());       
					 }
				String s = between+"";

				int i = Integer.parseInt(s);
				ifee.setSPEND_TIME(i);
				InterfaceSql isql =new InterfaceSql();
				isql.inset(ifee,mySession);
					log.info("StrategySMS---发送短信接口-手机号："+phonedata+"-----发送编码是："+dxbm+"--返回："+status);
					System.out.println("StrategySMS---发送短信接口-手机号："+phonedata+"-----发送编码是："+dxbm+"--返回："+status);
				
				} catch (Exception e) {
					log.info("StrategySMS---发送短信接口号码："+phonedata+"-----发送编码是："+dxbm+"-------------出现错误："+e.getMessage());
					System.out.println("StrategySMS---发送短信接口号码："+phonedata+"-----发送编码是："+dxbm+"-------------出现错误："+e.getMessage());
				}
		}
		
			log.info("StrategySMS---发送短信接口结束时间：----------"+df.format(new Date()));
			
	}
	/**
	 * 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_StrategyNext", 0, true, "Default");
		list.add(aGoto);

		return list;
	}
}
