118 lines
4.1 KiB
Python
118 lines
4.1 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.product_parameter_value import BasProductParameterValue, BasProductParameterValueHis
|
|
from app.schemas.product_parameter_value import ProductParameterValueCreate
|
|
|
|
|
|
class ProductParameterValueCRUD(CRUDBase[BasProductParameterValue]):
|
|
"""商品参数值CRUD操作"""
|
|
|
|
def get_by_product(self, db: Session, product_id: str) -> List[BasProductParameterValue]:
|
|
"""获取商品的所有参数值"""
|
|
return db.query(self.model).filter(
|
|
self.model.product_id == product_id,
|
|
self.model.is_del == 0
|
|
).order_by(self.model.sort_order.asc()).all()
|
|
|
|
def get_by_product_with_detail(self, db: Session, product_id: str) -> List[dict]:
|
|
"""获取商品的所有参数值(带参数详情)"""
|
|
from app.models.product_parameter import BasProductParameter
|
|
|
|
results = db.query(
|
|
BasProductParameterValue,
|
|
BasProductParameter
|
|
).join(
|
|
BasProductParameter,
|
|
and_(
|
|
BasProductParameterValue.parameter_id == BasProductParameter.id,
|
|
BasProductParameter.is_del == 0
|
|
)
|
|
).filter(
|
|
BasProductParameterValue.product_id == product_id,
|
|
BasProductParameterValue.is_del == 0
|
|
).order_by(
|
|
BasProductParameterValue.sort_order.asc()
|
|
).all()
|
|
|
|
return [
|
|
{
|
|
"id": ppv.id,
|
|
"product_id": ppv.product_id,
|
|
"parameter_id": ppv.parameter_id,
|
|
"parameter_name": pp.name,
|
|
"parameter_code": pp.code,
|
|
"param_type": pp.param_type,
|
|
"options": pp.options,
|
|
"placeholder": pp.placeholder,
|
|
"param_value": ppv.param_value,
|
|
"sort_order": ppv.sort_order,
|
|
}
|
|
for ppv, pp in results
|
|
]
|
|
|
|
def batch_save(
|
|
self,
|
|
db: Session,
|
|
product_id: str,
|
|
values: List[dict],
|
|
creator: dict = None
|
|
) -> List[BasProductParameterValue]:
|
|
"""批量保存商品参数值"""
|
|
from app.models.product_parameter import BasProductParameter
|
|
|
|
# 先删除原有的参数值
|
|
db.query(self.model).filter(
|
|
self.model.product_id == product_id,
|
|
self.model.is_del == 0
|
|
).update({"is_del": 1})
|
|
|
|
# 创建新的参数值
|
|
saved_values = []
|
|
for idx, value_item in enumerate(values):
|
|
parameter_id = value_item.get("parameter_id")
|
|
param_value = value_item.get("param_value")
|
|
|
|
# 获取参数信息
|
|
param = db.query(BasProductParameter).filter(
|
|
BasProductParameter.id == parameter_id,
|
|
BasProductParameter.is_del == 0
|
|
).first()
|
|
|
|
if param:
|
|
item_data = {
|
|
"product_id": product_id,
|
|
"parameter_id": parameter_id,
|
|
"parameter_name": param.name,
|
|
"parameter_code": param.code,
|
|
"param_type": param.param_type,
|
|
"param_value": param_value,
|
|
"sort_order": idx,
|
|
}
|
|
pv = self.create(db, item_data, creator)
|
|
saved_values.append(pv)
|
|
|
|
db.commit()
|
|
return saved_values
|
|
|
|
def delete_by_product(self, db: Session, product_id: str) -> int:
|
|
"""删除商品的所有参数值"""
|
|
count = db.query(self.model).filter(
|
|
self.model.product_id == product_id,
|
|
self.model.is_del == 0
|
|
).update({"is_del": 1})
|
|
db.commit()
|
|
return count
|
|
|
|
|
|
class ProductParameterValueHisCRUD(HistoryCRUD[BasProductParameterValueHis]):
|
|
"""商品参数值历史CRUD操作"""
|
|
pass
|
|
|
|
|
|
product_parameter_value = ProductParameterValueCRUD(BasProductParameterValue)
|
|
product_parameter_value_his = ProductParameterValueHisCRUD(BasProductParameterValueHis)
|