Authored by zhengwen.ge

品友

  1 +package com.yoho.unions.common.enums;
  2 +
  3 +/**
  4 + * 品友用户行为枚举
  5 + * Created by yoho on 2016/11/15.
  6 + */
  7 +public enum EnentValueEnum {
  8 +
  9 + ADD_CART("addCart","添加购物车"),
  10 + VIEW_SEARCH("viewSearch","搜索"),
  11 + VIEW_ITEM("viewItem","浏览商品");
  12 +
  13 + private String name;
  14 + private String name_ch;
  15 +
  16 + public String getName() {
  17 + return name;
  18 + }
  19 +
  20 + public String getName_ch() {
  21 + return name_ch;
  22 + }
  23 +
  24 + private EnentValueEnum(String name,String name_ch){
  25 + this.name = name;
  26 + this.name_ch=name_ch;
  27 + }
  28 +
  29 +
  30 +}
  1 +package com.yoho.unions.server.service;
  2 +
  3 +import com.yoho.service.model.union.request.TransPinYouRequestBO;
  4 +import com.yoho.service.model.union.request.ViewPinYouRequestBO;
  5 +import com.yoho.service.model.union.response.UnionResponse;
  6 +
  7 +/**
  8 + * 下单,浏览商品,加入购物车,搜索场景给品友发送回调
  9 + * Created by yoho on 2016/11/15.
  10 + */
  11 +public interface IPinYouService {
  12 +
  13 + /**
  14 + * 给品友发送访问数据
  15 + * @param requestBO
  16 + */
  17 + UnionResponse sendViem(ViewPinYouRequestBO requestBO);
  18 +
  19 + /**
  20 + * 给品友发送转化数据,主要是下单
  21 + */
  22 + UnionResponse sendTrans(TransPinYouRequestBO requestBO);
  23 +}
@@ -47,7 +47,7 @@ public class PinYouActServiceImpl extends UnionServiceImpl implements IUnionServ @@ -47,7 +47,7 @@ public class PinYouActServiceImpl extends UnionServiceImpl implements IUnionServ
47 private String urlEode(ActivateUnionRequestBO requestBO) { 47 private String urlEode(ActivateUnionRequestBO requestBO) {
48 String client_type = requestBO.getClient_type(); 48 String client_type = requestBO.getClient_type();
49 Integer currentTime = DateUtil.getCurrentTimeSecond(); 49 Integer currentTime = DateUtil.getCurrentTimeSecond();
50 - String pinYouUrl = "http://stats.ipinyou.com/madv?"; 50 + String pinYouUrl = "http://stats.ipinyou.com/mcdv?";
51 //广告主信息 51 //广告主信息
52 String advertiser = "MC.LF"; 52 String advertiser = "MC.LF";
53 String ip = requestBO.getClientIp(); 53 String ip = requestBO.getClientIp();
  1 +package com.yoho.unions.server.service.impl;
  2 +
  3 +import com.yoho.core.common.utils.MD5;
  4 +import com.yoho.service.model.union.request.TransPinYouRequestBO;
  5 +import com.yoho.service.model.union.request.ViewPinYouRequestBO;
  6 +import com.yoho.service.model.union.response.UnionResponse;
  7 +import com.yoho.unions.common.enums.ClientTypeEnum;
  8 +import com.yoho.unions.common.enums.EnentValueEnum;
  9 +import com.yoho.unions.common.utils.DateUtil;
  10 +import com.yoho.unions.common.utils.HttpUtils;
  11 +import com.yoho.unions.server.service.IPinYouService;
  12 +import org.apache.commons.lang.StringUtils;
  13 +import org.apache.commons.lang3.tuple.Pair;
  14 +import org.slf4j.Logger;
  15 +import org.slf4j.LoggerFactory;
  16 +import org.springframework.stereotype.Service;
  17 +
  18 +import java.net.URLDecoder;
  19 +
  20 +/**
  21 + * Created by yoho on 2016/11/15.
  22 + */
  23 +@Service
  24 +public class PinYouServiceImpl implements IPinYouService {
  25 +
  26 + static Logger log = LoggerFactory.getLogger(PinYouServiceImpl.class);
  27 +
  28 + static String PINYOU_VIEW_URL = "http://stats.ipinyou.com/madv?";
  29 +
  30 + static String PINYOU_TRANS_URL = "http://stats.ipinyou.com/mcdv?";
  31 +
  32 + @Override
  33 + /**
  34 + * jp:启动参数,固定位1
  35 + *df:debug flag 固定0
  36 + */
  37 + public UnionResponse sendViem(ViewPinYouRequestBO requestBO){
  38 + //广告主信息
  39 + String advertiser = "MC.LF";
  40 + //操作系统信息
  41 + String os = requestBO.getOs();
  42 + //根据从大数据获取的IDFA,IMEI来判断是安卓还是IOS
  43 + String client_type = ClientTypeEnum.IOS.getName();
  44 + String idfa = requestBO.getIdfa();
  45 + String imei = requestBO.getImei();
  46 + if(StringUtils.isEmpty(idfa)){
  47 + client_type = ClientTypeEnum.ANDROID.getName();
  48 + }
  49 + //时间戳
  50 + int currentTime = DateUtil.getCurrentTimeSecond();
  51 + //启动参数,固定位1
  52 + String jp = "1";
  53 + //用户id
  54 + String user_id = requestBO.getUser_id();
  55 + //用户行为事件
  56 + String event = requestBO.getEvent();
  57 + //用户行为数据
  58 + String event_value = requestBO.getEvent_value();
  59 + //客户端ip
  60 + String ip = requestBO.getIp();
  61 + //debug flag
  62 + String df = "0";
  63 + //按照品友的格式进行拼接
  64 + StringBuffer stringBuffer = new StringBuffer();
  65 + stringBuffer = stringBuffer.append(PINYOU_VIEW_URL).append("a=")
  66 + .append(advertiser).append("&os=").append(os)
  67 + .append("&ts=").append(currentTime)
  68 + .append("&jp=1").append("&ip=").append(ip)
  69 + .append("&event=").append(event)
  70 + .append("&event_vaule").append(event_value).append("&df=0");
  71 + String url = null;
  72 + if(event_value.equals(EnentValueEnum.ADD_CART.getName())||event_value.equals(EnentValueEnum.VIEW_ITEM.getName())){
  73 + stringBuffer= stringBuffer.append("&p").append(event_value);
  74 + }
  75 + if(StringUtils.isNotEmpty(user_id)){
  76 + stringBuffer = stringBuffer.append("&user_id").append(user_id);
  77 + }
  78 + if(client_type.equalsIgnoreCase(ClientTypeEnum.ANDROID.getName())){
  79 + //如果是浏览商品和加入购物车则需要传p=商品编号
  80 + String dim = MD5.md5(imei);
  81 + stringBuffer = stringBuffer.append("&dim=").append(dim);
  82 + }
  83 + if(client_type.equalsIgnoreCase(ClientTypeEnum.IOS.getName())){
  84 + String iam = MD5.md5(idfa);
  85 + stringBuffer = stringBuffer.append("&iam=").append(iam);
  86 + }
  87 + url = stringBuffer.toString();
  88 + UnionResponse response = sendUrl(url);
  89 + return response;
  90 + }
  91 +
  92 + public UnionResponse sendTrans(TransPinYouRequestBO requestBO){
  93 + //广告主信息
  94 + String advertiser = "MC.LF";
  95 + //操作系统信息
  96 + String os = requestBO.getOs();
  97 + //根据从大数据获取的IDFA,IMEI来判断是安卓还是IOS
  98 + String client_type = ClientTypeEnum.IOS.getName();
  99 + String idfa = requestBO.getIdfa();
  100 + String imei = requestBO.getImei();
  101 + if(StringUtils.isEmpty(idfa)){
  102 + client_type = ClientTypeEnum.ANDROID.getName();
  103 + }
  104 + //时间戳
  105 + int currentTime = DateUtil.getCurrentTimeSecond();
  106 + //启动参数,固定位1
  107 + String jp = "1";
  108 + //用户id
  109 + String user_id = requestBO.getUser_id();
  110 + //用户行为事件
  111 + String event = requestBO.getEvent();
  112 + //用户行为数据
  113 + String event_value = requestBO.getEvent_value();
  114 + //客户端ip
  115 + String ip = requestBO.getIp();
  116 + //debug flag
  117 + String df = "0";
  118 + StringBuffer stringBuffer = new StringBuffer();
  119 + stringBuffer = stringBuffer.append(PINYOU_TRANS_URL).append("a=")
  120 + .append(advertiser).append("&os=").append(os)
  121 + .append("&ts=").append(currentTime)
  122 + .append("&jp=1").append("&ip=").append(ip)
  123 + .append("&event=").append(event)
  124 + .append("&event_vaule").append(event_value).append("&df=0")
  125 + .append("&p=").append(event_value).append("&user_id").append(user_id);
  126 + String url = null;
  127 + if(client_type.equalsIgnoreCase(ClientTypeEnum.ANDROID.getName())){
  128 + //如果是浏览商品和加入购物车则需要传p=商品编号
  129 + String dim = MD5.md5(imei);
  130 + stringBuffer = stringBuffer.append("&dim=").append(dim);
  131 + }
  132 + if(client_type.equalsIgnoreCase(ClientTypeEnum.IOS.getName())){
  133 + String iam = MD5.md5(idfa);
  134 + stringBuffer = stringBuffer.append("&iam=").append(iam);
  135 + }
  136 + url = stringBuffer.toString();
  137 + UnionResponse response = sendUrl(url);
  138 + return response;
  139 + }
  140 +
  141 + private UnionResponse sendUrl(String url){
  142 + log.info("sendUrl url is {}",url);
  143 + try{
  144 + url = URLDecoder.decode(url, "UTF-8");
  145 + Pair<Integer, String> pair = HttpUtils.httpGet(url);
  146 + log.info("activateUnion call union success url={}, and result={}", url, pair);
  147 + if (pair.getLeft() != 200) {
  148 + log.warn("callback error with request={}", url);
  149 + return new UnionResponse(204, "callback error");
  150 + }
  151 + }catch (Exception e){
  152 + log.error("callback error with request={}", url, e.getMessage());
  153 + }
  154 + return new UnionResponse();
  155 + }
  156 +}
  1 +package com.yoho.unions.server.task;
  2 +
  3 +import com.yoho.service.model.union.request.TransPinYouRequestBO;
  4 +import com.yoho.service.model.union.request.ViewPinYouRequestBO;
  5 +import com.yoho.unions.common.redis.RedisListCache;
  6 +import com.yoho.unions.dal.model.UnionOrders;
  7 +import com.yoho.unions.server.service.IPinYouService;
  8 +import org.apache.commons.collections.CollectionUtils;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.scheduling.annotation.Scheduled;
  11 +import org.springframework.stereotype.Component;
  12 +
  13 +import javax.annotation.Resource;
  14 +import java.util.List;
  15 +
  16 +/**
  17 + * 定时从大数据的redis中捞取数据,然后推送给品友
  18 + * Created by yoho on 2016/11/15.
  19 + */
  20 +@Component
  21 +public class PinYouTask {
  22 +
  23 + @Autowired
  24 + private RedisListCache redisListCache;
  25 +
  26 + private static final String UNION_VIEW_KEY = "union:pinyou:view";
  27 +
  28 + private static final String UNION_TRANS_KEY = "union:pinyou:trans";
  29 +
  30 + @Resource
  31 + IPinYouService pinYouService;
  32 +
  33 + @Scheduled(cron = "0 0/10 * * * ?")
  34 + public void run(){
  35 + //从redis里面获取大数据的数据
  36 + List<ViewPinYouRequestBO> viewPinYouRequestBOList = redisListCache.rightPop(UNION_VIEW_KEY, List.class);
  37 + List<TransPinYouRequestBO> transPinYouRequestBOList = redisListCache.rightPop(UNION_TRANS_KEY,List.class);
  38 + //将取出来的值按照品友的要求发给品友
  39 + if(CollectionUtils.isNotEmpty(viewPinYouRequestBOList)){
  40 + for(ViewPinYouRequestBO requestBO:viewPinYouRequestBOList){
  41 + sendView(requestBO);
  42 + }
  43 + }
  44 + if(CollectionUtils.isNotEmpty(viewPinYouRequestBOList)){
  45 + for(TransPinYouRequestBO transPinYouRequestBO:transPinYouRequestBOList){
  46 + sendTrans(transPinYouRequestBO);
  47 + }
  48 + }
  49 + }
  50 +
  51 + //给品友推送访问数据
  52 + private void sendView(ViewPinYouRequestBO requestBO){
  53 + pinYouService.sendViem(requestBO);
  54 + }
  55 +
  56 + //给品友推送转化数据
  57 + private void sendTrans(TransPinYouRequestBO requestBO){
  58 + pinYouService.sendTrans(requestBO);
  59 + }
  60 +}