use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::Uom;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::CatalogStatus;
use crate::domain::entity::UomType;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateUomDto {
#[cfg_attr(feature = "validation", validate(length(max = 20)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub code: String,
#[cfg_attr(feature = "validation", validate(length(max = 80)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[serde(alias = "uom_type")]
pub uom_type: UomType,
#[cfg_attr(feature = "validation", validate(range(min = 0), range(max = 6)))]
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "decimal_places")]
pub decimal_places: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "relative_uom_id")]
pub relative_uom_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "relative_factor")]
pub relative_factor: Option<Decimal>,
pub factor: Decimal,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_protected")]
pub is_protected: bool,
pub status: CatalogStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateUomDto {
#[cfg_attr(feature = "validation", validate(length(max = 20)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub code: String,
#[cfg_attr(feature = "validation", validate(length(max = 80)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[serde(alias = "uom_type")]
pub uom_type: UomType,
#[cfg_attr(feature = "validation", validate(range(min = 0), range(max = 6)))]
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "decimal_places")]
pub decimal_places: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "relative_uom_id")]
pub relative_uom_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "relative_factor")]
pub relative_factor: Option<Decimal>,
pub factor: Decimal,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_protected")]
pub is_protected: bool,
pub status: CatalogStatus,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchUomDto {
#[cfg_attr(feature = "validation", validate(length(max = 20)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub code: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 80)))]
#[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", alias = "uom_type")]
pub uom_type: Option<UomType>,
#[cfg_attr(feature = "validation", validate(range(min = 0), range(max = 6)))]
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "decimal_places")]
pub decimal_places: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none", alias = "relative_uom_id")]
pub relative_uom_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "relative_factor")]
pub relative_factor: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub factor: Option<Decimal>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "is_protected")]
pub is_protected: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<CatalogStatus>,
}
impl PatchUomDto {
pub fn has_changes(&self) -> bool {
self.code.is_some() || self.name.is_some() || self.uom_type.is_some() || self.decimal_places.is_some() || self.relative_uom_id.is_some() || self.relative_factor.is_some() || self.factor.is_some() || self.is_protected.is_some() || self.status.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UomResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub code: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
pub uom_type: UomType,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub decimal_places: i32,
pub relative_uom_id: Option<Uuid>,
pub relative_factor: Option<Decimal>,
pub factor: Decimal,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub is_protected: bool,
pub status: CatalogStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UomListResponseDto {
pub items: Vec<UomResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl UomListResponseDto {
pub fn new(items: Vec<UomResponseDto>, 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 UomSummaryDto {
pub id: Uuid,
pub code: String,
pub name: String,
pub uom_type: UomType,
pub created_at: Option<DateTime<Utc>>,
}
impl From<Uom> for UomResponseDto {
fn from(entity: Uom) -> Self {
Self {
id: entity.id,
code: entity.code,
name: entity.name,
uom_type: entity.uom_type,
decimal_places: entity.decimal_places,
relative_uom_id: entity.relative_uom_id,
relative_factor: entity.relative_factor,
factor: entity.factor,
is_protected: entity.is_protected,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<Uom> for UomSummaryDto {
fn from(entity: Uom) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
code: entity.code,
name: entity.name,
uom_type: entity.uom_type,
created_at,
}
}
}
impl From<CreateUomDto> for Uom {
fn from(dto: CreateUomDto) -> Self {
Self {
id: Uuid::new_v4(),
code: dto.code,
name: dto.name,
uom_type: dto.uom_type,
decimal_places: dto.decimal_places,
relative_uom_id: dto.relative_uom_id,
relative_factor: dto.relative_factor,
factor: dto.factor,
is_protected: dto.is_protected,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl From<&Uom> for UomResponseDto {
fn from(entity: &Uom) -> Self {
Self {
id: entity.id.clone(),
code: entity.code.clone(),
name: entity.name.clone(),
uom_type: entity.uom_type.clone(),
decimal_places: entity.decimal_places.clone(),
relative_uom_id: entity.relative_uom_id.clone(),
relative_factor: entity.relative_factor.clone(),
factor: entity.factor.clone(),
is_protected: entity.is_protected.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateUomDto> for Uom {
fn from_create_dto(dto: CreateUomDto) -> backbone_core::ServiceResult<Self> {
Ok(Uom::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateUomDto> for Uom {
fn apply_update(mut self, dto: UpdateUomDto) -> backbone_core::ServiceResult<Self> {
self.code = dto.code;
self.name = dto.name;
self.uom_type = dto.uom_type;
self.decimal_places = dto.decimal_places;
self.relative_uom_id = dto.relative_uom_id;
self.relative_factor = dto.relative_factor;
self.factor = dto.factor;
self.is_protected = dto.is_protected;
self.status = dto.status;
Ok(self)
}
}