use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::domain::entity::RouteRule;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::ProcureMethod;
use crate::domain::entity::RuleAction;
use crate::domain::entity::RuleAuto;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct CreateRouteRuleDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub sequence: i32,
pub action: RuleAction,
pub auto: RuleAuto,
#[serde(alias = "procure_method")]
pub procure_method: ProcureMethod,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub delay: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "location_src_id")]
pub location_src_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "location_dest_id")]
pub location_dest_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "picking_type_id")]
pub picking_type_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "route_id")]
pub route_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "warehouse_id")]
pub warehouse_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "propagate_cancel")]
pub propagate_cancel: bool,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UpdateRouteRuleDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub sequence: i32,
pub action: RuleAction,
pub auto: RuleAuto,
#[serde(alias = "procure_method")]
pub procure_method: ProcureMethod,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub delay: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "location_src_id")]
pub location_src_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "location_dest_id")]
pub location_dest_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "picking_type_id")]
pub picking_type_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "route_id")]
pub route_id: Uuid,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "warehouse_id")]
pub warehouse_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "propagate_cancel")]
pub propagate_cancel: bool,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PatchRouteRuleDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none")]
pub sequence: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub action: Option<RuleAction>,
#[serde(skip_serializing_if = "Option::is_none")]
pub auto: Option<RuleAuto>,
#[serde(skip_serializing_if = "Option::is_none", alias = "procure_method")]
pub procure_method: Option<ProcureMethod>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none")]
pub delay: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none", alias = "location_src_id")]
pub location_src_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "location_dest_id")]
pub location_dest_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "picking_type_id")]
pub picking_type_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "route_id")]
pub route_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "warehouse_id")]
pub warehouse_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "propagate_cancel")]
pub propagate_cancel: Option<bool>,
}
impl PatchRouteRuleDto {
pub fn has_changes(&self) -> bool {
self.name.is_some() || self.active.is_some() || self.sequence.is_some() || self.action.is_some() || self.auto.is_some() || self.procure_method.is_some() || self.delay.is_some() || self.location_src_id.is_some() || self.location_dest_id.is_some() || self.picking_type_id.is_some() || self.route_id.is_some() || self.warehouse_id.is_some() || self.propagate_cancel.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct RouteRuleResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub sequence: i32,
pub action: RuleAction,
pub auto: RuleAuto,
pub procure_method: ProcureMethod,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub delay: i32,
pub location_src_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub location_dest_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub picking_type_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub route_id: Uuid,
pub warehouse_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub propagate_cancel: bool,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct RouteRuleListResponseDto {
pub items: Vec<RouteRuleResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl RouteRuleListResponseDto {
pub fn new(items: Vec<RouteRuleResponseDto>, 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 RouteRuleSummaryDto {
pub id: Uuid,
pub name: String,
pub active: bool,
pub sequence: i32,
pub created_at: Option<DateTime<Utc>>,
}
impl From<RouteRule> for RouteRuleResponseDto {
fn from(entity: RouteRule) -> Self {
Self {
id: entity.id,
name: entity.name,
active: entity.active,
sequence: entity.sequence,
action: entity.action,
auto: entity.auto,
procure_method: entity.procure_method,
delay: entity.delay,
location_src_id: entity.location_src_id,
location_dest_id: entity.location_dest_id,
picking_type_id: entity.picking_type_id,
route_id: entity.route_id,
warehouse_id: entity.warehouse_id,
propagate_cancel: entity.propagate_cancel,
metadata: entity.metadata,
}
}
}
impl From<RouteRule> for RouteRuleSummaryDto {
fn from(entity: RouteRule) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
name: entity.name,
active: entity.active,
sequence: entity.sequence,
created_at,
}
}
}
impl From<CreateRouteRuleDto> for RouteRule {
fn from(dto: CreateRouteRuleDto) -> Self {
Self {
id: Uuid::new_v4(),
name: dto.name,
active: dto.active,
sequence: dto.sequence,
action: dto.action,
auto: dto.auto,
procure_method: dto.procure_method,
delay: dto.delay,
location_src_id: dto.location_src_id,
location_dest_id: dto.location_dest_id,
picking_type_id: dto.picking_type_id,
route_id: dto.route_id,
warehouse_id: dto.warehouse_id,
propagate_cancel: dto.propagate_cancel,
metadata: AuditMetadata::default(),
}
}
}
impl From<&RouteRule> for RouteRuleResponseDto {
fn from(entity: &RouteRule) -> Self {
Self {
id: entity.id.clone(),
name: entity.name.clone(),
active: entity.active.clone(),
sequence: entity.sequence.clone(),
action: entity.action.clone(),
auto: entity.auto.clone(),
procure_method: entity.procure_method.clone(),
delay: entity.delay.clone(),
location_src_id: entity.location_src_id.clone(),
location_dest_id: entity.location_dest_id.clone(),
picking_type_id: entity.picking_type_id.clone(),
route_id: entity.route_id.clone(),
warehouse_id: entity.warehouse_id.clone(),
propagate_cancel: entity.propagate_cancel.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateRouteRuleDto> for RouteRule {
fn from_create_dto(dto: CreateRouteRuleDto) -> backbone_core::ServiceResult<Self> {
Ok(RouteRule::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateRouteRuleDto> for RouteRule {
fn apply_update(mut self, dto: UpdateRouteRuleDto) -> backbone_core::ServiceResult<Self> {
self.name = dto.name;
self.active = dto.active;
self.sequence = dto.sequence;
self.action = dto.action;
self.auto = dto.auto;
self.procure_method = dto.procure_method;
self.delay = dto.delay;
self.location_src_id = dto.location_src_id;
self.location_dest_id = dto.location_dest_id;
self.picking_type_id = dto.picking_type_id;
self.route_id = dto.route_id;
self.warehouse_id = dto.warehouse_id;
self.propagate_cancel = dto.propagate_cancel;
Ok(self)
}
}