Sfoglia il codice sorgente

feat(store): 添加预约订单物理删除功能并优化预约流程

- 在UserReservationMapper中添加physicalDeleteById方法用于物理删除预约
- 在UserReservationOrderMapper中添加physicalDeleteByReservationId方法用于按预约ID物理删除订单
- 实现UserReservationOrderService中的physicalDeleteByReservationId接口
- 更新UserReservationService中的删除预约逻辑为假删除模式
- 优化预约订单状态设置逻辑,根据门店预订配置动态设置订单状态
- 添加fillOrderFromStoreSettings方法用于从门店设置填充订单信息
- 修复updateReservation方法中的订单同步逻辑,确保与新建订单行为一致
- 实现removeReservation方法中的假删除机制,将相关表的delete_flag置为1
fcw 1 mese fa
parent
commit
49ee627c30

+ 8 - 0
alien-entity/src/main/java/shop/alien/mapper/UserReservationMapper.java

@@ -32,4 +32,12 @@ public interface UserReservationMapper extends BaseMapper<UserReservation> {
             @Param("dateTo") Date dateTo,
             @Param("orderStatus") Integer orderStatus
     );
+
+    /**
+     * 按主键物理删除预约(与 add 新建的表对应,用于 delete 接口)
+     *
+     * @param id 预约ID
+     * @return 删除行数
+     */
+    int physicalDeleteById(@Param("id") Integer id);
 }

+ 10 - 0
alien-entity/src/main/java/shop/alien/mapper/UserReservationOrderMapper.java

@@ -1,7 +1,9 @@
 package shop.alien.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Delete;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 import shop.alien.entity.store.UserReservationOrder;
 
 /**
@@ -12,4 +14,12 @@ import shop.alien.entity.store.UserReservationOrder;
 @Mapper
 public interface UserReservationOrderMapper extends BaseMapper<UserReservationOrder> {
 
+    /**
+     * 按预约ID物理删除订单(与 add 新建的 user_reservation_order 对应,用于 delete 接口)
+     *
+     * @param reservationId 预约ID
+     * @return 删除行数
+     */
+    @Delete("DELETE FROM user_reservation_order WHERE reservation_id = #{reservationId}")
+    int physicalDeleteByReservationId(@Param("reservationId") Integer reservationId);
 }

+ 4 - 0
alien-entity/src/main/resources/mapper/UserReservationMapper.xml

@@ -131,4 +131,8 @@
             ur.created_time DESC
     </select>
 
+    <delete id="physicalDeleteById">
+        DELETE FROM user_reservation WHERE id = #{id}
+    </delete>
+
 </mapper>

+ 8 - 0
alien-store/src/main/java/shop/alien/store/service/UserReservationOrderService.java

@@ -32,4 +32,12 @@ public interface UserReservationOrderService extends IService<UserReservationOrd
      * @return 订单编号
      */
     String generateOrderSn();
+
+    /**
+     * 按预约ID物理删除订单(用于 delete 接口与 add 新建数据对应)
+     *
+     * @param reservationId 预约ID
+     * @return 删除行数
+     */
+    int physicalDeleteByReservationId(Integer reservationId);
 }

+ 1 - 1
alien-store/src/main/java/shop/alien/store/service/UserReservationService.java

@@ -35,7 +35,7 @@ public interface UserReservationService extends IService<UserReservation> {
     boolean updateReservation(UserReservationDTO dto);
 
     /**
-     * 删除预约(逻辑删除主表,物理删除关联表
+     * 删除预约(假删除:将 user_reservation_order、user_reservation_table、user_reservation 的 delete_flag 置为 1,与 add 新建表数据对应
      *
      * @param id 预约ID
      * @return 是否成功

+ 8 - 0
alien-store/src/main/java/shop/alien/store/service/impl/UserReservationOrderServiceImpl.java

@@ -57,4 +57,12 @@ public class UserReservationOrderServiceImpl extends ServiceImpl<UserReservation
         int random = ThreadLocalRandom.current().nextInt(1000000);
         return "YS" + dateStr + String.format("%06d", random);
     }
+
+    @Override
+    public int physicalDeleteByReservationId(Integer reservationId) {
+        if (reservationId == null) {
+            return 0;
+        }
+        return baseMapper.physicalDeleteByReservationId(reservationId);
+    }
 }

+ 52 - 7
alien-store/src/main/java/shop/alien/store/service/impl/UserReservationServiceImpl.java

@@ -2,6 +2,7 @@ package shop.alien.store.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -15,6 +16,7 @@ import shop.alien.entity.store.dto.UserReservationDTO;
 import shop.alien.entity.store.vo.StoreMainInfoVo;
 import shop.alien.entity.store.vo.StoreReservationListVo;
 import shop.alien.entity.store.vo.UserReservationVo;
+import shop.alien.mapper.UserReservationOrderMapper;
 import shop.alien.store.vo.BookingTableItemVo;
 import shop.alien.store.vo.ReservationOrderDetailVo;
 import shop.alien.mapper.UserReservationMapper;
@@ -53,6 +55,8 @@ public class UserReservationServiceImpl extends ServiceImpl<UserReservationMappe
 
     private final ReservationOrderPaymentTimeoutService reservationOrderPaymentTimeoutService;
 
+    private final UserReservationOrderMapper userReservationOrderMapper;
+
     /** 预约状态:待确认 */
     private static final int STATUS_PENDING = 0;
     /** 预约状态:已取消(不参与约满统计与展示) */
@@ -107,10 +111,22 @@ public class UserReservationServiceImpl extends ServiceImpl<UserReservationMappe
         order.setReservationId(reservation.getId());
         order.setUserId(reservation.getUserId());
         order.setStoreId(reservation.getStoreId());
-        order.setOrderStatus(0);
-        order.setPaymentStatus(0);
+        // 当store_booking_settings.reservation = 1时,订单状态为待使用(1),不为1时,订单状态为待支付(0)
+        order.setOrderStatus(
+                storeBookingSettingsService.getByStoreId(reservation.getStoreId()) != null && "1".equals(
+                        storeBookingSettingsService.getByStoreId(reservation.getStoreId()).getReservation()) ? 0 : 1);
         order.setIsMerchantReservation(0);
+        fillOrderFromStoreSettings(order, reservation);
+        return order;
+    }
 
+    /**
+     * 按门店预订配置填充订单的收费/订金/取消政策等字段(与 add 一致)
+     */
+    private void fillOrderFromStoreSettings(UserReservationOrder order, UserReservation reservation) {
+        if (reservation == null || reservation.getStoreId() == null) {
+            return;
+        }
         StoreBookingSettings settings = storeBookingSettingsService.getByStoreId(reservation.getStoreId());
         if (settings != null && "1".equals(settings.getReservation()) && settings.getReservationMoney() != null && settings.getReservationMoney() > 0) {
             order.setOrderCostType(1);
@@ -128,8 +144,6 @@ public class UserReservationServiceImpl extends ServiceImpl<UserReservationMappe
             order.setDepositAmount(BigDecimal.ZERO);
             order.setCancellationPolicyType(0);
         }
-
-        return order;
     }
 
     @Override
@@ -137,7 +151,9 @@ public class UserReservationServiceImpl extends ServiceImpl<UserReservationMappe
         if (dto.getId() == null) {
             throw new RuntimeException("预约ID不能为空");
         }
-        UserReservation existing = this.getById(dto.getId());
+        // 根据订单id获取预约信息
+        UserReservationOrder userReservationOrder = userReservationOrderMapper.selectById(dto.getId());
+        UserReservation existing = this.getById(userReservationOrder.getReservationId());
         if (existing == null) {
             throw new RuntimeException("预约不存在");
         }
@@ -148,16 +164,45 @@ public class UserReservationServiceImpl extends ServiceImpl<UserReservationMappe
         this.updateById(entity);
 
         saveReservationTables(existing.getId(), dto.getTableIds());
+
+        // 与 add 一致:同步 user_reservation_order(无则创建,有则仅待支付时按门店配置刷新)
+        UserReservation updatedReservation = this.getById(existing.getId());
+        LambdaQueryWrapper<UserReservationOrder> orderWrapper = new LambdaQueryWrapper<>();
+        orderWrapper.eq(UserReservationOrder::getReservationId, existing.getId()).last("LIMIT 1");
+        UserReservationOrder order = userReservationOrderService.getOne(orderWrapper);
+        if (order == null) {
+            order = buildReservationOrder(updatedReservation);
+            userReservationOrderService.save(order);
+            if (order.getOrderCostType() != null && order.getOrderCostType() == 1 && order.getOrderSn() != null) {
+                reservationOrderPaymentTimeoutService.setReservationOrderPaymentTimeout(order.getOrderSn(), 15 * 60);
+            }
+        } else if (order.getOrderStatus() != null && order.getOrderStatus() == 0) {
+            fillOrderFromStoreSettings(order, updatedReservation);
+            userReservationOrderService.updateById(order);
+            if (order.getOrderCostType() != null && order.getOrderCostType() == 1 && order.getOrderSn() != null) {
+                reservationOrderPaymentTimeoutService.setReservationOrderPaymentTimeout(order.getOrderSn(), 15 * 60);
+            }
+        }
         return true;
     }
 
     @Override
     public boolean removeReservation(Integer id) {
-        UserReservation one = this.getById(id);
+
+        UserReservationOrder one = userReservationOrderService.getOne(new LambdaUpdateWrapper<UserReservationOrder>()
+                .eq(UserReservationOrder::getId, id));
         if (one == null) {
             throw new RuntimeException("预约不存在");
         }
-        userReservationTableMapper.physicalDeleteByReservationId(id);
+        // 假删除:将 delete_flag 置为 1(与 add 对应的三张表)
+        userReservationOrderService.update(
+                new LambdaUpdateWrapper<UserReservationOrder>()
+                        .eq(UserReservationOrder::getId, one.getId())
+                        .set(UserReservationOrder::getOrderStatus, 4));
+        userReservationTableMapper.update(null,
+                new LambdaUpdateWrapper<UserReservationTable>()
+                        .eq(UserReservationTable::getReservationId, one.getReservationId())
+                        .set(UserReservationTable::getDeleteFlag, 1));
         return this.removeById(id);
     }