完成模块2
This commit was merged in pull request #14.
This commit is contained in:
@@ -74,6 +74,13 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 会员模块依赖 -->
|
||||
<dependency>
|
||||
<groupId>cn.novalon.gym.manage</groupId>
|
||||
<artifactId>gym-member</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+50
-2
@@ -52,12 +52,60 @@ public interface GroupCourseBookingDao extends R2dbcRepository<GroupCourseBookin
|
||||
Mono<GroupCourseBookingEntity> findByCourseIdAndMemberIdAndStatusAndDeletedAtIsNull(Long courseId, Long memberId, String status);
|
||||
|
||||
/**
|
||||
* 根据会员卡ID查询预约记录
|
||||
* 根据会员卡记录ID查询预约记录
|
||||
*/
|
||||
Flux<GroupCourseBookingEntity> findByMemberCardIdAndDeletedAtIsNull(Long memberCardId);
|
||||
Flux<GroupCourseBookingEntity> findByMemberCardRecordIdAndDeletedAtIsNull(Long memberCardRecordId);
|
||||
|
||||
/**
|
||||
* 统计团课已预约人数
|
||||
*/
|
||||
Mono<Long> countByCourseIdAndStatusAndDeletedAtIsNull(Long courseId, String status);
|
||||
|
||||
/**
|
||||
* 查询会员是否有时间冲突的预约(状态为已预约且未取消)
|
||||
* 时间冲突条件:新课程的开始时间 < 已预约课程的结束时间 且 新课程的结束时间 > 已预约课程的开始时间
|
||||
*/
|
||||
@org.springframework.data.r2dbc.repository.Query("SELECT * FROM group_course_booking b " +
|
||||
"JOIN group_course c ON b.course_id = c.id " +
|
||||
"WHERE b.member_id = :memberId " +
|
||||
"AND b.status = '0' " +
|
||||
"AND b.deleted_at IS NULL " +
|
||||
"AND c.deleted_at IS NULL " +
|
||||
"AND :newStartTime < c.end_time " +
|
||||
"AND :newEndTime > c.start_time")
|
||||
Flux<GroupCourseBookingEntity> findConflictingBookings(Long memberId, java.time.LocalDateTime newStartTime, java.time.LocalDateTime newEndTime);
|
||||
|
||||
/**
|
||||
* 更新预约状态
|
||||
*/
|
||||
@org.springframework.data.r2dbc.repository.Modifying
|
||||
@org.springframework.data.r2dbc.repository.Query("UPDATE group_course_booking SET status = :status, cancel_time = :cancelTime, updated_at = :updatedAt WHERE id = :id AND deleted_at IS NULL")
|
||||
Mono<Integer> updateStatus(Long id, String status, java.time.LocalDateTime cancelTime, java.time.LocalDateTime updatedAt);
|
||||
|
||||
/**
|
||||
* 软删除预约记录
|
||||
*/
|
||||
@org.springframework.data.r2dbc.repository.Modifying
|
||||
@org.springframework.data.r2dbc.repository.Query("UPDATE group_course_booking SET deleted_at = :deletedAt WHERE id = :id")
|
||||
Mono<Integer> softDelete(Long id, java.time.LocalDateTime deletedAt);
|
||||
|
||||
/**
|
||||
* 查询已开始课程但未到场会员的预约记录
|
||||
* 条件:课程开始时间早于当前时间,预约状态为已预约(0)
|
||||
*/
|
||||
@org.springframework.data.r2dbc.repository.Query("SELECT b.* FROM group_course_booking b " +
|
||||
"JOIN group_course c ON b.course_id = c.id " +
|
||||
"WHERE b.status = '0' " +
|
||||
"AND b.deleted_at IS NULL " +
|
||||
"AND c.deleted_at IS NULL " +
|
||||
"AND c.status = '0' " +
|
||||
"AND c.start_time < CURRENT_TIMESTAMP")
|
||||
Flux<GroupCourseBookingEntity> findAbsentMembers();
|
||||
|
||||
/**
|
||||
* 批量更新预约状态为缺席
|
||||
*/
|
||||
@org.springframework.data.r2dbc.repository.Modifying
|
||||
@org.springframework.data.r2dbc.repository.Query("UPDATE group_course_booking SET status = '3', updated_at = :updatedAt WHERE id = :id AND deleted_at IS NULL")
|
||||
Mono<Integer> updateToAbsent(Long id, java.time.LocalDateTime updatedAt);
|
||||
}
|
||||
+16
@@ -3,11 +3,15 @@ package cn.novalon.gym.manage.groupcourse.dao;
|
||||
|
||||
import cn.novalon.gym.manage.groupcourse.entity.GroupCourseEntity;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.r2dbc.repository.Modifying;
|
||||
import org.springframework.data.r2dbc.repository.Query;
|
||||
import org.springframework.data.r2dbc.repository.R2dbcRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Repository
|
||||
public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long> {
|
||||
|
||||
@@ -20,4 +24,16 @@ public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long>
|
||||
Flux<GroupCourseEntity> findAllByDeletedAtIsNull();
|
||||
|
||||
Flux<GroupCourseEntity> findAllByDeletedAtIsNull(Sort sort);
|
||||
|
||||
@Modifying
|
||||
@Query("UPDATE group_course SET status = '1', updated_at = :updatedAt WHERE id = :id AND deleted_at IS NULL")
|
||||
Mono<Integer> cancelCourse(Long id, LocalDateTime updatedAt);
|
||||
|
||||
@Modifying
|
||||
@Query("UPDATE group_course SET current_members = current_members + :delta, updated_at = :updatedAt WHERE id = :id AND deleted_at IS NULL")
|
||||
Mono<Integer> updateCurrentMembers(Long id, Integer delta, LocalDateTime updatedAt);
|
||||
|
||||
@Modifying
|
||||
@Query("UPDATE group_course SET deleted_at = :deletedAt WHERE id = :id")
|
||||
Mono<Integer> softDelete(Long id, LocalDateTime deletedAt);
|
||||
}
|
||||
|
||||
+24
@@ -52,6 +52,14 @@ public class GroupCourse extends BaseDomain{
|
||||
@Schema(description = "课程描述", example = "从入门到入土")
|
||||
private String description;
|
||||
|
||||
//点卡额度(消耗次数)
|
||||
@Schema(description = "点卡额度(消耗次数)", example = "1")
|
||||
private Integer pointCardAmount;
|
||||
|
||||
//储值卡额度(消耗金额)
|
||||
@Schema(description = "储值卡额度(消耗金额)", example = "50.00")
|
||||
private java.math.BigDecimal storedValueAmount;
|
||||
|
||||
public String getCourseName() {
|
||||
return courseName;
|
||||
}
|
||||
@@ -139,4 +147,20 @@ public class GroupCourse extends BaseDomain{
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Integer getPointCardAmount() {
|
||||
return pointCardAmount;
|
||||
}
|
||||
|
||||
public void setPointCardAmount(Integer pointCardAmount) {
|
||||
this.pointCardAmount = pointCardAmount;
|
||||
}
|
||||
|
||||
public java.math.BigDecimal getStoredValueAmount() {
|
||||
return storedValueAmount;
|
||||
}
|
||||
|
||||
public void setStoredValueAmount(java.math.BigDecimal storedValueAmount) {
|
||||
this.storedValueAmount = storedValueAmount;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -25,9 +25,9 @@ public class GroupCourseBooking extends BaseDomain {
|
||||
@Schema(description = "会员ID", example = "1")
|
||||
private Long memberId;
|
||||
|
||||
//会员卡ID
|
||||
@Schema(description = "会员卡ID", example = "1")
|
||||
private Long memberCardId;
|
||||
//会员卡记录ID
|
||||
@Schema(description = "会员卡记录ID", example = "1")
|
||||
private Long memberCardRecordId;
|
||||
|
||||
//预约时间
|
||||
@Schema(description = "预约时间", example = "2026-06-01 10:00:00")
|
||||
@@ -77,12 +77,12 @@ public class GroupCourseBooking extends BaseDomain {
|
||||
this.memberId = memberId;
|
||||
}
|
||||
|
||||
public Long getMemberCardId() {
|
||||
return memberCardId;
|
||||
public Long getMemberCardRecordId() {
|
||||
return memberCardRecordId;
|
||||
}
|
||||
|
||||
public void setMemberCardId(Long memberCardId) {
|
||||
this.memberCardId = memberCardId;
|
||||
public void setMemberCardRecordId(Long memberCardRecordId) {
|
||||
this.memberCardRecordId = memberCardRecordId;
|
||||
}
|
||||
|
||||
public LocalDateTime getBookingTime() {
|
||||
|
||||
+6
-6
@@ -23,9 +23,9 @@ public class GroupCourseBookingEntity extends BaseEntity {
|
||||
@Column("member_id")
|
||||
private Long memberId;
|
||||
|
||||
//会员卡ID
|
||||
//会员卡记录ID
|
||||
@Column("member_card_id")
|
||||
private Long memberCardId;
|
||||
private Long memberCardRecordId;
|
||||
|
||||
//预约时间
|
||||
@Column("booking_time")
|
||||
@@ -71,12 +71,12 @@ public class GroupCourseBookingEntity extends BaseEntity {
|
||||
this.memberId = memberId;
|
||||
}
|
||||
|
||||
public Long getMemberCardId() {
|
||||
return memberCardId;
|
||||
public Long getMemberCardRecordId() {
|
||||
return memberCardRecordId;
|
||||
}
|
||||
|
||||
public void setMemberCardId(Long memberCardId) {
|
||||
this.memberCardId = memberCardId;
|
||||
public void setMemberCardRecordId(Long memberCardRecordId) {
|
||||
this.memberCardRecordId = memberCardRecordId;
|
||||
}
|
||||
|
||||
public LocalDateTime getBookingTime() {
|
||||
|
||||
+24
@@ -54,6 +54,14 @@ public class GroupCourseEntity extends BaseEntity {
|
||||
@Column("description")
|
||||
private String description;
|
||||
|
||||
//点卡额度(消耗次数)
|
||||
@Column("point_card_amount")
|
||||
private Integer pointCardAmount;
|
||||
|
||||
//储值卡额度(消耗金额)
|
||||
@Column("stored_value_amount")
|
||||
private java.math.BigDecimal storedValueAmount;
|
||||
|
||||
public String getCourseName() {
|
||||
return courseName;
|
||||
}
|
||||
@@ -141,4 +149,20 @@ public class GroupCourseEntity extends BaseEntity {
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Integer getPointCardAmount() {
|
||||
return pointCardAmount;
|
||||
}
|
||||
|
||||
public void setPointCardAmount(Integer pointCardAmount) {
|
||||
this.pointCardAmount = pointCardAmount;
|
||||
}
|
||||
|
||||
public java.math.BigDecimal getStoredValueAmount() {
|
||||
return storedValueAmount;
|
||||
}
|
||||
|
||||
public void setStoredValueAmount(java.math.BigDecimal storedValueAmount) {
|
||||
this.storedValueAmount = storedValueAmount;
|
||||
}
|
||||
}
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package cn.novalon.gym.manage.groupcourse.enums;
|
||||
|
||||
/**
|
||||
* 团课状态机事件枚举
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-06-02
|
||||
*/
|
||||
public enum CourseEvent {
|
||||
|
||||
CANCEL("取消课程"),
|
||||
END("结束课程"),
|
||||
START("开始课程");
|
||||
|
||||
private final String desc;
|
||||
|
||||
CourseEvent(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package cn.novalon.gym.manage.groupcourse.enums;
|
||||
|
||||
/**
|
||||
* 团课状态枚举
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-06-02
|
||||
*/
|
||||
public enum CourseStatus {
|
||||
|
||||
NORMAL(0L, "正常"),
|
||||
CANCELLED(1L, "已取消"),
|
||||
ENDED(2L, "已结束"),
|
||||
IN_PROGRESS(3L, "进行中");
|
||||
|
||||
private final Long value;
|
||||
private final String desc;
|
||||
|
||||
CourseStatus(Long value, String desc) {
|
||||
this.value = value;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public Long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据值获取枚举
|
||||
*/
|
||||
public static CourseStatus fromValue(Long value) {
|
||||
for (CourseStatus status : values()) {
|
||||
if (status.value.equals(value)) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return NORMAL;
|
||||
}
|
||||
}
|
||||
+369
@@ -0,0 +1,369 @@
|
||||
package cn.novalon.gym.manage.groupcourse.handler;
|
||||
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourseBooking;
|
||||
import cn.novalon.gym.manage.groupcourse.repository.IGroupCourseBookingRepository;
|
||||
import cn.novalon.gym.manage.groupcourse.repository.IGroupCourseRepository;
|
||||
import cn.novalon.gym.manage.groupcourse.service.impl.GroupCourseRedisService;
|
||||
import cn.novalon.gym.manage.member.entity.MemberCard;
|
||||
import cn.novalon.gym.manage.member.entity.MemberCardRecord;
|
||||
import cn.novalon.gym.manage.member.enums.MemberCardType;
|
||||
import cn.novalon.gym.manage.member.repository.MemberCardRepository;
|
||||
import cn.novalon.gym.manage.member.service.IMemberCardRecordService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 预约 Saga 处理器
|
||||
*
|
||||
* 实现预约和取消预约的分布式事务处理
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-06-02
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class BookingSagaHandler {
|
||||
|
||||
private final IGroupCourseBookingRepository bookingRepository;
|
||||
private final IGroupCourseRepository courseRepository;
|
||||
private final IMemberCardRecordService memberCardRecordService;
|
||||
private final MemberCardRepository memberCardRepository;
|
||||
private final GroupCourseRedisService redisService;
|
||||
|
||||
private static final double DEFAULT_GROUP_COURSE_PRICE = 50.0;
|
||||
|
||||
/**
|
||||
* 执行预约事务
|
||||
*
|
||||
* 步骤:
|
||||
* 1. 保存预约记录
|
||||
* 2. 扣减会员卡权益
|
||||
* 3. 更新课程当前人数
|
||||
*/
|
||||
public Mono<GroupCourseBooking> executeBooking(GroupCourseBooking booking, Long recordId) {
|
||||
List<SagaStep> steps = new ArrayList<>();
|
||||
List<SagaStep> rollbackSteps = new ArrayList<>();
|
||||
|
||||
// 步骤1:保存预约记录
|
||||
SagaStep step1 = new SagaStep(
|
||||
"保存预约记录",
|
||||
saveBooking(booking),
|
||||
Mono.defer(() -> deleteBooking(booking.getId()))
|
||||
);
|
||||
steps.add(step1);
|
||||
rollbackSteps.add(0, step1);
|
||||
|
||||
// 步骤2:扣减会员卡权益(根据卡类型决定扣除次数还是金额)
|
||||
SagaStep step2 = new SagaStep(
|
||||
"扣减会员卡权益",
|
||||
deductCardUsageByCardType(booking.getMemberId(), recordId),
|
||||
Mono.defer(() -> restoreCardUsageByCardType(booking.getMemberId(), recordId))
|
||||
);
|
||||
steps.add(step2);
|
||||
rollbackSteps.add(0, step2);
|
||||
|
||||
// 步骤3:更新课程当前人数
|
||||
SagaStep step3 = new SagaStep(
|
||||
"更新课程当前人数",
|
||||
incrementCourseCurrentMembers(booking.getCourseId()),
|
||||
Mono.defer(() -> decrementCourseCurrentMembers(booking.getCourseId()))
|
||||
);
|
||||
steps.add(step3);
|
||||
rollbackSteps.add(0, step3);
|
||||
|
||||
// 步骤4:更新Redis预约计数
|
||||
SagaStep step4 = new SagaStep(
|
||||
"更新Redis预约计数",
|
||||
incrementRedisBookingCount(booking.getCourseId()),
|
||||
Mono.defer(() -> decrementRedisBookingCount(booking.getCourseId()))
|
||||
);
|
||||
steps.add(step4);
|
||||
rollbackSteps.add(0, step4);
|
||||
|
||||
return executeSaga(steps, rollbackSteps)
|
||||
.then(Mono.just(booking));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据会员卡类型扣减权益
|
||||
*/
|
||||
private Mono<Void> deductCardUsageByCardType(Long memberId, Long recordId) {
|
||||
return memberCardRecordService.findById(recordId)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("会员卡记录不存在")))
|
||||
.flatMap(record -> {
|
||||
// 验证会员卡归属
|
||||
if (!record.getMemberId().equals(memberId)) {
|
||||
return Mono.error(new RuntimeException("会员卡不归属当前用户"));
|
||||
}
|
||||
|
||||
return memberCardRepository.findById(record.getMemberCardId())
|
||||
.flatMap(card -> {
|
||||
MemberCardType cardType = MemberCardType.valueOf(card.getMemberCardType());
|
||||
|
||||
switch (cardType) {
|
||||
case COUNT_CARD:
|
||||
// 次卡扣除次数
|
||||
return deductCardUsage(recordId, 1, 0.0);
|
||||
case STORED_VALUE_CARD:
|
||||
// 储值卡扣除金额
|
||||
return deductCardUsage(recordId, 0, DEFAULT_GROUP_COURSE_PRICE);
|
||||
case TIME_CARD:
|
||||
// 时长卡不扣除,但需验证有效期
|
||||
return validateTimeCard(record);
|
||||
default:
|
||||
return Mono.error(new RuntimeException("不支持的会员卡类型: " + cardType));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证时长卡有效期
|
||||
*
|
||||
* @param record 会员卡记录
|
||||
* @return 验证通过返回Mono.empty(),否则返回Mono.error()
|
||||
*/
|
||||
private Mono<Void> validateTimeCard(MemberCardRecord record) {
|
||||
// 验证会员卡状态
|
||||
cn.novalon.gym.manage.member.enums.MemberCardRecordStatus status = record.getStatus();
|
||||
if (status != cn.novalon.gym.manage.member.enums.MemberCardRecordStatus.ACTIVE) {
|
||||
return Mono.error(new RuntimeException("会员卡状态无效,当前状态: " + (status != null ? status.getDesc() : "未知")));
|
||||
}
|
||||
|
||||
// 验证有效期
|
||||
java.time.LocalDateTime expireTime = record.getExpireTime();
|
||||
if (expireTime == null) {
|
||||
return Mono.error(new RuntimeException("会员卡有效期未设置"));
|
||||
}
|
||||
if (expireTime.isBefore(java.time.LocalDateTime.now())) {
|
||||
return Mono.error(new RuntimeException("会员卡已过期,有效期至: " + expireTime));
|
||||
}
|
||||
|
||||
log.debug("时长卡验证通过: recordId={}, expireTime={}", record.getId(), expireTime);
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据会员卡类型恢复权益
|
||||
*/
|
||||
private Mono<Void> restoreCardUsageByCardType(Long memberId, Long recordId) {
|
||||
return memberCardRecordService.findById(recordId)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("会员卡记录不存在,无法恢复权益")))
|
||||
.flatMap(record -> {
|
||||
// 验证会员卡归属(memberId为null时跳过验证)
|
||||
if (memberId != null && !record.getMemberId().equals(memberId)) {
|
||||
return Mono.error(new RuntimeException("会员卡不归属当前用户"));
|
||||
}
|
||||
|
||||
return memberCardRepository.findById(record.getMemberCardId())
|
||||
.flatMap(card -> {
|
||||
MemberCardType cardType = MemberCardType.valueOf(card.getMemberCardType());
|
||||
|
||||
switch (cardType) {
|
||||
case COUNT_CARD:
|
||||
return restoreCardUsage(recordId, 1, 0.0);
|
||||
case STORED_VALUE_CARD:
|
||||
return restoreCardUsage(recordId, 0, DEFAULT_GROUP_COURSE_PRICE);
|
||||
case TIME_CARD:
|
||||
return Mono.empty();
|
||||
default:
|
||||
return Mono.error(new RuntimeException("不支持的会员卡类型: " + cardType));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行取消预约事务
|
||||
*/
|
||||
public Mono<GroupCourseBooking> executeCancelBooking(Long bookingId, Long courseId, Long recordId, Long memberId) {
|
||||
List<SagaStep> steps = new ArrayList<>();
|
||||
List<SagaStep> rollbackSteps = new ArrayList<>();
|
||||
|
||||
// 步骤1:更新预约状态为已取消
|
||||
SagaStep step1 = new SagaStep(
|
||||
"更新预约状态为已取消",
|
||||
updateBookingStatus(bookingId, "1"),
|
||||
Mono.defer(() -> updateBookingStatus(bookingId, "0"))
|
||||
);
|
||||
steps.add(step1);
|
||||
rollbackSteps.add(0, step1);
|
||||
|
||||
// 步骤2:恢复会员卡权益
|
||||
SagaStep step2 = new SagaStep(
|
||||
"恢复会员卡权益",
|
||||
restoreCardUsageByCardType(memberId, recordId),
|
||||
Mono.defer(() -> deductCardUsageByCardType(memberId, recordId))
|
||||
);
|
||||
steps.add(step2);
|
||||
rollbackSteps.add(0, step2);
|
||||
|
||||
// 步骤3:减少课程当前人数
|
||||
SagaStep step3 = new SagaStep(
|
||||
"减少课程当前人数",
|
||||
decrementCourseCurrentMembers(courseId),
|
||||
Mono.defer(() -> incrementCourseCurrentMembers(courseId))
|
||||
);
|
||||
steps.add(step3);
|
||||
rollbackSteps.add(0, step3);
|
||||
|
||||
// 步骤4:更新Redis预约计数
|
||||
SagaStep step4 = new SagaStep(
|
||||
"更新Redis预约计数",
|
||||
decrementRedisBookingCount(courseId),
|
||||
Mono.defer(() -> incrementRedisBookingCount(courseId))
|
||||
);
|
||||
steps.add(step4);
|
||||
rollbackSteps.add(0, step4);
|
||||
|
||||
return executeSaga(steps, rollbackSteps)
|
||||
.then(bookingRepository.findById(bookingId));
|
||||
}
|
||||
|
||||
private Mono<Void> updateBookingStatus(Long bookingId, String status) {
|
||||
return bookingRepository.updateStatus(bookingId, status)
|
||||
.flatMap(rows -> {
|
||||
if (rows == 0) {
|
||||
return Mono.error(new RuntimeException("更新预约状态失败"));
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Void> saveBooking(GroupCourseBooking booking) {
|
||||
return bookingRepository.save(booking)
|
||||
.flatMap(saved -> {
|
||||
if (saved.getId() == null) {
|
||||
return Mono.error(new RuntimeException("保存预约记录失败"));
|
||||
}
|
||||
booking.setId(saved.getId());
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Void> deleteBooking(Long bookingId) {
|
||||
return bookingRepository.deleteById(bookingId);
|
||||
}
|
||||
|
||||
private Mono<Void> deductCardUsage(Long recordId, Integer deductTimes, Double deductAmount) {
|
||||
if (deductTimes == 0 && deductAmount == 0.0) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
return memberCardRecordService.findById(recordId)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("会员卡记录不存在")))
|
||||
.flatMap(record -> {
|
||||
cn.novalon.gym.manage.member.enums.MemberCardRecordStatus status = record.getStatus();
|
||||
if (status != cn.novalon.gym.manage.member.enums.MemberCardRecordStatus.ACTIVE) {
|
||||
return Mono.error(new RuntimeException("会员卡状态无效,当前状态: " + (status != null ? status.getDesc() : "未知")));
|
||||
}
|
||||
java.time.LocalDateTime expireTime = record.getExpireTime();
|
||||
if (expireTime != null && expireTime.isBefore(java.time.LocalDateTime.now())) {
|
||||
return Mono.error(new RuntimeException("会员卡已过期"));
|
||||
}
|
||||
if (record.getRemainingTimes() != null && deductTimes > 0 && record.getRemainingTimes() < deductTimes) {
|
||||
return Mono.error(new RuntimeException("会员卡剩余次数不足,当前剩余: " + record.getRemainingTimes() + "次"));
|
||||
}
|
||||
if (record.getRemainingAmount() != null && deductAmount > 0 && record.getRemainingAmount() < deductAmount) {
|
||||
return Mono.error(new RuntimeException("会员卡余额不足,当前剩余: " + record.getRemainingAmount()));
|
||||
}
|
||||
return memberCardRecordService.deductUsage(recordId, deductTimes, deductAmount)
|
||||
.flatMap(rows -> {
|
||||
if (rows == 0) {
|
||||
return Mono.error(new RuntimeException("扣减会员卡权益失败,请重试"));
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Void> restoreCardUsage(Long recordId, Integer addTimes, Double addAmount) {
|
||||
if (addTimes == 0 && addAmount == 0.0) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
return memberCardRecordService.findById(recordId)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("会员卡记录不存在,无法恢复权益")))
|
||||
.flatMap(record -> {
|
||||
// 使用当前记录的过期时间,避免清空过期时间
|
||||
return memberCardRecordService.renewCard(recordId, addTimes, addAmount, record.getExpireTime())
|
||||
.flatMap(rows -> {
|
||||
if (rows == 0) {
|
||||
return Mono.error(new RuntimeException("恢复会员卡权益失败,请重试"));
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Void> incrementCourseCurrentMembers(Long courseId) {
|
||||
return courseRepository.updateCurrentMembers(courseId, 1).then();
|
||||
}
|
||||
|
||||
private Mono<Void> decrementCourseCurrentMembers(Long courseId) {
|
||||
return courseRepository.updateCurrentMembers(courseId, -1).then();
|
||||
}
|
||||
|
||||
private Mono<Void> incrementRedisBookingCount(Long courseId) {
|
||||
return redisService.incrementBookingCount(courseId).then();
|
||||
}
|
||||
|
||||
private Mono<Void> decrementRedisBookingCount(Long courseId) {
|
||||
return redisService.decrementBookingCount(courseId).then();
|
||||
}
|
||||
|
||||
private Mono<Void> executeSaga(List<SagaStep> steps, List<SagaStep> rollbackSteps) {
|
||||
List<SagaStep> completedSteps = new ArrayList<>();
|
||||
return executeStep(steps, 0, completedSteps);
|
||||
}
|
||||
|
||||
private Mono<Void> executeStep(List<SagaStep> steps, int index, List<SagaStep> completedSteps) {
|
||||
if (index >= steps.size()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
SagaStep currentStep = steps.get(index);
|
||||
|
||||
return currentStep.operation()
|
||||
.doOnSuccess(unused -> completedSteps.add(currentStep))
|
||||
.then(Mono.defer(() -> executeStep(steps, index + 1, completedSteps)))
|
||||
.onErrorResume(error -> {
|
||||
log.error("Saga步骤执行失败: step={}, error={}", currentStep.description(), error.getMessage());
|
||||
return rollbackCompletedSteps(completedSteps).then(Mono.error(error));
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Void> rollbackCompletedSteps(List<SagaStep> completedSteps) {
|
||||
if (completedSteps.isEmpty()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
// 反转已完成的步骤,按逆序回滚
|
||||
List<SagaStep> reversed = new ArrayList<>(completedSteps);
|
||||
Collections.reverse(reversed);
|
||||
|
||||
return rollbackSteps(reversed, 0);
|
||||
}
|
||||
|
||||
private Mono<Void> rollbackSteps(List<SagaStep> rollbackSteps, int index) {
|
||||
if (index >= rollbackSteps.size()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
SagaStep currentStep = rollbackSteps.get(index);
|
||||
|
||||
return currentStep.rollbackOperation()
|
||||
.then(Mono.defer(() -> rollbackSteps(rollbackSteps, index + 1)))
|
||||
.doOnError(error -> log.error("Saga回滚失败: step={}, error={}", currentStep.description(), error.getMessage()))
|
||||
.onErrorResume(e -> Mono.empty());
|
||||
}
|
||||
|
||||
private record SagaStep(String description, Mono<Void> operation, Mono<Void> rollbackOperation) {}
|
||||
}
|
||||
+23
-2
@@ -35,11 +35,22 @@ public class GroupCourseBookingHandler {
|
||||
public Mono<ServerResponse> bookCourse(ServerRequest request) {
|
||||
return request.bodyToMono(Map.class)
|
||||
.flatMap(body -> {
|
||||
// 验证必填参数
|
||||
if (body.get("courseId") == null) {
|
||||
return buildErrorResponse("请提供课程ID");
|
||||
}
|
||||
if (body.get("memberId") == null) {
|
||||
return buildErrorResponse("请提供会员ID");
|
||||
}
|
||||
if (body.get("memberCardRecordId") == null) {
|
||||
return buildErrorResponse("请提供会员卡记录ID");
|
||||
}
|
||||
|
||||
Long courseId = ((Number) body.get("courseId")).longValue();
|
||||
Long memberId = ((Number) body.get("memberId")).longValue();
|
||||
Long memberCardId = ((Number) body.get("memberCardId")).longValue();
|
||||
Long memberCardRecordId = ((Number) body.get("memberCardRecordId")).longValue();
|
||||
|
||||
return bookingService.bookCourse(courseId, memberId, memberCardId)
|
||||
return bookingService.bookCourse(courseId, memberId, memberCardRecordId)
|
||||
.flatMap(booking -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", true);
|
||||
@@ -117,4 +128,14 @@ public class GroupCourseBookingHandler {
|
||||
return ServerResponse.ok()
|
||||
.body(bookingService.getBookingsByCourseId(courseId), GroupCourseBooking.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建错误响应
|
||||
*/
|
||||
private Mono<ServerResponse> buildErrorResponse(String message) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", false);
|
||||
response.put("message", message);
|
||||
return ServerResponse.badRequest().bodyValue(response);
|
||||
}
|
||||
}
|
||||
+133
-2
@@ -71,8 +71,139 @@ public class GroupCourseHandler {
|
||||
@Operation(summary = "根据ID获取团课", description = "根据ID获取团课详情")
|
||||
public Mono<ServerResponse> getGroupCourseById(ServerRequest request){
|
||||
Long id = Long.valueOf(request.pathVariable("id"));
|
||||
return ServerResponse.ok()
|
||||
.body(groupCourseService.findById(id), GroupCourse.class);
|
||||
return groupCourseService.findById(id)
|
||||
.flatMap(course -> ServerResponse.ok().bodyValue(course))
|
||||
.switchIfEmpty(ServerResponse.notFound().build());
|
||||
}
|
||||
|
||||
@Operation(summary = "创建团课", description = "创建新的团课")
|
||||
public Mono<ServerResponse> createGroupCourse(ServerRequest request) {
|
||||
return request.bodyToMono(GroupCourse.class)
|
||||
.flatMap(groupCourse -> {
|
||||
if (groupCourse.getCourseName() == null || groupCourse.getCourseName().isEmpty()) {
|
||||
Map<String, Object> error = new HashMap<>();
|
||||
error.put("success", false);
|
||||
error.put("message", "课程名称不能为空");
|
||||
return ServerResponse.badRequest().bodyValue(error);
|
||||
}
|
||||
|
||||
return groupCourseService.create(groupCourse)
|
||||
.flatMap(course -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", true);
|
||||
response.put("message", "团课创建成功");
|
||||
response.put("data", course);
|
||||
return ServerResponse.ok().bodyValue(response);
|
||||
})
|
||||
.onErrorResume(error -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", false);
|
||||
response.put("message", error.getMessage());
|
||||
return ServerResponse.badRequest().bodyValue(response);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Operation(summary = "更新团课", description = "更新指定团课信息")
|
||||
public Mono<ServerResponse> updateGroupCourse(ServerRequest request) {
|
||||
Long id = Long.valueOf(request.pathVariable("id"));
|
||||
|
||||
return request.bodyToMono(GroupCourse.class)
|
||||
.flatMap(groupCourse -> {
|
||||
return groupCourseService.update(id, groupCourse)
|
||||
.flatMap(course -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", true);
|
||||
response.put("message", "团课更新成功");
|
||||
response.put("data", course);
|
||||
return ServerResponse.ok().bodyValue(response);
|
||||
})
|
||||
.onErrorResume(error -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", false);
|
||||
response.put("message", error.getMessage());
|
||||
return ServerResponse.badRequest().bodyValue(response);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Operation(summary = "取消团课", description = "取消指定团课(需提前24小时)")
|
||||
public Mono<ServerResponse> cancelGroupCourse(ServerRequest request) {
|
||||
Long id = Long.valueOf(request.pathVariable("id"));
|
||||
|
||||
return groupCourseService.cancel(id)
|
||||
.flatMap(course -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", true);
|
||||
response.put("message", "团课取消成功");
|
||||
response.put("data", course);
|
||||
return ServerResponse.ok().bodyValue(response);
|
||||
})
|
||||
.onErrorResume(error -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", false);
|
||||
response.put("message", error.getMessage());
|
||||
return ServerResponse.badRequest().bodyValue(response);
|
||||
});
|
||||
}
|
||||
|
||||
@Operation(summary = "团课签到", description = "会员签到参加团课")
|
||||
public Mono<ServerResponse> signIn(ServerRequest request) {
|
||||
Long courseId = Long.valueOf(request.pathVariable("courseId"));
|
||||
|
||||
return request.bodyToMono(Map.class)
|
||||
.flatMap(body -> {
|
||||
if (body == null || body.isEmpty()) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", false);
|
||||
response.put("message", "请求体不能为空");
|
||||
return ServerResponse.badRequest().bodyValue(response);
|
||||
}
|
||||
|
||||
Object memberIdObj = body.get("memberId");
|
||||
if (memberIdObj == null) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", false);
|
||||
response.put("message", "memberId不能为空");
|
||||
return ServerResponse.badRequest().bodyValue(response);
|
||||
}
|
||||
|
||||
Long memberId = ((Number) memberIdObj).longValue();
|
||||
|
||||
return groupCourseService.signIn(courseId, memberId)
|
||||
.flatMap(course -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", true);
|
||||
response.put("message", "签到成功");
|
||||
response.put("data", course);
|
||||
return ServerResponse.ok().bodyValue(response);
|
||||
})
|
||||
.onErrorResume(error -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", false);
|
||||
response.put("message", error.getMessage());
|
||||
return ServerResponse.badRequest().bodyValue(response);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Operation(summary = "删除团课", description = "删除指定团课(软删除)")
|
||||
public Mono<ServerResponse> deleteGroupCourse(ServerRequest request) {
|
||||
Long id = Long.valueOf(request.pathVariable("id"));
|
||||
|
||||
return groupCourseService.delete(id)
|
||||
.then(Mono.defer(() -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", true);
|
||||
response.put("message", "团课删除成功");
|
||||
return ServerResponse.ok().bodyValue(response);
|
||||
}))
|
||||
.onErrorResume(error -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", false);
|
||||
response.put("message", error.getMessage());
|
||||
return ServerResponse.badRequest().bodyValue(response);
|
||||
});
|
||||
}
|
||||
|
||||
@Operation(summary = "测试-根据Key获取Redis缓存", description = "测试接口:根据传入的key值获取Redis中缓存的数据")
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package cn.novalon.gym.manage.groupcourse.handler;
|
||||
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourse;
|
||||
import cn.novalon.gym.manage.groupcourse.enums.CourseEvent;
|
||||
import cn.novalon.gym.manage.groupcourse.enums.CourseStatus;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 团课状态机处理器
|
||||
*
|
||||
* 管理团课的状态转换逻辑
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-06-02
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class GroupCourseStateMachine {
|
||||
|
||||
private final Map<CourseStatus, Map<CourseEvent, CourseStatus>> stateTransitionMap;
|
||||
|
||||
public GroupCourseStateMachine() {
|
||||
this.stateTransitionMap = buildStateTransitionMap();
|
||||
}
|
||||
|
||||
private Map<CourseStatus, Map<CourseEvent, CourseStatus>> buildStateTransitionMap() {
|
||||
Map<CourseStatus, Map<CourseEvent, CourseStatus>> map = new HashMap<>();
|
||||
|
||||
// NORMAL 状态可以转换的事件
|
||||
Map<CourseEvent, CourseStatus> normalTransitions = new HashMap<>();
|
||||
normalTransitions.put(CourseEvent.CANCEL, CourseStatus.CANCELLED);
|
||||
normalTransitions.put(CourseEvent.END, CourseStatus.ENDED);
|
||||
normalTransitions.put(CourseEvent.START, CourseStatus.IN_PROGRESS);
|
||||
map.put(CourseStatus.NORMAL, normalTransitions);
|
||||
|
||||
// IN_PROGRESS 状态可以转换的事件
|
||||
Map<CourseEvent, CourseStatus> inProgressTransitions = new HashMap<>();
|
||||
inProgressTransitions.put(CourseEvent.END, CourseStatus.ENDED);
|
||||
map.put(CourseStatus.IN_PROGRESS, inProgressTransitions);
|
||||
|
||||
// CANCELLED 状态是终态,不允许任何转换
|
||||
|
||||
// ENDED 状态是终态,不允许任何转换
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否可以转换状态
|
||||
*/
|
||||
public Mono<Boolean> canTransition(CourseStatus currentState, CourseEvent event) {
|
||||
return Mono.fromSupplier(() -> {
|
||||
Map<CourseEvent, CourseStatus> transitions = stateTransitionMap.get(currentState);
|
||||
if (transitions == null) {
|
||||
return false;
|
||||
}
|
||||
return transitions.containsKey(event);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行状态转换
|
||||
*/
|
||||
public Mono<CourseStatus> transition(CourseStatus currentState, CourseEvent event) {
|
||||
return Mono.fromSupplier(() -> {
|
||||
Map<CourseEvent, CourseStatus> transitions = stateTransitionMap.get(currentState);
|
||||
if (transitions == null || !transitions.containsKey(event)) {
|
||||
log.error("Invalid state transition: currentState={}, event={}", currentState, event);
|
||||
throw new IllegalStateException(
|
||||
String.format("不允许的状态转换: 当前状态=%s, 事件=%s", currentState, event));
|
||||
}
|
||||
CourseStatus newState = transitions.get(event);
|
||||
log.info("State transition: {} --({})--> {}", currentState, event, newState);
|
||||
return newState;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证状态转换是否允许
|
||||
*/
|
||||
public Mono<Void> validateTransition(GroupCourse course, CourseEvent event) {
|
||||
Long statusValue = course.getStatus();
|
||||
CourseStatus currentState = CourseStatus.fromValue(statusValue != null ? statusValue : 0L);
|
||||
|
||||
return canTransition(currentState, event)
|
||||
.flatMap(canTransition -> {
|
||||
if (!canTransition) {
|
||||
String errorMessage = buildErrorMessage(course.getId(), currentState, event);
|
||||
return Mono.error(new IllegalStateException(errorMessage));
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建状态转换错误信息
|
||||
*/
|
||||
private String buildErrorMessage(Long courseId, CourseStatus currentState, CourseEvent event) {
|
||||
String currentStateDesc = currentState.getDesc();
|
||||
String eventDesc = event.getDesc();
|
||||
|
||||
String advice = "";
|
||||
switch (currentState) {
|
||||
case CANCELLED:
|
||||
advice = "(课程已取消,无法进行此操作)";
|
||||
break;
|
||||
case ENDED:
|
||||
advice = "(课程已结束,无法进行此操作)";
|
||||
break;
|
||||
case IN_PROGRESS:
|
||||
if (event == CourseEvent.CANCEL) {
|
||||
advice = "(课程正在进行中,无法取消)";
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return String.format("团课ID=%d 不允许的状态转换: 当前状态=%s(%s), 操作=%s%s",
|
||||
courseId, currentState, currentStateDesc, eventDesc, advice);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行状态转换并更新课程对象
|
||||
*/
|
||||
public Mono<GroupCourse> applyTransition(GroupCourse course, CourseEvent event) {
|
||||
return validateTransition(course, event)
|
||||
.then(transition(CourseStatus.fromValue(course.getStatus()), event))
|
||||
.map(newStatus -> {
|
||||
course.setStatus(newStatus.getValue());
|
||||
return course;
|
||||
});
|
||||
}
|
||||
}
|
||||
+35
-2
@@ -53,7 +53,40 @@ public interface IGroupCourseBookingRepository {
|
||||
Mono<GroupCourseBooking> update(GroupCourseBooking booking);
|
||||
|
||||
/**
|
||||
* 根据会员卡ID查询预约记录
|
||||
* 更新预约状态
|
||||
*/
|
||||
Flux<GroupCourseBooking> findByMemberCardId(Long memberCardId);
|
||||
Mono<Integer> updateStatus(Long bookingId, String status);
|
||||
|
||||
/**
|
||||
* 删除预约记录
|
||||
*/
|
||||
Mono<Void> deleteById(Long bookingId);
|
||||
|
||||
/**
|
||||
* 根据会员卡记录ID查询预约记录
|
||||
*/
|
||||
Flux<GroupCourseBooking> findByMemberCardRecordId(Long memberCardRecordId);
|
||||
|
||||
/**
|
||||
* 查询会员是否有时间冲突的预约
|
||||
*
|
||||
* @param memberId 会员ID
|
||||
* @param newStartTime 新课程开始时间
|
||||
* @param newEndTime 新课程结束时间
|
||||
* @return 冲突的预约记录列表
|
||||
*/
|
||||
Flux<GroupCourseBooking> findConflictingBookings(Long memberId, java.time.LocalDateTime newStartTime, java.time.LocalDateTime newEndTime);
|
||||
|
||||
/**
|
||||
* 查询已开始课程但未到场会员的预约记录
|
||||
* @return 缺席会员的预约记录列表
|
||||
*/
|
||||
Flux<GroupCourseBooking> findAbsentMembers();
|
||||
|
||||
/**
|
||||
* 更新预约状态为缺席(3)
|
||||
* @param bookingId 预约ID
|
||||
* @return 更新记录数
|
||||
*/
|
||||
Mono<Integer> updateToAbsent(Long bookingId);
|
||||
}
|
||||
+10
@@ -17,4 +17,14 @@ public interface IGroupCourseRepository {
|
||||
|
||||
Mono<PageResponse<GroupCourse>> findByPage(PageRequest pageRequest);
|
||||
Mono<PageResponse<GroupCourse>> findByPageAndNotDeleted(PageRequest pageRequest);
|
||||
|
||||
Mono<GroupCourse> save(GroupCourse groupCourse);
|
||||
|
||||
Mono<GroupCourse> update(GroupCourse groupCourse);
|
||||
|
||||
Mono<GroupCourse> cancel(Long id);
|
||||
|
||||
Mono<Void> deleteById(Long id);
|
||||
|
||||
Mono<GroupCourse> updateCurrentMembers(Long id, Integer delta);
|
||||
}
|
||||
|
||||
+32
-2
@@ -78,8 +78,38 @@ public class GroupCourseBookingRepository implements IGroupCourseBookingReposito
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<GroupCourseBooking> findByMemberCardId(Long memberCardId) {
|
||||
return groupCourseBookingDao.findByMemberCardIdAndDeletedAtIsNull(memberCardId)
|
||||
public Mono<Integer> updateStatus(Long bookingId, String status) {
|
||||
java.time.LocalDateTime now = java.time.LocalDateTime.now();
|
||||
java.time.LocalDateTime cancelTime = "1".equals(status) ? now : null;
|
||||
return groupCourseBookingDao.updateStatus(bookingId, status, cancelTime, now);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> deleteById(Long bookingId) {
|
||||
return groupCourseBookingDao.softDelete(bookingId, java.time.LocalDateTime.now()).then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<GroupCourseBooking> findByMemberCardRecordId(Long memberCardRecordId) {
|
||||
return groupCourseBookingDao.findByMemberCardRecordIdAndDeletedAtIsNull(memberCardRecordId)
|
||||
.map(groupCourseConverter::toBookingDomain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<GroupCourseBooking> findConflictingBookings(Long memberId, java.time.LocalDateTime newStartTime, java.time.LocalDateTime newEndTime) {
|
||||
return groupCourseBookingDao.findConflictingBookings(memberId, newStartTime, newEndTime)
|
||||
.map(groupCourseConverter::toBookingDomain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<GroupCourseBooking> findAbsentMembers() {
|
||||
return groupCourseBookingDao.findAbsentMembers()
|
||||
.map(groupCourseConverter::toBookingDomain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Integer> updateToAbsent(Long bookingId) {
|
||||
java.time.LocalDateTime now = java.time.LocalDateTime.now();
|
||||
return groupCourseBookingDao.updateToAbsent(bookingId, now);
|
||||
}
|
||||
}
|
||||
+50
@@ -15,6 +15,7 @@ import org.springframework.stereotype.Repository;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
@@ -128,4 +129,53 @@ public class GroupCourseRepository implements IGroupCourseRepository {
|
||||
return new PageResponse<>(courseList, totalPages, total, page, size);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> save(GroupCourse groupCourse) {
|
||||
GroupCourseEntity entity = groupCourseConverter.toEntity(groupCourse);
|
||||
entity.setCreatedAt(LocalDateTime.now());
|
||||
entity.setUpdatedAt(LocalDateTime.now());
|
||||
entity.setStatus(0L);
|
||||
entity.setCurrentMembers(0);
|
||||
|
||||
return groupCourseDao.save(entity)
|
||||
.map(groupCourseConverter::toDomain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> update(GroupCourse groupCourse) {
|
||||
GroupCourseEntity entity = groupCourseConverter.toEntity(groupCourse);
|
||||
entity.setUpdatedAt(LocalDateTime.now());
|
||||
|
||||
return r2dbcEntityTemplate.update(entity)
|
||||
.then(findByIdAndDeletedAtIsNull(groupCourse.getId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> cancel(Long id) {
|
||||
return groupCourseDao.cancelCourse(id, LocalDateTime.now())
|
||||
.flatMap(updated -> {
|
||||
if (updated > 0) {
|
||||
return findByIdAndDeletedAtIsNull(id);
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> deleteById(Long id) {
|
||||
return groupCourseDao.softDelete(id, LocalDateTime.now())
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> updateCurrentMembers(Long id, Integer delta) {
|
||||
return groupCourseDao.updateCurrentMembers(id, delta, LocalDateTime.now())
|
||||
.flatMap(updated -> {
|
||||
if (updated > 0) {
|
||||
return findByIdAndDeletedAtIsNull(id);
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package cn.novalon.gym.manage.groupcourse.scheduler;
|
||||
|
||||
import cn.novalon.gym.manage.groupcourse.service.IGroupCourseBookingService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 团课缺席会员定时任务
|
||||
*
|
||||
* 功能:定期检查已开始但会员未到场的课程,自动更新预约记录状态为缺席
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-06-02
|
||||
*/
|
||||
@Component
|
||||
public class GroupCourseAbsentScheduler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GroupCourseAbsentScheduler.class);
|
||||
|
||||
private final IGroupCourseBookingService bookingService;
|
||||
|
||||
public GroupCourseAbsentScheduler(IGroupCourseBookingService bookingService) {
|
||||
this.bookingService = bookingService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 每分钟检查一次已开始课程但未到场会员的预约记录
|
||||
* 使用fixedRate保证任务间隔稳定执行
|
||||
*/
|
||||
@Scheduled(fixedRate = 60000)
|
||||
public void checkAndUpdateAbsentMembers() {
|
||||
logger.debug("定时任务开始检查已开始课程但未到场会员的预约记录");
|
||||
|
||||
bookingService.processAbsentMembers()
|
||||
.subscribe(
|
||||
count -> logger.debug("定时任务完成,更新了 {} 条缺席记录", count),
|
||||
error -> logger.error("定时任务执行失败:{}", error.getMessage(), error)
|
||||
);
|
||||
}
|
||||
}
|
||||
+11
-3
@@ -14,13 +14,13 @@ public interface IGroupCourseBookingService {
|
||||
|
||||
/**
|
||||
* 预约团课
|
||||
*
|
||||
*
|
||||
* @param courseId 团课ID
|
||||
* @param memberId 会员ID
|
||||
* @param memberCardId 会员卡ID
|
||||
* @param memberCardRecordId 会员卡记录ID
|
||||
* @return 预约记录
|
||||
*/
|
||||
Mono<GroupCourseBooking> bookCourse(Long courseId, Long memberId, Long memberCardId);
|
||||
Mono<GroupCourseBooking> bookCourse(Long courseId, Long memberId, Long memberCardRecordId);
|
||||
|
||||
/**
|
||||
* 取消预约
|
||||
@@ -54,4 +54,12 @@ public interface IGroupCourseBookingService {
|
||||
* @return 预约记录列表
|
||||
*/
|
||||
Flux<GroupCourseBooking> getBookingsByCourseId(Long courseId);
|
||||
|
||||
/**
|
||||
* 处理已开始课程但未到场会员的预约记录
|
||||
* 将状态更新为缺席(3)
|
||||
*
|
||||
* @return 处理的记录数
|
||||
*/
|
||||
Mono<Integer> processAbsentMembers();
|
||||
}
|
||||
+10
@@ -13,4 +13,14 @@ public interface IGroupCourseService {
|
||||
Flux<GroupCourse> findAll(boolean includeDeleted);
|
||||
|
||||
Mono<PageResponse<GroupCourse>> findByPage(PageRequest pageRequest, boolean includeDeleted);
|
||||
|
||||
Mono<GroupCourse> create(GroupCourse groupCourse);
|
||||
|
||||
Mono<GroupCourse> update(Long id, GroupCourse groupCourse);
|
||||
|
||||
Mono<GroupCourse> cancel(Long id);
|
||||
|
||||
Mono<GroupCourse> signIn(Long courseId, Long memberId);
|
||||
|
||||
Mono<Void> delete(Long id);
|
||||
}
|
||||
|
||||
+164
-76
@@ -3,6 +3,7 @@ package cn.novalon.gym.manage.groupcourse.service.impl;
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourse;
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourseBooking;
|
||||
import cn.novalon.gym.manage.groupcourse.event.BookingReminderEventPublisher;
|
||||
import cn.novalon.gym.manage.groupcourse.handler.BookingSagaHandler;
|
||||
import cn.novalon.gym.manage.groupcourse.repository.IGroupCourseBookingRepository;
|
||||
import cn.novalon.gym.manage.groupcourse.repository.IGroupCourseRepository;
|
||||
import cn.novalon.gym.manage.groupcourse.service.IGroupCourseBookingService;
|
||||
@@ -43,6 +44,7 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
private final IGroupCourseRepository courseRepository;
|
||||
private final GroupCourseRedisService redisService;
|
||||
private final BookingReminderEventPublisher bookingReminderEventPublisher;
|
||||
private final BookingSagaHandler bookingSagaHandler;
|
||||
|
||||
// 预约提前时间限制(分钟)
|
||||
private static final long BOOKING_MIN_ADVANCE_MINUTES = 30;
|
||||
@@ -52,16 +54,18 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
public GroupCourseBookingService(IGroupCourseBookingRepository bookingRepository,
|
||||
IGroupCourseRepository courseRepository,
|
||||
GroupCourseRedisService redisService,
|
||||
BookingReminderEventPublisher bookingReminderEventPublisher) {
|
||||
BookingReminderEventPublisher bookingReminderEventPublisher,
|
||||
BookingSagaHandler bookingSagaHandler) {
|
||||
this.bookingRepository = bookingRepository;
|
||||
this.courseRepository = courseRepository;
|
||||
this.redisService = redisService;
|
||||
this.bookingReminderEventPublisher = bookingReminderEventPublisher;
|
||||
this.bookingSagaHandler = bookingSagaHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourseBooking> bookCourse(Long courseId, Long memberId, Long memberCardId) {
|
||||
logger.info("开始预约团课:courseId={}, memberId={}, memberCardId={}", courseId, memberId, memberCardId);
|
||||
public Mono<GroupCourseBooking> bookCourse(Long courseId, Long memberId, Long memberCardRecordId) {
|
||||
logger.info("开始预约团课:courseId={}, memberId={}, memberCardRecordId={}", courseId, memberId, memberCardRecordId);
|
||||
|
||||
// 生成唯一请求ID用于分布式锁
|
||||
String requestId = UUID.randomUUID().toString();
|
||||
@@ -77,8 +81,19 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
return getCourseWithCache(courseId)
|
||||
.flatMap(course -> {
|
||||
// 3. 验证课程状态
|
||||
if (!"0".equals(String.valueOf(course.getStatus()))) {
|
||||
return releaseLockAndError(courseId, requestId, "课程状态不可预约");
|
||||
Long status = course.getStatus();
|
||||
if (status == null || status != 0L) {
|
||||
String errorMessage;
|
||||
if (status == null) {
|
||||
errorMessage = "课程状态异常";
|
||||
} else if (status == 1L) {
|
||||
errorMessage = "课程已取消,无法预约";
|
||||
} else if (status == 2L) {
|
||||
errorMessage = "课程已结束,无法预约";
|
||||
} else {
|
||||
errorMessage = "课程状态不可预约";
|
||||
}
|
||||
return releaseLockAndError(courseId, requestId, errorMessage);
|
||||
}
|
||||
|
||||
// 4. 验证预约时间
|
||||
@@ -90,59 +105,84 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
"需在课程开始前" + BOOKING_MIN_ADVANCE_MINUTES + "分钟预约");
|
||||
}
|
||||
|
||||
// 5. 验证是否已预约
|
||||
return bookingRepository.findValidBooking(courseId, memberId)
|
||||
.flatMap(existingBooking -> {
|
||||
return releaseLockAndError(courseId, requestId, "您已预约该课程");
|
||||
})
|
||||
.switchIfEmpty(
|
||||
// 6. 使用Redis原子操作验证课程人数是否已满
|
||||
validateAndIncrementBookingCount(courseId, course.getMaxMembers())
|
||||
.flatMap(countValid -> {
|
||||
if (countValid > course.getMaxMembers()) {
|
||||
return releaseLockAndError(courseId, requestId, "课程已满");
|
||||
}
|
||||
// 5. 验证课程人数是否已满(使用数据库中的current_members字段)
|
||||
if (course.getCurrentMembers() != null &&
|
||||
course.getMaxMembers() != null &&
|
||||
course.getCurrentMembers() >= course.getMaxMembers()) {
|
||||
return releaseLockAndError(courseId, requestId, "课程已满");
|
||||
}
|
||||
|
||||
// 7. 创建预约记录
|
||||
GroupCourseBooking booking = new GroupCourseBooking();
|
||||
booking.setCourseId(courseId);
|
||||
booking.setMemberId(memberId);
|
||||
booking.setMemberCardId(memberCardId);
|
||||
booking.setBookingTime(LocalDateTime.now());
|
||||
booking.setStatus("0"); // 0-已预约
|
||||
// 6. 验证时间冲突(会员是否已预约时间重叠的课程)
|
||||
return bookingRepository.findConflictingBookings(memberId, course.getStartTime(), course.getEndTime())
|
||||
.collectList()
|
||||
.flatMap(conflictingBookings -> {
|
||||
if (!conflictingBookings.isEmpty()) {
|
||||
StringBuilder errorMsg = new StringBuilder("您已预约的课程与当前课程时间冲突:");
|
||||
for (GroupCourseBooking conflict : conflictingBookings) {
|
||||
errorMsg.append(conflict.getCourseName())
|
||||
.append("(")
|
||||
.append(conflict.getCourseStartTime())
|
||||
.append(" - ")
|
||||
.append(conflict.getCourseEndTime())
|
||||
.append(") ");
|
||||
}
|
||||
return releaseLockAndError(courseId, requestId, errorMsg.toString());
|
||||
}
|
||||
|
||||
// 添加课程信息到预约记录
|
||||
booking.setCourseName(course.getCourseName());
|
||||
booking.setCourseStartTime(course.getStartTime());
|
||||
booking.setCourseEndTime(course.getEndTime());
|
||||
booking.setLocation(course.getLocation());
|
||||
// 7. 验证是否已预约
|
||||
return bookingRepository.findValidBooking(courseId, memberId)
|
||||
.flatMap(existingBooking -> {
|
||||
return releaseLockAndError(courseId, requestId, "您已预约该课程");
|
||||
})
|
||||
.switchIfEmpty(
|
||||
// 8. 使用Redis原子操作验证课程人数是否已满
|
||||
validateAndIncrementBookingCount(courseId, course.getMaxMembers())
|
||||
.flatMap(countValid -> {
|
||||
if (countValid > course.getMaxMembers()) {
|
||||
return releaseLockAndError(courseId, requestId, "课程已满");
|
||||
}
|
||||
|
||||
// 8. 保存预约记录
|
||||
return bookingRepository.save(booking)
|
||||
.flatMap(savedBooking -> {
|
||||
// 9. 释放锁
|
||||
return redisService.releaseLock(courseId, requestId)
|
||||
.then(Mono.just(savedBooking));
|
||||
})
|
||||
.doOnSuccess(savedBooking -> {
|
||||
logger.info("预约成功:bookingId={}, courseId={}, memberId={}",
|
||||
savedBooking.getId(), courseId, memberId);
|
||||
// 发布预约成功事件
|
||||
bookingReminderEventPublisher.publishBookingSuccessEvent(
|
||||
savedBooking.getId(),
|
||||
savedBooking.getMemberId(),
|
||||
savedBooking.getCourseName(),
|
||||
savedBooking.getCourseStartTime().toString()
|
||||
);
|
||||
})
|
||||
.doOnError(error -> {
|
||||
// 回滚Redis计数
|
||||
redisService.decrementBookingCount(courseId).subscribe();
|
||||
logger.error("预约失败:courseId={}, memberId={}, error={}",
|
||||
courseId, memberId, error.getMessage());
|
||||
});
|
||||
})
|
||||
);
|
||||
// 9. 创建预约记录
|
||||
GroupCourseBooking booking = new GroupCourseBooking();
|
||||
booking.setCourseId(courseId);
|
||||
booking.setMemberId(memberId);
|
||||
booking.setMemberCardRecordId(memberCardRecordId);
|
||||
booking.setBookingTime(LocalDateTime.now());
|
||||
booking.setStatus("0"); // 0-已预约
|
||||
|
||||
// 添加课程信息到预约记录
|
||||
booking.setCourseName(course.getCourseName());
|
||||
booking.setCourseStartTime(course.getStartTime());
|
||||
booking.setCourseEndTime(course.getEndTime());
|
||||
booking.setLocation(course.getLocation());
|
||||
|
||||
// 10. 使用Saga事务执行预约(包含权益扣减)
|
||||
return bookingSagaHandler.executeBooking(booking, memberCardRecordId)
|
||||
.flatMap(savedBooking -> {
|
||||
// 11. 释放锁
|
||||
return redisService.releaseLock(courseId, requestId)
|
||||
.then(Mono.just(savedBooking));
|
||||
})
|
||||
.doOnSuccess(savedBooking -> {
|
||||
logger.info("预约成功:bookingId={}, courseId={}, memberId={}",
|
||||
savedBooking.getId(), courseId, memberId);
|
||||
// 发布预约成功事件
|
||||
bookingReminderEventPublisher.publishBookingSuccessEvent(
|
||||
savedBooking.getId(),
|
||||
savedBooking.getMemberId(),
|
||||
savedBooking.getCourseName(),
|
||||
savedBooking.getCourseStartTime().toString()
|
||||
);
|
||||
})
|
||||
.doOnError(error -> {
|
||||
// 回滚Redis计数
|
||||
redisService.decrementBookingCount(courseId).subscribe();
|
||||
logger.error("预约失败:courseId={}, memberId={}, error={}",
|
||||
courseId, memberId, error.getMessage());
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
})
|
||||
.onErrorResume(error -> {
|
||||
// 发生错误时释放锁
|
||||
@@ -172,12 +212,23 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
* 验证并增加预约人数(使用Redis原子操作)
|
||||
*/
|
||||
private Mono<Integer> validateAndIncrementBookingCount(Long courseId, Integer maxMembers) {
|
||||
return redisService.incrementBookingCount(courseId)
|
||||
.map(count -> {
|
||||
// 同时查询数据库中的预约数进行双重验证
|
||||
// 这里简化处理,实际生产环境应该结合数据库查询
|
||||
return count.intValue();
|
||||
});
|
||||
// 先获取当前Redis中的预约计数
|
||||
return redisService.getBookingCount(courseId)
|
||||
.flatMap(currentCount -> {
|
||||
// 如果Redis中计数为0,可能是首次访问,需要从数据库同步
|
||||
if (currentCount == 0) {
|
||||
// 从数据库查询实际预约人数
|
||||
return bookingRepository.countValidBookings(courseId)
|
||||
.flatMap(dbCount -> {
|
||||
// 将数据库中的实际预约人数同步到Redis
|
||||
return redisService.setBookingCount(courseId, dbCount.intValue())
|
||||
.then(Mono.just(dbCount.intValue()));
|
||||
});
|
||||
}
|
||||
return Mono.just(currentCount);
|
||||
})
|
||||
// 递增预约计数
|
||||
.flatMap(count -> redisService.incrementBookingCount(courseId).map(Long::intValue));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,32 +263,37 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
}
|
||||
|
||||
// 3. 验证预约状态
|
||||
if (!"0".equals(booking.getStatus())) {
|
||||
return releaseLockAndError(bookingId, requestId, "预约状态不允许取消");
|
||||
String status = booking.getStatus();
|
||||
if (!"0".equals(status)) {
|
||||
String errorMessage;
|
||||
if ("1".equals(status)) {
|
||||
errorMessage = "预约已取消,无需重复取消";
|
||||
} else if ("2".equals(status)) {
|
||||
errorMessage = "课程已出席,无法取消";
|
||||
} else if ("3".equals(status)) {
|
||||
errorMessage = "已缺席课程,无法取消";
|
||||
} else {
|
||||
errorMessage = "预约状态不允许取消";
|
||||
}
|
||||
return releaseLockAndError(bookingId, requestId, errorMessage);
|
||||
}
|
||||
|
||||
// 4. 验证取消时间
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime startTime = booking.getCourseStartTime();
|
||||
if (startTime == null) {
|
||||
return releaseLockAndError(bookingId, requestId, "课程开始时间为空,无法取消预约");
|
||||
}
|
||||
long hoursUntilStart = ChronoUnit.HOURS.between(now, startTime);
|
||||
if (hoursUntilStart < CANCEL_MIN_ADVANCE_HOURS) {
|
||||
return releaseLockAndError(bookingId, requestId,
|
||||
"需在课程开始前" + CANCEL_MIN_ADVANCE_HOURS + "小时取消");
|
||||
}
|
||||
|
||||
// 5. 更新预约状态
|
||||
booking.setStatus("1"); // 1-已取消
|
||||
booking.setCancelTime(LocalDateTime.now());
|
||||
|
||||
// 6. 保存更新
|
||||
return bookingRepository.update(booking)
|
||||
// 5. 使用Saga事务执行取消预约(包含权益恢复)
|
||||
return bookingSagaHandler.executeCancelBooking(bookingId, booking.getCourseId(), booking.getMemberCardRecordId(), memberId)
|
||||
.flatMap(updatedBooking -> {
|
||||
// 7. 减少Redis中的预约人数计数
|
||||
return redisService.decrementBookingCount(booking.getCourseId())
|
||||
.then(Mono.just(updatedBooking));
|
||||
})
|
||||
.flatMap(updatedBooking -> {
|
||||
// 8. 释放锁
|
||||
// 6. 释放锁
|
||||
return redisService.releaseLock(bookingId, requestId)
|
||||
.then(Mono.just(updatedBooking));
|
||||
})
|
||||
@@ -281,4 +337,36 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
return bookingRepository.findByCourseId(courseId)
|
||||
.doOnComplete(() -> logger.debug("查询完成:courseId={}", courseId));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Integer> processAbsentMembers() {
|
||||
logger.info("开始处理已开始课程但未到场会员的预约记录");
|
||||
|
||||
return bookingRepository.findAbsentMembers()
|
||||
.collectList()
|
||||
.flatMap(absentBookings -> {
|
||||
if (absentBookings.isEmpty()) {
|
||||
logger.info("没有需要处理的缺席会员记录");
|
||||
return Mono.just(0);
|
||||
}
|
||||
|
||||
logger.info("发现 {} 条需要更新为缺席状态的预约记录", absentBookings.size());
|
||||
|
||||
// 批量更新状态为缺席
|
||||
return Flux.fromIterable(absentBookings)
|
||||
.flatMap(booking -> {
|
||||
logger.debug("更新预约记录为缺席状态:bookingId={}, memberId={}, courseId={}",
|
||||
booking.getId(), booking.getMemberId(), booking.getCourseId());
|
||||
return bookingRepository.updateToAbsent(booking.getId());
|
||||
})
|
||||
.reduce(Integer::sum)
|
||||
.defaultIfEmpty(0)
|
||||
.doOnSuccess(count -> {
|
||||
logger.info("成功更新 {} 条预约记录为缺席状态", count);
|
||||
})
|
||||
.doOnError(error -> {
|
||||
logger.error("处理缺席会员记录时发生错误:{}", error.getMessage(), error);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+12
-1
@@ -180,4 +180,15 @@ public class GroupCourseRedisService {
|
||||
.decrement(key)
|
||||
.doOnSuccess(count -> logger.debug("预约人数减少:courseId={}, count={}", courseId, count));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置课程预约人数(用于从数据库同步)
|
||||
*/
|
||||
public Mono<Void> setBookingCount(Long courseId, Integer count) {
|
||||
String key = "booking_count:" + courseId;
|
||||
return reactiveRedisTemplate.opsForValue()
|
||||
.set(key, count)
|
||||
.doOnSuccess(result -> logger.debug("预约人数已设置:courseId={}, count={}", courseId, count))
|
||||
.then();
|
||||
}
|
||||
}
|
||||
|
||||
+261
-1
@@ -5,8 +5,18 @@ import cn.novalon.gym.manage.common.dto.PageRequest;
|
||||
import cn.novalon.gym.manage.common.dto.PageResponse;
|
||||
import cn.novalon.gym.manage.common.util.RedisUtil;
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourse;
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourseBooking;
|
||||
import cn.novalon.gym.manage.groupcourse.enums.CourseEvent;
|
||||
import cn.novalon.gym.manage.groupcourse.enums.CourseStatus;
|
||||
import cn.novalon.gym.manage.groupcourse.handler.GroupCourseStateMachine;
|
||||
import cn.novalon.gym.manage.groupcourse.repository.IGroupCourseBookingRepository;
|
||||
import cn.novalon.gym.manage.groupcourse.repository.IGroupCourseRepository;
|
||||
import cn.novalon.gym.manage.groupcourse.service.IGroupCourseService;
|
||||
import cn.novalon.gym.manage.member.entity.MemberCard;
|
||||
import cn.novalon.gym.manage.member.entity.MemberCardRecord;
|
||||
import cn.novalon.gym.manage.member.enums.MemberCardType;
|
||||
import cn.novalon.gym.manage.member.repository.MemberCardRepository;
|
||||
import cn.novalon.gym.manage.member.service.IMemberCardRecordService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
@@ -15,24 +25,40 @@ import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Service
|
||||
public class GroupCourseService implements IGroupCourseService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GroupCourseService.class);
|
||||
|
||||
private final IGroupCourseRepository groupCourseRepository;
|
||||
private final IGroupCourseBookingRepository bookingRepository;
|
||||
private final IMemberCardRecordService memberCardRecordService;
|
||||
private final MemberCardRepository memberCardRepository;
|
||||
private final RedisUtil redisUtil;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final GroupCourseStateMachine stateMachine;
|
||||
|
||||
private static final String CACHE_KEY_PREFIX = "group_course:page:";
|
||||
private static final String CACHE_KEY_ID_PREFIX = "group_course:id:";
|
||||
private static final long CACHE_EXPIRE_SECONDS = 300;
|
||||
|
||||
private static final double DEFAULT_GROUP_COURSE_PRICE = 50.0;
|
||||
|
||||
public GroupCourseService(IGroupCourseRepository groupCourseRepository,
|
||||
IGroupCourseBookingRepository bookingRepository,
|
||||
IMemberCardRecordService memberCardRecordService,
|
||||
MemberCardRepository memberCardRepository,
|
||||
RedisUtil redisUtil,
|
||||
ObjectMapper objectMapper){
|
||||
ObjectMapper objectMapper,
|
||||
GroupCourseStateMachine stateMachine){
|
||||
this.groupCourseRepository = groupCourseRepository;
|
||||
this.bookingRepository = bookingRepository;
|
||||
this.memberCardRecordService = memberCardRecordService;
|
||||
this.memberCardRepository = memberCardRepository;
|
||||
this.redisUtil = redisUtil;
|
||||
this.objectMapper = objectMapper;
|
||||
this.stateMachine = stateMachine;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -133,4 +159,238 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> create(GroupCourse groupCourse) {
|
||||
return groupCourseRepository.save(groupCourse)
|
||||
.doOnSuccess(course -> logger.info("团课创建成功 - id={}, name={}", course.getId(), course.getCourseName()))
|
||||
.flatMap(course -> clearCache().thenReturn(course))
|
||||
.doOnError(error -> logger.error("团课创建失败 - error: {}", error.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> update(Long id, GroupCourse groupCourse) {
|
||||
return groupCourseRepository.findByIdAndDeletedAtIsNull(id)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("团课不存在")))
|
||||
.flatMap(existing -> {
|
||||
if (groupCourse.getCourseName() != null) {
|
||||
existing.setCourseName(groupCourse.getCourseName());
|
||||
}
|
||||
if (groupCourse.getCoachId() != null) {
|
||||
existing.setCoachId(groupCourse.getCoachId());
|
||||
}
|
||||
if (groupCourse.getCourseType() != null) {
|
||||
existing.setCourseType(groupCourse.getCourseType());
|
||||
}
|
||||
if (groupCourse.getStartTime() != null) {
|
||||
existing.setStartTime(groupCourse.getStartTime());
|
||||
}
|
||||
if (groupCourse.getEndTime() != null) {
|
||||
existing.setEndTime(groupCourse.getEndTime());
|
||||
}
|
||||
if (groupCourse.getMaxMembers() != null) {
|
||||
existing.setMaxMembers(groupCourse.getMaxMembers());
|
||||
}
|
||||
if (groupCourse.getStatus() != null) {
|
||||
existing.setStatus(groupCourse.getStatus());
|
||||
}
|
||||
if (groupCourse.getLocation() != null) {
|
||||
existing.setLocation(groupCourse.getLocation());
|
||||
}
|
||||
if (groupCourse.getCoverImage() != null) {
|
||||
existing.setCoverImage(groupCourse.getCoverImage());
|
||||
}
|
||||
if (groupCourse.getDescription() != null) {
|
||||
existing.setDescription(groupCourse.getDescription());
|
||||
}
|
||||
if (groupCourse.getPointCardAmount() != null) {
|
||||
existing.setPointCardAmount(groupCourse.getPointCardAmount());
|
||||
}
|
||||
if (groupCourse.getStoredValueAmount() != null) {
|
||||
existing.setStoredValueAmount(groupCourse.getStoredValueAmount());
|
||||
}
|
||||
return groupCourseRepository.update(existing);
|
||||
})
|
||||
.doOnSuccess(course -> logger.info("团课更新成功 - id={}", id))
|
||||
.flatMap(course -> clearCache().thenReturn(course))
|
||||
.doOnError(error -> logger.error("团课更新失败 - id={}, error: {}", id, error.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> cancel(Long id) {
|
||||
return groupCourseRepository.findByIdAndDeletedAtIsNull(id)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("团课不存在")))
|
||||
.flatMap(course -> {
|
||||
// 使用状态机验证状态转换
|
||||
return stateMachine.validateTransition(course, CourseEvent.CANCEL)
|
||||
.then(Mono.just(course));
|
||||
})
|
||||
.flatMap(course -> {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
// 检查当前时间是否超过课程开始前24小时
|
||||
// 如果当前时间 > (课程开始时间 - 24小时),则不允许取消
|
||||
if (now.isAfter(course.getStartTime().minusHours(24))) {
|
||||
return Mono.error(new RuntimeException("课程取消需提前24小时"));
|
||||
}
|
||||
|
||||
// 取消课程并返还权益
|
||||
return groupCourseRepository.cancel(id)
|
||||
.flatMap(canceledCourse -> {
|
||||
// 为所有预约该课程的会员返还权益
|
||||
return refundBookingMembers(canceledCourse.getId())
|
||||
.then(Mono.just(canceledCourse));
|
||||
});
|
||||
})
|
||||
.doOnSuccess(course -> logger.info("团课取消成功 - id={}", id))
|
||||
.flatMap(course -> clearCache().thenReturn(course))
|
||||
.doOnError(error -> logger.error("团课取消失败 - id={}, error: {}", id, error.getMessage()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 为预约课程的所有会员返还权益
|
||||
*
|
||||
* @param courseId 课程ID
|
||||
* @return 空Mono
|
||||
*/
|
||||
private Mono<Void> refundBookingMembers(Long courseId) {
|
||||
logger.info("开始为课程预约会员返还权益: courseId={}", courseId);
|
||||
|
||||
return groupCourseRepository.findByIdAndDeletedAtIsNull(courseId)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("课程不存在")))
|
||||
.flatMap(course -> {
|
||||
Integer pointCardAmount = course.getPointCardAmount() != null ? course.getPointCardAmount() : 1;
|
||||
Double storedValueAmount = course.getStoredValueAmount() != null ?
|
||||
course.getStoredValueAmount().doubleValue() : DEFAULT_GROUP_COURSE_PRICE;
|
||||
|
||||
logger.info("课程权益信息: courseId={}, pointCardAmount={}, storedValueAmount={}",
|
||||
courseId, pointCardAmount, storedValueAmount);
|
||||
|
||||
return bookingRepository.findByCourseId(courseId)
|
||||
.filter(booking -> "0".equals(booking.getStatus())) // 只处理已预约状态的记录
|
||||
.flatMap(booking -> {
|
||||
Long memberCardRecordId = booking.getMemberCardRecordId();
|
||||
if (memberCardRecordId == null) {
|
||||
logger.warn("预约记录没有会员卡记录ID: bookingId={}", booking.getId());
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
// 根据会员卡类型返还权益
|
||||
return refundCardUsage(booking.getId(), memberCardRecordId, pointCardAmount, storedValueAmount)
|
||||
.doOnSuccess(unused -> logger.info("会员权益返还成功: bookingId={}, memberId={}",
|
||||
booking.getId(), booking.getMemberId()))
|
||||
.doOnError(error -> logger.error("会员权益返还失败: bookingId={}, memberId={}, error={}",
|
||||
booking.getId(), booking.getMemberId(), error.getMessage()))
|
||||
.onErrorResume(e -> Mono.empty()); // 单个会员返还失败不影响其他会员
|
||||
})
|
||||
.then();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据会员卡类型返还权益
|
||||
*
|
||||
* @param bookingId 预约ID
|
||||
* @param recordId 会员卡记录ID
|
||||
* @param pointCardAmount 次卡返还次数
|
||||
* @param storedValueAmount 储值卡返还金额
|
||||
* @return 空Mono
|
||||
*/
|
||||
private Mono<Void> refundCardUsage(Long bookingId, Long recordId, Integer pointCardAmount, Double storedValueAmount) {
|
||||
return memberCardRecordService.findById(recordId)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("会员卡记录不存在")))
|
||||
.flatMap(record -> {
|
||||
return memberCardRepository.findById(record.getMemberCardId())
|
||||
.flatMap(card -> {
|
||||
MemberCardType cardType = MemberCardType.valueOf(card.getMemberCardType());
|
||||
|
||||
Mono<Void> refundOperation;
|
||||
switch (cardType) {
|
||||
case COUNT_CARD:
|
||||
// 次卡返还次数(基于课程设置的pointCardAmount)
|
||||
logger.debug("返还次卡次数: recordId={}, amount={}", recordId, pointCardAmount);
|
||||
refundOperation = memberCardRecordService.renewCard(recordId, pointCardAmount, 0.0, record.getExpireTime())
|
||||
.then(Mono.empty());
|
||||
break;
|
||||
case STORED_VALUE_CARD:
|
||||
// 储值卡返还金额(基于课程设置的storedValueAmount)
|
||||
logger.debug("返还储值卡金额: recordId={}, amount={}", recordId, storedValueAmount);
|
||||
refundOperation = memberCardRecordService.renewCard(recordId, 0, storedValueAmount, record.getExpireTime())
|
||||
.then(Mono.empty());
|
||||
break;
|
||||
case TIME_CARD:
|
||||
// 时长卡不返还,仅修改预约记录状态
|
||||
logger.debug("时长卡不返还权益,仅更新预约状态: bookingId={}", bookingId);
|
||||
refundOperation = Mono.empty();
|
||||
break;
|
||||
default:
|
||||
return Mono.error(new RuntimeException("不支持的会员卡类型: " + cardType));
|
||||
}
|
||||
|
||||
// 无论哪种卡类型,都需要更新预约记录状态为已取消
|
||||
return refundOperation.then(bookingRepository.updateStatus(bookingId, "1"))
|
||||
.flatMap(rows -> {
|
||||
if (rows == 0) {
|
||||
logger.warn("更新预约状态失败: bookingId={}", bookingId);
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> signIn(Long courseId, Long memberId) {
|
||||
return groupCourseRepository.findByIdAndDeletedAtIsNull(courseId)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("团课不存在")))
|
||||
.flatMap(course -> {
|
||||
if (course.getStatus() != 0L) {
|
||||
return Mono.error(new RuntimeException("课程状态不允许签到"));
|
||||
}
|
||||
|
||||
if (course.getCurrentMembers() >= course.getMaxMembers()) {
|
||||
return Mono.error(new RuntimeException("课程已满员"));
|
||||
}
|
||||
|
||||
// 检查会员是否已预约此课程
|
||||
return bookingRepository.findValidBooking(courseId, memberId)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("会员未预约此课程")))
|
||||
.flatMap(booking -> {
|
||||
// 更新课程当前人数
|
||||
return groupCourseRepository.updateCurrentMembers(courseId, 1)
|
||||
.flatMap(updatedCourse -> {
|
||||
// 更新预约状态为已出席
|
||||
return bookingRepository.updateStatus(booking.getId(), "2")
|
||||
.thenReturn(updatedCourse);
|
||||
});
|
||||
});
|
||||
})
|
||||
.doOnSuccess(course -> logger.info("团课签到成功 - courseId={}, memberId={}", courseId, memberId))
|
||||
.flatMap(course -> clearCache().thenReturn(course))
|
||||
.doOnError(error -> logger.error("团课签到失败 - courseId={}, memberId={}, error: {}", courseId, memberId, error.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> delete(Long id) {
|
||||
// 先查询课程状态,只有已取消的课程才能删除
|
||||
return groupCourseRepository.findByIdAndDeletedAtIsNull(id)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("团课不存在")))
|
||||
.flatMap(course -> {
|
||||
// 检查课程状态是否为已取消(状态码1)
|
||||
if (course.getStatus() == null || !course.getStatus().equals(CourseStatus.CANCELLED.getValue())) {
|
||||
return Mono.error(new RuntimeException("只有已取消的课程才能删除,当前状态: " +
|
||||
(course.getStatus() != null ? course.getStatus() : "未知")));
|
||||
}
|
||||
|
||||
// 删除课程
|
||||
return groupCourseRepository.deleteById(id)
|
||||
.doOnSuccess(v -> logger.info("团课删除成功 - id={}", id))
|
||||
.then(clearCache())
|
||||
.doOnError(error -> logger.error("团课删除失败 - id={}, error: {}", id, error.getMessage()));
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Void> clearCache() {
|
||||
return redisUtil.deleteByPattern(CACHE_KEY_PREFIX + "*")
|
||||
.then(redisUtil.deleteByPattern(CACHE_KEY_ID_PREFIX + "*")).then();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user