|
@@ -21,6 +21,14 @@ from utils.resource_provision_util import *
|
|
|
from common.websocketManager import *
|
|
|
import json
|
|
|
import traceback
|
|
|
+import xlrd
|
|
|
+import os
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+# 目录在文档上传接口写死
|
|
|
+UPLOAD_mergefile_PATH = '/data/upload/mergefile'
|
|
|
+
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
@@ -261,3 +269,98 @@ async def delete_pattern(
|
|
|
except Exception as e:
|
|
|
traceback.print_exc()
|
|
|
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+
|
|
|
+# 导入
|
|
|
+@router.post('/import')
|
|
|
+async def import_doc(
|
|
|
+ request: Request,
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
+ user_id = Depends(valid_access_token)
|
|
|
+):
|
|
|
+ # print(body)
|
|
|
+
|
|
|
+ try:
|
|
|
+ filename = body['filename']
|
|
|
+ if len(filename) == 0:
|
|
|
+ raise Exception()
|
|
|
+
|
|
|
+ file = filename[0]
|
|
|
+ url = file['url']
|
|
|
+ file_path = f"{UPLOAD_mergefile_PATH}/uploads/{url}"
|
|
|
+ file_path = os.path.abspath(file_path)
|
|
|
+ print(file_path)
|
|
|
+
|
|
|
+ book = xlrd.open_workbook(file_path)
|
|
|
+ sheet = book.sheet_by_index(0)
|
|
|
+
|
|
|
+ data = []
|
|
|
+ '''
|
|
|
+ for i in range(9, sheet.nrows):
|
|
|
+
|
|
|
+ # 预案名称
|
|
|
+ plan_name = sheet.cell(i, 0).value
|
|
|
+
|
|
|
+ # 一级目录
|
|
|
+ title1 = sheet.cell(i, 1).value
|
|
|
+
|
|
|
+ # 二级目录
|
|
|
+ title2 = sheet.cell(i, 2).value
|
|
|
+
|
|
|
+ # 三级目录
|
|
|
+ title3 = sheet.cell(i, 3).value
|
|
|
+
|
|
|
+ # 正文
|
|
|
+ content = sheet.cell(i, 4).value
|
|
|
+
|
|
|
+ if len(plan_name) < 1 and len(title1) < 1 and len(title2) < 1 and len(title3) < 1 and len(content) < 1 :
|
|
|
+ break
|
|
|
+
|
|
|
+ data.append({
|
|
|
+ 'plan_name': plan_name,
|
|
|
+ 'title1': title1,
|
|
|
+ 'title2': title2,
|
|
|
+ 'title3': title3,
|
|
|
+ 'content': content,
|
|
|
+ })
|
|
|
+
|
|
|
+ if len(data) > 0:
|
|
|
+ db.query(EmergencyDoc).filter(EmergencyDoc.plan_id == plan_id).delete()
|
|
|
+ db.commit()
|
|
|
+
|
|
|
+ title1 = ''
|
|
|
+ content = ''
|
|
|
+ docs = []
|
|
|
+ for n in data:
|
|
|
+ if n['title1'] != '':
|
|
|
+ if len(docs) > 0:
|
|
|
+ add_doc_1(db, title1, content, docs, plan_id)
|
|
|
+
|
|
|
+ docs = []
|
|
|
+ title1 = n['title1']
|
|
|
+ content = n['content']
|
|
|
+ if n['title2'] != '':
|
|
|
+ docs.append(n)
|
|
|
+ continue
|
|
|
+
|
|
|
+ docs.append(n)
|
|
|
+
|
|
|
+ if len(docs) > 0:
|
|
|
+ add_doc_1(db, title1, content, docs, plan_id)
|
|
|
+ '''
|
|
|
+
|
|
|
+ return {
|
|
|
+ 'code': 200,
|
|
|
+ 'msg': '导入成功'
|
|
|
+ }
|
|
|
+ except Exception:
|
|
|
+ traceback.print_exc()
|
|
|
+ return {
|
|
|
+ 'code': 500,
|
|
|
+ 'msg': '导入发生异常'
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|