#!/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 ) 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)