use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct MeituanConvertRequest {
pub sid: String,
#[serde(rename = "actId")]
pub act_id: String,
#[serde(rename = "linkType")]
pub link_type: u32,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "miniCode")]
pub mini_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "miniCode2")]
pub mini_code2: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "miniCode3")]
pub mini_code3: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub platform: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer_id: Option<String>,
}
impl MeituanConvertRequest {
pub fn new(sid: impl Into<String>, act_id: impl Into<String>) -> Self {
Self {
sid: sid.into(),
act_id: act_id.into(),
link_type: 1, mini_code: None,
mini_code2: None,
mini_code3: None,
platform: None,
customer_id: None,
}
}
pub fn link_type(mut self, link_type: u32) -> Self {
self.link_type = link_type;
self
}
pub fn mini_code(mut self, mini_code: bool) -> Self {
self.mini_code = if mini_code {
Some("1".to_string())
} else {
None
};
self
}
pub fn mini_code2(mut self, mini_code2: bool) -> Self {
self.mini_code2 = if mini_code2 {
Some("1".to_string())
} else {
None
};
self
}
pub fn mini_code3(mut self, mini_code3: bool) -> Self {
self.mini_code3 = if mini_code3 {
Some("1".to_string())
} else {
None
};
self
}
pub fn platform(mut self, platform: impl Into<String>) -> Self {
self.platform = Some(platform.into());
self
}
pub fn customer_id(mut self, customer_id: impl Into<String>) -> Self {
self.customer_id = Some(customer_id.into());
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct MeituanOrderQueryRequest {
#[serde(rename = "type")]
pub query_type: u32,
pub page: u32,
pub page_size: u32,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "startTime")]
pub start_time: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "endTime")]
pub end_time: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub orderid: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sid: Option<String>,
}
impl MeituanOrderQueryRequest {
pub fn new(query_type: u32, page: u32, page_size: u32) -> Self {
Self {
query_type,
page,
page_size: page_size.min(50),
start_time: None,
end_time: None,
orderid: None,
sid: None,
}
}
pub fn start_time(mut self, start_time: impl Into<String>) -> Self {
self.start_time = Some(start_time.into());
self
}
pub fn end_time(mut self, end_time: impl Into<String>) -> Self {
self.end_time = Some(end_time.into());
self
}
pub fn time_range(mut self, start: impl Into<String>, end: impl Into<String>) -> Self {
self.start_time = Some(start.into());
self.end_time = Some(end.into());
self
}
pub fn orderid(mut self, orderid: impl Into<String>) -> Self {
self.orderid = Some(orderid.into());
self
}
pub fn sid(mut self, sid: impl Into<String>) -> Self {
self.sid = Some(sid.into());
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MeituanLinkType {
H5Long = 1,
H5Short = 2,
Deeplink = 3,
MiniProgram = 4,
Command = 5,
}
impl From<MeituanLinkType> for u32 {
fn from(link_type: MeituanLinkType) -> Self {
link_type as u32
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_meituan_convert_request_serialize() {
let request = MeituanConvertRequest::new("sid123", "7");
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["sid"], "sid123");
assert_eq!(json["actId"], "7");
assert_eq!(json["linkType"], 1);
assert!(json.get("miniCode").is_none());
assert!(json.get("customer_id").is_none());
}
#[test]
fn test_meituan_convert_request_with_options() {
let request = MeituanConvertRequest::new("sid123", "7")
.link_type(2)
.mini_code(true)
.customer_id("100000");
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["linkType"], 2);
assert_eq!(json["miniCode"], "1");
assert_eq!(json["customer_id"], "100000");
}
#[test]
fn test_meituan_order_query_request_serialize() {
let request = MeituanOrderQueryRequest::new(1, 1, 50);
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["type"], 1);
assert_eq!(json["page"], 1);
assert_eq!(json["page_size"], 50);
assert!(json.get("startTime").is_none());
}
#[test]
fn test_meituan_order_query_request_with_time_range() {
let request = MeituanOrderQueryRequest::new(1, 1, 50)
.time_range("2021-04-30 08:00:00", "2021-04-30 09:00:00")
.sid("sid123");
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["startTime"], "2021-04-30 08:00:00");
assert_eq!(json["endTime"], "2021-04-30 09:00:00");
assert_eq!(json["sid"], "sid123");
}
#[test]
fn test_meituan_order_query_page_size_limit() {
let request = MeituanOrderQueryRequest::new(1, 1, 100);
assert_eq!(request.page_size, 50); }
#[test]
fn test_optional_fields_not_serialized() {
let request = MeituanConvertRequest::new("sid", "7");
let json_str = serde_json::to_string(&request).unwrap();
assert!(!json_str.contains("miniCode"));
assert!(!json_str.contains("customer_id"));
assert!(!json_str.contains("platform"));
}
#[test]
fn test_meituan_link_type_conversion() {
assert_eq!(u32::from(MeituanLinkType::H5Long), 1);
assert_eq!(u32::from(MeituanLinkType::H5Short), 2);
assert_eq!(u32::from(MeituanLinkType::Deeplink), 3);
assert_eq!(u32::from(MeituanLinkType::MiniProgram), 4);
assert_eq!(u32::from(MeituanLinkType::Command), 5);
}
}