EnrollmentController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package com.train.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.train.common.Result;
  4. import com.train.cfc.entity.CfcActivity;
  5. import com.train.cfc.mapper.CfcActivityMapper;
  6. import com.train.dto.EnrollmentVO;
  7. import com.train.entity.TrainClass;
  8. import com.train.entity.TrainCourse;
  9. import com.train.entity.TrainEnrollment;
  10. import com.train.entity.TrainInvite;
  11. import com.train.entity.TrainOrder;
  12. import com.train.entity.TrainUser;
  13. import com.train.mapper.TrainClassMapper;
  14. import com.train.mapper.TrainCourseMapper;
  15. import com.train.mapper.TrainEnrollmentMapper;
  16. import com.train.mapper.TrainInviteMapper;
  17. import com.train.mapper.TrainOrderMapper;
  18. import com.train.mapper.TrainUserMapper;
  19. import io.swagger.v3.oas.annotations.Operation;
  20. import io.swagger.v3.oas.annotations.tags.Tag;
  21. import org.springframework.util.StringUtils;
  22. import org.springframework.web.bind.annotation.PostMapping;
  23. import org.springframework.web.bind.annotation.RequestAttribute;
  24. import org.springframework.web.bind.annotation.RequestBody;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import org.springframework.web.bind.annotation.RestController;
  27. import javax.annotation.Resource;
  28. import java.util.ArrayList;
  29. import java.util.Arrays;
  30. import java.util.Calendar;
  31. import java.util.Date;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. @Tag(name = "报名", description = "课程报名、我的报名")
  36. @RestController
  37. @RequestMapping("/api/enroll")
  38. public class EnrollmentController {
  39. @Resource
  40. private TrainClassMapper trainClassMapper;
  41. @Resource
  42. private TrainCourseMapper trainCourseMapper;
  43. @Resource
  44. private TrainEnrollmentMapper trainEnrollmentMapper;
  45. @Resource
  46. private TrainInviteMapper trainInviteMapper;
  47. @Resource
  48. private TrainUserMapper trainUserMapper;
  49. @Resource
  50. private CfcActivityMapper cfcActivityMapper;
  51. @Resource
  52. private TrainOrderMapper trainOrderMapper;
  53. /** 占用名额的报名状态 */
  54. private static final List<String> OCCUPY_STATUS = Arrays.asList("pending", "paid", "confirmed");
  55. /**
  56. * 可报名班次列表(含剩余名额/价格)
  57. */
  58. @Operation(summary = "可报名班次列表")
  59. @PostMapping("/classes")
  60. public Result<List<Map<String, Object>>> listClasses(@RequestBody(required = false) Map<String, Object> body) {
  61. List<TrainClass> classes = trainClassMapper.selectList(
  62. new LambdaQueryWrapper<TrainClass>()
  63. .eq(TrainClass::getStatus, "active")
  64. .orderByAsc(TrainClass::getId));
  65. List<Map<String, Object>> result = new ArrayList<>();
  66. for (TrainClass c : classes) {
  67. Long occupied = trainEnrollmentMapper.selectCount(
  68. new LambdaQueryWrapper<TrainEnrollment>()
  69. .eq(TrainEnrollment::getClassId, c.getId())
  70. .in(TrainEnrollment::getStatus, OCCUPY_STATUS));
  71. Map<String, Object> row = new HashMap<>();
  72. row.put("id", c.getId());
  73. row.put("courseId", c.getCourseId());
  74. // 报名=报课程:带课程名便于前端按课程分组展示该课程下的可报班次
  75. TrainCourse course = c.getCourseId() == null ? null : trainCourseMapper.selectById(c.getCourseId());
  76. row.put("courseName", course == null ? c.getName() : (course.getLevel() + ' ' + course.getName()));
  77. row.put("name", c.getName());
  78. row.put("time", c.getTime());
  79. row.put("place", c.getPlace());
  80. row.put("capacity", c.getCapacity());
  81. row.put("num", occupied == null ? 0L : occupied);
  82. row.put("status", c.getStatus());
  83. // 价格来自 cfc activities(分为单位,cfc 同库)
  84. int price = 0;
  85. int memberPrice = 0;
  86. if (c.getActivityId() != null) {
  87. CfcActivity activity = cfcActivityMapper.selectById(c.getActivityId());
  88. if (activity != null) {
  89. price = activity.getPrice() == null ? 0 : activity.getPrice();
  90. memberPrice = activity.getMemberPrice() == null ? price : activity.getMemberPrice();
  91. }
  92. }
  93. row.put("price", price);
  94. row.put("memberPrice", memberPrice);
  95. result.add(row);
  96. }
  97. return Result.success(result);
  98. }
  99. /**
  100. * 创建报名单(报名=报课程:courseId 必填,classId 可选,班次由助教审核时分配;
  101. * 带邀请码则记录转介绍关系)
  102. */
  103. @Operation(summary = "创建报名单")
  104. @PostMapping("/create")
  105. public Result<EnrollmentVO> create(@RequestBody Map<String, Object> body,
  106. @RequestAttribute("userId") Long userId) {
  107. if (body.get("courseId") == null || body.get("courseId").toString().trim().isEmpty()) {
  108. return Result.error("请选择要报名的课程");
  109. }
  110. Long courseId = Long.valueOf(body.get("courseId").toString());
  111. String name = body.get("name") == null ? "" : body.get("name").toString().trim();
  112. String phone = body.get("phone") == null ? "" : body.get("phone").toString().trim();
  113. if (!StringUtils.hasText(name) || !StringUtils.hasText(phone)) {
  114. return Result.error("请填写姓名和手机号");
  115. }
  116. TrainCourse course = trainCourseMapper.selectById(courseId);
  117. if (course == null || "draft".equals(course.getStatus())) {
  118. return Result.error("课程不存在或不可报名");
  119. }
  120. // 报名=报课程:班次可选(兼容存量选择班次报名的入口);若传 classId 校验归属该课程
  121. Long classId = null;
  122. if (body.get("classId") != null && !body.get("classId").toString().trim().isEmpty()) {
  123. classId = Long.valueOf(body.get("classId").toString());
  124. TrainClass trainClass = trainClassMapper.selectById(classId);
  125. if (trainClass == null || !"active".equals(trainClass.getStatus())) {
  126. return Result.error("班次不存在或不可报名");
  127. }
  128. if (trainClass.getCourseId() != null && !trainClass.getCourseId().equals(courseId)) {
  129. return Result.error("所选班次不属于该课程");
  130. }
  131. // 容量校验(并发下允许少量超卖,最终以支付为准)
  132. Long occupied = trainEnrollmentMapper.selectCount(
  133. new LambdaQueryWrapper<TrainEnrollment>()
  134. .eq(TrainEnrollment::getClassId, classId)
  135. .in(TrainEnrollment::getStatus, OCCUPY_STATUS));
  136. if (trainClass.getCapacity() != null && occupied != null && occupied >= trainClass.getCapacity()) {
  137. return Result.error("该班次名额已满");
  138. }
  139. }
  140. // 防重复报名(同课程已有未支付/在途报名单)
  141. Long exists = trainEnrollmentMapper.selectCount(
  142. new LambdaQueryWrapper<TrainEnrollment>()
  143. .eq(TrainEnrollment::getUid, userId)
  144. .eq(TrainEnrollment::getCourseId, courseId)
  145. .in(TrainEnrollment::getStatus, OCCUPY_STATUS));
  146. if (exists != null && exists > 0) {
  147. return Result.error("您已报名该课程,请勿重复提交");
  148. }
  149. String inviteCode = body.get("inviteCode") == null ? "" : body.get("inviteCode").toString().trim();
  150. TrainEnrollment e = new TrainEnrollment();
  151. e.setUid(userId);
  152. e.setCourseId(courseId);
  153. e.setClassId(classId);
  154. e.setName(name);
  155. e.setPhone(phone);
  156. e.setBringLaptop(body.get("bringLaptop") != null ? Integer.valueOf(body.get("bringLaptop").toString()) : 0);
  157. e.setDataReady(body.get("dataReady") == null ? null : body.get("dataReady").toString());
  158. e.setTopProblems(body.get("topProblems") == null ? null : body.get("topProblems").toString());
  159. e.setSource(StringUtils.hasText(inviteCode) ? "invite" : "scene");
  160. e.setInviteCode(StringUtils.hasText(inviteCode) ? inviteCode : null);
  161. e.setStatus("pending");
  162. trainEnrollmentMapper.insert(e);
  163. // 转介绍关系(幂等:uk_code_invitee 唯一键兜底)
  164. if (StringUtils.hasText(inviteCode)) {
  165. TrainUser inviter = trainUserMapper.selectOne(
  166. new LambdaQueryWrapper<TrainUser>()
  167. .eq(TrainUser::getInviteCode, inviteCode)
  168. .last("LIMIT 1"));
  169. if (inviter != null && !inviter.getId().equals(userId)) {
  170. Long invCount = trainInviteMapper.selectCount(
  171. new LambdaQueryWrapper<TrainInvite>()
  172. .eq(TrainInvite::getInviteCode, inviteCode)
  173. .eq(TrainInvite::getInviteeId, userId));
  174. if (invCount == null || invCount == 0) {
  175. TrainInvite inv = new TrainInvite();
  176. inv.setInviterId(inviter.getId());
  177. inv.setInviteeId(userId);
  178. inv.setInviteCode(inviteCode);
  179. inv.setSuccessful(0);
  180. inv.setRewardStatus("pending");
  181. try {
  182. trainInviteMapper.insert(inv);
  183. } catch (Exception ex) {
  184. // 唯一键冲突(并发重复注册)可忽略
  185. }
  186. }
  187. }
  188. }
  189. EnrollmentVO vo = new EnrollmentVO();
  190. vo.setId(e.getId());
  191. vo.setCourseId(courseId);
  192. vo.setCourseName(course.getLevel() + ' ' + course.getName());
  193. vo.setClassId(classId);
  194. vo.setClassName(classId == null ? null : trainClassMapper.selectById(classId).getName());
  195. vo.setName(name);
  196. vo.setPhone(phone);
  197. vo.setSource(e.getSource());
  198. vo.setInviteCode(e.getInviteCode());
  199. vo.setStatus(e.getStatus());
  200. return Result.success(vo);
  201. }
  202. /**
  203. * 我的报名单列表
  204. */
  205. @Operation(summary = "我的报名单列表")
  206. @PostMapping("/mine")
  207. public Result<List<Map<String, Object>>> mine(@RequestAttribute("userId") Long userId) {
  208. List<TrainEnrollment> list = trainEnrollmentMapper.selectList(
  209. new LambdaQueryWrapper<TrainEnrollment>()
  210. .eq(TrainEnrollment::getUid, userId)
  211. .orderByDesc(TrainEnrollment::getId));
  212. List<Map<String, Object>> result = new ArrayList<>();
  213. for (TrainEnrollment e : list) {
  214. Map<String, Object> row = new HashMap<>();
  215. row.put("id", e.getId());
  216. row.put("classId", e.getClassId());
  217. TrainClass c = e.getClassId() == null ? null : trainClassMapper.selectById(e.getClassId());
  218. row.put("className", c != null ? c.getName() : null);
  219. // 报名=报课程:回填课程信息,前端「我的报名」按课程展示
  220. row.put("courseId", e.getCourseId());
  221. String courseName = null;
  222. if (e.getCourseId() != null) {
  223. TrainCourse course = trainCourseMapper.selectById(e.getCourseId());
  224. if (course != null) {
  225. courseName = course.getLevel() + ' ' + course.getName();
  226. }
  227. }
  228. row.put("courseName", courseName);
  229. row.put("name", e.getName());
  230. row.put("phone", e.getPhone());
  231. row.put("source", e.getSource());
  232. row.put("inviteCode", e.getInviteCode());
  233. row.put("status", e.getStatus());
  234. // 学籍(V4.0 §3.4.1):生效时间/有效期 + 派生状态(active/dormant/none)
  235. row.put("enrolledAt", e.getEnrolledAt());
  236. row.put("validUntil", e.getValidUntil());
  237. row.put("enrollmentStatus", enrollmentStatus(e));
  238. row.put("createdAt", e.getCreatedAt());
  239. // 补关联订单号:未关闭订单(unpaid)用于前端继续支付/取消;paid 也回填便于查询
  240. TrainOrder order = trainOrderMapper.selectOne(
  241. new LambdaQueryWrapper<TrainOrder>()
  242. .eq(TrainOrder::getEnrollmentId, e.getId())
  243. .ne(TrainOrder::getStatus, "closed")
  244. .orderByDesc(TrainOrder::getId)
  245. .last("LIMIT 1"));
  246. row.put("orderNo", order == null ? null : order.getOrderNo());
  247. result.add(row);
  248. }
  249. return Result.success(result);
  250. }
  251. /**
  252. * 激活休眠学籍(V4.0 §3.4.1):激活费 = 课程费 20%;test 直通成功 → 学籍重新生效 12 个月。
  253. */
  254. @Operation(summary = "激活休眠学籍")
  255. @PostMapping("/activate")
  256. public Result<Map<String, Object>> activate(@RequestBody Map<String, Object> body,
  257. @RequestAttribute("userId") Long userId) {
  258. if (body.get("enrollmentId") == null) {
  259. return Result.error("缺少报名单ID");
  260. }
  261. Long enrollmentId = Long.valueOf(body.get("enrollmentId").toString());
  262. TrainEnrollment en = trainEnrollmentMapper.selectById(enrollmentId);
  263. if (en == null || !userId.equals(en.getUid())) {
  264. return Result.error("报名单不存在");
  265. }
  266. if (!"paid".equals(en.getStatus())) {
  267. return Result.error("未完成缴费,暂无学籍可激活");
  268. }
  269. if ("active".equals(enrollmentStatus(en))) {
  270. return Result.error("学籍仍有效,无需激活");
  271. }
  272. // 激活费 = 课程费 20%(分)
  273. int activationFee = 0;
  274. if (en.getCourseId() != null) {
  275. TrainCourse course = trainCourseMapper.selectById(en.getCourseId());
  276. if (course != null && course.getPrice() != null) {
  277. activationFee = course.getPrice() * 20 / 100;
  278. }
  279. }
  280. // test 直通:学籍重新生效 12 个月(正式环境此处需生成激活费订单走支付)
  281. Calendar cal = Calendar.getInstance();
  282. cal.setTime(new Date());
  283. cal.add(Calendar.MONTH, 12);
  284. en.setValidUntil(cal.getTime());
  285. en.setUpdatedAt(new Date());
  286. trainEnrollmentMapper.updateById(en);
  287. Map<String, Object> row = new HashMap<>();
  288. row.put("enrollmentId", en.getId());
  289. row.put("activationFee", activationFee);
  290. row.put("validUntil", en.getValidUntil());
  291. return Result.success(row);
  292. }
  293. /**
  294. * 学籍状态派生:paid 且未超期 = active;paid 且已超期 = dormant(休眠);未缴费或无有效期 = none。
  295. * 学籍自缴费完成自动建立(PayService 写 enrolledAt/validUntil);paid 但 validUntil 为空(存量数据)
  296. * 按 active 处理,避免误导为休眠。
  297. */
  298. private String enrollmentStatus(TrainEnrollment e) {
  299. if (e == null || !"paid".equals(e.getStatus())) {
  300. return "none";
  301. }
  302. if (e.getValidUntil() == null) {
  303. return "active";
  304. }
  305. return e.getValidUntil().after(new Date()) ? "active" : "dormant";
  306. }
  307. }