|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
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.ActivateDingdangRequestBO;
|
|
|
import com.yoho.service.model.union.request.AddDingdangRequestBO;
|
|
|
import com.yoho.service.model.union.request.MainUnionRequestBO;
|
|
|
import com.yoho.service.model.union.response.ActivateDingdangResponseBO;
|
|
|
import com.yoho.service.model.union.response.AddDingdangResponseBO;
|
|
|
import com.yoho.service.model.union.response.MainUnionResponseBO;
|
|
|
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.MainUnionService;
|
|
|
|
|
|
/**
|
|
|
* 描述:
|
|
|
* 顶当联盟实现
|
|
|
* @author ping.huang
|
|
|
* 2016年3月10日
|
|
|
*/
|
|
|
@Service
|
|
|
public class DingdangServiceImpl2 implements MainUnionService {
|
|
|
|
|
|
static Logger log = LoggerFactory.getLogger(DingdangServiceImpl2.class);
|
|
|
|
|
|
static Logger addDingdang = LoggerFactory.getLogger("addDingdang");
|
|
|
static Logger activeDingdang = LoggerFactory.getLogger("activeDingdang");
|
|
|
private static final String unions_KEY = "yh:unions:dingdang_";
|
|
|
|
|
|
@Resource
|
|
|
IUnionsActiveRecordDAO unionsActiveRecordDAO;
|
|
|
|
|
|
@Resource
|
|
|
CacheClient cacheClient;
|
|
|
|
|
|
@Override
|
|
|
public MainUnionResponseBO addUnionCheck(MainUnionRequestBO request) {
|
|
|
AddDingdangRequestBO requestBO = (AddDingdangRequestBO) request;
|
|
|
if (StringUtils.isEmpty(requestBO.getApp())) {
|
|
|
log.warn("addunions error app is null with param is {}", request);
|
|
|
return new AddDingdangResponseBO(false, "APP IS NULL");
|
|
|
}
|
|
|
if (StringUtils.isEmpty(requestBO.getUdid())) {
|
|
|
log.warn("addunions error udid is null with param is {}", request);
|
|
|
return new AddDingdangResponseBO(false, "UDID IS NULL");
|
|
|
}
|
|
|
if (StringUtils.isEmpty(requestBO.getCallbackurl())) {
|
|
|
log.warn("addunions error callbackurl is null with param is {}", request);
|
|
|
return new AddDingdangResponseBO(false, "CALLBACKURL IS NULL");
|
|
|
}
|
|
|
return new AddDingdangResponseBO();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public MainUnionResponseBO addUnion(MainUnionRequestBO request) {
|
|
|
AddDingdangRequestBO requestBO = (AddDingdangRequestBO) request;
|
|
|
log.debug("addunions with param is {}", request);
|
|
|
|
|
|
//检查memcached中是否已经有该udid
|
|
|
String cacheUdid = cacheClient.get(unions_KEY + requestBO.getApp() + "_" + requestBO.getUdid(), String.class);
|
|
|
log.info("addunions get cache key={}, cacheUdid={}", unions_KEY + requestBO.getApp() + "_" + requestBO.getUdid(), cacheUdid);
|
|
|
if (StringUtils.isNotEmpty(cacheUdid)) {
|
|
|
log.warn("addunions error app and udid is added with param is {}", request);
|
|
|
return new AddDingdangResponseBO(false, "APP UDID IS EXISTS");
|
|
|
}
|
|
|
|
|
|
//保存到memcached,时间,一个小时
|
|
|
DynamicIntProperty activeTime = DynamicPropertyFactory.getInstance().getIntProperty("activeTime", 60 * 60);
|
|
|
cacheClient.set(unions_KEY + requestBO.getApp() + "_" + requestBO.getUdid(), activeTime.get(), requestBO.getUdid());
|
|
|
log.debug("addunions set cache success");
|
|
|
//插入数据库
|
|
|
try {
|
|
|
UnionsActiveRecord unions = new UnionsActiveRecord();
|
|
|
unions.setApp(requestBO.getApp());
|
|
|
unions.setUdid(requestBO.getUdid());
|
|
|
unions.setCallbackurl(requestBO.getCallbackurl());
|
|
|
unions.setType(requestBO.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);
|
|
|
return new AddDingdangResponseBO(true, "成功");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public MainUnionResponseBO activateUnionCheck(MainUnionRequestBO request) {
|
|
|
ActivateDingdangRequestBO requestBO = (ActivateDingdangRequestBO) request;
|
|
|
if (StringUtils.isEmpty(requestBO.getAppid())) {
|
|
|
log.warn("activeunions error app is null with param is {}", request);
|
|
|
return new ActivateDingdangResponseBO(false, "APP IS NULL");
|
|
|
}
|
|
|
if (StringUtils.isEmpty(requestBO.getUdid())) {
|
|
|
log.warn("activeunions error udid is null with param is {}", request);
|
|
|
return new ActivateDingdangResponseBO(false, "UDID IS NULL");
|
|
|
}
|
|
|
return new ActivateDingdangResponseBO();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public MainUnionResponseBO activateUnion(MainUnionRequestBO request) {
|
|
|
ActivateDingdangRequestBO requestBO = (ActivateDingdangRequestBO) request;
|
|
|
log.debug("activeunions with param is {}", requestBO);
|
|
|
|
|
|
String memKey = unions_KEY + requestBO.getAppid() + "_" + requestBO.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);
|
|
|
return new ActivateDingdangResponseBO(false, "APP UDID IS NOT EXISTS IN CACHE");
|
|
|
}
|
|
|
|
|
|
//检查该app和udid是否已经激活
|
|
|
UnionsActiveRecord unions = new UnionsActiveRecord();
|
|
|
unions.setApp(requestBO.getAppid());
|
|
|
unions.setUdid(requestBO.getUdid());
|
|
|
unions.setType(requestBO.getApptype());
|
|
|
UnionsActiveRecord u = unionsActiveRecordDAO.selectByUdidAndApp(unions);
|
|
|
|
|
|
if (u == null) {
|
|
|
log.warn("activeunions error app and udid is not exists with param is {}", request);
|
|
|
return new ActivateDingdangResponseBO(false, "database has not exists this udid but cache exists");
|
|
|
}
|
|
|
|
|
|
//已经激活
|
|
|
if ("1".equals(u.getIsActive())) {
|
|
|
log.warn("activeunions error app is actived with param is {}", request);
|
|
|
return new ActivateDingdangResponseBO(false, "app udid had actived");
|
|
|
}
|
|
|
|
|
|
//调用联盟激活接口
|
|
|
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());
|
|
|
|
|
|
MainUnionResponseBO response = null;
|
|
|
//更新数据库
|
|
|
if (isSuccess) {
|
|
|
unions.setIsActive("1");
|
|
|
unions.setId(u.getId());
|
|
|
unionsActiveRecordDAO.updateByPrimaryKey(unions);
|
|
|
//激活成功,从memcached中删除
|
|
|
cacheClient.delete(memKey);
|
|
|
log.debug("activeUnion delete cache success key={}", memKey);
|
|
|
response = new ActivateDingdangResponseBO(true, "激活成功");
|
|
|
} else {
|
|
|
unions.setIsActive("0");
|
|
|
unions.setId(u.getId());
|
|
|
unionsActiveRecordDAO.updateByPrimaryKey(unions);
|
|
|
response = new ActivateDingdangResponseBO(false, "激活失败");
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
//组装大数据分析的日志
|
|
|
JSONObject j = new JSONObject();
|
|
|
j.put("apptype", requestBO.getApptype());
|
|
|
j.put("appid", requestBO.getAppid());
|
|
|
j.put("udid", requestBO.getUdid());
|
|
|
j.put("dateid", DateUtil.getcurrentDateTime());
|
|
|
SourceEnum e = SourceEnum.getSourceEnumByValue(u.getType());
|
|
|
j.put("source", e == null ? "" : e.getName());
|
|
|
j.put("ip", requestBO.getClientIP());
|
|
|
j.put("collect_ip", "");
|
|
|
activeDingdang.info(j.toString());
|
|
|
} catch (Exception e) {
|
|
|
log.error("", e);
|
|
|
}
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
|
|
|
} |