117 lines
4.0 KiB
Python
117 lines
4.0 KiB
Python
"""CRUD - 分类参数关联"""
|
|
from typing import List
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import and_
|
|
|
|
from app.crud.base import CRUDBase, HistoryCRUD
|
|
from app.models.category_parameter import BasCategoryParameter, BasCategoryParameterHis
|
|
from app.schemas.category_parameter import CategoryParameterBase
|
|
|
|
|
|
class CategoryParameterCRUD(CRUDBase[BasCategoryParameter]):
|
|
"""分类参数关联CRUD操作"""
|
|
|
|
def get_by_category(self, db: Session, category_id: str) -> List[BasCategoryParameter]:
|
|
"""获取分类的所有关联参数"""
|
|
return db.query(self.model).filter(
|
|
self.model.category_id == category_id,
|
|
self.model.is_del == 0
|
|
).order_by(self.model.sort_order.asc()).all()
|
|
|
|
def get_by_category_with_detail(self, db: Session, category_id: str) -> List[dict]:
|
|
"""获取分类的所有关联参数(带参数详情)"""
|
|
from app.models.product_parameter import BasProductParameter
|
|
|
|
results = db.query(
|
|
BasCategoryParameter,
|
|
BasProductParameter
|
|
).join(
|
|
BasProductParameter,
|
|
and_(
|
|
BasCategoryParameter.parameter_id == BasProductParameter.id,
|
|
BasProductParameter.is_del == 0
|
|
)
|
|
).filter(
|
|
BasCategoryParameter.category_id == category_id,
|
|
BasCategoryParameter.is_del == 0
|
|
).order_by(
|
|
BasCategoryParameter.sort_order.asc()
|
|
).all()
|
|
|
|
return [
|
|
{
|
|
"id": cp.id,
|
|
"parameterId": cp.parameter_id,
|
|
"parameterName": pp.name,
|
|
"parameterCode": pp.code,
|
|
"paramType": pp.param_type,
|
|
"options": pp.options,
|
|
"placeholder": pp.placeholder,
|
|
"isRequired": cp.is_required,
|
|
"defaultValue": cp.default_value,
|
|
"sortOrder": cp.sort_order,
|
|
}
|
|
for cp, pp in results
|
|
]
|
|
|
|
def batch_save(
|
|
self,
|
|
db: Session,
|
|
category_id: str,
|
|
items: List[dict],
|
|
creator: dict = None
|
|
) -> List[BasCategoryParameter]:
|
|
"""批量保存分类参数关联"""
|
|
# 先获取要删除的记录ID
|
|
old_records = db.query(self.model).filter(
|
|
self.model.category_id == category_id,
|
|
self.model.is_del == 0
|
|
).all()
|
|
|
|
# 物理删除旧记录(释放唯一索引约束)
|
|
for record in old_records:
|
|
db.delete(record)
|
|
db.commit()
|
|
|
|
# 创建新的关联
|
|
saved_items = []
|
|
for idx, item in enumerate(items):
|
|
item_data = {
|
|
"category_id": category_id,
|
|
"parameter_id": item.get("parameter_id"),
|
|
"is_required": item.get("is_required", 0),
|
|
"default_value": item.get("default_value"),
|
|
"sort_order": item.get("sort_order", idx),
|
|
}
|
|
cp = self.create(db, item_data, creator)
|
|
saved_items.append(cp)
|
|
|
|
db.commit()
|
|
return saved_items
|
|
|
|
def delete_by_category(self, db: Session, category_id: str) -> int:
|
|
"""删除分类的所有关联"""
|
|
count = db.query(self.model).filter(
|
|
self.model.category_id == category_id,
|
|
self.model.is_del == 0
|
|
).update({"is_del": 1})
|
|
db.commit()
|
|
return count
|
|
|
|
def exists_link(self, db: Session, category_id: str, parameter_id: str) -> bool:
|
|
"""检查是否已有关联"""
|
|
return db.query(self.model).filter(
|
|
self.model.category_id == category_id,
|
|
self.model.parameter_id == parameter_id,
|
|
self.model.is_del == 0
|
|
).first() is not None
|
|
|
|
|
|
class CategoryParameterHisCRUD(HistoryCRUD[BasCategoryParameterHis]):
|
|
"""分类参数关联历史CRUD操作"""
|
|
pass
|
|
|
|
|
|
category_parameter = CategoryParameterCRUD(BasCategoryParameter)
|
|
category_parameter_his = CategoryParameterHisCRUD(BasCategoryParameterHis)
|