package com.kaidi.oa.web; import com.kaidi.oa.common.ApiException; import com.kaidi.oa.common.ApiResp; import com.kaidi.oa.common.NotFoundException; import com.kaidi.oa.domain.BrandEvent; import com.kaidi.oa.domain.EventGuest; import com.kaidi.oa.repository.BrandEventRepository; import com.kaidi.oa.repository.EventGuestRepository; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.time.Instant; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; /** * 品牌推广部·活动嘉宾管理(活动策划与执行·模块4深化)。 * *

为 {@link BrandEvent} 下达嘉宾邀请({@link EventGuest}),跟踪邀请状态 * (待邀请→已邀请→已确认/已拒绝→已签到/未出席),支持到场统计。 * /checkin 端点模拟现场扫码签到,更新 checkinTime 并推进 inviteStatus=已签到。 * /stats 端点聚合邀请/确认/签到人数,支持实时大屏显示。 */ @RestController @RequestMapping("/api/oa/event-guests") public class EventGuestController { private final EventGuestRepository guestRepo; private final BrandEventRepository eventRepo; public EventGuestController(EventGuestRepository guestRepo, BrandEventRepository eventRepo) { this.guestRepo = guestRepo; this.eventRepo = eventRepo; } // ==================== CRUD ==================== @GetMapping("/by-event/{eventId}") public ApiResp> listByEvent(@PathVariable Long eventId) { findEvent(eventId); return ApiResp.ok(guestRepo.findByEventIdOrderByIdAsc(eventId)); } @GetMapping("/{id}") public ApiResp get(@PathVariable Long id) { return ApiResp.ok(findGuest(id)); } public record GuestRequest( Long eventId, String guestName, String organization, String title, String phone, String email, String guestType, String specialRequirements, String remark) { } @PostMapping public ApiResp create(@RequestBody GuestRequest req) { if (req.eventId() == null) { throw new ApiException(400, "eventId 不能为空"); } if (req.guestName() == null || req.guestName().isBlank()) { throw new ApiException(400, "嘉宾姓名(guestName) 不能为空"); } BrandEvent e = findEvent(req.eventId()); if ("已总结".equals(e.getStatus()) || "已取消".equals(e.getStatus())) { throw new ApiException(409, "「" + e.getStatus() + "」活动不能再添加嘉宾"); } EventGuest g = new EventGuest(); g.setEventId(e.getId()); g.setEventName(e.getName()); g.setGuestName(req.guestName()); g.setOrganization(req.organization()); g.setTitle(req.title()); g.setPhone(req.phone()); g.setEmail(req.email()); g.setGuestType(req.guestType() == null || req.guestType().isBlank() ? "合作伙伴" : req.guestType()); g.setSpecialRequirements(req.specialRequirements()); g.setRemark(req.remark()); g.setInviteStatus("待邀请"); g.setCreatedAt(Instant.now()); return ApiResp.ok(guestRepo.save(g)); } @PatchMapping("/{id}") public ApiResp update(@PathVariable Long id, @RequestBody GuestRequest req) { EventGuest g = findGuest(id); if (req.guestName() != null && !req.guestName().isBlank()) g.setGuestName(req.guestName()); if (req.organization() != null) g.setOrganization(req.organization()); if (req.title() != null) g.setTitle(req.title()); if (req.phone() != null) g.setPhone(req.phone()); if (req.email() != null) g.setEmail(req.email()); if (req.guestType() != null) g.setGuestType(req.guestType()); if (req.specialRequirements() != null) g.setSpecialRequirements(req.specialRequirements()); if (req.remark() != null) g.setRemark(req.remark()); return ApiResp.ok(guestRepo.save(g)); } @DeleteMapping("/{id}") public ApiResp delete(@PathVariable Long id) { if (!guestRepo.existsById(id)) { throw new NotFoundException("event guest not found: " + id); } guestRepo.deleteById(id); return ApiResp.ok(null); } // ==================== 邀请状态流转 ==================== public record InviteStatusRequest(String inviteStatus, String date) { } /** 更新邀请状态(已邀请/已确认/已拒绝)。 */ @PostMapping("/{id}/update-status") public ApiResp updateStatus(@PathVariable Long id, @RequestBody InviteStatusRequest req) { EventGuest g = findGuest(id); String allowed = "已邀请,已确认,已拒绝"; if (req.inviteStatus() == null || !allowed.contains(req.inviteStatus())) { throw new ApiException(400, "状态只能为:" + allowed); } g.setInviteStatus(req.inviteStatus()); String today = java.time.LocalDate.now().toString(); if ("已邀请".equals(req.inviteStatus())) { g.setInvitedDate(req.date() == null ? today : req.date()); } else if ("已确认".equals(req.inviteStatus())) { g.setConfirmedDate(req.date() == null ? today : req.date()); } return ApiResp.ok(guestRepo.save(g)); } /** 现场签到(已确认/已邀请 → 已签到),记录签到时间。 */ @PostMapping("/{id}/checkin") public ApiResp checkin(@PathVariable Long id) { EventGuest g = findGuest(id); if ("已签到".equals(g.getInviteStatus())) { throw new ApiException(409, "嘉宾已签到,不可重复操作"); } if ("已拒绝".equals(g.getInviteStatus()) || "未出席".equals(g.getInviteStatus())) { throw new ApiException(409, "嘉宾状态「" + g.getInviteStatus() + "」,不可签到"); } g.setInviteStatus("已签到"); g.setCheckinTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); return ApiResp.ok(guestRepo.save(g)); } /** 标记未出席(活动结束后,已确认/已邀请但未签到的嘉宾)。 */ @PostMapping("/{id}/absent") public ApiResp markAbsent(@PathVariable Long id) { EventGuest g = findGuest(id); if ("已签到".equals(g.getInviteStatus())) { throw new ApiException(409, "已签到嘉宾不能标记为未出席"); } g.setInviteStatus("未出席"); return ApiResp.ok(guestRepo.save(g)); } // ==================== 签到统计(大屏聚合)==================== public record GuestStats(Long eventId, String eventName, int total, int invited, int confirmed, int checkedIn, int absent, int refused, int pending, double checkinRate) { } /** 嘉宾签到统计:邀请/确认/签到/缺席人数,支持活动大屏实时显示。 */ @GetMapping("/stats/{eventId}") public ApiResp stats(@PathVariable Long eventId) { BrandEvent e = findEvent(eventId); List guests = guestRepo.findByEventIdOrderByIdAsc(eventId); int total = guests.size(); int invited = 0, confirmed = 0, checkedIn = 0, absent = 0, refused = 0, pending = 0; for (EventGuest g : guests) { switch (g.getInviteStatus() == null ? "待邀请" : g.getInviteStatus()) { case "待邀请" -> pending++; case "已邀请" -> invited++; case "已确认" -> confirmed++; case "已签到" -> checkedIn++; case "未出席" -> absent++; case "已拒绝" -> refused++; default -> pending++; } } double rate = total == 0 ? 0 : Math.round(checkedIn * 1000.0 / total) / 10.0; return ApiResp.ok(new GuestStats(eventId, e.getName(), total, invited, confirmed, checkedIn, absent, refused, pending, rate)); } // ==================== 私有工具 ==================== private BrandEvent findEvent(Long id) { return eventRepo.findById(id) .orElseThrow(() -> new NotFoundException("brand event not found: " + id)); } private EventGuest findGuest(Long id) { return guestRepo.findById(id) .orElseThrow(() -> new NotFoundException("event guest not found: " + id)); } }