Authored by zhengwen.ge

去掉品友上报

package com.yoho.unions.kafka.consumer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.unions.kafka.consumer.handler.ClickEventHandIntf;
@Service
public class ClickActionDispatcher {
private static final Logger logger = LoggerFactory.getLogger(ClickActionDispatcher.class);
/**
* 只有bean初始化时修改,后续只作读取,线程安全
*/
private Map<String, List<ClickEventHandIntf>> handlerMap = new HashMap<>();
/**
* handler注册
* @param eventKey
* @param handler
*/
synchronized public void register(String eventKey, ClickEventHandIntf handler) {
if(eventKey == null || handler == null) {
logger.warn("invalid click event handler, key: {}, handler:{}", eventKey, handler);
return;
}
List<ClickEventHandIntf> handlerList = handlerMap.get(eventKey);
if(handlerList == null) {
handlerList = new ArrayList<>();
handlerMap.put(eventKey, handlerList);
}
handlerList.add(handler);
logger.info("register handler, event: {}, handler: {}", eventKey, handler);
}
/**
* 点击事件分发处理
* @param eventJson
*/
public void dispatch(JSONObject eventJson) {
if(eventJson == null)
return;
String op = eventJson.getString("op");
if(op == null)
return;
List<ClickEventHandIntf> handlerList = handlerMap.get(op.toUpperCase());
if(CollectionUtils.isEmpty(handlerList))
return;
for(ClickEventHandIntf handler : handlerList) {
handler.process(eventJson);
}
}
}
//package com.yoho.unions.kafka.consumer;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import org.apache.commons.collections.CollectionUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Service;
//
//import com.alibaba.fastjson.JSONObject;
//import com.yoho.unions.kafka.consumer.handler.ClickEventHandIntf;
//
//
//@Service
//public class ClickActionDispatcher {
// private static final Logger logger = LoggerFactory.getLogger(ClickActionDispatcher.class);
//
// /**
// * 只有bean初始化时修改,后续只作读取,线程安全
// */
// private Map<String, List<ClickEventHandIntf>> handlerMap = new HashMap<>();
//
// /**
// * handler注册
// * @param eventKey
// * @param handler
// */
// synchronized public void register(String eventKey, ClickEventHandIntf handler) {
// if(eventKey == null || handler == null) {
// logger.warn("invalid click event handler, key: {}, handler:{}", eventKey, handler);
// return;
// }
//
// List<ClickEventHandIntf> handlerList = handlerMap.get(eventKey);
// if(handlerList == null) {
// handlerList = new ArrayList<>();
// handlerMap.put(eventKey, handlerList);
// }
//
// handlerList.add(handler);
// logger.info("register handler, event: {}, handler: {}", eventKey, handler);
// }
//
// /**
// * 点击事件分发处理
// * @param eventJson
// */
// public void dispatch(JSONObject eventJson) {
// if(eventJson == null)
// return;
//
// String op = eventJson.getString("op");
// if(op == null)
// return;
//
// List<ClickEventHandIntf> handlerList = handlerMap.get(op.toUpperCase());
// if(CollectionUtils.isEmpty(handlerList))
// return;
//
// for(ClickEventHandIntf handler : handlerList) {
// handler.process(eventJson);
// }
// }
//}
... ...
package com.yoho.unions.kafka.consumer.handler;
import com.alibaba.fastjson.JSONObject;
public interface ClickEventHandIntf {
void process(JSONObject actionData);
}
//package com.yoho.unions.kafka.consumer.handler;
//
//import com.alibaba.fastjson.JSONObject;
//
//public interface ClickEventHandIntf {
//
// void process(JSONObject actionData);
//
//}
... ...
package com.yoho.unions.kafka.consumer.handler;
import java.math.BigDecimal;
import java.util.List;
import javax.annotation.PostConstruct;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.service.model.union.request.TransPinYouRequestBO;
import com.yoho.service.model.union.request.ViewPinYouRequestBO;
import com.yoho.unions.dal.BaseOrderShipmentUdidMapper;
import com.yoho.unions.dal.ErpOrdersGoodsMapper;
import com.yoho.unions.dal.ErpOrdersMapper;
import com.yoho.unions.dal.model.BaseOrderShipmentUdid;
import com.yoho.unions.dal.model.ErpOrders;
import com.yoho.unions.dal.model.ErpOrdersGoods;
import com.yoho.unions.kafka.consumer.ClickActionDispatcher;
import com.yoho.unions.kafka.consumer.ClickEventConstants;
import com.yoho.unions.server.service.IPinYouService;
/**
* 品友行为上报
* @author yoho
*
*/
@Service
public class PinyouReportHandler implements ClickEventHandIntf {
private static final Logger logger = LoggerFactory.getLogger(PinyouReportHandler.class);
@Autowired
private ClickActionDispatcher dispatcher;
@Autowired
IPinYouService pinYouService;
@Autowired
private ErpOrdersMapper erpOrdersMapper;
@Autowired
private ErpOrdersGoodsMapper erpOrdersGoodsMapper;
@Autowired
private BaseOrderShipmentUdidMapper shipOrderUdidMapper;
@PostConstruct
public void init() {
dispatcher.register(ClickEventConstants.YB_MAIN_S, this);
dispatcher.register(ClickEventConstants.YB_GDS_DT_INFO, this);
dispatcher.register(ClickEventConstants.YB_GDS_DT_SEL, this);
dispatcher.register(ClickEventConstants.YB_SC_ORD, this);
}
@Override
public void process(JSONObject actionData) {
logger.info("click event: {}", actionData.toJSONString());
//3个月内存在已交寄订单,则不上报数据
if(hasShipOrdersLast3Months(actionData.getString("udid"))) {
logger.info("event ignore for shipped order in 3 month, udid: {}", actionData.getString("udid"));
return;
}
String opr = actionData.getString("op") == null ? "" : actionData.getString("op").toUpperCase();
switch(opr) {
case ClickEventConstants.YB_MAIN_S:
case ClickEventConstants.YB_GDS_DT_INFO:
case ClickEventConstants.YB_GDS_DT_SEL:
ViewPinYouRequestBO viewBO = getPinyouViewData(opr, actionData);
pinYouService.sendView(viewBO);
break;
case ClickEventConstants.YB_SC_ORD:
TransPinYouRequestBO transBO = getPinyouTransData(opr, actionData);
pinYouService.sendTrans(transBO);
break;
}
}
/**
* 解析品友行为数据
* @param opr
* @param actionData
* @return
*/
private ViewPinYouRequestBO getPinyouViewData(String opr, JSONObject actionData) {
JSONObject paramData = actionData.getJSONObject("param");
String eventName = null;
String eventValue = null;
switch(opr) {
case ClickEventConstants.YB_MAIN_S:
eventName = "viewSearch";
eventValue = paramData == null ? "" : paramData.getString("KW");
break;
case ClickEventConstants.YB_GDS_DT_INFO:
eventName = "viewItem";
eventValue = paramData == null ? "" : paramData.getString("PRD_ID");
break;
case ClickEventConstants.YB_GDS_DT_SEL:
eventName = "addCart";
eventValue = paramData == null ? "" : paramData.getString("PRD_SKC");
break;
}
if(eventName == null || eventValue == null)
return null;
ViewPinYouRequestBO viewRequest = new ViewPinYouRequestBO();
viewRequest.setEvent(eventName);
viewRequest.setEvent_value(eventValue);
viewRequest.setUser_id(actionData.getString("uid"));
viewRequest.setOs(actionData.getString("os"));
viewRequest.setIp(actionData.getString("ip"));
viewRequest.setIdfa(actionData.getString("idfa"));
viewRequest.setImei(actionData.getString("imei"));
viewRequest.setTs(actionData.getString("ts"));
return viewRequest;
}
/**
* 解析品友转化数据
* @param actionData
* @return
*/
private TransPinYouRequestBO getPinyouTransData(String opr, JSONObject actionData) {
if(!StringUtils.equalsIgnoreCase(ClickEventConstants.YB_SC_ORD, opr)) {
return null;
}
TransPinYouRequestBO transRequest = new TransPinYouRequestBO();
String orderCode = actionData.getJSONObject("param") == null ? null : actionData.getJSONObject("param").getString("ORD_NUM");
String uid = actionData.getString("uid");
if(orderCode == null || uid == null) {
return null;
}
ErpOrders orders = erpOrdersMapper.selectByOrderCode(Long.valueOf(orderCode));
List<ErpOrdersGoods> orderGoodsList = erpOrdersGoodsMapper.selectByOrderCode(Long.valueOf(orderCode));
if(orders == null || CollectionUtils.isEmpty(orderGoodsList)) {
return null;
}
StringBuilder plistBuf = new StringBuilder();
int count = 0;
for(ErpOrdersGoods ordersGoods : orderGoodsList) {
if(count > 0)
plistBuf.append(":");
plistBuf.append(ordersGoods.getProductSkn()).append(",").append(ordersGoods.getBuyNumber());
count++;
}
transRequest.setEvent("createOrder");
transRequest.setEvent_value(orderCode);
transRequest.setUser_id(actionData.getString("uid"));
transRequest.setOs(actionData.getString("os"));
transRequest.setIp(actionData.getString("ip"));
transRequest.setIdfa(actionData.getString("idfa"));
transRequest.setImei(actionData.getString("imei"));
transRequest.setTs(actionData.getString("ts"));
transRequest.setOrder(orderCode);
transRequest.setMoney(orders.getLastOrderAmount().setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
transRequest.setPlist(plistBuf.toString());
transRequest.setOrderStatus("Y".equals(orders.getPaymentStatus()) ? "1" : "0");
return transRequest;
}
/**
*
* @param udid
* @return
*/
private boolean hasShipOrdersLast3Months(String udid) {
/**
* BASE_ORDER_SHIPMENT_UDID表为大数据离线统计表,如果udid在该表存在记录,表明过去三个月存在已交寄订单
*/
if(StringUtils.isEmpty(udid))
return false;
List<BaseOrderShipmentUdid> udidShipOrders = shipOrderUdidMapper.selectByUdid(udid);
return CollectionUtils.isNotEmpty(udidShipOrders);
}
}
//package com.yoho.unions.kafka.consumer.handler;
//
//import java.math.BigDecimal;
//import java.util.List;
//
//import javax.annotation.PostConstruct;
//
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import com.alibaba.fastjson.JSONObject;
//import com.yoho.service.model.union.request.TransPinYouRequestBO;
//import com.yoho.service.model.union.request.ViewPinYouRequestBO;
//import com.yoho.unions.dal.BaseOrderShipmentUdidMapper;
//import com.yoho.unions.dal.ErpOrdersGoodsMapper;
//import com.yoho.unions.dal.ErpOrdersMapper;
//import com.yoho.unions.dal.model.BaseOrderShipmentUdid;
//import com.yoho.unions.dal.model.ErpOrders;
//import com.yoho.unions.dal.model.ErpOrdersGoods;
//import com.yoho.unions.kafka.consumer.ClickActionDispatcher;
//import com.yoho.unions.kafka.consumer.ClickEventConstants;
//import com.yoho.unions.server.service.IPinYouService;
//
///**
// * 品友行为上报
// * @author yoho
// *
// */
//@Service
//public class PinyouReportHandler implements ClickEventHandIntf {
//
// private static final Logger logger = LoggerFactory.getLogger(PinyouReportHandler.class);
//
// @Autowired
// private ClickActionDispatcher dispatcher;
//
// @Autowired
// IPinYouService pinYouService;
//
// @Autowired
// private ErpOrdersMapper erpOrdersMapper;
//
// @Autowired
// private ErpOrdersGoodsMapper erpOrdersGoodsMapper;
//
// @Autowired
// private BaseOrderShipmentUdidMapper shipOrderUdidMapper;
//
// @PostConstruct
// public void init() {
// dispatcher.register(ClickEventConstants.YB_MAIN_S, this);
// dispatcher.register(ClickEventConstants.YB_GDS_DT_INFO, this);
// dispatcher.register(ClickEventConstants.YB_GDS_DT_SEL, this);
// dispatcher.register(ClickEventConstants.YB_SC_ORD, this);
// }
//
// @Override
// public void process(JSONObject actionData) {
// logger.info("click event: {}", actionData.toJSONString());
//
// //3个月内存在已交寄订单,则不上报数据
// if(hasShipOrdersLast3Months(actionData.getString("udid"))) {
// logger.info("event ignore for shipped order in 3 month, udid: {}", actionData.getString("udid"));
// return;
// }
//
// String opr = actionData.getString("op") == null ? "" : actionData.getString("op").toUpperCase();
// switch(opr) {
// case ClickEventConstants.YB_MAIN_S:
// case ClickEventConstants.YB_GDS_DT_INFO:
// case ClickEventConstants.YB_GDS_DT_SEL:
// ViewPinYouRequestBO viewBO = getPinyouViewData(opr, actionData);
// pinYouService.sendView(viewBO);
// break;
//
// case ClickEventConstants.YB_SC_ORD:
// TransPinYouRequestBO transBO = getPinyouTransData(opr, actionData);
// pinYouService.sendTrans(transBO);
// break;
// }
// }
//
// /**
// * 解析品友行为数据
// * @param opr
// * @param actionData
// * @return
// */
// private ViewPinYouRequestBO getPinyouViewData(String opr, JSONObject actionData) {
// JSONObject paramData = actionData.getJSONObject("param");
// String eventName = null;
// String eventValue = null;
// switch(opr) {
// case ClickEventConstants.YB_MAIN_S:
// eventName = "viewSearch";
// eventValue = paramData == null ? "" : paramData.getString("KW");
// break;
//
// case ClickEventConstants.YB_GDS_DT_INFO:
// eventName = "viewItem";
// eventValue = paramData == null ? "" : paramData.getString("PRD_ID");
// break;
//
// case ClickEventConstants.YB_GDS_DT_SEL:
// eventName = "addCart";
// eventValue = paramData == null ? "" : paramData.getString("PRD_SKC");
// break;
// }
//
// if(eventName == null || eventValue == null)
// return null;
//
// ViewPinYouRequestBO viewRequest = new ViewPinYouRequestBO();
// viewRequest.setEvent(eventName);
// viewRequest.setEvent_value(eventValue);
// viewRequest.setUser_id(actionData.getString("uid"));
// viewRequest.setOs(actionData.getString("os"));
// viewRequest.setIp(actionData.getString("ip"));
// viewRequest.setIdfa(actionData.getString("idfa"));
// viewRequest.setImei(actionData.getString("imei"));
// viewRequest.setTs(actionData.getString("ts"));
//
// return viewRequest;
// }
//
//
// /**
// * 解析品友转化数据
// * @param actionData
// * @return
// */
// private TransPinYouRequestBO getPinyouTransData(String opr, JSONObject actionData) {
// if(!StringUtils.equalsIgnoreCase(ClickEventConstants.YB_SC_ORD, opr)) {
// return null;
// }
//
// TransPinYouRequestBO transRequest = new TransPinYouRequestBO();
// String orderCode = actionData.getJSONObject("param") == null ? null : actionData.getJSONObject("param").getString("ORD_NUM");
// String uid = actionData.getString("uid");
// if(orderCode == null || uid == null) {
// return null;
// }
//
// ErpOrders orders = erpOrdersMapper.selectByOrderCode(Long.valueOf(orderCode));
// List<ErpOrdersGoods> orderGoodsList = erpOrdersGoodsMapper.selectByOrderCode(Long.valueOf(orderCode));
// if(orders == null || CollectionUtils.isEmpty(orderGoodsList)) {
// return null;
// }
//
// StringBuilder plistBuf = new StringBuilder();
// int count = 0;
// for(ErpOrdersGoods ordersGoods : orderGoodsList) {
// if(count > 0)
// plistBuf.append(":");
// plistBuf.append(ordersGoods.getProductSkn()).append(",").append(ordersGoods.getBuyNumber());
// count++;
// }
//
// transRequest.setEvent("createOrder");
// transRequest.setEvent_value(orderCode);
// transRequest.setUser_id(actionData.getString("uid"));
// transRequest.setOs(actionData.getString("os"));
// transRequest.setIp(actionData.getString("ip"));
// transRequest.setIdfa(actionData.getString("idfa"));
// transRequest.setImei(actionData.getString("imei"));
// transRequest.setTs(actionData.getString("ts"));
//
// transRequest.setOrder(orderCode);
// transRequest.setMoney(orders.getLastOrderAmount().setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
// transRequest.setPlist(plistBuf.toString());
// transRequest.setOrderStatus("Y".equals(orders.getPaymentStatus()) ? "1" : "0");
//
// return transRequest;
// }
//
// /**
// *
// * @param udid
// * @return
// */
// private boolean hasShipOrdersLast3Months(String udid) {
// /**
// * BASE_ORDER_SHIPMENT_UDID表为大数据离线统计表,如果udid在该表存在记录,表明过去三个月存在已交寄订单
// */
// if(StringUtils.isEmpty(udid))
// return false;
//
// List<BaseOrderShipmentUdid> udidShipOrders = shipOrderUdidMapper.selectByUdid(udid);
// return CollectionUtils.isNotEmpty(udidShipOrders);
// }
//}
... ...