main.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. from subdir import util
  14. import numpy as np
  15. from fastapi.staticfiles import StaticFiles
  16. from pymongo import AsyncMongoClient
  17. client = AsyncMongoClient()
  18. from fastapi import WebSocket, WebSocketDisconnect
  19. class ConnectionManager:
  20. """Class defining socket events"""
  21. def __init__(self):
  22. """init method, keeping track of connections"""
  23. self.active_connections = []
  24. async def connect(self, websocket: WebSocket):
  25. """connect event"""
  26. await websocket.accept()
  27. self.active_connections.append(websocket)
  28. async def send_personal_message(self, message: str, websocket: WebSocket):
  29. """Direct Message"""
  30. await websocket.send_text(message)
  31. def disconnect(self, websocket: WebSocket):
  32. """disconnect event"""
  33. self.active_connections.remove(websocket)
  34. app = FastAPI()
  35. origins = [
  36. "http://localhost.tiangolo.com",
  37. "https://localhost.tiangolo.com",
  38. "http://localhost",
  39. "http://localhost:9002",
  40. "http://127.0.0.1:9002",
  41. ]
  42. app.add_middleware(
  43. CORSMiddleware,
  44. allow_origins=["*"],
  45. allow_credentials=True,
  46. allow_methods=["*"],
  47. allow_headers=["*"],
  48. )
  49. app.add_middleware(GZipMiddleware, minimum_size=1000, compresslevel=5)
  50. app.mount("/static", StaticFiles(directory="front/dist"), name="static")
  51. manager = ConnectionManager()
  52. for root, dirs, files in os.walk("./data", topdown=False):
  53. for name in files:
  54. print(os.path.join(root, name))
  55. file_data = os.path.join(root, name)
  56. def getDetail(root):
  57. print(root.tag)
  58. print(root.text)
  59. print(root.attrib)
  60. result = []
  61. result.append(["名称", "金额", "暂估价", "安全文明施工费", "规费"])
  62. for child in root:
  63. print(child.tag)
  64. print(child.attrib)
  65. if child.tag == "TouBiaoXx":
  66. result.append([child.attrib["Zbr"], child.attrib["Tbzj"], child.attrib["Zgj"], child.attrib["Aqwmf"], child.attrib["Gf"]])
  67. if child.tag == "Dxgcxx":
  68. Dxgcbh = child.attrib["Dxgcbh"]
  69. Dxgcmc = child.attrib["Dxgcmc"]
  70. result.append([child.attrib["Dxgcmc"], child.attrib["Je"], child.attrib["Zgj"], child.attrib["Aqwmf"], child.attrib["Gf"]])
  71. return result
  72. def getOutline(root):
  73. print(root.tag)
  74. print(root.text)
  75. print(root.attrib)
  76. result = []
  77. for child in root:
  78. print(child.tag)
  79. print(child.attrib)
  80. if child.tag == "TouBiaoXx":
  81. result.append({"id" : "TouBiaoXx", "label" : "投标信息" })
  82. if child.tag == "Dxgcxx":
  83. Dxgcbh = child.attrib["Dxgcbh"]
  84. Dxgcmc = child.attrib["Dxgcmc"]
  85. result2 = []
  86. for child1 in child:
  87. #print("level2===================================")
  88. #print(child1.tag)
  89. #print(child1.attrib)
  90. Dwgcbh = child1.attrib["Dwgcbh"]
  91. Dwgcmc = child1.attrib["Dwgcmc"]
  92. Zylb = child1.attrib["Zylb"]
  93. result2.append({"id" : Dwgcbh,"Zylb":Zylb, "label": Dwgcmc, "children" : service.getDwgc(root, Dwgcbh, Zylb)})
  94. result.append({"id" : Dxgcbh, "label" : Dxgcmc, "children" : result2})
  95. return result
  96. class Info(BaseModel):
  97. name: str
  98. class DingeshuRequest(BaseModel):
  99. value: int
  100. class DingeXilieRequest(BaseModel):
  101. value: int
  102. id: str
  103. class SingleDingeXilieRequest(BaseModel):
  104. zhuanye: int
  105. debh: str
  106. class InfoWithID(BaseModel):
  107. name: str
  108. id: str
  109. class Tiaojia(BaseModel):
  110. biao_id: str
  111. bh: str
  112. bm: str
  113. mingcheng: str
  114. danwei: str
  115. jiage: str
  116. glf: str
  117. lr: str
  118. bz: int
  119. class SearchDe(BaseModel):
  120. zhuanye: str
  121. text: str
  122. class UpdateZjcs(BaseModel):
  123. biao_id: str
  124. bh: str
  125. mc: str
  126. fl: str
  127. @app.post("/outline")
  128. async def read_root(info: Info):
  129. for root, dirs, files in os.walk("./data", topdown=False):
  130. for name in files:
  131. if name == info.name:
  132. print(os.path.join(root, name))
  133. file_data = os.path.join(root, name)
  134. tree = ET.parse(file_data)
  135. root = tree.getroot()
  136. return getOutline(root)
  137. @app.post("/outline2")
  138. async def read_root2(info: Info):
  139. return await db.getOutline(client, info.name)
  140. @app.post("/detail")
  141. async def read_detail(info: Info):
  142. for root, dirs, files in os.walk("./data", topdown=False):
  143. for name in files:
  144. if name == info.name:
  145. print(os.path.join(root, name))
  146. file_data = os.path.join(root, name)
  147. tree = ET.parse(file_data)
  148. root = tree.getroot()
  149. return getDetail(root)
  150. @app.post("/detail2")
  151. async def read_detail2(info: Info):
  152. return await db.getDetail(client, info.name)
  153. @app.post("/baojiahuizong/")
  154. async def read_bjhz(info: InfoWithID):
  155. for root, dirs, files in os.walk("./data", topdown=False):
  156. for name in files:
  157. if name == info.name:
  158. print(os.path.join(root, name))
  159. file_data = os.path.join(root, name)
  160. tree = ET.parse(file_data)
  161. root = tree.getroot()
  162. return service.getBjhz(root, info.id)
  163. @app.post("/baojiahuizong2/")
  164. async def read_bjhz2(info: InfoWithID):
  165. raw = await db.getBjhz(client, info.name, info.id)
  166. raw2 = []
  167. for entry in raw:
  168. if "__children" in entry:
  169. entry["_children"] = entry["__children"]
  170. del entry["__children"]
  171. raw2.append(entry)
  172. return raw2
  173. @app.post("/guifeishuijin/")
  174. async def read_gfsj(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.getGfsj(root, info.id)
  183. @app.post("/guifeishuijin2/")
  184. async def read_gfsj2(info: InfoWithID):
  185. raw = await db.getGfsj(client, info.name, info.id)
  186. raw2 = []
  187. for entry in raw:
  188. if "__children" in entry:
  189. entry["_children"] = entry["__children"]
  190. del entry["__children"]
  191. raw2.append(entry)
  192. return raw2
  193. @app.post("/qitaxiangmu/")
  194. async def read_qtxm(info: InfoWithID):
  195. for root, dirs, files in os.walk("./data", topdown=False):
  196. for name in files:
  197. if name == info.name:
  198. print(os.path.join(root, name))
  199. file_data = os.path.join(root, name)
  200. tree = ET.parse(file_data)
  201. root = tree.getroot()
  202. return service.getQtxm(root, info.id)
  203. @app.post("/qitaxiangmu2/")
  204. async def read_qtxm2(info: InfoWithID):
  205. return await db.getQtxm(client, info.name, info.id)
  206. @app.post("/zygczgj/")
  207. async def zygczgj(info: InfoWithID):
  208. return await db.getZygczgj(client, info.name, info.id)
  209. @app.post("/zanliejine/")
  210. async def read_zlje(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.getZlje(root, info.id)
  219. @app.post("/zanliejine2/")
  220. async def read_zlje2(info: InfoWithID):
  221. return await db.getZlje(client, info.name, info.id)
  222. @app.post("/jirigong/")
  223. async def read_jrg(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.getJrg(root, info.id)
  232. @app.post("/jirigong2/")
  233. async def read_jrg2(info: InfoWithID):
  234. return await db.getJrg(client, info.name, info.id)
  235. @app.post("/zongchengbaofuwufei/")
  236. async def read_zcbfwf(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.getZcbfwf(root, info.id)
  245. @app.post("/zongchengbaofuwufei2/")
  246. async def read_zcbfwf2(info: InfoWithID):
  247. return await db.getZcbfwf(client, info.name, info.id)
  248. @app.post("/fabaorengongyingcailiao/")
  249. async def read_fbrgycl(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.getFbrgycl(root, info.id)
  258. @app.post("/fabaorengongyingcailiao2/")
  259. async def read_fbrgycl2(info: InfoWithID):
  260. return await db.getFbrgycl(client, info.name, info.id)
  261. @app.post("/rencaijihuizong/")
  262. async def read_rcjhz(info: InfoWithID):
  263. for root, dirs, files in os.walk("./data", topdown=False):
  264. for name in files:
  265. if name == info.name:
  266. print(os.path.join(root, name))
  267. file_data = os.path.join(root, name)
  268. tree = ET.parse(file_data)
  269. root = tree.getroot()
  270. return service.getRcjhz(root, info.id)
  271. @app.post("/rencaijihuizong2/")
  272. async def read_rcjhz2(info: InfoWithID):
  273. return await db.getRcjhz(client, info.name, info.id)
  274. @app.post("/qingdanxiangmu/")
  275. async def read_qdxm(info: InfoWithID):
  276. for root, dirs, files in os.walk("./data", topdown=False):
  277. for name in files:
  278. if name == info.name:
  279. print(os.path.join(root, name))
  280. file_data = os.path.join(root, name)
  281. tree = ET.parse(file_data)
  282. root = tree.getroot()
  283. return service.getQdxm(root, info.id)
  284. #return []
  285. @app.post("/qingdanxiangmu2/")
  286. async def read_qdxm2(info: InfoWithID):
  287. return await db.getQdxm(client, info.name, info.id)
  288. class Item(BaseModel):
  289. bh: str
  290. bt: str
  291. name: str
  292. class Rcj(BaseModel):
  293. bh: str
  294. bt: str
  295. bm: str
  296. name: str
  297. class Dercj(BaseModel):
  298. bh: str
  299. bt: str
  300. qdbm: str
  301. debm: str
  302. name: str
  303. class Zjcs(BaseModel):
  304. bh: str
  305. name: str
  306. @app.post("/qingdanmingxi/")
  307. async def read_qdmx(item : Item):
  308. return await db.getQdmx(client, item.name, item.bh, item.bt)
  309. @app.post("/qingdanrcj/")
  310. async def read_rcj(item : Rcj):
  311. if item.bt == "Djcs":
  312. return await db.getDjcsQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  313. return await db.getQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  314. @app.post("/qingdantuijian/")
  315. async def read_tuijian(item : Rcj):
  316. return service.getQingdanTuijian(item.bh, item.bt, item.bm)
  317. @app.post("/dingercj/")
  318. async def read_dercj(item : Dercj):
  319. if item.bt == "Djcs":
  320. return await db.getDjcsDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  321. return await db.getDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  322. @app.post("/zjcs/")
  323. async def read_zjcs(item : Zjcs):
  324. raw = await db.getZjcs(client, item.name, item.bh)
  325. raw2 = []
  326. for entry in raw:
  327. if "__children" in entry:
  328. entry["_children"] = entry["__children"]
  329. del entry["__children"]
  330. raw2.append(entry)
  331. return raw2
  332. @app.post("/djcs/")
  333. async def read_djcs(item : Zjcs):
  334. raw = await db.getDjcs(client, item.name, item.bh)
  335. raw2 = []
  336. for entry in raw:
  337. if "__children" in entry:
  338. entry["_children"] = entry["__children"]
  339. del entry["__children"]
  340. raw2.append(entry)
  341. return raw2
  342. @app.post("/files/")
  343. async def read_files():
  344. result = []
  345. for root, dirs, files in os.walk("./data", topdown=False):
  346. for name in files:
  347. print(os.path.join(root, name))
  348. result.append([name, "", ""])
  349. return result
  350. @app.post("/files2/")
  351. async def read_files2():
  352. result = await db.list_files(client)
  353. return result
  354. @app.post("/des/")
  355. async def read_des(r: DingeshuRequest):
  356. result = service.getDes(r.value)
  357. #print(result)
  358. return result
  359. @app.post("/pbs/")
  360. async def read_pbs(r: DingeshuRequest):
  361. result = service.getPbs(r.value)
  362. #print(result)
  363. result.insert(0, {"id": "0", "label": "全部"})
  364. return result
  365. @app.post("/pbxl/")
  366. async def read_pbxl(r: Info):
  367. result = service.getPbxl(r.name)
  368. #print(result)
  369. return result
  370. @app.post("/cankao/")
  371. async def cankao():
  372. result = service.getCankao()
  373. #print(result)
  374. return result
  375. @app.post("/qufei/")
  376. async def read_qufei(r: Info):
  377. return await db.getQufei(client, r.name)
  378. @app.post("/dexilie/")
  379. async def read_dexilie(r: DingeXilieRequest):
  380. result = service.getDeXilie(r.value, r.id)
  381. #print(result)
  382. return result
  383. @app.post("/singledexilie/")
  384. async def read_singledexilie(r: SingleDingeXilieRequest):
  385. if r.debh.startswith("D") :
  386. return json.dumps({
  387. "reverse": "None",
  388. "rgde": None,
  389. "jxde": None,
  390. "clde": None,
  391. "actual_zhuanye": r.zhuanye,
  392. "bz_selected": {"BZBH": {}},
  393. "bz_selected2": {"BZBH": {}}
  394. }, ensure_ascii=False)
  395. result1, result2, rgde, jxde, clde, bz_selected, bz_selected2, actual_zhuanye= service.getSingleDeXilie(r.zhuanye, r.debh)
  396. print("get result ***************************************")
  397. if result1:
  398. result3 = json.loads(result1)
  399. else:
  400. result3 = {}
  401. result3["reverse"] = str(result2)
  402. result3["rgde"] = rgde
  403. result3["jxde"] = jxde
  404. result3["clde"] = clde
  405. result3["actual_zhuanye"] = actual_zhuanye
  406. if bz_selected != None:
  407. result3["bz_selected"] = json.loads(bz_selected)
  408. else:
  409. result3["bz_selected"] = {"BZBH": {}}
  410. if bz_selected2 != None:
  411. result3["bz_selected2"] = json.loads(bz_selected2)
  412. else:
  413. result3["bz_selected2"] = {"BZBH": {}}
  414. if "[" in r.debh:
  415. position3 = r.debh.find("*")
  416. coef = 1
  417. if position3 == -1:
  418. pass
  419. else:
  420. coef = r.debh[1 + position3:]
  421. print(coef)
  422. tail = 0
  423. for i in range(0, len(coef) + 1):
  424. if i == len(coef):
  425. tail = i
  426. break
  427. if coef[i] > '9' or coef[i] < '0':
  428. tail = i
  429. break
  430. if tail == 0:
  431. print("1113111")
  432. result3["reverse"] = 'None'
  433. return json.dumps(result3, ensure_ascii=False)
  434. coef = int(coef[0:tail])
  435. position1 = r.debh.find("[")
  436. position2 = r.debh.find("]")
  437. if position2 == -1:
  438. print("1121111")
  439. result3["reverse"] = 'None'
  440. return json.dumps(result3, ensure_ascii=False)
  441. if r.debh[position1-1] == "+":
  442. pass
  443. elif r.debh[position1-1] == "-":
  444. coef = -coef
  445. else:
  446. print("111111")
  447. result3["reverse"] = 'None'
  448. return json.dumps(result3, ensure_ascii=False)
  449. debh = r.debh[position1+1: position2]
  450. if result3['GLDE']:
  451. hit = False
  452. for key, value in result3['GLDE'].items():
  453. if value == debh:
  454. hit = True
  455. if not hit:
  456. result3["reverse"] = 'None'
  457. return json.dumps(result3, ensure_ascii=False)
  458. else:
  459. result3["reverse"] = 'None'
  460. return json.dumps(result3, ensure_ascii=False)
  461. result1_, result2_, rgde_, jxde_, clde_, bz_selected_, bz_selected2_, actual_zhuanye_ = service.getSingleDeXilie(r.zhuanye, debh)
  462. if result1_:
  463. util.mergerg(rgde, rgde_, coef)
  464. util.mergejx(jxde, jxde_, coef)
  465. util.mergecl(clde, clde_, coef)
  466. result3["rgde"] = rgde
  467. result3["jxde"] = jxde
  468. result3["clde"] = clde
  469. item_ = None
  470. for item in result3['DW'].keys():
  471. item_ = item
  472. result3['DEBH'] = {item_: util.cleanBM(r.debh)}
  473. else:
  474. result3["reverse"] = 'None'
  475. return json.dumps(result3, ensure_ascii=False)
  476. @app.post("/save/")
  477. async def save(r: Info):
  478. data = json.loads(r.name)
  479. print(data)
  480. return await db.save(client, data)
  481. @app.post("/savedjcs/")
  482. async def savedjcs(r: Info):
  483. data = json.loads(r.name)
  484. print(data)
  485. return await db.savedjcs(client, data)
  486. @app.post("/applyFL/")
  487. async def applyFL(r: InfoWithID):
  488. data = json.loads(r.name)
  489. print(data)
  490. return await db.applyFL(client, r.id, data)
  491. @app.post("/tiaojia/")
  492. async def tiaojia(r: Tiaojia):
  493. ##return []
  494. glf = r.glf
  495. lr = r.lr
  496. if r.glf == '':
  497. glf = '0'
  498. if r.lr == "":
  499. lr = '0'
  500. return await db.tiaojia(client, r.biao_id, r.bh, r.bm, r.mingcheng, r.danwei, r.jiage, glf, lr, r.bz)
  501. @app.post("/searchde/")
  502. async def searchde(r: SearchDe):
  503. ##return []
  504. return await db.searchde(client, r.zhuanye, r.text)
  505. @app.post("/updatezjcs/")
  506. async def updatezjcs(r: UpdateZjcs):
  507. ##return []
  508. return await db.updatezjcs(client, r.biao_id, r.bh, r.mc, r.fl)
  509. async def resolve(websocket, data):
  510. await db.resolve(manager, websocket, data, client)##manager.send_personal_message(f"You wrote: {data}", websocket)
  511. @app.websocket("/ws")
  512. async def websocket_endpoint(websocket: WebSocket):
  513. await manager.connect(websocket)
  514. try:
  515. while True:
  516. data = await websocket.receive_text()
  517. await resolve(websocket, data)
  518. ##await manager.broadcast(f"Client #{client_id} says: {data}")
  519. except WebSocketDisconnect:
  520. manager.disconnect(websocket)
  521. ##await manager.broadcast(f"Client left the chat")