From e18ae611fd9f964c6fc38d0d2a4a62f2c6b78af8 Mon Sep 17 00:00:00 2001 From: tianyuan <2861334240@qq.com> Date: Fri, 26 Dec 2025 22:55:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=B4=E4=BF=AE=E4=BA=BA=E5=91=98=E8=A2=AB?= =?UTF-8?q?=E6=B4=BE=E5=8D=95=E9=80=9A=E7=9F=A5=E6=8E=A5=E5=8F=A3=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RepairmanNotificationController.java | 66 +++++++++++++++++++ .../com/campus/water/entity/Notification.java | 51 ++++++++++++++ .../water/mapper/NotificationRepository.java | 22 +++++++ .../water/service/NotificationService.java | 37 +++++++++++ .../service/NotificationServiceImpl.java | 59 +++++++++++++++++ .../water/service/WorkOrderServiceImpl.java | 14 +++- 6 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/campus/water/controller/RepairmanNotificationController.java create mode 100644 src/main/java/com/campus/water/entity/Notification.java create mode 100644 src/main/java/com/campus/water/mapper/NotificationRepository.java create mode 100644 src/main/java/com/campus/water/service/NotificationService.java create mode 100644 src/main/java/com/campus/water/service/NotificationServiceImpl.java diff --git a/src/main/java/com/campus/water/controller/RepairmanNotificationController.java b/src/main/java/com/campus/water/controller/RepairmanNotificationController.java new file mode 100644 index 0000000..8d45fb3 --- /dev/null +++ b/src/main/java/com/campus/water/controller/RepairmanNotificationController.java @@ -0,0 +1,66 @@ +package com.campus.water.controller; + +import com.campus.water.entity.Notification; +import com.campus.water.service.NotificationService; +import com.campus.water.util.ResultVO; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * 维修人员通知专属控制器 + * 负责APP端通知相关接口,与原有维修人员控制器解耦 + */ +@RestController +@RequestMapping("/api/app/repairman/notification") +@RequiredArgsConstructor +@Tag(name = "维修人员通知接口", description = "维修人员APP端通知查询/已读标记接口") +public class RepairmanNotificationController { + + private final NotificationService notificationService; + + /** + * 获取维修人员未读通知 + */ + @GetMapping("/unread") + @Operation(summary = "获取未读通知", description = "查询维修人员所有未读的派单/系统通知") + public ResultVO> getUnreadNotifications(@RequestParam String repairmanId) { + try { + List unreadNotifications = notificationService.getUnreadNotifications(repairmanId); + return ResultVO.success(unreadNotifications, "获取未读通知成功"); + } catch (Exception e) { + return ResultVO.error(500, "获取未读通知失败:" + e.getMessage()); + } + } + + /** + * 获取维修人员所有通知 + */ + @GetMapping("/all") + @Operation(summary = "获取所有通知", description = "查询维修人员所有通知(已读+未读)") + public ResultVO> getAllNotifications(@RequestParam String repairmanId) { + try { + List allNotifications = notificationService.getAllNotifications(repairmanId); + return ResultVO.success(allNotifications, "获取所有通知成功"); + } catch (Exception e) { + return ResultVO.error(500, "获取所有通知失败:" + e.getMessage()); + } + } + + /** + * 标记通知为已读 + */ + @PostMapping("/read") + @Operation(summary = "标记通知为已读", description = "将指定通知标记为已读状态") + public ResultVO markNotificationAsRead(@RequestParam Long notificationId) { + try { + notificationService.markAsRead(notificationId); + return ResultVO.success(true, "标记通知为已读成功"); + } catch (Exception e) { + return ResultVO.error(500, "标记通知为已读失败:" + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/campus/water/entity/Notification.java b/src/main/java/com/campus/water/entity/Notification.java new file mode 100644 index 0000000..7c592ab --- /dev/null +++ b/src/main/java/com/campus/water/entity/Notification.java @@ -0,0 +1,51 @@ +package com.campus.water.entity; + +import lombok.Data; +import jakarta.persistence.*; +import java.time.LocalDateTime; + +/** + * 维修人员通知实体 + * 存储派单、系统通知等消息 + */ +@Data +@Entity +@Table(name = "notification") +public class Notification { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + /** 维修人员ID */ + @Column(name = "repairman_id", nullable = false, length = 50) + private String repairmanId; + + /** 关联工单ID */ + @Column(name = "order_id", length = 50) + private String orderId; + + /** 通知内容 */ + @Column(name = "content", nullable = false, length = 500) + private String content; + + /** 是否已读(默认未读) */ + @Column(name = "is_read") + private boolean isRead = false; + + /** 创建时间 */ + @Column(name = "created_time", nullable = false) + private LocalDateTime createdTime = LocalDateTime.now(); + + /** 通知类型 */ + @Enumerated(EnumType.STRING) + @Column(name = "type", nullable = false, length = 20) + private NotificationType type; + + /** 通知类型枚举 */ + public enum NotificationType { + ORDER_ASSIGNED, // 派单通知 + ORDER_GRABBED, // 抢单通知 + ORDER_REJECTED, // 拒单通知 + SYSTEM // 系统通知 + } +} \ No newline at end of file diff --git a/src/main/java/com/campus/water/mapper/NotificationRepository.java b/src/main/java/com/campus/water/mapper/NotificationRepository.java new file mode 100644 index 0000000..e8bf9d1 --- /dev/null +++ b/src/main/java/com/campus/water/mapper/NotificationRepository.java @@ -0,0 +1,22 @@ +package com.campus.water.mapper; + +import com.campus.water.entity.Notification; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import java.util.List; + +/** + * 通知数据访问层 + */ +@Repository +public interface NotificationRepository extends JpaRepository { + /** + * 查询维修人员未读通知(按创建时间倒序) + */ + List findByRepairmanIdAndIsReadFalseOrderByCreatedTimeDesc(String repairmanId); + + /** + * 查询维修人员所有通知(按创建时间倒序) + */ + List findByRepairmanIdOrderByCreatedTimeDesc(String repairmanId); +} \ No newline at end of file diff --git a/src/main/java/com/campus/water/service/NotificationService.java b/src/main/java/com/campus/water/service/NotificationService.java new file mode 100644 index 0000000..6fddd6c --- /dev/null +++ b/src/main/java/com/campus/water/service/NotificationService.java @@ -0,0 +1,37 @@ +package com.campus.water.service; + +import com.campus.water.entity.Notification; +import java.util.List; + +/** + * 维修人员通知服务接口 + */ +public interface NotificationService { + /** + * 发送派单通知 + * @param repairmanId 维修人员ID + * @param orderId 工单ID + * @param content 通知内容 + */ + void sendOrderAssignedNotification(String repairmanId, String orderId, String content); + + /** + * 获取维修人员未读通知 + * @param repairmanId 维修人员ID + * @return 未读通知列表 + */ + List getUnreadNotifications(String repairmanId); + + /** + * 获取维修人员所有通知 + * @param repairmanId 维修人员ID + * @return 所有通知列表 + */ + List getAllNotifications(String repairmanId); + + /** + * 标记通知为已读 + * @param notificationId 通知ID + */ + void markAsRead(Long notificationId); +} \ No newline at end of file diff --git a/src/main/java/com/campus/water/service/NotificationServiceImpl.java b/src/main/java/com/campus/water/service/NotificationServiceImpl.java new file mode 100644 index 0000000..9af8e5c --- /dev/null +++ b/src/main/java/com/campus/water/service/NotificationServiceImpl.java @@ -0,0 +1,59 @@ +package com.campus.water.service.impl; + +import com.campus.water.entity.Notification; +import com.campus.water.mapper.NotificationRepository; +import com.campus.water.service.NotificationService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 通知服务实现类 + */ +@Service +@RequiredArgsConstructor +public class NotificationServiceImpl implements NotificationService { + + private final NotificationRepository notificationRepository; + + /** + * 发送派单通知 + */ + @Override + public void sendOrderAssignedNotification(String repairmanId, String orderId, String content) { + Notification notification = new Notification(); + notification.setRepairmanId(repairmanId); + notification.setOrderId(orderId); + notification.setContent(content); + notification.setType(Notification.NotificationType.ORDER_ASSIGNED); + notificationRepository.save(notification); + } + + /** + * 获取未读通知 + */ + @Override + public List getUnreadNotifications(String repairmanId) { + return notificationRepository.findByRepairmanIdAndIsReadFalseOrderByCreatedTimeDesc(repairmanId); + } + + /** + * 获取所有通知 + */ + @Override + public List getAllNotifications(String repairmanId) { + return notificationRepository.findByRepairmanIdOrderByCreatedTimeDesc(repairmanId); + } + + /** + * 标记通知为已读 + */ + @Override + public void markAsRead(Long notificationId) { + notificationRepository.findById(notificationId).ifPresent(notification -> { + notification.setRead(true); + notificationRepository.save(notification); + }); + } +} \ No newline at end of file diff --git a/src/main/java/com/campus/water/service/WorkOrderServiceImpl.java b/src/main/java/com/campus/water/service/WorkOrderServiceImpl.java index b9b1fa4..b5382ea 100644 --- a/src/main/java/com/campus/water/service/WorkOrderServiceImpl.java +++ b/src/main/java/com/campus/water/service/WorkOrderServiceImpl.java @@ -9,7 +9,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - +import com.campus.water.service.NotificationService; +import com.campus.water.service.WorkOrderService; import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; @@ -28,6 +29,7 @@ public class WorkOrderServiceImpl implements WorkOrderService { private final WorkOrderRepository workOrderRepository; private final RepairmanRepository repairmanRepository; + private final NotificationService notificationService; @Override public WorkOrder getOrderDetail(String orderId) { @@ -288,6 +290,16 @@ public class WorkOrderServiceImpl implements WorkOrderService { order.setGrabbedTime(LocalDateTime.now()); workOrderRepository.save(order); + // ===== 新增:派单通知 ===== + try { + String notificationContent = String.format("您有新的维修工单待处理!工单ID:%s", orderId); + notificationService.sendOrderAssignedNotification(repairmanId, orderId, notificationContent); + } catch (Exception e) { + // 捕获异常,不影响原有派单逻辑 + System.err.println("派单通知发送失败:" + e.getMessage()); + } + // ============================================== + // 更新维修人员状态 Repairman repairman = repairmanOpt.get(); repairman.setStatus(Repairman.RepairmanStatus.busy); -- 2.34.1