Files
ragflow_python/api/db/services/antique_service.py

138 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from datetime import datetime
import peewee
from werkzeug.security import generate_password_hash, check_password_hash
from api.db import UserTenantRole
from api.db.db_models import DB, UserTenant
from api.db.db_models import User, Tenant, MesumAntique
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
class MesumAntiqueService(CommonService):
model = MesumAntique
@classmethod
@DB.connection_context()
def get_by_mesum_id(cls, mesum_id):
objs = cls.query(mesum_id=mesum_id)
return objs
@classmethod
@DB.connection_context()
def get_all_categories(cls,mesum_id):
# 查询所有唯一的category
mesum_antique_categories = []
try:
mesum_brief = MesumOverviewService.query(id=mesum_id)
if mesum_brief:
categories_text= mesum_brief[0].category
# 统一替换中文分号为英文分号,并去除末尾分号
if categories_text:
categories_text = categories_text.replace("", ";").rstrip(";")
# 分割并清理空格/空值
mesum_antique_categories = [dynasty.strip() for dynasty in categories_text.split(";") if dynasty.strip()]
finally:
pass
categories = [category.category
for category in (
cls.model.select(cls.model.category)
.where(cls.model.mesum_id==mesum_id)
.distinct()
.execute()
)
if category.category
]
# 下面代码是按照博物馆brief定义的目录顺序调整输出的目录顺序以便前端能够按照正确顺序显示
# cyx 20250415
# 创建字典映射元素到索引,提升查询效率
mesum_antique_categories_dict = {value: idx for idx, value in enumerate(mesum_antique_categories)}
# 排序逻辑:通过查字典获取顺序号,不在字典的给极大值(排最后)
categories_sorted = sorted(
categories,
key=lambda item: mesum_antique_categories_dict.get(item, len(mesum_antique_categories)) # 查字典获取顺序号
)
return categories_sorted
@classmethod
@DB.connection_context()
def get_all_labels(cls):
# 查询所有去重后的label
labels = [label.label for label in cls.model.select(cls.model.label).distinct().execute() if label.label]
return labels
@classmethod
@DB.connection_context()
def get_labels_ext(cls, mesum_id):
# 根据mesum_id过滤并排除空的category
query = cls.model.select().where(
(cls.model.mesum_id == mesum_id) &
(cls.model.category != "")
).order_by(cls.model.category)
# 按category分组并存储结果
grouped_data = {}
for obj in query.dicts().execute():
category = obj['category']
if category not in grouped_data:
grouped_data[category] = []
grouped_data[category].append({
'id': obj['id'],
'label': obj['label']
})
return grouped_data
@classmethod
@DB.connection_context()
def get_labels_with_id(cls, mesum_id):
# 根据mesum_id过滤并排除空的category
query = cls.model.select().where(
(cls.model.mesum_id == mesum_id)
).order_by(cls.model.id)
# 将label和关联的id 一并返回
labels_data = []
for obj in query.dicts():
labels_data.append({
'id': obj['id'],
'label': obj['label']
})
return labels_data
@classmethod
@DB.connection_context()
def get_antique_by_id(cls, mesum_id,antique_id):
query = cls.model.select().where(
(cls.model.mesum_id == mesum_id) &
(cls.model.id == antique_id)
)
data = []
for obj in query.dicts().execute():
data.append(obj)
if len(data) > 0:
data = data[0]
return data