fix(优惠券中心):新增用户查询优惠券逻辑接口,和用户核销优惠券逻辑
This commit is contained in:
+266
@@ -0,0 +1,266 @@
|
||||
package com.central.coupon.controller;
|
||||
|
||||
import com.central.coupon.dto.OrderQueryDTO;
|
||||
import com.central.coupon.dto.ConsumeOrderDTO;
|
||||
import com.central.coupon.dto.SimpleConsumeDTO;
|
||||
import com.central.coupon.vo.OrderDetailVO;
|
||||
import com.central.coupon.service.IOrderDetailService;
|
||||
import com.central.coupon.entity.CtripOrder;
|
||||
import com.central.coupon.mapper.CtripOrderMapper;
|
||||
import com.central.coupon.controller.CtripConsumedController;
|
||||
import com.central.coupon.model.CtripConsumedNoticeResponse;
|
||||
import com.central.coupon.service.CtripConsumedService;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.central.coupon.entity.CtripPassenger;
|
||||
import com.central.coupon.mapper.CtripPassengerMapper;
|
||||
import com.central.coupon.entity.CtripContact;
|
||||
import com.central.coupon.mapper.CtripContactMapper;
|
||||
import com.central.coupon.entity.CtripOrderItem;
|
||||
import com.central.coupon.mapper.CtripOrderItemMapper;
|
||||
|
||||
/**
|
||||
* 订单明细控制器
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/order-detail")
|
||||
public class OrderDetailController {
|
||||
|
||||
@Autowired
|
||||
private IOrderDetailService orderDetailService;
|
||||
|
||||
@Autowired
|
||||
private CtripOrderMapper ctripOrderMapper;
|
||||
|
||||
@Autowired
|
||||
private CtripConsumedService ctripConsumedService;
|
||||
|
||||
@Autowired
|
||||
private CtripPassengerMapper ctripPassengerMapper;
|
||||
|
||||
@Autowired
|
||||
private CtripContactMapper ctripContactMapper;
|
||||
|
||||
@Autowired
|
||||
private CtripOrderItemMapper ctripOrderItemMapper;
|
||||
|
||||
/**
|
||||
* 根据手机号和优惠券价格查询可核销的订单明细
|
||||
*
|
||||
* @param queryDTO 查询条件
|
||||
* @return 订单明细分页结果
|
||||
*/
|
||||
@PostMapping("/query")
|
||||
public Result<PageResult<OrderDetailVO>> queryOrderDetails(@Valid @RequestBody OrderQueryDTO queryDTO) {
|
||||
log.info("查询可核销订单明细,手机号: {}, 优惠券价格: {}", queryDTO.getUserPhone(), queryDTO.getCouponPrice());
|
||||
|
||||
try {
|
||||
PageResult<OrderDetailVO> result = orderDetailService.queryOrderDetails(queryDTO);
|
||||
return Result.succeed(result);
|
||||
} catch (Exception e) {
|
||||
log.error("查询可核销订单明细异常", e);
|
||||
return Result.failed("查询可核销订单明细失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据订单号查询订单明细
|
||||
*
|
||||
* @param otaOrderId 携程订单号
|
||||
* @return 订单明细
|
||||
*/
|
||||
@GetMapping("/{otaOrderId}")
|
||||
public Result<OrderDetailVO> getOrderDetail(@PathVariable String otaOrderId) {
|
||||
log.info("查询订单明细,订单号: {}", otaOrderId);
|
||||
|
||||
try {
|
||||
OrderDetailVO orderDetail = orderDetailService.getOrderDetail(otaOrderId);
|
||||
if (orderDetail == null) {
|
||||
return Result.failed("订单不存在");
|
||||
}
|
||||
return Result.succeed(orderDetail);
|
||||
} catch (Exception e) {
|
||||
log.error("查询订单明细异常,订单号: {}", otaOrderId, e);
|
||||
return Result.failed("查询订单明细失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 核销订单
|
||||
*
|
||||
* @param consumeDTO 核销请求
|
||||
* @return 核销结果
|
||||
*/
|
||||
@PostMapping("/consume")
|
||||
public Result<Boolean> consumeOrder(@Valid @RequestBody ConsumeOrderDTO consumeDTO) {
|
||||
log.info("核销订单,订单号: {}", consumeDTO.getOtaOrderId());
|
||||
|
||||
try {
|
||||
boolean result = orderDetailService.consumeOrder(consumeDTO);
|
||||
if (result) {
|
||||
return Result.succeed(true, "核销成功");
|
||||
} else {
|
||||
return Result.failed("核销失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("核销订单异常,订单号: {}", consumeDTO.getOtaOrderId(), e);
|
||||
return Result.failed("核销失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证订单是否可核销
|
||||
*
|
||||
* @param otaOrderId 携程订单号
|
||||
* @return 是否可核销
|
||||
*/
|
||||
@GetMapping("/{otaOrderId}/validate")
|
||||
public Result<Boolean> validateOrderForConsume(@PathVariable String otaOrderId) {
|
||||
log.info("验证订单是否可核销,订单号: {}", otaOrderId);
|
||||
|
||||
try {
|
||||
boolean canConsume = orderDetailService.validateOrderForConsume(otaOrderId);
|
||||
return Result.succeed(canConsume);
|
||||
} catch (Exception e) {
|
||||
log.error("验证订单核销异常,订单号: {}", otaOrderId, e);
|
||||
return Result.failed("验证失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 简化核销订单(单商品)
|
||||
*
|
||||
* @param consumeDTO 核销请求
|
||||
* @return 核销结果
|
||||
*/
|
||||
@PostMapping("/consume-simple")
|
||||
public Result<Boolean> consumeOrderSimple(@Valid @RequestBody SimpleConsumeDTO consumeDTO) {
|
||||
log.info("简化核销订单,订单号: {}, 商品ID: {}, 数量: {}",
|
||||
consumeDTO.getOtaOrderId(), consumeDTO.getItemId(), consumeDTO.getUseQuantity());
|
||||
|
||||
try {
|
||||
// 1. 验证订单是否存在
|
||||
CtripOrder order = ctripOrderMapper.selectByOtaOrderId(consumeDTO.getOtaOrderId());
|
||||
if (order == null) {
|
||||
log.error("订单不存在,订单号: {}", consumeDTO.getOtaOrderId());
|
||||
return Result.failed("订单不存在");
|
||||
}
|
||||
|
||||
// 2. 验证订单是否可核销
|
||||
if (!orderDetailService.validateOrderForConsume(consumeDTO.getOtaOrderId())) {
|
||||
log.error("订单不可核销,订单号: {}", consumeDTO.getOtaOrderId());
|
||||
return Result.failed("订单不可核销");
|
||||
}
|
||||
|
||||
// 3. 调用携程核销服务
|
||||
CtripConsumedController.CtripConsumeRequest request = new CtripConsumedController.CtripConsumeRequest();
|
||||
request.setOtaOrderId(consumeDTO.getOtaOrderId());
|
||||
request.setSupplierOrderId(order.getSupplierOrderId());
|
||||
request.setItemId(consumeDTO.getItemId());
|
||||
request.setUseQuantity(consumeDTO.getUseQuantity());
|
||||
request.setRemark(consumeDTO.getRemark());
|
||||
|
||||
CtripConsumedNoticeResponse response = ctripConsumedService.consumeOrder(request);
|
||||
boolean success = response != null && "0000".equals(response.getHeader().getResultCode());
|
||||
|
||||
if (success) {
|
||||
return Result.succeed(true, "核销成功");
|
||||
} else {
|
||||
return Result.failed("核销失败: " + (response != null ? response.getHeader().getResultMessage() : "未知错误"));
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("简化核销订单异常,订单号: {}", consumeDTO.getOtaOrderId(), e);
|
||||
return Result.failed("核销失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调试接口 - 检查数据库中的数据
|
||||
*/
|
||||
@GetMapping("/debug/data")
|
||||
public Result<Map<String, Object>> debugData() {
|
||||
log.info("调试接口 - 检查数据库中的数据");
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
try {
|
||||
// 1. 检查订单总数
|
||||
long orderCount = ctripOrderMapper.selectCount(null);
|
||||
result.put("orderCount", orderCount);
|
||||
|
||||
// 2. 检查乘客总数
|
||||
long passengerCount = ctripPassengerMapper.selectCount(null);
|
||||
result.put("passengerCount", passengerCount);
|
||||
|
||||
// 3. 检查联系人总数
|
||||
long contactCount = ctripContactMapper.selectCount(null);
|
||||
result.put("contactCount", contactCount);
|
||||
|
||||
// 4. 检查订单项总数
|
||||
long orderItemCount = ctripOrderItemMapper.selectCount(null);
|
||||
result.put("orderItemCount", orderItemCount);
|
||||
|
||||
// 5. 查询最新的几个订单
|
||||
List<CtripOrder> latestOrders = ctripOrderMapper.selectList(
|
||||
new QueryWrapper<CtripOrder>().orderByDesc("create_time").last("LIMIT 5")
|
||||
);
|
||||
result.put("latestOrders", latestOrders.stream().map(order -> {
|
||||
Map<String, Object> orderInfo = new HashMap<>();
|
||||
orderInfo.put("id", order.getId());
|
||||
orderInfo.put("otaOrderId", order.getOtaOrderId());
|
||||
orderInfo.put("totalAmount", order.getTotalAmount());
|
||||
orderInfo.put("status", order.getStatus());
|
||||
orderInfo.put("createTime", order.getCreateTime());
|
||||
return orderInfo;
|
||||
}).collect(Collectors.toList()));
|
||||
|
||||
// 6. 查询手机号18322694510的乘客信息
|
||||
List<CtripPassenger> passengers = ctripPassengerMapper.selectByPhone("18322694510");
|
||||
result.put("passengersForPhone", passengers != null ? passengers.size() : 0);
|
||||
if (passengers != null && !passengers.isEmpty()) {
|
||||
result.put("passengerDetails", passengers.stream().map(passenger -> {
|
||||
Map<String, Object> passengerInfo = new HashMap<>();
|
||||
passengerInfo.put("id", passenger.getId());
|
||||
passengerInfo.put("orderId", passenger.getOrderId());
|
||||
passengerInfo.put("passengerName", passenger.getPassengerName());
|
||||
passengerInfo.put("phone", passenger.getPhone());
|
||||
return passengerInfo;
|
||||
}).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
// 7. 查询手机号18322694510的联系人信息
|
||||
List<CtripContact> contacts = ctripContactMapper.selectByMobile("18322694510");
|
||||
result.put("contactsForPhone", contacts != null ? contacts.size() : 0);
|
||||
if (contacts != null && !contacts.isEmpty()) {
|
||||
result.put("contactDetails", contacts.stream().map(contact -> {
|
||||
Map<String, Object> contactInfo = new HashMap<>();
|
||||
contactInfo.put("id", contact.getId());
|
||||
contactInfo.put("orderId", contact.getOrderId());
|
||||
contactInfo.put("name", contact.getName());
|
||||
contactInfo.put("mobile", contact.getMobile());
|
||||
return contactInfo;
|
||||
}).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
return Result.succeed(result);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("调试接口异常", e);
|
||||
result.put("error", e.getMessage());
|
||||
return Result.failed("调试接口异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.central.coupon.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 核销项目DTO
|
||||
*/
|
||||
@Data
|
||||
public class ConsumeItemDTO {
|
||||
|
||||
/**
|
||||
* 商品ID
|
||||
*/
|
||||
private String itemId;
|
||||
|
||||
/**
|
||||
* PLU码
|
||||
*/
|
||||
private String plu;
|
||||
|
||||
/**
|
||||
* 核销数量
|
||||
*/
|
||||
private Integer quantity;
|
||||
|
||||
/**
|
||||
* 乘客ID列表
|
||||
*/
|
||||
private String[] passengerIds;
|
||||
|
||||
/**
|
||||
* 凭证ID列表
|
||||
*/
|
||||
private String[] voucherIds;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.central.coupon.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 核销订单DTO
|
||||
*/
|
||||
@Data
|
||||
public class ConsumeOrderDTO {
|
||||
|
||||
/**
|
||||
* 携程订单号
|
||||
*/
|
||||
private String otaOrderId;
|
||||
|
||||
/**
|
||||
* 供应商订单号
|
||||
*/
|
||||
private String supplierOrderId;
|
||||
|
||||
/**
|
||||
* 核销项目列表
|
||||
*/
|
||||
private List<ConsumeItemDTO> items;
|
||||
|
||||
/**
|
||||
* 核销备注
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.central.coupon.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 订单查询DTO
|
||||
*/
|
||||
@Data
|
||||
public class OrderQueryDTO {
|
||||
|
||||
/**
|
||||
* 用户手机号
|
||||
*/
|
||||
private String userPhone;
|
||||
|
||||
/**
|
||||
* 优惠券价格(用于筛选匹配的订单)
|
||||
*/
|
||||
private BigDecimal couponPrice;
|
||||
|
||||
/**
|
||||
* 是否只查询可核销的订单
|
||||
*/
|
||||
private Boolean onlyConsumable = true;
|
||||
|
||||
/**
|
||||
* 页码
|
||||
*/
|
||||
private Integer page = 1;
|
||||
|
||||
/**
|
||||
* 页大小
|
||||
*/
|
||||
private Integer size = 10;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.central.coupon.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 简化核销DTO
|
||||
*/
|
||||
@Data
|
||||
public class SimpleConsumeDTO {
|
||||
|
||||
/**
|
||||
* 携程订单号
|
||||
*/
|
||||
private String otaOrderId;
|
||||
|
||||
/**
|
||||
* 商品ID
|
||||
*/
|
||||
private String itemId;
|
||||
|
||||
/**
|
||||
* 核销数量
|
||||
*/
|
||||
private Integer useQuantity;
|
||||
|
||||
/**
|
||||
* 备注信息
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
@@ -47,11 +47,6 @@ public class CtripContact implements Serializable {
|
||||
*/
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 联系人类型:BOOKER-预订人,CONTACT-联系人
|
||||
*/
|
||||
private String contactType;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
|
||||
+5
@@ -27,4 +27,9 @@ public interface CtripConsumedMapper extends BaseMapper<CtripConsumed> {
|
||||
* 根据供应商订单ID查询核销记录
|
||||
*/
|
||||
List<CtripConsumed> selectBySupplierOrderId(@Param("supplierOrderId") String supplierOrderId);
|
||||
|
||||
/**
|
||||
* 根据商品ID查询核销记录
|
||||
*/
|
||||
List<CtripConsumed> selectByItemId(@Param("itemId") String itemId);
|
||||
}
|
||||
+3
-4
@@ -22,11 +22,10 @@ public interface CtripContactMapper extends BaseMapper<CtripContact> {
|
||||
List<CtripContact> selectByOrderId(@Param("orderId") Long orderId);
|
||||
|
||||
/**
|
||||
* 根据订单ID和联系人类型查询联系人
|
||||
* 根据手机号查询联系人列表
|
||||
*
|
||||
* @param orderId 订单ID
|
||||
* @param contactType 联系人类型
|
||||
* @param mobile 手机号
|
||||
* @return 联系人列表
|
||||
*/
|
||||
List<CtripContact> selectByOrderIdAndType(@Param("orderId") Long orderId, @Param("contactType") String contactType);
|
||||
List<CtripContact> selectByMobile(@Param("mobile") String mobile);
|
||||
}
|
||||
+8
@@ -36,4 +36,12 @@ public interface CtripPassengerMapper extends BaseMapper<CtripPassenger> {
|
||||
* @return 乘客信息
|
||||
*/
|
||||
CtripPassenger selectByPassengerId(@Param("passengerId") String passengerId);
|
||||
|
||||
/**
|
||||
* 根据手机号查询乘客列表
|
||||
*
|
||||
* @param phone 手机号
|
||||
* @return 乘客列表
|
||||
*/
|
||||
List<CtripPassenger> selectByPhone(@Param("phone") String phone);
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.central.coupon.service;
|
||||
|
||||
import com.central.coupon.dto.OrderQueryDTO;
|
||||
import com.central.coupon.dto.ConsumeOrderDTO;
|
||||
import com.central.coupon.vo.OrderDetailVO;
|
||||
import com.central.common.model.PageResult;
|
||||
|
||||
/**
|
||||
* 订单明细服务接口
|
||||
*/
|
||||
public interface IOrderDetailService {
|
||||
|
||||
/**
|
||||
* 根据手机号和价格查询订单明细
|
||||
*
|
||||
* @param queryDTO 查询条件
|
||||
* @return 订单明细分页结果
|
||||
*/
|
||||
PageResult<OrderDetailVO> queryOrderDetails(OrderQueryDTO queryDTO);
|
||||
|
||||
/**
|
||||
* 根据订单号查询订单明细
|
||||
*
|
||||
* @param otaOrderId 携程订单号
|
||||
* @return 订单明细
|
||||
*/
|
||||
OrderDetailVO getOrderDetail(String otaOrderId);
|
||||
|
||||
/**
|
||||
* 核销订单
|
||||
*
|
||||
* @param consumeDTO 核销请求
|
||||
* @return 核销结果
|
||||
*/
|
||||
boolean consumeOrder(ConsumeOrderDTO consumeDTO);
|
||||
|
||||
/**
|
||||
* 验证订单是否可核销
|
||||
*
|
||||
* @param otaOrderId 携程订单号
|
||||
* @return 是否可核销
|
||||
*/
|
||||
boolean validateOrderForConsume(String otaOrderId);
|
||||
}
|
||||
+483
@@ -0,0 +1,483 @@
|
||||
package com.central.coupon.service.impl;
|
||||
|
||||
import com.central.coupon.dto.OrderQueryDTO;
|
||||
import com.central.coupon.dto.ConsumeOrderDTO;
|
||||
import com.central.coupon.vo.OrderDetailVO;
|
||||
import com.central.coupon.vo.OrderItemDetailVO;
|
||||
import com.central.coupon.vo.PassengerDetailVO;
|
||||
import com.central.coupon.vo.ContactDetailVO;
|
||||
import com.central.coupon.entity.CtripOrder;
|
||||
import com.central.coupon.entity.CtripOrderItem;
|
||||
import com.central.coupon.entity.CtripPassenger;
|
||||
import com.central.coupon.entity.CtripContact;
|
||||
import com.central.coupon.entity.CtripConsumed;
|
||||
import com.central.coupon.mapper.CtripOrderMapper;
|
||||
import com.central.coupon.mapper.CtripOrderItemMapper;
|
||||
import com.central.coupon.mapper.CtripPassengerMapper;
|
||||
import com.central.coupon.mapper.CtripContactMapper;
|
||||
import com.central.coupon.mapper.CtripConsumedMapper;
|
||||
import com.central.coupon.service.IOrderDetailService;
|
||||
import com.central.coupon.service.CtripConsumedService;
|
||||
import com.central.coupon.controller.CtripConsumedController;
|
||||
import com.central.coupon.model.CtripConsumedNoticeResponse;
|
||||
import com.central.common.model.PageResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import com.central.coupon.dto.ConsumeItemDTO;
|
||||
|
||||
/**
|
||||
* 订单明细服务实现类
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class OrderDetailServiceImpl implements IOrderDetailService {
|
||||
|
||||
@Autowired
|
||||
private CtripOrderMapper ctripOrderMapper;
|
||||
|
||||
@Autowired
|
||||
private CtripOrderItemMapper ctripOrderItemMapper;
|
||||
|
||||
@Autowired
|
||||
private CtripPassengerMapper ctripPassengerMapper;
|
||||
|
||||
@Autowired
|
||||
private CtripContactMapper ctripContactMapper;
|
||||
|
||||
@Autowired
|
||||
private CtripConsumedMapper ctripConsumedMapper;
|
||||
|
||||
@Autowired
|
||||
private CtripConsumedService ctripConsumedService;
|
||||
|
||||
@Override
|
||||
public PageResult<OrderDetailVO> queryOrderDetails(OrderQueryDTO queryDTO) {
|
||||
log.info("查询可核销订单明细,手机号: {}, 优惠券价格: {}", queryDTO.getUserPhone(), queryDTO.getCouponPrice());
|
||||
|
||||
try {
|
||||
// 1. 根据手机号查询订单
|
||||
List<CtripOrder> orders = findOrdersByPhone(queryDTO.getUserPhone());
|
||||
if (orders == null || orders.isEmpty()) {
|
||||
log.info("未找到手机号 {} 的订单", queryDTO.getUserPhone());
|
||||
return PageResult.<OrderDetailVO>builder()
|
||||
.data(new ArrayList<>())
|
||||
.count(0L)
|
||||
.resp_code(0)
|
||||
.build();
|
||||
}
|
||||
|
||||
// 2. 筛选符合条件的订单
|
||||
List<CtripOrder> filteredOrders = filterOrders(orders, queryDTO);
|
||||
|
||||
// 3. 分页处理
|
||||
int startIndex = (queryDTO.getPage() - 1) * queryDTO.getSize();
|
||||
int endIndex = Math.min(startIndex + queryDTO.getSize(), filteredOrders.size());
|
||||
List<CtripOrder> pagedOrders = filteredOrders.subList(startIndex, endIndex);
|
||||
|
||||
// 4. 转换为VO
|
||||
List<OrderDetailVO> orderDetails = pagedOrders.stream()
|
||||
.map(this::convertToOrderDetailVO)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return PageResult.<OrderDetailVO>builder()
|
||||
.data(orderDetails)
|
||||
.count((long) filteredOrders.size())
|
||||
.resp_code(0)
|
||||
.build();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("查询可核销订单明细异常", e);
|
||||
return PageResult.<OrderDetailVO>builder()
|
||||
.data(new ArrayList<>())
|
||||
.count(0L)
|
||||
.resp_code(0)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OrderDetailVO getOrderDetail(String otaOrderId) {
|
||||
log.info("查询订单明细,订单号: {}", otaOrderId);
|
||||
|
||||
try {
|
||||
CtripOrder order = ctripOrderMapper.selectByOtaOrderId(otaOrderId);
|
||||
if (order == null) {
|
||||
log.warn("订单不存在,订单号: {}", otaOrderId);
|
||||
return null;
|
||||
}
|
||||
|
||||
return convertToOrderDetailVO(order);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("查询订单明细异常,订单号: {}", otaOrderId, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean consumeOrder(ConsumeOrderDTO consumeDTO) {
|
||||
log.info("核销订单,订单号: {}", consumeDTO.getOtaOrderId());
|
||||
|
||||
try {
|
||||
// 1. 验证订单是否存在
|
||||
CtripOrder order = ctripOrderMapper.selectByOtaOrderId(consumeDTO.getOtaOrderId());
|
||||
if (order == null) {
|
||||
log.error("订单不存在,订单号: {}", consumeDTO.getOtaOrderId());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 验证订单是否可核销
|
||||
if (!validateOrderForConsume(consumeDTO.getOtaOrderId())) {
|
||||
log.error("订单不可核销,订单号: {}", consumeDTO.getOtaOrderId());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. 调用携程核销服务
|
||||
// 由于CtripConsumeRequest是单个核销请求,我们需要为每个item创建一个请求
|
||||
List<CtripConsumedController.CtripConsumeRequest> requests = new ArrayList<>();
|
||||
for (ConsumeItemDTO item : consumeDTO.getItems()) {
|
||||
CtripConsumedController.CtripConsumeRequest request = new CtripConsumedController.CtripConsumeRequest();
|
||||
request.setOtaOrderId(consumeDTO.getOtaOrderId());
|
||||
request.setSupplierOrderId(consumeDTO.getSupplierOrderId());
|
||||
request.setItemId(item.getItemId());
|
||||
request.setUseQuantity(item.getQuantity());
|
||||
request.setPassengerIds(item.getPassengerIds() != null ?
|
||||
List.of(item.getPassengerIds()) : new ArrayList<>());
|
||||
request.setVoucherIds(item.getVoucherIds() != null ?
|
||||
List.of(item.getVoucherIds()) : new ArrayList<>());
|
||||
request.setRemark(consumeDTO.getRemark());
|
||||
requests.add(request);
|
||||
}
|
||||
|
||||
// 使用批量核销接口
|
||||
CtripConsumedController.CtripConsumeBatchRequest batchRequest = new CtripConsumedController.CtripConsumeBatchRequest();
|
||||
batchRequest.setItems(requests);
|
||||
|
||||
CtripConsumedNoticeResponse response = ctripConsumedService.consumeOrderBatch(batchRequest);
|
||||
return response != null && "0000".equals(response.getHeader().getResultCode());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("核销订单异常,订单号: {}", consumeDTO.getOtaOrderId(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validateOrderForConsume(String otaOrderId) {
|
||||
log.info("验证订单是否可核销,订单号: {}", otaOrderId);
|
||||
|
||||
try {
|
||||
// 1. 查询订单
|
||||
CtripOrder order = ctripOrderMapper.selectByOtaOrderId(otaOrderId);
|
||||
if (order == null) {
|
||||
log.warn("订单不存在,订单号: {}", otaOrderId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 检查订单状态
|
||||
if (order.getStatus() == null || (order.getStatus() != 1 && order.getStatus() != 13)) {
|
||||
log.warn("订单状态不正确,订单号: {}, 状态: {}", otaOrderId, order.getStatus());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. 检查订单项是否可核销
|
||||
List<CtripOrderItem> orderItems = ctripOrderItemMapper.selectByOrderId(order.getId());
|
||||
if (orderItems == null || orderItems.isEmpty()) {
|
||||
log.warn("订单项不存在,订单号: {}", otaOrderId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 4. 检查每个订单项是否可核销
|
||||
for (CtripOrderItem item : orderItems) {
|
||||
if (!validateOrderItemForConsume(item)) {
|
||||
log.warn("订单项不可核销,订单号: {}, 商品ID: {}", otaOrderId, item.getItemId());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("验证订单核销异常,订单号: {}", otaOrderId, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据手机号查找订单
|
||||
*/
|
||||
private List<CtripOrder> findOrdersByPhone(String userPhone) {
|
||||
log.info("开始根据手机号查询订单,手机号: {}", userPhone);
|
||||
|
||||
if (StringUtils.isEmpty(userPhone)) {
|
||||
log.warn("手机号为空,返回空列表");
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 1. 通过乘客信息查询订单
|
||||
List<CtripPassenger> passengers = ctripPassengerMapper.selectByPhone(userPhone);
|
||||
log.info("通过乘客信息查询到 {} 条记录", passengers != null ? passengers.size() : 0);
|
||||
|
||||
List<Long> orderIds = new ArrayList<>();
|
||||
|
||||
if (passengers != null && !passengers.isEmpty()) {
|
||||
orderIds.addAll(passengers.stream()
|
||||
.map(CtripPassenger::getOrderId)
|
||||
.distinct()
|
||||
.collect(Collectors.toList()));
|
||||
log.info("从乘客信息中提取到 {} 个订单ID: {}", orderIds.size(), orderIds);
|
||||
}
|
||||
|
||||
// 2. 通过联系人信息查询订单
|
||||
List<CtripContact> contacts = ctripContactMapper.selectByMobile(userPhone);
|
||||
log.info("通过联系人信息查询到 {} 条记录", contacts != null ? contacts.size() : 0);
|
||||
|
||||
if (contacts != null && !contacts.isEmpty()) {
|
||||
List<Long> contactOrderIds = contacts.stream()
|
||||
.map(CtripContact::getOrderId)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
orderIds.addAll(contactOrderIds);
|
||||
log.info("从联系人信息中提取到 {} 个订单ID: {}", contactOrderIds.size(), contactOrderIds);
|
||||
}
|
||||
|
||||
// 3. 查询订单详情
|
||||
if (orderIds.isEmpty()) {
|
||||
log.warn("未找到任何订单ID,返回空列表");
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
log.info("总共找到 {} 个订单ID,开始查询订单详情", orderIds.size());
|
||||
List<CtripOrder> orders = ctripOrderMapper.selectBatchIds(orderIds);
|
||||
log.info("查询到 {} 个订单详情", orders != null ? orders.size() : 0);
|
||||
|
||||
return orders;
|
||||
}
|
||||
|
||||
/**
|
||||
* 筛选订单
|
||||
*/
|
||||
private List<CtripOrder> filterOrders(List<CtripOrder> orders, OrderQueryDTO queryDTO) {
|
||||
log.info("开始筛选订单,原始订单数量: {}", orders.size());
|
||||
|
||||
List<CtripOrder> filteredOrders = orders.stream()
|
||||
.filter(order -> {
|
||||
log.debug("筛选订单: otaOrderId={}, totalAmount={}", order.getOtaOrderId(), order.getTotalAmount());
|
||||
|
||||
// 按优惠券价格筛选
|
||||
if (queryDTO.getCouponPrice() != null) {
|
||||
if (order.getTotalAmount() == null ||
|
||||
!order.getTotalAmount().equals(queryDTO.getCouponPrice())) {
|
||||
log.debug("订单 {} 价格不匹配,期望: {}, 实际: {}",
|
||||
order.getOtaOrderId(), queryDTO.getCouponPrice(), order.getTotalAmount());
|
||||
return false;
|
||||
}
|
||||
log.debug("订单 {} 价格匹配", order.getOtaOrderId());
|
||||
}
|
||||
|
||||
// 如果只查询可核销订单,则过滤掉不可核销的订单
|
||||
if (queryDTO.getOnlyConsumable() != null && queryDTO.getOnlyConsumable()) {
|
||||
if (!validateOrderForConsume(order.getOtaOrderId())) {
|
||||
log.debug("订单 {} 不可核销", order.getOtaOrderId());
|
||||
return false;
|
||||
}
|
||||
log.debug("订单 {} 可核销", order.getOtaOrderId());
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
log.info("筛选后订单数量: {}", filteredOrders.size());
|
||||
return filteredOrders;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为订单明细VO
|
||||
*/
|
||||
private OrderDetailVO convertToOrderDetailVO(CtripOrder order) {
|
||||
OrderDetailVO orderDetail = new OrderDetailVO();
|
||||
orderDetail.setOrderId(order.getId());
|
||||
orderDetail.setOtaOrderId(order.getOtaOrderId());
|
||||
orderDetail.setSupplierOrderId(order.getSupplierOrderId());
|
||||
orderDetail.setOrderStatus(order.getStatus());
|
||||
orderDetail.setOrderStatusDesc(getOrderStatusDesc(order.getStatus()));
|
||||
orderDetail.setTotalAmount(order.getTotalAmount());
|
||||
orderDetail.setCurrency(order.getCurrency());
|
||||
orderDetail.setCreateTime(order.getCreateTime());
|
||||
|
||||
// 查询订单项
|
||||
List<CtripOrderItem> orderItems = ctripOrderItemMapper.selectByOrderId(order.getId());
|
||||
orderDetail.setOrderItems(convertToOrderItemDetailVOList(orderItems));
|
||||
|
||||
// 查询乘客信息
|
||||
List<CtripPassenger> passengers = ctripPassengerMapper.selectByOrderId(order.getId());
|
||||
orderDetail.setPassengers(convertToPassengerDetailVOList(passengers));
|
||||
|
||||
// 查询联系人信息
|
||||
List<CtripContact> contacts = ctripContactMapper.selectByOrderId(order.getId());
|
||||
if (contacts != null && !contacts.isEmpty()) {
|
||||
orderDetail.setContact(convertToContactDetailVO(contacts.get(0)));
|
||||
}
|
||||
|
||||
// 判断是否可核销
|
||||
boolean canConsume = validateOrderForConsume(order.getOtaOrderId());
|
||||
orderDetail.setCanConsume(canConsume);
|
||||
if (!canConsume) {
|
||||
orderDetail.setConsumeReason(getConsumeReason(order));
|
||||
}
|
||||
|
||||
return orderDetail;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为订单项明细VO列表
|
||||
*/
|
||||
private List<OrderItemDetailVO> convertToOrderItemDetailVOList(List<CtripOrderItem> orderItems) {
|
||||
if (orderItems == null || orderItems.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
return orderItems.stream().map(item -> {
|
||||
OrderItemDetailVO itemDetail = new OrderItemDetailVO();
|
||||
itemDetail.setItemId(item.getId());
|
||||
itemDetail.setProductId(item.getItemId());
|
||||
itemDetail.setItemName(item.getItemName());
|
||||
itemDetail.setItemType(item.getItemType());
|
||||
itemDetail.setPlu(item.getPlu());
|
||||
itemDetail.setQuantity(item.getQuantity());
|
||||
itemDetail.setUnitPrice(item.getUnitPrice());
|
||||
itemDetail.setTotalPrice(item.getTotalPrice());
|
||||
itemDetail.setCurrency(item.getCurrency());
|
||||
itemDetail.setUseStartDate(item.getUseStartDate());
|
||||
itemDetail.setUseEndDate(item.getUseEndDate());
|
||||
|
||||
// 计算已使用数量和剩余数量
|
||||
int usedQuantity = getUsedQuantity(item.getId());
|
||||
itemDetail.setUsedQuantity(usedQuantity);
|
||||
itemDetail.setAvailableQuantity(item.getQuantity() - usedQuantity);
|
||||
itemDetail.setCanConsume(usedQuantity < item.getQuantity());
|
||||
|
||||
return itemDetail;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为乘客明细VO列表
|
||||
*/
|
||||
private List<PassengerDetailVO> convertToPassengerDetailVOList(List<CtripPassenger> passengers) {
|
||||
if (passengers == null || passengers.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
return passengers.stream().map(passenger -> {
|
||||
PassengerDetailVO passengerDetail = new PassengerDetailVO();
|
||||
passengerDetail.setPassengerId(passenger.getPassengerId());
|
||||
passengerDetail.setPassengerName(passenger.getPassengerName());
|
||||
passengerDetail.setPhone(passenger.getPhone());
|
||||
passengerDetail.setPassengerStatus(0); // 默认状态
|
||||
passengerDetail.setPassengerStatusDesc("未使用");
|
||||
passengerDetail.setIdCard(passenger.getIdNumber());
|
||||
passengerDetail.setGender(passenger.getPassengerType());
|
||||
passengerDetail.setAge(null); // CtripPassenger没有age字段
|
||||
return passengerDetail;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为联系人明细VO
|
||||
*/
|
||||
private ContactDetailVO convertToContactDetailVO(CtripContact contact) {
|
||||
ContactDetailVO contactDetail = new ContactDetailVO();
|
||||
contactDetail.setName(contact.getName());
|
||||
contactDetail.setMobile(contact.getMobile());
|
||||
contactDetail.setEmail(contact.getEmail());
|
||||
contactDetail.setContactType("CONTACT"); // 默认联系人类型
|
||||
return contactDetail;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证订单项是否可核销
|
||||
*/
|
||||
private boolean validateOrderItemForConsume(CtripOrderItem item) {
|
||||
// 检查使用日期
|
||||
LocalDate today = LocalDate.now();
|
||||
if (item.getUseStartDate() != null && today.isBefore(item.getUseStartDate())) {
|
||||
return false;
|
||||
}
|
||||
if (item.getUseEndDate() != null && today.isAfter(item.getUseEndDate())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查剩余数量
|
||||
int usedQuantity = getUsedQuantity(item.getId());
|
||||
return usedQuantity < item.getQuantity();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已使用数量
|
||||
*/
|
||||
private int getUsedQuantity(Long itemId) {
|
||||
try {
|
||||
List<CtripConsumed> consumedRecords = ctripConsumedMapper.selectByItemId(itemId.toString());
|
||||
return consumedRecords.stream()
|
||||
.mapToInt(CtripConsumed::getUseQuantity)
|
||||
.sum();
|
||||
} catch (Exception e) {
|
||||
log.error("查询已使用数量异常,商品ID: {}", itemId, e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单状态描述
|
||||
*/
|
||||
private String getOrderStatusDesc(Integer status) {
|
||||
if (status == null) return "未知";
|
||||
|
||||
switch (status) {
|
||||
case 0: return "待确认";
|
||||
case 1: return "已确认";
|
||||
case 2: return "已取消";
|
||||
case 3: return "已退款";
|
||||
case 4: return "已核销";
|
||||
case 5: return "已取消";
|
||||
case 11: return "待支付";
|
||||
case 12: return "支付中";
|
||||
case 13: return "支付确认";
|
||||
case 14: return "预订单已取消";
|
||||
default: return "未知";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取不可核销原因
|
||||
*/
|
||||
private String getConsumeReason(CtripOrder order) {
|
||||
if (order.getStatus() == null || (order.getStatus() != 1 && order.getStatus() != 13)) {
|
||||
return "订单状态不正确";
|
||||
}
|
||||
|
||||
List<CtripOrderItem> orderItems = ctripOrderItemMapper.selectByOrderId(order.getId());
|
||||
if (orderItems == null || orderItems.isEmpty()) {
|
||||
return "订单项不存在";
|
||||
}
|
||||
|
||||
for (CtripOrderItem item : orderItems) {
|
||||
if (!validateOrderItemForConsume(item)) {
|
||||
return "商品已过期或无可用数量";
|
||||
}
|
||||
}
|
||||
|
||||
return "未知原因";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.central.coupon.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 联系人明细VO
|
||||
*/
|
||||
@Data
|
||||
public class ContactDetailVO {
|
||||
|
||||
/**
|
||||
* 联系人姓名
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 联系人手机号
|
||||
*/
|
||||
private String mobile;
|
||||
|
||||
/**
|
||||
* 联系人邮箱
|
||||
*/
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 联系人类型
|
||||
*/
|
||||
private String contactType;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.central.coupon.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 订单明细VO
|
||||
*/
|
||||
@Data
|
||||
public class OrderDetailVO {
|
||||
|
||||
/**
|
||||
* 订单ID
|
||||
*/
|
||||
private Long orderId;
|
||||
|
||||
/**
|
||||
* 携程订单号
|
||||
*/
|
||||
private String otaOrderId;
|
||||
|
||||
/**
|
||||
* 供应商订单号
|
||||
*/
|
||||
private String supplierOrderId;
|
||||
|
||||
/**
|
||||
* 订单状态
|
||||
*/
|
||||
private Integer orderStatus;
|
||||
|
||||
/**
|
||||
* 订单状态描述
|
||||
*/
|
||||
private String orderStatusDesc;
|
||||
|
||||
/**
|
||||
* 订单总金额
|
||||
*/
|
||||
private BigDecimal totalAmount;
|
||||
|
||||
/**
|
||||
* 货币类型
|
||||
*/
|
||||
private String currency;
|
||||
|
||||
/**
|
||||
* 订单创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 订单项列表
|
||||
*/
|
||||
private List<OrderItemDetailVO> orderItems;
|
||||
|
||||
/**
|
||||
* 乘客信息列表
|
||||
*/
|
||||
private List<PassengerDetailVO> passengers;
|
||||
|
||||
/**
|
||||
* 联系人信息
|
||||
*/
|
||||
private ContactDetailVO contact;
|
||||
|
||||
/**
|
||||
* 是否可用于核销
|
||||
*/
|
||||
private Boolean canConsume;
|
||||
|
||||
/**
|
||||
* 不可核销原因
|
||||
*/
|
||||
private String consumeReason;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.central.coupon.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 订单项明细VO
|
||||
*/
|
||||
@Data
|
||||
public class OrderItemDetailVO {
|
||||
|
||||
/**
|
||||
* 订单项ID
|
||||
*/
|
||||
private Long itemId;
|
||||
|
||||
/**
|
||||
* 商品ID
|
||||
*/
|
||||
private String productId;
|
||||
|
||||
/**
|
||||
* 商品名称
|
||||
*/
|
||||
private String itemName;
|
||||
|
||||
/**
|
||||
* 商品类型
|
||||
*/
|
||||
private String itemType;
|
||||
|
||||
/**
|
||||
* PLU码
|
||||
*/
|
||||
private String plu;
|
||||
|
||||
/**
|
||||
* 数量
|
||||
*/
|
||||
private Integer quantity;
|
||||
|
||||
/**
|
||||
* 单价
|
||||
*/
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
/**
|
||||
* 总价
|
||||
*/
|
||||
private BigDecimal totalPrice;
|
||||
|
||||
/**
|
||||
* 货币类型
|
||||
*/
|
||||
private String currency;
|
||||
|
||||
/**
|
||||
* 使用开始日期
|
||||
*/
|
||||
private LocalDate useStartDate;
|
||||
|
||||
/**
|
||||
* 使用结束日期
|
||||
*/
|
||||
private LocalDate useEndDate;
|
||||
|
||||
/**
|
||||
* 是否可用于核销
|
||||
*/
|
||||
private Boolean canConsume;
|
||||
|
||||
/**
|
||||
* 已使用数量
|
||||
*/
|
||||
private Integer usedQuantity;
|
||||
|
||||
/**
|
||||
* 剩余可用数量
|
||||
*/
|
||||
private Integer availableQuantity;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.central.coupon.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 乘客明细VO
|
||||
*/
|
||||
@Data
|
||||
public class PassengerDetailVO {
|
||||
|
||||
/**
|
||||
* 乘客ID
|
||||
*/
|
||||
private String passengerId;
|
||||
|
||||
/**
|
||||
* 乘客姓名
|
||||
*/
|
||||
private String passengerName;
|
||||
|
||||
/**
|
||||
* 乘客手机号
|
||||
*/
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 乘客状态
|
||||
*/
|
||||
private Integer passengerStatus;
|
||||
|
||||
/**
|
||||
* 乘客状态描述
|
||||
*/
|
||||
private String passengerStatusDesc;
|
||||
|
||||
/**
|
||||
* 身份证号
|
||||
*/
|
||||
private String idCard;
|
||||
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private String gender;
|
||||
|
||||
/**
|
||||
* 年龄
|
||||
*/
|
||||
private Integer age;
|
||||
}
|
||||
Reference in New Issue
Block a user