142 lines
5.5 KiB
Python
142 lines
5.5 KiB
Python
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from backend.app.core.error_codes import ErrorCode
|
|
from backend.app.core.exceptions import AppException
|
|
from backend.app.repositories.supplier_repository import SupplierRepository
|
|
|
|
|
|
class SupplierService:
|
|
def __init__(self) -> None:
|
|
self.repository = SupplierRepository()
|
|
|
|
def list_suppliers(self, filters: dict | None = None, session: Session | None = None) -> dict:
|
|
if session is not None:
|
|
try:
|
|
suppliers = self.repository.list_suppliers(session, filters or {})
|
|
return {
|
|
"total": len(suppliers),
|
|
"page_no": 1,
|
|
"page_size": len(suppliers) or 20,
|
|
"list": [
|
|
{
|
|
"supplier_id": supplier.id,
|
|
"supplier_name": supplier.supplier_name,
|
|
"supplier_type": supplier.supplier_type,
|
|
"contact_name": supplier.contact_name,
|
|
"contact_mobile": supplier.contact_mobile,
|
|
"address": supplier.address,
|
|
"template_type": supplier.template_type,
|
|
"status": supplier.status,
|
|
"remark": supplier.remark,
|
|
}
|
|
for supplier in suppliers
|
|
],
|
|
}
|
|
except SQLAlchemyError:
|
|
pass
|
|
|
|
supplier_list = [
|
|
{
|
|
"supplier_id": 1001,
|
|
"supplier_name": "工厂A",
|
|
"supplier_type": "factory",
|
|
"contact_name": "王师傅",
|
|
"contact_mobile": "13800000001",
|
|
"template_type": "default",
|
|
"status": 1,
|
|
},
|
|
{
|
|
"supplier_id": 1002,
|
|
"supplier_name": "物流合作方B",
|
|
"supplier_type": "logistics",
|
|
"contact_name": "赵调度",
|
|
"contact_mobile": "13800000002",
|
|
"template_type": "default",
|
|
"status": 1,
|
|
},
|
|
]
|
|
|
|
if filters and filters.get("supplier_type"):
|
|
supplier_list = [item for item in supplier_list if item["supplier_type"] == filters["supplier_type"]]
|
|
|
|
return {
|
|
"total": len(supplier_list),
|
|
"page_no": 1,
|
|
"page_size": 20,
|
|
"list": supplier_list,
|
|
}
|
|
|
|
def create_supplier(self, payload: dict, session: Session | None = None) -> dict:
|
|
if session is not None:
|
|
try:
|
|
if not payload["supplier_name"].strip():
|
|
raise AppException(code=ErrorCode.PARAM_ERROR, message="供应商名称不能为空", status_code=400)
|
|
if not payload["supplier_type"].strip():
|
|
raise AppException(code=ErrorCode.PARAM_ERROR, message="供应商类型不能为空", status_code=400)
|
|
existed = self.repository.get_supplier_by_name_and_type(
|
|
session,
|
|
payload["supplier_name"],
|
|
payload["supplier_type"],
|
|
)
|
|
if existed is not None:
|
|
raise AppException(code=ErrorCode.DUPLICATE, message="供应商已存在", status_code=400)
|
|
|
|
supplier = self.repository.create_supplier(session, payload)
|
|
session.commit()
|
|
return {
|
|
"supplier_id": supplier.id,
|
|
"supplier_name": supplier.supplier_name,
|
|
"supplier_type": supplier.supplier_type,
|
|
"status": supplier.status,
|
|
}
|
|
except AppException:
|
|
session.rollback()
|
|
raise
|
|
except SQLAlchemyError:
|
|
session.rollback()
|
|
|
|
return {
|
|
"supplier_id": 1003,
|
|
"supplier_name": payload["supplier_name"],
|
|
"supplier_type": payload["supplier_type"],
|
|
"status": payload.get("status", 1),
|
|
}
|
|
|
|
def get_supplier(self, supplier_id: int, session: Session | None = None) -> dict:
|
|
if session is not None:
|
|
try:
|
|
supplier = self.repository.get_supplier(session, supplier_id)
|
|
if supplier is not None:
|
|
return {
|
|
"supplier_id": supplier.id,
|
|
"supplier_name": supplier.supplier_name,
|
|
"supplier_type": supplier.supplier_type,
|
|
"contact_name": supplier.contact_name,
|
|
"contact_mobile": supplier.contact_mobile,
|
|
"address": supplier.address,
|
|
"template_type": supplier.template_type,
|
|
"status": supplier.status,
|
|
"remark": supplier.remark,
|
|
}
|
|
raise AppException(code=ErrorCode.NOT_FOUND, message="供应商不存在", status_code=404)
|
|
except AppException:
|
|
raise
|
|
except SQLAlchemyError:
|
|
pass
|
|
|
|
return {
|
|
"supplier_id": supplier_id,
|
|
"supplier_name": "工厂A",
|
|
"supplier_type": "factory",
|
|
"contact_name": "王师傅",
|
|
"contact_mobile": "13800000001",
|
|
"address": "演示工厂地址",
|
|
"template_type": "default",
|
|
"status": 1,
|
|
"remark": "演示备注",
|
|
}
|
|
|
|
|
|
supplier_service = SupplierService()
|