新增教练管理,优化预约人数计数方式
This commit is contained in:
+83
-70
@@ -33,10 +33,6 @@ public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long>
|
||||
@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);
|
||||
@@ -47,46 +43,64 @@ public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long>
|
||||
|
||||
Flux<GroupCourseEntity> findByCourseTypeAndDeletedAtIsNull(Long courseType);
|
||||
|
||||
// ==================== 教练相关查询 ====================
|
||||
|
||||
Flux<GroupCourseEntity> findByCoachIdAndDeletedAtIsNull(Long coachId);
|
||||
|
||||
Flux<GroupCourseEntity> findByCoachIdAndDeletedAtIsNull(Long coachId, Sort sort);
|
||||
|
||||
@Query("SELECT COUNT(*) FROM group_course WHERE coach_id = :coachId AND status = :status AND deleted_at IS NULL")
|
||||
Mono<Long> countByCoachIdAndStatus(Long coachId, String status);
|
||||
|
||||
@Modifying
|
||||
@Query("UPDATE group_course SET status = '1', updated_at = :updatedAt WHERE coach_id = :coachId AND status != :excludeStatus AND deleted_at IS NULL")
|
||||
Mono<Integer> cancelCoursesByCoachIdExceptStatus(Long coachId, String excludeStatus, LocalDateTime updatedAt);
|
||||
|
||||
/**
|
||||
* 多条件查询团课(使用 DatabaseClient 构建动态 SQL)
|
||||
*/
|
||||
default Flux<GroupCourseEntity> searchGroupCourses(DatabaseClient databaseClient, GroupCourseQueryDto query) {
|
||||
StringBuilder sql = new StringBuilder("SELECT * FROM group_course WHERE deleted_at IS NULL");
|
||||
StringBuilder sql = new StringBuilder(
|
||||
"SELECT gc.*, COALESCE(b.cnt, 0) AS current_members " +
|
||||
"FROM group_course gc " +
|
||||
"LEFT JOIN (SELECT course_id, COUNT(*) AS cnt FROM group_course_booking WHERE status = '0' GROUP BY course_id) b " +
|
||||
"ON gc.id = b.course_id " +
|
||||
"WHERE gc.deleted_at IS NULL");
|
||||
List<String> conditions = new ArrayList<>();
|
||||
|
||||
// 默认不查询可预约团课(status = '0' 且未过期)
|
||||
conditions.add("status = '0'");
|
||||
conditions.add("end_time > NOW()");
|
||||
conditions.add("gc.status = '0'");
|
||||
conditions.add("gc.end_time > NOW()");
|
||||
|
||||
// 1. 团课名称模糊查询
|
||||
if (query.getCourseName() != null && !query.getCourseName().isEmpty()) {
|
||||
conditions.add("course_name ILIKE :courseName");
|
||||
conditions.add("gc.course_name ILIKE :courseName");
|
||||
}
|
||||
|
||||
// 2. 基于团课类型查询
|
||||
if (query.getCourseType() != null) {
|
||||
conditions.add("course_type = :courseType");
|
||||
conditions.add("gc.course_type = :courseType");
|
||||
}
|
||||
|
||||
// 3. 基于日期时间段查询
|
||||
if (query.getStartDate() != null) {
|
||||
conditions.add("start_time >= :startDate");
|
||||
conditions.add("gc.start_time >= :startDate");
|
||||
}
|
||||
if (query.getEndDate() != null) {
|
||||
conditions.add("start_time <= :endDate");
|
||||
conditions.add("gc.start_time <= :endDate");
|
||||
}
|
||||
|
||||
// 4. 基于早晨/下午/夜晚时间段查询
|
||||
if (query.getTimePeriod() != null && !query.getTimePeriod().isEmpty()) {
|
||||
switch (query.getTimePeriod().toLowerCase()) {
|
||||
case "morning":
|
||||
conditions.add("EXTRACT(HOUR FROM start_time) >= 6 AND EXTRACT(HOUR FROM start_time) < 12");
|
||||
conditions.add("EXTRACT(HOUR FROM gc.start_time) >= 6 AND EXTRACT(HOUR FROM gc.start_time) < 12");
|
||||
break;
|
||||
case "afternoon":
|
||||
conditions.add("EXTRACT(HOUR FROM start_time) >= 12 AND EXTRACT(HOUR FROM start_time) < 18");
|
||||
conditions.add("EXTRACT(HOUR FROM gc.start_time) >= 12 AND EXTRACT(HOUR FROM gc.start_time) < 18");
|
||||
break;
|
||||
case "evening":
|
||||
conditions.add("EXTRACT(HOUR FROM start_time) >= 18 AND EXTRACT(HOUR FROM start_time) < 24");
|
||||
conditions.add("EXTRACT(HOUR FROM gc.start_time) >= 18 AND EXTRACT(HOUR FROM gc.start_time) < 24");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -104,20 +118,20 @@ public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long>
|
||||
List<String> orderClauses = new ArrayList<>();
|
||||
|
||||
if (hasRemainingMost) {
|
||||
orderClauses.add(" (max_members - current_members) DESC");
|
||||
orderClauses.add(" (gc.max_members - COALESCE(b.cnt, 0)) DESC");
|
||||
}
|
||||
|
||||
if (hasPriceSort) {
|
||||
if ("asc".equalsIgnoreCase(query.getPriceSort())) {
|
||||
orderClauses.add(" stored_value_amount ASC");
|
||||
orderClauses.add(" gc.stored_value_amount ASC");
|
||||
} else if ("desc".equalsIgnoreCase(query.getPriceSort())) {
|
||||
orderClauses.add(" stored_value_amount DESC");
|
||||
orderClauses.add(" gc.stored_value_amount DESC");
|
||||
}
|
||||
}
|
||||
|
||||
sql.append(String.join(",", orderClauses));
|
||||
} else {
|
||||
sql.append(" ORDER BY start_time ASC");
|
||||
sql.append(" ORDER BY gc.start_time ASC");
|
||||
}
|
||||
|
||||
// 分页
|
||||
@@ -145,30 +159,7 @@ public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long>
|
||||
spec = spec.bind("limit", size);
|
||||
spec = spec.bind("offset", offset);
|
||||
|
||||
return spec.map((row, meta) -> {
|
||||
GroupCourseEntity entity = new GroupCourseEntity();
|
||||
entity.setId(row.get("id", Long.class));
|
||||
entity.setCourseName(row.get("course_name", String.class));
|
||||
entity.setCoachId(row.get("coach_id", Long.class));
|
||||
entity.setCourseType(row.get("course_type", Long.class));
|
||||
entity.setStartTime(row.get("start_time", LocalDateTime.class));
|
||||
entity.setEndTime(row.get("end_time", LocalDateTime.class));
|
||||
entity.setMaxMembers(row.get("max_members", Integer.class));
|
||||
entity.setCurrentMembers(row.get("current_members", Integer.class));
|
||||
String statusStr = row.get("status", String.class);
|
||||
entity.setStatus(statusStr != null ? Long.parseLong(statusStr) : null);
|
||||
entity.setLocation(row.get("location", String.class));
|
||||
entity.setCoverImage(row.get("cover_image", String.class));
|
||||
entity.setDescription(row.get("description", String.class));
|
||||
entity.setStoredValueAmount(row.get("stored_value_amount", java.math.BigDecimal.class));
|
||||
entity.setQrCodePath(row.get("qr_code_path", String.class));
|
||||
entity.setCreateBy(row.get("create_by", String.class));
|
||||
entity.setUpdateBy(row.get("update_by", String.class));
|
||||
entity.setCreatedAt(row.get("created_at", LocalDateTime.class));
|
||||
entity.setUpdatedAt(row.get("updated_at", LocalDateTime.class));
|
||||
entity.setDeletedAt(row.get("deleted_at", LocalDateTime.class));
|
||||
return entity;
|
||||
}).all();
|
||||
return spec.map((row, meta) -> mapRowToEntity(row)).all();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,14 +224,19 @@ public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long>
|
||||
* 分页查询团课(支持 keyword 和 status 过滤)
|
||||
*/
|
||||
default Flux<GroupCourseEntity> findByPageFiltered(DatabaseClient databaseClient, PageRequest pageRequest) {
|
||||
StringBuilder sql = new StringBuilder("SELECT * FROM group_course WHERE deleted_at IS NULL");
|
||||
StringBuilder sql = new StringBuilder(
|
||||
"SELECT gc.*, COALESCE(b.cnt, 0) AS current_members " +
|
||||
"FROM group_course gc " +
|
||||
"LEFT JOIN (SELECT course_id, COUNT(*) AS cnt FROM group_course_booking WHERE status = '0' GROUP BY course_id) b " +
|
||||
"ON gc.id = b.course_id " +
|
||||
"WHERE gc.deleted_at IS NULL");
|
||||
List<String> conditions = new ArrayList<>();
|
||||
|
||||
if (pageRequest.getKeyword() != null && !pageRequest.getKeyword().isEmpty()) {
|
||||
conditions.add("course_name ILIKE :keyword");
|
||||
conditions.add("gc.course_name ILIKE :keyword");
|
||||
}
|
||||
if (pageRequest.getStatus() != null && !pageRequest.getStatus().isEmpty()) {
|
||||
conditions.add("status = :status");
|
||||
conditions.add("gc.status = :status");
|
||||
}
|
||||
|
||||
if (!conditions.isEmpty()) {
|
||||
@@ -249,7 +245,7 @@ public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long>
|
||||
|
||||
String sort = pageRequest.getSort() != null ? pageRequest.getSort() : "id";
|
||||
String order = "desc".equalsIgnoreCase(pageRequest.getOrder()) ? "DESC" : "ASC";
|
||||
sql.append(" ORDER BY ").append(sort).append(" ").append(order);
|
||||
sql.append(" ORDER BY gc.").append(sort).append(" ").append(order);
|
||||
|
||||
int size = pageRequest.getSize();
|
||||
if (size < 1) size = 10;
|
||||
@@ -268,30 +264,7 @@ public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long>
|
||||
spec = spec.bind("limit", size);
|
||||
spec = spec.bind("offset", offset);
|
||||
|
||||
return spec.map((row, meta) -> {
|
||||
GroupCourseEntity entity = new GroupCourseEntity();
|
||||
entity.setId(row.get("id", Long.class));
|
||||
entity.setCourseName(row.get("course_name", String.class));
|
||||
entity.setCoachId(row.get("coach_id", Long.class));
|
||||
entity.setCourseType(row.get("course_type", Long.class));
|
||||
entity.setStartTime(row.get("start_time", LocalDateTime.class));
|
||||
entity.setEndTime(row.get("end_time", LocalDateTime.class));
|
||||
entity.setMaxMembers(row.get("max_members", Integer.class));
|
||||
entity.setCurrentMembers(row.get("current_members", Integer.class));
|
||||
String statusStr = row.get("status", String.class);
|
||||
entity.setStatus(statusStr != null ? Long.parseLong(statusStr) : null);
|
||||
entity.setLocation(row.get("location", String.class));
|
||||
entity.setCoverImage(row.get("cover_image", String.class));
|
||||
entity.setDescription(row.get("description", String.class));
|
||||
entity.setStoredValueAmount(row.get("stored_value_amount", java.math.BigDecimal.class));
|
||||
entity.setQrCodePath(row.get("qr_code_path", String.class));
|
||||
entity.setCreateBy(row.get("create_by", String.class));
|
||||
entity.setUpdateBy(row.get("update_by", String.class));
|
||||
entity.setCreatedAt(row.get("created_at", LocalDateTime.class));
|
||||
entity.setUpdatedAt(row.get("updated_at", LocalDateTime.class));
|
||||
entity.setDeletedAt(row.get("deleted_at", LocalDateTime.class));
|
||||
return entity;
|
||||
}).all();
|
||||
return spec.map((row, meta) -> mapRowToEntity(row)).all();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -323,4 +296,44 @@ public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long>
|
||||
|
||||
return spec.map((row, meta) -> row.get(0, Long.class)).one();
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计单门课程的有效预约人数(status='0')
|
||||
*/
|
||||
default Mono<Long> countValidBookings(DatabaseClient databaseClient, Long courseId) {
|
||||
return databaseClient.sql("SELECT COUNT(*) FROM group_course_booking WHERE course_id = :id AND status = '0'")
|
||||
.bind("id", courseId)
|
||||
.map((row, meta) -> row.get(0, Long.class))
|
||||
.one()
|
||||
.defaultIfEmpty(0L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据库行映射为 GroupCourseEntity
|
||||
*/
|
||||
private GroupCourseEntity mapRowToEntity(io.r2dbc.spi.Row row) {
|
||||
GroupCourseEntity entity = new GroupCourseEntity();
|
||||
entity.setId(row.get("id", Long.class));
|
||||
entity.setCourseName(row.get("course_name", String.class));
|
||||
entity.setCoachId(row.get("coach_id", Long.class));
|
||||
entity.setCourseType(row.get("course_type", Long.class));
|
||||
entity.setStartTime(row.get("start_time", LocalDateTime.class));
|
||||
entity.setEndTime(row.get("end_time", LocalDateTime.class));
|
||||
entity.setMaxMembers(row.get("max_members", Integer.class));
|
||||
Integer cm = row.get("current_members", Integer.class);
|
||||
entity.setCurrentMembers(cm != null ? cm : 0);
|
||||
String statusStr = row.get("status", String.class);
|
||||
entity.setStatus(statusStr != null ? Long.parseLong(statusStr) : null);
|
||||
entity.setLocation(row.get("location", String.class));
|
||||
entity.setCoverImage(row.get("cover_image", String.class));
|
||||
entity.setDescription(row.get("description", String.class));
|
||||
entity.setStoredValueAmount(row.get("stored_value_amount", java.math.BigDecimal.class));
|
||||
entity.setQrCodePath(row.get("qr_code_path", String.class));
|
||||
entity.setCreateBy(row.get("create_by", String.class));
|
||||
entity.setUpdateBy(row.get("update_by", String.class));
|
||||
entity.setCreatedAt(row.get("created_at", LocalDateTime.class));
|
||||
entity.setUpdatedAt(row.get("updated_at", LocalDateTime.class));
|
||||
entity.setDeletedAt(row.get("deleted_at", LocalDateTime.class));
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
+11
@@ -21,6 +21,9 @@ public class GroupCourseDetail extends BaseDomain {
|
||||
@Schema(description = "教练ID", example = "1")
|
||||
private Long coachId;
|
||||
|
||||
@Schema(description = "教练名称", example = "张教练")
|
||||
private String coachName;
|
||||
|
||||
@Schema(description = "课程类型ID", example = "1")
|
||||
private Long courseType;
|
||||
|
||||
@@ -99,6 +102,14 @@ public class GroupCourseDetail extends BaseDomain {
|
||||
this.coachId = coachId;
|
||||
}
|
||||
|
||||
public String getCoachName() {
|
||||
return coachName;
|
||||
}
|
||||
|
||||
public void setCoachName(String coachName) {
|
||||
this.coachName = coachName;
|
||||
}
|
||||
|
||||
public Long getCourseType() {
|
||||
return courseType;
|
||||
}
|
||||
|
||||
-56
@@ -2,8 +2,6 @@ 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;
|
||||
@@ -34,11 +32,9 @@ import java.util.List;
|
||||
public class BookingSagaHandler {
|
||||
|
||||
private final IGroupCourseBookingRepository bookingRepository;
|
||||
private final IGroupCourseRepository courseRepository;
|
||||
private final IMemberCardRecordService memberCardRecordService;
|
||||
private final IMemberStoredCardService memberStoredCardService;
|
||||
private final MemberCardRepository memberCardRepository;
|
||||
private final GroupCourseRedisService redisService;
|
||||
|
||||
private static final double DEFAULT_GROUP_COURSE_PRICE = 50.0;
|
||||
|
||||
@@ -72,24 +68,6 @@ public class BookingSagaHandler {
|
||||
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));
|
||||
}
|
||||
@@ -225,24 +203,6 @@ public class BookingSagaHandler {
|
||||
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));
|
||||
}
|
||||
@@ -301,22 +261,6 @@ public class BookingSagaHandler {
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
+57
-4
@@ -7,21 +7,27 @@ import cn.novalon.gym.manage.groupcourse.domain.GroupCourse;
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourseDetail;
|
||||
import cn.novalon.gym.manage.groupcourse.dto.GroupCourseQueryDto;
|
||||
import cn.novalon.gym.manage.groupcourse.service.IGroupCourseService;
|
||||
import cn.novalon.gym.manage.groupcourse.vo.GroupCourseVO;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Validator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Component
|
||||
@Tag(name="团课管理",description = "团课相关操作")
|
||||
public class GroupCourseHandler {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GroupCourseHandler.class);
|
||||
private final IGroupCourseService groupCourseService;
|
||||
private final Validator validator;
|
||||
private final RedisUtil redisUtil;
|
||||
@@ -37,11 +43,12 @@ public class GroupCourseHandler {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有团课", description = "获取系统中所有团课列表")
|
||||
@Operation(summary = "获取所有团课", description = "获取系统中所有团课列表(含教练昵称)")
|
||||
public Mono<ServerResponse> getAllGroupCourse(ServerRequest request){
|
||||
boolean includeDeleted = Boolean.valueOf(request.queryParam("includeDeleted").orElse("false"));
|
||||
return ServerResponse.ok()
|
||||
.body(groupCourseService.findAll(includeDeleted), GroupCourse.class);
|
||||
return groupCourseService.findAllAsVO(includeDeleted)
|
||||
.collectList()
|
||||
.flatMap(list -> ServerResponse.ok().bodyValue(list));
|
||||
}
|
||||
|
||||
@Operation(summary = "分页获取团课", description = "根据分页参数获取团课列表")
|
||||
@@ -65,7 +72,7 @@ public class GroupCourseHandler {
|
||||
pageRequest.setOrder("asc");
|
||||
}
|
||||
|
||||
return groupCourseService.findByPage(pageRequest, includeDeleted)
|
||||
return groupCourseService.findByPageAsVO(pageRequest, includeDeleted)
|
||||
.flatMap(response -> ServerResponse.ok().bodyValue(response));
|
||||
});
|
||||
}
|
||||
@@ -289,4 +296,50 @@ public class GroupCourseHandler {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Operation(summary = "检查教练时间冲突", description = "检查教练在指定时间段内是否有时间冲突的团课")
|
||||
public Mono<ServerResponse> checkCoachConflict(ServerRequest request) {
|
||||
return request.bodyToMono(Map.class)
|
||||
.flatMap(body -> {
|
||||
if (body == null) {
|
||||
Map<String, Object> error = new HashMap<>();
|
||||
error.put("success", false);
|
||||
error.put("message", "请求体不能为空");
|
||||
return ServerResponse.badRequest().bodyValue(error);
|
||||
}
|
||||
|
||||
Object coachIdObj = body.get("coachId");
|
||||
Object startTimeStr = body.get("startTime");
|
||||
Object endTimeStr = body.get("endTime");
|
||||
Object excludeCourseIdObj = body.get("excludeCourseId");
|
||||
|
||||
if (coachIdObj == null || startTimeStr == null || endTimeStr == null) {
|
||||
Map<String, Object> error = new HashMap<>();
|
||||
error.put("success", false);
|
||||
error.put("message", "coachId、startTime、endTime 不能为空");
|
||||
return ServerResponse.badRequest().bodyValue(error);
|
||||
}
|
||||
|
||||
Long coachId = Long.valueOf(String.valueOf(coachIdObj));
|
||||
LocalDateTime startTime = LocalDateTime.parse(String.valueOf(startTimeStr));
|
||||
LocalDateTime endTime = LocalDateTime.parse(String.valueOf(endTimeStr));
|
||||
Long excludeCourseId = excludeCourseIdObj != null ? Long.valueOf(String.valueOf(excludeCourseIdObj)) : null;
|
||||
|
||||
return groupCourseService.checkCoachConflict(coachId, startTime, endTime, excludeCourseId)
|
||||
.flatMap(conflicts -> {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", true);
|
||||
response.put("hasConflict", !conflicts.isEmpty());
|
||||
response.put("conflicts", conflicts);
|
||||
return ServerResponse.ok().bodyValue(response);
|
||||
});
|
||||
})
|
||||
.onErrorResume(error -> {
|
||||
logger.warn("检查教练时间冲突失败: {}", error.getMessage());
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("success", false);
|
||||
response.put("message", "检查失败: " + error.getMessage());
|
||||
return ServerResponse.badRequest().bodyValue(response);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+7
-2
@@ -27,9 +27,14 @@ public interface IGroupCourseRepository {
|
||||
|
||||
Mono<Void> deleteById(Long id);
|
||||
|
||||
Mono<GroupCourse> updateCurrentMembers(Long id, Integer delta);
|
||||
|
||||
Flux<GroupCourse> findByCourseType(Long courseType);
|
||||
|
||||
Mono<PageResponse<GroupCourse>> searchGroupCourses(GroupCourseQueryDto query);
|
||||
|
||||
// ==================== 教练相关 ====================
|
||||
|
||||
Flux<GroupCourse> findByCoachId(Long coachId);
|
||||
Flux<GroupCourse> findByCoachId(Long coachId, Sort sort);
|
||||
Mono<Long> countByCoachIdAndStatus(Long coachId, Long status);
|
||||
Mono<Integer> cancelCoursesByCoachIdExceptStatus(Long coachId, Long excludeStatus);
|
||||
}
|
||||
|
||||
+24
-12
@@ -119,7 +119,6 @@ public class GroupCourseRepository implements IGroupCourseRepository {
|
||||
entity.setCreatedAt(LocalDateTime.now());
|
||||
entity.setUpdatedAt(LocalDateTime.now());
|
||||
entity.setStatus(0L);
|
||||
entity.setCurrentMembers(0);
|
||||
|
||||
return groupCourseDao.save(entity)
|
||||
.map(groupCourseConverter::toDomain);
|
||||
@@ -151,17 +150,6 @@ public class GroupCourseRepository implements IGroupCourseRepository {
|
||||
.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();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<GroupCourse> findByCourseType(Long courseType) {
|
||||
return groupCourseDao.findByCourseTypeAndDeletedAtIsNull(courseType)
|
||||
@@ -190,4 +178,28 @@ public class GroupCourseRepository implements IGroupCourseRepository {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 教练相关 ====================
|
||||
|
||||
@Override
|
||||
public Flux<GroupCourse> findByCoachId(Long coachId) {
|
||||
return groupCourseDao.findByCoachIdAndDeletedAtIsNull(coachId)
|
||||
.map(groupCourseConverter::toDomain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<GroupCourse> findByCoachId(Long coachId, Sort sort) {
|
||||
return groupCourseDao.findByCoachIdAndDeletedAtIsNull(coachId, sort)
|
||||
.map(groupCourseConverter::toDomain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> countByCoachIdAndStatus(Long coachId, Long status) {
|
||||
return groupCourseDao.countByCoachIdAndStatus(coachId, String.valueOf(status));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Integer> cancelCoursesByCoachIdExceptStatus(Long coachId, Long excludeStatus) {
|
||||
return groupCourseDao.cancelCoursesByCoachIdExceptStatus(coachId, String.valueOf(excludeStatus), LocalDateTime.now());
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -6,14 +6,20 @@ import cn.novalon.gym.manage.common.dto.PageResponse;
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourse;
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourseDetail;
|
||||
import cn.novalon.gym.manage.groupcourse.dto.GroupCourseQueryDto;
|
||||
import cn.novalon.gym.manage.groupcourse.vo.GroupCourseVO;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public interface IGroupCourseService {
|
||||
Mono<GroupCourse> findById(Long id);
|
||||
Mono<GroupCourseDetail> findDetailById(Long id);
|
||||
Flux<GroupCourse> findAll();
|
||||
Flux<GroupCourse> findAll(boolean includeDeleted);
|
||||
Flux<GroupCourseVO> findAllAsVO(boolean includeDeleted);
|
||||
Mono<PageResponse<GroupCourseVO>> findByPageAsVO(PageRequest pageRequest, boolean includeDeleted);
|
||||
|
||||
Mono<PageResponse<GroupCourse>> findByPage(PageRequest pageRequest, boolean includeDeleted);
|
||||
|
||||
@@ -28,4 +34,14 @@ public interface IGroupCourseService {
|
||||
Mono<Void> delete(Long id);
|
||||
|
||||
Mono<PageResponse<GroupCourse>> searchGroupCourses(GroupCourseQueryDto query);
|
||||
|
||||
/**
|
||||
* 检查教练在指定时间段内是否有时间冲突的团课
|
||||
* @param coachId 教练ID
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @param excludeCourseId 排除的课程ID(编辑时排除自身)
|
||||
* @return 冲突的课程列表
|
||||
*/
|
||||
Mono<List<GroupCourse>> checkCoachConflict(Long coachId, LocalDateTime startTime, LocalDateTime endTime, Long excludeCourseId);
|
||||
}
|
||||
|
||||
+39
-54
@@ -72,8 +72,16 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
return Mono.error(new RuntimeException("系统繁忙,请稍后重试"));
|
||||
}
|
||||
|
||||
// 2. 尝试从缓存获取课程信息,如果缓存不存在则从数据库获取
|
||||
// 2. 从缓存或数据库获取课程信息,并用实时预约数覆盖currentMembers
|
||||
return getCourseWithCache(courseId)
|
||||
.flatMap(course ->
|
||||
bookingRepository.countValidBookings(courseId)
|
||||
.map(count -> {
|
||||
course.setCurrentMembers(count.intValue());
|
||||
return course;
|
||||
})
|
||||
.defaultIfEmpty(course)
|
||||
)
|
||||
.flatMap(course -> {
|
||||
// 3. 验证课程状态
|
||||
Long courseStatus = course.getStatus();
|
||||
@@ -150,35 +158,20 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
if (saved.getId() == null) {
|
||||
return Mono.error(new RuntimeException("保存预约记录失败"));
|
||||
}
|
||||
// 10. 更新课程当前人数
|
||||
return courseRepository.updateCurrentMembers(courseId, 1)
|
||||
.flatMap(updatedCourse -> {
|
||||
// 11. 更新Redis预约计数
|
||||
return redisService.incrementBookingCount(courseId)
|
||||
.flatMap(count -> {
|
||||
// 12. 释放锁
|
||||
return redisService.releaseLock(courseId, requestId)
|
||||
.then(Mono.just(saved));
|
||||
});
|
||||
})
|
||||
.doOnSuccess(savedBooking -> {
|
||||
logger.info("预约成功:bookingId={}, courseId={}, memberId={}",
|
||||
saved.getId(), courseId, memberId);
|
||||
// 发布预约成功事件
|
||||
bookingReminderEventPublisher.publishBookingSuccessEvent(
|
||||
saved.getId(),
|
||||
saved.getMemberId(),
|
||||
saved.getCourseName(),
|
||||
saved.getCourseStartTime().toString()
|
||||
);
|
||||
})
|
||||
.doOnError(error -> {
|
||||
// 失败时回滚Redis计数和课程人数
|
||||
redisService.decrementBookingCount(courseId).subscribe();
|
||||
courseRepository.updateCurrentMembers(courseId, -1).subscribe();
|
||||
logger.error("预约失败:courseId={}, memberId={}, error={}",
|
||||
courseId, memberId, error.getMessage());
|
||||
});
|
||||
// 10. 释放锁
|
||||
return redisService.releaseLock(courseId, requestId)
|
||||
.then(Mono.just(saved));
|
||||
})
|
||||
.doOnSuccess(savedBooking -> {
|
||||
logger.info("预约成功:bookingId={}, courseId={}, memberId={}",
|
||||
savedBooking.getId(), courseId, memberId);
|
||||
// 发布预约成功事件
|
||||
bookingReminderEventPublisher.publishBookingSuccessEvent(
|
||||
savedBooking.getId(),
|
||||
savedBooking.getMemberId(),
|
||||
savedBooking.getCourseName(),
|
||||
savedBooking.getCourseStartTime().toString()
|
||||
);
|
||||
});
|
||||
})
|
||||
);
|
||||
@@ -273,30 +266,22 @@ public class GroupCourseBookingService implements IGroupCourseBookingService {
|
||||
if (rows == 0) {
|
||||
return Mono.error(new RuntimeException("更新预约状态失败"));
|
||||
}
|
||||
// 6. 减少课程当前人数
|
||||
return courseRepository.updateCurrentMembers(booking.getCourseId(), -1)
|
||||
.flatMap(updatedCourse -> {
|
||||
// 7. 更新Redis预约计数
|
||||
return redisService.decrementBookingCount(booking.getCourseId())
|
||||
.flatMap(count -> {
|
||||
// 8. 释放锁
|
||||
return redisService.releaseLock(bookingId, requestId)
|
||||
.then(bookingRepository.findById(bookingId));
|
||||
});
|
||||
})
|
||||
.doOnSuccess(updatedBooking -> {
|
||||
logger.info("取消预约成功:bookingId={}, memberId={}", bookingId, memberId);
|
||||
// 发布预约取消事件
|
||||
bookingReminderEventPublisher.publishBookingCancelEvent(
|
||||
updatedBooking.getId(),
|
||||
updatedBooking.getMemberId(),
|
||||
updatedBooking.getCourseName()
|
||||
);
|
||||
})
|
||||
.doOnError(error -> {
|
||||
logger.error("取消预约失败:bookingId={}, memberId={}, error={}",
|
||||
bookingId, memberId, error.getMessage());
|
||||
});
|
||||
// 6. 释放锁
|
||||
return redisService.releaseLock(bookingId, requestId)
|
||||
.then(bookingRepository.findById(bookingId));
|
||||
})
|
||||
.doOnSuccess(updatedBooking -> {
|
||||
logger.info("取消预约成功:bookingId={}, memberId={}", bookingId, memberId);
|
||||
// 发布预约取消事件
|
||||
bookingReminderEventPublisher.publishBookingCancelEvent(
|
||||
updatedBooking.getId(),
|
||||
updatedBooking.getMemberId(),
|
||||
updatedBooking.getCourseName()
|
||||
);
|
||||
})
|
||||
.doOnError(error -> {
|
||||
logger.error("取消预约失败:bookingId={}, memberId={}, error={}",
|
||||
bookingId, memberId, error.getMessage());
|
||||
});
|
||||
})
|
||||
.onErrorResume(error -> {
|
||||
|
||||
-42
@@ -149,46 +149,4 @@ public class GroupCourseRedisService {
|
||||
})
|
||||
.defaultIfEmpty(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取课程预约人数(缓存)
|
||||
*/
|
||||
public Mono<Integer> getBookingCount(Long courseId) {
|
||||
String key = "booking_count:" + courseId;
|
||||
return reactiveRedisTemplate.opsForValue()
|
||||
.get(key)
|
||||
.map(obj -> (Integer) obj)
|
||||
.defaultIfEmpty(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加课程预约人数
|
||||
*/
|
||||
public Mono<Long> incrementBookingCount(Long courseId) {
|
||||
String key = "booking_count:" + courseId;
|
||||
return reactiveRedisTemplate.opsForValue()
|
||||
.increment(key)
|
||||
.doOnSuccess(count -> logger.debug("预约人数增加:courseId={}, count={}", courseId, count));
|
||||
}
|
||||
|
||||
/**
|
||||
* 减少课程预约人数
|
||||
*/
|
||||
public Mono<Long> decrementBookingCount(Long courseId) {
|
||||
String key = "booking_count:" + courseId;
|
||||
return reactiveRedisTemplate.opsForValue()
|
||||
.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();
|
||||
}
|
||||
}
|
||||
|
||||
+208
-7
@@ -19,12 +19,15 @@ import cn.novalon.gym.manage.groupcourse.repository.IGroupCourseRepository;
|
||||
import cn.novalon.gym.manage.groupcourse.repository.IGroupCourseTypeRepository;
|
||||
import cn.novalon.gym.manage.groupcourse.service.IGroupCourseService;
|
||||
import cn.novalon.gym.manage.groupcourse.util.QRCodeUtil;
|
||||
import cn.novalon.gym.manage.groupcourse.vo.GroupCourseVO;
|
||||
import cn.novalon.gym.manage.file.core.service.ISysFileService;
|
||||
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 cn.novalon.gym.manage.sys.core.domain.SysUser;
|
||||
import cn.novalon.gym.manage.sys.core.repository.ISysUserRepository;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
@@ -35,7 +38,9 @@ import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@@ -53,6 +58,7 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
private final GroupCourseStateMachine stateMachine;
|
||||
private final DatabaseClient databaseClient;
|
||||
private final ISysFileService fileService;
|
||||
private final ISysUserRepository sysUserRepository;
|
||||
|
||||
private static final String CACHE_KEY_PREFIX = "group_course:page:";
|
||||
private static final String CACHE_KEY_ID_PREFIX = "group_course:id:";
|
||||
@@ -71,7 +77,8 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
ObjectMapper objectMapper,
|
||||
GroupCourseStateMachine stateMachine,
|
||||
DatabaseClient databaseClient,
|
||||
ISysFileService fileService){
|
||||
ISysFileService fileService,
|
||||
ISysUserRepository sysUserRepository){
|
||||
this.groupCourseRepository = groupCourseRepository;
|
||||
this.bookingRepository = bookingRepository;
|
||||
this.groupCourseTypeRepository = groupCourseTypeRepository;
|
||||
@@ -83,6 +90,7 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
this.stateMachine = stateMachine;
|
||||
this.databaseClient = databaseClient;
|
||||
this.fileService = fileService;
|
||||
this.sysUserRepository = sysUserRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -128,6 +136,8 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
})
|
||||
.switchIfEmpty(Mono.just(buildDetail(course, null)));
|
||||
})
|
||||
.flatMap(this::enrichCoachName)
|
||||
.flatMap(this::enrichCurrentMembers)
|
||||
.flatMap(detail -> {
|
||||
try {
|
||||
String jsonData = objectMapper.writeValueAsString(detail);
|
||||
@@ -173,6 +183,52 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
return detail;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为团课详情对象填充教练名称
|
||||
*/
|
||||
private Mono<GroupCourseDetail> enrichCoachName(GroupCourseDetail detail) {
|
||||
Long coachId = detail.getCoachId();
|
||||
if (coachId == null) {
|
||||
return Mono.just(detail);
|
||||
}
|
||||
return sysUserRepository.findByIdIncludingDeleted(coachId)
|
||||
.map(user -> {
|
||||
String name = user.getNickname() != null ? user.getNickname() : user.getUsername();
|
||||
detail.setCoachName(name);
|
||||
logger.debug("enrichCoachName: courseId={}, coachId={}, coachName={}", detail.getId(), coachId, name);
|
||||
return detail;
|
||||
})
|
||||
.doOnNext(d -> {}) // 避免空doOnNext告警
|
||||
.defaultIfEmpty(detail)
|
||||
.doOnDiscard(GroupCourseDetail.class, d ->
|
||||
logger.warn("enrichCoachName: coach not found, coachId={}, courseId={}", coachId, detail.getId())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为团课详情对象填充实时预约人数
|
||||
*/
|
||||
private Mono<GroupCourseDetail> enrichCurrentMembers(GroupCourseDetail detail) {
|
||||
return bookingRepository.countValidBookings(detail.getId())
|
||||
.map(count -> {
|
||||
detail.setCurrentMembers(count.intValue());
|
||||
return detail;
|
||||
})
|
||||
.defaultIfEmpty(detail);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为团课对象填充实时预约人数
|
||||
*/
|
||||
private Mono<GroupCourse> enrichCurrentMembers(GroupCourse course) {
|
||||
return bookingRepository.countValidBookings(course.getId())
|
||||
.map(count -> {
|
||||
course.setCurrentMembers(count.intValue());
|
||||
return course;
|
||||
})
|
||||
.defaultIfEmpty(course);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> findById(Long id) {
|
||||
String cacheKey = CACHE_KEY_ID_PREFIX + id;
|
||||
@@ -206,7 +262,8 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
}
|
||||
})
|
||||
.doOnSubscribe(sub -> logger.debug("缓存未命中,查询数据库 - findById: id={}", id))
|
||||
);
|
||||
)
|
||||
.flatMap(this::enrichCurrentMembers);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -223,6 +280,47 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<GroupCourseVO> findAllAsVO(boolean includeDeleted) {
|
||||
return findAll(includeDeleted)
|
||||
.flatMap(this::enrichCurrentMembers)
|
||||
.collectList()
|
||||
.flatMapMany(courses -> {
|
||||
if (courses.isEmpty()) {
|
||||
logger.info("findAllAsVO: no courses found");
|
||||
return Flux.empty();
|
||||
}
|
||||
var coachIds = courses.stream()
|
||||
.map(GroupCourse::getCoachId)
|
||||
.filter(id -> id != null)
|
||||
.distinct()
|
||||
.toList();
|
||||
|
||||
logger.info("findAllAsVO: {} courses, {} unique coach IDs: {}", courses.size(), coachIds.size(), coachIds);
|
||||
|
||||
if (coachIds.isEmpty()) {
|
||||
logger.info("findAllAsVO: no coach IDs, returning VOs with null coachName");
|
||||
return Flux.fromIterable(courses.stream()
|
||||
.map(c -> GroupCourseVO.from(c, null))
|
||||
.toList());
|
||||
}
|
||||
|
||||
return Flux.fromIterable(coachIds)
|
||||
.flatMap(sysUserRepository::findByIdIncludingDeleted)
|
||||
.doOnNext(u -> logger.info("findAllAsVO: found coach id={}, nickname={}, username={}", u.getId(), u.getNickname(), u.getUsername()))
|
||||
.collectMap(SysUser::getId, u -> u.getNickname() != null ? u.getNickname() : u.getUsername())
|
||||
.doOnNext(m -> logger.info("findAllAsVO: coachNameMap size={}, keys={}", m.size(), m.keySet()))
|
||||
.flatMapMany(coachNameMap ->
|
||||
Flux.fromIterable(courses.stream()
|
||||
.map(c -> {
|
||||
String name = coachNameMap.get(c.getCoachId());
|
||||
logger.debug("findAllAsVO: courseId={}, coachId={}, coachName={}", c.getId(), c.getCoachId(), name);
|
||||
return GroupCourseVO.from(c, name);
|
||||
})
|
||||
.toList()));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<PageResponse<GroupCourse>> findByPage(PageRequest pageRequest, boolean includeDeleted) {
|
||||
int page = pageRequest.getPage();
|
||||
@@ -275,6 +373,73 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<PageResponse<GroupCourseVO>> findByPageAsVO(PageRequest pageRequest, boolean includeDeleted) {
|
||||
return findByPage(pageRequest, includeDeleted)
|
||||
.flatMap(pageResponse -> {
|
||||
List<GroupCourse> courses = pageResponse.getContent();
|
||||
if (courses.isEmpty()) {
|
||||
PageResponse<GroupCourseVO> voResponse = new PageResponse<>(
|
||||
Collections.emptyList(),
|
||||
pageResponse.getTotalPages(),
|
||||
pageResponse.getTotalElements(),
|
||||
pageResponse.getCurrentPage(),
|
||||
pageResponse.getPageSize()
|
||||
);
|
||||
return Mono.just(voResponse);
|
||||
}
|
||||
|
||||
// 用实时预约人数覆盖缓存/DB中的currentMembers
|
||||
return Flux.fromIterable(courses)
|
||||
.flatMap(this::enrichCurrentMembers)
|
||||
.collectList()
|
||||
.flatMap(enrichedCourses -> {
|
||||
var coachIds = enrichedCourses.stream()
|
||||
.map(GroupCourse::getCoachId)
|
||||
.filter(id -> id != null)
|
||||
.distinct()
|
||||
.toList();
|
||||
|
||||
logger.info("findByPageAsVO: {} courses, {} unique coach IDs: {}", enrichedCourses.size(), coachIds.size(), coachIds);
|
||||
|
||||
if (coachIds.isEmpty()) {
|
||||
List<GroupCourseVO> voList = enrichedCourses.stream()
|
||||
.map(c -> GroupCourseVO.from(c, null))
|
||||
.toList();
|
||||
PageResponse<GroupCourseVO> voResponse = new PageResponse<>(
|
||||
voList,
|
||||
pageResponse.getTotalPages(),
|
||||
pageResponse.getTotalElements(),
|
||||
pageResponse.getCurrentPage(),
|
||||
pageResponse.getPageSize()
|
||||
);
|
||||
return Mono.just(voResponse);
|
||||
}
|
||||
|
||||
return Flux.fromIterable(coachIds)
|
||||
.flatMap(sysUserRepository::findByIdIncludingDeleted)
|
||||
.doOnNext(u -> logger.info("findByPageAsVO: found coach id={}, nickname={}", u.getId(), u.getNickname()))
|
||||
.collectMap(SysUser::getId, u -> u.getNickname() != null ? u.getNickname() : u.getUsername())
|
||||
.map(coachNameMap -> {
|
||||
List<GroupCourseVO> voList = enrichedCourses.stream()
|
||||
.map(c -> {
|
||||
String name = coachNameMap.get(c.getCoachId());
|
||||
logger.debug("findByPageAsVO: courseId={}, coachId={}, coachName={}", c.getId(), c.getCoachId(), name);
|
||||
return GroupCourseVO.from(c, name);
|
||||
})
|
||||
.toList();
|
||||
return new PageResponse<>(
|
||||
voList,
|
||||
pageResponse.getTotalPages(),
|
||||
pageResponse.getTotalElements(),
|
||||
pageResponse.getCurrentPage(),
|
||||
pageResponse.getPageSize()
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GroupCourse> create(GroupCourse groupCourse) {
|
||||
return groupCourseRepository.save(groupCourse)
|
||||
@@ -491,6 +656,15 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
public Mono<GroupCourse> signIn(Long courseId, Long memberId) {
|
||||
return groupCourseRepository.findByIdAndDeletedAtIsNull(courseId)
|
||||
.switchIfEmpty(Mono.error(new RuntimeException("团课不存在或已删除")))
|
||||
.flatMap(course ->
|
||||
// 用实时预约人数覆盖currentMembers
|
||||
bookingRepository.countValidBookings(courseId)
|
||||
.map(count -> {
|
||||
course.setCurrentMembers(count.intValue());
|
||||
return course;
|
||||
})
|
||||
.defaultIfEmpty(course)
|
||||
)
|
||||
.flatMap(course -> {
|
||||
// 校验1:团课已取消
|
||||
if (course.getStatus().equals(CourseStatus.CANCELLED.getValue())) {
|
||||
@@ -521,11 +695,9 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
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);
|
||||
});
|
||||
// 更新预约状态为已出席(2),不再手动计数
|
||||
return bookingRepository.updateStatus(booking.getId(), "2")
|
||||
.thenReturn(course);
|
||||
});
|
||||
})
|
||||
.doOnSuccess(course -> logger.info("团课签到成功 - courseId={}, memberId={}", courseId, memberId))
|
||||
@@ -571,4 +743,33 @@ public class GroupCourseService implements IGroupCourseService {
|
||||
.then(redisUtil.deleteByPattern(CACHE_KEY_ID_PREFIX + "*"))
|
||||
.then(redisUtil.deleteByPattern(CACHE_KEY_DETAIL_PREFIX + "*")).then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<List<GroupCourse>> checkCoachConflict(Long coachId, LocalDateTime startTime, LocalDateTime endTime, Long excludeCourseId) {
|
||||
if (coachId == null || startTime == null || endTime == null) {
|
||||
return Mono.just(Collections.emptyList());
|
||||
}
|
||||
return groupCourseRepository.findByCoachId(coachId)
|
||||
.filter(course -> {
|
||||
// 排除已取消的课程
|
||||
if (course.getStatus() != null && course.getStatus().equals(CourseStatus.CANCELLED.getValue())) {
|
||||
return false;
|
||||
}
|
||||
// 排除自身(编辑时)
|
||||
if (excludeCourseId != null && course.getId() != null && course.getId().equals(excludeCourseId)) {
|
||||
return false;
|
||||
}
|
||||
// 检查时间是否重叠:startTime < course.endTime AND course.startTime < endTime
|
||||
if (course.getStartTime() == null || course.getEndTime() == null) {
|
||||
return false;
|
||||
}
|
||||
return startTime.isBefore(course.getEndTime()) && course.getStartTime().isBefore(endTime);
|
||||
})
|
||||
.collectList()
|
||||
.doOnNext(conflicts -> {
|
||||
if (!conflicts.isEmpty()) {
|
||||
logger.info("checkCoachConflict: coachId={}, conflicts={} courses", coachId, conflicts.size());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
package cn.novalon.gym.manage.groupcourse.vo;
|
||||
|
||||
import cn.novalon.gym.manage.groupcourse.domain.GroupCourse;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 团课视图对象(含教练昵称)
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-07-19
|
||||
*/
|
||||
public class GroupCourseVO {
|
||||
|
||||
private Long id;
|
||||
private String courseName;
|
||||
private Long coachId;
|
||||
private String coachName;
|
||||
private Long courseType;
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
private Integer maxMembers;
|
||||
private Integer currentMembers;
|
||||
private Long status;
|
||||
private String location;
|
||||
private String coverImage;
|
||||
private String description;
|
||||
private BigDecimal storedValueAmount;
|
||||
private String qrCodePath;
|
||||
private String createBy;
|
||||
private String updateBy;
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
public GroupCourseVO() {}
|
||||
|
||||
/**
|
||||
* 从领域对象和教练名称构建 VO
|
||||
*/
|
||||
public static GroupCourseVO from(GroupCourse course, String coachName) {
|
||||
GroupCourseVO vo = new GroupCourseVO();
|
||||
vo.setId(course.getId());
|
||||
vo.setCourseName(course.getCourseName());
|
||||
vo.setCoachId(course.getCoachId());
|
||||
vo.setCoachName(coachName);
|
||||
vo.setCourseType(course.getCourseType());
|
||||
vo.setStartTime(course.getStartTime());
|
||||
vo.setEndTime(course.getEndTime());
|
||||
vo.setMaxMembers(course.getMaxMembers());
|
||||
vo.setCurrentMembers(course.getCurrentMembers());
|
||||
vo.setStatus(course.getStatus());
|
||||
vo.setLocation(course.getLocation());
|
||||
vo.setCoverImage(course.getCoverImage());
|
||||
vo.setDescription(course.getDescription());
|
||||
vo.setStoredValueAmount(course.getStoredValueAmount());
|
||||
vo.setQrCodePath(course.getQrCodePath());
|
||||
vo.setCreateBy(course.getCreateBy());
|
||||
vo.setUpdateBy(course.getUpdateBy());
|
||||
vo.setCreatedAt(course.getCreatedAt());
|
||||
vo.setUpdatedAt(course.getUpdatedAt());
|
||||
return vo;
|
||||
}
|
||||
|
||||
// -- getters / setters --
|
||||
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
public String getCourseName() { return courseName; }
|
||||
public void setCourseName(String courseName) { this.courseName = courseName; }
|
||||
|
||||
public Long getCoachId() { return coachId; }
|
||||
public void setCoachId(Long coachId) { this.coachId = coachId; }
|
||||
|
||||
public String getCoachName() { return coachName; }
|
||||
public void setCoachName(String coachName) { this.coachName = coachName; }
|
||||
|
||||
public Long getCourseType() { return courseType; }
|
||||
public void setCourseType(Long courseType) { this.courseType = courseType; }
|
||||
|
||||
public LocalDateTime getStartTime() { return startTime; }
|
||||
public void setStartTime(LocalDateTime startTime) { this.startTime = startTime; }
|
||||
|
||||
public LocalDateTime getEndTime() { return endTime; }
|
||||
public void setEndTime(LocalDateTime endTime) { this.endTime = endTime; }
|
||||
|
||||
public Integer getMaxMembers() { return maxMembers; }
|
||||
public void setMaxMembers(Integer maxMembers) { this.maxMembers = maxMembers; }
|
||||
|
||||
public Integer getCurrentMembers() { return currentMembers; }
|
||||
public void setCurrentMembers(Integer currentMembers) { this.currentMembers = currentMembers; }
|
||||
|
||||
public Long getStatus() { return status; }
|
||||
public void setStatus(Long status) { this.status = status; }
|
||||
|
||||
public String getLocation() { return location; }
|
||||
public void setLocation(String location) { this.location = location; }
|
||||
|
||||
public String getCoverImage() { return coverImage; }
|
||||
public void setCoverImage(String coverImage) { this.coverImage = coverImage; }
|
||||
|
||||
public String getDescription() { return description; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
|
||||
public BigDecimal getStoredValueAmount() { return storedValueAmount; }
|
||||
public void setStoredValueAmount(BigDecimal storedValueAmount) { this.storedValueAmount = storedValueAmount; }
|
||||
|
||||
public String getQrCodePath() { return qrCodePath; }
|
||||
public void setQrCodePath(String qrCodePath) { this.qrCodePath = qrCodePath; }
|
||||
|
||||
public String getCreateBy() { return createBy; }
|
||||
public void setCreateBy(String createBy) { this.createBy = createBy; }
|
||||
|
||||
public String getUpdateBy() { return updateBy; }
|
||||
public void setUpdateBy(String updateBy) { this.updateBy = updateBy; }
|
||||
|
||||
public LocalDateTime getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
public LocalDateTime getUpdatedAt() { return updatedAt; }
|
||||
public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }
|
||||
}
|
||||
Reference in New Issue
Block a user