Tôi sẽ giúp bạn phát triển chức năng gửi thông tin tiền dịch vụ của phòng cho đại diện phòng. Dựa vào code hiện có, chúng ta cần thực hiện các bước sau:
Đầu tiên, cần thêm trường is_representative vào model Users:
[Redacted
// Trong file Users.java
@Entity
@Table(name = "Users")
public class Users {
// Thêm trường mới
private Boolean isRepresentative;
// Thêm getter/setter
public Boolean getIsRepresentative() {
return isRepresentative;
}
public void setIsRepresentative(Boolean isRepresentative) {
this.isRepresentative = isRepresentative;
}
}
Tạo một DTO mới để chứa thông tin gửi dịch vụ:
[Redacted
// ServiceNotificationDTO.java
public class ServiceNotificationDTO {
private Integer roomId;
private String representativeEmail;
private List<RoomServices> services;
private Double totalAmount;
// Getters and setters
}
Thêm API endpoint mới trong RoomServicesController:
[Redacted
@RestController
@RequestMapping("/room-services")
public class RoomServicesController {
@Autowired
private UsersRepository usersRepository;
@GetMapping("/room/{roomId}/representative")
public ResponseEntity<ServiceNotificationDTO> getRoomServicesForRepresentative(@PathVariable int roomId) {
// Lấy thông tin phòng và dịch vụ
List<RoomServices> services = _roomServicesService.getRoomServicesByRoomId(roomId);
// Tìm đại diện phòng
Users representative = usersRepository.findByRoomIdAndIsRepresentative(roomId, true);
if (representative == null) {
return ResponseEntity.notFound().build();
}
ServiceNotificationDTO dto = new ServiceNotificationDTO();
dto.setRoomId(roomId);
dto.setRepresentativeEmail(representative.getEmail());
dto.setServices(services);
dto.setTotalAmount(calculateTotalAmount(services));
return ResponseEntity.ok(dto);
}
@PostMapping("/notify-representative")
public ResponseEntity<?> notifyRepresentative(@RequestBody ServiceNotificationDTO dto) {
// Implement email sending logic here
// You can use JavaMailSender or any email service
return ResponseEntity.ok().build();
}
private Double calculateTotalAmount(List<RoomServices> services) {
return services.stream()
.mapToDouble(service -> service.getCost() * service.getUnit())
.sum();
}
}