supplier_service.py 4.0 KB

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