use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::domain::entity::PickingBatch;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::PickingBatchState;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreatePickingBatchDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_wave")]
pub is_wave: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "user_id")]
pub user_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "scheduled_date")]
pub scheduled_date: DateTime<Utc>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "had_members")]
pub had_members: bool,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdatePickingBatchDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
pub state: PickingBatchState,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_wave")]
pub is_wave: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "user_id")]
pub user_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "scheduled_date")]
pub scheduled_date: DateTime<Utc>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "had_members")]
pub had_members: bool,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchPickingBatchDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub state: Option<PickingBatchState>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "is_wave")]
pub is_wave: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", alias = "user_id")]
pub user_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "scheduled_date")]
pub scheduled_date: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "had_members")]
pub had_members: Option<bool>,
}
impl PatchPickingBatchDto {
pub fn has_changes(&self) -> bool {
self.name.is_some() || self.note.is_some() || self.state.is_some() || self.is_wave.is_some() || self.user_id.is_some() || self.scheduled_date.is_some() || self.had_members.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PickingBatchResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
pub note: Option<String>,
pub state: PickingBatchState,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub is_wave: bool,
pub user_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub scheduled_date: DateTime<Utc>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub had_members: bool,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PickingBatchListResponseDto {
pub items: Vec<PickingBatchResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl PickingBatchListResponseDto {
pub fn new(items: Vec<PickingBatchResponseDto>, 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 PickingBatchSummaryDto {
pub id: Uuid,
pub name: String,
pub note: Option<String>,
pub state: PickingBatchState,
pub created_at: Option<DateTime<Utc>>,
}
impl From<PickingBatch> for PickingBatchResponseDto {
fn from(entity: PickingBatch) -> Self {
Self {
id: entity.id,
name: entity.name,
note: entity.note,
state: entity.state,
is_wave: entity.is_wave,
user_id: entity.user_id,
scheduled_date: entity.scheduled_date,
had_members: entity.had_members,
metadata: entity.metadata,
}
}
}
impl From<PickingBatch> for PickingBatchSummaryDto {
fn from(entity: PickingBatch) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
name: entity.name,
note: entity.note,
state: entity.state,
created_at,
}
}
}
impl From<CreatePickingBatchDto> for PickingBatch {
fn from(dto: CreatePickingBatchDto) -> Self {
Self {
id: Uuid::new_v4(),
name: dto.name,
note: dto.note,
state: PickingBatchState::Draft,
is_wave: dto.is_wave,
user_id: dto.user_id,
scheduled_date: dto.scheduled_date,
had_members: dto.had_members,
metadata: AuditMetadata::default(),
}
}
}
impl From<&PickingBatch> for PickingBatchResponseDto {
fn from(entity: &PickingBatch) -> Self {
Self {
id: entity.id.clone(),
name: entity.name.clone(),
note: entity.note.clone(),
state: entity.state.clone(),
is_wave: entity.is_wave.clone(),
user_id: entity.user_id.clone(),
scheduled_date: entity.scheduled_date.clone(),
had_members: entity.had_members.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreatePickingBatchDto> for PickingBatch {
fn from_create_dto(dto: CreatePickingBatchDto) -> backbone_core::ServiceResult<Self> {
Ok(PickingBatch::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdatePickingBatchDto> for PickingBatch {
fn apply_update(mut self, dto: UpdatePickingBatchDto) -> backbone_core::ServiceResult<Self> {
self.name = dto.name;
self.note = dto.note;
self.state = dto.state;
self.is_wave = dto.is_wave;
self.user_id = dto.user_id;
self.scheduled_date = dto.scheduled_date;
self.had_members = dto.had_members;
Ok(self)
}
}