Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, Depends, HTTPException, status, UploadFile, File, Form | |
| from sqlalchemy.orm import Session | |
| from typing import List, Optional | |
| from database import get_db | |
| from models.product_certification import ProductCertification | |
| from models.user import User | |
| from schemas.product import ( | |
| ProductCertificationResponse, | |
| ProductCertificationCreate, | |
| ProductCertificationUpdate | |
| ) | |
| from routers.auth import get_current_user | |
| from routers.product_utils import validate_product_exists, get_company_r2_config, delete_r2_and_local_files | |
| from config.certification_types import DEFAULT_CERTIFICATION_TYPES | |
| # 导入日志模块 | |
| from utils.logger import logger | |
| router = APIRouter() | |
| async def get_certification_types(): | |
| """获取商品认证类型列表""" | |
| logger.info("获取商品认证类型列表") | |
| return DEFAULT_CERTIFICATION_TYPES | |
| async def read_product_certifications( | |
| product_number: str, | |
| current_user: User = Depends(get_current_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| """获取商品认证列表""" | |
| logger.info(f"获取商品认证列表 - product_number: {product_number}, user: {current_user.username}") | |
| from routers.auth import is_admin_user | |
| query = db.query(ProductCertification).filter(ProductCertification.product_number == product_number) | |
| if not is_admin_user(current_user): | |
| query = query.filter(ProductCertification.company_code == current_user.company_code) | |
| certifications = query.all() | |
| logger.info(f"获取商品认证列表成功 - product_number: {product_number}, 共 {len(certifications)} 个认证") | |
| return certifications | |
| async def create_product_certification( | |
| product_number: str, | |
| certification_type: str = Form(...), | |
| file_url: str = Form(...), | |
| thumbnail_url: Optional[str] = Form(None), | |
| media_id: str = Form(...), | |
| current_user: User = Depends(get_current_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| """创建商品认证(通过素材选择器方式)""" | |
| logger.info(f"创建商品认证 - product_number: {product_number}, certification_type: {certification_type}, user: {current_user.username}") | |
| product = validate_product_exists(product_number, current_user, db) | |
| cert_data = { | |
| 'product_number': product_number, | |
| 'product_id': product.id, | |
| 'company_code': current_user.company_code, | |
| 'certification_type': certification_type, | |
| 'r2_url': file_url, | |
| 'thumbnail_r2_url': thumbnail_url or file_url, | |
| 'media_id': media_id | |
| } | |
| cert = ProductCertification(**cert_data) | |
| db.add(cert) | |
| db.commit() | |
| db.refresh(cert) | |
| logger.info(f"创建商品认证成功 - certification_id: {cert.id}, product_number: {product_number}") | |
| return cert | |
| async def batch_create_product_certification( | |
| product_number: str, | |
| cert_items: List[dict], | |
| current_user: User = Depends(get_current_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| """批量创建商品认证""" | |
| logger.info(f"批量创建商品认证 - product_number: {product_number}, 数量: {len(cert_items)}, user: {current_user.username}") | |
| product = validate_product_exists(product_number, current_user, db) | |
| created_certs = [] | |
| for item in cert_items: | |
| cert_data = { | |
| 'product_number': product_number, | |
| 'product_id': product.id, | |
| 'company_code': current_user.company_code, | |
| 'certification_type': item.get('certification_type'), | |
| 'r2_url': item.get('file_url'), | |
| 'thumbnail_r2_url': item.get('thumbnail_url') or item.get('file_url'), | |
| 'media_id': item.get('media_id') | |
| } | |
| cert = ProductCertification(**cert_data) | |
| db.add(cert) | |
| created_certs.append(cert) | |
| db.commit() | |
| for cert in created_certs: | |
| db.refresh(cert) | |
| logger.info(f"批量创建商品认证成功 - product_number: {product_number}, 成功创建 {len(created_certs)} 个认证") | |
| return created_certs | |
| async def update_product_certification( | |
| certification_id: int, | |
| cert_update: ProductCertificationUpdate, | |
| current_user: User = Depends(get_current_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| """更新商品认证""" | |
| logger.info(f"更新商品认证 - certification_id: {certification_id}, user: {current_user.username}") | |
| from routers.auth import is_admin_user | |
| query = db.query(ProductCertification).filter(ProductCertification.id == certification_id) | |
| if not is_admin_user(current_user): | |
| query = query.filter(ProductCertification.company_code == current_user.company_code) | |
| certification = query.first() | |
| if certification is None: | |
| logger.error(f"认证不存在 - certification_id: {certification_id}") | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Certification not found") | |
| update_data = cert_update.model_dump(exclude_unset=True) | |
| for field, value in update_data.items(): | |
| setattr(certification, field, value) | |
| db.commit() | |
| db.refresh(certification) | |
| logger.info(f"更新商品认证成功 - certification_id: {certification_id}") | |
| return certification | |
| async def delete_product_certification( | |
| certification_id: int, | |
| current_user: User = Depends(get_current_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| """删除商品认证""" | |
| logger.info(f"删除商品认证 - certification_id: {certification_id}, user: {current_user.username}") | |
| from routers.auth import is_admin_user | |
| query = db.query(ProductCertification).filter(ProductCertification.id == certification_id) | |
| if not is_admin_user(current_user): | |
| query = query.filter(ProductCertification.company_code == current_user.company_code) | |
| certification = query.first() | |
| if certification is None: | |
| logger.error(f"认证不存在 - certification_id: {certification_id}") | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Certification not found") | |
| db.delete(certification) | |
| db.commit() | |
| logger.info(f"删除商品认证成功 - certification_id: {certification_id}") | |
| return None | |