99c0cf98fc20d5353474dd3780f793e1d50e5923.svn-base 3.2 KB
package com.espeed.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.espeed.dao.YxyLinkInfoDao;
import com.espeed.dao.YxyReadingInfoDao;
import com.espeed.dao.YxyReplyInfoDao;
import com.espeed.dao.YxyUnsubscribeInfoDao;
import com.espeed.service.YxyMailIpService;
import com.espeed.tool.getArea;

/**
 * ip处理service实现类
 * @author 陈南巧
 */
public class YxyMailIpServiceImpl implements YxyMailIpService {
	
	private static String ipPattern = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."  
            +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
            +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
            +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; 
	
	public Map<String,String> updateIpOfArea(int id,int type,String ip)throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		if(type != 1 && type != 2 && type != 3 && type != 4){
			map.put("result", "1");
			map.put("area", "未知");
			return map;
		}
		if(ip == null || ip.equals("") || ip.split("\\.").length != 4){
			map.put("result", "1");
			map.put("area", "未知");
			return map;
		}
        Pattern pattern = Pattern.compile(ipPattern);  
        Matcher matcher = pattern.matcher(ip);
        if(!matcher.matches()){
        	map.put("result", "1");
			map.put("area", "未知");
			return map;
        }
		String area = getArea.getArea(ip);
		if(area!=null&&!area.equals("error")){
			map.put("result", "1");
			map.put("area", area);
			area=area.replace("'", "");
			if(type==1){
				String hql = "update YxyReadingInfo set yxy_reading_area = '"+area+"' where yxy_reading_id = "+id;
				yxyreadinginfodao.updateByHql(hql);
			}else if(type==2){
				String hql = "update YxyUnsubscribeInfo set area = '"+area+"' where unsubscribe_id = "+id;
				yxyunsubscribeinfodao.updateByHql(hql);
			}else if(type==3){
				String hql = "update YxyReplyInfo set area = '"+area+"' where replyid = "+id;
				yxyreplyinfodao.updateByHql(hql);
			}else if(type==4){
				String hql = "update YxyLinkInfo set area = '"+area+"' where id = "+id;
				yxylinkinfodao.updateByHql(hql);
			}
		}else{
			map.put("result", "0");
		}
		return map;
	}
	
	private YxyReadingInfoDao yxyreadinginfodao;//点读dao
	private YxyUnsubscribeInfoDao yxyunsubscribeinfodao;//退订dao
	private YxyReplyInfoDao yxyreplyinfodao;//回复dao
	private YxyLinkInfoDao yxylinkinfodao;//链接dao
	public YxyReadingInfoDao getYxyreadinginfodao() {
		return yxyreadinginfodao;
	}
	public void setYxyreadinginfodao(YxyReadingInfoDao yxyreadinginfodao) {
		this.yxyreadinginfodao = yxyreadinginfodao;
	}
	public YxyUnsubscribeInfoDao getYxyunsubscribeinfodao() {
		return yxyunsubscribeinfodao;
	}
	public void setYxyunsubscribeinfodao(YxyUnsubscribeInfoDao yxyunsubscribeinfodao) {
		this.yxyunsubscribeinfodao = yxyunsubscribeinfodao;
	}
	public YxyReplyInfoDao getYxyreplyinfodao() {
		return yxyreplyinfodao;
	}
	public void setYxyreplyinfodao(YxyReplyInfoDao yxyreplyinfodao) {
		this.yxyreplyinfodao = yxyreplyinfodao;
	}
	public YxyLinkInfoDao getYxylinkinfodao() {
		return yxylinkinfodao;
	}
	public void setYxylinkinfodao(YxyLinkInfoDao yxylinkinfodao) {
		this.yxylinkinfodao = yxylinkinfodao;
	}
}