Authored by zhengwen.ge

update

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);
}
}
}
... ...
... ... @@ -8,13 +8,13 @@ import java.math.BigDecimal;
public class UnionActivityTrans {
//订单金额
private BigDecimal orderAmount;
private BigDecimal order_amount;
//设备号
private String udid;
private String uid;
private String ordercode;
private String order_code;
public String getUid() {
return uid;
... ... @@ -24,27 +24,27 @@ public class UnionActivityTrans {
this.uid = uid;
}
public String getOrdercode() {
return ordercode;
public String getUdid() {
return udid;
}
public void setOrdercode(String ordercode) {
this.ordercode = ordercode;
public void setUdid(String udid) {
this.udid = udid;
}
public BigDecimal getOrderAmount() {
return orderAmount;
public BigDecimal getOrder_amount() {
return order_amount;
}
public void setOrderAmount(BigDecimal orderAmount) {
this.orderAmount = orderAmount;
public void setOrder_amount(BigDecimal order_amount) {
this.order_amount = order_amount;
}
public String getUdid() {
return udid;
public String getOrder_code() {
return order_code;
}
public void setUdid(String udid) {
this.udid = udid;
public void setOrder_code(String order_code) {
this.order_code = order_code;
}
}
... ...
... ... @@ -13,16 +13,12 @@
<sql id="Base_Column_List" >
id, udid, union_type, client_type, advertiser_id, click_id, create_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
<select id="selectByUdid" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from union_activity
where id = #{id,jdbcType=BIGINT}
where udid = #{udid} limit 1
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from union_activity
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.yoho.unions.dal.model.UnionActivity" >
insert into union_activity (id, udid, union_type,
client_type, advertiser_id, click_id,
... ...
... ... @@ -29,13 +29,13 @@ 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_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_TRANS_UNIONTYPE = "YH:BD:UNI:1:ORDER:";
private static final String UNION_NORMAL_TRANS_UNIONTYPE = "YH_BD_UNI_ALL_ORDER_";
private static final String UNION_NORMAL_TRANS_UNIONTYPE = "YH:BD:UNI:ALL:ORDER:";
@Autowired
private RedisListCache redisListCache;
... ... @@ -77,35 +77,46 @@ public class UnionOrderServiceImpl implements IUnionOrderService {
}
@Override
public List<TransInfo> getTrandInfo(UnionOrderReqVO req){
public List<TransInfo> getTrandInfo(UnionOrderReqVO req) {
logger.info("Enter getTrandInfo: request param is {}", req);
int limit = req.getLimit();
limit = limit < 1 ? 20 : limit;
try{
try {
List<TransInfo> resList = new ArrayList<TransInfo>();
Long size = redisListCache.size(UNION_TRAND);
int sizeInt = size == null ? 0 : size.intValue();
limit = sizeInt < limit ? sizeInt : limit;
for(int i=0; i < limit; i++){
UnionTrans unionTrans = redisListCache.rightPop(UNION_TRAND, UnionTrans.class);
logger.info("getTrandInfo: get order list form bigdata redis: key is {}, result is {}", UNION_ORDER_KEY, unionTrans);
if(unionTrans == null){
continue;
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);
}
String union_type = unionTrans.getUnion_type();
//首单交寄
String firstOrder = UNION_TRANS_UNIONTYPE + union_type;
//普通交寄
String normalOrder = UNION_NORMAL_TRANS_UNIONTYPE+union_type;
List<UnionActivityTrans> firstOrderList = redisListCache.rightPop(firstOrder, List.class);
logger.info("getFirstOrderList: from bigdata redis: key is {}, result size is {}", firstOrder, firstOrderList == null ? 0 : firstOrderList.size());
List<UnionActivityTrans> normalOrderList = redisListCache.rightPop(normalOrder,List.class);
logger.info("getNormalOrderList: from bigdata redis: key is {}, result size is {}", normalOrder, normalOrderList == null ? 0 : normalOrderList.size());
resList = this.convertTrans(firstOrderList,normalOrderList);
}
return resList;
}catch(Exception e){
} catch (Exception e) {
logger.warn("getUnionOrders: get orders from bigdata redis failed, error is {}", e);
return null;
}
... ... @@ -158,21 +169,21 @@ public class UnionOrderServiceImpl implements IUnionOrderService {
return resList;
}
private List<TransInfo> convertTrans(List<UnionActivityTrans> firstOrderList,List<UnionActivityTrans> normalOrderList){
private List<TransInfo> convertTrans(List<TransInfo> resList,List<UnionActivityTrans> firstOrderList,List<UnionActivityTrans> normalOrderList){
if(CollectionUtils.isEmpty(firstOrderList)&&CollectionUtils.isEmpty(normalOrderList)){
return null;
}
List<TransInfo> transInfoList = new ArrayList<TransInfo>();
if(CollectionUtils.isNotEmpty(firstOrderList)){
for(UnionActivityTrans unionActivityTrans:firstOrderList){
TransInfo transInfo = new TransInfo();
//首单设为1
transInfo.setType("1");
transInfo.setUdid(unionActivityTrans.getUdid());
transInfo.setOrderAmount(unionActivityTrans.getOrderAmount());
transInfo.setOrderAmount(unionActivityTrans.getOrder_amount());
transInfo.setUid(unionActivityTrans.getUid());
transInfo.setOrdercode(unionActivityTrans.getOrdercode());
transInfoList.add(transInfo);
transInfo.setOrdercode(unionActivityTrans.getOrder_code());
resList.add(transInfo);
}
}
if(CollectionUtils.isNotEmpty(normalOrderList)){
... ... @@ -181,12 +192,12 @@ public class UnionOrderServiceImpl implements IUnionOrderService {
//正常单设为2
transInfo.setType("2");
transInfo.setUdid(unionActivityTran.getUdid());
transInfo.setOrderAmount(unionActivityTran.getOrderAmount());
transInfo.setOrderAmount(unionActivityTran.getOrder_amount());
transInfo.setUid(unionActivityTran.getUid());
transInfo.setOrdercode(unionActivityTran.getOrdercode());
transInfoList.add(transInfo);
transInfo.setOrdercode(unionActivityTran.getOrder_code());
resList.add(transInfo);
}
}
return transInfoList;
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
... ... @@ -185,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) {
... ... @@ -511,7 +509,10 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
return new UnionResponse(204, "callback error");
}
//如果来源是广点通,则把广点通的一些信息记入表,给之后做转化上报使用
saveUnionActivity(click,request);
if(union_type.equals("3")){
saveUnionActivity(click,request);
}
} catch (Exception e) {
log.error("callback error with request={}", request, e);
... ... @@ -583,6 +584,7 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
unionActivity.setAdvertiserId(click.getCommonUse());
//广点通点击id
unionActivity.setClickId(click.getClickId());
unionActivity.setCreateTime(DateUtil.getCurrentTimeSecond());
unionActivityDAO.insertSelective(unionActivity);
}
});
... ...
... ... @@ -64,6 +64,10 @@ public class GdtTransTask {
String udid = transInfo.getUdid();
//利用udid去表中查出是两个广点通账号带来的
UnionActivity unionActivity = unionActivityDAO.selectByUdid(udid);
if(unionActivity==null){
return;
}
//按照广点通要求拼接数据
String url = urlEode(unionActivity,transInfo);
//改成httpclient方式调用
... ...
... ... @@ -42,5 +42,6 @@ datasources:
- com.yoho.unions.dal.IUnionTypeMatchDAO
- com.yoho.unions.dal.ITencentMktActivityDAO
- com.yoho.unions.dal.ITencentMktCouponHistoryDAO
- com.yoho.unions.dal.IUnionActivityDAO
readOnlyInSlave: true
\ No newline at end of file
... ...
... ... @@ -43,5 +43,6 @@ datasources:
- com.yoho.unions.dal.IUnionTypeMatchDAO
- com.yoho.unions.dal.ITencentMktActivityDAO
- com.yoho.unions.dal.ITencentMktCouponHistoryDAO
- com.yoho.unions.dal.IUnionActivityDAO
readOnlyInSlave: true
\ No newline at end of file
... ...