Files
LinHelp/backend/src/main/java/com/linhelp/delivery/RiderTaskService.java
2026-07-07 17:36:58 +08:00

129 lines
6.1 KiB
Java

package com.linhelp.delivery;
import com.linhelp.common.enums.DeliveryTaskStatus;
import com.linhelp.common.enums.ExpressOrderStatus;
import com.linhelp.common.exception.BizException;
import com.linhelp.express.ExpressOrderResponse;
import com.linhelp.express.ExpressOrderService;
import com.linhelp.groupbuy.GroupBuyOrderService;
import com.linhelp.order.GoodsOrderService;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class RiderTaskService {
private static final int INCOME_PER_DELIVERED_TASK_CENT = 300;
private final DeliveryAssignmentService assignmentService;
private final GoodsOrderService goodsOrderService;
private final ExpressOrderService expressOrderService;
private final GroupBuyOrderService groupBuyOrderService;
public RiderTaskService(DeliveryAssignmentService assignmentService,
GoodsOrderService goodsOrderService,
ExpressOrderService expressOrderService,
GroupBuyOrderService groupBuyOrderService) {
this.assignmentService = assignmentService;
this.goodsOrderService = goodsOrderService;
this.expressOrderService = expressOrderService;
this.groupBuyOrderService = groupBuyOrderService;
}
public List<DeliveryOrderResponse> listTasks(Long riderId) {
return assignmentService.listRiderTasks(riderId);
}
public DeliveryOrderResponse detail(Long riderId, Long taskId) {
return assignmentService.detailForRider(riderId, taskId);
}
public DeliveryOrderResponse start(Long riderId, Long taskId) {
DeliveryOrderResponse task = assignmentService.requireForRider(riderId, taskId);
if (task.getStatus() != DeliveryTaskStatus.ASSIGNED && task.getStatus() != DeliveryTaskStatus.ACCEPTED) {
throw new BizException("任务状态不允许操作");
}
if (DeliveryAssignmentService.BIZ_TYPE_GOODS_ORDER.equals(task.getBizType())) {
goodsOrderService.markDelivering(task.getBizOrderId());
} else if (DeliveryAssignmentService.BIZ_TYPE_EXPRESS_ORDER.equals(task.getBizType())) {
startExpressOrder(task.getBizOrderId());
} else if (DeliveryAssignmentService.BIZ_TYPE_GROUP_BUY_ORDER.equals(task.getBizType())) {
groupBuyOrderService.markDelivering(task.getBizOrderId());
}
task.setStatus(DeliveryTaskStatus.DELIVERING);
task.setUpdatedAt(LocalDateTime.now());
return assignmentService.copyTask(task);
}
public DeliveryOrderResponse delivered(Long riderId, Long taskId, String photoUrl) {
DeliveryOrderResponse task = assignmentService.requireForRider(riderId, taskId);
if (task.getStatus() != DeliveryTaskStatus.DELIVERING) {
throw new BizException("任务状态不允许操作");
}
if (DeliveryAssignmentService.BIZ_TYPE_GOODS_ORDER.equals(task.getBizType())) {
goodsOrderService.complete(task.getBizOrderId());
} else if (DeliveryAssignmentService.BIZ_TYPE_EXPRESS_ORDER.equals(task.getBizType())) {
deliverExpressOrder(task.getBizOrderId(), photoUrl);
} else if (DeliveryAssignmentService.BIZ_TYPE_GROUP_BUY_ORDER.equals(task.getBizType())) {
groupBuyOrderService.complete(task.getBizOrderId());
}
task.setDeliveredPhotoUrl(photoUrl);
task.setStatus(DeliveryTaskStatus.DELIVERED);
task.setUpdatedAt(LocalDateTime.now());
return assignmentService.copyTask(task);
}
public DeliveryOrderResponse exception(Long riderId, Long taskId, String exceptionType, String exceptionNote) {
DeliveryOrderResponse task = assignmentService.requireForRider(riderId, taskId);
task.setExceptionType(exceptionType);
task.setExceptionNote(exceptionNote);
task.setStatus(DeliveryTaskStatus.EXCEPTION);
task.setUpdatedAt(LocalDateTime.now());
return assignmentService.copyTask(task);
}
public RiderIncomeSummaryResponse incomeSummary(Long riderId) {
RiderIncomeSummaryResponse response = new RiderIncomeSummaryResponse();
for (DeliveryOrderResponse task : listTasks(riderId)) {
if (task.getStatus() == DeliveryTaskStatus.ASSIGNED || task.getStatus() == DeliveryTaskStatus.ACCEPTED) {
response.setAssignedCount(response.getAssignedCount() + 1);
} else if (task.getStatus() == DeliveryTaskStatus.DELIVERING) {
response.setDeliveringCount(response.getDeliveringCount() + 1);
} else if (task.getStatus() == DeliveryTaskStatus.DELIVERED || task.getStatus() == DeliveryTaskStatus.COMPLETED) {
response.setDeliveredCount(response.getDeliveredCount() + 1);
}
}
response.setEstimatedIncomeCent(response.getDeliveredCount() * INCOME_PER_DELIVERED_TASK_CENT);
return response;
}
private void startExpressOrder(Long orderId) {
if (expressOrderService == null) {
throw new BizException("快递代取服务未启用");
}
ExpressOrderResponse order = expressOrderService.detail(orderId);
if (order.getStatus() == ExpressOrderStatus.PENDING_PICKUP) {
expressOrderService.markPickingUp(orderId);
} else if (order.getStatus() == ExpressOrderStatus.PICKING_UP) {
expressOrderService.markDelivering(orderId);
} else {
throw new BizException("快递订单状态不允许操作");
}
}
private void deliverExpressOrder(Long orderId, String photoUrl) {
if (expressOrderService == null) {
throw new BizException("快递代取服务未启用");
}
ExpressOrderResponse order = expressOrderService.detail(orderId);
if (order.getStatus() == ExpressOrderStatus.PENDING_PICKUP) {
expressOrderService.markPickingUp(orderId);
expressOrderService.markDelivering(orderId);
} else if (order.getStatus() == ExpressOrderStatus.PICKING_UP) {
expressOrderService.markDelivering(orderId);
}
expressOrderService.markDelivered(orderId, photoUrl);
}
}