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_checkBlack 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_checkBlack() {
		// {{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_checkBlack");
		String statrVip ="普通用户" ;
		getblack(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);
			String msViptype = mySession.getVariableField(IProjectVariables.VIPTYPE)
					.getStringValue();
			//msViptype="FIND用户";
			for (CMenuItemConfig cMenuItemConfig : list) {
//				System.out.println(cMenuItemConfig);
				String remark = cMenuItemConfig.getRemark();
				if(remark.equals(msViptype)){
					if(!cMenuItemConfig.getTransferNO().isEmpty()&&!cMenuItemConfig.getTransferNO().isEmpty()){
						System.out.println(cMenuItemConfig.getTransferNO());
						mySession.getVariableField(IProjectVariables.TRANS_DNIS).setValue(cMenuItemConfig.getTransferNO());
						mySession.getVariableField(IProjectVariables.SL_SERV_NAME).setValue(cMenuItemConfig.getRemark());
						mySession.getVariableField(IProjectVariables.SL_CALL_IN_TYPE).setValue("1020");
						System.out.println(mySession.getVariableField(IProjectVariables.TRANS_DNIS).getStringValue());
					}
					vipType=true;
					break;
				}	
			}
		} catch (Exception e) {
			System.out.println("进入ser_checkBlack   获取黑名单分配remark错误： "+e.getMessage());
		}
		if(vipType==true){
			System.out.println("进入ser_checkBlack   获取黑名单分配为正常分配");
			String viptypes = mySession.getVariableField(IProjectVariables.VIPTYPE)
					.getStringValue();
			System.out.println("ser_checkBlack:VIPTYPE:" + viptypes);
			mySession.getVariableField(IProjectVariables.SER_RESULT).setValue(viptypes);	
		}else {
			System.out.println("进入ser_checkBlack   获取黑名单分配为异常分配");
			System.out.println("ser_checkBlack:VIPTYPE:" + statrVip);
			mySession.getVariableField(IProjectVariables.SER_RESULT).setValue(statrVip);	
		}
		

	}
	/**
	 * 利用getblack进行黑名单,普通用户区分
	 * @ClassName: ser_checkBlack 
	 * @Description: seven
	 * @author: 进行黑名单,普通用户
	 * @date 2022年6月13日 下午5:07
	 *  
	 */
	public void getblack(com.avaya.sce.runtimecommon.SCESession mySession){
		try {
			String ani = mySession.getVariableField(IProjectVariables.SESSION,IProjectVariables.SESSION_FIELD_ANI).getStringValue();
			Connection conn = null;
			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_checkBlack当前数据库为测试库-----");
					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_checkBlack当前数据库为正式库-----");
					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_checkBlack数据库基础参数   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_checkBlack数据库基础参数对应表名称   目录表名称:"+catalogue+"   ,细节节点表名称:"+details);
				DriverManager.setLoginTimeout(3);
				conn = DriverManager.getConnection(url, user, password);
				System.out.println("进入ser_checkBlack链接成功");
				/*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);
				}
				
				SimpleDateFormat formatter7 = new SimpleDateFormat ("yyyy-MM-dd");
				String date7 = formatter7.format(new Date());
				String sql = "SELECT DH_BLACKLIST.PHONE_NUMBER, DH_BLACKLIST.START_TIME, DH_BLACKLIST.END_TIME "+
							 "FROM CALLCENTER.DH_BLACKLIST "+
							 "WHERE  ( ( ( DH_BLACKLIST.PHONE_NUMBER = '"+ ani11+"' )  OR ( DH_BLACKLIST.PHONE_NUMBER = '"+ ani+"'  )  )  AND ( ( DH_BLACKLIST.START_TIME = '"+date7+"' )  AND ( DH_BLACKLIST.END_TIME = '"+date7+"' )  )  )";
				
				System.out.println("-----进入ser_checkBlack数据库基础参数   对应SQL语句:"+sql);
				PreparedStatement pstmt = conn.prepareStatement(sql);
				
				ResultSet rs = pstmt.executeQuery();
				
				if(rs.next()==false){    //查不到数据 sql执行false
					mySession.getVariableField(IProjectVariables.VIPTYPE).setValue("普通用户");
					System.out.println("进入ser_checkBlack  当前用户为普通用户");
					
	            }else  {
	            	mySession.getVariableField(IProjectVariables.VIPTYPE).setValue("黑名单用户");
					System.out.println("进入ser_checkBlack  当前用户为黑名单用户");
	            }
	         
				System.out.println("进入ser_checkBlack  查询结束");
			} 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;

				}

			}
		} catch (Exception e) {
			System.out.println("ser_checkBlack出现错误："+e.getMessage());
			e.printStackTrace();
		}
		
	}

	/**
	 * 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;
	}
}
