123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- #!/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 sqlalchemy import text, exists, and_, or_, not_
- 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.db import db_yzy, db_msg_center
- from common import YzyApi
- import json
- import traceback
- from config import settings
- router = APIRouter()
- @router.post("/create")
- async def create(
- user_id=Depends(valid_access_token),
- body = Depends(remove_xss_json),
- db: Session = Depends(get_db)
- ):
- try:
- user_info = db.query(SysUser).filter(and_(SysUser.phonenumber == mpfun.enc_data(body['contact_phone']), SysUser.del_flag == '0')).first()
- if user_info is None:
- return {"code": 500, "msg": "联系方式无法匹配到粤政易账号", "data": None}
-
- new_trans = RescueMateriaTransfer(
- application_time = datetime.now(),
- applicant = body['applicant'],
- # processing_person = '',
- # transfer_unit = body['transfer_unit'],
- # transferred_unit = body['transferred_unit'],
- # reason_transfer = body['reason_transfer'],
- # book_value = body['application_time'],
- receiving_unit = body['receiving_unit'],
- receiving_location = body['receiving_location'],
- use = body['use'],
- contact_person = body['contact_person'],
- contact_phone = body['contact_phone'],
- notes = body['notes'],
- # reason = body['reason'],
- treatment = body['treatment'],
- apply_status = '0',
- apply_time = None
- )
- db.add(new_trans)
- db.commit()
- db.refresh(new_trans)
- i = 0
- for n in body['items']:
- i += 1
- new_detail = RescueMateriaTransferDetail(
- pid = new_trans.id,
- materia_id = n['id'],
- materia_name = n['materia_name'],
- materia_type = n['materia_type'],
- materia_unit = n['materia_unit'],
- materia_num = n['materia_num'],
- num = n['num'],
- area = n['area'],
- management_unit = n['management_unit'],
- fuzeren = n['fuzeren'],
- order_num = i,
- price = 0
- )
- db.add(new_detail)
- db.commit()
- '''
- # 信息中心
- to_user_id = user_info.user_id
- yzy_account = user_info.yzy_account
- task_title = '物资调配审批'
- description = f"你有一个{task_title}任务需要处理,点击处理"
- detail_url = "{}/yjxp/#/worker/rescueMateriaTranferApply".format(settings.YZY_WEB_ROOT)
- foreign_key = str(new_trans.id)
- from_scenario = "rescue_materia_tranfer"
- db_msg_center.add_message(db, task_title, to_user_id, task_title, description, foreign_key, from_scenario)
- # 发送粤政易消息
- data = {
- "yzy_userid": yzy_account,
- "mobile": body['contact_phone'],
- "content": description,
- "recorded_by": user_id,
- "detail_url": detail_url,
- "foreign_key": foreign_key,
- "from_scenario": from_scenario,
- "title": "物资调配审批"
- }
- YzyApi.add_to_msg_queue(db, data)
- '''
- 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)}")
- # 发送粤政易消息
- def send_yzy_msg(db: Session, msg_type: str, to_user_id: int, yzy_account: str, mobile, description: str, detail_url: str, foreign_key:str, from_scenario: str) -> None:
- data = {
- "yzy_userid": yzy_account,
- "mobile": mobile,
- "content": description,
- "recorded_by": 0,
- "detail_url": detail_url,
- "foreign_key": foreign_key,
- "from_scenario": from_scenario,
- "title": "物资调配审批"
- }
- YzyApi.add_to_msg_queue(db, data)
- db_msg_center.add_message(db, msg_type, to_user_id, "物资调配审批", description, foreign_key, from_scenario)
- @router.get("/list")
- async def get_transfter_list(
- user_id = Depends(valid_access_token),
- page: int = Query(1, gt=0, description='页码'),
- pageSize: int = Query(None, gt=0, description='每页条目数量'),
- db: Session = Depends(get_db)
- ):
- try:
- query = db.query(RescueMateriaTransfer)
- total_items = query.count()
- # 排序
- if pageSize is None:
- pageSize = total_items
- query = query.order_by(RescueMateriaTransfer.application_time.desc())
- # 执行分页查询
- lists = query.offset((page - 1) * pageSize).limit(pageSize).all()
- data = []
- for info in lists:
- pid = info.id
- material = []
- rows = db.query(RescueMateriaTransferDetail).filter(RescueMateriaTransferDetail.pid == pid).order_by(RescueMateriaTransferDetail.order_num.asc()).all()
- for row in rows:
- material.append({
- "id": row.id,
- "materia_id": row.materia_id,
- "materia_name": row.materia_name,
- "materia_type": row.materia_type,
- "materia_unit": row.materia_unit,
- "materia_num": row.materia_num,
- "num": row.num,
- "price": row.price,
- "purchase_time": get_datetime_str(row.purchase_time),
- "order_num": row.order_num,
- "area": row.area,
- "management_unit": row.management_unit,
- "fuzeren": row.fuzeren
- })
- data.append({
- "id": info.id,
- "application_time": get_datetime_str(info.application_time),
- "applicant": info.applicant,
- "treatment": get_treatment_text(info.treatment),
- "receiving_unit": info.receiving_unit,
- "receiving_location": info.receiving_location,
- "use": info.use,
- "contact_person": info.contact_person,
- "contact_phone": info.contact_phone,
- "notes": info.notes,
- "apply_status": info.apply_status,
- "apply_time": get_datetime_str(info.apply_time),
- "material": material
- })
- 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)}")
- def get_treatment_text(val: str) -> str:
- if val == '1':
- return '无偿调拨'
- elif val == '2':
- return '有偿调拨'
- else:
- return str(val)
-
- @router.get("/detail")
- async def get_transfter_detail(
- id: str,
- user_id = Depends(valid_access_token),
- page: int = Query(1, gt=0, description='页码'),
- pageSize: int = Query(None, gt=0, description='每页条目数量'),
- db: Session = Depends(get_db)
- ):
- try:
- row = db.query(RescueMateriaTransfer).filter(RescueMateriaTransfer.id == int(id)).first()
- if row:
- data = get_model_dict(row)
- data["apply_time"] = get_datetime_str(row.apply_time)
- material = []
- rows = db.query(RescueMateriaTransferDetail).filter(RescueMateriaTransferDetail.pid == row.id).order_by(RescueMateriaTransferDetail.order_num.asc()).all()
- for row in rows:
- material.append({
- "id": row.id,
- "materia_id": row.materia_id,
- "materia_name": row.materia_name,
- "materia_type": row.materia_type,
- "materia_unit": row.materia_unit,
- "materia_num": row.materia_num,
- "num": row.num,
- "price": row.price,
- "purchase_time": get_datetime_str(row.purchase_time),
- "order_num": row.order_num,
- "area": row.area,
- "management_unit": row.management_unit,
- "fuzeren": row.fuzeren
- })
- data['material'] = material
- 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)}")
|