|
@@ -0,0 +1,313 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+
|
|
|
+from fastapi import APIRouter, Request, Depends, Query, HTTPException, status,WebSocket,WebSocketDisconnect
|
|
|
+from common.security import valid_access_token,valid_websocket_token
|
|
|
+from fastapi.responses import JSONResponse
|
|
|
+from sqlalchemy.orm import Session
|
|
|
+from sqlalchemy.sql import func
|
|
|
+from common.auth_user import *
|
|
|
+from sqlalchemy import text
|
|
|
+from pydantic import BaseModel
|
|
|
+from common.BigDataCenterAPI import *
|
|
|
+from database import get_db
|
|
|
+from typing import List
|
|
|
+from models import *
|
|
|
+from utils import *
|
|
|
+from utils.spatial import *
|
|
|
+from utils.ry_system_util import *
|
|
|
+from utils.resource_provision_util import *
|
|
|
+from common.websocketManager import *
|
|
|
+import json
|
|
|
+import traceback
|
|
|
+
|
|
|
+router = APIRouter()
|
|
|
+
|
|
|
+
|
|
|
+@router.post("/create")
|
|
|
+async def create_pattern(
|
|
|
+ user_id=Depends(valid_access_token),
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
+ db: Session = Depends(get_db)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ dept_info = user_id_get_user_info(db,user_id)
|
|
|
+ new_declaration = ResourceProvisionMaterialType(
|
|
|
+ id = new_guid(),
|
|
|
+ declaration_amount=body['declaration_amount'],
|
|
|
+ declaration_person=user_id,
|
|
|
+ declaration_unit=dept_info.dept_id,
|
|
|
+ create_by = user_id
|
|
|
+ )
|
|
|
+ db.add(new_declaration)
|
|
|
+ for info in body['detail']:
|
|
|
+ new_detail = ResourceProvisionProcurementDeclarationDetail(
|
|
|
+ id=new_guid(),
|
|
|
+ serial_number=info['serial_number'],
|
|
|
+ declaration_id = new_declaration.id,
|
|
|
+ material_type = info['material_type'],
|
|
|
+ material_code = info['material_code'],
|
|
|
+ material_quantity = info['material_quantity'],
|
|
|
+ material_unit_price = info['material_unit_price'],
|
|
|
+ material_purpose = info['material_purpose'],
|
|
|
+ create_by = user_id
|
|
|
+ )
|
|
|
+ db.add(new_detail)
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "创建成功", "data": None}
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+@router.put("/update/{id}")
|
|
|
+async def update_pattern(
|
|
|
+ id :str ,
|
|
|
+ user_id=Depends(valid_access_token),
|
|
|
+ body=Depends(remove_xss_json),
|
|
|
+ db: Session = Depends(get_db)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ update_declaration = declaration_id_get_declaration_info(db,id)
|
|
|
+ if not update_declaration:
|
|
|
+ return JSONResponse(status_code=404,content={"code":404,"msg":"declaration not found"})
|
|
|
+ if str(update_declaration.declaration_person) != str(user_id):
|
|
|
+ return JSONResponse(status_code=404, content={"code": 404, "msg": "declaration not permission"})
|
|
|
+ update_declaration.declaration_amount = body['declaration_amount']
|
|
|
+ update_declaration.update_by = user_id
|
|
|
+
|
|
|
+ detail_list = []
|
|
|
+ for info in body['detail']:
|
|
|
+ if info['id']=='':
|
|
|
+ new_detail = ResourceProvisionProcurementDeclarationDetail(
|
|
|
+ id=new_guid(),
|
|
|
+ serial_number=info['serial_number'],
|
|
|
+ declaration_id=update_declaration.id,
|
|
|
+ material_type=info['material_type'],
|
|
|
+ material_code=info['material_code'],
|
|
|
+ material_quantity=info['material_quantity'],
|
|
|
+ material_unit_price=info['material_unit_price'],
|
|
|
+ material_purpose=info['material_purpose'],
|
|
|
+ create_by=user_id
|
|
|
+ )
|
|
|
+ db.add(new_detail)
|
|
|
+ detail_list.append(new_detail.id)
|
|
|
+ else:
|
|
|
+ detail = detail_id_get_declaration_detail_info(db,info['id'])
|
|
|
+ if detail is None:
|
|
|
+ new_detail = ResourceProvisionProcurementDeclarationDetail(
|
|
|
+ id=new_guid(),
|
|
|
+ serial_number=info['serial_number'],
|
|
|
+ declaration_id=update_declaration.id,
|
|
|
+ material_type=info['material_type'],
|
|
|
+ material_code=info['material_code'],
|
|
|
+ material_quantity=info['material_quantity'],
|
|
|
+ material_unit_price=info['material_unit_price'],
|
|
|
+ material_purpose=info['material_purpose'],
|
|
|
+ create_by=user_id
|
|
|
+ )
|
|
|
+ db.add(new_detail)
|
|
|
+ detail_list.append(new_detail.id)
|
|
|
+ else:
|
|
|
+ detail.serial_number = info['serial_number']
|
|
|
+ detail.material_type = info['material_type']
|
|
|
+ detail.material_code = info['material_code']
|
|
|
+ detail.material_quantity = info['material_quantity']
|
|
|
+ detail.material_unit_price = info['material_unit_price']
|
|
|
+ detail.material_purpose = info['material_purpose']
|
|
|
+ detail.update_by = user_id
|
|
|
+ detail_list.append(info['id'])
|
|
|
+ query = db.query(ResourceProvisionProcurementDeclarationDetail)
|
|
|
+ query = query.filter_by(declaration_id=id, del_flag='0').filter(~ResourceProvisionProcurementDeclarationDetail.id.in_(detail_list))
|
|
|
+ for i in query.all():
|
|
|
+ i.del_flag='2'
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "更新成功"}
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+@router.get("/info/{id}")
|
|
|
+async def get_pattern_info(
|
|
|
+ id: str,
|
|
|
+ db: Session = Depends(get_db)
|
|
|
+):
|
|
|
+ try:
|
|
|
+
|
|
|
+ info = declaration_id_get_declaration_info(db,id)
|
|
|
+ if not info:
|
|
|
+ return JSONResponse(status_code=404,content={"code":404,"msg":"type not found"})
|
|
|
+ dept_info = dept_id_get_dept_info(db,info.declaration_unit)
|
|
|
+ user_info = user_id_get_user_info(db,info.declaration_person)
|
|
|
+ review_info = declaration_id_get_declaration_review_info(db,id)
|
|
|
+ if review_info is None:
|
|
|
+ declaration_details = info.declaration_details
|
|
|
+ else:
|
|
|
+ declaration_details = review_info.review_result
|
|
|
+ data = {"id": info.id,
|
|
|
+ "declaration_date": info.declaration_date,
|
|
|
+ "declaration_amount": info.declaration_amount,
|
|
|
+ "declaration_unit": info.declaration_unit,
|
|
|
+ "declaration_unit_name": dept_info.dept_name,
|
|
|
+ "declaration_person": info.declaration_person,
|
|
|
+ "declaration_person_name": user_info.user_name,
|
|
|
+ "declaration_details": declaration_details,
|
|
|
+ "create_time": info.create_time}
|
|
|
+ detail=[]
|
|
|
+ for detail_info in declaration_id_get_declaration_detail_list(db,id):
|
|
|
+ material_type_info = type_id_get_material_type_info(db,detail_info.material_type)
|
|
|
+ material_into = material_id_get_material_info(db,detail_info.material_code)
|
|
|
+ detail.append({"id":detail_info.id,
|
|
|
+ "serial_number":detail_info.serial_number,
|
|
|
+ "material_type":detail_info.material_type,
|
|
|
+ "material_category_name":material_type_info.material_category_name,
|
|
|
+ "material_code":detail_info.material_code,
|
|
|
+ "material_name":material_into.material_name,
|
|
|
+ "material_quantity":detail_info.material_quantity,
|
|
|
+ "material_unit_price":detail_info.material_unit_price,
|
|
|
+ "material_purpose":detail_info.material_purpose})
|
|
|
+ return {"code": 200, "msg": "获取成功", "data": data}
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+@router.get("/list")
|
|
|
+async def get_pattern_list(
|
|
|
+ # declaration_details: str = Query(None, description='名称'),
|
|
|
+ page: int = Query(1, gt=0, description='页码'),
|
|
|
+ pageSize: int = Query(None, gt=0, description='每页条目数量'),
|
|
|
+ db: Session = Depends(get_db)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ query = db.query(ResourceProvisionProcurementDeclaration)
|
|
|
+ query = query.filter_by(del_flag='0')
|
|
|
+ # if declaration_details:
|
|
|
+ # query = query.filter(ResourceProvisionProcurementDeclaration.declaration_details==declaration_details)
|
|
|
+ total_items = query.count()
|
|
|
+ # 排序
|
|
|
+ if pageSize is None:
|
|
|
+ pageSize=total_items
|
|
|
+ query = query.order_by(ResourceProvisionProcurementDeclaration.declaration_date.desc())
|
|
|
+ # 执行分页查询
|
|
|
+ lists = query.offset((page - 1) * pageSize).limit(pageSize).all()
|
|
|
+ data = []
|
|
|
+ for info in lists:
|
|
|
+ dept_info = dept_id_get_dept_info(db, info.declaration_unit)
|
|
|
+ user_info = user_id_get_user_info(db, info.declaration_person)
|
|
|
+ review_info = declaration_id_get_declaration_review_info(db, info.id)
|
|
|
+ if review_info is None:
|
|
|
+ declaration_details = info.declaration_details
|
|
|
+ else:
|
|
|
+ declaration_details = review_info.review_result
|
|
|
+ data = {"id": info.id,
|
|
|
+ "declaration_date": info.declaration_date,
|
|
|
+ "declaration_amount": info.declaration_amount,
|
|
|
+ "declaration_unit": info.declaration_unit,
|
|
|
+ "declaration_unit_name": dept_info.dept_name,
|
|
|
+ "declaration_person": info.declaration_person,
|
|
|
+ "declaration_person_name": user_info.user_name,
|
|
|
+ "declaration_details": declaration_details,
|
|
|
+ "create_time": info.create_time}
|
|
|
+
|
|
|
+ return {"code": 200, "msg": "查询成功", "data": data,
|
|
|
+ "total": total_items,
|
|
|
+ "page": page,
|
|
|
+ "pageSize": pageSize,
|
|
|
+ "totalPages": (total_items + pageSize - 1) // pageSize
|
|
|
+ }
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+# @router.delete("/delete/{id}")
|
|
|
+# async def delete_pattern(
|
|
|
+# id: str,
|
|
|
+# db: Session = Depends(get_db)
|
|
|
+# ):
|
|
|
+# try:
|
|
|
+# # 检查图案是否存在
|
|
|
+# info = declaration_id_get_declaration_info(db, id)
|
|
|
+# if not info:
|
|
|
+# return JSONResponse(status_code=404, content={"code": 404, "msg": "type not found"})
|
|
|
+# info.del_flag='2'
|
|
|
+# review_info = declaration_id_get_declaration_review_info(db, id)
|
|
|
+# review_info.del_flag ='2'
|
|
|
+# for detail_info in declaration_id_get_declaration_detail_list(db, id):
|
|
|
+# detail_info.del_flag='2'
|
|
|
+# db.commit()
|
|
|
+# return {"code": 200, "msg": "删除成功"}
|
|
|
+# except Exception as e:
|
|
|
+# traceback.print_exc()
|
|
|
+# raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+
|
|
|
+@router.post("/review")
|
|
|
+async def create_pattern(
|
|
|
+ user_id=Depends(valid_access_token),
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
+ db: Session = Depends(get_db)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ new_review = ResourceProvisionProcurementReview(
|
|
|
+ id=new_guid(),
|
|
|
+ reviewer=user_id,
|
|
|
+ declaration_id=body['declaration_id'],
|
|
|
+ review_result=body['review_result'],
|
|
|
+ review_comments=body['review_comments'],
|
|
|
+ create_by = user_id
|
|
|
+ )
|
|
|
+ db.add(new_review)
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "审核成功", "data": None}
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+
|
|
|
+@router.get("/review/list")
|
|
|
+async def get_pattern_list(
|
|
|
+ page: int = Query(1, gt=0, description='页码'),
|
|
|
+ pageSize: int = Query(None, gt=0, description='每页条目数量'),
|
|
|
+ db: Session = Depends(get_db)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ review_list = [info.declaration_id for info in get_declaration_review_list(db)]
|
|
|
+ query = db.query(ResourceProvisionProcurementDeclaration)
|
|
|
+ query = query.filter_by(del_flag='0')
|
|
|
+ query = query.filter(~ResourceProvisionProcurementDeclaration.id.in_(review_list))
|
|
|
+ total_items = query.count()
|
|
|
+ # 排序
|
|
|
+ if pageSize is None:
|
|
|
+ pageSize=total_items
|
|
|
+ query = query.order_by(ResourceProvisionProcurementDeclaration.declaration_date.desc())
|
|
|
+ # 执行分页查询
|
|
|
+ lists = query.offset((page - 1) * pageSize).limit(pageSize).all()
|
|
|
+ data = []
|
|
|
+ for info in lists:
|
|
|
+ dept_info = dept_id_get_dept_info(db, info.declaration_unit)
|
|
|
+ user_info = user_id_get_user_info(db, info.declaration_person)
|
|
|
+ # review_info = declaration_id_get_declaration_review_info(db, info.id)
|
|
|
+ # if review_info is None:
|
|
|
+ # declaration_details = info.declaration_details
|
|
|
+ # else:
|
|
|
+ # declaration_details = review_info.review_result
|
|
|
+ data = {"id": info.id,
|
|
|
+ "declaration_date": info.declaration_date,
|
|
|
+ "declaration_amount": info.declaration_amount,
|
|
|
+ "declaration_unit": info.declaration_unit,
|
|
|
+ "declaration_unit_name": dept_info.dept_name,
|
|
|
+ "declaration_person": info.declaration_person,
|
|
|
+ "declaration_person_name": user_info.user_name,
|
|
|
+ "declaration_details": info.declaration_details,
|
|
|
+ "create_time": info.create_time}
|
|
|
+
|
|
|
+ return {"code": 200, "msg": "查询成功", "data": data,
|
|
|
+ "total": total_items,
|
|
|
+ "page": page,
|
|
|
+ "pageSize": pageSize,
|
|
|
+ "totalPages": (total_items + pageSize - 1) // pageSize
|
|
|
+ }
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|