Authored by qinchao

物流信息:接口查询快递公司

package com.yohoufo.dal.order;
import com.yohoufo.dal.order.model.ExpressInfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ExpressInfoMapper {
int deleteByPrimaryKey(Integer id);
... ... @@ -14,4 +17,24 @@ public interface ExpressInfoMapper {
int updateByPrimaryKeySelective(ExpressInfo record);
int updateByPrimaryKey(ExpressInfo record);
/**
* 根据uid和快递单号查询快递信息
* @param uid
* @param orderCode 订单号
* @param expressType 物流类型; 1:卖家到鉴定中心,2:鉴定中心到买家,3:鉴定中心退回到卖家
* @return
*/
List<ExpressInfo> queryExpressInfo(@Param("uid") Integer uid, @Param("orderCode") Long orderCode, @Param("expressType") Integer expressType);
/**
* 根据uid、订单号、物流类型和快递状态查询快递信息
* @param uid
* @param orderCode
* @param states 0:未签收 1签收 2未发货
* @param expressType 物流类型; 1:卖家到鉴定中心,2:鉴定中心到买家,3:鉴定中心退回到卖家
* @return
*/
List<ExpressInfo> selectByOrderCodeAndStatesAndUidAndExpressType(@Param("uid") Integer uid, @Param("orderCode") Long orderCode, @Param("states") List<Integer> states, @Param("expressType") Integer expressType);
}
\ No newline at end of file
... ...
... ... @@ -144,4 +144,21 @@
state = #{state,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="queryExpressInfo" resultMap="BaseResultMap">
SELECT id, uid, order_code, waybill_code, accept_address, accept_remark, logistics_type, create_time, express_type, state
FROM express_info
where uid = #{uid} and order_code = #{orderCode} and express_type = #{expressType}
ORDER BY create_time DESC
</select>
<select id="selectByOrderCodeAndStatesAndUidAndExpressType" resultMap="BaseResultMap">
SELECT id, uid, order_code, waybill_code, accept_address, accept_remark, logistics_type, create_time, express_type, state
FROM express_info
where uid = #{uid} and order_code = #{orderCode} and express_type = #{expressType} and
<foreach collection="states" item="state" separator="," open="(" close=")" >
#{state,jdbcType=INTEGER}
</foreach>
ORDER BY create_time DESC
</select>
</mapper>
\ No newline at end of file
... ...
package com.yohoufo.order.controller;
import com.yohobuy.ufo.model.order.resp.ExpressInfoRespBo;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.order.service.IExpressInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author kun.wang
* @date 2018/9/25
*/
@RequestMapping
@RestController
public class ExpressInfoController {
@Autowired
private IExpressInfoService expressInfoService;
/**
* 卖家发货到鉴定中心
* @param expressCompanyId 快递公司id
* @param orderCode 订单号
* @param wayBillCode 运单号(快递单号)
* @return
*/
@RequestMapping(params = "method=ufo.order.deliverToDepot")
public ApiResponse deliverToDepot(@RequestParam("expressCompanyId") Integer expressCompanyId,
@RequestParam("orderCode") Long orderCode, @RequestParam("wayBillCode") String wayBillCode) {
expressInfoService.deliverToDepot(expressCompanyId, orderCode, wayBillCode);
return new ApiResponse();
}
/**
* 商品鉴定不通过,鉴定中心寄回商品给卖家
* @param expressCompanyId 快递公司id
* @param orderCode 订单号
* @param wayBillCode 快递单号
* @return
*/
@RequestMapping(params = "method=ufo.order.appraiseFail")
public ApiResponse appraiseFail(@RequestParam("expressCompanyId") Integer expressCompanyId,
@RequestParam("orderCode") Long orderCode, @RequestParam("wayBillCode") String wayBillCode) {
expressInfoService.appraiseFail(expressCompanyId, orderCode, wayBillCode);
return new ApiResponse();
}
/**
* 鉴定通过,鉴定中心发货给买家
* @param expressCompanyId
* @param orderCode
* @param wayBillCode
* @return
*/
@RequestMapping(params = "method=ufo.order.appraiseSuccess")
public ApiResponse appraiseSuccess(@RequestParam("expressCompanyId") Integer expressCompanyId,
@RequestParam("orderCode") Long orderCode, @RequestParam("wayBillCode") String wayBillCode) {
expressInfoService.appraiseSuccess(expressCompanyId, orderCode, wayBillCode);
return new ApiResponse();
}
/**
* 查询快递详情
* @param orderCode
* @return
*/
@RequestMapping(params = "method=ufo.order.expressDetailInfo")
public ApiResponse queryExpressDetailInfo(@RequestParam("orderCode") Long orderCode) {
ExpressInfoRespBo expressInfoRespBo = expressInfoService.queryExpressDetailInfo(orderCode);
return new ApiResponse.ApiResponseBuilder().code(200).data(expressInfoRespBo).build();
}
}
... ...
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
... ...
package com.yohoufo.order.service;
import com.yohobuy.ufo.model.order.resp.ExpressInfoRespBo;
/**
* @author kun.wang
* @date 2018/9/25
*/
public interface IExpressInfoService {
/**
* 卖家发货到鉴定中心
*
* @param expressCompanyId 快递公司id
* @param orderCode 订单号
* @param wayBillCode 快递单号
*/
void deliverToDepot(Integer expressCompanyId, Long orderCode, String wayBillCode);
/**
* 商品鉴定不通过,鉴定中心寄回商品给卖家
*
* @param expressCompanyId 快递公司id
* @param orderCode 订单号
* @param wayBillCode 快递单号
*/
void appraiseFail(Integer expressCompanyId, Long orderCode, String wayBillCode);
/**
* 商品鉴定通过,鉴定中心发货给买家
*
* @param expressCompanyId 快递公司id
* @param orderCode 订单号
* @param wayBillCode 快递单号
*/
void appraiseSuccess(Integer expressCompanyId, Long orderCode, String wayBillCode);
/**
* 根据订单号查询快递信息
*
* @param orderCode
* @return
*/
ExpressInfoRespBo queryExpressDetailInfo(Long orderCode);
/**
* 根据订单号获取快递类型
* 1:卖家到鉴定中心
* 2:鉴定中心到买家
* 3:鉴定中心退回到卖家
*
* @param orderCode
* @return
*/
Integer getExpressType(Long orderCode);
}
... ...
package com.yohoufo.order.service.impl;
import com.alibaba.fastjson.JSON;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.order.resp.ExpressInfoDetail;
import com.yohobuy.ufo.model.order.resp.ExpressInfoRespBo;
import com.yohoufo.common.constant.ExpressInfoConstant;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.order.ExpressInfoMapper;
import com.yohoufo.dal.order.SellerOrderMapper;
import com.yohoufo.dal.order.model.ExpressInfo;
import com.yohoufo.dal.order.model.SellerOrder;
import com.yohoufo.order.common.SellerOrderStatus;
import com.yohoufo.order.service.IExpressCompanyService;
import com.yohoufo.order.service.IExpressInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @author kun.wang
* @date 2018/9/25
*/
@Service
public class ExpressInfoServiceImpl implements IExpressInfoService {
private static final Logger LOGGER = LoggerFactory.getLogger(ExpressInfoServiceImpl.class);
@Autowired
private ExpressInfoMapper expressInfoMapper;
@Autowired
private IExpressCompanyService expressCompanyService;
@Autowired
private SellerOrderMapper sellerOrderMapper;
@Override
public void deliverToDepot(Integer expressCompanyId, Long orderCode, String wayBillCode) {
LOGGER.info("deliverToDepot expressCompanyId = {}, orderCode = {}, wayBillCode = {}", new Object[]{expressCompanyId, orderCode, wayBillCode});
// TODO
// 保存订单物流信息
// 更新买家订单状态
}
@Override
public void appraiseFail(Integer expressCompanyId, Long orderCode, String wayBillCode) {
LOGGER.info("appraiseFail expressCompanyId = {}, orderCode = {}, wayBillCode = {}", new Object[]{expressCompanyId, orderCode, wayBillCode});
// TODO
}
@Override
public void appraiseSuccess(Integer expressCompanyId, Long orderCode, String wayBillCode) {
LOGGER.info("appraiseSuccess expressCompanyId = {}, orderCode = {}, wayBillCode = {}", new Object[]{expressCompanyId, orderCode, wayBillCode});
// TODO
}
@Override
public ExpressInfoRespBo queryExpressDetailInfo(Long orderCode) {
LOGGER.info("queryExpressDetailInfo orderCode = {}", orderCode);
ExpressInfoRespBo expressInfoRespBo = new ExpressInfoRespBo();
// TODO 获取uid
Integer uid = null;
Integer expressType = getExpressType(orderCode);
LOGGER.info("getExpressType result = {}", expressType);
List<ExpressInfo> expressInfoList = expressInfoMapper.queryExpressInfo(uid, orderCode, expressType);
processExpressInfo(expressInfoList, expressInfoRespBo);
LOGGER.info("queryExpressDetailInfo result = {}", JSON.toJSONString(expressInfoRespBo));
return expressInfoRespBo;
}
/**
* @param expressInfoList
* @param expressInfoRespBo
*/
private void processExpressInfo(List<ExpressInfo> expressInfoList, ExpressInfoRespBo expressInfoRespBo) {
if (CollectionUtils.isNotEmpty(expressInfoList)) {
List<ExpressInfoDetail> expressInfoDetailList = new ArrayList<>();
ExpressInfoDetail expressInfoDetail;
for (ExpressInfo expressInfo : expressInfoList) {
expressInfoDetail = new ExpressInfoDetail();
// 运单接收地
expressInfoDetail.setAcceptAddress(expressInfo.getAcceptAddress());
// 运单信息
expressInfoDetail.setAcceptRemark(expressInfo.getAcceptRemark());
expressInfoDetail.setCreateTimeStr(DateUtil.formatDate(expressInfo.getCreateTime(), DateUtil.yyyy_MM_dd_HH_mm_SS));
expressInfoDetailList.add(expressInfoDetail);
}
expressInfoRespBo.setExpressInfoDetailList(expressInfoDetailList);
// 快递单号
expressInfoRespBo.setWayBillCode(expressInfoList.get(0).getWaybillCode());
// 快递公司名称
expressInfoRespBo.setExpressCompanyName(expressCompanyService.getExpressName(expressInfoList.get(0).getLogisticsType()));
}
}
@Override
public Integer getExpressType(Long orderCode) {
Integer uid = null;
// TODO 根据订单号判断是买家还是卖家
boolean buyer = true;
if (buyer) {
return ExpressInfoConstant.EXPRESS_TYPE_2;
} else {
SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCodeUid(orderCode, uid);
if (sellerOrder == null) {
LOGGER.warn("query seller order is null.orderCode = {}, uid = {}", sellerOrder, uid);
throw new ServiceException(400, "卖家订单信息不存在!");
}
if (SellerOrderStatus.HAS_PAYED.getCode() == sellerOrder.getStatus()) {
// 查询卖家发货到鉴定中心的物流信息
return ExpressInfoConstant.EXPRESS_TYPE_1;
} else if (SellerOrderStatus.APPRAISAL_FAIL_COMPENSATE.getCode() == sellerOrder.getStatus()) {
// 查询鉴定中心发货给卖家的物流信息
return ExpressInfoConstant.EXPRESS_TYPE_3;
}
}
return 0;
}
}
... ...