libushang hai 6 meses
pai
achega
2ee41f124e
Modificáronse 1 ficheiros con 17 adicións e 19 borrados
  1. 17 19
      routers/api/emergencyPlans/contact.py

+ 17 - 19
routers/api/emergencyPlans/contact.py

@@ -19,6 +19,7 @@ from utils import *
 from datetime import datetime, timedelta
 import pandas as pd
 from common.db import db_dept
+from exceptions import AppException
 
 router = APIRouter()
 
@@ -309,10 +310,8 @@ async def create_contact(
         try:
             df = pd.read_excel(file_path, engine='openpyxl', header=0)
         except Exception as e:
-            return JSONResponse(status_code=404, content={
-                'errcode': 501,
-                'errmsg': f"读取Excel文件时出错: {e}"
-            })
+            raise AppException(500, "请按模板上传!")
+        
         # 读取Excel文件
         # try:
         #     df = pd.read_excel(file_path, engine='openpyxl', header=0)
@@ -324,26 +323,20 @@ async def create_contact(
         # print(first_two_rows)
         # 检查列名是否匹配
         actual_columns = first_two_rows.columns.tolist()
-        print(actual_columns)
+        # print('actual_columns', actual_columns)
         missing_columns = [col for col in expected_columns.keys() if col not in actual_columns]
         
         if len(missing_columns) > 0:
-            return JSONResponse(status_code=404, content={
-                'errcode': 502,
-                'errmsg': "请按模板上传."
-            })
+            raise AppException(500, "请按模板上传")
         
         head1 = df.head(1)
         head1data = head1.to_dict(orient='records')[0]
         print(head1data)
-        if head1data['单位名称'] == '填写单位名称' and head1data['联系人'] == '填写单位职责' and head1data['职务'] == '填写联系人职务' and \
+        if head1data['单位名称'] == '填写单位名称' and head1data['联系人'] == '填写单位联系人' and head1data['职务'] == '填写联系人职务' and \
                 head1data['粤政易手机号码'] == '填写联系人的粤政易注册手机号码':
             df = df.drop(index=0)
         else:
-            return JSONResponse(status_code=404, content={
-                'errcode': 502,
-                'errmsg': "请按模板上传!"
-            })
+            raise AppException(500, "请按模板上传。")
 
         # 检查前两行的字段长度
         for index, row in first_two_rows.iterrows():
@@ -351,10 +344,8 @@ async def create_contact(
                 # if pd.isna(row[col]):
                 #     return f"第{index + 1}行的'{col}'字段不能为空。"
                 if pd.notna(row[col]) and len(str(row[col])) > max_length:
-                    return JSONResponse(status_code=404, content={
-                'errcode': 502,
-                'errmsg': f"第{index + 1}行的'{col}'字段长度超过{max_length}字符。"
-            })
+                    raise AppException(500, f"第{index + 1}行的'{col}'字段长度超过{max_length}字符。")
+                
 
         data = df.to_dict(orient='records')
         # print(data)
@@ -372,7 +363,7 @@ async def create_contact(
         for contact in infos:
             unit_id = db_dept.get_dept_id_by_name(db, contact['单位名称'])
             if unit_id == '':
-                raise Exception('单位名称不正确')
+                raise AppException(500, "单位名称不正确")
         
             new_contact = EmergencyContactInfo(
                 unit_id=unit_id,
@@ -394,6 +385,13 @@ async def create_contact(
             "msg": "创建成功",
             "data": None
         }
+    
+    except AppException as e:
+        return {
+            "code": 500,
+            "msg": e.msg
+        }
+
     except Exception as e:
         traceback.print_exc()
         # 处理异常