MatchmakerApplyController.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.zhentao.controller;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.zhentao.pojo.MatchmakerApply;
  4. import com.zhentao.service.MatchmakerApplyService;
  5. import com.zhentao.vo.Result;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.*;
  8. import javax.servlet.http.HttpServletRequest;
  9. import java.io.BufferedReader;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * 红娘申请控制器
  14. */
  15. @RestController
  16. @RequestMapping("/api/matchmaker-apply")
  17. @CrossOrigin(origins = "*")
  18. public class MatchmakerApplyController {
  19. @Autowired
  20. private MatchmakerApplyService matchmakerApplyService;
  21. /**
  22. * 提交红娘申请
  23. * @param matchmakerApply 申请信息(包含userId和表单数据)
  24. * @return 申请结果
  25. */
  26. /**
  27. * 提交红娘申请 - 使用Map接收原始数据
  28. */
  29. @PostMapping("/submit")
  30. public Result<String> submitApply(@RequestBody Map<String, Object> requestData, HttpServletRequest request) {
  31. try {
  32. // 打印接收到的原始数据
  33. System.out.println("========== 红娘申请提交 ==========");
  34. System.out.println("原始请求数据: " + requestData);
  35. System.out.println("userId字段: " + requestData.get("userId"));
  36. System.out.println("userId类型: " + (requestData.get("userId") != null ? requestData.get("userId").getClass().getName() : "null"));
  37. System.out.println("==================================");
  38. // 手动转换为 MatchmakerApply 对象
  39. ObjectMapper mapper = new ObjectMapper();
  40. MatchmakerApply matchmakerApply = mapper.convertValue(requestData, MatchmakerApply.class);
  41. System.out.println("转换后的对象: " + matchmakerApply);
  42. System.out.println("转换后userId: " + matchmakerApply.getUserId());
  43. System.out.println("==================================");
  44. // 验证必填字段
  45. if (matchmakerApply.getUserId() == null) {
  46. System.err.println("❌ 用户ID为空!");
  47. return Result.error("用户ID不能为空");
  48. }
  49. if (matchmakerApply.getName() == null || matchmakerApply.getName().trim().isEmpty()) {
  50. return Result.error("姓名不能为空");
  51. }
  52. if (matchmakerApply.getPhone() == null || matchmakerApply.getPhone().trim().isEmpty()) {
  53. return Result.error("手机号不能为空");
  54. }
  55. if (matchmakerApply.getEmail() == null || matchmakerApply.getEmail().trim().isEmpty()) {
  56. return Result.error("邮箱不能为空");
  57. }
  58. if (matchmakerApply.getAge() == null) {
  59. return Result.error("年龄不能为空");
  60. }
  61. if (matchmakerApply.getGender() == null) {
  62. return Result.error("性别不能为空");
  63. }
  64. if (matchmakerApply.getArea() == null || matchmakerApply.getArea().trim().isEmpty()) {
  65. return Result.error("所在地区不能为空");
  66. }
  67. if (matchmakerApply.getExperience() == null || matchmakerApply.getExperience().trim().isEmpty()) {
  68. return Result.error("婚姻介绍经验不能为空");
  69. }
  70. // if (matchmakerApply.getServerTime() == null || matchmakerApply.getServerTime().trim().isEmpty()) {
  71. // return Result.error("可服务时间不能为空");
  72. // }
  73. if (matchmakerApply.getIntroduction() == null || matchmakerApply.getIntroduction().trim().isEmpty()) {
  74. return Result.error("个人简介不能为空");
  75. }
  76. // 提交申请
  77. boolean success = matchmakerApplyService.submitApply(matchmakerApply);
  78. if (success) {
  79. return Result.success("申请提交成功,请等待审核");
  80. } else {
  81. return Result.error("申请提交失败,请稍后重试");
  82. }
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. return Result.error("申请提交失败:" + e.getMessage());
  86. }
  87. }
  88. /**
  89. * 查询用户的红娘申请状态
  90. * @param userId 用户ID
  91. * @return 申请状态信息
  92. */
  93. @GetMapping("/status")
  94. public Result<Map<String, Object>> getApplyStatus(@RequestParam("userId") Long userId) {
  95. try {
  96. System.out.println("========== 查询红娘申请状态 ==========");
  97. System.out.println("userId: " + userId);
  98. if (userId == null) {
  99. return Result.error("用户ID不能为空");
  100. }
  101. // 查询申请记录
  102. MatchmakerApply apply = matchmakerApplyService.getApplyByUserId(userId);
  103. Map<String, Object> result = new HashMap<>();
  104. if (apply == null) {
  105. // 没有申请记录,status为null
  106. result.put("status", null);
  107. result.put("hasApplied", false);
  108. System.out.println("未找到申请记录");
  109. } else {
  110. // 有申请记录,返回status值
  111. result.put("status", apply.getStatus());
  112. result.put("hasApplied", true);
  113. result.put("applyId", apply.getApplyId());
  114. result.put("createTime", apply.getCreateTime());
  115. System.out.println("找到申请记录,status: " + apply.getStatus());
  116. }
  117. System.out.println("返回结果: " + result);
  118. System.out.println("=====================================");
  119. return Result.success(result);
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. return Result.error("查询申请状态失败:" + e.getMessage());
  123. }
  124. }
  125. }