From 5ad49852b309954ab14ae3afb213ed4a0c2664b3 Mon Sep 17 00:00:00 2001 From: soehi <811446351@qq.com> Date: Fri, 4 Dec 2026 15:11:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=9B=E8=81=98=E5=85=AC=E5=91=8A=E6=8C=89?= =?UTF-8?q?=E5=8E=9F=E7=BD=91=E9=A1=B5=E9=A1=BA=E5=BA=8F=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/main.py | 3 +++ server/models.py | 1 + server/recruit.py | 6 ++++-- tests/test_recruit.py | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/server/main.py b/server/main.py index 922cf7c..c41c0fd 100644 --- a/server/main.py +++ b/server/main.py @@ -592,6 +592,9 @@ async def lifespan(_app: FastAPI): columns = await conn.run_sync(lambda c: {col['name'] for col in inspect(c).get_columns('center_activity_meta')}) if 'material_id' not in columns: await conn.execute(text('ALTER TABLE center_activity_meta ADD COLUMN material_id VARCHAR(32)')) + recruit_columns = await conn.run_sync(lambda c: {col['name'] for col in inspect(c).get_columns('recruit_announcements')}) + if 'source_order' not in recruit_columns: + await conn.execute(text('ALTER TABLE recruit_announcements ADD COLUMN source_order INTEGER NOT NULL DEFAULT 0')) await _migrate_legacy(conn) # 一楼历史活动统一作为公开记录,历史参加数据保留存档但不再参与统计。 center_ids = select(center_activity_meta.c.activity_id) diff --git a/server/models.py b/server/models.py index bae00de..eb3b7f6 100644 --- a/server/models.py +++ b/server/models.py @@ -199,6 +199,7 @@ recruit_announcements = Table('recruit_announcements', metadata, Column('iswailian', Integer, nullable=False, default=0), Column('wailian', String(500), nullable=False, default=''), Column('url', String(500), nullable=False, default=''), + Column('source_order', Integer, nullable=False, default=0), Column('synced_at', Integer, nullable=False)) Index('ix_recruit_announcements_check_time', recruit_announcements.c.check_time.desc()) diff --git a/server/recruit.py b/server/recruit.py index 6a70b63..c404871 100644 --- a/server/recruit.py +++ b/server/recruit.py @@ -295,6 +295,7 @@ async def sync_all(db, client: httpx.AsyncClient | None = None) -> dict: if owns_client: await client.aclose() now = int(datetime.now(BEIJING).timestamp() * 1000) + source_order = {int(raw["gonggaoId"]): order for order, raw in enumerate(rows) if str(raw.get("gonggaoId") or "").isdigit()} for raw in rows: try: gonggao_id = int(raw["gonggaoId"]) @@ -312,6 +313,7 @@ async def sync_all(db, client: httpx.AsyncClient | None = None) -> dict: "iswailian": int(raw.get("iswailian") or 0), "wailian": str(raw.get("wailian") or ""), "url": str(raw.get("url") or ""), + "source_order": source_order.get(gonggao_id, 0), "synced_at": now, } existing = (await db.execute(select(recruit_announcements.c.gonggao_id).where(recruit_announcements.c.gonggao_id == gonggao_id))).scalar() @@ -414,7 +416,7 @@ def install(app, get_db, current_user, cache_dir: Path | None = None): keyword = f"%{q.strip()}%" statement = statement.where(recruit_announcements.c.title.like(keyword) | recruit_announcements.c.companyname.like(keyword)) total = (await db.execute(select(func.count()).select_from(statement.subquery()))).scalar() - statement = statement.order_by(recruit_announcements.c.check_time.desc(), recruit_announcements.c.gonggao_id.desc()).limit(500) + statement = statement.order_by(recruit_announcements.c.source_order.asc(), recruit_announcements.c.gonggao_id.desc()).limit(500) rows = [public_row(row) for row in (await db.execute(statement)).mappings()] return {"items": rows, "total": total} @@ -448,7 +450,7 @@ def install(app, get_db, current_user, cache_dir: Path | None = None): elif q.strip(): keyword = f"%{q.strip()}%" statement = statement.where(recruit_announcements.c.title.like(keyword) | recruit_announcements.c.companyname.like(keyword)) - statement = statement.order_by(recruit_announcements.c.check_time.desc(), recruit_announcements.c.gonggao_id.desc()) + statement = statement.order_by(recruit_announcements.c.source_order.asc(), recruit_announcements.c.gonggao_id.desc()) rows = [dict(row) for row in (await db.execute(statement)).mappings()] if not rows: raise HTTPException(404, "没有匹配的公告") diff --git a/tests/test_recruit.py b/tests/test_recruit.py index e4e8acc..48af4f7 100644 --- a/tests/test_recruit.py +++ b/tests/test_recruit.py @@ -76,3 +76,20 @@ class RecruitTests(ApiTestCase): fetch_detail.assert_awaited_once() detail = (await self.client.get('/api/recruit/announcements/2', headers=auth(owner['token']))).json() self.assertIn('外贸业务员', detail['content_html']) + + async def test_list_and_export_follow_source_order(self): + owner = await register(self.client, 'recruit-order') + rows = [ + dict(RAW, gonggaoId=21, checkTime='2020-01-01 00:00:00'), + dict(RAW, gonggaoId=22, checkTime='2020-01-02 00:00:00'), + dict(RAW, gonggaoId=23, checkTime='2020-01-03 00:00:00'), + ] + fetch = AsyncMock(return_value={"code": 0, "rows": rows, "extra": {"pages": 1}}) + with patch.object(recruit, 'fetch_page', new=fetch): + await self.client.post('/api/recruit/sync', headers=auth(owner['token'])) + data = (await self.client.get('/api/recruit/announcements', headers=auth(owner['token']))).json() + self.assertEqual([item['gonggao_id'] for item in data['items']], [21, 22, 23]) + response = await self.client.get('/api/recruit/export', params={'ids': '23,21,22', 'format': 'markdown'}, headers=auth(owner['token'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.headers['X-Recruit-Export-Count'], '3') + self.assertLess(response.text.find('测试公告'), response.text.find('原文链接'))