|
|
@@ -0,0 +1,292 @@
|
|
|
+package com.etotem.num.service;
|
|
|
+
|
|
|
+import com.etotem.num.entity.Order;
|
|
|
+import com.etotem.num.entity.PlanRequest;
|
|
|
+import com.etotem.num.entity.PlanRequestLog;
|
|
|
+import com.etotem.num.entity.User;
|
|
|
+import com.etotem.num.repository.PlanRequestLogRepository;
|
|
|
+import com.etotem.num.repository.PlanRequestRepository;
|
|
|
+import com.etotem.num.repository.UserRepository;
|
|
|
+import com.etotem.num.common.BizException;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class PlanRequestService {
|
|
|
+
|
|
|
+ private final PlanRequestRepository planRequestRepository;
|
|
|
+ private final PlanRequestLogRepository planRequestLogRepository;
|
|
|
+ private final UserRepository userRepository;
|
|
|
+ private final OrderService orderService;
|
|
|
+
|
|
|
+ public PlanRequestService(PlanRequestRepository planRequestRepository,
|
|
|
+ PlanRequestLogRepository planRequestLogRepository,
|
|
|
+ UserRepository userRepository,
|
|
|
+ OrderService orderService) {
|
|
|
+ this.planRequestRepository = planRequestRepository;
|
|
|
+ this.planRequestLogRepository = planRequestLogRepository;
|
|
|
+ this.userRepository = userRepository;
|
|
|
+ this.orderService = orderService;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * US-9.2: Apply for a practitioner plan.
|
|
|
+ * Auto-assigns to practitioner if user has an invitedBy link.
|
|
|
+ * @return Map containing planRequest, assigned (boolean), and message (String)
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public Map<String, Object> applyPlanRequest(Long userId, Long chartId, String requestType,
|
|
|
+ String description, String budgetRange) {
|
|
|
+ PlanRequest request = new PlanRequest();
|
|
|
+ request.setUserId(userId);
|
|
|
+ request.setChartId(chartId);
|
|
|
+ request.setRequestType(requestType);
|
|
|
+ request.setDescription(description);
|
|
|
+ request.setBudgetRange(budgetRange);
|
|
|
+ request.setStatus("pending");
|
|
|
+ request.setNegotiationCount(0);
|
|
|
+
|
|
|
+ boolean assigned = false;
|
|
|
+ String message = "暂未开放系统分配,请通过推荐链接找到专属能量师";
|
|
|
+
|
|
|
+ // Auto-assignment: if user has invitedBy and inviter is a practitioner
|
|
|
+ User user = userRepository.findById(userId).orElse(null);
|
|
|
+ if (user != null && user.getInvitedBy() != null) {
|
|
|
+ User inviter = userRepository.findById(user.getInvitedBy()).orElse(null);
|
|
|
+ if (inviter != null && "practitioner".equals(inviter.getVipType())) {
|
|
|
+ request.setAssignedPractitionerId(inviter.getId());
|
|
|
+ assigned = true;
|
|
|
+ message = "已自动分配给您的专属能量师";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ PlanRequest saved = planRequestRepository.save(request);
|
|
|
+
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
+ result.put("planRequest", saved);
|
|
|
+ result.put("assigned", assigned);
|
|
|
+ result.put("message", message);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get requests assigned to a practitioner.
|
|
|
+ */
|
|
|
+ public List<PlanRequest> getIncomingRequests(Long practitionerId) {
|
|
|
+ return planRequestRepository.findByAssignedPractitionerIdOrderByCreatedAtDesc(practitionerId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get requests made by a user.
|
|
|
+ */
|
|
|
+ public List<PlanRequest> getMyRequests(Long userId) {
|
|
|
+ return planRequestRepository.findByUserIdOrderByCreatedAtDesc(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Accept a request (practitioner accepts).
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public PlanRequest acceptRequest(Long requestId, Long practitionerId) {
|
|
|
+ PlanRequest request = planRequestRepository.findById(requestId)
|
|
|
+ .orElseThrow(() -> new BizException(1003, "Request not found"));
|
|
|
+ if (!practitionerId.equals(request.getAssignedPractitionerId())) {
|
|
|
+ throw new BizException(1004, "Not authorized");
|
|
|
+ }
|
|
|
+ request.setStatus("accepted");
|
|
|
+ return planRequestRepository.save(request);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Reject a request (practitioner rejects).
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public PlanRequest rejectRequest(Long requestId, Long practitionerId) {
|
|
|
+ PlanRequest request = planRequestRepository.findById(requestId)
|
|
|
+ .orElseThrow(() -> new BizException(1003, "Request not found"));
|
|
|
+ if (!practitionerId.equals(request.getAssignedPractitionerId())) {
|
|
|
+ throw new BizException(1004, "Not authorized");
|
|
|
+ }
|
|
|
+ request.setStatus("rejected");
|
|
|
+ return planRequestRepository.save(request);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Ensure the user is an active practitioner.
|
|
|
+ * @throws BizException(1005, "仅能量师可执行此操作") if not a valid practitioner
|
|
|
+ */
|
|
|
+ public void ensurePractitioner(Long userId) {
|
|
|
+ User user = userRepository.findById(userId)
|
|
|
+ .orElseThrow(() -> new BizException(1003, "User not found"));
|
|
|
+ if (!"practitioner".equals(user.getVipType()) || user.getVipEndTime() == null || user.getVipEndTime().isBefore(LocalDateTime.now())) {
|
|
|
+ throw new BizException(1005, "仅能量师可执行此操作");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Update request price (after negotiation).
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public PlanRequest updatePrice(Long requestId, Integer newPrice) {
|
|
|
+ PlanRequest request = planRequestRepository.findById(requestId)
|
|
|
+ .orElseThrow(() -> new BizException(1003, "Request not found"));
|
|
|
+ request.setPrice(newPrice);
|
|
|
+ return planRequestRepository.save(request);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Cancel a request.
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public void cancelRequest(Long requestId, Long userId) {
|
|
|
+ PlanRequest request = planRequestRepository.findById(requestId)
|
|
|
+ .orElseThrow(() -> new BizException(1003, "Request not found"));
|
|
|
+ if (!userId.equals(request.getUserId())) {
|
|
|
+ throw new BizException(1004, "Not authorized");
|
|
|
+ }
|
|
|
+ request.setStatus("cancelled");
|
|
|
+ planRequestRepository.save(request);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * US-9.4: Submit a proposal (practitioner submits price proposal).
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public PlanRequest submitProposal(Long requestId, Long practitionerId, Integer price, String message) {
|
|
|
+ PlanRequest request = planRequestRepository.findById(requestId)
|
|
|
+ .orElseThrow(() -> new BizException(1003, "Request not found"));
|
|
|
+ if (!practitionerId.equals(request.getAssignedPractitionerId())) {
|
|
|
+ throw new BizException(1004, "Not authorized");
|
|
|
+ }
|
|
|
+ if (!"accepted".equals(request.getStatus())) {
|
|
|
+ throw new BizException(1003, "Request not in correct state for proposal");
|
|
|
+ }
|
|
|
+ request.setStatus("negotiating");
|
|
|
+ request.setPrice(price);
|
|
|
+ request.setNegotiationCount(0);
|
|
|
+ PlanRequest saved = planRequestRepository.save(request);
|
|
|
+
|
|
|
+ PlanRequestLog log = new PlanRequestLog();
|
|
|
+ log.setPlanRequestId(requestId);
|
|
|
+ log.setAction("proposal");
|
|
|
+ log.setOldPrice(null);
|
|
|
+ log.setNewPrice(price);
|
|
|
+ log.setMessage(message);
|
|
|
+ log.setOperatorId(practitionerId);
|
|
|
+ planRequestLogRepository.save(log);
|
|
|
+
|
|
|
+ return saved;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * US-9.4: Counter offer (user counters the practitioner's proposal).
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public PlanRequest counterOffer(Long requestId, Long userId, Integer newPrice, String message) {
|
|
|
+ PlanRequest request = planRequestRepository.findById(requestId)
|
|
|
+ .orElseThrow(() -> new BizException(1003, "Request not found"));
|
|
|
+ if (!userId.equals(request.getUserId())) {
|
|
|
+ throw new BizException(1004, "Not authorized");
|
|
|
+ }
|
|
|
+ if (!"negotiating".equals(request.getStatus())) {
|
|
|
+ throw new BizException(1003, "Request not in negotiating state");
|
|
|
+ }
|
|
|
+ if (request.getNegotiationCount() >= 5) {
|
|
|
+ throw new BizException(1003, "已达到最大协商次数(5次)");
|
|
|
+ }
|
|
|
+ Integer oldPrice = request.getPrice();
|
|
|
+ request.setNegotiationCount(request.getNegotiationCount() + 1);
|
|
|
+ request.setPrice(newPrice);
|
|
|
+ PlanRequest saved = planRequestRepository.save(request);
|
|
|
+
|
|
|
+ PlanRequestLog log = new PlanRequestLog();
|
|
|
+ log.setPlanRequestId(requestId);
|
|
|
+ log.setAction("counter");
|
|
|
+ log.setOldPrice(oldPrice);
|
|
|
+ log.setNewPrice(newPrice);
|
|
|
+ log.setMessage(message);
|
|
|
+ log.setOperatorId(userId);
|
|
|
+ planRequestLogRepository.save(log);
|
|
|
+
|
|
|
+ return saved;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * US-9.4/9.5: Accept proposal (user accepts the practitioner's proposal).
|
|
|
+ * Creates an Order and sets status to pending_payment (not paid — payment is pending).
|
|
|
+ * @return Map containing planRequest and order
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public Map<String, Object> acceptProposal(Long requestId, Long userId) {
|
|
|
+ PlanRequest request = planRequestRepository.findById(requestId)
|
|
|
+ .orElseThrow(() -> new BizException(1003, "Request not found"));
|
|
|
+ if (!userId.equals(request.getUserId())) {
|
|
|
+ throw new BizException(1004, "Not authorized");
|
|
|
+ }
|
|
|
+ if (!"negotiating".equals(request.getStatus())) {
|
|
|
+ throw new BizException(1003, "Request not in negotiating state");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Create order for this plan request (status will be "pending" from OrderService)
|
|
|
+ Order order = orderService.createOrder(userId, request.getPrice(), "wechat", "practitioner_plan", false);
|
|
|
+
|
|
|
+ // Update plan request
|
|
|
+ request.setOrderId(order.getId());
|
|
|
+ request.setStatus("pending_payment");
|
|
|
+ PlanRequest saved = planRequestRepository.save(request);
|
|
|
+
|
|
|
+ PlanRequestLog log = new PlanRequestLog();
|
|
|
+ log.setPlanRequestId(requestId);
|
|
|
+ log.setAction("accept");
|
|
|
+ log.setOldPrice(request.getPrice());
|
|
|
+ log.setNewPrice(request.getPrice());
|
|
|
+ log.setMessage("用户已接受方案,待支付");
|
|
|
+ log.setOperatorId(userId);
|
|
|
+ planRequestLogRepository.save(log);
|
|
|
+
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
+ result.put("planRequest", saved);
|
|
|
+ result.put("order", order);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * US-9.4: Reject proposal (user rejects the practitioner's proposal).
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public PlanRequest rejectProposal(Long requestId, Long userId) {
|
|
|
+ PlanRequest request = planRequestRepository.findById(requestId)
|
|
|
+ .orElseThrow(() -> new BizException(1003, "Request not found"));
|
|
|
+ if (!userId.equals(request.getUserId())) {
|
|
|
+ throw new BizException(1004, "Not authorized");
|
|
|
+ }
|
|
|
+ if (!"negotiating".equals(request.getStatus())) {
|
|
|
+ throw new BizException(1003, "Request not in negotiating state");
|
|
|
+ }
|
|
|
+ request.setStatus("cancelled");
|
|
|
+ PlanRequest saved = planRequestRepository.save(request);
|
|
|
+
|
|
|
+ PlanRequestLog log = new PlanRequestLog();
|
|
|
+ log.setPlanRequestId(requestId);
|
|
|
+ log.setAction("reject");
|
|
|
+ log.setOldPrice(request.getPrice());
|
|
|
+ log.setNewPrice(null);
|
|
|
+ log.setMessage("用户已拒绝方案");
|
|
|
+ log.setOperatorId(userId);
|
|
|
+ planRequestLogRepository.save(log);
|
|
|
+
|
|
|
+ return saved;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * US-9.4: Get negotiation logs for a request.
|
|
|
+ */
|
|
|
+ public List<PlanRequestLog> getNegotiationLogs(Long requestId) {
|
|
|
+ return planRequestLogRepository.findByPlanRequestIdOrderByCreatedAtAsc(requestId);
|
|
|
+ }
|
|
|
+}
|