|
@@ -0,0 +1,258 @@
|
|
|
+# from fastapi import APIRouter, HTTPException, Depends, Body,Query
|
|
|
+from fastapi import APIRouter, Request, Depends, HTTPException, Query, BackgroundTasks
|
|
|
+from fastapi.responses import JSONResponse
|
|
|
+import os
|
|
|
+# from sqlalchemy.orm import Session
|
|
|
+from sqlalchemy.orm import Session, joinedload
|
|
|
+import xlrd
|
|
|
+from database import get_db, get_db_share
|
|
|
+from models import *
|
|
|
+from typing import List, Optional
|
|
|
+from pydantic import BaseModel,Extra, Field
|
|
|
+import uuid
|
|
|
+from common.security import valid_access_token
|
|
|
+from pydantic import BaseModel
|
|
|
+from exceptions import AppException, HmacException
|
|
|
+from common.security import valid_access_token
|
|
|
+import traceback
|
|
|
+from utils import *
|
|
|
+from common.auth_user import *
|
|
|
+from common.db import db_czrz
|
|
|
+from sqlalchemy import create_engine, Column, Integer, String, Boolean, MetaData, Table, \
|
|
|
+ inspect, exists,or_,text,insert,asc,desc
|
|
|
+
|
|
|
+# 目录在文档上传接口写死
|
|
|
+UPLOAD_mergefile_PATH = '/data/upload/mergefile'
|
|
|
+
|
|
|
+router = APIRouter()
|
|
|
+
|
|
|
+
|
|
|
+#避难场所接口
|
|
|
+class ShelterSchema(BaseModel):
|
|
|
+ id: Optional[int] = None
|
|
|
+ data_id: Optional[str] = None
|
|
|
+ admin_area: Optional[str] = None
|
|
|
+ full_name: Optional[str] = None
|
|
|
+ address: Optional[str] = None
|
|
|
+ incident_type: Optional[str] = None
|
|
|
+ shelter_type: Optional[str] = None
|
|
|
+ total_area: Optional[float] = None
|
|
|
+ indoor_area: Optional[float] = None
|
|
|
+ capacity: Optional[float] = None
|
|
|
+ supplies: Optional[str] = None
|
|
|
+ facilities: Optional[str] = None
|
|
|
+ is_delete: Optional[bool] = False
|
|
|
+
|
|
|
+ duration_type: Optional[str] = None
|
|
|
+ space_type: Optional[str] = None
|
|
|
+ function_type: Optional[str] = None
|
|
|
+ construct_type: Optional[str] = None
|
|
|
+ sponsor: Optional[str] = None
|
|
|
+ construction_unit: Optional[str] = None
|
|
|
+ completion_time: Optional[str] = None
|
|
|
+ planning_situation: Optional[str] = None
|
|
|
+ total_investment: Optional[str] = None
|
|
|
+ standard_name: Optional[str] = None
|
|
|
+ management_number: Optional[str] = None
|
|
|
+ full_number: Optional[str] = None
|
|
|
+ property_unit: Optional[str] = None
|
|
|
+ transportation_investment: Optional[str] = None
|
|
|
+ channel: Optional[str] = None
|
|
|
+ identification_department: Optional[str] = None
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+
|
|
|
+class ShelterListSchema(BaseModel):
|
|
|
+ shelters: List[ShelterSchema] = Field(default_factory=list)
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ orm_mode = True
|
|
|
+
|
|
|
+
|
|
|
+# 创建
|
|
|
+@router.post("/add")
|
|
|
+def create_shelters(shelter_list_data: ShelterListSchema, db: Session = Depends(get_db)):
|
|
|
+ shelters = shelter_list_data.shelters
|
|
|
+ if not shelters:
|
|
|
+ raise HTTPException(status_code=400, detail="避难场所列表不能为空")
|
|
|
+ try:
|
|
|
+ new_shelters = []
|
|
|
+ for shelter_data in shelters:
|
|
|
+ # new_shelter = Shelter(**shelter_data.dict(exclude_none=True))
|
|
|
+ # new_shelters.append(new_shelter)
|
|
|
+ shelter_data = shelter_data.dict(exclude_none=True)
|
|
|
+ n_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
+ shelter_data["created_time"] = n_time
|
|
|
+ shelter_data["data_id"] = uuid.uuid1()
|
|
|
+
|
|
|
+ new_unit = Shelter(**shelter_data)
|
|
|
+ db.add(new_unit)
|
|
|
+ new_shelters.append(new_unit)
|
|
|
+ db.add_all(new_shelters)
|
|
|
+ db.commit()
|
|
|
+ shelter_ids = [shelter.id for shelter in new_shelters]
|
|
|
+ return {"code": 200, "msg": "创建成功", "shelter_ids": shelter_ids}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+# 删除
|
|
|
+@router.delete("/delete/{shelter_id}")
|
|
|
+def delete_shelter(shelter_id: int, db: Session = Depends(get_db)):
|
|
|
+ shelter = db.query(Shelter).filter_by(id=shelter_id, is_delete=0).first()
|
|
|
+ if not shelter:
|
|
|
+ raise HTTPException(status_code=404, detail="避难场所不存在")
|
|
|
+ try:
|
|
|
+ shelter.is_delete = True
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "删除成功"}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+# 查询列表
|
|
|
+#
|
|
|
+
|
|
|
+@router.get("/list")
|
|
|
+def get_shelters(page: int = Query(default=1, gt=0),
|
|
|
+ pageSize: int = Query(default=10, gt=0), db: Session = Depends(get_db)):
|
|
|
+ data_query = db.query(Shelter).filter(Shelter.is_delete == False)
|
|
|
+ data_query = data_query.order_by(Shelter.created_time.desc())
|
|
|
+
|
|
|
+ total_count = data_query.count()
|
|
|
+ offset = (page - 1) * pageSize
|
|
|
+ shelters = data_query.offset(offset).limit(pageSize).all()
|
|
|
+ result_items = [shelter.to_dict() for shelter in shelters]
|
|
|
+ result = {
|
|
|
+ "code": 200,
|
|
|
+ 'msg': '查询成功',
|
|
|
+ 'total': total_count,
|
|
|
+ 'totalPages': (total_count + pageSize - 1) // pageSize,
|
|
|
+ 'page': page,
|
|
|
+ 'pageSize': pageSize,
|
|
|
+ 'data': result_items
|
|
|
+ }
|
|
|
+ return result
|
|
|
+
|
|
|
+# 查询详情
|
|
|
+@router.get("/detail/{shelter_id}")
|
|
|
+def get_shelter_by_id(shelter_id: int, db: Session = Depends(get_db)):
|
|
|
+ shelter = db.query(Shelter).filter_by(id=shelter_id, is_delete=0).first()
|
|
|
+ if not shelter:
|
|
|
+ raise HTTPException(status_code=404, detail="避难场所不存在或已被标记为删除")
|
|
|
+ return {"code": 200, "msg": "查询成功", "shelter": shelter.to_dict()}
|
|
|
+
|
|
|
+# 修改
|
|
|
+@router.put("/edit/{shelter_id}")
|
|
|
+def update_shelter(shelter_id: int, update_data: ShelterSchema, db: Session = Depends(get_db)):
|
|
|
+ shelter = db.query(Shelter).filter_by(id=shelter_id, is_delete=False).first()
|
|
|
+ if not shelter:
|
|
|
+ raise HTTPException(status_code=404, detail="避难场所不存在或已被标记为删除")
|
|
|
+ try:
|
|
|
+ for key, value in update_data.dict(exclude_none=True).items():
|
|
|
+ if key != 'id':
|
|
|
+ setattr(shelter, key, value)
|
|
|
+ shelter.modified_time = datetime.utcnow()
|
|
|
+ db.commit()
|
|
|
+ db.refresh(shelter)
|
|
|
+ return {"code": 200, "msg": "更新成功", "shelter": shelter.to_dict()}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ raise HTTPException(status_code=400, 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:
|
|
|
+ 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': '导入发生异常'
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|