package com.taoke.autopay.service.impl.js2;
|
|
import com.taoke.autopay.dao.OrderTaskExecutionDetailMapper;
|
import com.taoke.autopay.dao.OrderTaskMapper;
|
import com.taoke.autopay.entity.js2.OrderTask;
|
import com.taoke.autopay.entity.js2.OrderTaskExecutionDetail;
|
import com.taoke.autopay.exception.OrderTaskExecutionDetailException;
|
import com.taoke.autopay.service.js2.OrderTaskExecutionDetailService;
|
import com.taoke.autopay.utils.StringUtil;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* @author
|
* @title: OrderTaskExecutionDetailServiceImpl
|
* @description: 下单任务执行详情服务实现类
|
* @date 2025/7/28
|
*/
|
@Service
|
public class OrderTaskExecutionDetailServiceImpl implements OrderTaskExecutionDetailService {
|
|
@Resource
|
private OrderTaskExecutionDetailMapper orderTaskExecutionDetailMapper;
|
|
private static final Logger logger = LoggerFactory.getLogger(OrderTaskExecutionDetailServiceImpl.class);
|
@Autowired
|
private OrderTaskMapper orderTaskMapper;
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public OrderTaskExecutionDetail createOrderTaskExecutionDetail(OrderTaskExecutionDetail orderTaskExecutionDetail) throws OrderTaskExecutionDetailException {
|
validateOrderTaskExecutionDetail(orderTaskExecutionDetail);
|
|
// 设置创建时间
|
if (orderTaskExecutionDetail.getCreateTime() == null) {
|
orderTaskExecutionDetail.setCreateTime(new Date());
|
}
|
|
// 设置更新时间
|
orderTaskExecutionDetail.setUpdateTime(new Date());
|
|
// 插入数据库
|
orderTaskExecutionDetailMapper.insertSelective(orderTaskExecutionDetail);
|
|
return orderTaskExecutionDetail;
|
}
|
|
@Override
|
public OrderTaskExecutionDetail getOrderTaskExecutionDetailById(String id) {
|
if (id == null) {
|
return null;
|
}
|
return orderTaskExecutionDetailMapper.selectByPrimaryKey(id);
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public OrderTaskExecutionDetail getOrderTaskExecutionDetailByIdForUpdate(String id) {
|
if (id == null) {
|
return null;
|
}
|
return orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void updateOrderTaskExecutionDetail(OrderTaskExecutionDetail orderTaskExecutionDetail) throws OrderTaskExecutionDetailException {
|
if (orderTaskExecutionDetail == null || orderTaskExecutionDetail.getId() == null) {
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "下单任务执行详情或详情ID不能为空");
|
}
|
|
// 设置更新时间
|
orderTaskExecutionDetail.setUpdateTime(new Date());
|
|
// 更新数据库
|
orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(orderTaskExecutionDetail);
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void deleteOrderTaskExecutionDetail(String id) throws OrderTaskExecutionDetailException {
|
if (id == null) {
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
|
}
|
|
// 删除数据库记录
|
orderTaskExecutionDetailMapper.deleteByPrimaryKey(id);
|
}
|
|
@Override
|
public List<OrderTaskExecutionDetail> listOrderTaskExecutionDetails(OrderTaskExecutionDetailMapper.DaoQuery query, int page, int pageSize) {
|
if (query == null) {
|
query = new OrderTaskExecutionDetailMapper.DaoQuery();
|
}
|
query.start = (long) (page - 1) * pageSize;
|
query.count = pageSize;
|
return orderTaskExecutionDetailMapper.list(query);
|
}
|
|
@Override
|
public long countOrderTaskExecutionDetails(OrderTaskExecutionDetailMapper.DaoQuery query) {
|
if (query == null) {
|
query = new OrderTaskExecutionDetailMapper.DaoQuery();
|
}
|
return orderTaskExecutionDetailMapper.count(query);
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void orderSuccess(String id, String orderNo, String productTitle, String shopName, Date orderTime) throws OrderTaskExecutionDetailException {
|
if (id == null) {
|
logger.error("任务执行详情ID不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
|
}
|
|
// 获取任务执行详情(带锁)
|
OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
|
if (detail == null) {
|
logger.error("未找到ID为{}的任务执行详情", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
|
}
|
|
if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_NOT_ORDERED){
|
logger.error("尚未处于未下单状态:{}", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于未下单状态");
|
}
|
|
OrderTaskExecutionDetail update=OrderTaskExecutionDetail.builder()
|
.id(id)
|
.orderNo(orderNo)
|
.productName(productTitle)
|
.shopName(shopName)
|
.orderTime(orderTime)
|
.executionStatus(OrderTaskExecutionDetail.STATUS_ORDERED)
|
.statusDescription("下单成功")
|
.updateTime(new Date())
|
.build();
|
// 更新数据库
|
orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(update);
|
// 更新任务下单成功数量
|
OrderTask orderTask = orderTaskMapper.selectByPrimaryKeyForUpdate(detail.getTaskId());
|
orderTaskMapper.updateByPrimaryKeySelective( OrderTask.builder()
|
.id(orderTask.getId())
|
.updateTime(new Date())
|
.completedOrderCount(orderTask.getCompletedOrderCount() + 1)
|
.build());
|
logger.info("任务执行详情ID:{} 下单成功,订单号:{}", id, orderNo);
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void orderFailure(String id, String failureReason) throws OrderTaskExecutionDetailException {
|
if (id == null) {
|
logger.error("任务执行详情ID不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
|
}
|
|
if (StringUtil.isNullOrEmpty(failureReason)) {
|
logger.warn("下单失败原因为空,使用默认原因");
|
failureReason = "未知错误";
|
}
|
|
// 获取任务执行详情(带锁)
|
OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
|
if (detail == null) {
|
logger.error("未找到ID为{}的任务执行详情", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
|
}
|
if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_NOT_ORDERED){
|
logger.error("尚未处于未下单状态:{}", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于未下单状态");
|
}
|
|
OrderTaskExecutionDetail update=OrderTaskExecutionDetail.builder()
|
.id(id)
|
.executionStatus(OrderTaskExecutionDetail.STATUS_ORDER_FAILED)
|
.statusDescription("下单失败:" + failureReason)
|
.updateTime(new Date())
|
.build();
|
// 更新数据库
|
orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(update);
|
logger.info("任务执行详情ID:{} 下单失败,原因:{}", id, failureReason);
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void confirmReceiptSuccess(String id, String couponCode) throws OrderTaskExecutionDetailException {
|
if (id == null) {
|
logger.error("任务执行详情ID不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
|
}
|
|
// 获取任务执行详情(带锁)
|
OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
|
if (detail == null) {
|
logger.error("未找到ID为{}的任务执行详情", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
|
}
|
|
if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_ORDERED){
|
logger.error("尚未处于下单状态:{}", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于下单状态");
|
}
|
|
// 更新字段
|
OrderTaskExecutionDetail update = OrderTaskExecutionDetail.builder()
|
.id(id)
|
.couponCode(couponCode)
|
.receiveTime(new Date())
|
.executionStatus(OrderTaskExecutionDetail.STATUS_RECEIVE_SUCCESS)
|
.statusDescription("确认收货成功")
|
.updateTime(new Date())
|
.build();
|
// 更新数据库
|
orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(update);
|
|
// 更新任务确认收货成功数量
|
OrderTask orderTask = orderTaskMapper.selectByPrimaryKeyForUpdate(detail.getTaskId());
|
orderTaskMapper.updateByPrimaryKeySelective( OrderTask.builder()
|
.id(orderTask.getId())
|
.updateTime(new Date())
|
.receivedOrderCount(orderTask.getReceivedOrderCount() + 1)
|
.build());
|
|
logger.info("任务执行详情ID:{} 确认收货成功,券码:{}", id, couponCode);
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void confirmReceiptFailure(String id, String reason) throws OrderTaskExecutionDetailException {
|
if (id == null) {
|
logger.error("任务执行详情ID不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
|
}
|
|
if (StringUtil.isNullOrEmpty(reason)) {
|
logger.warn("确认收货失败原因为空,使用默认原因");
|
reason = "未知错误";
|
}
|
|
// 获取任务执行详情(带锁)
|
OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
|
if (detail == null) {
|
logger.error("未找到ID为{}的任务执行详情", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
|
}
|
|
if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_ORDERED){
|
logger.error("尚未处于下单状态:{}", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于下单状态");
|
}
|
|
|
// 更新字段
|
detail.setExecutionStatus(OrderTaskExecutionDetail.STATUS_RECEIVE_FAILED); // 设置为确认收货失败状态
|
detail.setStatusDescription("确认收货失败:" + reason);
|
|
// 更新时间
|
detail.setUpdateTime(new Date());
|
|
// 更新数据库
|
orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(detail);
|
logger.info("任务执行详情ID:{} 确认收货失败,原因:{}", id, reason);
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void reviewSuccess(String id) throws OrderTaskExecutionDetailException {
|
if (id == null) {
|
logger.error("任务执行详情ID不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
|
}
|
|
// 获取任务执行详情(带锁)
|
OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
|
if (detail == null) {
|
logger.error("未找到ID为{}的任务执行详情", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
|
}
|
|
if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_RECEIVE_SUCCESS){
|
logger.error("尚未处于确认收货状态:{}", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于确认收货状态");
|
}
|
|
|
// 更新字段
|
OrderTaskExecutionDetail update = OrderTaskExecutionDetail.builder()
|
.id(id)
|
.reviewTime(new Date())
|
.executionStatus(OrderTaskExecutionDetail.STATUS_REVIEW_SUCCESS)
|
.statusDescription("评价成功")
|
.updateTime(new Date())
|
.build();
|
// 更新数据库
|
orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(update);
|
// 更新任务评价成功数量
|
OrderTask orderTask = orderTaskMapper.selectByPrimaryKeyForUpdate(detail.getTaskId());
|
orderTaskMapper.updateByPrimaryKeySelective( OrderTask.builder()
|
.id(orderTask.getId())
|
.updateTime(new Date())
|
.reviewedOrderCount(orderTask.getReviewedOrderCount() + 1)
|
.build());
|
|
logger.info("任务执行详情ID:{} 评价成功", id);
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void reviewFailure(String id, String reason) throws OrderTaskExecutionDetailException {
|
if (id == null) {
|
logger.error("任务执行详情ID不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务执行详情ID不能为空");
|
}
|
|
if (StringUtil.isNullOrEmpty(reason)) {
|
logger.warn("评价失败原因为空,使用默认原因");
|
reason = "未知错误";
|
}
|
|
// 获取任务执行详情(带锁)
|
OrderTaskExecutionDetail detail = orderTaskExecutionDetailMapper.selectByPrimaryKeyForUpdate(id);
|
if (detail == null) {
|
logger.error("未找到ID为{}的任务执行详情", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_DETAIL_NOT_FOUND, "未找到对应的任务执行详情");
|
}
|
|
if(detail.getExecutionStatus()!=OrderTaskExecutionDetail.STATUS_RECEIVE_SUCCESS){
|
logger.error("尚未处于确认收货状态:{}", id);
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "尚未处于确认收货状态");
|
}
|
|
// 更新字段
|
detail.setExecutionStatus(OrderTaskExecutionDetail.STATUS_REVIEW_FAILED); // 设置为评价失败状态
|
detail.setStatusDescription("评价失败:" + reason);
|
|
// 更新时间
|
detail.setUpdateTime(new Date());
|
|
// 更新数据库
|
orderTaskExecutionDetailMapper.updateByPrimaryKeySelective(detail);
|
logger.info("任务执行详情ID:{} 评价失败,原因:{}", id, reason);
|
}
|
|
@Override
|
public List<OrderTaskExecutionDetail> listCanExcuteTaskDetail(Long clientId, int page, int pageSize) {
|
// 获取30分钟以内的订单任务执行详情
|
return orderTaskExecutionDetailMapper.listCanExcuteTaskDetail(clientId, new Date(System.currentTimeMillis() - 1000*60*30L));
|
}
|
|
@Override
|
public long countCanExcuteTaskDetail(Long clientId) {
|
return orderTaskExecutionDetailMapper.countCanExcuteTaskDetail(clientId, new Date(System.currentTimeMillis() - 1000*60*30L));
|
}
|
|
/**
|
* 验证订单任务执行详情的必填字段
|
*
|
* @param orderTaskExecutionDetail 订单任务执行详情对象
|
* @throws OrderTaskExecutionDetailException 如果必填字段为空则抛出异常
|
*/
|
private void validateOrderTaskExecutionDetail(OrderTaskExecutionDetail orderTaskExecutionDetail) throws OrderTaskExecutionDetailException {
|
if (orderTaskExecutionDetail == null) {
|
logger.error("下单任务执行详情不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "下单任务执行详情不能为空");
|
}
|
|
if (StringUtil.isNullOrEmpty(orderTaskExecutionDetail.getId())) {
|
logger.error("下单任务执行详情ID不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "下单任务执行详情ID不能为空");
|
}
|
|
if (orderTaskExecutionDetail.getTaskId() == null) {
|
logger.error("任务ID不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "任务ID不能为空");
|
}
|
|
if (orderTaskExecutionDetail.getClientId() == null) {
|
logger.error("客户端ID不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "客户端ID不能为空");
|
}
|
|
if (orderTaskExecutionDetail.getExecutionStatus() == null) {
|
logger.error("执行状态不能为空");
|
throw new OrderTaskExecutionDetailException(OrderTaskExecutionDetailException.CODE_COMMON, "执行状态不能为空");
|
}
|
}
|
}
|