| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- package com.train.controller;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.train.common.Result;
- import com.train.cfc.entity.CfcActivity;
- import com.train.cfc.mapper.CfcActivityMapper;
- import com.train.dto.EnrollmentVO;
- import com.train.entity.TrainClass;
- import com.train.entity.TrainCourse;
- import com.train.entity.TrainEnrollment;
- import com.train.entity.TrainInvite;
- import com.train.entity.TrainOrder;
- import com.train.entity.TrainUser;
- import com.train.mapper.TrainClassMapper;
- import com.train.mapper.TrainCourseMapper;
- import com.train.mapper.TrainEnrollmentMapper;
- import com.train.mapper.TrainInviteMapper;
- import com.train.mapper.TrainOrderMapper;
- import com.train.mapper.TrainUserMapper;
- import io.swagger.v3.oas.annotations.Operation;
- import io.swagger.v3.oas.annotations.tags.Tag;
- import org.springframework.util.StringUtils;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestAttribute;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.annotation.Resource;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Tag(name = "报名", description = "课程报名、我的报名")
- @RestController
- @RequestMapping("/api/enroll")
- public class EnrollmentController {
- @Resource
- private TrainClassMapper trainClassMapper;
- @Resource
- private TrainCourseMapper trainCourseMapper;
- @Resource
- private TrainEnrollmentMapper trainEnrollmentMapper;
- @Resource
- private TrainInviteMapper trainInviteMapper;
- @Resource
- private TrainUserMapper trainUserMapper;
- @Resource
- private CfcActivityMapper cfcActivityMapper;
- @Resource
- private TrainOrderMapper trainOrderMapper;
- /** 占用名额的报名状态 */
- private static final List<String> OCCUPY_STATUS = Arrays.asList("pending", "paid", "confirmed");
- /**
- * 可报名班次列表(含剩余名额/价格)
- */
- @Operation(summary = "可报名班次列表")
- @PostMapping("/classes")
- public Result<List<Map<String, Object>>> listClasses(@RequestBody(required = false) Map<String, Object> body) {
- List<TrainClass> classes = trainClassMapper.selectList(
- new LambdaQueryWrapper<TrainClass>()
- .eq(TrainClass::getStatus, "active")
- .orderByAsc(TrainClass::getId));
- List<Map<String, Object>> result = new ArrayList<>();
- for (TrainClass c : classes) {
- Long occupied = trainEnrollmentMapper.selectCount(
- new LambdaQueryWrapper<TrainEnrollment>()
- .eq(TrainEnrollment::getClassId, c.getId())
- .in(TrainEnrollment::getStatus, OCCUPY_STATUS));
- Map<String, Object> row = new HashMap<>();
- row.put("id", c.getId());
- row.put("courseId", c.getCourseId());
- // 报名=报课程:带课程名便于前端按课程分组展示该课程下的可报班次
- TrainCourse course = c.getCourseId() == null ? null : trainCourseMapper.selectById(c.getCourseId());
- row.put("courseName", course == null ? c.getName() : (course.getLevel() + ' ' + course.getName()));
- row.put("name", c.getName());
- row.put("time", c.getTime());
- row.put("place", c.getPlace());
- row.put("capacity", c.getCapacity());
- row.put("num", occupied == null ? 0L : occupied);
- row.put("status", c.getStatus());
- // 价格来自 cfc activities(分为单位,cfc 同库)
- int price = 0;
- int memberPrice = 0;
- if (c.getActivityId() != null) {
- CfcActivity activity = cfcActivityMapper.selectById(c.getActivityId());
- if (activity != null) {
- price = activity.getPrice() == null ? 0 : activity.getPrice();
- memberPrice = activity.getMemberPrice() == null ? price : activity.getMemberPrice();
- }
- }
- row.put("price", price);
- row.put("memberPrice", memberPrice);
- result.add(row);
- }
- return Result.success(result);
- }
- /**
- * 创建报名单(报名=报课程:courseId 必填,classId 可选,班次由助教审核时分配;
- * 带邀请码则记录转介绍关系)
- */
- @Operation(summary = "创建报名单")
- @PostMapping("/create")
- public Result<EnrollmentVO> create(@RequestBody Map<String, Object> body,
- @RequestAttribute("userId") Long userId) {
- if (body.get("courseId") == null || body.get("courseId").toString().trim().isEmpty()) {
- return Result.error("请选择要报名的课程");
- }
- Long courseId = Long.valueOf(body.get("courseId").toString());
- String name = body.get("name") == null ? "" : body.get("name").toString().trim();
- String phone = body.get("phone") == null ? "" : body.get("phone").toString().trim();
- if (!StringUtils.hasText(name) || !StringUtils.hasText(phone)) {
- return Result.error("请填写姓名和手机号");
- }
- TrainCourse course = trainCourseMapper.selectById(courseId);
- if (course == null || "draft".equals(course.getStatus())) {
- return Result.error("课程不存在或不可报名");
- }
- // 报名=报课程:班次可选(兼容存量选择班次报名的入口);若传 classId 校验归属该课程
- Long classId = null;
- if (body.get("classId") != null && !body.get("classId").toString().trim().isEmpty()) {
- classId = Long.valueOf(body.get("classId").toString());
- TrainClass trainClass = trainClassMapper.selectById(classId);
- if (trainClass == null || !"active".equals(trainClass.getStatus())) {
- return Result.error("班次不存在或不可报名");
- }
- if (trainClass.getCourseId() != null && !trainClass.getCourseId().equals(courseId)) {
- return Result.error("所选班次不属于该课程");
- }
- // 容量校验(并发下允许少量超卖,最终以支付为准)
- Long occupied = trainEnrollmentMapper.selectCount(
- new LambdaQueryWrapper<TrainEnrollment>()
- .eq(TrainEnrollment::getClassId, classId)
- .in(TrainEnrollment::getStatus, OCCUPY_STATUS));
- if (trainClass.getCapacity() != null && occupied != null && occupied >= trainClass.getCapacity()) {
- return Result.error("该班次名额已满");
- }
- }
- // 防重复报名(同课程已有未支付/在途报名单)
- Long exists = trainEnrollmentMapper.selectCount(
- new LambdaQueryWrapper<TrainEnrollment>()
- .eq(TrainEnrollment::getUid, userId)
- .eq(TrainEnrollment::getCourseId, courseId)
- .in(TrainEnrollment::getStatus, OCCUPY_STATUS));
- if (exists != null && exists > 0) {
- return Result.error("您已报名该课程,请勿重复提交");
- }
- String inviteCode = body.get("inviteCode") == null ? "" : body.get("inviteCode").toString().trim();
- TrainEnrollment e = new TrainEnrollment();
- e.setUid(userId);
- e.setCourseId(courseId);
- e.setClassId(classId);
- e.setName(name);
- e.setPhone(phone);
- e.setBringLaptop(body.get("bringLaptop") != null ? Integer.valueOf(body.get("bringLaptop").toString()) : 0);
- e.setDataReady(body.get("dataReady") == null ? null : body.get("dataReady").toString());
- e.setTopProblems(body.get("topProblems") == null ? null : body.get("topProblems").toString());
- e.setSource(StringUtils.hasText(inviteCode) ? "invite" : "scene");
- e.setInviteCode(StringUtils.hasText(inviteCode) ? inviteCode : null);
- e.setStatus("pending");
- trainEnrollmentMapper.insert(e);
- // 转介绍关系(幂等:uk_code_invitee 唯一键兜底)
- if (StringUtils.hasText(inviteCode)) {
- TrainUser inviter = trainUserMapper.selectOne(
- new LambdaQueryWrapper<TrainUser>()
- .eq(TrainUser::getInviteCode, inviteCode)
- .last("LIMIT 1"));
- if (inviter != null && !inviter.getId().equals(userId)) {
- Long invCount = trainInviteMapper.selectCount(
- new LambdaQueryWrapper<TrainInvite>()
- .eq(TrainInvite::getInviteCode, inviteCode)
- .eq(TrainInvite::getInviteeId, userId));
- if (invCount == null || invCount == 0) {
- TrainInvite inv = new TrainInvite();
- inv.setInviterId(inviter.getId());
- inv.setInviteeId(userId);
- inv.setInviteCode(inviteCode);
- inv.setSuccessful(0);
- inv.setRewardStatus("pending");
- try {
- trainInviteMapper.insert(inv);
- } catch (Exception ex) {
- // 唯一键冲突(并发重复注册)可忽略
- }
- }
- }
- }
- EnrollmentVO vo = new EnrollmentVO();
- vo.setId(e.getId());
- vo.setCourseId(courseId);
- vo.setCourseName(course.getLevel() + ' ' + course.getName());
- vo.setClassId(classId);
- vo.setClassName(classId == null ? null : trainClassMapper.selectById(classId).getName());
- vo.setName(name);
- vo.setPhone(phone);
- vo.setSource(e.getSource());
- vo.setInviteCode(e.getInviteCode());
- vo.setStatus(e.getStatus());
- return Result.success(vo);
- }
- /**
- * 我的报名单列表
- */
- @Operation(summary = "我的报名单列表")
- @PostMapping("/mine")
- public Result<List<Map<String, Object>>> mine(@RequestAttribute("userId") Long userId) {
- List<TrainEnrollment> list = trainEnrollmentMapper.selectList(
- new LambdaQueryWrapper<TrainEnrollment>()
- .eq(TrainEnrollment::getUid, userId)
- .orderByDesc(TrainEnrollment::getId));
- List<Map<String, Object>> result = new ArrayList<>();
- for (TrainEnrollment e : list) {
- Map<String, Object> row = new HashMap<>();
- row.put("id", e.getId());
- row.put("classId", e.getClassId());
- TrainClass c = e.getClassId() == null ? null : trainClassMapper.selectById(e.getClassId());
- row.put("className", c != null ? c.getName() : null);
- // 报名=报课程:回填课程信息,前端「我的报名」按课程展示
- row.put("courseId", e.getCourseId());
- String courseName = null;
- if (e.getCourseId() != null) {
- TrainCourse course = trainCourseMapper.selectById(e.getCourseId());
- if (course != null) {
- courseName = course.getLevel() + ' ' + course.getName();
- }
- }
- row.put("courseName", courseName);
- row.put("name", e.getName());
- row.put("phone", e.getPhone());
- row.put("source", e.getSource());
- row.put("inviteCode", e.getInviteCode());
- row.put("status", e.getStatus());
- // 学籍(V4.0 §3.4.1):生效时间/有效期 + 派生状态(active/dormant/none)
- row.put("enrolledAt", e.getEnrolledAt());
- row.put("validUntil", e.getValidUntil());
- row.put("enrollmentStatus", enrollmentStatus(e));
- row.put("createdAt", e.getCreatedAt());
- // 补关联订单号:未关闭订单(unpaid)用于前端继续支付/取消;paid 也回填便于查询
- TrainOrder order = trainOrderMapper.selectOne(
- new LambdaQueryWrapper<TrainOrder>()
- .eq(TrainOrder::getEnrollmentId, e.getId())
- .ne(TrainOrder::getStatus, "closed")
- .orderByDesc(TrainOrder::getId)
- .last("LIMIT 1"));
- row.put("orderNo", order == null ? null : order.getOrderNo());
- result.add(row);
- }
- return Result.success(result);
- }
- /**
- * 激活休眠学籍(V4.0 §3.4.1):激活费 = 课程费 20%;test 直通成功 → 学籍重新生效 12 个月。
- */
- @Operation(summary = "激活休眠学籍")
- @PostMapping("/activate")
- public Result<Map<String, Object>> activate(@RequestBody Map<String, Object> body,
- @RequestAttribute("userId") Long userId) {
- if (body.get("enrollmentId") == null) {
- return Result.error("缺少报名单ID");
- }
- Long enrollmentId = Long.valueOf(body.get("enrollmentId").toString());
- TrainEnrollment en = trainEnrollmentMapper.selectById(enrollmentId);
- if (en == null || !userId.equals(en.getUid())) {
- return Result.error("报名单不存在");
- }
- if (!"paid".equals(en.getStatus())) {
- return Result.error("未完成缴费,暂无学籍可激活");
- }
- if ("active".equals(enrollmentStatus(en))) {
- return Result.error("学籍仍有效,无需激活");
- }
- // 激活费 = 课程费 20%(分)
- int activationFee = 0;
- if (en.getCourseId() != null) {
- TrainCourse course = trainCourseMapper.selectById(en.getCourseId());
- if (course != null && course.getPrice() != null) {
- activationFee = course.getPrice() * 20 / 100;
- }
- }
- // test 直通:学籍重新生效 12 个月(正式环境此处需生成激活费订单走支付)
- Calendar cal = Calendar.getInstance();
- cal.setTime(new Date());
- cal.add(Calendar.MONTH, 12);
- en.setValidUntil(cal.getTime());
- en.setUpdatedAt(new Date());
- trainEnrollmentMapper.updateById(en);
- Map<String, Object> row = new HashMap<>();
- row.put("enrollmentId", en.getId());
- row.put("activationFee", activationFee);
- row.put("validUntil", en.getValidUntil());
- return Result.success(row);
- }
- /**
- * 学籍状态派生:paid 且未超期 = active;paid 且已超期 = dormant(休眠);未缴费或无有效期 = none。
- * 学籍自缴费完成自动建立(PayService 写 enrolledAt/validUntil);paid 但 validUntil 为空(存量数据)
- * 按 active 处理,避免误导为休眠。
- */
- private String enrollmentStatus(TrainEnrollment e) {
- if (e == null || !"paid".equals(e.getStatus())) {
- return "none";
- }
- if (e.getValidUntil() == null) {
- return "active";
- }
- return e.getValidUntil().after(new Date()) ? "active" : "dormant";
- }
- }
|