use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::domain::entity::Lot;
use crate::domain::entity::AuditMetadata;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateLotDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reference: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "item_id")]
pub item_id: Uuid,
#[serde(alias = "product_qty")]
pub product_qty: Decimal,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "location_id")]
pub location_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateLotDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reference: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "item_id")]
pub item_id: Uuid,
#[serde(alias = "product_qty")]
pub product_qty: Decimal,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "location_id")]
pub location_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchLotDto {
#[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 reference: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "item_id")]
pub item_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "product_qty")]
pub product_qty: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none", alias = "location_id")]
pub location_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
}
impl PatchLotDto {
pub fn has_changes(&self) -> bool {
self.name.is_some() || self.reference.is_some() || self.item_id.is_some() || self.product_qty.is_some() || self.location_id.is_some() || self.note.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LotResponseDto {
#[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 reference: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub item_id: Uuid,
pub product_qty: Decimal,
pub location_id: Option<Uuid>,
pub note: Option<String>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct LotListResponseDto {
pub items: Vec<LotResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl LotListResponseDto {
pub fn new(items: Vec<LotResponseDto>, 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 LotSummaryDto {
pub id: Uuid,
pub name: String,
pub reference: Option<String>,
pub item_id: Uuid,
pub created_at: Option<DateTime<Utc>>,
}
impl From<Lot> for LotResponseDto {
fn from(entity: Lot) -> Self {
Self {
id: entity.id,
name: entity.name,
reference: entity.reference,
item_id: entity.item_id,
product_qty: entity.product_qty,
location_id: entity.location_id,
note: entity.note,
metadata: entity.metadata,
}
}
}
impl From<Lot> for LotSummaryDto {
fn from(entity: Lot) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
name: entity.name,
reference: entity.reference,
item_id: entity.item_id,
created_at,
}
}
}
impl From<CreateLotDto> for Lot {
fn from(dto: CreateLotDto) -> Self {
Self {
id: Uuid::new_v4(),
name: dto.name,
reference: dto.reference,
item_id: dto.item_id,
product_qty: dto.product_qty,
location_id: dto.location_id,
note: dto.note,
metadata: AuditMetadata::default(),
}
}
}
impl From<&Lot> for LotResponseDto {
fn from(entity: &Lot) -> Self {
Self {
id: entity.id.clone(),
name: entity.name.clone(),
reference: entity.reference.clone(),
item_id: entity.item_id.clone(),
product_qty: entity.product_qty.clone(),
location_id: entity.location_id.clone(),
note: entity.note.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateLotDto> for Lot {
fn from_create_dto(dto: CreateLotDto) -> backbone_core::ServiceResult<Self> {
Ok(Lot::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateLotDto> for Lot {
fn apply_update(mut self, dto: UpdateLotDto) -> backbone_core::ServiceResult<Self> {
self.name = dto.name;
self.reference = dto.reference;
self.item_id = dto.item_id;
self.product_qty = dto.product_qty;
self.location_id = dto.location_id;
self.note = dto.note;
Ok(self)
}
}