package flow;

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

/**
 * 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_checkLOG extends com.avaya.sce.runtime.BasicServlet {

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

	/**
	 * Default constructor Last generated by Orchestration Designer at:
	 * 2021年7月30日 下午04时30分47秒
	 */
	public ser_checkLOG() {
		// {{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_checkLOG");
		String statrVip ="普通用户" ;
		//setLog(mySession);
		boolean vipType =false;
		try {
			String nextIDname = mySession.getVariableField(IProjectVariables.NEXT_IDNAME).getStringValue();
			HashMap<Integer, CMenuConfig> hMapParent = (HashMap<Integer, CMenuConfig>) mySession.getVariableField(IProjectVariables.MENU_PARENT).getObjectValue();
			HashMap<String, ArrayList<CMenuItemConfig>> hMapChild = (HashMap<String, ArrayList<CMenuItemConfig>>) mySession
					.getVariableField(IProjectVariables.MENU_CHILD)
					.getObjectValue();
			ArrayList<CMenuItemConfig> list = hMapChild.get(nextIDname);
			ivrLogFoundatin(mySession);
			String area = setLog(mySession);
			
			String msViptype = "日志记录";
			//msViptype="FIND用户";
			for (CMenuItemConfig cMenuItemConfig : list) {
//				System.out.println(cMenuItemConfig);
				String remark = cMenuItemConfig.getRemark();
				if(remark.equals(msViptype)){
					vipType=true;
					break;
				}	
			}
		} catch (Exception e) {
			System.out.println("进入ser_checkLOG   获取来电地区查询分配remark错误： "+e.getMessage());
		}
		mySession.getVariableField(IProjectVariables.SER_RESULT).setValue("日志记录");
		

	}

	/**
	 * 利用getArea进行VIP用户,普通用户区分
	 * @ClassName: ser_checkLOG 
	 * @Description: seven
	 * @author: 进行来电地区查询并修改数据记录
	 * @date 2022年6月13日 下午5:07
	 *  
	 */
	public String setLog(com.avaya.sce.runtimecommon.SCESession mySession){
		String ani = mySession.getVariableField(IProjectVariables.SESSION,IProjectVariables.SESSION_FIELD_ANI).getStringValue();
		Connection conn = null;
		String aniType="0";
		try {
			String type = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_TYPE).getStringValue();
			String driver = null;
			String user = null;
			String password = null;
			String url = null;
			// 若安装多个实例时，要确认相应的port及实例名
			if(type.equals("0")){
				System.out.println("-----进入ser_checkLOG当前数据库为测试库-----");
				driver = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_DRIVER_2).getStringValue();
				Class.forName(driver);
				user = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_USER_2).getStringValue();
				password = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_PASSWORD_2).getStringValue();
				url = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_URL_2).getStringValue();
			}else {
				System.out.println("-----进入ser_checkLOG当前数据库为正式库-----");
				driver = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_DRIVER_1).getStringValue();
				Class.forName(driver);
				user = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_USER_1).getStringValue();
				password = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_PASSWORD_1).getStringValue();
				url = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_URL_1).getStringValue();
			}
			System.out.println("-----进入ser_checkLOG数据库基础参数   URL:"+url+"   ,USER:"+user+"   ,password:"+password);
			//String DBType = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_TYPE).getStringValue();
			//获取目录表名称
			String catalogue = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_TABLE_1).getStringValue();
			//获取细节节点表名称
			String details = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_TABLE_2).getStringValue();
			System.out.println("-----进入ser_checkLOG数据库基础参数对应表名称   目录表名称:"+catalogue+"   ,细节节点表名称:"+details);
			DriverManager.setLoginTimeout(3);
			conn = DriverManager.getConnection(url, user, password);
			System.out.println("进入ser_checkLOG链接成功");
			/*String sql = "SELECT a.[MenuID] parentMenuID,a.[MenuName] parentMenuName,a.[MenuType] parentMenuType,a.[NextMenu] parentNextMenu,a.[Remark] parentRemark,a.BEnable parentBEnable,a.MenuErrorMaxCount,a.Strategy,a.NodeType,b.[RowID],b.[ParentMenuID],b.[MenuItemID],b.[PlayType],b.[PlayItem],b.[BEnable],b.[MenuOrder],B.[TransferNO] ,b.[NextMenu],b.[DTMF] ,b.[Remark],b.[SMSTemplate],b.ItemMenuName,b.SonNodeType FROM "
			+catalogue+" a left join "+details+" b on a.MenuID=b.ParentMenuID order by b.ParentMenuID,MenuOrder";*/
			String ani11="";
			if(ani.length()>=11){
				ani11 = ani.substring(0, 11);
			}
			String sql="1";
			LogEntity logEntity = (LogEntity)mySession.getVariableField(IProjectVariables.IVR__LOG__FOUNDATION).getObjectValue();
			sql="INSERT INTO CALLCENTER.IVR_LOG_FOUNDATION (LOG_ID,CALL_ID,CLNT_ID,CLNT_TYPE,CLNT_BELG_ORG,CLNT_BELG_CHNEL,CLNT_NAME,CLNT_SEX,CLNT_AREA,CLNT_LEVEL,CLNT_FEELING,CLNT_IDNUM,CLNT_INFO_FLAG,ANI,DNIS,CALL_IN_TYPE,AREA_GROUP_NO,AREA_NO,AREA_CITY_NAME,AREA_PROV_NAME,AREA_PROV_NO,START_DATE,START_TIME,END_DATE,END_TIME,TO_AGENT_IF,TO_AGENT_TYPE,TO_AGENT_IF_SUCCESS,LEAVEWORD_IF,FAIL_IF_DEAL,AGENT_ID,TRACE,CHANNEL,IVR_NODE,DEAL_DESC,DEAL_TIME,DEAL_WITH_PERSON,START_TIME_STR,END_TIME_STR) values "+
					"('"+logEntity.getLOG_ID()+"','"+logEntity.getCALL_ID()+"','"+logEntity.getCLNT_ID()+"','"+logEntity.getCLNT_TYPE()+"','"+logEntity.getCLNT_BELG_ORG()+"','"+logEntity.getCLNT_BELG_CHNEL()+"','"+logEntity.getCLNT_NAME()+"','"+logEntity.getCLNT_SEX()+"','"+logEntity.getCLNT_AREA()+"','"+logEntity.getCLNT_LEVEL()+"','"+logEntity.getCLNT_FEELING()+"','"+logEntity.getCLNT_IDNUM()+"','"+logEntity.getCLNT_INFO_FLAG()+"','"+logEntity.getANI()+"','"+logEntity.getDNIS()+"','"+logEntity.getCALL_IN_TYPE()+"','"+logEntity.getAREA_GROUP_NO()+"','"+logEntity.getAREA_NO()+"','"+logEntity.getAREA_CITY_NAME()+"','"+logEntity.getAREA_PROV_NAME()+"','"+logEntity.getAREA_PROV_NO()+"',"+logEntity.getSTART_DATE()+","+logEntity.getSTART_TIME()+","+logEntity.getEND_DATE()+","+logEntity.getEND_TIME()+",'"+logEntity.getTO_AGENT_IF()+"','"+logEntity.getTO_AGENT_TYPE()+"','"+logEntity.getTO_AGENT_IF_SUCCESS()+"','"+logEntity.getLEAVEWORD_IF()+"','"+logEntity.getFAIL_IF_DEAL()+"','"+logEntity.getAGENT_ID()+"','"+logEntity.getTRACE()+"','"+logEntity.getCHANNEL()+"','"+logEntity.getIVR_NODE()+"','"+logEntity.getDEAL_DESC()+"','"+logEntity.getDEAL_TIME()+"','"+logEntity.getDEAL_WITH_PERSON()+"','"+logEntity.getSTART_TIME_STR()+"','"+logEntity.getEND_TIME_STR()+"')";
			SimpleDateFormat formatter7 = new SimpleDateFormat ("yyyy-MM-dd");
			String date7 = formatter7.format(new Date());
			System.out.println("-----进入ser_checkLOG数据库基础参数   对应SQL语句:"+sql);
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.execute();
			
			System.out.println("进入ser_checkLOG  插入成功");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				conn = null;

			}

		}
		return aniType;
	}
	
	
	/**
	 * ivrLogFoundatin方法主要是 初始化日志记录 
	 * @ClassName: ser_init
	 * @Description: seven
	 * @author LogEntity 为类对象; 存入系统变量mySession中 LogEntity变量
	 * @date 2022年6月13日 下午4:14
	 *  
	 */
	public void ivrLogFoundatin(com.avaya.sce.runtimecommon.SCESession mySession){
		LogEntity logEntity = (LogEntity)mySession.getVariableField(IProjectVariables.IVR__LOG__FOUNDATION).getObjectValue();
		Date currentDateTime2 = new Date();
		SimpleDateFormat formatter1 = new SimpleDateFormat ("yyyyMMdd");
		logEntity.setEND_DATE(formatter1.format(currentDateTime2));
		SimpleDateFormat formatter2 = new SimpleDateFormat ("HHmmssS");
		logEntity.setEND_TIME(formatter2.format(currentDateTime2));
		SimpleDateFormat formatter3 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
		logEntity.setEND_TIME_STR(formatter3.format(currentDateTime2));
		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分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;
	}
}
