person_service.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Person service for business logic operations."""
  2. from sqlalchemy.exc import IntegrityError
  3. from app import db
  4. from app.models.person import Person
  5. from app.utils.validators import is_valid_name
  6. class PersonService:
  7. """Service class for Person CRUD operations."""
  8. @staticmethod
  9. def create(name):
  10. """Create a new person.
  11. Args:
  12. name: Person's name
  13. Returns:
  14. Tuple of (person_dict, error_message)
  15. On success: (person_dict, None)
  16. On failure: (None, error_message)
  17. """
  18. if not is_valid_name(name):
  19. return None, "人员名称不能为空"
  20. # Check for duplicate name
  21. existing = Person.query.filter(Person.name == name.strip()).first()
  22. if existing:
  23. return None, "人员名称已存在"
  24. try:
  25. person = Person(name=name.strip())
  26. db.session.add(person)
  27. db.session.commit()
  28. return person.to_dict(), None
  29. except IntegrityError:
  30. db.session.rollback()
  31. return None, "人员名称已存在"
  32. @staticmethod
  33. def get_all():
  34. """Get all persons.
  35. Returns:
  36. List of person dictionaries
  37. """
  38. persons = Person.query.order_by(Person.id).all()
  39. return [p.to_dict() for p in persons]
  40. @staticmethod
  41. def get_by_id(person_id):
  42. """Get a person by ID.
  43. Args:
  44. person_id: Person's ID
  45. Returns:
  46. Tuple of (person_dict, error_message)
  47. On success: (person_dict, None)
  48. On failure: (None, error_message)
  49. """
  50. person = db.session.get(Person, person_id)
  51. if not person:
  52. return None, f"未找到ID为 {person_id} 的人员"
  53. return person.to_dict(), None
  54. @staticmethod
  55. def update(person_id, name):
  56. """Update a person's name.
  57. Args:
  58. person_id: Person's ID
  59. name: New name
  60. Returns:
  61. Tuple of (person_dict, error_message)
  62. On success: (person_dict, None)
  63. On failure: (None, error_message)
  64. """
  65. if not is_valid_name(name):
  66. return None, "人员名称不能为空"
  67. person = db.session.get(Person, person_id)
  68. if not person:
  69. return None, f"未找到ID为 {person_id} 的人员"
  70. # Check for duplicate name (excluding current person)
  71. existing = Person.query.filter(
  72. Person.name == name.strip(),
  73. Person.id != person_id
  74. ).first()
  75. if existing:
  76. return None, "人员名称已存在"
  77. try:
  78. person.name = name.strip()
  79. db.session.commit()
  80. return person.to_dict(), None
  81. except IntegrityError:
  82. db.session.rollback()
  83. return None, "人员名称已存在"
  84. @staticmethod
  85. def delete(person_id):
  86. """Delete a person.
  87. Args:
  88. person_id: Person's ID
  89. Returns:
  90. Tuple of (success, error_message)
  91. On success: (True, None)
  92. On failure: (False, error_message)
  93. """
  94. person = db.session.get(Person, person_id)
  95. if not person:
  96. return False, f"未找到ID为 {person_id} 的人员"
  97. # Check if person has associated work records
  98. if person.work_records.count() > 0:
  99. return False, "无法删除已有工作记录的人员"
  100. db.session.delete(person)
  101. db.session.commit()
  102. return True, None