GuideManagementController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package com.etotem.cfc.controller.guide;
  2. import com.etotem.cfc.common.Result;
  3. import com.etotem.cfc.dto.GuideRegisterDTO;
  4. import com.etotem.cfc.dto.PackagePurchaseDTO;
  5. import com.etotem.cfc.entity.GuideApplication;
  6. import com.etotem.cfc.entity.GuideApplicationRecord;
  7. import com.etotem.cfc.entity.GuideInviteCode;
  8. import com.etotem.cfc.entity.GuidePackage;
  9. import com.etotem.cfc.entity.GuideHierarchy;
  10. import com.etotem.cfc.entity.PackageOrder;
  11. import com.etotem.cfc.entity.User;
  12. import com.etotem.cfc.mapper.UserMapper;
  13. import com.etotem.cfc.service.GuideApplicationService;
  14. import com.etotem.cfc.service.GuideHierarchyService;
  15. import com.etotem.cfc.service.GuideInviteCodeService;
  16. import com.etotem.cfc.service.GuidePackageService;
  17. import com.etotem.cfc.service.UserService;
  18. import io.swagger.v3.oas.annotations.Operation;
  19. import io.swagger.v3.oas.annotations.tags.Tag;
  20. import org.springframework.web.bind.annotation.*;
  21. import javax.annotation.Resource;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * 成长规划师管理控制器
  27. */
  28. @Tag(name = "成长规划师管理", description = "成长规划师注册、邀请码、层级关系等接口")
  29. @RestController
  30. @RequestMapping("/api/guide")
  31. public class GuideManagementController {
  32. @Resource
  33. private GuideInviteCodeService guideInviteCodeService;
  34. @Resource
  35. private GuideHierarchyService guideHierarchyService;
  36. @Resource
  37. private GuidePackageService guidePackageService;
  38. @Resource
  39. private UserMapper userMapper;
  40. @Resource
  41. private UserService userService;
  42. @Resource
  43. private GuideApplicationService guideApplicationService;
  44. /**
  45. * 成长规划师注册(保存草稿)
  46. */
  47. @Operation(summary = "成长规划师注册")
  48. @PostMapping("/register")
  49. public Result<Boolean> registerGuide(@RequestBody GuideRegisterDTO dto) {
  50. // 检查用户是否存在
  51. User user = userMapper.selectById(dto.getUserId());
  52. if (user == null) {
  53. return Result.error("用户不存在");
  54. }
  55. // 更新用户信息
  56. user.setTeacherNo(dto.getTeacherNo());
  57. user.setTeacherPhoto(dto.getTeacherPhoto());
  58. user.setCertificateImage(dto.getCertificateImage());
  59. user.setRealName(dto.getRealName());
  60. user.setPhone(dto.getPhone());
  61. user.setIdCard(dto.getIdCard());
  62. user.setAddress(dto.getAddress());
  63. user.setTeacherStatus("pending");
  64. user.setUpdatedAt(new java.util.Date());
  65. userMapper.updateById(user);
  66. // 创建申请记录
  67. guideApplicationService.createDraft(
  68. dto.getUserId(), "new_apply", "junior", null,
  69. dto.getRealName(), dto.getPhone(), dto.getIdCard(),
  70. dto.getTeacherNo(), dto.getTeacherPhoto(), dto.getCertificateImage(),
  71. dto.getAddress(), dto.getBankCard(), dto.getBankName()
  72. );
  73. // 将teacher角色添加到用户的角色列表中
  74. userService.addRole(user.getId(), "teacher");
  75. return Result.success(true);
  76. }
  77. /**
  78. * 提交申请(draft -> pending)
  79. */
  80. @Operation(summary = "提交成长规划师申请")
  81. @PostMapping("/application/submit")
  82. public Result<Boolean> submitApplication(@RequestAttribute("userId") Long userId) {
  83. // 查找用户的草稿申请
  84. List<GuideApplication> apps = guideApplicationService.getUserApplications(userId);
  85. GuideApplication draft = null;
  86. for (GuideApplication app : apps) {
  87. if ("draft".equals(app.getStatus())) {
  88. draft = app;
  89. break;
  90. }
  91. }
  92. if (draft == null) {
  93. return Result.error("未找到草稿申请");
  94. }
  95. boolean success = guideApplicationService.submitApplication(draft.getId(), userId);
  96. return success ? Result.success(true) : Result.error("提交失败");
  97. }
  98. /**
  99. * 撤回申请
  100. */
  101. @Operation(summary = "撤回成长规划师申请")
  102. @PostMapping("/application/withdraw")
  103. public Result<Boolean> withdrawApplication(@RequestAttribute("userId") Long userId) {
  104. List<GuideApplication> apps = guideApplicationService.getUserApplications(userId);
  105. GuideApplication pending = null;
  106. for (GuideApplication app : apps) {
  107. if ("pending".equals(app.getStatus())) {
  108. pending = app;
  109. break;
  110. }
  111. }
  112. if (pending == null) {
  113. return Result.error("未找到待审核的申请");
  114. }
  115. boolean success = guideApplicationService.withdrawApplication(pending.getId(), userId);
  116. return success ? Result.success(true) : Result.error("撤回失败");
  117. }
  118. /**
  119. * 重新提交申请
  120. */
  121. @Operation(summary = "重新提交成长规划师申请")
  122. @PostMapping("/application/resubmit")
  123. public Result<Boolean> resubmitApplication(@RequestAttribute("userId") Long userId) {
  124. List<GuideApplication> apps = guideApplicationService.getUserApplications(userId);
  125. GuideApplication target = null;
  126. for (GuideApplication app : apps) {
  127. if ("withdrawn".equals(app.getStatus()) || "rejected".equals(app.getStatus())) {
  128. target = app;
  129. break;
  130. }
  131. }
  132. if (target == null) {
  133. return Result.error("未找到可重新提交的申请");
  134. }
  135. boolean success = guideApplicationService.resubmitApplication(target.getId(), userId);
  136. return success ? Result.success(true) : Result.error("重新提交失败");
  137. }
  138. /**
  139. * 获取用户申请列表
  140. */
  141. @Operation(summary = "获取用户申请列表")
  142. @PostMapping("/applications")
  143. public Result<List<GuideApplication>> getUserApplications(@RequestAttribute("userId") Long userId) {
  144. return Result.success(guideApplicationService.getUserApplications(userId));
  145. }
  146. /**
  147. * 获取申请处理记录
  148. */
  149. @Operation(summary = "获取申请处理记录")
  150. @PostMapping("/application/{id}/records")
  151. public Result<List<GuideApplicationRecord>> getApplicationRecords(@PathVariable Long id) {
  152. return Result.success(guideApplicationService.getApplicationRecords(id));
  153. }
  154. /**
  155. * 如果有邀请码,验证并建立层级关系
  156. */
  157. private void processInviteCode(String inviteCode, Long userId) {
  158. if (inviteCode != null && !inviteCode.isEmpty()) {
  159. GuideInviteCode code = guideInviteCodeService.validateInviteCode(inviteCode);
  160. if (code != null) {
  161. guideHierarchyService.addChildGuide(code.getGuideId(), userId);
  162. guideInviteCodeService.useInviteCode(inviteCode, userId);
  163. }
  164. }
  165. }
  166. /**
  167. * 生成邀请码
  168. */
  169. @Operation(summary = "生成邀请码")
  170. @PostMapping("/invite-code/generate")
  171. public Result<GuideInviteCode> generateInviteCode(@RequestAttribute("userId") Long userId,
  172. @RequestParam(defaultValue = "1") Integer maxUseCount,
  173. @RequestParam(defaultValue = "30") Integer expireDays) {
  174. GuideInviteCode inviteCode = guideInviteCodeService.generateInviteCode(userId, maxUseCount, expireDays);
  175. return Result.success(inviteCode);
  176. }
  177. /**
  178. * 获取邀请码列表
  179. */
  180. @Operation(summary = "获取邀请码列表")
  181. @PostMapping("/invite-codes")
  182. public Result<List<GuideInviteCode>> getInviteCodes(@RequestAttribute("userId") Long userId) {
  183. List<GuideInviteCode> inviteCodes = guideInviteCodeService.getGuideInviteCodes(userId);
  184. return Result.success(inviteCodes);
  185. }
  186. /**
  187. * 验证邀请码
  188. */
  189. @Operation(summary = "验证邀请码")
  190. @PostMapping("/invite-code/validate")
  191. public Result<Map<String, Object>> validateInviteCode(@RequestParam String code) {
  192. GuideInviteCode inviteCode = guideInviteCodeService.validateInviteCode(code);
  193. if (inviteCode == null) {
  194. return Result.error("邀请码无效或已过期");
  195. }
  196. Map<String, Object> result = new HashMap<>();
  197. result.put("guideId", inviteCode.getGuideId());
  198. result.put("code", inviteCode.getCode());
  199. result.put("maxUseCount", inviteCode.getMaxUseCount());
  200. result.put("useCount", inviteCode.getUseCount());
  201. result.put("expireAt", inviteCode.getExpireAt());
  202. User guide = userMapper.selectById(inviteCode.getGuideId());
  203. if (guide != null) {
  204. result.put("guideName", guide.getNickname());
  205. result.put("guideAvatar", guide.getAvatar());
  206. }
  207. return Result.success(result);
  208. }
  209. /**
  210. * 获取下级成长规划师列表
  211. */
  212. @Operation(summary = "获取下级成长规划师列表")
  213. @PostMapping("/child-guides")
  214. public Result<List<User>> getChildGuides(@RequestAttribute("userId") Long userId) {
  215. List<User> childGuides = guideHierarchyService.getChildGuides(userId);
  216. return Result.success(childGuides);
  217. }
  218. /**
  219. * 获取所有下级成长规划师(包括间接下级)
  220. */
  221. @Operation(summary = "获取所有下级成长规划师")
  222. @PostMapping("/all-child-guides")
  223. public Result<List<User>> getAllChildGuides(@RequestAttribute("userId") Long userId) {
  224. List<User> childGuides = guideHierarchyService.getAllChildGuides(userId);
  225. return Result.success(childGuides);
  226. }
  227. /**
  228. * 获取上级成长规划师
  229. */
  230. @Operation(summary = "获取上级成长规划师")
  231. @PostMapping("/parent-guide")
  232. public Result<User> getParentGuide(@RequestAttribute("userId") Long userId) {
  233. User parentGuide = guideHierarchyService.getParentGuide(userId);
  234. return Result.success(parentGuide);
  235. }
  236. /**
  237. * 添加成长规划师套餐
  238. */
  239. @Operation(summary = "添加成长规划师套餐")
  240. @PostMapping("/package/add")
  241. public Result<Boolean> addGuidePackage(@RequestAttribute("userId") Long userId,
  242. @RequestBody Map<String, Object> params) {
  243. Long packageId = Long.valueOf(params.get("packageId").toString());
  244. Integer price = Integer.valueOf(params.get("price").toString());
  245. Integer commissionRate = params.containsKey("commissionRate")
  246. ? Integer.valueOf(params.get("commissionRate").toString())
  247. : 1000; // 默认10%
  248. boolean success = guidePackageService.addGuidePackage(userId, packageId, price, commissionRate);
  249. return Result.success(success);
  250. }
  251. /**
  252. * 获取成长规划师对特定套餐的定价
  253. */
  254. @Operation(summary = "获取成长规划师套餐定价")
  255. @PostMapping("/package/{packageId}")
  256. public Result<GuidePackage> getGuidePackage(@RequestAttribute("userId") Long userId,
  257. @PathVariable Long packageId) {
  258. GuidePackage guidePackage = guidePackageService.getGuidePackage(userId, packageId);
  259. return Result.success(guidePackage);
  260. }
  261. /**
  262. * 更新成长规划师套餐价格
  263. */
  264. @Operation(summary = "更新成长规划师套餐价格")
  265. @PostMapping("/package/{packageId}/update")
  266. public Result<Boolean> updateGuidePackage(@RequestAttribute("userId") Long userId,
  267. @PathVariable Long packageId,
  268. @RequestBody Map<String, Object> params) {
  269. Integer price = Integer.valueOf(params.get("price").toString());
  270. boolean success = guidePackageService.updateGuidePackagePrice(userId, packageId, price);
  271. return Result.success(success);
  272. }
  273. /**
  274. * 删除成长规划师套餐
  275. */
  276. @Operation(summary = "删除成长规划师套餐")
  277. @PostMapping("/package/{packageId}/delete")
  278. public Result<Boolean> deleteGuidePackage(@RequestAttribute("userId") Long userId,
  279. @PathVariable Long packageId) {
  280. boolean success = guidePackageService.deleteGuidePackage(userId, packageId);
  281. return Result.success(success);
  282. }
  283. }