Commit 10a425d8 authored by ZZH's avatar ZZH

news search add conditions 2024-03-6

parent a27dbeda
...@@ -48,6 +48,9 @@ class NewsPagesReq(Model): ...@@ -48,6 +48,9 @@ class NewsPagesReq(Model):
news_type: int = Opt(Int("文章类型(1:公司新闻, 2:行业新闻) 不传则返回所有类型文章").eg(1)) news_type: int = Opt(Int("文章类型(1:公司新闻, 2:行业新闻) 不传则返回所有类型文章").eg(1))
page_num: int = Int("页码").eg(1) page_num: int = Int("页码").eg(1)
page_size: int = Opt(Int("条数").eg(10)) page_size: int = Opt(Int("条数").eg(10))
start_time: str = Opt(Str("起始时间").eg("2024-03-07 00:00:00"))
end_time: str = Opt(Str("截止时间").eg("2023-12-29 18:06"))
se_title: str = Opt(Str("搜索的文章标题").eg("万物梁行携手清科优能"))
@dataclass @dataclass
......
...@@ -78,27 +78,48 @@ async def update_news_seo(news_id, keywords, contents): ...@@ -78,27 +78,48 @@ async def update_news_seo(news_id, keywords, contents):
return await conn.execute(sql, (news_id, keywords, contents,)) return await conn.execute(sql, (news_id, keywords, contents,))
async def load_news_pages(news_type, page_num, page_size): async def load_news_pages(params):
page_num, page_size = params["page_num"], params["page_size"]
news_type, se_title = params["news_type"], params["se_title"]
s_dts, e_dts = params["s_dts"], params["e_dts"]
offsets = 0 if page_num == 0 else (page_num - 1) * page_size
conds, param_lst = [], []
if news_type:
param_lst.append(news_type)
conds.append(f"news_type=%s")
if s_dts:
s_dt = pendulum.parse(s_dts, tz="Asia/Shanghai")
param_lst.append(str(s_dt))
conds.append(f"pub_time>=%s")
if e_dts:
e_dt = pendulum.parse(e_dts, tz="Asia/Shanghai")
param_lst.append(str(e_dt))
conds.append(f"pub_time<%s")
if se_title:
param_lst.append(se_title)
conds.append(f"title LIKE %{se_title}%")
if len(conds) > 1:
conds_str = "WHERE" + " AND ".join(conds)
elif len(conds) == 1:
conds_str = f"WHERE {conds[0]} "
else:
conds_str = ""
param_lst += [page_size, offsets]
async with MysqlUtil(db="official_web") as conn: async with MysqlUtil(db="official_web") as conn:
if news_type: if news_type:
sql = "SELECT count(*) FROM official_web.news_info " \ sql = "SELECT count(*) FROM official_web.news_info " \
"WHERE news_type=%s;" "WHERE news_type=%s;"
total = await conn.fetch_value(sql, (news_type,)) total = await conn.fetch_value(sql, (news_type,))
sql = f"SELECT id, title, author, news_type, pub_time, top " \
f"FROM official_web.news_info " \
f"WHERE news_type=%s " \
f"ORDER BY pub_time DESC, id LIMIT %s OFFSET %s;"
offsets = 0 if page_num == 0 else (page_num - 1) * page_size
articles = await conn.fetchall(sql, (news_type, page_size,
offsets,))
else: else:
sql = "SELECT count(*) FROM official_web.news_info;" sql = "SELECT count(*) FROM official_web.news_info;"
total = await conn.fetch_value(sql) total = await conn.fetch_value(sql)
sql = f"SELECT id, title, author, news_type, pub_time, top " \ sql = f"SELECT id, title, author, news_type, pub_time, top " \
f"FROM official_web.news_info " \ f"FROM official_web.news_info " \
f"ORDER BY pub_time DESC, id LIMIT %s OFFSET %s;" f"{conds_str} " \
offsets = 0 if page_num == 0 else (page_num - 1) * page_size f"ORDER BY pub_time DESC, id LIMIT %s OFFSET %s;"
articles = await conn.fetchall(sql, (page_size, offsets,))
articles = await conn.fetchall(sql, (tuple(param_lst)))
return total, articles return total, articles
...@@ -50,8 +50,13 @@ async def post_revise_news_seo(request, body: ReviseNewsSeoReq) -> SuccessRsp: ...@@ -50,8 +50,13 @@ async def post_revise_news_seo(request, body: ReviseNewsSeoReq) -> SuccessRsp:
async def post_news_pages(request, body: NewsPagesReq) -> NewsPagesRsp: async def post_news_pages(request, body: NewsPagesReq) -> NewsPagesRsp:
page_num = int(body.page_num) page_num = int(body.page_num)
page_size = int(body.page_size) page_size = int(body.page_size)
news_type = int(body.page_size) if body.page_size else 0 news_type = int(body.news_type) if body.news_type else 0
total, articles = await load_news_pages(news_type, page_num, page_size) s_dts = body.start_time
e_dts = body.end_time
se_title = body.se_title
params = dict(page_num=page_num, page_size=page_size, news_type=news_type,
s_dts=s_dts, e_dts=e_dts, se_title=se_title)
total, articles = await load_news_pages(params)
rsp_articles = [ rsp_articles = [
NewsInfo(id=r["id"], title=r["title"], author=r["author"], NewsInfo(id=r["id"], title=r["title"], author=r["author"],
pub_time=str(r["pub_time"]), news_type=r["news_type"], pub_time=str(r["pub_time"]), news_type=r["news_type"],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment