2025-12-04 15:41:22 +07:00
|
|
|
import io
|
2025-12-10 09:57:16 +00:00
|
|
|
from filters import PhonePage
|
2025-12-09 14:10:13 +07:00
|
|
|
from flask import jsonify, request
|
|
|
|
|
from helpers import init_db_connection, CORS_HEADERS
|
2025-12-04 15:41:22 +07:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
def create_phone(user_id: str, data):
|
2025-12-04 15:41:22 +07:00
|
|
|
try:
|
2025-12-09 14:10:13 +07:00
|
|
|
conn = init_db_connection()
|
|
|
|
|
cursor = conn.cursor()
|
2025-12-10 09:57:16 +00:00
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
# Câu truy vấn SQL để thêm số điện thoại vào bảng UserPhone
|
|
|
|
|
query = """
|
2025-12-10 09:57:16 +00:00
|
|
|
INSERT INTO ailbl_user_phone (user_id, phone_number, prefix, area_code, created, modified)
|
|
|
|
|
VALUES (%s, %s, %s,%s, NOW(), NOW())
|
2025-12-09 14:10:13 +07:00
|
|
|
"""
|
|
|
|
|
# Lấy các trường từ data
|
|
|
|
|
phone_number = data.get("phone_number")
|
2025-12-10 09:57:16 +00:00
|
|
|
# Nếu không có prefix, có thể để null
|
|
|
|
|
prefix = data.get("prefix", None)
|
|
|
|
|
area_code = data.get("area_code")
|
2025-12-09 14:10:13 +07:00
|
|
|
# Thực thi câu truy vấn SQL
|
2025-12-10 09:57:16 +00:00
|
|
|
cursor.execute(query, (user_id, phone_number, prefix, area_code))
|
2025-12-09 14:10:13 +07:00
|
|
|
conn.commit() # Lưu thay đổi vào cơ sở dữ liệu
|
2025-12-04 15:41:22 +07:00
|
|
|
|
2025-12-10 09:57:16 +00:00
|
|
|
return {
|
|
|
|
|
"user_id": user_id,
|
|
|
|
|
"phone_number": phone_number,
|
|
|
|
|
"prefix": prefix,
|
|
|
|
|
"area": area_code,
|
|
|
|
|
"status": "added"
|
|
|
|
|
}, 200, CORS_HEADERS
|
|
|
|
|
|
|
|
|
|
# result = cursor.fetchall()
|
|
|
|
|
# return {"message": "Phone created successfully"}, 200, CORS_HEADERS
|
2025-12-04 15:41:22 +07:00
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-12-10 09:57:16 +00:00
|
|
|
return {"error": str(e)}, 500, CORS_HEADERS
|
2025-12-04 15:41:22 +07:00
|
|
|
|
|
|
|
|
|
2025-12-10 09:57:16 +00:00
|
|
|
def filter_phone(user_id: str, paging):
|
|
|
|
|
conn = None # Khởi tạo conn với giá trị None
|
|
|
|
|
cursor = None
|
2025-12-04 15:41:22 +07:00
|
|
|
try:
|
2025-12-09 14:10:13 +07:00
|
|
|
conn = init_db_connection()
|
2025-12-10 09:57:16 +00:00
|
|
|
cursor = conn.cursor()
|
|
|
|
|
|
|
|
|
|
# Xay dung dieu kien loc
|
2025-12-09 14:10:13 +07:00
|
|
|
conditions = ["user_id = %(user_id)s"]
|
|
|
|
|
values = {"user_id": user_id} # Điều kiện cơ bản cho user_id
|
2025-12-10 09:57:16 +00:00
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
# Lọc theo phone_number
|
|
|
|
|
if paging.filter.phone_number:
|
|
|
|
|
conditions.append("LOWER(PhoneNumber) LIKE %(phone_number)s")
|
|
|
|
|
values["phone_number"] = f"%{paging.filter.phone_number.lower()}%"
|
2025-12-04 15:41:22 +07:00
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
# Lọc theo prefix
|
|
|
|
|
if paging.filter.prefix:
|
|
|
|
|
conditions.append("Prefix = %(prefix)s")
|
|
|
|
|
values["prefix"] = paging.filter.prefix
|
|
|
|
|
|
|
|
|
|
# Lọc theo ngày tạo
|
|
|
|
|
if paging.filter.created_from:
|
|
|
|
|
conditions.append("Created >= %(created_from)s")
|
|
|
|
|
values["created_from"] = paging.filter.created_from
|
2025-12-04 15:41:22 +07:00
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
if paging.filter.created_to:
|
|
|
|
|
conditions.append("Created <= %(created_to)s")
|
|
|
|
|
values["created_to"] = paging.filter.created_to
|
|
|
|
|
|
|
|
|
|
# Lọc theo ngày sửa đổi
|
|
|
|
|
if paging.filter.modified_from:
|
|
|
|
|
conditions.append("Modified >= %(modified_from)s")
|
|
|
|
|
values["modified_from"] = paging.filter.modified_from
|
|
|
|
|
|
|
|
|
|
if paging.filter.modified_to:
|
|
|
|
|
conditions.append("Modified <= %(modified_to)s")
|
|
|
|
|
values["modified_to"] = paging.filter.modified_to
|
2025-12-10 09:57:16 +00:00
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
# Ket hop dieu kien loc
|
2025-12-10 09:57:16 +00:00
|
|
|
where_clause = " AND ".join(conditions)
|
|
|
|
|
if where_clause:
|
|
|
|
|
where_clause = "WHERE " + where_clause
|
|
|
|
|
|
|
|
|
|
# Sap xep ket qua neu co:
|
|
|
|
|
order_clause = ""
|
|
|
|
|
if paging.sortby:
|
|
|
|
|
direction = "ASC" if paging.asc else "DESC"
|
2025-12-09 14:10:13 +07:00
|
|
|
order_clause = f"ORDER BY {paging.sortby} {direction}"
|
2025-12-10 09:57:16 +00:00
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
# Gop Truy van
|
|
|
|
|
sql = f"""
|
|
|
|
|
SELECT *, COUNT(*) OVER() AS total
|
|
|
|
|
FROM ailbl_user_phone
|
|
|
|
|
{where_clause}
|
|
|
|
|
{order_clause}
|
2025-12-10 09:57:16 +00:00
|
|
|
LIMIT %(limit)s OFFSET %(offset)s
|
|
|
|
|
"""
|
2025-12-09 14:10:13 +07:00
|
|
|
values["limit"] = paging.size
|
|
|
|
|
values["offset"] = paging.page * paging.size
|
2025-12-10 09:57:16 +00:00
|
|
|
|
|
|
|
|
cursor.execute(sql, values) # Thuc Thi Cau Truy Van
|
2025-12-09 14:10:13 +07:00
|
|
|
phones = cursor.fetchall()
|
2025-12-10 09:57:16 +00:00
|
|
|
|
|
|
|
|
# Chuyển kết quả thành danh sách các đối tượng với tên trường rõ ràng
|
|
|
|
|
phone_list = []
|
|
|
|
|
for phone in phones:
|
|
|
|
|
phone_dict = {
|
|
|
|
|
"id": phone[0],
|
|
|
|
|
"user_id": phone[1],
|
|
|
|
|
"phone_number": phone[2],
|
|
|
|
|
"prefix": phone[3],
|
|
|
|
|
"area_code": phone[4],
|
|
|
|
|
"created": phone[5],
|
|
|
|
|
"modified": phone[6]
|
|
|
|
|
}
|
|
|
|
|
phone_list.append(phone_dict)
|
|
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
return phones, 200, CORS_HEADERS
|
2025-12-10 09:57:16 +00:00
|
|
|
|
2025-12-04 15:41:22 +07:00
|
|
|
except Exception as e:
|
|
|
|
|
return {"error": str(e)}, 500
|
2025-12-10 09:57:16 +00:00
|
|
|
finally:
|
|
|
|
|
if conn:
|
2025-12-09 14:10:13 +07:00
|
|
|
conn.close()
|
2025-12-04 15:41:22 +07:00
|
|
|
|
|
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
def delete_phone(phone_id: str, user_id: str) -> dict:
|
2025-12-04 15:41:22 +07:00
|
|
|
try:
|
2025-12-09 14:10:13 +07:00
|
|
|
conn = init_db_connection()
|
|
|
|
|
cursor = conn.cursor()
|
2025-12-10 09:57:16 +00:00
|
|
|
|
2025-12-09 14:10:13 +07:00
|
|
|
query = "DELETE FROM ailbl_user_phone WHERE id = %s AND user_id = %s"
|
|
|
|
|
cursor.execute(query, (phone_id, user_id))
|
2025-12-10 09:57:16 +00:00
|
|
|
conn.commit() # Save DB
|
2025-12-09 14:10:13 +07:00
|
|
|
return {"message": "Phone deleted successfully"}, 200, CORS_HEADERS
|
2025-12-10 09:57:16 +00:00
|
|
|
|
2025-12-04 15:41:22 +07:00
|
|
|
except Exception as e:
|
|
|
|
|
return {"error": str(e)}, 500
|
2025-12-09 14:10:13 +07:00
|
|
|
|
|
|
|
|
|
2025-12-10 09:57:16 +00:00
|
|
|
def exists_phone(user_id: str, phone_number: str = None, phone_id: str = None):
|
2025-12-09 14:10:13 +07:00
|
|
|
try:
|
|
|
|
|
conn = init_db_connection()
|
|
|
|
|
cursor = conn.cursor()
|
2025-12-10 09:57:16 +00:00
|
|
|
|
|
|
|
|
if phone_number: # Nếu là `POST`, kiểm tra sự tồn tại của phone_number
|
|
|
|
|
cursor.execute("""
|
|
|
|
|
SELECT 1
|
|
|
|
|
FROM ailbl_user_phone
|
|
|
|
|
WHERE user_id = %s AND phone_number = %s
|
|
|
|
|
""", (user_id, phone_number))
|
|
|
|
|
|
|
|
|
|
elif phone_id: # Nếu là `DELETE`, kiểm tra sự tồn tại của phone_id
|
|
|
|
|
cursor.execute("""
|
|
|
|
|
SELECT 1
|
|
|
|
|
FROM ailbl_user_phone
|
|
|
|
|
WHERE id = %s AND user_id = %s
|
2025-12-09 14:10:13 +07:00
|
|
|
""", (phone_id, user_id))
|
2025-12-10 09:57:16 +00:00
|
|
|
|
|
|
|
|
row = cursor.fetchone() # Co ket qua thi tra ve du lieu 1 dong
|
|
|
|
|
# Nếu có dòng dữ liệu, trả về True (tồn tại số điện thoại), nếu không, trả về False
|
|
|
|
|
return row is not None
|
2025-12-09 14:10:13 +07:00
|
|
|
except Exception as e:
|
|
|
|
|
return False
|
|
|
|
|
finally:
|
2025-12-10 09:57:16 +00:00
|
|
|
if cursor:
|
|
|
|
|
cursor.close()
|
|
|
|
|
if conn:
|
|
|
|
|
conn.close() # Đảm bảo đóng kết nối sau khi xong
|