main.py 14 KB

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