|
|
|
|
|
package com.yohoufo.order.mq.consumer;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yoho.core.rabbitmq.YhConsumer;
|
|
|
import com.yohoufo.common.constant.ExpressInfoConstant;
|
|
|
import com.yohoufo.common.utils.DateUtil;
|
|
|
import com.yohoufo.dal.order.BuyerOrderMapper;
|
|
|
import com.yohoufo.dal.order.ExpressInfoMapper;
|
|
|
import com.yohoufo.dal.order.SellerOrderMapper;
|
|
|
import com.yohoufo.dal.order.model.BuyerOrder;
|
|
|
import com.yohoufo.dal.order.model.ExpressInfo;
|
|
|
import com.yohoufo.dal.order.model.SellerOrder;
|
|
|
import com.yohoufo.order.service.IExpressInfoService;
|
|
|
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.Component;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
@Component
|
|
|
public class ExpressInfoUpdateConsumer implements YhConsumer {
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(ExpressInfoUpdateConsumer.class);
|
|
|
|
|
|
|
|
|
private final String topic = "order.updateExpressInfo";
|
|
|
|
|
|
@Autowired
|
|
|
private ExpressInfoMapper expressInfoMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private SellerOrderMapper sellerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private BuyerOrderMapper buyerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private IExpressInfoService expressInfoService;
|
|
|
|
|
|
|
|
|
|
|
|
public String getMessageTopic() {
|
|
|
return topic;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void handleMessage(Object message) {
|
|
|
try {
|
|
|
LOGGER.info("begin handle express info update message, message is {}.", message);
|
|
|
updateExpressInfo(message);
|
|
|
LOGGER.info("handle express info update message success, message is {}.", message);
|
|
|
} catch (Exception e) {
|
|
|
//消费失败上报到influxdb 暂时不需要
|
|
|
//consumerExceptionPublisher.pushlishEvent(getMessageTopic(), e);
|
|
|
LOGGER.warn("handle express info update message fail, message is {}.", message);
|
|
|
LOGGER.error("handleMessage error = ", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void updateExpressInfo(Object message) {
|
|
|
JSONObject jsonMsg = JSONObject.parseObject(message.toString());
|
|
|
if (!checkValidMessage(jsonMsg)) {
|
|
|
return;
|
|
|
}
|
|
|
long orderCode = jsonMsg.getLongValue("orderCode");
|
|
|
int uid = jsonMsg.getIntValue("uid");
|
|
|
if (uid == 0) {
|
|
|
LOGGER.info("handle express info update message, not give me a uid, message is {}.", message);
|
|
|
return;
|
|
|
}
|
|
|
// 判断是买家订单号还是卖家订单号
|
|
|
boolean buyer = false;
|
|
|
//TODO
|
|
|
if (buyer) {
|
|
|
// 买家
|
|
|
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCodeSellerUid(orderCode, uid);
|
|
|
if (Objects.isNull(buyerOrder)) {
|
|
|
LOGGER.info("handle express info update message, orders not found, message is {}.", message);
|
|
|
return;
|
|
|
}
|
|
|
} else {
|
|
|
// 卖家
|
|
|
SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCodeUid(orderCode, uid);
|
|
|
if (Objects.isNull(sellerOrder)) {
|
|
|
LOGGER.info("handle express info update message, orders not found, message is {}.", message);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
int createTime = jsonMsg.getIntValue("createTime");
|
|
|
//int smsType = jsonMsg.getIntValue("businessType");
|
|
|
int state = jsonMsg.getIntValue("state");
|
|
|
|
|
|
// 正常订单,签收消息
|
|
|
if (state == ExpressInfoConstant.EXPRESS_STATUS_SIGN) {
|
|
|
handleAcceptExpress(jsonMsg);
|
|
|
return;
|
|
|
}
|
|
|
int logisticsType = jsonMsg.getIntValue("logisticsType");
|
|
|
String waybillCode = jsonMsg.getString("waybillCode");
|
|
|
|
|
|
// 正常订单,物流调拨消息
|
|
|
if (state == ExpressInfoConstant.EXPRESS_STATUS_UNSING
|
|
|
&& logisticsType < 1
|
|
|
&& (StringUtils.isEmpty(waybillCode) || StringUtils.equals(waybillCode, "0"))) {
|
|
|
state = ExpressInfoConstant.EXPRESS_STATUS_UNSEND;
|
|
|
// 清除order详情缓存
|
|
|
/*noSyncRedisCacheManager.newCacheCleaner4Orders(orders)
|
|
|
.buildOrderDetailKey(uid, orderCode)
|
|
|
.clear();*/
|
|
|
|
|
|
}
|
|
|
String acceptAddress = jsonMsg.getString("acceptAddress");
|
|
|
String acceptRemark = jsonMsg.getString("acceptRemark");
|
|
|
ExpressInfo expressInfo = new ExpressInfo();
|
|
|
expressInfo.setState(state);
|
|
|
expressInfo.setLogisticsType(logisticsType);
|
|
|
expressInfo.setOrderCode(orderCode);
|
|
|
expressInfo.setWaybillCode(StringUtils.defaultString(waybillCode));
|
|
|
expressInfo.setAcceptAddress(StringUtils.defaultString(acceptAddress));
|
|
|
expressInfo.setAcceptRemark(StringUtils.defaultString(acceptRemark));
|
|
|
expressInfo.setCreateTime(createTime);
|
|
|
expressInfo.setUid(uid);
|
|
|
expressInfo.setExpressType(expressInfoService.getExpressType(orderCode).byteValue());
|
|
|
expressInfoMapper.insert(expressInfo);
|
|
|
}
|
|
|
|
|
|
private boolean checkValidMessage(JSONObject jsonMsg) {
|
|
|
boolean flag = true;
|
|
|
int smsType = jsonMsg.getIntValue("businessType");
|
|
|
int logisticsType = jsonMsg.getIntValue("logisticsType");
|
|
|
int state = jsonMsg.getIntValue("state");
|
|
|
long orderCode = jsonMsg.getLongValue("orderCode");
|
|
|
String waybillCode = jsonMsg.getString("waybillCode");
|
|
|
if (smsType < 1 || state < 0 || logisticsType < 0 || orderCode < 0) {
|
|
|
LOGGER.warn("invalid express info message,message is {}.", jsonMsg);
|
|
|
flag = false;
|
|
|
}
|
|
|
if (logisticsType > 0 && StringUtils.isBlank(waybillCode)) {
|
|
|
LOGGER.warn("invalid express info message,message is {}.", jsonMsg);
|
|
|
flag = false;
|
|
|
}
|
|
|
return flag;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 确认收货处理
|
|
|
*/
|
|
|
public void handleAcceptExpress(JSONObject jsonMsg) {
|
|
|
int state = jsonMsg.getIntValue("state");
|
|
|
long orderCode = jsonMsg.getLongValue("orderCode");
|
|
|
int logisticsType = jsonMsg.getIntValue("logisticsType");
|
|
|
String waybillCode = jsonMsg.getString("waybillCode");
|
|
|
String acceptAddress = jsonMsg.getString("acceptAddress");
|
|
|
String acceptRemark = jsonMsg.getString("acceptRemark");
|
|
|
int uid = jsonMsg.getIntValue("uid");
|
|
|
int createTime = DateUtil.getCurrentTimeSecond();
|
|
|
|
|
|
Integer expressType = expressInfoService.getExpressType(orderCode);
|
|
|
List<ExpressInfo> expressInfos = expressInfoMapper.selectByOrderCodeAndStatesAndUidAndExpressType(uid, orderCode, Lists.newArrayList(ExpressInfoConstant.EXPRESS_STATUS_SIGN), expressType);
|
|
|
processExpressInfo(expressInfos, state, logisticsType, orderCode, waybillCode, acceptAddress, acceptRemark, createTime, uid, expressType);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param expressInfos 快递信息
|
|
|
* @param state 快递状态
|
|
|
* @param logisticsType 快递公司id
|
|
|
* @param orderCode 订单号
|
|
|
* @param waybillCode 快递单号
|
|
|
* @param acceptAddress 运单接收地
|
|
|
* @param acceptRemark 运单信息
|
|
|
* @param createTime 创建时间
|
|
|
* @param uid
|
|
|
* @param expressType 物流类型
|
|
|
*/
|
|
|
private void processExpressInfo(List<ExpressInfo> expressInfos, Integer state, Integer logisticsType, Long orderCode, String waybillCode,
|
|
|
String acceptAddress, String acceptRemark, Integer createTime, Integer uid, Integer expressType) {
|
|
|
if (CollectionUtils.isEmpty(expressInfos)) {
|
|
|
ExpressInfo expressInfo = new ExpressInfo();
|
|
|
expressInfo.setState(state);
|
|
|
expressInfo.setLogisticsType(logisticsType);
|
|
|
expressInfo.setOrderCode(orderCode);
|
|
|
expressInfo.setWaybillCode(StringUtils.defaultString(waybillCode));
|
|
|
expressInfo.setAcceptAddress(StringUtils.defaultString(acceptAddress));
|
|
|
expressInfo.setAcceptRemark(StringUtils.defaultString(acceptRemark));
|
|
|
expressInfo.setCreateTime(createTime);
|
|
|
expressInfo.setUid(uid);
|
|
|
expressInfo.setExpressType(expressType.byteValue());
|
|
|
expressInfoMapper.insert(expressInfo);
|
|
|
} else {
|
|
|
for (ExpressInfo expressInfo : expressInfos) {
|
|
|
expressInfo.setState(state);
|
|
|
expressInfo.setLogisticsType(logisticsType);
|
|
|
expressInfo.setOrderCode(orderCode);
|
|
|
expressInfo.setWaybillCode(StringUtils.defaultString(waybillCode));
|
|
|
expressInfo.setAcceptAddress(StringUtils.defaultString(acceptAddress));
|
|
|
expressInfo.setAcceptRemark(StringUtils.defaultString(acceptRemark));
|
|
|
expressInfo.setCreateTime(createTime);
|
|
|
expressInfo.setUid(uid);
|
|
|
expressInfo.setExpressType(expressType.byteValue());
|
|
|
expressInfoMapper.updateByPrimaryKeySelective(expressInfo);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|