main.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import xml.etree.ElementTree as ET
  2. from typing import Union
  3. from fastapi import FastAPI
  4. import os
  5. import uuid
  6. import re
  7. import json
  8. from fastapi.middleware.cors import CORSMiddleware
  9. from pydantic import BaseModel
  10. from subdir import service
  11. from subdir import db
  12. import numpy as np
  13. from fastapi.staticfiles import StaticFiles
  14. from pymongo import AsyncMongoClient
  15. client = AsyncMongoClient()
  16. from fastapi import WebSocket, WebSocketDisconnect
  17. class ConnectionManager:
  18. """Class defining socket events"""
  19. def __init__(self):
  20. """init method, keeping track of connections"""
  21. self.active_connections = []
  22. async def connect(self, websocket: WebSocket):
  23. """connect event"""
  24. await websocket.accept()
  25. self.active_connections.append(websocket)
  26. async def send_personal_message(self, message: str, websocket: WebSocket):
  27. """Direct Message"""
  28. await websocket.send_text(message)
  29. def disconnect(self, websocket: WebSocket):
  30. """disconnect event"""
  31. self.active_connections.remove(websocket)
  32. app = FastAPI()
  33. origins = [
  34. "http://localhost.tiangolo.com",
  35. "https://localhost.tiangolo.com",
  36. "http://localhost",
  37. "http://localhost:9002",
  38. "http://127.0.0.1:9002",
  39. ]
  40. app.add_middleware(
  41. CORSMiddleware,
  42. allow_origins=["*"],
  43. allow_credentials=True,
  44. allow_methods=["*"],
  45. allow_headers=["*"],
  46. )
  47. app.mount("/static", StaticFiles(directory="front/dist"), name="static")
  48. manager = ConnectionManager()
  49. for root, dirs, files in os.walk("./data", topdown=False):
  50. for name in files:
  51. print(os.path.join(root, name))
  52. file_data = os.path.join(root, name)
  53. def getDetail(root):
  54. print(root.tag)
  55. print(root.text)
  56. print(root.attrib)
  57. result = []
  58. result.append(["名称", "金额", "暂估价", "安全文明施工费", "规费"])
  59. for child in root:
  60. print(child.tag)
  61. print(child.attrib)
  62. if child.tag == "TouBiaoXx":
  63. result.append([child.attrib["Zbr"], child.attrib["Tbzj"], child.attrib["Zgj"], child.attrib["Aqwmf"], child.attrib["Gf"]])
  64. if child.tag == "Dxgcxx":
  65. Dxgcbh = child.attrib["Dxgcbh"]
  66. Dxgcmc = child.attrib["Dxgcmc"]
  67. result.append([child.attrib["Dxgcmc"], child.attrib["Je"], child.attrib["Zgj"], child.attrib["Aqwmf"], child.attrib["Gf"]])
  68. return result
  69. def getOutline(root):
  70. print(root.tag)
  71. print(root.text)
  72. print(root.attrib)
  73. result = []
  74. for child in root:
  75. print(child.tag)
  76. print(child.attrib)
  77. if child.tag == "TouBiaoXx":
  78. result.append({"id" : "TouBiaoXx", "label" : "投标信息" })
  79. if child.tag == "Dxgcxx":
  80. Dxgcbh = child.attrib["Dxgcbh"]
  81. Dxgcmc = child.attrib["Dxgcmc"]
  82. result2 = []
  83. for child1 in child:
  84. #print("level2===================================")
  85. #print(child1.tag)
  86. #print(child1.attrib)
  87. Dwgcbh = child1.attrib["Dwgcbh"]
  88. Dwgcmc = child1.attrib["Dwgcmc"]
  89. Zylb = child1.attrib["Zylb"]
  90. result2.append({"id" : Dwgcbh,"Zylb":Zylb, "label": Dwgcmc, "children" : service.getDwgc(root, Dwgcbh, Zylb)})
  91. result.append({"id" : Dxgcbh, "label" : Dxgcmc, "children" : result2})
  92. return result
  93. class Info(BaseModel):
  94. name: str
  95. class DingeshuRequest(BaseModel):
  96. value: int
  97. class DingeXilieRequest(BaseModel):
  98. value: int
  99. id: str
  100. class SingleDingeXilieRequest(BaseModel):
  101. zhuanye: int
  102. debh: str
  103. class InfoWithID(BaseModel):
  104. name: str
  105. id: str
  106. @app.post("/outline")
  107. async def read_root(info: Info):
  108. for root, dirs, files in os.walk("./data", topdown=False):
  109. for name in files:
  110. if name == info.name:
  111. print(os.path.join(root, name))
  112. file_data = os.path.join(root, name)
  113. tree = ET.parse(file_data)
  114. root = tree.getroot()
  115. return getOutline(root)
  116. @app.post("/outline2")
  117. async def read_root2(info: Info):
  118. return await db.getOutline(client, info.name)
  119. @app.post("/detail")
  120. async def read_detail(info: Info):
  121. for root, dirs, files in os.walk("./data", topdown=False):
  122. for name in files:
  123. if name == info.name:
  124. print(os.path.join(root, name))
  125. file_data = os.path.join(root, name)
  126. tree = ET.parse(file_data)
  127. root = tree.getroot()
  128. return getDetail(root)
  129. @app.post("/detail2")
  130. async def read_detail2(info: Info):
  131. return await db.getDetail(client, info.name)
  132. @app.post("/baojiahuizong/")
  133. async def read_bjhz(info: InfoWithID):
  134. for root, dirs, files in os.walk("./data", topdown=False):
  135. for name in files:
  136. if name == info.name:
  137. print(os.path.join(root, name))
  138. file_data = os.path.join(root, name)
  139. tree = ET.parse(file_data)
  140. root = tree.getroot()
  141. return service.getBjhz(root, info.id)
  142. @app.post("/baojiahuizong2/")
  143. async def read_bjhz2(info: InfoWithID):
  144. return await db.getBjhz(client, info.name, info.id)
  145. @app.post("/guifeishuijin/")
  146. async def read_gfsj(info: InfoWithID):
  147. for root, dirs, files in os.walk("./data", topdown=False):
  148. for name in files:
  149. if name == info.name:
  150. print(os.path.join(root, name))
  151. file_data = os.path.join(root, name)
  152. tree = ET.parse(file_data)
  153. root = tree.getroot()
  154. return service.getGfsj(root, info.id)
  155. @app.post("/guifeishuijin2/")
  156. async def read_gfsj2(info: InfoWithID):
  157. return await db.getGfsj(client, info.name, info.id)
  158. @app.post("/qitaxiangmu/")
  159. async def read_qtxm(info: InfoWithID):
  160. for root, dirs, files in os.walk("./data", topdown=False):
  161. for name in files:
  162. if name == info.name:
  163. print(os.path.join(root, name))
  164. file_data = os.path.join(root, name)
  165. tree = ET.parse(file_data)
  166. root = tree.getroot()
  167. return service.getQtxm(root, info.id)
  168. @app.post("/qitaxiangmu2/")
  169. async def read_qtxm2(info: InfoWithID):
  170. return await db.getQtxm(client, info.name, info.id)
  171. @app.post("/zanliejine/")
  172. async def read_zlje(info: InfoWithID):
  173. for root, dirs, files in os.walk("./data", topdown=False):
  174. for name in files:
  175. if name == info.name:
  176. print(os.path.join(root, name))
  177. file_data = os.path.join(root, name)
  178. tree = ET.parse(file_data)
  179. root = tree.getroot()
  180. return service.getZlje(root, info.id)
  181. @app.post("/zanliejine2/")
  182. async def read_zlje2(info: InfoWithID):
  183. return await db.getZlje(client, info.name, info.id)
  184. @app.post("/jirigong/")
  185. async def read_jrg(info: InfoWithID):
  186. for root, dirs, files in os.walk("./data", topdown=False):
  187. for name in files:
  188. if name == info.name:
  189. print(os.path.join(root, name))
  190. file_data = os.path.join(root, name)
  191. tree = ET.parse(file_data)
  192. root = tree.getroot()
  193. return service.getJrg(root, info.id)
  194. @app.post("/jirigong2/")
  195. async def read_jrg2(info: InfoWithID):
  196. return await db.getJrg(client, info.name, info.id)
  197. @app.post("/zongchengbaofuwufei/")
  198. async def read_zcbfwf(info: InfoWithID):
  199. for root, dirs, files in os.walk("./data", topdown=False):
  200. for name in files:
  201. if name == info.name:
  202. print(os.path.join(root, name))
  203. file_data = os.path.join(root, name)
  204. tree = ET.parse(file_data)
  205. root = tree.getroot()
  206. return service.getZcbfwf(root, info.id)
  207. @app.post("/zongchengbaofuwufei2/")
  208. async def read_zcbfwf2(info: InfoWithID):
  209. return await db.getZcbfwf(client, info.name, info.id)
  210. @app.post("/fabaorengongyingcailiao/")
  211. async def read_fbrgycl(info: InfoWithID):
  212. for root, dirs, files in os.walk("./data", topdown=False):
  213. for name in files:
  214. if name == info.name:
  215. print(os.path.join(root, name))
  216. file_data = os.path.join(root, name)
  217. tree = ET.parse(file_data)
  218. root = tree.getroot()
  219. return service.getFbrgycl(root, info.id)
  220. @app.post("/fabaorengongyingcailiao2/")
  221. async def read_fbrgycl2(info: InfoWithID):
  222. return await db.getFbrgycl(client, info.name, info.id)
  223. @app.post("/rencaijihuizong/")
  224. async def read_rcjhz(info: InfoWithID):
  225. for root, dirs, files in os.walk("./data", topdown=False):
  226. for name in files:
  227. if name == info.name:
  228. print(os.path.join(root, name))
  229. file_data = os.path.join(root, name)
  230. tree = ET.parse(file_data)
  231. root = tree.getroot()
  232. return service.getRcjhz(root, info.id)
  233. @app.post("/rencaijihuizong2/")
  234. async def read_rcjhz2(info: InfoWithID):
  235. return await db.getRcjhz(client, info.name, info.id)
  236. @app.post("/qingdanxiangmu/")
  237. async def read_qdxm(info: InfoWithID):
  238. for root, dirs, files in os.walk("./data", topdown=False):
  239. for name in files:
  240. if name == info.name:
  241. print(os.path.join(root, name))
  242. file_data = os.path.join(root, name)
  243. tree = ET.parse(file_data)
  244. root = tree.getroot()
  245. return service.getQdxm(root, info.id)
  246. #return []
  247. @app.post("/qingdanxiangmu2/")
  248. async def read_qdxm2(info: InfoWithID):
  249. return await db.getQdxm(client, info.name, info.id)
  250. class Item(BaseModel):
  251. bh: str
  252. bt: str
  253. name: str
  254. class Rcj(BaseModel):
  255. bh: str
  256. bt: str
  257. bm: str
  258. name: str
  259. class Dercj(BaseModel):
  260. bh: str
  261. bt: str
  262. qdbm: str
  263. debm: str
  264. name: str
  265. class Zjcs(BaseModel):
  266. bh: str
  267. name: str
  268. @app.post("/qingdanmingxi/")
  269. async def read_qdmx(item : Item):
  270. return await db.getQdmx(client, item.name, item.bh, item.bt)
  271. @app.post("/qingdanrcj/")
  272. async def read_rcj(item : Rcj):
  273. if item.bt == "Djcs":
  274. return await db.getDjcsQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  275. return await db.getQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  276. @app.post("/qingdantuijian/")
  277. async def read_tuijian(item : Rcj):
  278. return service.getQingdanTuijian(item.bh, item.bt, item.bm)
  279. @app.post("/dingercj/")
  280. async def read_dercj(item : Dercj):
  281. if item.bt == "Djcs":
  282. return await db.getDjcsDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  283. return await db.getDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  284. @app.post("/zjcs/")
  285. async def read_zjcs(item : Zjcs):
  286. return await db.getZjcs(client, item.name, item.bh)
  287. @app.post("/djcs/")
  288. async def read_djcs(item : Zjcs):
  289. return await db.getDjcs(client, item.name, item.bh)
  290. @app.post("/files/")
  291. async def read_files():
  292. result = []
  293. for root, dirs, files in os.walk("./data", topdown=False):
  294. for name in files:
  295. print(os.path.join(root, name))
  296. result.append([name, "", ""])
  297. return result
  298. @app.post("/files2/")
  299. async def read_files2():
  300. result = await db.list_files(client)
  301. return result
  302. @app.post("/des/")
  303. async def read_des(r: DingeshuRequest):
  304. result = service.getDes(r.value)
  305. #print(result)
  306. return result
  307. @app.post("/pbs/")
  308. async def read_pbs(r: DingeshuRequest):
  309. result = service.getPbs(r.value)
  310. #print(result)
  311. return result
  312. @app.post("/pbxl/")
  313. async def read_pbxl(r: Info):
  314. result = service.getPbxl(r.name)
  315. #print(result)
  316. return result
  317. @app.post("/dexilie/")
  318. async def read_dexilie(r: DingeXilieRequest):
  319. result = service.getDeXilie(r.value, r.id)
  320. #print(result)
  321. return result
  322. @app.post("/singledexilie/")
  323. async def read_singledexilie(r: SingleDingeXilieRequest):
  324. result1, result2, rgde, jxde, clde, bz_selected , actual_zhuanye= service.getSingleDeXilie(r.zhuanye, r.debh)
  325. print("get result ***************************************")
  326. if result1:
  327. result3 = json.loads(result1)
  328. else:
  329. result3 = {}
  330. result3["reverse"] = str(result2)
  331. result3["rgde"] = rgde
  332. result3["jxde"] = jxde
  333. result3["clde"] = clde
  334. result3["actual_zhuanye"] = actual_zhuanye
  335. if bz_selected != None:
  336. result3["bz_selected"] = json.loads(bz_selected)
  337. else:
  338. result3["bz_selected"] = {"BZBH": {}}
  339. print(result3)
  340. return json.dumps(result3, ensure_ascii=False)
  341. @app.post("/save/")
  342. async def save(r: Info):
  343. data = json.loads(r.name)
  344. print(data)
  345. return await db.save(client, data)
  346. async def resolve(websocket, data):
  347. await db.resolve(manager, websocket, data, client)##manager.send_personal_message(f"You wrote: {data}", websocket)
  348. @app.websocket("/ws")
  349. async def websocket_endpoint(websocket: WebSocket):
  350. await manager.connect(websocket)
  351. try:
  352. while True:
  353. data = await websocket.receive_text()
  354. await resolve(websocket, data)
  355. ##await manager.broadcast(f"Client #{client_id} says: {data}")
  356. except WebSocketDisconnect:
  357. manager.disconnect(websocket)
  358. ##await manager.broadcast(f"Client left the chat")