mall-portrait-common 0.1.1

Common utilities and types for mall portrait projects.
Documentation
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

/// 用户画像宽表(最终输出)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserProfile {
    /// 全局唯一ID
    pub global_id: String,
    /// 更新时间(Unix时间戳,秒)
    pub updated_at: i64,
    /// 用户手机号(脱敏或加密存储)
    pub mobile: Option<String>,
    /// 注册时间(Unix时间戳,秒)
    pub register_time: Option<i64>,
    /// 注册渠道(如app、web、线下等)
    pub register_channel: Option<String>,
    /// 会员等级(如普通、银卡、金卡、黑金等)
    pub member_level: Option<String>,
    /// 邮箱
    pub email: Option<String>,
    /// 生日(yyyy-MM-dd)
    pub birthday: Option<String>,
    /// 实名认证状态(0未实名,1已实名)
    pub is_verified: Option<bool>,
    /// 昵称
    pub nickname: Option<String>,
    /// 用户头像URL
    pub avatar_url: Option<String>,
    /// 邀请人ID
    pub inviter_id: Option<String>,
    /// 性别
    pub gender: Option<String>,
    /// 年龄
    pub age: Option<u32>,
    /// 预测最小年龄
    pub predicted_min_age: Option<u32>,
    /// 预测最大年龄
    pub predicted_max_age: Option<u32>,
    /// 城市
    pub city: Option<String>,
    /// 省份
    pub province: Option<String>,
    /// 累计浏览商品次数
    pub view_count: u32,
    /// 累计加购商品次数
    pub cart_count: u32,
    /// 累计收藏商品次数
    pub favorite_count: u32,
    /// 累计分享商品次数
    pub share_count: u32,
    /// 累计评论次数
    pub comment_count: u32,
    /// 累计点赞次数
    pub like_count: u32,
    /// 最近一次浏览时间(Unix时间戳,秒)
    pub last_view_time: Option<i64>,
    /// 最近一次加购时间(Unix时间戳,秒)
    pub last_cart_time: Option<i64>,
    /// 最近一次收藏时间(Unix时间戳,秒)
    pub last_favorite_time: Option<i64>,
    /// 最近一次分享时间(Unix时间戳,秒)
    pub last_share_time: Option<i64>,
    /// 最近一次评论时间(Unix时间戳,秒)
    pub last_comment_time: Option<i64>,
    /// 最近一次点赞时间(Unix时间戳,秒)
    pub last_like_time: Option<i64>,
    /// 近N天浏览商品次数(N=1/3/7/15/30/90/180/360/1080)
    pub view_count_1d: u32,
    pub view_count_3d: u32,
    pub view_count_7d: u32,
    pub view_count_15d: u32,
    pub view_count_30d: u32,
    pub view_count_90d: u32,
    pub view_count_180d: u32,
    pub view_count_360d: u32,
    pub view_count_1080d: u32,
    /// 近N天加购商品次数
    pub cart_count_1d: u32,
    pub cart_count_3d: u32,
    pub cart_count_7d: u32,
    pub cart_count_15d: u32,
    pub cart_count_30d: u32,
    pub cart_count_90d: u32,
    pub cart_count_180d: u32,
    pub cart_count_360d: u32,
    pub cart_count_1080d: u32,
    /// 近N天收藏商品次数
    pub favorite_count_1d: u32,
    pub favorite_count_3d: u32,
    pub favorite_count_7d: u32,
    pub favorite_count_15d: u32,
    pub favorite_count_30d: u32,
    pub favorite_count_90d: u32,
    pub favorite_count_180d: u32,
    pub favorite_count_360d: u32,
    pub favorite_count_1080d: u32,
    /// 近N天分享商品次数
    pub share_count_1d: u32,
    pub share_count_3d: u32,
    pub share_count_7d: u32,
    pub share_count_15d: u32,
    pub share_count_30d: u32,
    pub share_count_90d: u32,
    pub share_count_180d: u32,
    pub share_count_360d: u32,
    pub share_count_1080d: u32,
    /// 近N天评论次数
    pub comment_count_1d: u32,
    pub comment_count_3d: u32,
    pub comment_count_7d: u32,
    pub comment_count_15d: u32,
    pub comment_count_30d: u32,
    pub comment_count_90d: u32,
    pub comment_count_180d: u32,
    pub comment_count_360d: u32,
    pub comment_count_1080d: u32,
    /// 近N天点赞次数
    pub like_count_1d: u32,
    pub like_count_3d: u32,
    pub like_count_7d: u32,
    pub like_count_15d: u32,
    pub like_count_30d: u32,
    pub like_count_90d: u32,
    pub like_count_180d: u32,
    pub like_count_360d: u32,
    pub like_count_1080d: u32,
    /// 近N天下单次数(N=1/3/7/15/30/90/180/360/1080)
    pub order_count_1d: u32,
    pub order_count_3d: u32,
    pub order_count_7d: u32,
    pub order_count_15d: u32,
    pub order_count_30d: u32,
    pub order_count_90d: u32,
    pub order_count_180d: u32,
    pub order_count_360d: u32,
    pub order_count_1080d: u32,
    /// 近N天下单总金额
    pub order_amount_1d: f64,
    pub order_amount_3d: f64,
    pub order_amount_7d: f64,
    pub order_amount_15d: f64,
    pub order_amount_30d: f64,
    pub order_amount_90d: f64,
    pub order_amount_180d: f64,
    pub order_amount_360d: f64,
    pub order_amount_1080d: f64,
    /// 近N天支付次数
    pub payment_count_1d: u32,
    pub payment_count_3d: u32,
    pub payment_count_7d: u32,
    pub payment_count_15d: u32,
    pub payment_count_30d: u32,
    pub payment_count_90d: u32,
    pub payment_count_180d: u32,
    pub payment_count_360d: u32,
    pub payment_count_1080d: u32,
    /// 近N天支付总金额
    pub payment_amount_1d: f64,
    pub payment_amount_3d: f64,
    pub payment_amount_7d: f64,
    pub payment_amount_15d: f64,
    pub payment_amount_30d: f64,
    pub payment_amount_90d: f64,
    pub payment_amount_180d: f64,
    pub payment_amount_360d: f64,
    pub payment_amount_1080d: f64,
    /// 近N天退款次数
    pub refund_count_1d: u32,
    pub refund_count_3d: u32,
    pub refund_count_7d: u32,
    pub refund_count_15d: u32,
    pub refund_count_30d: u32,
    pub refund_count_90d: u32,
    pub refund_count_180d: u32,
    pub refund_count_360d: u32,
    pub refund_count_1080d: u32,
    /// 近N天退款总金额
    pub refund_amount_1d: f64,
    pub refund_amount_3d: f64,
    pub refund_amount_7d: f64,
    pub refund_amount_15d: f64,
    pub refund_amount_30d: f64,
    pub refund_amount_90d: f64,
    pub refund_amount_180d: f64,
    pub refund_amount_360d: f64,
    pub refund_amount_1080d: f64,
    /// 近N天活跃天数
    pub active_days_1d: u32,
    pub active_days_3d: u32,
    pub active_days_7d: u32,
    pub active_days_15d: u32,
    pub active_days_30d: u32,
    pub active_days_90d: u32,
    pub active_days_180d: u32,
    pub active_days_360d: u32,
    pub active_days_1080d: u32,
    /// 近N天平均客单价
    pub avg_order_value_1d: f64,
    pub avg_order_value_3d: f64,
    pub avg_order_value_7d: f64,
    pub avg_order_value_15d: f64,
    pub avg_order_value_30d: f64,
    pub avg_order_value_90d: f64,
    pub avg_order_value_180d: f64,
    pub avg_order_value_360d: f64,
    pub avg_order_value_1080d: f64,
    /// 近N天最大单笔订单金额
    pub max_order_amount_1d: f64,
    pub max_order_amount_3d: f64,
    pub max_order_amount_7d: f64,
    pub max_order_amount_15d: f64,
    pub max_order_amount_30d: f64,
    pub max_order_amount_90d: f64,
    pub max_order_amount_180d: f64,
    pub max_order_amount_360d: f64,
    pub max_order_amount_1080d: f64,
    /// 近N天最小单笔订单金额
    pub min_order_amount_1d: f64,
    pub min_order_amount_3d: f64,
    pub min_order_amount_7d: f64,
    pub min_order_amount_15d: f64,
    pub min_order_amount_30d: f64,
    pub min_order_amount_90d: f64,
    pub min_order_amount_180d: f64,
    pub min_order_amount_360d: f64,
    pub min_order_amount_1080d: f64,
    /// 近N天平均退款金额
    pub avg_refund_amount_1d: f64,
    pub avg_refund_amount_3d: f64,
    pub avg_refund_amount_7d: f64,
    pub avg_refund_amount_15d: f64,
    pub avg_refund_amount_30d: f64,
    pub avg_refund_amount_90d: f64,
    pub avg_refund_amount_180d: f64,
    pub avg_refund_amount_360d: f64,
    pub avg_refund_amount_1080d: f64,
    /// 距离最近一次消费的天数
    pub recency_days: Option<u32>,
    /// 一定周期内消费次数
    pub frequency_count: u32,
    /// 一定周期内消费总金额
    pub monetary_total: f64,
    /// 近30天登录天数
    pub login_days_last_30: u32,
    /// 平均单次会话时长(秒)
    pub avg_session_duration_seconds: f32,
    /// 平均客单价
    pub avg_order_value: f64,
    /// 退款率
    pub refund_rate: f32,
    /// RFM分层标签
    pub rfm_segment: Option<RfmSegment>,
    /// 聚类ID
    pub cluster_id: Option<u32>,
    /// 流失概率
    pub churn_probability: Option<f32>,
    /// 标签集合
    pub tags: HashSet<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RfmSegment {
    ImportantRetention,
    ImportantDevelopment,
    ImportantMaintenance,
    ImportantRecovery,
    GeneralRetention,
    GeneralDevelopment,
    GeneralMaintenance,
    GeneralRecovery,
}