refactor: is_paid→is_member, paid_expires_at→membership_expires_at 全局重命名
This commit is contained in:
50
src/db.rs
50
src/db.rs
@@ -11,12 +11,12 @@ use crate::error::AppError;
|
||||
pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData, user_id: i32) -> Result<i32, AppError> {
|
||||
// 配额检查:非付费用户数据条数限制
|
||||
let user = get_user_by_id(pool, user_id).await?;
|
||||
let is_paid_active = user.is_paid && user.paid_expires_at.map_or(true, |expires| expires > Utc::now());
|
||||
let is_active_member = user.is_member && user.membership_expires_at.map_or(true, |expires| expires > Utc::now());
|
||||
|
||||
// 维护模式:非会员使用更高的临时限额,防止资源滥用
|
||||
let is_maintenance = std::env::var("PAYMENT_MAINTENANCE_MODE").ok() == Some("true".to_string());
|
||||
|
||||
if !is_paid_active {
|
||||
if !is_active_member {
|
||||
let current_count = count_user_weather_data(pool, user_id).await?;
|
||||
let limit: i64 = if is_maintenance {
|
||||
env::var("MAINTENANCE_MODE_DATA_LIMIT")
|
||||
@@ -237,7 +237,7 @@ pub async fn delete_weather_data(
|
||||
pub async fn get_user_by_id(pool: &PgPool, user_id: i32) -> Result<User, AppError> {
|
||||
let query = r#"
|
||||
SELECT
|
||||
id, name, openid, phone, type, "desc", is_paid, is_admin, paid_expires_at,
|
||||
id, name, openid, phone, type, "desc", is_member, is_admin, membership_expires_at,
|
||||
avatar_url, nickname
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
@@ -278,18 +278,18 @@ pub async fn count_user_weather_data(pool: &PgPool, user_id: i32) -> Result<i64,
|
||||
pub async fn update_user_payment_status(
|
||||
pool: &PgPool,
|
||||
user_id: i32,
|
||||
is_paid: bool,
|
||||
paid_expires_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||
is_member: bool,
|
||||
membership_expires_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||
) -> Result<(), AppError> {
|
||||
let query = r#"
|
||||
UPDATE users
|
||||
SET is_paid = $1, paid_expires_at = $2
|
||||
SET is_member = $1, membership_expires_at = $2
|
||||
WHERE id = $3
|
||||
"#;
|
||||
|
||||
match sqlx::query(query)
|
||||
.bind(is_paid)
|
||||
.bind(paid_expires_at)
|
||||
.bind(is_member)
|
||||
.bind(membership_expires_at)
|
||||
.bind(user_id)
|
||||
.execute(pool)
|
||||
.await
|
||||
@@ -510,11 +510,11 @@ pub async fn confirm_payment_order(
|
||||
UPDATE payment_orders SET status = 'paid', paid_at = NOW() WHERE order_no = $1 RETURNING package_type
|
||||
)
|
||||
UPDATE users SET
|
||||
is_paid = true,
|
||||
paid_expires_at =
|
||||
is_member = true,
|
||||
membership_expires_at =
|
||||
CASE
|
||||
WHEN uo.package_type = 'permanent' THEN NULL
|
||||
ELSE GREATEST(COALESCE(users.paid_expires_at, NOW()), NOW()) +
|
||||
ELSE GREATEST(COALESCE(users.membership_expires_at, NOW()), NOW()) +
|
||||
CASE
|
||||
WHEN uo.package_type = 'monthly' THEN INTERVAL '30 days'
|
||||
WHEN uo.package_type = 'yearly' THEN INTERVAL '365 days'
|
||||
@@ -523,7 +523,7 @@ pub async fn confirm_payment_order(
|
||||
END
|
||||
FROM updated_order uo
|
||||
WHERE users.id = $2
|
||||
RETURNING users.paid_expires_at
|
||||
RETURNING users.membership_expires_at
|
||||
"#,
|
||||
)
|
||||
.bind(order_no)
|
||||
@@ -575,11 +575,11 @@ pub async fn confirm_payment_order_by_orderno(
|
||||
UPDATE payment_orders SET status = 'paid', paid_at = NOW() WHERE order_no = $1 RETURNING package_type, user_id
|
||||
)
|
||||
UPDATE users SET
|
||||
is_paid = true,
|
||||
paid_expires_at =
|
||||
is_member = true,
|
||||
membership_expires_at =
|
||||
CASE
|
||||
WHEN uo.package_type = 'permanent' THEN NULL
|
||||
ELSE GREATEST(COALESCE(users.paid_expires_at, NOW()), NOW()) +
|
||||
ELSE GREATEST(COALESCE(users.membership_expires_at, NOW()), NOW()) +
|
||||
CASE
|
||||
WHEN uo.package_type = 'monthly' THEN INTERVAL '30 days'
|
||||
WHEN uo.package_type = 'yearly' THEN INTERVAL '365 days'
|
||||
@@ -654,7 +654,7 @@ pub async fn refund_payment_order(
|
||||
// 如果没有其他有效订单,撤销会员状态
|
||||
if other_active.0 == 0 {
|
||||
sqlx::query(
|
||||
"UPDATE users SET is_paid = false, paid_expires_at = NULL WHERE id = $1",
|
||||
"UPDATE users SET is_member = false, membership_expires_at = NULL WHERE id = $1",
|
||||
)
|
||||
.bind(user_id)
|
||||
.execute(pool)
|
||||
@@ -684,10 +684,10 @@ pub async fn get_user_quota(
|
||||
user_id: i32,
|
||||
) -> Result<(i64, bool, Option<chrono::DateTime<chrono::Utc>>, bool), AppError> {
|
||||
let user = get_user_by_id(pool, user_id).await?;
|
||||
let is_paid_active = user.is_paid
|
||||
&& user.paid_expires_at.map_or(true, |expires| expires > chrono::Utc::now());
|
||||
let is_active_member = user.is_member
|
||||
&& user.membership_expires_at.map_or(true, |expires| expires > chrono::Utc::now());
|
||||
let used = count_user_weather_data(pool, user_id).await?;
|
||||
Ok((used, is_paid_active, user.paid_expires_at, user.is_paid))
|
||||
Ok((used, is_active_member, user.membership_expires_at, user.is_member))
|
||||
}
|
||||
|
||||
// ===== 收藏功能 DB 函数 =====
|
||||
@@ -924,11 +924,11 @@ pub async fn admin_force_confirm_order(
|
||||
UPDATE payment_orders SET status = 'paid', paid_at = NOW() WHERE order_no = $1 RETURNING package_type
|
||||
)
|
||||
UPDATE users SET
|
||||
is_paid = true,
|
||||
paid_expires_at =
|
||||
is_member = true,
|
||||
membership_expires_at =
|
||||
CASE
|
||||
WHEN uo.package_type = 'permanent' THEN NULL
|
||||
ELSE GREATEST(COALESCE(users.paid_expires_at, NOW()), NOW()) +
|
||||
ELSE GREATEST(COALESCE(users.membership_expires_at, NOW()), NOW()) +
|
||||
CASE
|
||||
WHEN uo.package_type = 'monthly' THEN INTERVAL '30 days'
|
||||
WHEN uo.package_type = 'yearly' THEN INTERVAL '365 days'
|
||||
@@ -937,7 +937,7 @@ pub async fn admin_force_confirm_order(
|
||||
END
|
||||
FROM updated_order uo
|
||||
WHERE users.id = $2
|
||||
RETURNING users.paid_expires_at
|
||||
RETURNING users.membership_expires_at
|
||||
"#,
|
||||
)
|
||||
.bind(order_no)
|
||||
@@ -972,7 +972,7 @@ pub async fn admin_force_confirm_order(
|
||||
|
||||
// 返回当前到期时间
|
||||
sqlx::query_scalar::<_, Option<chrono::DateTime<chrono::Utc>>>(
|
||||
r#"SELECT paid_expires_at FROM users WHERE id = $1"#,
|
||||
r#"SELECT membership_expires_at FROM users WHERE id = $1"#,
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_one(pool)
|
||||
@@ -1074,7 +1074,7 @@ pub async fn admin_refund_order(
|
||||
|
||||
if other_active.0 == 0 {
|
||||
sqlx::query(
|
||||
"UPDATE users SET is_paid = false, paid_expires_at = NULL WHERE id = $1",
|
||||
"UPDATE users SET is_member = false, membership_expires_at = NULL WHERE id = $1",
|
||||
)
|
||||
.bind(user_id)
|
||||
.execute(pool)
|
||||
|
||||
Reference in New Issue
Block a user