contract_repo.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from sqlalchemy.ext.asyncio import AsyncSession
  2. from alien_store.db.models.contract_store import ContractStore
  3. class ContractRepository:
  4. """合同数据访问层"""
  5. def __init__(self, db: AsyncSession):
  6. self.db = db
  7. async def get_by_store_id(self, store_id: int):
  8. """根据店铺id查询所有合同"""
  9. result = await self.db.execute(
  10. ContractStore.__table__.select().where(ContractStore.store_id == store_id)
  11. )
  12. # 返回列表[dict],避免 Pydantic 序列化 Row 对象出错
  13. return [dict(row) for row in result.mappings().all()]
  14. async def create(self, user_data):
  15. """创建未签署合同模板"""
  16. db_templates = ContractStore(
  17. store_id=user_data.store_id,
  18. merchant_name=user_data.merchant_name,
  19. business_segment=user_data.business_segment,
  20. contact_phone=user_data.contact_phone,
  21. contract_url=user_data.contract_url,
  22. seal_url='0.0',
  23. )
  24. self.db.add(db_templates)
  25. await self.db.commit()
  26. await self.db.refresh(db_templates)
  27. return db_templates