transfer.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends, Query, HTTPException, status,WebSocket,WebSocketDisconnect
  4. from common.security import valid_access_token,valid_websocket_token
  5. from fastapi.responses import JSONResponse
  6. from sqlalchemy.orm import Session
  7. from sqlalchemy.sql import func
  8. from sqlalchemy import text, exists, and_, or_, not_
  9. from common.auth_user import *
  10. from sqlalchemy import text
  11. from pydantic import BaseModel
  12. from common.BigDataCenterAPI import *
  13. from database import get_db
  14. from typing import List
  15. from models import *
  16. from utils import *
  17. from utils.spatial import *
  18. from utils.ry_system_util import *
  19. from utils.resource_provision_util import *
  20. from common.db import db_yzy, db_msg_center
  21. from common import YzyApi
  22. import json
  23. import traceback
  24. from config import settings
  25. router = APIRouter()
  26. @router.post("/create")
  27. async def create(
  28. user_id=Depends(valid_access_token),
  29. body = Depends(remove_xss_json),
  30. db: Session = Depends(get_db)
  31. ):
  32. try:
  33. user_info = db.query(SysUser).filter(and_(SysUser.phonenumber == mpfun.enc_data(body['contact_phone']), SysUser.del_flag == '0')).first()
  34. if user_info is None:
  35. return {"code": 500, "msg": "联系方式无法匹配到粤政易账号", "data": None}
  36. new_trans = RescueMateriaTransfer(
  37. application_time = datetime.now(),
  38. applicant = body['applicant'],
  39. # processing_person = '',
  40. # transfer_unit = body['transfer_unit'],
  41. # transferred_unit = body['transferred_unit'],
  42. # reason_transfer = body['reason_transfer'],
  43. # book_value = body['application_time'],
  44. receiving_unit = body['receiving_unit'],
  45. receiving_location = body['receiving_location'],
  46. use = body['use'],
  47. contact_person = body['contact_person'],
  48. contact_phone = body['contact_phone'],
  49. notes = body['notes'],
  50. # reason = body['reason'],
  51. treatment = body['treatment'],
  52. apply_status = '0',
  53. apply_time = None
  54. )
  55. db.add(new_trans)
  56. db.commit()
  57. db.refresh(new_trans)
  58. i = 0
  59. for n in body['items']:
  60. i += 1
  61. new_detail = RescueMateriaTransferDetail(
  62. pid = new_trans.id,
  63. materia_name = n['materia_name'],
  64. materia_type = n['materia_type'],
  65. materia_unit = n['materia_unit'],
  66. materia_num = n['materia_num'],
  67. num = n['num'],
  68. area = n['area'],
  69. management_unit = n['management_unit'],
  70. fuzeren = n['fuzeren'],
  71. order_num = i
  72. )
  73. db.add(new_detail)
  74. db.commit()
  75. # 信息中心
  76. to_user_id = user_info.user_id
  77. yzy_account = user_info.yzy_account
  78. task_title = '物资调配审批'
  79. description = f"你有一个{task_title}任务需要处理,点击处理"
  80. detail_url = "{}/yjxp/#/worker/rescueMateriaTranferApply".format(settings.YZY_WEB_ROOT)
  81. foreign_key = str(new_trans.id)
  82. from_scenario = "rescue_materia_tranfer"
  83. db_msg_center.add_message(db, task_title, to_user_id, task_title, description, foreign_key, from_scenario)
  84. # 发送粤政易消息
  85. data = {
  86. "yzy_userid": yzy_account,
  87. "mobile": body['contact_phone'],
  88. "content": description,
  89. "recorded_by": user_id,
  90. "detail_url": detail_url,
  91. "foreign_key": foreign_key,
  92. "from_scenario": from_scenario,
  93. "title": "物资调配审批"
  94. }
  95. YzyApi.add_to_msg_queue(db, data)
  96. return {"code": 200, "msg": "创建成功", "data": None}
  97. except Exception as e:
  98. traceback.print_exc()
  99. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  100. # 发送粤政易消息
  101. 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:
  102. data = {
  103. "yzy_userid": yzy_account,
  104. "mobile": mobile,
  105. "content": description,
  106. "recorded_by": 0,
  107. "detail_url": detail_url,
  108. "foreign_key": foreign_key,
  109. "from_scenario": from_scenario,
  110. "title": "物资调配审批"
  111. }
  112. YzyApi.add_to_msg_queue(db, data)
  113. db_msg_center.add_message(db, msg_type, to_user_id, "物资调配审批", description, foreign_key, from_scenario)