Authored by linlong

Merge branch 'master' into fix_0223

package com.yoho.unions.common.redis;
import com.yoho.core.redis.YHListOperations;
import com.yoho.core.redis.YHRedisTemplate;
import com.yoho.unions.common.utils.SerializeUtils;
import com.yoho.unions.helper.CacheKeyHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -9,6 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
@Component
public class RedisListCache {
... ... @@ -17,6 +23,8 @@ public class RedisListCache {
@Resource(name="yhListOperations")
YHListOperations<String, String> yhListOperations;
@Resource
YHRedisTemplate<String, String> yHRedisTemplate;
/**
* redis rightPop 操作
... ... @@ -66,4 +74,33 @@ public class RedisListCache {
}
public <T> void rightPushAll(String key,Collection<T> values, long timeout, TimeUnit unit) {
logger.debug("Enter rightPushAll redis list. key is {}, value is {}, timeout is {}, unit is {}", key, values, timeout, unit);
// 如果是空列表,直接返回
if (CollectionUtils.isEmpty(values)) {
return;
}
String cacheKey = key;
// 如果获取的key为空,则说明缓存开关是关闭的
if (StringUtils.isEmpty(cacheKey)) {
return;
}
try {
Collection<String> strValues = new ArrayList<String>();
for (T t : values) {
String strValue = CacheKeyHelper.value2String(t);
if (StringUtils.isEmpty(strValue)) {
continue;
}
strValues.add(strValue);
}
yhListOperations.rightPushAll(cacheKey, strValues);
yHRedisTemplate.longExpire(cacheKey, timeout, unit);
} catch (Exception e) {
logger.warn("rightPushAll redis list operation failed. key is {}", cacheKey, e);
}
}
}
... ...
package com.yoho.unions.vo;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* 转化数据
* Created by yoho on 2017/2/22.
*/
public class TransInfo implements Serializable {
//标示是普通交寄还是首单交寄
private String type;
private BigDecimal orderAmount;
private String udid;
private String uid;
private String ordercode;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getOrdercode() {
return ordercode;
}
public void setOrdercode(String ordercode) {
this.ordercode = ordercode;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public BigDecimal getOrderAmount() {
return orderAmount;
}
public void setOrderAmount(BigDecimal orderAmount) {
this.orderAmount = orderAmount;
}
public String getUdid() {
return udid;
}
public void setUdid(String udid) {
this.udid = udid;
}
}
... ...
package com.yoho.unions.dal;
import com.yoho.unions.dal.model.UnionActivity;
public interface IUnionActivityDAO {
int insert(UnionActivity record);
int insertSelective(UnionActivity record);
UnionActivity selectByPrimaryKey(Long id);
UnionActivity selectByUdid(String udid);
}
\ No newline at end of file
... ...
//package com.yoho.unions.dal;
//
//
//import com.yoho.unions.dal.model.UnionDepartmentUrl;
//
//public interface IUnionDepartmentUrlDAO {
// int deleteByPrimaryKey(Integer id);
//
// int insert(UnionDepartmentUrl record);
//
// int insertSelective(UnionDepartmentUrl record);
//
// UnionDepartmentUrl selectByPrimaryKey(Integer id);
//
// int updateByPrimaryKeySelective(UnionDepartmentUrl record);
//
// int updateByPrimaryKey(UnionDepartmentUrl record);
//
// UnionDepartmentUrl selectByUnionType(String unionType);
//}
\ No newline at end of file
//package com.yoho.unions.dal;
//
//
//import com.yoho.unions.dal.model.UnionType;
//
//public interface IUnionTypeDAO {
// int deleteByPrimaryKey(Integer id);
//
// int insert(UnionType record);
//
// int insertSelective(UnionType record);
//
// UnionType selectByPrimaryKey(Integer id);
//
// UnionType selectByUnionType(Integer unionType);
//
// int updateByPrimaryKeySelective(UnionType record);
//
// int updateByPrimaryKey(UnionType record);
//}
\ No newline at end of file
... ... @@ -27,6 +27,8 @@ public class MktMarketingUrl {
private String mktActivityCode;
private String landingPageUrl;
public String getMktActivityCode() {
return mktActivityCode;
}
... ... @@ -130,4 +132,12 @@ public class MktMarketingUrl {
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
public String getLandingPageUrl() {
return landingPageUrl;
}
public void setLandingPageUrl(String landingPageUrl) {
this.landingPageUrl = landingPageUrl;
}
}
\ No newline at end of file
... ...
package com.yoho.unions.dal.model;
public class UnionActivity {
private Long id;
private String udid;
private String muid;
private String unionType;
private String clientType;
private String advertiserId;
private String clickId;
private Integer createTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUdid() {
return udid;
}
public void setUdid(String udid) {
this.udid = udid == null ? null : udid.trim();
}
public String getMuid() {
return muid;
}
public void setMuid(String muid) {
this.muid = muid == null ? null : muid.trim();
}
public String getUnionType() {
return unionType;
}
public void setUnionType(String unionType) {
this.unionType = unionType == null ? null : unionType.trim();
}
public String getClientType() {
return clientType;
}
public void setClientType(String clientType) {
this.clientType = clientType == null ? null : clientType.trim();
}
public String getAdvertiserId() {
return advertiserId;
}
public void setAdvertiserId(String advertiserId) {
this.advertiserId = advertiserId == null ? null : advertiserId.trim();
}
public String getClickId() {
return clickId;
}
public void setClickId(String clickId) {
this.clickId = clickId == null ? null : clickId.trim();
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
}
\ No newline at end of file
... ...
package com.yoho.unions.dal.model;
import java.math.BigDecimal;
/**
* Created by yoho on 2017/2/22.
*/
public class UnionActivityTrans {
//订单金额
private BigDecimal order_amount;
//设备号
private String udid;
private String uid;
private String order_code;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUdid() {
return udid;
}
public void setUdid(String udid) {
this.udid = udid;
}
public BigDecimal getOrder_amount() {
return order_amount;
}
public void setOrder_amount(BigDecimal order_amount) {
this.order_amount = order_amount;
}
public String getOrder_code() {
return order_code;
}
public void setOrder_code(String order_code) {
this.order_code = order_code;
}
}
... ...
... ... @@ -15,10 +15,11 @@
<result column="status" property="status" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
<result column="mkt_activity_code" property="mktActivityCode" jdbcType="VARCHAR" />
<result column="landing_page_url" property="landingPageUrl" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
union_type, division_code, class_code, channel_code, device_code, dept_id, create_id,
name, src_url, dest_url, status, create_time,mkt_activity_code
name, src_url, dest_url, status, create_time,mkt_activity_code,landing_page_url
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yoho.unions.dal.IUnionActivityDAO" >
<resultMap id="BaseResultMap" type="com.yoho.unions.dal.model.UnionActivity" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="udid" property="udid" jdbcType="VARCHAR" />
<result column="muid" property="muid" jdbcType="VARCHAR" />
<result column="union_type" property="unionType" jdbcType="VARCHAR" />
<result column="client_type" property="clientType" jdbcType="VARCHAR" />
<result column="advertiser_id" property="advertiserId" jdbcType="VARCHAR" />
<result column="click_id" property="clickId" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, udid, muid, union_type, client_type, advertiser_id, click_id, create_time
</sql>
<select id="selectByUdid" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from union_activity
where udid = #{udid} limit 1
</select>
<insert id="insert" parameterType="com.yoho.unions.dal.model.UnionActivity" >
insert into union_activity (id, udid, muid,
union_type, client_type, advertiser_id,
click_id, create_time)
values (#{id,jdbcType=BIGINT}, #{udid,jdbcType=VARCHAR}, #{muid,jdbcType=VARCHAR},
#{unionType,jdbcType=VARCHAR}, #{clientType,jdbcType=VARCHAR}, #{advertiserId,jdbcType=VARCHAR},
#{clickId,jdbcType=VARCHAR}, #{createTime,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.yoho.unions.dal.model.UnionActivity" >
insert into union_activity
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="udid != null" >
udid,
</if>
<if test="muid != null" >
muid,
</if>
<if test="unionType != null" >
union_type,
</if>
<if test="clientType != null" >
client_type,
</if>
<if test="advertiserId != null" >
advertiser_id,
</if>
<if test="clickId != null" >
click_id,
</if>
<if test="createTime != null" >
create_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=BIGINT},
</if>
<if test="udid != null" >
#{udid,jdbcType=VARCHAR},
</if>
<if test="muid != null" >
#{muid,jdbcType=VARCHAR},
</if>
<if test="unionType != null" >
#{unionType,jdbcType=VARCHAR},
</if>
<if test="clientType != null" >
#{clientType,jdbcType=VARCHAR},
</if>
<if test="advertiserId != null" >
#{advertiserId,jdbcType=VARCHAR},
</if>
<if test="clickId != null" >
#{clickId,jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=INTEGER},
</if>
</trim>
</insert>
</mapper>
\ No newline at end of file
... ...
... ... @@ -62,7 +62,7 @@ public class ActivateUnionRest {
* @return
*/
@RequestMapping("/activateUnion")
@ResponseBody public ActiveUnionResponseBO activateUnion(ActivateUnionRequestVO vo, HttpServletRequest request, HttpServletResponse response) {
@ResponseBody public UnionResponse activateUnion(ActivateUnionRequestVO vo, HttpServletRequest request, HttpServletResponse response) {
log.info("activateUnion with param is {}", vo);
if ("iphone".equals(vo.getClient_type())) {
vo.setClient_type(ClientTypeEnum.IOS.getName());
... ... @@ -97,7 +97,7 @@ public class ActivateUnionRest {
if (!ClientTypeEnum.ANDROID.getName().equalsIgnoreCase(clientType) && !ClientTypeEnum.IOS.getName().equalsIgnoreCase(clientType)) {
log.warn("activateUnion error with param is {}", vo);
return new ActiveUnionResponseBO(600, "error");
return new UnionResponse(200, "error",new JSONObject());
}
//IP取出来的有可能是42.239.40.36,123.151.42.50,只取第一个
String IP = RemoteIPInterceptor.getRemoteIP();
... ... @@ -108,8 +108,40 @@ public class ActivateUnionRest {
}
bo.setClientIp(clientIp);
//多线程处理
exe.execute(new RunActivate(bo));
return new ActiveUnionResponseBO(200, "success");
//exe.execute(new RunActivate(bo));
return getGoUrl(bo);
}
private UnionResponse getGoUrl(ActivateUnionRequestBO bo){
MainUnionService service = null;
if (ClientTypeEnum.ANDROID.getName().equalsIgnoreCase(bo.getClient_type())) {
//处理安卓的服务
for (String str : UnionConstant.andriodServiceList) {
//捕获异常,不影响后面的联盟
try {
service = SpringContextUtil.getBean(str, MainUnionService.class);
service.activeUnion(bo);
} catch (Exception e) {
log.warn("addUnion error with param is {}", bo, e);
}
}
} else if (ClientTypeEnum.IOS.getName().equalsIgnoreCase(bo.getClient_type())) {
//处理iOS的服务
for (String str : UnionConstant.iOSServiceList) {
//捕获异常,不影响后面的联盟
try {
service = SpringContextUtil.getBean(str, MainUnionService.class);
service.activeUnion(bo);
} catch (Exception e) {
log.warn("addUnion error with param is {}", bo, e);
}
}
}
//调用统一的联盟激活接口
IUnionService unionService = SpringContextUtil.getBean("unionServiceImpl", IUnionService.class);
activeUnion.info("activeUnion request is {}",bo);
return unionService.activateUnion(bo);
}
public static class RunActivate implements Runnable {
... ... @@ -122,35 +154,7 @@ public class ActivateUnionRest {
@Override
public void run() {
MainUnionService service = null;
if (ClientTypeEnum.ANDROID.getName().equalsIgnoreCase(bo.getClient_type())) {
//处理安卓的服务
for (String str : UnionConstant.andriodServiceList) {
//捕获异常,不影响后面的联盟
try {
service = SpringContextUtil.getBean(str, MainUnionService.class);
service.activeUnion(bo);
} catch (Exception e) {
log.warn("addUnion error with param is {}", bo, e);
}
}
} else if (ClientTypeEnum.IOS.getName().equalsIgnoreCase(bo.getClient_type())) {
//处理iOS的服务
for (String str : UnionConstant.iOSServiceList) {
//捕获异常,不影响后面的联盟
try {
service = SpringContextUtil.getBean(str, MainUnionService.class);
service.activeUnion(bo);
} catch (Exception e) {
log.warn("addUnion error with param is {}", bo, e);
}
}
}
//调用统一的联盟激活接口
IUnionService unionService = SpringContextUtil.getBean("unionServiceImpl", IUnionService.class);
activeUnion.info("activeUnion request is {}",bo);
unionService.activateUnion(bo);
}
public ActivateUnionRequestBO getBo() {
... ...
package com.yoho.unions.server.service;
import com.yoho.unions.vo.OrderInfo;
import com.yoho.unions.vo.TransInfo;
import com.yoho.unions.vo.UnionOrderReqVO;
import java.util.List;
... ... @@ -9,4 +10,6 @@ public interface IUnionOrderService {
public List<OrderInfo> getUnionOrders(UnionOrderReqVO req);
public List<TransInfo> getTrandInfo(UnionOrderReqVO req);
}
... ...
package com.yoho.unions.server.service.impl;
import com.yoho.service.model.union.UnionTrans;
import com.yoho.unions.common.redis.RedisHashCache;
import com.yoho.unions.common.redis.RedisListCache;
import com.yoho.unions.dal.model.UnionActivityTrans;
import com.yoho.unions.dal.model.UnionOrders;
import com.yoho.unions.dal.model.UnionOrdersGoods;
import com.yoho.unions.server.service.IUnionOrderService;
import com.yoho.unions.vo.OrderInfo;
import com.yoho.unions.vo.OrdersGood;
import com.yoho.unions.vo.TransInfo;
import com.yoho.unions.vo.UnionOrderReqVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
... ... @@ -26,8 +29,14 @@ public class UnionOrderServiceImpl implements IUnionOrderService {
private static final String UNION_ORDER_KEY = "union:orders";
private static final String UNION_TRAND = "YH:BD:ORDER:UNIONS";
private static final String UNION_ORDER_GOODS_KEY_PRE = "union:order_goods:";
private static final String UNION_TRANS_UNIONTYPE = "YH:BD:UNI:1:ORDER:";
private static final String UNION_NORMAL_TRANS_UNIONTYPE = "YH:BD:UNI:ALL:ORDER:";
@Autowired
private RedisListCache redisListCache;
... ... @@ -59,6 +68,7 @@ public class UnionOrderServiceImpl implements IUnionOrderService {
orderInfo.setOrdersGoods(ordersGoodList);
resList.add(orderInfo);
}
logger.info("Exit getUnionOrders: resList size is {}",resList.size());
return resList;
}catch(Exception e){
... ... @@ -67,6 +77,53 @@ public class UnionOrderServiceImpl implements IUnionOrderService {
}
}
@Override
public List<TransInfo> getTrandInfo(UnionOrderReqVO req) {
logger.info("Enter getTrandInfo: request param is {}", req);
try {
List<TransInfo> resList = new ArrayList<TransInfo>();
Long size = redisListCache.size(UNION_TRAND);
int sizeInt = size == null ? 0 : size.intValue();
logger.info("getTrandInfo UNION_TRAND size is {}",size);
if (sizeInt > 0) {
for (int i = 0; i < size; i++) {
UnionTrans unionTrans = redisListCache.rightPop(UNION_TRAND, UnionTrans.class);
String union_type = unionTrans.getUnion_type();
//首单交寄
String firstOrder = UNION_TRANS_UNIONTYPE + union_type;
//普通交寄
String normalOrder = UNION_NORMAL_TRANS_UNIONTYPE + union_type;
List<UnionActivityTrans> firstOrderList = new ArrayList<>();
Long firstOrderSize = redisListCache.size(firstOrder);
Long normalOrderSize = redisListCache.size(normalOrder);
if (firstOrderSize > 0) {
for (int j = 0; j < firstOrderSize; j++) {
UnionActivityTrans unionActivityTrans = redisListCache.rightPop(firstOrder, UnionActivityTrans.class);
firstOrderList.add(unionActivityTrans);
}
}
logger.info("getFirstOrderList: from bigdata redis: key is {}, result size is {}", firstOrder, firstOrderList == null ? 0 : firstOrderList.size());
List<UnionActivityTrans> normalOrderList = new ArrayList<>();
if (normalOrderSize > 0) {
for (int k = 0; k < normalOrderSize; k++) {
UnionActivityTrans unionActivityTrans = redisListCache.rightPop(normalOrder, UnionActivityTrans.class);
normalOrderList.add(unionActivityTrans);
}
}
logger.info("getNormalOrderList: from bigdata redis: key is {}, result size is {}", normalOrder, normalOrderList == null ? 0 : normalOrderList.size());
resList = this.convertTrans(resList,firstOrderList, normalOrderList);
}
}
logger.info("exit getTrandInfo result size is {}",resList.size());
return resList;
} catch (Exception e) {
logger.warn("getTrandInfo: get orders from bigdata redis failed, error is {}", e.getMessage());
return null;
}
}
private OrderInfo convertToUnionOrderBo(UnionOrders unionOrder){
if(null == unionOrder){
return null;
... ... @@ -114,4 +171,35 @@ public class UnionOrderServiceImpl implements IUnionOrderService {
return resList;
}
private List<TransInfo> convertTrans(List<TransInfo> resList,List<UnionActivityTrans> firstOrderList,List<UnionActivityTrans> normalOrderList){
if(CollectionUtils.isEmpty(firstOrderList)&&CollectionUtils.isEmpty(normalOrderList)){
return null;
}
if(CollectionUtils.isNotEmpty(firstOrderList)){
for(UnionActivityTrans unionActivityTrans:firstOrderList){
TransInfo transInfo = new TransInfo();
//首单设为1
transInfo.setType("1");
transInfo.setUdid(unionActivityTrans.getUdid());
transInfo.setOrderAmount(unionActivityTrans.getOrder_amount());
transInfo.setUid(unionActivityTrans.getUid());
transInfo.setOrdercode(unionActivityTrans.getOrder_code());
resList.add(transInfo);
}
}
if(CollectionUtils.isNotEmpty(normalOrderList)){
for(UnionActivityTrans unionActivityTran:normalOrderList){
TransInfo transInfo = new TransInfo();
//正常单设为2
transInfo.setType("2");
transInfo.setUdid(unionActivityTran.getUdid());
transInfo.setOrderAmount(unionActivityTran.getOrder_amount());
transInfo.setUid(unionActivityTran.getUid());
transInfo.setOrdercode(unionActivityTran.getOrder_code());
resList.add(transInfo);
}
}
return resList;
}
}
... ...
... ... @@ -19,6 +19,7 @@ import com.yoho.service.model.union.request.ActivateUnionRequestBO;
import com.yoho.service.model.union.request.ClickUnionRequestBO;
import com.yoho.service.model.union.response.UnionResponse;
import com.yoho.unions.common.enums.ClientTypeEnum;
import com.yoho.unions.common.redis.RedisListCache;
import com.yoho.unions.common.redis.RedisValueCache;
import com.yoho.unions.common.utils.DateUtil;
import com.yoho.unions.common.utils.HttpUtils;
... ... @@ -34,6 +35,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.net.URLDecoder;
import java.util.List;
... ... @@ -70,7 +72,7 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
@Resource(name="yhValueOperations")
YHValueOperations<String, String> yhValueOperations;
@Resource
IUnionLogsDAO unionLogsDAO;
... ... @@ -84,6 +86,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
RedisValueCache redisValueCache;
@Resource
RedisListCache redisListCache;
@Resource
IMktMarketingUrlDAO mktMarketingUrlDAO;
@Resource
... ... @@ -93,6 +98,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
IUnionActivityLogsDAO unionActivityLogsDAO;
@Resource
IUnionActivityDAO unionActivityDAO;
@Resource
IUnionTypeMatchDAO unionTypeMatchDAO;
@Resource(name="unionServiceImpl")
... ... @@ -182,25 +190,18 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
yhValueOperations.set(key, JSON.toJSONString(request));
yHRedisTemplate.longExpire(key, activeTime.get(), TimeUnit.HOURS);
clickUnion.info("clickUnion set redis second success. with key={}, value={}", key, JSON.toJSONString(request));
// 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();
// UnionActivityTrans unionActivityTrans = new UnionActivityTrans();
// List<UnionActivityTrans> list = new ArrayList<>();
// for(int i=0;i<3;i++){
// UnionActivityTrans unionActivityTrans = new UnionActivityTrans();
// unionActivityTrans.setOrdercode("123");
// unionActivityTrans.setUdid("234");
// unionActivityTrans.setOrderAmount(new BigDecimal(12));
// unionActivityTrans.setUid("12");
// list.add(unionActivityTrans);
// }
//保存信息到数据库
// 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);
// redisListCache.rightPushAll("UNION:KEY:123",list,activeTime.get(),TimeUnit.HOURS);
return new UnionResponse();
} catch (Exception e) {
... ... @@ -216,25 +217,25 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
// 检查输入参数
if (StringUtils.isEmpty(request.getAppid())) {
log.warn("activateUnion error because appid is empty with param is {}", request);
return new UnionResponse(201, "appid is empty");
return new UnionResponse(200, "appid is empty",new JSONObject());
}
if (StringUtils.isEmpty(request.getTd())) {
log.warn("activateUnion error because td is empty with param is {}", request);
return new UnionResponse(201, "td is empty");
return new UnionResponse(200, "td is empty",new JSONObject());
}
if (StringUtils.isEmpty(request.getUdid())) {
log.warn("activateUnion error because udid is empty with param is {}", request);
return new UnionResponse(201, "udid is empty");
return new UnionResponse(200, "udid is empty",new JSONObject());
}
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");
return new UnionResponse(200, "idfa is empty",new JSONObject());
}
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");
return new UnionResponse(200, "imei is empty",new JSONObject());
}
try{
... ... @@ -315,7 +316,7 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
value = yhValueOperations.get(key);
if(StringUtils.isNotEmpty(value)){
ipMatch.info("activateUnion with IP params td is {},imei is {},idfa is {},IP is {},---- clickMsg is {}",request.getTd(),request.getImei(),request.getIdfa(),request.getClientIp(),value);
return new UnionResponse(204, "user not click");
return new UnionResponse(200, "user not click",new JSONObject());
}
}
... ... @@ -326,7 +327,7 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
// 如果redis中不存在存在该用户点击信息,则退出
if (StringUtils.isEmpty(value)) {
log.warn("activateUnion error user not click info. with param is {}", request);
return new UnionResponse(204, "user not click");
return new UnionResponse(200, "user not click",new JSONObject());
}
// 把存储的字符串变为对象
... ... @@ -348,17 +349,23 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
//强制删除带ip的key
yHRedisTemplate.delete(UNION_KEY + "_" + request.getClientIp() + "_" + request.getAppkey());
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");
}
String unionTypekey = "yh:union:uniontype:"+click.getUnion_type();
MktMarketingUrl mktMarketingUrl = redisValueCache.get(unionTypekey,MktMarketingUrl.class);
if(mktMarketingUrl==null){
mktMarketingUrl = mktMarketingUrlDAO.selectByPrimaryKey(Long.valueOf(click.getUnion_type()));
redisValueCache.set(unionTypekey, mktMarketingUrl, 1, TimeUnit.HOURS);
}
JSONObject result = new JSONObject();
result.put("landing_page_url",StringUtils.isEmpty(mktMarketingUrl.getLandingPageUrl()) ? "" : mktMarketingUrl.getLandingPageUrl());
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(200, "have activite in 90 days",result);
}
// UnionTypeModel u = UnionConstant.unionTypeMap.get(Integer.parseInt(click.getUnion_type()));
UnionTypeModel u = new UnionTypeModel();
u.setName(mktMarketingUrl.getName());
u.setValue(String.valueOf(mktMarketingUrl.getUnionType()));
... ... @@ -406,7 +413,7 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
j.put("tdid", request.getTdid());
activeDingdang.info(j.toString());
}
return new UnionResponse(203, "have activite in 90 days");
return new UnionResponse(200, "have activite in 90 days",result);
}
}
... ... @@ -489,7 +496,7 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
}
if (StringUtils.isEmpty(url)) {
log.info("activateUnion in success request is {}", request);
return new UnionResponse();
return new UnionResponse(200,"success",result);
}
if(!"3".equals(union_type)||!"100000000000453".equals(union_type)){
url = URLDecoder.decode(url, "UTF-8");
... ... @@ -506,21 +513,26 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
activeUnion.info("activateUnion call union success url={}, and result={}", url, pair);
if (pair.getLeft() != 200) {
log.warn("callback error with request={}", request);
return new UnionResponse(204, "callback error");
return new UnionResponse(200, "callback error",result);
}
//如果来源是广点通,则把广点通的一些信息记入表,给之后做转化上报使用
if(union_type.equals("3")){
saveUnionActivity(click,request);
}
} catch (Exception e) {
log.error("callback error with request={}", request, e);
//return new UnionResponse(204, "callback error");
}
activeUnion.info("activateUnion in success request is {}", request);
return new UnionResponse();
return new UnionResponse(200,"success",result);
} catch (Exception e) {
log.error("activateUnion error with request={}", request, e);
return new UnionResponse(300, e.getMessage());
return new UnionResponse(200, e.getMessage(),new JSONObject());
}
}
... ... @@ -567,6 +579,30 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
});
}
private void saveUnionActivity(ClickUnionRequestBO click,ActivateUnionRequestBO request){
taskExecutor.execute(new Runnable(){
@Override
public void run()
{
UnionActivity unionActivity = new UnionActivity();
unionActivity.setClientType(request.getClient_type());
unionActivity.setUdid(request.getUdid());
if(StringUtils.isNotEmpty(click.getIdfa())){
unionActivity.setUdid(click.getIdfa());
}else {
unionActivity.setUdid(click.getImei());
}
unionActivity.setUnionType(click.getUnion_type());
//广点通的账号id存入的是click的commonUse字段
unionActivity.setAdvertiserId(click.getCommonUse());
//广点通点击id
unionActivity.setClickId(click.getClickId());
unionActivity.setCreateTime(DateUtil.getCurrentTimeSecond());
unionActivityDAO.insertSelective(unionActivity);
}
});
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
... ...
package com.yoho.unions.server.task;
import com.yoho.service.model.union.response.UnionResponse;
import com.yoho.unions.common.enums.ClientTypeEnum;
import com.yoho.unions.common.utils.DateUtil;
import com.yoho.unions.common.utils.HttpUtils;
import com.yoho.unions.dal.IUnionActivityDAO;
import com.yoho.unions.dal.IUnionConfigDAO;
import com.yoho.unions.dal.model.UnionActivity;
import com.yoho.unions.dal.model.UnionConfig;
import com.yoho.unions.server.service.IUnionOrderService;
import com.yoho.core.common.utils.MD5;
import org.apache.commons.codec.binary.Base64;
import com.yoho.unions.vo.TransInfo;
import com.yoho.unions.vo.UnionOrderReqVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;
/**
* 广点通转化数据上报,定时从大数据抽取数据,然后联盟拼接,上报给广点通
* Created by yoho on 2017/2/22.
*/
@Component
public class GdtTransTask {
static Logger log = LoggerFactory.getLogger(GdtTransTask.class);
@Autowired
private IUnionOrderService unionOrderService;
@Resource
IUnionActivityDAO unionActivityDAO;
@Resource
IUnionConfigDAO unionConfigDAO;
@Scheduled(cron = "0 0/6 * * * ?")
public void run() {
UnionOrderReqVO reqVO = new UnionOrderReqVO();
reqVO.setLimit(10);
this.execute(reqVO);
}
public void execute(UnionOrderReqVO reqVO){
//从大数据中取出联盟需要的广点通带来转化的设备号,和金额
List<TransInfo> transInfoList = unionOrderService.getTrandInfo(reqVO);
if (CollectionUtils.isEmpty(transInfoList)) {
return;
}
//将取出来的数据进行拼接
for(TransInfo transInfo:transInfoList){
log.info("enter trans udid is {},ordercode is {}",transInfo.getUdid(),transInfo.getOrdercode());
String udid = transInfo.getUdid();
//利用udid去表中查出是两个广点通账号带来的
UnionActivity unionActivity = unionActivityDAO.selectByUdid(udid);
if(unionActivity==null){
continue;
}
//按照广点通要求拼接数据
String url = urlEode(unionActivity,transInfo);
//改成httpclient方式调用
Pair<Integer, String> pair = HttpUtils.httpGet(url);
log.info("GdtTransTask call union success url={}, and result={}", url, pair);
if (pair.getLeft() != 200) {
log.warn("callback error with request={}", url);
}
}
}
public String urlEode(UnionActivity requestBO,TransInfo transInfo) {
log.info("enter GdtTransTask.urlEode requestBO is {}, transInfo is {} ", requestBO,transInfo);
// 根据不同的应用类型,获取不同的加密密钥和签名密钥
String encryptKey = "BAAAAAAAAAAABZJQ";
String signKey = "1461f1237d22dfc7";
String muid4MD5 = requestBO.getMuid();
String url = "http://t.gdt.qq.com/conv/app/";
//有货在智慧推的账号id
String uid = "365136";
if(StringUtils.isNotEmpty(requestBO.getAdvertiserId())){
uid = requestBO.getAdvertiserId();
}
String clientType = ClientTypeEnum.IOS.getName();
if(StringUtils.isNotEmpty(requestBO.getClientType())){
clientType = requestBO.getClientType().toLowerCase();
}
UnionConfig unionConfig = unionConfigDAO.selectByUidAndClientType(Integer.valueOf(uid),clientType);
if(null!=unionConfig){
encryptKey = unionConfig.getEncryptKey();
signKey = unionConfig.getSignKey();
url = unionConfig.getUrl();
}
//APPID可以固定根据clientType判断
String appId = "490655927";
if(clientType.equals(ClientTypeEnum.ANDROID.getName())){
appId = "100898104";
}
int conv_time = DateUtil.getCurrentTimeSecond();
String page = null;
String query_string = null;
String clickId = requestBO.getClickId();
StringBuffer bf = new StringBuffer();
StringBuffer sf = new StringBuffer();
page = bf.append(url).append(appId).append("/conv?click_id=").append(clickId).append("&muid=").append(muid4MD5).append("&conv_time=").append(conv_time).append("&value=").append(transInfo.getOrderAmount().multiply(new BigDecimal(100))).toString();
query_string = sf.append("click_id=").append(clickId).append("&muid=").append(muid4MD5).append("&conv_time=").append(conv_time).append("&value=").append(transInfo.getOrderAmount().multiply(new BigDecimal(100))).toString();
log.info("enter GdtTransTask.urlEode page is {}",page);
log.info("enter GdtTransTask.urlEode query_string is {}",query_string);
// 对url进行加密
String encode_page = null;
try {
encode_page = URLEncoder.encode(page, "UTF-8");
log.info("activateZhiHuitui urlEncoder is{} ", encode_page);
} catch (UnsupportedEncodingException e) {
log.error("encode exception.", e);
}
// 将签名密钥和提交方式与加密后的url进行拼接
StringBuffer urlBuffer = new StringBuffer();
String property = urlBuffer.append(signKey).append("&GET&").append(encode_page).toString();
String signature = MD5.md5(property);
StringBuffer base = new StringBuffer();
String base_data = base.append(query_string).append("&sign=").append(signature).toString();
// 简单异或处理
String data = simpleXorByGdt(base_data, encryptKey);
// base64编码
String ret = Base64.encodeBase64String(data.getBytes());
// 64位处理之后,防止里面有换行符
String retRep = ret.replaceAll("\n", "");
// 将64位处理之后的值,按照url加密的方式进行加密
try {
retRep = URLEncoder.encode(retRep, "UTF-8");
log.info("activateUnion retRep is{}", retRep);
} catch (UnsupportedEncodingException e) {
log.error("encode exception.", e);
}
// conv_type,,现在只有移动应用激活类型(MOBILEAPP_ACTIVITE);
String convType = "MOBILEAPP_COST";
if(transInfo.getType().equals("1")){
convType = "MOBILEAPP_ADDTOCART";
}
StringBuffer sb = new StringBuffer();
url = sb.append(url).append(appId).append("/conv?v=").append(retRep).append("&conv_type=").append(convType).append("&app_type=").append(clientTypeConver(clientType)).append("&advertiser_id=").append(uid).toString();
log.info("GdtTransTask url is {}", url);
return url;
}
/**
* 简单异或
*
* @param source
* @param key
* @return
*/
private String simpleXorByGdt(String source, String key) {
String result = "";
int j = 0;
for (int i = 0; i < source.length(); i++) {
int c1 = source.charAt(i);
int c2 = key.charAt(j);
result = result + (char) (c1 ^ c2);
j = j + 1;
j = j % (key.length());
}
return result;
}
private String clientTypeConver(String clientType) {
if (StringUtils.isEmpty(clientType)) {
return null;
}
if (ClientTypeEnum.ANDROID.getName().equalsIgnoreCase(clientType)) {
return clientType.toUpperCase();
} else {
return "IOS";
}
}
}
... ...
... ... @@ -42,5 +42,7 @@ datasources:
- com.yoho.unions.dal.IUnionTypeMatchDAO
- com.yoho.unions.dal.ITencentMktActivityDAO
- com.yoho.unions.dal.ITencentMktCouponHistoryDAO
- com.yoho.unions.dal.IUnionActivityDAO
- com.yoho.unions.dal.IUnionConfigDAO
readOnlyInSlave: true
\ No newline at end of file
... ...
... ... @@ -43,5 +43,7 @@ datasources:
- com.yoho.unions.dal.IUnionTypeMatchDAO
- com.yoho.unions.dal.ITencentMktActivityDAO
- com.yoho.unions.dal.ITencentMktCouponHistoryDAO
- com.yoho.unions.dal.IUnionActivityDAO
- com.yoho.unions.dal.IUnionConfigDAO
readOnlyInSlave: true
\ No newline at end of file
... ...
package com.test;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
... ... @@ -30,7 +31,7 @@ public class Test {
String encryptKey = "test_encrypt_key";
String signKey = "test_sign_key";
String muid4MD5 = "0f074dc8e1f0547310e729032ac0730b";
String url = "http://jump.t.l.qq.com/conv/app/";
String url = "http://t.gdt.qq.com/conv/app/";
//有货在智慧推的账号id
String uid = "18261";
... ... @@ -40,20 +41,33 @@ public class Test {
// clientType = requestBO.getClient_type().toLowerCase();
// }
String client_ip = "10.11.12.13";
// if(org.apache.commons.lang.StringUtils.isNotEmpty(requestBO.getAdvertiserId())){
// uid = requestBO.getAdvertiserId();
// }
// String clientType = ClientTypeEnum.IOS.getName();
// if(org.apache.commons.lang.StringUtils.isNotEmpty(requestBO.getClientType())){
// clientType = requestBO.getClientType().toLowerCase();
// }
// UnionConfig unionConfig = unionConfigDAO.selectByUidAndClientType(Integer.valueOf(uid),clientType);
// if(null!=unionConfig){
// encryptKey = unionConfig.getEncryptKey();
// signKey = unionConfig.getSignKey();
// url = unionConfig.getUrl();
// }
//APPID可以固定根据clientType判断
String appId = "112233";
int conv_time =1422263664;
if(clientType.equals(ClientTypeEnum.ANDROID.getName())){
appId = "112233";
}
int conv_time = DateUtil.getCurrentTimeSecond();
String page = null;
String query_string = null;
StringBuffer bf = new StringBuffer();
StringBuffer sf = new StringBuffer();
if (StringUtils.isEmpty(client_ip)){
page = bf.append(url).append(appId).append("/conv?muid=").append(muid4MD5).append("&conv_time=").append(conv_time).toString();
query_string = sf.append("muid=").append(muid4MD5).append("&conv_time=").append(conv_time).toString();
}else{
page = bf.append(url).append(appId).append("/conv?muid=").append(muid4MD5).append("&conv_time=").append(conv_time).append("&client_ip=").append(client_ip).toString();
query_string = sf.append("muid=").append(muid4MD5).append("&conv_time=").append(conv_time).append("&client_ip=").append(client_ip).toString();
}
page = bf.append(url).append(appId).append("/conv?click_id=").append("007210548a030059ccdfd1d4").append("&muid=").append(muid4MD5).append("&conv_time=").append(conv_time).append("&client_ip=10.11.12.13").toString();
query_string = sf.append("click_id=").append("007210548a030059ccdfd1d4").append("&muid=").append(muid4MD5).append("&conv_time=").append(conv_time).append("&client_ip=10.11.12.13").toString();
// 对url进行加密
String encode_page = null;
try {
... ... @@ -84,11 +98,15 @@ public class Test {
log.error("encode exception.", e);
}
// conv_type,,现在只有移动应用激活类型(MOBILEAPP_ACTIVITE);
String convType = "MOBILE_APP_ACTIVITE";
String convType = "MOBILEAPP_COST";
// if(transInfo.getType().equals("1")){
// convType = "MOBILEAPP_ADDTOCART";
// }
StringBuffer sb = new StringBuffer();
url = sb.append(url).append(112233).append("/conv?v=").append(retRep).append("&conv_type=").append(convType).append("&app_type=").append(clientTypeConver("Android")).append("&advertiser_id=").append(uid).toString();
log.info("zhihuitui url is {}", url);
System.out.print(url);
url = sb.append(url).append(appId).append("/conv?v=").append(retRep).append("&conv_type=").append(convType).append("&app_type=").append(clientTypeConver(clientType)).append("&advertiser_id=").append(uid).toString();
log.info("GdtTransTask url is {}", url);
String md5 = MD5.md5("862380036648114");
System.out.print(md5);
}
private static String clientTypeConver(String clientType) {
... ...