transfer.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_id = n['id'],
  64. materia_name = n['materia_name'],
  65. materia_type = n['materia_type'],
  66. materia_unit = n['materia_unit'],
  67. materia_num = n['materia_num'],
  68. num = n['num'],
  69. area = n['area'],
  70. management_unit = n['management_unit'],
  71. fuzeren = n['fuzeren'],
  72. order_num = i,
  73. price = 0
  74. )
  75. db.add(new_detail)
  76. db.commit()
  77. '''
  78. # 信息中心
  79. to_user_id = user_info.user_id
  80. yzy_account = user_info.yzy_account
  81. task_title = '物资调配审批'
  82. description = f"你有一个{task_title}任务需要处理,点击处理"
  83. detail_url = "{}/yjxp/#/worker/rescueMateriaTranferApply".format(settings.YZY_WEB_ROOT)
  84. foreign_key = str(new_trans.id)
  85. from_scenario = "rescue_materia_tranfer"
  86. db_msg_center.add_message(db, task_title, to_user_id, task_title, description, foreign_key, from_scenario)
  87. # 发送粤政易消息
  88. data = {
  89. "yzy_userid": yzy_account,
  90. "mobile": body['contact_phone'],
  91. "content": description,
  92. "recorded_by": user_id,
  93. "detail_url": detail_url,
  94. "foreign_key": foreign_key,
  95. "from_scenario": from_scenario,
  96. "title": "物资调配审批"
  97. }
  98. YzyApi.add_to_msg_queue(db, data)
  99. '''
  100. return {"code": 200, "msg": "创建成功", "data": None}
  101. except Exception as e:
  102. traceback.print_exc()
  103. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  104. # 发送粤政易消息
  105. 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:
  106. data = {
  107. "yzy_userid": yzy_account,
  108. "mobile": mobile,
  109. "content": description,
  110. "recorded_by": 0,
  111. "detail_url": detail_url,
  112. "foreign_key": foreign_key,
  113. "from_scenario": from_scenario,
  114. "title": "物资调配审批"
  115. }
  116. YzyApi.add_to_msg_queue(db, data)
  117. db_msg_center.add_message(db, msg_type, to_user_id, "物资调配审批", description, foreign_key, from_scenario)
  118. @router.get("/list")
  119. async def get_transfter_list(
  120. user_id = Depends(valid_access_token),
  121. page: int = Query(1, gt=0, description='页码'),
  122. pageSize: int = Query(None, gt=0, description='每页条目数量'),
  123. db: Session = Depends(get_db)
  124. ):
  125. try:
  126. query = db.query(RescueMateriaTransfer)
  127. total_items = query.count()
  128. # 排序
  129. if pageSize is None:
  130. pageSize = total_items
  131. query = query.order_by(RescueMateriaTransfer.application_time.desc())
  132. # 执行分页查询
  133. lists = query.offset((page - 1) * pageSize).limit(pageSize).all()
  134. data = []
  135. for info in lists:
  136. pid = info.id
  137. material = []
  138. rows = db.query(RescueMateriaTransferDetail).filter(RescueMateriaTransferDetail.pid == pid).order_by(RescueMateriaTransferDetail.order_num.asc()).all()
  139. for row in rows:
  140. material.append({
  141. "id": row.id,
  142. "materia_id": row.materia_id,
  143. "materia_name": row.materia_name,
  144. "materia_type": row.materia_type,
  145. "materia_unit": row.materia_unit,
  146. "materia_num": row.materia_num,
  147. "num": row.num,
  148. "price": row.price,
  149. "purchase_time": get_datetime_str(row.purchase_time),
  150. "order_num": row.order_num,
  151. "area": row.area,
  152. "management_unit": row.management_unit,
  153. "fuzeren": row.fuzeren
  154. })
  155. data.append({
  156. "id": info.id,
  157. "application_time": get_datetime_str(info.application_time),
  158. "applicant": info.applicant,
  159. "treatment": get_treatment_text(info.treatment),
  160. "receiving_unit": info.receiving_unit,
  161. "receiving_location": info.receiving_location,
  162. "use": info.use,
  163. "contact_person": info.contact_person,
  164. "contact_phone": info.contact_phone,
  165. "notes": info.notes,
  166. "apply_status": info.apply_status,
  167. "apply_time": get_datetime_str(info.apply_time),
  168. "material": material
  169. })
  170. return {
  171. "code": 200,
  172. "msg": "查询成功",
  173. "data": data,
  174. "total": total_items,
  175. "page": page,
  176. "pageSize": pageSize,
  177. "totalPages": (total_items + pageSize - 1) // pageSize
  178. }
  179. except Exception as e:
  180. traceback.print_exc()
  181. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  182. def get_treatment_text(val: str) -> str:
  183. if val == '1':
  184. return '无偿调拨'
  185. elif val == '2':
  186. return '有偿调拨'
  187. else:
  188. return str(val)
  189. @router.get("/detail")
  190. async def get_transfter_detail(
  191. id: str,
  192. user_id = Depends(valid_access_token),
  193. page: int = Query(1, gt=0, description='页码'),
  194. pageSize: int = Query(None, gt=0, description='每页条目数量'),
  195. db: Session = Depends(get_db)
  196. ):
  197. try:
  198. row = db.query(RescueMateriaTransfer).filter(RescueMateriaTransfer.id == int(id)).first()
  199. if row:
  200. data = get_model_dict(row)
  201. data["apply_time"] = get_datetime_str(row.apply_time)
  202. material = []
  203. rows = db.query(RescueMateriaTransferDetail).filter(RescueMateriaTransferDetail.pid == row.id).order_by(RescueMateriaTransferDetail.order_num.asc()).all()
  204. for row in rows:
  205. material.append({
  206. "id": row.id,
  207. "materia_id": row.materia_id,
  208. "materia_name": row.materia_name,
  209. "materia_type": row.materia_type,
  210. "materia_unit": row.materia_unit,
  211. "materia_num": row.materia_num,
  212. "num": row.num,
  213. "price": row.price,
  214. "purchase_time": get_datetime_str(row.purchase_time),
  215. "order_num": row.order_num,
  216. "area": row.area,
  217. "management_unit": row.management_unit,
  218. "fuzeren": row.fuzeren
  219. })
  220. data['material'] = material
  221. return {
  222. "code": 200,
  223. "msg": "查询成功",
  224. "data": data
  225. }
  226. except Exception as e:
  227. traceback.print_exc()
  228. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")