trade:完成交易订单的收货逻辑

pull/2/head
YunaiV 2022-11-29 23:02:37 +08:00
parent 66abe4a84b
commit 7056d89b0c
7 changed files with 95 additions and 5 deletions

View File

@ -27,6 +27,7 @@ public interface ErrorCodeConstants {
ErrorCode ORDER_UPDATE_PAID_FAIL_PAY_ORDER_STATUS_NOT_SUCCESS = new ErrorCode(1011000015, "交易订单更新支付状态失败,支付单状态不是【支付成功】状态");
ErrorCode ORDER_UPDATE_PAID_FAIL_PAY_PRICE_NOT_MATCH = new ErrorCode(1011000016, "交易订单更新支付状态失败,支付单金额不匹配");
ErrorCode ORDER_DELIVERY_FAIL_STATUS_NOT_UNDELIVERED = new ErrorCode(1011000017, "交易订单发货失败,订单不是【待发货】状态");
ErrorCode ORDER_RECEIVE_FAIL_STATUS_NOT_DELIVERED = new ErrorCode(1011000018, "交易订单收货失败,订单不是【待收货】状态");
// ========== After Sale 模块 1-011-000-000 ==========
ErrorCode AFTER_SALE_NOT_FOUND = new ErrorCode(1011000100, "售后单不存在");

View File

@ -75,6 +75,15 @@ public enum TradeOrderStatusEnum implements IntArrayValuable {
return ObjectUtil.equal(UNDELIVERED.getStatus(), status);
}
/**
*
*
* @param status
* @return
*/
public static boolean isDelivered(Integer status) {
return ObjectUtil.equals(status, DELIVERED.getStatus());
}
/**
*

View File

@ -13,4 +13,9 @@ public interface TradeOrderMapper extends BaseMapperX<TradeOrderDO> {
.eq(TradeOrderDO::getId, id).eq(TradeOrderDO::getStatus, status));
}
default TradeOrderDO selectByIdAndUserId(Long id, Long userId) {
return selectOne(TradeOrderDO::getId, id,
TradeOrderDO::getUserId, userId);
}
}

View File

@ -41,14 +41,22 @@ public interface TradeOrderService {
*/
void deliveryOrder(Long userId, TradeOrderDeliveryReqVO deliveryReqVO);
/**
*
*
* @param userId
* @param id
*/
void receiveOrder(Long userId, Long id);
/**
*
*
* @param userId
* @param orderId
* @param id
* @return
*/
TradeOrderDO getOrder(Long userId, Long orderId);
TradeOrderDO getOrder(Long userId, Long id);
// =================== Order Item ===================

View File

@ -315,6 +315,7 @@ public class TradeOrderServiceImpl implements TradeOrderService {
return new KeyValue<>(order, payOrder);
}
// TODO 芋艿:如果无需发货,需要怎么存储?
@Override
public void deliveryOrder(Long userId, TradeOrderDeliveryReqVO deliveryReqVO) {
// 校验并获得交易订单(可发货)
@ -365,8 +366,52 @@ public class TradeOrderServiceImpl implements TradeOrderService {
}
@Override
public TradeOrderDO getOrder(Long userId, Long orderId) {
TradeOrderDO order = tradeOrderMapper.selectById(orderId);
@Transactional(rollbackFor = Exception.class)
public void receiveOrder(Long userId, Long id) {
// 校验并获得交易订单(可收货)
TradeOrderDO order = validateOrderReceivable(userId, id);
// 更新 TradeOrderDO 状态为已完成
int updateCount = tradeOrderMapper.updateByIdAndStatus(order.getId(), order.getStatus(),
new TradeOrderDO().setStatus(TradeOrderStatusEnum.COMPLETED.getStatus())
.setDeliveryStatus(TradeOrderDeliveryStatusEnum.RECEIVED.getStatus()).setReceiveTime(LocalDateTime.now()));
if (updateCount == 0) {
throw exception(ORDER_RECEIVE_FAIL_STATUS_NOT_DELIVERED);
}
// TODO 芋艿OrderLog
// TODO 芋艿lili 发送订单变化的消息
// TODO 芋艿lili 发送商品被购买完成的数据
}
/**
*
*
* 1.
*
* @param userId
* @param id
* @return
*/
private TradeOrderDO validateOrderReceivable(Long userId, Long id) {
// 校验订单是否存在
TradeOrderDO order = tradeOrderMapper.selectByIdAndUserId(id, userId);
if (order == null) {
throw exception(ORDER_NOT_FOUND);
}
// 校验订单是否是待收货状态
if (!TradeOrderStatusEnum.isDelivered(order.getStatus())
|| ObjectUtil.notEqual(order.getDeliveryStatus(), TradeOrderDeliveryStatusEnum.DELIVERED.getStatus())) {
throw exception(ORDER_RECEIVE_FAIL_STATUS_NOT_DELIVERED);
}
return order;
}
@Override
public TradeOrderDO getOrder(Long userId, Long id) {
TradeOrderDO order = tradeOrderMapper.selectById(id);
if (order != null
&& ObjectUtil.notEqual(order.getUserId(), userId)) {
return null;

View File

@ -295,4 +295,26 @@ public class TradeOrderServiceTest extends BaseDbUnitTest {
assertNotNull(dbOrder.getDeliveryTime());
}
@Test
public void testReceiveOrder() {
// mock 数据TradeOrder
TradeOrderDO order = randomPojo(TradeOrderDO.class, o -> {
o.setId(1L).setUserId(10L).setStatus(TradeOrderStatusEnum.DELIVERED.getStatus());
o.setDeliveryStatus(TradeOrderDeliveryStatusEnum.DELIVERED.getStatus()).setReceiveTime(null);
});
tradeOrderMapper.insert(order);
// 准备参数
Long id = 1L;
Long userId = 10L;
// mock 方法(支付单)
// 调用
tradeOrderService.receiveOrder(userId, id);
// 断言
TradeOrderDO dbOrder = tradeOrderMapper.selectById(1L);
assertEquals(dbOrder.getStatus(), TradeOrderStatusEnum.COMPLETED.getStatus());
assertEquals(dbOrder.getDeliveryStatus(), TradeOrderDeliveryStatusEnum.RECEIVED.getStatus());
assertNotNull(dbOrder.getReceiveTime());
}
}

View File

@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS "trade_order" (
"delivery_template_id" bigint,
"logistics_id" bigint,
"logistics_no" varchar,
"delivery_status" bit NOT NULL,
"delivery_status" smallint NOT NULL,
"delivery_time" datetime,
"receive_time" datetime,
"receiver_name" varchar NOT NULL,