Authored by ping

update

... ... @@ -6,8 +6,6 @@ package com.yoho.unions.server.restapi;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
... ...
... ... @@ -61,163 +61,162 @@ public class UnionServiceImpl implements IUnionService {
@Override
public UnionResponse clickUnion(ClickUnionRequestBO request) throws ServiceException {
log.info("enter clickUnion with param is {}", request);
//检查输入参数
if (StringUtils.isEmpty(request.getAppid())) {
log.warn("clickUnion error because appid is empty with param is {}", request);
return new UnionResponse(201, "appid is empty");
}
if (StringUtils.isEmpty(request.getTd())) {
log.warn("clickUnion error because td is empty with param is {}", request);
return new UnionResponse(201, "td is empty");
}
if (StringUtils.isEmpty(request.getUnion_type())) {
log.warn("clickUnion error because union_type is empty with param is {}", request);
return new UnionResponse(201, "union_type is empty");
}
if (StringUtils.isEmpty(request.getCallbackurl())) {
log.warn("clickUnion error because callbackurl is empty with param is {}", request);
return new UnionResponse(201, "callbackurl is empty");
}
//组装redis保存的key
String key = UNION_KEY + "_" + request.getClientIp() + "_" + request.getTd();
// String value = yhValueOperations.get(key);
// log.info("clickUnion get key={}, value={}", key, value);
// //如果redis中已经存在该用户点击信息,则不再重复保存
// if (StringUtils.isNotEmpty(value)) {
// log.warn("clickUnion error click info is exists with param is {}", request);
// return new UnionResponse(202, "click info is exists");
// }
// //查询该用户是否已经激活过
//
// UnionLogs union = unionLogsDAO.selectByTd(request.getTd());
// log.info("clickUnion in selectByTd result is {}", union);
// if (union != null && union.getIsActivate() != null && union.getIsActivate().byteValue() == 1) {
// //如果90天之内有过激活日志,则不允许重复激活
// log.warn("clickUnion error because 90 days has activate info with param is {}", request);
// return new UnionResponse(203, "have activite in 90 days");
// }
//保存到缓存中,有效期三个小时
try {
log.info("enter clickUnion with param is {}", request);
//检查输入参数
if (StringUtils.isEmpty(request.getAppid())) {
log.warn("clickUnion error because appid is empty with param is {}", request);
return new UnionResponse(201, "appid is empty");
}
if (StringUtils.isEmpty(request.getTd())) {
log.warn("clickUnion error because td is empty with param is {}", request);
return new UnionResponse(201, "td is empty");
}
if (StringUtils.isEmpty(request.getUnion_type())) {
log.warn("clickUnion error because union_type is empty with param is {}", request);
return new UnionResponse(201, "union_type is empty");
}
if (StringUtils.isEmpty(request.getCallbackurl())) {
log.warn("clickUnion error because callbackurl is empty with param is {}", request);
return new UnionResponse(201, "callbackurl is empty");
}
//组装redis保存的key
String key = UNION_KEY + "_" + request.getClientIp() + "_" + request.getTd();
// String value = yhValueOperations.get(key);
// log.info("clickUnion get key={}, value={}", key, value);
// //如果redis中已经存在该用户点击信息,则不再重复保存
// if (StringUtils.isNotEmpty(value)) {
// log.warn("clickUnion error click info is exists with param is {}", request);
// return new UnionResponse(202, "click info is exists");
// }
// //查询该用户是否已经激活过
//
// UnionLogs union = unionLogsDAO.selectByTd(request.getTd());
// log.info("clickUnion in selectByTd result is {}", union);
// if (union != null && union.getIsActivate() != null && union.getIsActivate().byteValue() == 1) {
// //如果90天之内有过激活日志,则不允许重复激活
// log.warn("clickUnion error because 90 days has activate info with param is {}", request);
// return new UnionResponse(203, "have activite in 90 days");
// }
//保存到缓存中,有效期三个小时
yhValueOperations.setIfAbsent(key, JSON.toJSONString(request));
DynamicIntProperty activeTime = DynamicPropertyFactory.getInstance().getIntProperty("activeTime", 3 * 60 * 60);
yHRedisTemplate.longExpire(key, activeTime.get(), TimeUnit.SECONDS);
log.info("clickUnion set redis success with request={}", request);
// if (union != null) {
// //如果90天以内,已经存在点击记录,则不需要插入或更新数据库
// log.info("clickUnion have click in 90 days with request={}", request);
// return new UnionResponse();
// }
//保存信息到数据库
// UnionLogs logs = new UnionLogs();
// logs.setTd(request.getTd());
// logs.setAppId(request.getAppid());
// logs.setClientType(request.getClient_type());
// logs.setUnionType(Byte.valueOf(request.getUnion_type()));
// logs.setCreateTime(DateUtil.getCurrentTimeSecond());
// logs.setUpdateTime(logs.getCreateTime());
// logs.setAddParams(JSON.toJSONString(request));
// logs.setIsActivate(Byte.valueOf("0"));
// unionLogsDAO.insert(logs);
return new UnionResponse();
} catch (Exception e) {
log.error("clickUnion error set redis error with request={}", request);
return new UnionResponse(204, "set redis error");
log.error("clickUnion error with request={}", request, e);
return new UnionResponse(300, e.getMessage());
}
// if (union != null) {
// //如果90天以内,已经存在点击记录,则不需要插入或更新数据库
// log.info("clickUnion have click in 90 days with request={}", request);
// return new UnionResponse();
// }
//保存信息到数据库
// UnionLogs logs = new UnionLogs();
// logs.setTd(request.getTd());
// logs.setAppId(request.getAppid());
// logs.setClientType(request.getClient_type());
// logs.setUnionType(Byte.valueOf(request.getUnion_type()));
// logs.setCreateTime(DateUtil.getCurrentTimeSecond());
// logs.setUpdateTime(logs.getCreateTime());
// logs.setAddParams(JSON.toJSONString(request));
// logs.setIsActivate(Byte.valueOf("0"));
// unionLogsDAO.insert(logs);
return new UnionResponse();
}
@Override
public UnionResponse activateUnion(ActivateUnionRequestBO request) throws ServiceException {
log.info("activateUnion with request is {}", request);
//检查输入参数
if (StringUtils.isEmpty(request.getAppid())) {
log.warn("activateUnion error because appid is empty with param is {}", request);
return new UnionResponse(201, "appid is empty");
}
if (StringUtils.isEmpty(request.getTd())) {
log.warn("activateUnion error because td is empty with param is {}", request);
return new UnionResponse(201, "td is empty");
}
if (StringUtils.isEmpty(request.getUdid())) {
log.warn("activateUnion error because udid is empty with param is {}", request);
return new UnionResponse(201, "udid is empty");
}
if (ClientTypeEnum.IOS.getName().equals(request.getClient_type()) && StringUtils.isEmpty(request.getIdfa())) {
log.warn("activateUnion error because idfa is empty with request is {}", request);
return new UnionResponse(201, "idfa is empty");
}
if (ClientTypeEnum.ANDROID.getName().equals(request.getClient_type()) && StringUtils.isEmpty(request.getImei())) {
log.warn("activateUnion error because imei is empty with request is {}", request);
return new UnionResponse(201, "imei is empty");
}
//组装redis保存的key
String key = UNION_KEY + "_" + request.getClientIp() + "_" + request.getTd();
String value = "";
try {
value = yhValueOperations.get(key);
} catch (Exception e) {
log.error("activateUnion error get redis error with request={}", request);
return new UnionResponse(204, "get redis error");
}
log.info("activateUnion get key={}, value={}", key, value);
//如果redis中不存在存在该用户点击信息,则退出
if (StringUtils.isEmpty(value)) {
log.warn("activateUnion error user not click info. with param is {}", request);
return new UnionResponse(204, "user not click");
}
//把存储的字符串变为对象
ClickUnionRequestBO click = JSON.parseObject(value, ClickUnionRequestBO.class);
//查询该td在90天内是否已经激活过
UnionLogs union = unionLogsDAO.selectByClientType(request.getClient_type(), request.getIdfa(), request.getImei(), Byte.valueOf(click.getUnion_type()));
log.info("activateUnion in selectByClientType result is {}", union);
//没有点击记录,则退出
// if (union == null) {
// log.warn("activateUnion error because not click record with request is {}", request);
// return new UnionResponse(205, "not click record");
// }
if (union != null && union.getIsActivate() != null && union.getIsActivate().byteValue() == 1) {
//如果90天之内有过激活日志,则不允许重复激活
log.warn("activateUnion error because 90 days has activate info with param is {}", request);
return new UnionResponse(203, "have activite in 90 days");
}
//UnionTypeModel type = UnionConstant.unionTypeMap.get(Integer.parseInt(click.getUnion_type()));
String url = click.getCallbackurl();
if (url.indexOf("?") > 0) {
url += "&identify_id=" + click.getIdentify_id();
} else {
url += "?identify_id=" + click.getIdentify_id();
}
log.info("activateUnion with request is {}", request);
// 检查输入参数
if (StringUtils.isEmpty(request.getAppid())) {
log.warn("activateUnion error because appid is empty with param is {}", request);
return new UnionResponse(201, "appid is empty");
}
if (StringUtils.isEmpty(request.getTd())) {
log.warn("activateUnion error because td is empty with param is {}", request);
return new UnionResponse(201, "td is empty");
}
if (StringUtils.isEmpty(request.getUdid())) {
log.warn("activateUnion error because udid is empty with param is {}", request);
return new UnionResponse(201, "udid is empty");
}
if (ClientTypeEnum.IOS.getName().equals(request.getClient_type()) && StringUtils.isEmpty(request.getIdfa())) {
log.warn("activateUnion error because idfa is empty with request is {}", request);
return new UnionResponse(201, "idfa is empty");
}
if (ClientTypeEnum.ANDROID.getName().equals(request.getClient_type()) && StringUtils.isEmpty(request.getImei())) {
log.warn("activateUnion error because imei is empty with request is {}", request);
return new UnionResponse(201, "imei is empty");
}
// 组装redis保存的key
String key = UNION_KEY + "_" + request.getClientIp() + "_" + request.getTd();
String value = yhValueOperations.get(key);
log.info("activateUnion get key={}, value={}", key, value);
// 如果redis中不存在存在该用户点击信息,则退出
if (StringUtils.isEmpty(value)) {
log.warn("activateUnion error user not click info. with param is {}", request);
return new UnionResponse(204, "user not click");
}
// 把存储的字符串变为对象
ClickUnionRequestBO click = JSON.parseObject(value, ClickUnionRequestBO.class);
// 查询该td在90天内是否已经激活过
UnionLogs union = unionLogsDAO.selectByClientType(request.getClient_type(), request.getIdfa(), request.getImei(), Byte.valueOf(click.getUnion_type()));
log.info("activateUnion in selectByClientType result is {}", union);
// 没有点击记录,则退出
// if (union == null) {
// log.warn("activateUnion error because not click record with request is {}",
// request);
// return new UnionResponse(205, "not click record");
// }
if (union != null && union.getIsActivate() != null && union.getIsActivate().byteValue() == 1) {
// 如果90天之内有过激活日志,则不允许重复激活
log.warn("activateUnion error because 90 days has activate info with param is {}", request);
return new UnionResponse(203, "have activite in 90 days");
}
// UnionTypeModel type =
// UnionConstant.unionTypeMap.get(Integer.parseInt(click.getUnion_type()));
String url = click.getCallbackurl();
if (url.indexOf("?") > 0) {
url += "&identify_id=" + click.getIdentify_id();
} else {
url += "?identify_id=" + click.getIdentify_id();
}
if (ClientTypeEnum.IOS.getName().equals(request.getClient_type())) {
url += "&idfa=" + click.getIdfa();
} else {
url += "&imei=" + click.getImei();
}
// 调用接口发送短信请求
AsyncFuture<String> response = service.get("union.activate", url, null, String.class, null);
String result = response.get();
log.info("activateUnion call union url={}, and result={}", url, result);
if (ClientTypeEnum.IOS.getName().equals(request.getClient_type())) {
url += "&idfa=" + click.getIdfa();
} else {
url += "&imei=" + click.getImei();
}
// 调用接口发送短信请求
AsyncFuture<String> response = service.get("union.activate", url, null, String.class, null);
String result = response.get();
log.info("activateUnion call union url={}, and result={}", url, result);
try {
JSONObject json = JSON.parseObject(result);
if (json.getInteger("code") == 200) {
//调用成功,更新数据库
// 调用成功,更新数据库
UnionLogs logs = new UnionLogs();
logs.setAppId(click.getAppid());
logs.setUdid(request.getUdid());
... ... @@ -232,9 +231,9 @@ public class UnionServiceImpl implements IUnionService {
logs.setAddParams(value);
logs.setTd(request.getTd());
unionLogsDAO.insert(logs);
UnionTypeModel u = UnionConstant.unionTypeMap.get(Integer.parseInt(click.getUnion_type()));
//记录日志
// 记录日志
JSONObject j = new JSONObject();
j.put("apptype", request.getClient_type());
j.put("appid", click.getAppid());
... ... @@ -251,9 +250,10 @@ public class UnionServiceImpl implements IUnionService {
return new UnionResponse(json.getInteger("code"), json.getString("message"));
}
} catch (Exception e) {
log.error("activateUnion result is not json with url={}, result={}", url, result);
return new UnionResponse(210, "error");
log.error("activateUnion error with request={}", request, e);
return new UnionResponse(300, e.getMessage());
}
}
}
... ...