Update app.py
Browse files
app.py
CHANGED
|
@@ -715,57 +715,70 @@ async def cap_nhat_hoa_chat(request: Request):
|
|
| 715 |
status_code=500,
|
| 716 |
content={"error": str(e), "trace": traceback.format_exc()}
|
| 717 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
@app.post("/cap_nhat_hoa_chat")
|
| 719 |
async def cap_nhat_hoa_chat(request: Request):
|
| 720 |
try:
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
rows = data.get("rows", [])
|
| 724 |
|
| 725 |
-
|
|
|
|
|
|
|
|
|
|
| 726 |
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
if "ngày" in col.lower():
|
| 730 |
-
df[col] = pd.to_datetime(df[col], errors='coerce').dt.strftime("%Y-%m-%d")
|
| 731 |
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
|
|
|
|
|
|
| 735 |
|
| 736 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 738 |
-
|
|
|
|
| 739 |
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
|
| 749 |
-
return {"
|
| 750 |
|
| 751 |
except Exception as e:
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
|
|
|
| 755 |
|
| 756 |
-
def tinh_tinh_trang(han_sd_str):
|
| 757 |
-
try:
|
| 758 |
-
han_sd = pd.to_datetime(han_sd_str, errors='coerce')
|
| 759 |
-
if pd.isnull(han_sd):
|
| 760 |
-
return "Không xác định"
|
| 761 |
-
today = pd.Timestamp.today()
|
| 762 |
-
diff = (han_sd - today).days
|
| 763 |
-
if diff < 0:
|
| 764 |
-
return "HẾT HẠN"
|
| 765 |
-
elif diff <= 30:
|
| 766 |
-
return f"CÒN: {diff} ngày"
|
| 767 |
-
else:
|
| 768 |
-
return f"CÒN: {diff} ngày"
|
| 769 |
-
except:
|
| 770 |
-
return "Không xác định"
|
| 771 |
|
|
|
|
| 715 |
status_code=500,
|
| 716 |
content={"error": str(e), "trace": traceback.format_exc()}
|
| 717 |
)
|
| 718 |
+
def chuan_hoa_ngay(value):
|
| 719 |
+
if pd.isna(value):
|
| 720 |
+
return None
|
| 721 |
+
if isinstance(value, datetime):
|
| 722 |
+
return value
|
| 723 |
+
if isinstance(value, str):
|
| 724 |
+
for fmt in ("%d/%m/%Y", "%d-%m-%Y", "%Y-%m-%d", "%Y/%m/%d"):
|
| 725 |
+
try:
|
| 726 |
+
return datetime.strptime(value.strip(), fmt)
|
| 727 |
+
except:
|
| 728 |
+
continue
|
| 729 |
+
return None
|
| 730 |
+
|
| 731 |
@app.post("/cap_nhat_hoa_chat")
|
| 732 |
async def cap_nhat_hoa_chat(request: Request):
|
| 733 |
try:
|
| 734 |
+
du_lieu = await request.json()
|
| 735 |
+
df = pd.DataFrame(du_lieu["data"], columns=du_lieu["columns"])
|
|
|
|
| 736 |
|
| 737 |
+
# Tìm index các cột chính
|
| 738 |
+
cols = df.columns.str.upper()
|
| 739 |
+
idx_ngay_het_han = next((i for i, c in enumerate(cols) if "HẠN" in c and "NGÀY" in c), None)
|
| 740 |
+
idx_tinh_trang = next((i for i, c in enumerate(cols) if "TÌNH TRẠNG" in c), None)
|
| 741 |
|
| 742 |
+
if idx_ngay_het_han is None or idx_tinh_trang is None:
|
| 743 |
+
raise ValueError("Không tìm thấy cột 'NGÀY HẾT HẠN' hoặc 'TÌNH TRẠNG' trong dữ liệu.")
|
|
|
|
|
|
|
| 744 |
|
| 745 |
+
today = datetime.today()
|
| 746 |
+
for i, row in df.iterrows():
|
| 747 |
+
ngay_str = row[idx_ngay_het_han]
|
| 748 |
+
dt = chuan_hoa_ngay(ngay_str)
|
| 749 |
+
status = "KHÔNG RÕ"
|
| 750 |
|
| 751 |
+
if dt:
|
| 752 |
+
diff = (dt - today).days
|
| 753 |
+
if diff < 0:
|
| 754 |
+
status = "HẾT HẠN"
|
| 755 |
+
elif diff <= 30:
|
| 756 |
+
status = f"CÒN: {diff} NGÀY ⚠"
|
| 757 |
+
else:
|
| 758 |
+
status = f"CÒN: {diff} NGÀY"
|
| 759 |
+
|
| 760 |
+
df.iat[i, idx_tinh_trang] = status
|
| 761 |
+
|
| 762 |
+
# Ghi file Excel tạm
|
| 763 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 764 |
+
temp_path = tmp.name
|
| 765 |
+
df.to_excel(temp_path, sheet_name="HOA CHAT SU DUNG", index=False)
|
| 766 |
|
| 767 |
+
# Upload lên Hugging Face
|
| 768 |
+
upload_file(
|
| 769 |
+
path_or_fileobj=temp_path,
|
| 770 |
+
path_in_repo="Quan_ly_hoa_chat.xlsx",
|
| 771 |
+
repo_id="doluong6/Quatest1",
|
| 772 |
+
repo_type="dataset",
|
| 773 |
+
token=os.environ.get("HF_TOKEN"),
|
| 774 |
+
)
|
| 775 |
|
| 776 |
+
return JSONResponse(content={"message": "✅ Đã lưu thành công và tính tình trạng tự động."})
|
| 777 |
|
| 778 |
except Exception as e:
|
| 779 |
+
return JSONResponse(
|
| 780 |
+
status_code=500,
|
| 781 |
+
content={"error": str(e), "trace": traceback.format_exc()}
|
| 782 |
+
)
|
| 783 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 784 |
|