Authored by zhengwen.ge

广点通需求

package com.yoho.unions.common.enums;
import org.apache.commons.lang3.StringUtils;
/**
* 广点通返回信息枚举
* @author yoho
*
*/
public enum MsgEnum {
SUCCESS("0","成功"),
ILLEGAL_PARAMETER("-1","请求非法参数"),
PARSE_ARGUMENTS_FAIL("-2","参数解析失败"),
PARSE_DECODE_FAIL("-3","参数解码失败"),
GET_KEY_FAIL("-12","获取密钥失败"),
ILLEGAL_APPTYPE("-13","非法的应用类型"),
ILLEGAL_CONVTIME("-14","非法的转化时间"),
ILLEGAL_MUID("-15","非法的广点通移动设备标识"),
CONV_RULE_FAIL("-17","获取转化规则失败");
private String code;
private String name;
public String getCode() {
return code;
}
public String getName() {
return name;
}
private MsgEnum(String code,String name){
this.code = code;
this.name = name;
}
/**
* 根据code获取name
*/
public static String getNameByCode(String code){
if (StringUtils.isEmpty(code)) {
return null;
}
for(MsgEnum e:values()){
if (code.equals(e.getCode())) {
return e.getName();
}
}
return null;
}
}
... ...
package com.yoho.unions.server.service.impl;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.netflix.config.DynamicIntProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.yoho.core.cache.CacheClient;
import com.yoho.service.model.union.request.ActiveUnionRequestBO;
import com.yoho.service.model.union.request.AddUnionRequestBO;
import com.yoho.service.model.union.response.UnionResponseBO;
import com.yoho.unions.common.enums.SourceEnum;
import com.yoho.unions.common.utils.DateUtil;
import com.yoho.unions.common.utils.HttpUtils;
import com.yoho.unions.dal.IUnionsActiveRecordDAO;
import com.yoho.unions.dal.model.UnionsActiveRecord;
import com.yoho.unions.server.service.DingdangService;
@Service
public class DingdangServiceImpl_OLD implements DingdangService {
static Logger log = LoggerFactory.getLogger(DingdangServiceImpl_OLD.class);
static Logger addDingdang = LoggerFactory.getLogger("addDingdang");
static Logger activeDingdang = LoggerFactory.getLogger("activeDingdang");
@Resource
CacheClient cacheClient;
@Resource
IUnionsActiveRecordDAO unionsActiveRecordDAO;
private static final String unions_KEY = "yh:unions:dingdang_";
@Override
public UnionResponseBO addUnion(AddUnionRequestBO request) throws Exception {
log.debug("addunions with param is {}", request);
UnionResponseBO bo = new UnionResponseBO();
if (StringUtils.isEmpty(request.getApp())) {
log.warn("addunions error app is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("APP IS NULL");
return bo;
// throw new ServiceException(ServiceError.APP_IS_NULL);
}
if (StringUtils.isEmpty(request.getUdid())) {
log.warn("addunions error udid is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("UDID IS NULL");
return bo;
// throw new ServiceException(ServiceError.UDID_IS_NULL);
}
if (StringUtils.isEmpty(request.getCallbackurl())) {
log.warn("addunions error callbackurl is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("CALLBACKURL IS NULL");
return bo;
// throw new ServiceException(ServiceError.CALLBACKURL_IS_NULL);
}
//检查memcached中是否已经有该udid
String cacheUdid = cacheClient.get(unions_KEY + request.getApp() + "_" + request.getUdid(), String.class);
log.info("addunions get cache key={}, cacheUdid={}", unions_KEY + request.getApp() + "_" + request.getUdid(), cacheUdid);
if (StringUtils.isNotEmpty(cacheUdid)) {
log.warn("addunions error app and udid is added with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("APP UDID IS EXISTS");
return bo;
// throw new ServiceException(ServiceError.APP_UDID_IS_EXISTS);
}
//保存到memcached,时间,一个小时
DynamicIntProperty activeTime = DynamicPropertyFactory.getInstance().getIntProperty("activeTime", 60 * 60);
cacheClient.set(unions_KEY + request.getApp() + "_" + request.getUdid(), activeTime.get(), request.getUdid());
log.debug("addunions set cache success");
//插入数据库
try {
UnionsActiveRecord unions = new UnionsActiveRecord();
unions.setApp(request.getApp());
unions.setUdid(request.getUdid());
unions.setCallbackurl(request.getCallbackurl());
unions.setType(request.getType());
log.debug("add to unionsLog db with param is {}", unions);
unionsActiveRecordDAO.insert(unions);
} catch (Exception e) {
log.error("", e);
}
log.info("addunions success with param is {}", request);
addDingdang.info("addunions success with param is {}", request);
bo.setMsg("成功");
bo.setIsSuccess(true);
return bo;
}
@Override
public UnionResponseBO activeUnion(ActiveUnionRequestBO request) throws Exception {
log.debug("activeunions with param is {}", request);
UnionResponseBO bo = new UnionResponseBO();
if (StringUtils.isEmpty(request.getAppid())) {
log.warn("activeunions error app is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("APP IS NULL");
return bo;
//throw new ServiceException(500, "APP_IS_NULL");
}
if (StringUtils.isEmpty(request.getUdid())) {
log.warn("activeunions error udid is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("UDID IS NULL");
return bo;
// throw new ServiceException(500, "UDID_IS_NULL");
}
String memKey = unions_KEY + request.getAppid() + "_" + request.getUdid();
//检查memcached中是否已经有该udid
String cacheUdid = cacheClient.get(memKey, String.class);
log.info("activeUnion get cache key={}, cacheUdid={}", memKey, cacheUdid);
if (StringUtils.isEmpty(cacheUdid)) {
log.warn("activeunions error app and udid is not exists with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("APP UDID IS NOT EXISTS IN CACHE");
return bo;
}
//检查该app和udid是否已经激活
UnionsActiveRecord unions = new UnionsActiveRecord();
unions.setApp(request.getAppid());
unions.setUdid(request.getUdid());
unions.setType(request.getApptype());
UnionsActiveRecord u = unionsActiveRecordDAO.selectByUdidAndApp(unions);
if (u == null) {
log.warn("activeunions error app and udid is not exists with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("database has not exists this udid but cache exists");
return bo;
}
//已经激活
if ("1".equals(u.getIsActive())) {
log.warn("activeunions error app is actived with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("app udid had actived");
return bo;
}
//调用联盟激活接口
int repeatCount = 0;
Pair<Integer, String> pair = null;
JSONObject json = null;
boolean isSuccess = false;
String url = "";
while (true) {
//如果已经成功,或者重试次数大于3次,则不再调用
if (isSuccess || repeatCount >= 3) {
break;
}
try {
url = u.getCallbackurl();
pair = HttpUtils.httpGet(url);
if (pair.getLeft() == HttpStatus.SC_OK) {
log.debug("call " + u.getCallbackurl() + " url success return message is {}", pair.getRight());
json = JSONObject.parseObject(pair.getRight());
isSuccess = json.getBooleanValue("isSuccess");
}
} catch (Exception e) {
log.error("", e);
}
repeatCount++;
}
log.info("call " + u.getCallbackurl() + " url return message is {}", pair.getRight());
//更新数据库
if (isSuccess) {
unions.setIsActive("1");
unions.setId(u.getId());
unionsActiveRecordDAO.updateByPrimaryKey(unions);
//激活成功,从memcached中删除
cacheClient.delete(memKey);
log.debug("activeUnion delete cache success key={}", memKey);
bo.setMsg("激活成功");
bo.setIsSuccess(true);
} else {
unions.setIsActive("0");
unions.setId(u.getId());
unionsActiveRecordDAO.updateByPrimaryKey(unions);
bo.setMsg("激活失败");
bo.setIsSuccess(false);
}
try {
//组装大数据分析的日志
JSONObject j = new JSONObject();
j.put("apptype", request.getApptype());
j.put("appid", request.getAppid());
j.put("udid", request.getUdid());
j.put("dateid", DateUtil.getcurrentDateTime());
SourceEnum e = SourceEnum.getSourceEnumByValue(u.getType());
j.put("source", e == null ? "" : e.getName());
j.put("ip", request.getClientIP());
j.put("collect_ip", "");
activeDingdang.info(j.toString());
} catch (Exception e) {
log.error("", e);
}
return bo;
}
}
... ... @@ -23,6 +23,7 @@ import com.yoho.service.model.union.request.MainUnionRequestBO;
import com.yoho.service.model.union.response.ActivateDingdangResponseBO;
import com.yoho.service.model.union.response.ActivateGDTResponseBO;
import com.yoho.service.model.union.response.MainUnionResponseBO;
import com.yoho.unions.common.enums.MsgEnum;
import com.yoho.unions.common.utils.DateUtil;
import com.yoho.unions.common.utils.HttpUtils;
import com.yoho.unions.dal.IUnionLogsDAO;
... ... @@ -103,7 +104,8 @@ public class GDTServiceImpl implements MainUnionService {
log.debug("url return message is {}",pair.getRight());
JSONObject json = JSONObject.parseObject(pair.getRight());
ret = json.getString("ret");
msg = json.getString("msg");
// msg = json.getString("msg");
msg = MsgEnum.getNameByCode(ret);
//广点通返回成功
if(StringUtils.isNotEmpty(ret)&&"0".equals(ret)){
unionLogs.setIsActivate((byte) 1);
... ...