package com.chinacreator.videoalliance.order.dao; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Component; import com.chinacreator.common.util.RequestUtil; import com.frameworkset.common.poolman.SQLExecutor; /** * 根据IP匹配渠道标识,用于直接调order.do的请求,渠道标识加统一前缀 * @author xu.zhou * @date */ @Component public class OrderchannelIpmatchDao { /** * 用IP的前三个段位到数据库进行模糊匹配,把能匹配上的IP再进行第四个段位的匹配 * @param channel * @param cpid * @param spid * @param request * @return */ public String getChannel(String channel, String cpid, String spid, HttpServletRequest request){ if(!"bilibl".equals(cpid) || !"979".equals(spid)) { return channel; } if(StringUtils.isEmpty(channel)) channel = ""; if(channel.startsWith("WP_")){//以WP_开头的,是自己的订购页面过来的 return channel; } String orderchannel = ""; try { String ip = RequestUtil.getIpAddr(request); if(StringUtils.isEmpty(ip)) { return channel; } //去掉最后一个IP段 String qryip = ip.replace(ip.split("\\.")[3],""); String lastip = ip.split("\\.")[3]; List dataList = this.getChannelByIp(qryip); if(dataList != null && dataList.size()>0){ String tmpip = ""; for(HashMap tmpMap : dataList){ tmpip = tmpMap.get("IP").toString(); if(ip.equals(tmpip)){ orderchannel = (String)tmpMap.get("CHANNEL"); break; }else if(tmpip.contains("~")){ String startip = tmpip.split("\\.")[3].split("~")[0]; String endip = tmpip.split("\\.")[3].split("~")[1]; if(Integer.parseInt(lastip) >= Integer.parseInt(startip) && Integer.parseInt(lastip) <= Integer.parseInt(endip)){ orderchannel = (String)tmpMap.get("CHANNEL"); break; } } } } //有渠道匹配, if(!"".equals(orderchannel)){ orderchannel = orderchannel+"_"+channel; if(orderchannel.getBytes().length > 20){ //只包含数字英文和下划线 if(orderchannel.matches("[0-9A-Za-z_]*")){ orderchannel = orderchannel.substring(0,20); }else{//可能包含中文 ,一个中文占两个字节 orderchannel = orderchannel.substring(0,10); } } }else{//无渠道匹配,默认加UK_ orderchannel = "UK_"+channel; } } catch (Exception e) { orderchannel = channel; e.printStackTrace(); System.out.println("IP匹配渠道出现异常,"+e.getMessage()+",channel=>"+channel); } return orderchannel; } public List getChannelByIp(String ip) throws SQLException{ String sql = "SELECT CHANNEL,IP FROM TB_CHANNEL_IPMATCH_CONF WHERE STATUS = '0' AND IP LIKE ? "; return SQLExecutor.queryListWithDBName(HashMap.class, "net3g", sql, ip+"%"); } public static void main(String[] args) throws Exception { OrderchannelIpmatchDao dao = new OrderchannelIpmatchDao(); System.out.println("AAAAABBBBBAAAA你ABBBB".matches("[0-9A-Za-z_]*")); String ip = "43.250.146.10"; //System.out.println(dao.getChannel(channel, cpid, spid, request)); //System.out.println(dao.getChannelByIp(ip)); //System.out.println(ip.split("\\.")[3].split("~")[0]); //System.out.println("你好".getBytes().length); //System.out.println("你好".length()); } }