main.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. class Tiaojia(BaseModel):
  109. biao_id: str
  110. bh: str
  111. bm: str
  112. jiage: str
  113. glf: str
  114. lr: str
  115. bz: int
  116. @app.post("/outline")
  117. async def read_root(info: Info):
  118. for root, dirs, files in os.walk("./data", topdown=False):
  119. for name in files:
  120. if name == info.name:
  121. print(os.path.join(root, name))
  122. file_data = os.path.join(root, name)
  123. tree = ET.parse(file_data)
  124. root = tree.getroot()
  125. return getOutline(root)
  126. @app.post("/outline2")
  127. async def read_root2(info: Info):
  128. return await db.getOutline(client, info.name)
  129. @app.post("/detail")
  130. async def read_detail(info: Info):
  131. for root, dirs, files in os.walk("./data", topdown=False):
  132. for name in files:
  133. if name == info.name:
  134. print(os.path.join(root, name))
  135. file_data = os.path.join(root, name)
  136. tree = ET.parse(file_data)
  137. root = tree.getroot()
  138. return getDetail(root)
  139. @app.post("/detail2")
  140. async def read_detail2(info: Info):
  141. return await db.getDetail(client, info.name)
  142. @app.post("/baojiahuizong/")
  143. async def read_bjhz(info: InfoWithID):
  144. for root, dirs, files in os.walk("./data", topdown=False):
  145. for name in files:
  146. if name == info.name:
  147. print(os.path.join(root, name))
  148. file_data = os.path.join(root, name)
  149. tree = ET.parse(file_data)
  150. root = tree.getroot()
  151. return service.getBjhz(root, info.id)
  152. @app.post("/baojiahuizong2/")
  153. async def read_bjhz2(info: InfoWithID):
  154. return await db.getBjhz(client, info.name, info.id)
  155. @app.post("/guifeishuijin/")
  156. async def read_gfsj(info: InfoWithID):
  157. for root, dirs, files in os.walk("./data", topdown=False):
  158. for name in files:
  159. if name == info.name:
  160. print(os.path.join(root, name))
  161. file_data = os.path.join(root, name)
  162. tree = ET.parse(file_data)
  163. root = tree.getroot()
  164. return service.getGfsj(root, info.id)
  165. @app.post("/guifeishuijin2/")
  166. async def read_gfsj2(info: InfoWithID):
  167. return await db.getGfsj(client, info.name, info.id)
  168. @app.post("/qitaxiangmu/")
  169. async def read_qtxm(info: InfoWithID):
  170. for root, dirs, files in os.walk("./data", topdown=False):
  171. for name in files:
  172. if name == info.name:
  173. print(os.path.join(root, name))
  174. file_data = os.path.join(root, name)
  175. tree = ET.parse(file_data)
  176. root = tree.getroot()
  177. return service.getQtxm(root, info.id)
  178. @app.post("/qitaxiangmu2/")
  179. async def read_qtxm2(info: InfoWithID):
  180. return await db.getQtxm(client, info.name, info.id)
  181. @app.post("/zanliejine/")
  182. async def read_zlje(info: InfoWithID):
  183. for root, dirs, files in os.walk("./data", topdown=False):
  184. for name in files:
  185. if name == info.name:
  186. print(os.path.join(root, name))
  187. file_data = os.path.join(root, name)
  188. tree = ET.parse(file_data)
  189. root = tree.getroot()
  190. return service.getZlje(root, info.id)
  191. @app.post("/zanliejine2/")
  192. async def read_zlje2(info: InfoWithID):
  193. return await db.getZlje(client, info.name, info.id)
  194. @app.post("/jirigong/")
  195. async def read_jrg(info: InfoWithID):
  196. for root, dirs, files in os.walk("./data", topdown=False):
  197. for name in files:
  198. if name == info.name:
  199. print(os.path.join(root, name))
  200. file_data = os.path.join(root, name)
  201. tree = ET.parse(file_data)
  202. root = tree.getroot()
  203. return service.getJrg(root, info.id)
  204. @app.post("/jirigong2/")
  205. async def read_jrg2(info: InfoWithID):
  206. return await db.getJrg(client, info.name, info.id)
  207. @app.post("/zongchengbaofuwufei/")
  208. async def read_zcbfwf(info: InfoWithID):
  209. for root, dirs, files in os.walk("./data", topdown=False):
  210. for name in files:
  211. if name == info.name:
  212. print(os.path.join(root, name))
  213. file_data = os.path.join(root, name)
  214. tree = ET.parse(file_data)
  215. root = tree.getroot()
  216. return service.getZcbfwf(root, info.id)
  217. @app.post("/zongchengbaofuwufei2/")
  218. async def read_zcbfwf2(info: InfoWithID):
  219. return await db.getZcbfwf(client, info.name, info.id)
  220. @app.post("/fabaorengongyingcailiao/")
  221. async def read_fbrgycl(info: InfoWithID):
  222. for root, dirs, files in os.walk("./data", topdown=False):
  223. for name in files:
  224. if name == info.name:
  225. print(os.path.join(root, name))
  226. file_data = os.path.join(root, name)
  227. tree = ET.parse(file_data)
  228. root = tree.getroot()
  229. return service.getFbrgycl(root, info.id)
  230. @app.post("/fabaorengongyingcailiao2/")
  231. async def read_fbrgycl2(info: InfoWithID):
  232. return await db.getFbrgycl(client, info.name, info.id)
  233. @app.post("/rencaijihuizong/")
  234. async def read_rcjhz(info: InfoWithID):
  235. for root, dirs, files in os.walk("./data", topdown=False):
  236. for name in files:
  237. if name == info.name:
  238. print(os.path.join(root, name))
  239. file_data = os.path.join(root, name)
  240. tree = ET.parse(file_data)
  241. root = tree.getroot()
  242. return service.getRcjhz(root, info.id)
  243. @app.post("/rencaijihuizong2/")
  244. async def read_rcjhz2(info: InfoWithID):
  245. return await db.getRcjhz(client, info.name, info.id)
  246. @app.post("/qingdanxiangmu/")
  247. async def read_qdxm(info: InfoWithID):
  248. for root, dirs, files in os.walk("./data", topdown=False):
  249. for name in files:
  250. if name == info.name:
  251. print(os.path.join(root, name))
  252. file_data = os.path.join(root, name)
  253. tree = ET.parse(file_data)
  254. root = tree.getroot()
  255. return service.getQdxm(root, info.id)
  256. #return []
  257. @app.post("/qingdanxiangmu2/")
  258. async def read_qdxm2(info: InfoWithID):
  259. return await db.getQdxm(client, info.name, info.id)
  260. class Item(BaseModel):
  261. bh: str
  262. bt: str
  263. name: str
  264. class Rcj(BaseModel):
  265. bh: str
  266. bt: str
  267. bm: str
  268. name: str
  269. class Dercj(BaseModel):
  270. bh: str
  271. bt: str
  272. qdbm: str
  273. debm: str
  274. name: str
  275. class Zjcs(BaseModel):
  276. bh: str
  277. name: str
  278. @app.post("/qingdanmingxi/")
  279. async def read_qdmx(item : Item):
  280. return await db.getQdmx(client, item.name, item.bh, item.bt)
  281. @app.post("/qingdanrcj/")
  282. async def read_rcj(item : Rcj):
  283. if item.bt == "Djcs":
  284. return await db.getDjcsQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  285. return await db.getQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  286. @app.post("/qingdantuijian/")
  287. async def read_tuijian(item : Rcj):
  288. return service.getQingdanTuijian(item.bh, item.bt, item.bm)
  289. @app.post("/dingercj/")
  290. async def read_dercj(item : Dercj):
  291. if item.bt == "Djcs":
  292. return await db.getDjcsDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  293. return await db.getDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  294. @app.post("/zjcs/")
  295. async def read_zjcs(item : Zjcs):
  296. return await db.getZjcs(client, item.name, item.bh)
  297. @app.post("/djcs/")
  298. async def read_djcs(item : Zjcs):
  299. raw = await db.getDjcs(client, item.name, item.bh)
  300. raw2 = []
  301. for entry in raw:
  302. if "__children" in entry:
  303. entry["_children"] = entry["__children"]
  304. del entry["__children"]
  305. raw2.append(entry)
  306. return raw2
  307. @app.post("/files/")
  308. async def read_files():
  309. result = []
  310. for root, dirs, files in os.walk("./data", topdown=False):
  311. for name in files:
  312. print(os.path.join(root, name))
  313. result.append([name, "", ""])
  314. return result
  315. @app.post("/files2/")
  316. async def read_files2():
  317. result = await db.list_files(client)
  318. return result
  319. @app.post("/des/")
  320. async def read_des(r: DingeshuRequest):
  321. result = service.getDes(r.value)
  322. #print(result)
  323. return result
  324. @app.post("/pbs/")
  325. async def read_pbs(r: DingeshuRequest):
  326. result = service.getPbs(r.value)
  327. #print(result)
  328. result.insert(0, {"id": "0", "label": "全部"})
  329. return result
  330. @app.post("/pbxl/")
  331. async def read_pbxl(r: Info):
  332. result = service.getPbxl(r.name)
  333. #print(result)
  334. return result
  335. @app.post("/qufei/")
  336. async def read_qufei(r: Info):
  337. return await db.getQufei(client, r.name)
  338. @app.post("/dexilie/")
  339. async def read_dexilie(r: DingeXilieRequest):
  340. result = service.getDeXilie(r.value, r.id)
  341. #print(result)
  342. return result
  343. @app.post("/singledexilie/")
  344. async def read_singledexilie(r: SingleDingeXilieRequest):
  345. if r.debh.startswith("D") :
  346. return json.dumps({
  347. "reverse": "None",
  348. "rgde": None,
  349. "jxde": None,
  350. "clde": None,
  351. "actual_zhuanye": r.zhuanye,
  352. "bz_selected": {"BZBH": {}},
  353. "bz_selected2": {"BZBH": {}}
  354. }, ensure_ascii=False)
  355. result1, result2, rgde, jxde, clde, bz_selected, bz_selected2, actual_zhuanye= service.getSingleDeXilie(r.zhuanye, r.debh)
  356. print("get result ***************************************")
  357. if result1:
  358. result3 = json.loads(result1)
  359. else:
  360. result3 = {}
  361. result3["reverse"] = str(result2)
  362. result3["rgde"] = rgde
  363. result3["jxde"] = jxde
  364. result3["clde"] = clde
  365. result3["actual_zhuanye"] = actual_zhuanye
  366. if bz_selected != None:
  367. result3["bz_selected"] = json.loads(bz_selected)
  368. else:
  369. result3["bz_selected"] = {"BZBH": {}}
  370. if bz_selected2 != None:
  371. result3["bz_selected2"] = json.loads(bz_selected2)
  372. else:
  373. result3["bz_selected2"] = {"BZBH": {}}
  374. print(result3)
  375. return json.dumps(result3, ensure_ascii=False)
  376. @app.post("/save/")
  377. async def save(r: Info):
  378. data = json.loads(r.name)
  379. print(data)
  380. return await db.save(client, data)
  381. @app.post("/savedjcs/")
  382. async def savedjcs(r: Info):
  383. data = json.loads(r.name)
  384. print(data)
  385. return await db.savedjcs(client, data)
  386. @app.post("/applyFL/")
  387. async def applyFL(r: InfoWithID):
  388. data = json.loads(r.name)
  389. print(data)
  390. return await db.applyFL(client, r.id, data)
  391. @app.post("/tiaojia/")
  392. async def tiaojia(r: Tiaojia):
  393. ##return []
  394. glf = r.glf
  395. lr = r.lr
  396. if r.glf == '':
  397. glf = '0'
  398. if r.lr == "":
  399. lr = '0'
  400. return await db.tiaojia(client, r.biao_id, r.bh, r.bm, r.jiage, glf, lr, r.bz)
  401. async def resolve(websocket, data):
  402. await db.resolve(manager, websocket, data, client)##manager.send_personal_message(f"You wrote: {data}", websocket)
  403. @app.websocket("/ws")
  404. async def websocket_endpoint(websocket: WebSocket):
  405. await manager.connect(websocket)
  406. try:
  407. while True:
  408. data = await websocket.receive_text()
  409. await resolve(websocket, data)
  410. ##await manager.broadcast(f"Client #{client_id} says: {data}")
  411. except WebSocketDisconnect:
  412. manager.disconnect(websocket)
  413. ##await manager.broadcast(f"Client left the chat")