feat: network_matched_count缓存机制优化
1. 创建寻配号时初始化network_matched_count 2. 列表接口从数据库读取,如为0则计算一次 3. 点击查看网络匹配时更新数据库 News.jsx: 0.0.6 → 0.0.7 seek.py: 0.0.7 → 0.0.8 seek_info.py: 添加network_matched_count字段
This commit is contained in:
parent
3a2258d2d3
commit
d1a245c340
|
|
@ -33,6 +33,7 @@ class SeekInfo(Base):
|
|||
# 统计
|
||||
view_count = Column(Integer, default=0)
|
||||
contact_count = Column(Integer, default=0)
|
||||
network_matched_count = Column(Integer, default=0) # 网络匹配数量(缓存)
|
||||
|
||||
# 时间
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# seek - 寻配号路由
|
||||
# Version: 0.0.7 (2026-04-24)
|
||||
# 更新:添加network_matched_count计算
|
||||
# Version: 0.0.8 (2026-04-24)
|
||||
# 更新:network_matched_count缓存到数据库,创建时初始化,点击时更新
|
||||
|
||||
from fastapi import APIRouter, Depends, Query, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
|
@ -104,14 +104,19 @@ def get_seek_list(
|
|||
user_name = user.f01_01_name if user else '匿名用户'
|
||||
|
||||
# 构建响应(不实时计算网络匹配,网络匹配在点击时再查)
|
||||
# 计算自有匹配数量和网络匹配数量
|
||||
# 计算自有匹配数量,网络匹配数量从数据库读取(如为0则计算一次)
|
||||
matched_count = 0
|
||||
network_matched_count = 0
|
||||
network_matched_count = getattr(item, 'network_matched_count', 0) or 0
|
||||
if network_matched_count == 0 and item.expect_number and len(item.expect_number) == 10:
|
||||
# 如果数据库中没有值,计算一次并保存
|
||||
network_matched_count = match_collections_count_from_coolbot(item.expect_number)
|
||||
# 更新数据库
|
||||
item.network_matched_count = network_matched_count
|
||||
db.commit()
|
||||
|
||||
if item.expect_number and len(item.expect_number) == 10:
|
||||
if current_user and current_user.f99_90_id:
|
||||
matched_count = match_collections_count(db, current_user.f99_90_id, item.expect_number)
|
||||
# 网络匹配数量(需要一定时间,可以返回0让用户点击时再计算)
|
||||
network_matched_count = match_collections_count_from_coolbot(item.expect_number)
|
||||
|
||||
result.append(SeekInfoResponse(
|
||||
id=item.id,
|
||||
|
|
@ -164,6 +169,11 @@ def create_seek(
|
|||
if not current_user:
|
||||
raise HTTPException(status_code=401, detail="请先登录")
|
||||
|
||||
# 计算网络匹配数量(创建时初始化)
|
||||
network_matched_count = 0
|
||||
if data.expect_number and len(data.expect_number) == 10:
|
||||
network_matched_count = match_collections_count_from_coolbot(data.expect_number)
|
||||
|
||||
seek = SeekInfo(
|
||||
user_id=current_user.f99_90_id,
|
||||
title=data.title,
|
||||
|
|
@ -174,7 +184,10 @@ def create_seek(
|
|||
expect_number=data.expect_number,
|
||||
expect_price_min=data.expect_price_min,
|
||||
expect_price_max=data.expect_price_max,
|
||||
status="active"
|
||||
status="active",
|
||||
view_count=0,
|
||||
contact_count=0,
|
||||
network_matched_count=network_matched_count # 保存初始化值
|
||||
)
|
||||
db.add(seek)
|
||||
db.commit()
|
||||
|
|
@ -232,10 +245,10 @@ def get_seek_match(
|
|||
@router.get("/network-match/{info_id}")
|
||||
def get_network_match(
|
||||
info_id: str,
|
||||
limit: int = Query(20, ge=1, le=100),
|
||||
limit: int = Query(100, ge=1, le=500),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""获取一尘数据库中匹配的藏品列表"""
|
||||
"""获取一尘数据库中匹配的藏品列表,点击时更新network_matched_count"""
|
||||
info = db.query(SeekInfo).filter(SeekInfo.id == info_id).first()
|
||||
if not info:
|
||||
raise HTTPException(status_code=404, detail="寻配号信息不存在")
|
||||
|
|
@ -243,8 +256,15 @@ def get_network_match(
|
|||
if not info.expect_number:
|
||||
return {"matched_count": 0, "collections": []}
|
||||
|
||||
# 获取匹配列表
|
||||
matched = match_collections_list_from_coolbot(info.expect_number, limit=limit)
|
||||
return {"matched_count": len(matched), "collections": matched}
|
||||
actual_count = len(matched)
|
||||
|
||||
# 点击查看时更新network_matched_count
|
||||
info.network_matched_count = actual_count
|
||||
db.commit()
|
||||
|
||||
return {"matched_count": actual_count, "collections": matched}
|
||||
|
||||
@router.post("/match-confirm")
|
||||
def match_seek_confirm(
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* News - 资讯列表页面
|
||||
* Version: 0.0.6 (2026-04-24)
|
||||
* 更新:网络匹配数量正确显示
|
||||
* Version: 0.0.7 (2026-04-24)
|
||||
* 更新:network_matched_count缓存机制
|
||||
*/
|
||||
|
||||
import React, { useState, useEffect } from 'react'
|
||||
|
|
|
|||
Loading…
Reference in New Issue