Browse Source

no message

libushang 9 months ago
parent
commit
24fa337bc9

+ 1 - 0
models/yjya_base.py

@@ -25,6 +25,7 @@ class EmergencyPlan(Base):
     create_by = Column(BigInteger, default=None, comment='创建者')
     del_flag = Column(String(1), default='0', comment='删除标志(0代表存在 2代表删除)')
     response_level = Column(String(2), comment='响应级别')
+    event_type = Column(String(10), comment='对应事件类型')
 
     class Config:
         orm_mode = True

+ 11 - 2
routers/api/emergencyPlans/__init__.py

@@ -38,6 +38,7 @@ class PlanCreateForm(BaseModel):
     publishDate: str
     organizingUnit : str
     document : str
+    event_type: str
     fileList : List[File]
 
 class PlanUpdateForm(BaseModel):
@@ -47,6 +48,7 @@ class PlanUpdateForm(BaseModel):
     publishDate: str = None
     organizingUnit : str = None
     document : str = None
+    event_type: str = None
     fileList : List[File] = []
 
 class DrillCreateForm(BaseModel):
@@ -199,7 +201,8 @@ async def get_emergency_plan_list(
                 "planType": plan.plan_type,
                 "organizingUnit": plan.organizing_unit,
                 "document": plan.document_number,
-                "publishDate": plan.publish_date.strftime('%Y-%m-%d')
+                "publishDate": plan.publish_date.strftime('%Y-%m-%d'),
+                "event_type": plan.event_type
             }
             for plan in emergency_plans
         ]
@@ -246,7 +249,8 @@ async def get_emergency_plan(
             "organizingUnit": emergency_plan.organizing_unit,
             "document": emergency_plan.document_number,
             "publishDate": emergency_plan.publish_date.strftime('%Y-%m-%d'),
-            "fileList": get_file_query_fun(from_scenario='emergencyPlans_plan',foreign_key=emergency_plan.plan_id,db=db)
+            "fileList": get_file_query_fun(from_scenario='emergencyPlans_plan',foreign_key=emergency_plan.plan_id,db=db),
+            "event_type": emergency_plan.event_type
         }
 
         # 返回结果
@@ -279,6 +283,7 @@ async def create_emergency_plan(
         publish_date = form_data.publishDate  # 如果没有提供发布日期,则使用当前时间
         organizing_unit = form_data.organizingUnit  # 使用用户位置作为编制单位
         document_number = form_data.document
+        event_type = form.event_type
 
         # 创建新的预案记录
         new_plan = EmergencyPlan(
@@ -289,6 +294,7 @@ async def create_emergency_plan(
             publish_date=publish_date,
             organizing_unit=organizing_unit,
             document_number=document_number,
+            event_type=event_type,
             create_by = user_id
         )
 
@@ -344,6 +350,9 @@ async def update_emergency_plan(
 
         if form_data.document:
             plan.document_number = form_data.document
+        
+        if form_data.event_type:
+            plan.event_type = form_data.event_type
 
         if len(form_data.fileList)>0:
             delete_file_fun(from_scenario='emergencyPlans_plan',foreign_key=plan.plan_id,db=db)

+ 92 - 5
routers/api/emergencyPlans/doc.py

@@ -7,6 +7,7 @@ from sqlalchemy import text, exists, and_, or_, not_
 from sqlalchemy.orm import Session
 from models import *
 import json
+import os
 import random
 from sqlalchemy import create_engine, select
 from typing import Optional
@@ -16,9 +17,13 @@ from common.security import valid_access_token
 import traceback
 from utils import *
 from datetime import datetime, timedelta
+import xlrd
 
 router = APIRouter()
 
+# 目录在文档上传接口写死
+UPLOAD_mergefile_PATH = '/data/upload/mergefile'
+
 # 获取预案结构化文档
 @router.post('/detail')
 async def doc(
@@ -73,9 +78,91 @@ async def import_doc(
     user_id = Depends(valid_access_token)
 ):
     print(body)
-    time.sleep(3)
+    
+    try:
+        plan_id = body['plan_id']
+        filename = body['filename']
+        if len(filename) == 0:
+            raise Exception()
+        
 
-    return {
-        'code': 200,
-        'msg': '导入成功'
-    }
+        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
+
+            # 正文
+            content = sheet.cell(i, 3).value
+
+            if len(plan_name) < 1 and len(title1) < 1 and len(title2) < 1 and len(content) < 1 :
+                break
+
+            data.append({
+                'plan_name': plan_name,
+                'title1': title1,
+                'title2': title2,
+                '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(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(db, title1, content, docs, plan_id)
+
+        return {
+            'code': 200,
+            'msg': '导入成功'
+        }
+    except Exception:
+        traceback.print_exc()
+        return {
+            'code': 500,
+            'msg': '导入发生异常'
+        }
+    
+def add_doc(db: Session, title: str, content: str, docs: list, plan_id: str):
+    main_entity = EmergencyDoc(title = title, value= content, pid = 0, plan_id = plan_id)
+    db.add(main_entity)
+    db.commit()
+    db.refresh(main_entity)
+    main_id = main_entity.id
+
+    for n in docs:
+        sub_entity = EmergencyDoc(title = n['title2'], value= n['content'], pid = main_id, plan_id = plan_id)
+        db.add(sub_entity)
+        db.commit()

+ 71 - 1
routers/api/emergencyPlans/unit.py

@@ -16,11 +16,15 @@ import json
 from common.db import db_dept
 import traceback
 from pprint import pprint
+import xlrd
+import os
 
 from . import unit
 
-router = APIRouter()
+# 目录在文档上传接口写死
+UPLOAD_mergefile_PATH = '/data/upload/mergefile'
 
+router = APIRouter()
 
 @router.get('/list')
 async def get_emergency_unit_list(
@@ -192,3 +196,69 @@ async def get_emergency_unit(
         traceback.print_exc()
         # 处理异常
         raise HTTPException(status_code=500, detail=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:
+        plan_id = body['plan_id']
+        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(4, sheet.nrows):
+            c1 = sheet.cell(i, 1).value
+            c2 = sheet.cell(i, 2).value
+            
+            data.append({'dept_name': c1, 'content': c2})
+
+        '''
+        for n in data:
+            row = db.query(SysDept).filter(SysDept.dept_name == n['dept_name']).first()
+            if row is None:
+                raise Exception('单位名称不正确')
+        '''
+            
+        if len(data) > 0:
+            db.query(EmergencyUnit).filter(EmergencyUnit.plan_id == plan_id).delete()
+            db.commit()
+
+        i = 0
+        for n in data:
+            dept_id = 100
+            row = db.query(SysDept).filter(SysDept.dept_name == n['dept_name']).first()
+            if row is not None:
+                dept_id = row.dept_id
+
+            main_entity = EmergencyUnit(dept_id = dept_id, dept_name = n['dept_name'], content = n['content'], dept_order = i, plan_id = plan_id)
+            db.add(main_entity)
+            db.commit()
+            i = i + 1
+
+        return {
+            'code': 200,
+            'msg': '导入成功'
+        }
+    except Exception as e:
+        traceback.print_exc()
+        return {
+            'code': 500,
+            'msg': '导入发生异常:'+str(e)
+        }