|
|
@@ -0,0 +1,142 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.Activity;
|
|
|
+import com.etotem.cfc.entity.ActivityRegistration;
|
|
|
+import com.etotem.cfc.mapper.ActivityMapper;
|
|
|
+import com.etotem.cfc.mapper.ActivityRegistrationMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ActivityRegistrationService {
|
|
|
+ @Resource
|
|
|
+ private ActivityRegistrationMapper registrationMapper;
|
|
|
+ @Resource
|
|
|
+ private ActivityMapper activityMapper;
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Result<Map<String, Object>> register(Long activityId, Long userId, Long childId) {
|
|
|
+ Activity activity = activityMapper.selectById(activityId);
|
|
|
+ if (activity == null) return Result.error("活动不存在");
|
|
|
+ if (!"published".equals(activity.getStatus())) return Result.error("活动未发布");
|
|
|
+ if (activity.getRequireRegistration() != null && activity.getRequireRegistration() == 0) {
|
|
|
+ return Result.error("该活动无需报名");
|
|
|
+ }
|
|
|
+ if (isRegistered(activityId, childId)) return Result.error("您已报名此活动");
|
|
|
+ ActivityRegistration reg = new ActivityRegistration();
|
|
|
+ reg.setActivityId(activityId); reg.setUserId(userId); reg.setChildId(childId);
|
|
|
+ reg.setStatus("pending"); reg.setRegisteredAt(LocalDateTime.now());
|
|
|
+ reg.setCreatedAt(LocalDateTime.now()); reg.setUpdatedAt(LocalDateTime.now());
|
|
|
+ registrationMapper.insert(reg);
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("registrationId", reg.getId()); result.put("status", "pending");
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Result<Void> cancel(Long activityId, Long userId, Long childId) {
|
|
|
+ ActivityRegistration reg = findRegistration(activityId, childId, userId, "pending");
|
|
|
+ if (reg == null) return Result.error("报名记录不存在");
|
|
|
+ reg.setStatus("cancelled"); reg.setUpdatedAt(LocalDateTime.now());
|
|
|
+ registrationMapper.updateById(reg);
|
|
|
+ return Result.success(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Result<Void> approve(Long registrationId, Long adminUserId) {
|
|
|
+ ActivityRegistration reg = registrationMapper.selectById(registrationId);
|
|
|
+ if (reg == null) return Result.error("报名记录不存在");
|
|
|
+ if (!"pending".equals(reg.getStatus())) return Result.error("当前状态不允许审核");
|
|
|
+ reg.setStatus("approved"); reg.setApprovedAt(LocalDateTime.now());
|
|
|
+ reg.setApprovedBy(adminUserId); reg.setUpdatedAt(LocalDateTime.now());
|
|
|
+ registrationMapper.updateById(reg);
|
|
|
+ return Result.success(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Result<Void> reject(Long registrationId, Long adminUserId) {
|
|
|
+ ActivityRegistration reg = registrationMapper.selectById(registrationId);
|
|
|
+ if (reg == null) return Result.error("报名记录不存在");
|
|
|
+ if (!"pending".equals(reg.getStatus())) return Result.error("当前状态不允许审核");
|
|
|
+ reg.setStatus("rejected"); reg.setApprovedAt(LocalDateTime.now());
|
|
|
+ reg.setApprovedBy(adminUserId); reg.setUpdatedAt(LocalDateTime.now());
|
|
|
+ registrationMapper.updateById(reg);
|
|
|
+ return Result.success(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<Map<String, Object>> getMyRegistrations(Long userId, Long childId, Integer page, Integer size) {
|
|
|
+ if (page == null || page < 1) page = 1;
|
|
|
+ if (size == null || size < 1) size = 10;
|
|
|
+ LambdaQueryWrapper<ActivityRegistration> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ if (userId != null) wrapper.eq(ActivityRegistration::getUserId, userId);
|
|
|
+ if (childId != null) wrapper.eq(ActivityRegistration::getChildId, childId);
|
|
|
+ wrapper.orderByDesc(ActivityRegistration::getRegisteredAt);
|
|
|
+ Page<ActivityRegistration> pageResult = registrationMapper.selectPage(new Page<>(page, size), wrapper);
|
|
|
+ List<Map<String, Object>> records = pageResult.getRecords().stream().map(reg -> {
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
+ item.put("id", reg.getId()); item.put("activityId", reg.getActivityId());
|
|
|
+ item.put("childId", reg.getChildId()); item.put("status", reg.getStatus());
|
|
|
+ item.put("registeredAt", reg.getRegisteredAt());
|
|
|
+ Activity activity = activityMapper.selectById(reg.getActivityId());
|
|
|
+ if (activity != null) {
|
|
|
+ item.put("activityTitle", activity.getTitle());
|
|
|
+ item.put("activityStartTime", activity.getStartTime());
|
|
|
+ }
|
|
|
+ return item;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("records", records); data.put("total", pageResult.getTotal());
|
|
|
+ data.put("page", pageResult.getCurrent()); data.put("size", pageResult.getSize());
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<List<Map<String, Object>>> getRegistrationsByActivity(Long activityId, String status) {
|
|
|
+ LambdaQueryWrapper<ActivityRegistration> wrapper = new LambdaQueryWrapper<ActivityRegistration>()
|
|
|
+ .eq(ActivityRegistration::getActivityId, activityId).orderByDesc(ActivityRegistration::getRegisteredAt);
|
|
|
+ if (status != null && !status.isEmpty()) wrapper.eq(ActivityRegistration::getStatus, status);
|
|
|
+ List<ActivityRegistration> list = registrationMapper.selectList(wrapper);
|
|
|
+ List<Map<String, Object>> result = list.stream().map(reg -> {
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
+ item.put("id", reg.getId()); item.put("childId", reg.getChildId());
|
|
|
+ item.put("userId", reg.getUserId()); item.put("status", reg.getStatus());
|
|
|
+ item.put("registeredAt", reg.getRegisteredAt());
|
|
|
+ return item;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isRegistered(Long activityId, Long childId) {
|
|
|
+ return registrationMapper.selectOne(new LambdaQueryWrapper<ActivityRegistration>()
|
|
|
+ .eq(ActivityRegistration::getActivityId, activityId)
|
|
|
+ .eq(ActivityRegistration::getChildId, childId)
|
|
|
+ .in(ActivityRegistration::getStatus, "pending", "approved").last("LIMIT 1")) != null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isApproved(Long activityId, Long childId) {
|
|
|
+ return registrationMapper.selectOne(new LambdaQueryWrapper<ActivityRegistration>()
|
|
|
+ .eq(ActivityRegistration::getActivityId, activityId)
|
|
|
+ .eq(ActivityRegistration::getChildId, childId)
|
|
|
+ .eq(ActivityRegistration::getStatus, "approved").last("LIMIT 1")) != null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ActivityRegistration findRegistration(Long activityId, Long childId, Long userId, String status) {
|
|
|
+ LambdaQueryWrapper<ActivityRegistration> wrapper = new LambdaQueryWrapper<ActivityRegistration>()
|
|
|
+ .eq(ActivityRegistration::getActivityId, activityId)
|
|
|
+ .eq(ActivityRegistration::getChildId, childId)
|
|
|
+ .eq(ActivityRegistration::getUserId, userId);
|
|
|
+ if (status != null) wrapper.eq(ActivityRegistration::getStatus, status);
|
|
|
+ wrapper.last("LIMIT 1");
|
|
|
+ return registrationMapper.selectOne(wrapper);
|
|
|
+ }
|
|
|
+}
|