초기화
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
# customer package marker
|
||||
from .db_manager import (
|
||||
add_customer_to_db,
|
||||
update_customer_in_db,
|
||||
delete_customer_from_db,
|
||||
get_all_customers_from_db,
|
||||
get_customer_details_from_db,
|
||||
add_contact_to_db,
|
||||
update_contact_in_db,
|
||||
delete_contact_from_db,
|
||||
add_sales_activity_to_db,
|
||||
update_sales_activity_in_db,
|
||||
delete_sales_activity_from_db,
|
||||
get_customer_id_from_contact_id,
|
||||
get_customer_id_from_sales_activity_id
|
||||
)
|
||||
from .router import router
|
||||
@@ -0,0 +1,243 @@
|
||||
import mysql.connector
|
||||
from database.connection import get_db_connection
|
||||
import logging
|
||||
|
||||
# 고객사 추가
|
||||
def add_customer_to_db(customer_data):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
sql = """
|
||||
INSERT INTO customers (name_kr, name_en, business_registration_number, contact_number, address)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
"""
|
||||
cursor.execute(sql, (
|
||||
customer_data.get('name_kr'),
|
||||
customer_data.get('name_en'),
|
||||
customer_data.get('business_registration_number'),
|
||||
customer_data.get('contact_number'),
|
||||
customer_data.get('address')
|
||||
))
|
||||
conn.commit()
|
||||
return cursor.lastrowid
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error adding customer: {err}")
|
||||
return None
|
||||
|
||||
# 고객사 수정
|
||||
def update_customer_in_db(customer_data):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
sql = """
|
||||
UPDATE customers SET
|
||||
name_kr = %s,
|
||||
name_en = %s,
|
||||
business_registration_number = %s,
|
||||
contact_number = %s,
|
||||
address = %s
|
||||
WHERE id = %s
|
||||
"""
|
||||
cursor.execute(sql, (
|
||||
customer_data.get('name_kr'),
|
||||
customer_data.get('name_en'),
|
||||
customer_data.get('business_registration_number'),
|
||||
customer_data.get('contact_number'),
|
||||
customer_data.get('address'),
|
||||
customer_data.get('id')
|
||||
))
|
||||
conn.commit()
|
||||
return True
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error updating customer: {err}")
|
||||
return False
|
||||
|
||||
# 고객사 삭제
|
||||
def delete_customer_from_db(customer_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
cursor.execute("DELETE FROM sales_activities WHERE customer_id = %s", (customer_id,))
|
||||
cursor.execute("DELETE FROM contacts WHERE customer_id = %s", (customer_id,))
|
||||
cursor.execute("DELETE FROM customers WHERE id = %s", (customer_id,))
|
||||
conn.commit()
|
||||
return True
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error deleting customer: {err}")
|
||||
return False
|
||||
|
||||
# 모든 고객사 목록 조회
|
||||
def get_all_customers_from_db():
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
cursor.execute("SELECT id, name_kr, name_en, business_registration_number, contact_number, address FROM customers")
|
||||
return cursor.fetchall()
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error getting all customers: {err}")
|
||||
return []
|
||||
|
||||
# 특정 고객사 상세 정보 조회 (담당자, 영업활동 포함)
|
||||
def get_customer_details_from_db(customer_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
cursor.execute("SELECT * FROM customers WHERE id = %s", (customer_id,))
|
||||
customer_data = cursor.fetchone()
|
||||
if not customer_data:
|
||||
return None
|
||||
|
||||
cursor.execute("SELECT * FROM contacts WHERE customer_id = %s", (customer_id,))
|
||||
contacts_data = cursor.fetchall()
|
||||
|
||||
cursor.execute("SELECT * FROM sales_activities WHERE customer_id = %s", (customer_id,))
|
||||
sales_activities_data = cursor.fetchall()
|
||||
|
||||
return {
|
||||
"customer": customer_data,
|
||||
"contacts": contacts_data,
|
||||
"salesActivities": sales_activities_data
|
||||
}
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error getting customer details: {err}")
|
||||
return None
|
||||
|
||||
# 담당자 추가
|
||||
def add_contact_to_db(contact_data):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
sql = """
|
||||
INSERT INTO contacts (customer_id, name, email, phone_number, position, work_location)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
cursor.execute(sql, (
|
||||
contact_data.get('customer_id'),
|
||||
contact_data.get('name'),
|
||||
contact_data.get('email'),
|
||||
contact_data.get('phone_number'),
|
||||
contact_data.get('position'),
|
||||
contact_data.get('work_location')
|
||||
))
|
||||
conn.commit()
|
||||
return cursor.lastrowid
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error adding contact: {err}")
|
||||
return None
|
||||
|
||||
# 담당자 수정
|
||||
def update_contact_in_db(contact_data):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
sql = """
|
||||
UPDATE contacts SET
|
||||
name = %s, email = %s, phone_number = %s, position = %s, work_location = %s
|
||||
WHERE id = %s
|
||||
"""
|
||||
cursor.execute(sql, (
|
||||
contact_data.get('name'),
|
||||
contact_data.get('email'),
|
||||
contact_data.get('phone_number'),
|
||||
contact_data.get('position'),
|
||||
contact_data.get('work_location'),
|
||||
contact_data.get('id')
|
||||
))
|
||||
conn.commit()
|
||||
return True
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error updating contact: {err}")
|
||||
return False
|
||||
|
||||
# 담당자 삭제
|
||||
def delete_contact_from_db(contact_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
cursor.execute("DELETE FROM contacts WHERE id = %s", (contact_id,))
|
||||
conn.commit()
|
||||
return True
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error deleting contact: {err}")
|
||||
return False
|
||||
|
||||
# 담당자 ID로 고객사 ID 조회
|
||||
def get_customer_id_from_contact_id(contact_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
cursor.execute("SELECT customer_id FROM contacts WHERE id = %s", (contact_id,))
|
||||
result = cursor.fetchone()
|
||||
return result['customer_id'] if result else None
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error getting customer ID from contact: {err}")
|
||||
return None
|
||||
|
||||
# 영업 활동 추가
|
||||
def add_sales_activity_to_db(activity_data):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
sql = """
|
||||
INSERT INTO sales_activities (customer_id, contact_id, activity_date, activity_type, memo)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
"""
|
||||
cursor.execute(sql, (
|
||||
activity_data.get('customer_id'),
|
||||
activity_data.get('contact_id'),
|
||||
activity_data.get('activity_date'),
|
||||
activity_data.get('activity_type'),
|
||||
activity_data.get('memo')
|
||||
))
|
||||
conn.commit()
|
||||
return cursor.lastrowid
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error adding sales activity: {err}")
|
||||
return None
|
||||
|
||||
# 영업 활동 수정
|
||||
def update_sales_activity_in_db(activity_data):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
sql = """
|
||||
UPDATE sales_activities SET
|
||||
contact_id = %s, activity_date = %s, activity_type = %s, memo = %s
|
||||
WHERE id = %s
|
||||
"""
|
||||
cursor.execute(sql, (
|
||||
activity_data.get('contact_id'),
|
||||
activity_data.get('activity_date'),
|
||||
activity_data.get('activity_type'),
|
||||
activity_data.get('memo'),
|
||||
activity_data.get('id')
|
||||
))
|
||||
conn.commit()
|
||||
return True
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error updating sales activity: {err}")
|
||||
return False
|
||||
|
||||
# 영업 활동 삭제
|
||||
def delete_sales_activity_from_db(activity_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
cursor.execute("DELETE FROM sales_activities WHERE id = %s", (activity_id,))
|
||||
conn.commit()
|
||||
return True
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error deleting sales activity: {err}")
|
||||
return False
|
||||
|
||||
# 영업 활동 ID로 고객사 ID 조회
|
||||
def get_customer_id_from_sales_activity_id(activity_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
with conn.cursor(dictionary=True) as cursor:
|
||||
cursor.execute("SELECT customer_id FROM sales_activities WHERE id = %s", (activity_id,))
|
||||
result = cursor.fetchone()
|
||||
return result['customer_id'] if result else None
|
||||
except mysql.connector.Error as err:
|
||||
logging.error(f"Error getting customer ID from sales activity: {err}")
|
||||
return None
|
||||
@@ -0,0 +1,127 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import List, Optional
|
||||
from .db_manager import (
|
||||
add_customer_to_db,
|
||||
update_customer_in_db,
|
||||
delete_customer_from_db,
|
||||
get_all_customers_from_db,
|
||||
get_customer_details_from_db,
|
||||
add_contact_to_db,
|
||||
update_contact_in_db,
|
||||
delete_contact_from_db,
|
||||
add_sales_activity_to_db,
|
||||
update_sales_activity_in_db,
|
||||
delete_sales_activity_from_db
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/customers", tags=["customers"])
|
||||
|
||||
# --- Pydantic Schemas ---
|
||||
class CustomerSchema(BaseModel):
|
||||
id: Optional[int] = None
|
||||
name_kr: str
|
||||
name_en: Optional[str] = None
|
||||
business_registration_number: Optional[str] = None
|
||||
contact_number: Optional[str] = None
|
||||
address: Optional[str] = None
|
||||
|
||||
class ContactSchema(BaseModel):
|
||||
id: Optional[int] = None
|
||||
customer_id: int
|
||||
name: str
|
||||
email: Optional[str] = None
|
||||
phone_number: Optional[str] = None
|
||||
position: Optional[str] = None
|
||||
work_location: Optional[str] = None
|
||||
|
||||
class SalesActivitySchema(BaseModel):
|
||||
id: Optional[int] = None
|
||||
customer_id: int
|
||||
contact_id: Optional[int] = None
|
||||
activity_date: str # YYYY-MM-DD
|
||||
activity_type: str
|
||||
memo: Optional[str] = None
|
||||
|
||||
# --- Customers APIs ---
|
||||
@router.post("/", response_model=dict)
|
||||
def create_customer(customer: CustomerSchema):
|
||||
result = add_customer_to_db(customer.dict())
|
||||
if result is None:
|
||||
raise HTTPException(status_code=500, detail="Failed to create customer")
|
||||
return {"id": result, "status": "success"}
|
||||
|
||||
@router.put("/{customer_id}", response_model=dict)
|
||||
def update_customer(customer_id: int, customer: CustomerSchema):
|
||||
data = customer.dict()
|
||||
data['id'] = customer_id
|
||||
success = update_customer_in_db(data)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to update customer")
|
||||
return {"status": "success"}
|
||||
|
||||
@router.delete("/{customer_id}", response_model=dict)
|
||||
def delete_customer(customer_id: int):
|
||||
success = delete_customer_from_db(customer_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to delete customer")
|
||||
return {"status": "success"}
|
||||
|
||||
@router.get("/", response_model=List[CustomerSchema])
|
||||
def list_customers():
|
||||
return get_all_customers_from_db()
|
||||
|
||||
@router.get("/{customer_id}", response_model=dict)
|
||||
def get_customer_details(customer_id: int):
|
||||
details = get_customer_details_from_db(customer_id)
|
||||
if not details:
|
||||
raise HTTPException(status_code=404, detail="Customer not found")
|
||||
return details
|
||||
|
||||
# --- Contacts APIs ---
|
||||
@router.post("/contacts", response_model=dict)
|
||||
def create_contact(contact: ContactSchema):
|
||||
result = add_contact_to_db(contact.dict())
|
||||
if result is None:
|
||||
raise HTTPException(status_code=500, detail="Failed to create contact")
|
||||
return {"id": result, "status": "success"}
|
||||
|
||||
@router.put("/contacts/{contact_id}", response_model=dict)
|
||||
def update_contact(contact_id: int, contact: ContactSchema):
|
||||
data = contact.dict()
|
||||
data['id'] = contact_id
|
||||
success = update_contact_in_db(data)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to update contact")
|
||||
return {"status": "success"}
|
||||
|
||||
@router.delete("/contacts/{contact_id}", response_model=dict)
|
||||
def delete_contact(contact_id: int):
|
||||
success = delete_contact_from_db(contact_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to delete contact")
|
||||
return {"status": "success"}
|
||||
|
||||
# --- Sales Activities APIs ---
|
||||
@router.post("/activities", response_model=dict)
|
||||
def create_sales_activity(activity: SalesActivitySchema):
|
||||
result = add_sales_activity_to_db(activity.dict())
|
||||
if result is None:
|
||||
raise HTTPException(status_code=500, detail="Failed to create sales activity")
|
||||
return {"id": result, "status": "success"}
|
||||
|
||||
@router.put("/activities/{activity_id}", response_model=dict)
|
||||
def update_sales_activity(activity_id: int, activity: SalesActivitySchema):
|
||||
data = activity.dict()
|
||||
data['id'] = activity_id
|
||||
success = update_sales_activity_in_db(data)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to update sales activity")
|
||||
return {"status": "success"}
|
||||
|
||||
@router.delete("/activities/{activity_id}", response_model=dict)
|
||||
def delete_sales_activity(activity_id: int):
|
||||
success = delete_sales_activity_from_db(activity_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to delete sales activity")
|
||||
return {"status": "success"}
|
||||
Reference in New Issue
Block a user