Commit 5fbd8156 authored by ZZH's avatar ZZH

contact add time condition 2024-03-14

parent 82a5137f
...@@ -12,6 +12,8 @@ from pot_libs.sanic_api.column import Opt, Int, Str, List ...@@ -12,6 +12,8 @@ from pot_libs.sanic_api.column import Opt, Int, Str, List
class ContactsReq(Model): class ContactsReq(Model):
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"))
@dataclass @dataclass
......
...@@ -6,14 +6,20 @@ DATE:2024/3/7 11:08 ...@@ -6,14 +6,20 @@ DATE:2024/3/7 11:08
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
async def load_contact_records(page_num, page_size): async def load_contact_records(page_num, page_size, s_dts, e_dts):
offsets = 0 if page_num == 0 else (page_num - 1) * page_size offsets = 0 if page_num == 0 else (page_num - 1) * page_size
ts_conds = []
if s_dts:
ts_conds.append(f"pub_time>='{str(s_dts)}'")
if e_dts:
ts_conds.append(f"pub_time<'{str(e_dts)}'")
conds_str = " WHERE " + " AND ".join(ts_conds) if ts_conds else ""
async with MysqlUtil(db="official_web") as conn: async with MysqlUtil(db="official_web") as conn:
sql = "SELECT count(*) FROM official_web.contact_record;" sql = f"SELECT count(*) FROM official_web.contact_record {conds_str};"
total = await conn.fetch_value(sql) total = await conn.fetch_value(sql)
sql = f"SELECT * FROM official_web.contact_record " \ sql = f"SELECT * FROM official_web.contact_record {conds_str} " \
f"ORDER BY create_time DESC, id LIMIT %s OFFSET %s;" f"ORDER BY create_time DESC, id LIMIT %s OFFSET %s;"
records = await conn.fetchall(sql, (page_size, offsets)) records = await conn.fetchall(sql, (page_size, offsets))
return total, records return total, records
...@@ -125,27 +125,14 @@ async def load_news_pages(params): ...@@ -125,27 +125,14 @@ async def load_news_pages(params):
if se_title: if se_title:
conds.append(f"title LIKE '%%{se_title}%%'") conds.append(f"title LIKE '%%{se_title}%%'")
if len(conds) > 1: conds_str = " WHERE " + " AND ".join(conds) if conds else ""
conds_str = "WHERE " + " AND ".join(conds)
elif len(conds) == 1:
conds_str = f"WHERE {conds[0]} "
else:
conds_str = ""
async with MysqlUtil(db="official_web") as conn: async with MysqlUtil(db="official_web") as conn:
if news_type: sql = f"SELECT count(*) FROM official_web.news_info {conds_str};"
sql = "SELECT count(*) FROM official_web.news_info " \
"WHERE news_type=%s;"
total = await conn.fetch_value(sql, (news_type,))
else:
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 {conds_str} " \
f"{conds_str} " \
f"ORDER BY pub_time DESC, id LIMIT {page_size} OFFSET {offsets};" f"ORDER BY pub_time DESC, id LIMIT {page_size} OFFSET {offsets};"
articles = await conn.fetchall(sql) articles = await conn.fetchall(sql)
return total, articles return total, articles
......
...@@ -14,9 +14,10 @@ from unify_api.modules.qkadmin.service.contact_us_srv import ( ...@@ -14,9 +14,10 @@ from unify_api.modules.qkadmin.service.contact_us_srv import (
@summary("联系我们记录") @summary("联系我们记录")
async def post_contact_records(request, body: ContactsReq) -> ContactsRsp: async def post_contact_records(request, body: ContactsReq) -> ContactsRsp:
page_num = int(body.page_num) pg_num = int(body.page_num)
page_size = int(body.page_size) pg_size = int(body.page_size)
total, records = await load_contact_records(page_num, page_size) s_dts, e_dts = body.start_time, body.end_time
total, records = await load_contact_records(pg_num, pg_size, s_dts, e_dts)
contacts = [ContactInfo(id=r["id"], user_name=r["user_name"], contacts = [ContactInfo(id=r["id"], user_name=r["user_name"],
tele=r["tele"], company=r["company"], tele=r["tele"], company=r["company"],
msg=r["msg"], create_time=str(r["create_time"]), msg=r["msg"], create_time=str(r["create_time"]),
......
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