小程序在9月27日发布了正式版
This commit is contained in:
@@ -25,7 +25,7 @@ from api.db.services.common_service import CommonService
|
||||
from api.db.services.brief_service import MesumOverviewService
|
||||
from api.utils import get_uuid, get_format_time, current_timestamp, datetime_format
|
||||
from api.db import StatusEnum
|
||||
import logging
|
||||
import logging,json
|
||||
|
||||
class MesumAntiqueService(CommonService):
|
||||
model = MesumAntique
|
||||
@@ -33,8 +33,25 @@ class MesumAntiqueService(CommonService):
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
def get_by_mesum_id(cls, mesum_id):
|
||||
objs = cls.query(mesum_id=mesum_id)
|
||||
return objs
|
||||
# objs = cls.query(mesum_id=mesum_id)
|
||||
# return objs
|
||||
"""
|
||||
根据mesum_id查询记录,并按category和sort_order排序
|
||||
"""
|
||||
try:
|
||||
objs = (cls.model
|
||||
.select()
|
||||
.where(cls.model.mesum_id == mesum_id)
|
||||
.order_by(
|
||||
cls.model.category, # 一级排序:按category
|
||||
cls.model.sort_order.asc() # 二级排序:sort_order升序
|
||||
)
|
||||
.execute())
|
||||
return list(objs)
|
||||
|
||||
except Exception as e:
|
||||
print(f"根据mesum_id查询失败: {e}")
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
@@ -44,6 +61,15 @@ class MesumAntiqueService(CommonService):
|
||||
try:
|
||||
mesum_brief = MesumOverviewService.query(id=mesum_id)
|
||||
if mesum_brief:
|
||||
|
||||
"""
|
||||
20250922 数据库中category 改为如下形式
|
||||
[{"name":"第一展厅(新石器-唐、战国、秦汉)","audio":"xxxx","photo":"xxx"},
|
||||
{"name":"第二展厅 (辽金)","audio":"xxx","photo":""},
|
||||
{"name":"第三展厅(元)","audio":"xxx","photo":""},
|
||||
{"name":"第四展厅(明清-至今)","audio":"xxx","photo":""}]
|
||||
"""
|
||||
"""
|
||||
categories_text= mesum_brief[0].category
|
||||
# 统一替换中文分号为英文分号,并去除末尾分号
|
||||
if categories_text:
|
||||
@@ -51,6 +77,9 @@ class MesumAntiqueService(CommonService):
|
||||
# 分割并清理空格/空值
|
||||
mesum_antique_categories = [dynasty.strip() for dynasty in categories_text.split(";") if dynasty.strip()]
|
||||
|
||||
"""
|
||||
mesum_category_json = json.loads(mesum_brief[0].category.strip())
|
||||
mesum_antique_categories = [item["name"] for item in mesum_category_json]
|
||||
finally:
|
||||
pass
|
||||
categories = [category.category
|
||||
@@ -82,13 +111,61 @@ class MesumAntiqueService(CommonService):
|
||||
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
def get_labels_ext(cls, mesum_id):
|
||||
def get_labels_ext(cls, mesum_id,tags = None):
|
||||
# 根据mesum_id过滤,并排除空的category
|
||||
query = cls.model.select().where(
|
||||
"""query = cls.model.select().where(
|
||||
(cls.model.mesum_id == mesum_id) &
|
||||
(cls.model.category != "")
|
||||
).order_by(cls.model.category)
|
||||
).order_by(
|
||||
cls.model.category, # 一级排序:按category
|
||||
cls.model.sort_order.asc() # 二级排序:sort_order升序
|
||||
)
|
||||
"""
|
||||
# 基础查询条件
|
||||
conditions = [
|
||||
(cls.model.mesum_id == mesum_id),
|
||||
(cls.model.category != ""),
|
||||
(cls.model.category.is_null(False)) # 确保category不为NULL
|
||||
]
|
||||
# 添加tags条件
|
||||
if tags is not None:
|
||||
# 如果tags是字符串,按逗号分割处理
|
||||
if isinstance(tags, str):
|
||||
tags_list = [tag.strip() for tag in tags.split(',') if tag.strip()]
|
||||
elif isinstance(tags, list):
|
||||
tags_list = tags
|
||||
else:
|
||||
tags_list = []
|
||||
|
||||
if tags_list:
|
||||
# 构建tags匹配条件:tags包含任意一个指定的标签
|
||||
tag_conditions = []
|
||||
for tag in tags_list:
|
||||
# 匹配tags字段包含该标签(考虑逗号分隔的情况)
|
||||
tag_conditions.append(
|
||||
(cls.model.tags.contains(tag)) |
|
||||
(cls.model.tags.startswith(f"{tag},")) |
|
||||
(cls.model.tags.endswith(f",{tag}")) |
|
||||
(cls.model.tags == tag)
|
||||
)
|
||||
|
||||
# 将所有tag条件用OR连接
|
||||
tags_condition = reduce(lambda a, b: a | b, tag_conditions)
|
||||
conditions.append(tags_condition)
|
||||
else:
|
||||
# 如果tags为空但传入了参数,确保tags不为空
|
||||
conditions.append(
|
||||
(cls.model.tags != "") &
|
||||
(cls.model.tags.is_null(False))
|
||||
)
|
||||
else:
|
||||
# 如果未传入tags参数,包含所有tags记录(包括空值)
|
||||
pass
|
||||
# 执行查询,添加二级排序
|
||||
query = cls.model.select().where(*conditions).order_by(
|
||||
cls.model.category, # 一级排序:按category
|
||||
cls.model.sort_order.asc() # 二级排序:按sort_order升序
|
||||
)
|
||||
# 按category分组并存储结果
|
||||
grouped_data = {}
|
||||
for obj in query.dicts().execute():
|
||||
|
||||
Reference in New Issue
Block a user