2023-04-04 14:28:53 +08:00
|
|
|
import os.path
|
|
|
|
import time
|
|
|
|
|
2023-02-28 16:28:48 +08:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from sqlalchemy.orm import Session
|
2023-04-04 14:28:53 +08:00
|
|
|
from starlette.responses import FileResponse
|
2023-03-01 16:04:43 +08:00
|
|
|
|
2023-04-04 14:28:53 +08:00
|
|
|
from Models.DepartmentModel import Department
|
2023-03-06 14:48:41 +08:00
|
|
|
from Schemas.DailySchemas import DailyTypeEnum
|
2023-03-01 16:04:43 +08:00
|
|
|
from Schemas.UserSchemas import TokenData
|
2023-03-06 09:45:17 +08:00
|
|
|
from Utils.AuthUtils import token_data_depend, check_auth, registered_depend
|
2023-02-28 16:28:48 +08:00
|
|
|
from Models.DailyModel import Daily
|
|
|
|
from Utils.CrudUtils import auto_create_crud
|
2023-03-01 16:04:43 +08:00
|
|
|
import json
|
|
|
|
from Schemas import DailySchemas
|
|
|
|
from Crud import DailyCrud
|
2023-04-04 14:28:53 +08:00
|
|
|
from Utils.SqlAlchemyUtils import get_db, QueryParams, query_common
|
|
|
|
import pandas as pd
|
2023-02-28 16:28:48 +08:00
|
|
|
|
|
|
|
router = APIRouter(
|
|
|
|
tags=["日报"],
|
|
|
|
prefix="/api/daily/daily",
|
2023-03-06 09:45:17 +08:00
|
|
|
dependencies=[Depends(registered_depend)]
|
2023-02-28 16:28:48 +08:00
|
|
|
)
|
|
|
|
|
2023-03-01 16:04:43 +08:00
|
|
|
|
|
|
|
# crud = auto_create_crud(Daily, 'daily', "日报", auto_create_keys=['create_time', 'update_time', 'id'],
|
|
|
|
# array_keys=['department', 'post'])
|
|
|
|
# crud.mount(router)
|
|
|
|
|
|
|
|
|
2023-03-02 15:19:14 +08:00
|
|
|
@router.post("/daily_get", response_model=DailySchemas.DailyGetRes, summary="获取日报")
|
2023-03-01 16:04:43 +08:00
|
|
|
def daily_get(req: DailySchemas.DailyGetReq, db: Session = Depends(get_db),
|
2023-03-06 09:45:17 +08:00
|
|
|
token_data: TokenData = Depends(registered_depend)):
|
2023-03-17 15:30:40 +08:00
|
|
|
item = DailyCrud.daily_get(db, req.id)
|
|
|
|
if not item:
|
|
|
|
raise HTTPException(detail="未取到信息")
|
|
|
|
return DailySchemas.DailyGetRes(**item.to_dict())
|
2023-03-01 16:04:43 +08:00
|
|
|
|
|
|
|
|
2023-03-02 15:19:14 +08:00
|
|
|
@router.post("/daily_add", response_model=DailySchemas.DailyAddRes, summary="添加日报")
|
2023-03-01 16:04:43 +08:00
|
|
|
def daily_add(req: DailySchemas.DailyAddReq, db: Session = Depends(get_db),
|
|
|
|
token_data: TokenData = Depends(token_data_depend)):
|
2023-03-07 12:03:06 +08:00
|
|
|
if req.fill_user != token_data.email:
|
|
|
|
raise HTTPException(detail="填报人与email不符", status_code=305)
|
2023-03-01 16:04:43 +08:00
|
|
|
# 本部门填报权限
|
2023-03-09 16:33:39 +08:00
|
|
|
# print(token_data.department, token_data, "token_data.departmentxx")
|
2023-03-02 15:19:14 +08:00
|
|
|
if str(req.department) not in token_data.department and not check_auth(token_data.auth_data, ['7']):
|
2023-03-01 16:04:43 +08:00
|
|
|
raise HTTPException(detail="没有本部门填报权限", status_code=305)
|
2023-03-06 14:48:41 +08:00
|
|
|
new_daily = DailyCrud.daily_add(db, req.dict())
|
2023-03-02 15:19:14 +08:00
|
|
|
return DailySchemas.DailyAddRes(**new_daily.to_dict())
|
2023-03-01 16:04:43 +08:00
|
|
|
|
|
|
|
|
2023-03-02 15:19:14 +08:00
|
|
|
@router.post("/daily_change", response_model=DailySchemas.DailyChangeRes, summary="修改日报")
|
2023-03-01 16:04:43 +08:00
|
|
|
def daily_change(req: DailySchemas.DailyChangeReq, db: Session = Depends(get_db),
|
2023-03-06 09:45:17 +08:00
|
|
|
token_data: TokenData = Depends(registered_depend)):
|
2023-03-01 16:04:43 +08:00
|
|
|
# 本部门填报权限
|
2023-03-02 15:19:14 +08:00
|
|
|
if str(req.department) not in token_data.department and not check_auth(token_data.auth_data, ['7']):
|
2023-03-01 16:04:43 +08:00
|
|
|
raise HTTPException(detail="没有本部门填报权限", status_code=305)
|
2023-03-17 15:30:40 +08:00
|
|
|
new_daily = DailyCrud.daily_change(db, req.id, req.dict())
|
|
|
|
return DailySchemas.DailyChangeRes(**new_daily.to_dict())
|
2023-03-01 16:04:43 +08:00
|
|
|
|
|
|
|
|
2023-03-02 15:19:14 +08:00
|
|
|
@router.post("/daily_query", response_model=DailySchemas.DailyQueryRes, summary="查询日报")
|
|
|
|
def daily_query(req: DailySchemas.DailyQuery, db: Session = Depends(get_db),
|
2023-03-06 09:45:17 +08:00
|
|
|
token_data: TokenData = Depends(registered_depend)):
|
2023-03-09 16:33:39 +08:00
|
|
|
# print(req, "req")
|
2023-04-04 14:28:53 +08:00
|
|
|
# 董监高日报动态查看 = check_auth(token_data.auth_data, [1])
|
|
|
|
# 所有部门动态查看 = check_auth(token_data.auth_data, [2])
|
|
|
|
#
|
|
|
|
# if req.type == DailyTypeEnum.董监高日报:
|
|
|
|
# if not 董监高日报动态查看:
|
|
|
|
# raise HTTPException(detail="无董监高日报动态查看权限", status_code=305)
|
|
|
|
# if req.type == DailyTypeEnum.部门子公司日报:
|
|
|
|
# if not 所有部门动态查看:
|
|
|
|
# if not req.department:
|
|
|
|
# raise HTTPException(detail="无所有部门动态查看权限", status_code=305)
|
|
|
|
# if req.department not in [int(item) for item in token_data.department.split(',')]:
|
|
|
|
# raise HTTPException(detail="没有该部门动态查看权限", status_code=305)
|
2023-03-06 14:48:41 +08:00
|
|
|
# same_department = set([req.department]) < set([int(item) for item in token_data.department.split(',')])
|
|
|
|
# if same_department and not check_auth(token_data.auth_data, [3]):
|
|
|
|
# raise HTTPException(detail="没有本部门动态查看权限", status_code=305)
|
2023-03-01 16:04:43 +08:00
|
|
|
count, items = DailyCrud.daily_query(db, req)
|
|
|
|
items = [DailySchemas.DailyInfo(**item.to_dict()) for item in items]
|
|
|
|
return DailySchemas.DailyQueryRes(count=count, items=items)
|
|
|
|
|
|
|
|
|
2023-03-02 15:19:14 +08:00
|
|
|
@router.post("/daily_delete", response_model=DailySchemas.DailyDeleteRes, summary="删除日报")
|
2023-03-17 15:30:40 +08:00
|
|
|
def daily_delete(req: DailySchemas.DailyDeleteReq, db: Session = Depends(get_db), ):
|
|
|
|
DailyCrud.daily_delete(db, req.id)
|
2023-03-01 16:04:43 +08:00
|
|
|
return DailySchemas.DailyDeleteRes(msg="删除成功", state=1)
|
2023-04-03 17:04:48 +08:00
|
|
|
|
|
|
|
|
2023-04-04 14:28:53 +08:00
|
|
|
@router.post("/daily_export", summary="日报导出")
|
|
|
|
def daily_export(req: QueryParams, db: Session = Depends(get_db)):
|
|
|
|
count, query = query_common(db, Daily, req)
|
|
|
|
query = query.order_by(Daily.daily_time.desc())
|
|
|
|
department_name_dic = {item.id: item.name for item in db.query(Department).all()}
|
|
|
|
daily_items = []
|
|
|
|
for item in query:
|
|
|
|
daily_item = {"日报类型": item.type.name,
|
|
|
|
"填报人": item.user_info.name,
|
|
|
|
"部门": department_name_dic.get(item.department), "标题": item.title,
|
|
|
|
"内容": item.content, "填报时间": item.daily_time}
|
|
|
|
daily_items.append(daily_item)
|
|
|
|
temp_path = "static_data/daily_out_temp"
|
|
|
|
if not os.path.exists(temp_path):
|
|
|
|
os.makedirs(temp_path)
|
|
|
|
file_name = f"{time.strftime('%Y%m%d%H%M%S')}.xlsx"
|
|
|
|
file_path = f"{temp_path}/{file_name}"
|
|
|
|
pd.DataFrame(daily_items).to_excel(file_path, index=False)
|
|
|
|
return {"url": file_path}
|
|
|
|
# return FileResponse(
|
|
|
|
# path=file_path, headers={'Content-Disposition': f'attachment; filename="{file_name}"'}, filename=file_name)
|