package com.xiaopeng.itinerary.controller; import com.xiaopeng.api.feignClient.order.XpOrderClient; import com.xiaopeng.api.model.order.XpOrder; import com.xiaopeng.common.core.domain.R; import io.seata.spring.annotation.GlobalTransactional; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType; import org.apache.shardingsphere.transaction.core.TransactionType; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.math.BigDecimal; import java.time.LocalDateTime; /** * Seata 分布式事务测试接口 */ @Slf4j @RestController @RequestMapping("/test/seata") @Api(tags = "Seata分布式事务测试") public class SeataTestController { @Resource private XpOrderClient xpOrderClient; /** * 测试分布式事务 - 正常提交 * 调用order服务创建订单,正常返回,事务提交 */ @GetMapping("/commit") @ApiOperation("测试分布式事务-正常提交") @GlobalTransactional(name = "seata-test-commit", rollbackFor = Exception.class) public R testCommit() { XpOrder order = buildTestOrder(); log.info("[Seata测试] 开始创建测试订单,事务正常提交场景"); R result = xpOrderClient.addOrder(order); log.info("[Seata测试] 订单创建结果: code={}, orderId={}", result.getCode(), result.getData()); return R.ok(result.getData(), "分布式事务正常提交成功,订单ID: " + result.getData()); } /** * 测试分布式事务 - 回滚 * 调用order服务创建订单后,手动抛异常触发全局回滚 * 验证:调用后去数据库查,订单应该不存在 */ @GetMapping("/rollback") @ApiOperation("测试分布式事务-回滚") @Transactional @ShardingSphereTransactionType(TransactionType.BASE) public R testRollback() { XpOrder order = buildTestOrder(); log.info("[Seata测试] 开始创建测试订单,事务回滚场景"); R result = xpOrderClient.addOrder(order); log.info("[Seata测试] 订单已创建 orderId={},即将抛出异常触发回滚...", result.getData()); // 故意抛异常,触发Seata全局事务回滚 throw new RuntimeException("[Seata测试] 模拟业务异常,触发分布式事务回滚"); } private XpOrder buildTestOrder() { LocalDateTime now = LocalDateTime.now(); XpOrder order = new XpOrder(); // 主键由数据库或框架生成,不设置 order.setAppUserId(99999L); order.setCityId(1L); order.setVehicleId(1001L); order.setFleetId(1L); order.setOrderNumber("SEATA_TEST_" + System.currentTimeMillis()); order.setTransactionId("TXN_SEATA_TEST_" + System.currentTimeMillis()); order.setOrderSource(1); order.setRideTime(now); // 下单地点(深圳南山) order.setPlacementLon(113.934528); order.setPlacementLat(22.530381); order.setPlacementAddress("Seata分布式事务测试-下单地点"); // 出发地 order.setStartLon(113.934528); order.setStartLat(22.530381); order.setStartAddress("测试起点-科技园"); // 目的地 order.setEndLon(113.944528); order.setEndLat(22.540381); order.setEndAddress("测试终点-深圳湾"); // 上车点 order.setBoardingLon(113.934528); order.setBoardingLat(22.530381); order.setBoardingAddress("测试上车点-科技园地铁站"); order.setBoardingTime(now); // 下车点 order.setGetoffLon(113.944528); order.setGetoffLat(22.540381); order.setGetoffAddress("测试下车点-深圳湾公园"); order.setGetoffTime(now.plusMinutes(15)); // 支付信息 order.setPayType(1); order.setOrderMoney(new BigDecimal("25.00")); order.setPayMoney(new BigDecimal("20.00")); order.setPrepayMoney(new BigDecimal("25.00")); // 后备箱 order.setOpenTrunk(0); // 里程费用 order.setStartMileage(3.0); order.setStartMoney(new BigDecimal("10.00")); order.setMileageKilometers(5.5); order.setMileageMoney(new BigDecimal("8.00")); // 时长费用 order.setDuration(15.0); order.setDurationType(1); order.setDurationMoney(new BigDecimal("5.00")); // 等待费用 order.setWait(2.0); order.setWaitMoney(new BigDecimal("2.00")); // 远途费用 order.setLongDistance(0.0); order.setLongDistanceType(1); order.setLongDistancemoney(new BigDecimal("0.00")); // 优惠 order.setRedPacketMoney(new BigDecimal("3.00")); order.setCouponMoney(new BigDecimal("2.00")); order.setRedPacketId(0L); order.setCouponId(0L); // 订单状态:待接单 order.setStatus(1); // 城市信息 order.setCityName("深圳市"); order.setCityCode("440300"); // 站点信息 order.setStartPointId(1L); order.setEndPointId(2L); order.setStartPointName("科技园站"); order.setEndPointName("深圳湾站"); // 预估时间 order.setEstimatedTime(now.plusMinutes(20)); // 区域与时段 order.setXpElectronicFenceAreaId(1L); order.setTimeSlot("08:00-10:00"); // 车辆到达、接单、行程时间 order.setArrivalTime(now.plusMinutes(5)); order.setAcceptanceTime(now); order.setTripBegin(now.plusMinutes(5)); order.setTripEnd(now.plusMinutes(20)); // 队列名称 order.setQueueName("default_queue"); // BaseModel 字段(通常自动填充,这里显式设置避免null) order.setCreateBy("seata-test"); order.setCreateTime(now); order.setUpdateBy("seata-test"); order.setUpdateTime(now); order.setDeleted(false); return order; } }