被派单通知接口 #108

Merged
p95fco63j merged 1 commits from junmao_branch into develop 2 weeks ago

@ -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<List<Notification>> getUnreadNotifications(@RequestParam String repairmanId) {
try {
List<Notification> unreadNotifications = notificationService.getUnreadNotifications(repairmanId);
return ResultVO.success(unreadNotifications, "获取未读通知成功");
} catch (Exception e) {
return ResultVO.error(500, "获取未读通知失败:" + e.getMessage());
}
}
/**
*
*/
@GetMapping("/all")
@Operation(summary = "获取所有通知", description = "查询维修人员所有通知(已读+未读)")
public ResultVO<List<Notification>> getAllNotifications(@RequestParam String repairmanId) {
try {
List<Notification> allNotifications = notificationService.getAllNotifications(repairmanId);
return ResultVO.success(allNotifications, "获取所有通知成功");
} catch (Exception e) {
return ResultVO.error(500, "获取所有通知失败:" + e.getMessage());
}
}
/**
*
*/
@PostMapping("/read")
@Operation(summary = "标记通知为已读", description = "将指定通知标记为已读状态")
public ResultVO<Boolean> markNotificationAsRead(@RequestParam Long notificationId) {
try {
notificationService.markAsRead(notificationId);
return ResultVO.success(true, "标记通知为已读成功");
} catch (Exception e) {
return ResultVO.error(500, "标记通知为已读失败:" + e.getMessage());
}
}
}

@ -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 // 系统通知
}
}

@ -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<Notification, Long> {
/**
*
*/
List<Notification> findByRepairmanIdAndIsReadFalseOrderByCreatedTimeDesc(String repairmanId);
/**
*
*/
List<Notification> findByRepairmanIdOrderByCreatedTimeDesc(String repairmanId);
}

@ -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<Notification> getUnreadNotifications(String repairmanId);
/**
*
* @param repairmanId ID
* @return
*/
List<Notification> getAllNotifications(String repairmanId);
/**
*
* @param notificationId ID
*/
void markAsRead(Long notificationId);
}

@ -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<Notification> getUnreadNotifications(String repairmanId) {
return notificationRepository.findByRepairmanIdAndIsReadFalseOrderByCreatedTimeDesc(repairmanId);
}
/**
*
*/
@Override
public List<Notification> getAllNotifications(String repairmanId) {
return notificationRepository.findByRepairmanIdOrderByCreatedTimeDesc(repairmanId);
}
/**
*
*/
@Override
public void markAsRead(Long notificationId) {
notificationRepository.findById(notificationId).ifPresent(notification -> {
notification.setRead(true);
notificationRepository.save(notification);
});
}
}

@ -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);

Loading…
Cancel
Save