main.py 14 KB

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