Authored by zhengwen.ge

品友

package com.yoho.unions.common.enums;
/**
* 品友用户行为枚举
* Created by yoho on 2016/11/15.
*/
public enum EnentValueEnum {
ADD_CART("addCart","添加购物车"),
VIEW_SEARCH("viewSearch","搜索"),
VIEW_ITEM("viewItem","浏览商品");
private String name;
private String name_ch;
public String getName() {
return name;
}
public String getName_ch() {
return name_ch;
}
private EnentValueEnum(String name,String name_ch){
this.name = name;
this.name_ch=name_ch;
}
}
... ...
package com.yoho.unions.server.service;
import com.yoho.service.model.union.request.TransPinYouRequestBO;
import com.yoho.service.model.union.request.ViewPinYouRequestBO;
import com.yoho.service.model.union.response.UnionResponse;
/**
* 下单,浏览商品,加入购物车,搜索场景给品友发送回调
* Created by yoho on 2016/11/15.
*/
public interface IPinYouService {
/**
* 给品友发送访问数据
* @param requestBO
*/
UnionResponse sendViem(ViewPinYouRequestBO requestBO);
/**
* 给品友发送转化数据,主要是下单
*/
UnionResponse sendTrans(TransPinYouRequestBO requestBO);
}
... ...
... ... @@ -47,7 +47,7 @@ public class PinYouActServiceImpl extends UnionServiceImpl implements IUnionServ
private String urlEode(ActivateUnionRequestBO requestBO) {
String client_type = requestBO.getClient_type();
Integer currentTime = DateUtil.getCurrentTimeSecond();
String pinYouUrl = "http://stats.ipinyou.com/madv?";
String pinYouUrl = "http://stats.ipinyou.com/mcdv?";
//广告主信息
String advertiser = "MC.LF";
String ip = requestBO.getClientIp();
... ...
package com.yoho.unions.server.service.impl;
import com.yoho.core.common.utils.MD5;
import com.yoho.service.model.union.request.TransPinYouRequestBO;
import com.yoho.service.model.union.request.ViewPinYouRequestBO;
import com.yoho.service.model.union.response.UnionResponse;
import com.yoho.unions.common.enums.ClientTypeEnum;
import com.yoho.unions.common.enums.EnentValueEnum;
import com.yoho.unions.common.utils.DateUtil;
import com.yoho.unions.common.utils.HttpUtils;
import com.yoho.unions.server.service.IPinYouService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.net.URLDecoder;
/**
* Created by yoho on 2016/11/15.
*/
@Service
public class PinYouServiceImpl implements IPinYouService {
static Logger log = LoggerFactory.getLogger(PinYouServiceImpl.class);
static String PINYOU_VIEW_URL = "http://stats.ipinyou.com/madv?";
static String PINYOU_TRANS_URL = "http://stats.ipinyou.com/mcdv?";
@Override
/**
* jp:启动参数,固定位1
*df:debug flag 固定0
*/
public UnionResponse sendViem(ViewPinYouRequestBO requestBO){
//广告主信息
String advertiser = "MC.LF";
//操作系统信息
String os = requestBO.getOs();
//根据从大数据获取的IDFA,IMEI来判断是安卓还是IOS
String client_type = ClientTypeEnum.IOS.getName();
String idfa = requestBO.getIdfa();
String imei = requestBO.getImei();
if(StringUtils.isEmpty(idfa)){
client_type = ClientTypeEnum.ANDROID.getName();
}
//时间戳
int currentTime = DateUtil.getCurrentTimeSecond();
//启动参数,固定位1
String jp = "1";
//用户id
String user_id = requestBO.getUser_id();
//用户行为事件
String event = requestBO.getEvent();
//用户行为数据
String event_value = requestBO.getEvent_value();
//客户端ip
String ip = requestBO.getIp();
//debug flag
String df = "0";
//按照品友的格式进行拼接
StringBuffer stringBuffer = new StringBuffer();
stringBuffer = stringBuffer.append(PINYOU_VIEW_URL).append("a=")
.append(advertiser).append("&os=").append(os)
.append("&ts=").append(currentTime)
.append("&jp=1").append("&ip=").append(ip)
.append("&event=").append(event)
.append("&event_vaule").append(event_value).append("&df=0");
String url = null;
if(event_value.equals(EnentValueEnum.ADD_CART.getName())||event_value.equals(EnentValueEnum.VIEW_ITEM.getName())){
stringBuffer= stringBuffer.append("&p").append(event_value);
}
if(StringUtils.isNotEmpty(user_id)){
stringBuffer = stringBuffer.append("&user_id").append(user_id);
}
if(client_type.equalsIgnoreCase(ClientTypeEnum.ANDROID.getName())){
//如果是浏览商品和加入购物车则需要传p=商品编号
String dim = MD5.md5(imei);
stringBuffer = stringBuffer.append("&dim=").append(dim);
}
if(client_type.equalsIgnoreCase(ClientTypeEnum.IOS.getName())){
String iam = MD5.md5(idfa);
stringBuffer = stringBuffer.append("&iam=").append(iam);
}
url = stringBuffer.toString();
UnionResponse response = sendUrl(url);
return response;
}
public UnionResponse sendTrans(TransPinYouRequestBO requestBO){
//广告主信息
String advertiser = "MC.LF";
//操作系统信息
String os = requestBO.getOs();
//根据从大数据获取的IDFA,IMEI来判断是安卓还是IOS
String client_type = ClientTypeEnum.IOS.getName();
String idfa = requestBO.getIdfa();
String imei = requestBO.getImei();
if(StringUtils.isEmpty(idfa)){
client_type = ClientTypeEnum.ANDROID.getName();
}
//时间戳
int currentTime = DateUtil.getCurrentTimeSecond();
//启动参数,固定位1
String jp = "1";
//用户id
String user_id = requestBO.getUser_id();
//用户行为事件
String event = requestBO.getEvent();
//用户行为数据
String event_value = requestBO.getEvent_value();
//客户端ip
String ip = requestBO.getIp();
//debug flag
String df = "0";
StringBuffer stringBuffer = new StringBuffer();
stringBuffer = stringBuffer.append(PINYOU_TRANS_URL).append("a=")
.append(advertiser).append("&os=").append(os)
.append("&ts=").append(currentTime)
.append("&jp=1").append("&ip=").append(ip)
.append("&event=").append(event)
.append("&event_vaule").append(event_value).append("&df=0")
.append("&p=").append(event_value).append("&user_id").append(user_id);
String url = null;
if(client_type.equalsIgnoreCase(ClientTypeEnum.ANDROID.getName())){
//如果是浏览商品和加入购物车则需要传p=商品编号
String dim = MD5.md5(imei);
stringBuffer = stringBuffer.append("&dim=").append(dim);
}
if(client_type.equalsIgnoreCase(ClientTypeEnum.IOS.getName())){
String iam = MD5.md5(idfa);
stringBuffer = stringBuffer.append("&iam=").append(iam);
}
url = stringBuffer.toString();
UnionResponse response = sendUrl(url);
return response;
}
private UnionResponse sendUrl(String url){
log.info("sendUrl url is {}",url);
try{
url = URLDecoder.decode(url, "UTF-8");
Pair<Integer, String> pair = HttpUtils.httpGet(url);
log.info("activateUnion call union success url={}, and result={}", url, pair);
if (pair.getLeft() != 200) {
log.warn("callback error with request={}", url);
return new UnionResponse(204, "callback error");
}
}catch (Exception e){
log.error("callback error with request={}", url, e.getMessage());
}
return new UnionResponse();
}
}
... ...
package com.yoho.unions.server.task;
import com.yoho.service.model.union.request.TransPinYouRequestBO;
import com.yoho.service.model.union.request.ViewPinYouRequestBO;
import com.yoho.unions.common.redis.RedisListCache;
import com.yoho.unions.dal.model.UnionOrders;
import com.yoho.unions.server.service.IPinYouService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* 定时从大数据的redis中捞取数据,然后推送给品友
* Created by yoho on 2016/11/15.
*/
@Component
public class PinYouTask {
@Autowired
private RedisListCache redisListCache;
private static final String UNION_VIEW_KEY = "union:pinyou:view";
private static final String UNION_TRANS_KEY = "union:pinyou:trans";
@Resource
IPinYouService pinYouService;
@Scheduled(cron = "0 0/10 * * * ?")
public void run(){
//从redis里面获取大数据的数据
List<ViewPinYouRequestBO> viewPinYouRequestBOList = redisListCache.rightPop(UNION_VIEW_KEY, List.class);
List<TransPinYouRequestBO> transPinYouRequestBOList = redisListCache.rightPop(UNION_TRANS_KEY,List.class);
//将取出来的值按照品友的要求发给品友
if(CollectionUtils.isNotEmpty(viewPinYouRequestBOList)){
for(ViewPinYouRequestBO requestBO:viewPinYouRequestBOList){
sendView(requestBO);
}
}
if(CollectionUtils.isNotEmpty(viewPinYouRequestBOList)){
for(TransPinYouRequestBO transPinYouRequestBO:transPinYouRequestBOList){
sendTrans(transPinYouRequestBO);
}
}
}
//给品友推送访问数据
private void sendView(ViewPinYouRequestBO requestBO){
pinYouService.sendViem(requestBO);
}
//给品友推送转化数据
private void sendTrans(TransPinYouRequestBO requestBO){
pinYouService.sendTrans(requestBO);
}
}
... ...