intent_records_service.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from typing import Sequence
  4. from app.call_center.crud.crud_intent_records import intent_records_dao
  5. from model.intent_records import IntentRecords
  6. from app.call_center.schema.intent_records import CreateIntentRecordsParam, UpdateIntentRecordsParam, \
  7. GetIntentRecordsByIdParam
  8. from common.exception import errors
  9. from database.db_mysql import async_db_session
  10. from sqlalchemy import Select
  11. class IntentRecordsService:
  12. @staticmethod
  13. async def get(*, pk: str) -> IntentRecords:
  14. async with async_db_session() as db:
  15. intent_records = await intent_records_dao.get(db, pk)
  16. if not intent_records:
  17. raise errors.NotFoundError(msg='record不存在')
  18. return intent_records
  19. @staticmethod
  20. async def get_select_by_id(*, obj: GetIntentRecordsByIdParam) -> IntentRecords | None:
  21. async with async_db_session.begin() as db:
  22. return await intent_records_dao.get(db, obj)
  23. @staticmethod
  24. async def get_select() -> Select:
  25. return await intent_records_dao.get_list()
  26. @staticmethod
  27. async def get_all() -> Sequence[IntentRecords]:
  28. async with async_db_session() as db:
  29. intent_records = await intent_records_dao.get_all(db)
  30. return intent_records
  31. @staticmethod
  32. async def create(*, obj: CreateIntentRecordsParam) -> None:
  33. async with async_db_session.begin() as db:
  34. await intent_records_dao.create(db, obj)
  35. @staticmethod
  36. async def update(*, pk: int, obj: UpdateIntentRecordsParam) -> int:
  37. async with async_db_session.begin() as db:
  38. count = await intent_records_dao.update(db, pk, obj)
  39. return count
  40. @staticmethod
  41. async def update_manual_intent(*, obj: UpdateIntentRecordsParam) -> int:
  42. async with async_db_session.begin() as db:
  43. count = await intent_records_dao.update_manual_intent(db, obj)
  44. return count
  45. @staticmethod
  46. async def delete(*, pk: list[int]) -> int:
  47. async with async_db_session.begin() as db:
  48. count = await intent_records_dao.delete(db, pk)
  49. return count
  50. intent_records_service: IntentRecordsService = IntentRecordsService()