use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc, NaiveDate};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::MaintenanceSchedule;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::MaintenanceScheduleStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateMaintenanceScheduleDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "asset_id")]
pub asset_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "interval_days")]
pub interval_days: i32,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "next_due_date")]
pub next_due_date: NaiveDate,
pub status: MaintenanceScheduleStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateMaintenanceScheduleDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "asset_id")]
pub asset_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "interval_days")]
pub interval_days: i32,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(alias = "next_due_date")]
pub next_due_date: NaiveDate,
pub status: MaintenanceScheduleStatus,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchMaintenanceScheduleDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "asset_id")]
pub asset_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "interval_days")]
pub interval_days: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "next_due_date")]
pub next_due_date: Option<NaiveDate>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<MaintenanceScheduleStatus>,
}
impl PatchMaintenanceScheduleDto {
pub fn has_changes(&self) -> bool {
self.asset_id.is_some() || self.name.is_some() || self.interval_days.is_some() || self.next_due_date.is_some() || self.status.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MaintenanceScheduleResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub asset_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub interval_days: i32,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01"))]
pub next_due_date: NaiveDate,
pub status: MaintenanceScheduleStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MaintenanceScheduleListResponseDto {
pub items: Vec<MaintenanceScheduleResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl MaintenanceScheduleListResponseDto {
pub fn new(items: Vec<MaintenanceScheduleResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
let total_pages = if per_page > 0 {
((total as f64) / (per_page as f64)).ceil() as u32
} else {
0
};
Self {
items,
total,
page,
per_page,
total_pages,
has_next: page < total_pages,
has_prev: page > 1,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MaintenanceScheduleSummaryDto {
pub id: Uuid,
pub asset_id: Uuid,
pub name: String,
pub interval_days: i32,
pub created_at: Option<DateTime<Utc>>,
}
impl From<MaintenanceSchedule> for MaintenanceScheduleResponseDto {
fn from(entity: MaintenanceSchedule) -> Self {
Self {
id: entity.id,
asset_id: entity.asset_id,
name: entity.name,
interval_days: entity.interval_days,
next_due_date: entity.next_due_date,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<MaintenanceSchedule> for MaintenanceScheduleSummaryDto {
fn from(entity: MaintenanceSchedule) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
asset_id: entity.asset_id,
name: entity.name,
interval_days: entity.interval_days,
created_at,
}
}
}
impl From<CreateMaintenanceScheduleDto> for MaintenanceSchedule {
fn from(dto: CreateMaintenanceScheduleDto) -> Self {
Self {
id: Uuid::new_v4(),
asset_id: dto.asset_id,
name: dto.name,
interval_days: dto.interval_days,
next_due_date: dto.next_due_date,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl From<&MaintenanceSchedule> for MaintenanceScheduleResponseDto {
fn from(entity: &MaintenanceSchedule) -> Self {
Self {
id: entity.id.clone(),
asset_id: entity.asset_id.clone(),
name: entity.name.clone(),
interval_days: entity.interval_days.clone(),
next_due_date: entity.next_due_date.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateMaintenanceScheduleDto> for MaintenanceSchedule {
fn from_create_dto(dto: CreateMaintenanceScheduleDto) -> backbone_core::ServiceResult<Self> {
Ok(MaintenanceSchedule::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateMaintenanceScheduleDto> for MaintenanceSchedule {
fn apply_update(mut self, dto: UpdateMaintenanceScheduleDto) -> backbone_core::ServiceResult<Self> {
self.asset_id = dto.asset_id;
self.name = dto.name;
self.interval_days = dto.interval_days;
self.next_due_date = dto.next_due_date;
self.status = dto.status;
Ok(self)
}
}