main.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. for grandchild in entry["_children"]:
  171. if "__children" in grandchild:
  172. grandchild['_children'] = grandchild['__children']
  173. del grandchild['__children']
  174. del entry["__children"]
  175. raw2.append(entry)
  176. return raw2
  177. @app.post("/guifeishuijin/")
  178. async def read_gfsj(info: InfoWithID):
  179. for root, dirs, files in os.walk("./data", topdown=False):
  180. for name in files:
  181. if name == info.name:
  182. print(os.path.join(root, name))
  183. file_data = os.path.join(root, name)
  184. tree = ET.parse(file_data)
  185. root = tree.getroot()
  186. return service.getGfsj(root, info.id)
  187. @app.post("/guifeishuijin2/")
  188. async def read_gfsj2(info: InfoWithID):
  189. raw = await db.getGfsj(client, info.name, info.id)
  190. raw2 = []
  191. for entry in raw:
  192. if "__children" in entry:
  193. entry["_children"] = entry["__children"]
  194. del entry["__children"]
  195. raw2.append(entry)
  196. return raw2
  197. @app.post("/qitaxiangmu/")
  198. async def read_qtxm(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.getQtxm(root, info.id)
  207. @app.post("/qitaxiangmu2/")
  208. async def read_qtxm2(info: InfoWithID):
  209. return await db.getQtxm(client, info.name, info.id)
  210. @app.post("/zygczgj/")
  211. async def zygczgj(info: InfoWithID):
  212. return await db.getZygczgj(client, info.name, info.id)
  213. @app.post("/zanliejine/")
  214. async def read_zlje(info: InfoWithID):
  215. for root, dirs, files in os.walk("./data", topdown=False):
  216. for name in files:
  217. if name == info.name:
  218. print(os.path.join(root, name))
  219. file_data = os.path.join(root, name)
  220. tree = ET.parse(file_data)
  221. root = tree.getroot()
  222. return service.getZlje(root, info.id)
  223. @app.post("/zanliejine2/")
  224. async def read_zlje2(info: InfoWithID):
  225. return await db.getZlje(client, info.name, info.id)
  226. @app.post("/jirigong/")
  227. async def read_jrg(info: InfoWithID):
  228. for root, dirs, files in os.walk("./data", topdown=False):
  229. for name in files:
  230. if name == info.name:
  231. print(os.path.join(root, name))
  232. file_data = os.path.join(root, name)
  233. tree = ET.parse(file_data)
  234. root = tree.getroot()
  235. return service.getJrg(root, info.id)
  236. @app.post("/jirigong2/")
  237. async def read_jrg2(info: InfoWithID):
  238. return await db.getJrg(client, info.name, info.id)
  239. @app.post("/zongchengbaofuwufei/")
  240. async def read_zcbfwf(info: InfoWithID):
  241. for root, dirs, files in os.walk("./data", topdown=False):
  242. for name in files:
  243. if name == info.name:
  244. print(os.path.join(root, name))
  245. file_data = os.path.join(root, name)
  246. tree = ET.parse(file_data)
  247. root = tree.getroot()
  248. return service.getZcbfwf(root, info.id)
  249. @app.post("/zongchengbaofuwufei2/")
  250. async def read_zcbfwf2(info: InfoWithID):
  251. return await db.getZcbfwf(client, info.name, info.id)
  252. @app.post("/fabaorengongyingcailiao/")
  253. async def read_fbrgycl(info: InfoWithID):
  254. for root, dirs, files in os.walk("./data", topdown=False):
  255. for name in files:
  256. if name == info.name:
  257. print(os.path.join(root, name))
  258. file_data = os.path.join(root, name)
  259. tree = ET.parse(file_data)
  260. root = tree.getroot()
  261. return service.getFbrgycl(root, info.id)
  262. @app.post("/fabaorengongyingcailiao2/")
  263. async def read_fbrgycl2(info: InfoWithID):
  264. return await db.getFbrgycl(client, info.name, info.id)
  265. @app.post("/rencaijihuizong/")
  266. async def read_rcjhz(info: InfoWithID):
  267. for root, dirs, files in os.walk("./data", topdown=False):
  268. for name in files:
  269. if name == info.name:
  270. print(os.path.join(root, name))
  271. file_data = os.path.join(root, name)
  272. tree = ET.parse(file_data)
  273. root = tree.getroot()
  274. return service.getRcjhz(root, info.id)
  275. @app.post("/rencaijihuizong2/")
  276. async def read_rcjhz2(info: InfoWithID):
  277. return await db.getRcjhz(client, info.name, info.id)
  278. @app.post("/qingdanxiangmu/")
  279. async def read_qdxm(info: InfoWithID):
  280. for root, dirs, files in os.walk("./data", topdown=False):
  281. for name in files:
  282. if name == info.name:
  283. print(os.path.join(root, name))
  284. file_data = os.path.join(root, name)
  285. tree = ET.parse(file_data)
  286. root = tree.getroot()
  287. return service.getQdxm(root, info.id)
  288. #return []
  289. @app.post("/qingdanxiangmu2/")
  290. async def read_qdxm2(info: InfoWithID):
  291. return await db.getQdxm(client, info.name, info.id)
  292. class Item(BaseModel):
  293. bh: str
  294. bt: str
  295. name: str
  296. class Rcj(BaseModel):
  297. bh: str
  298. bt: str
  299. bm: str
  300. name: str
  301. class Dercj(BaseModel):
  302. bh: str
  303. bt: str
  304. qdbm: str
  305. debm: str
  306. name: str
  307. class Zjcs(BaseModel):
  308. bh: str
  309. name: str
  310. @app.post("/qingdanmingxi/")
  311. async def read_qdmx(item : Item):
  312. return await db.getQdmx(client, item.name, item.bh, item.bt)
  313. @app.post("/qingdanrcj/")
  314. async def read_rcj(item : Rcj):
  315. if item.bt == "Djcs":
  316. return await db.getDjcsQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  317. return await db.getQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  318. @app.post("/qingdantuijian/")
  319. async def read_tuijian(item : Rcj):
  320. return service.getQingdanTuijian(item.bh, item.bt, item.bm)
  321. @app.post("/dingercj/")
  322. async def read_dercj(item : Dercj):
  323. if item.bt == "Djcs":
  324. return await db.getDjcsDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  325. return await db.getDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  326. @app.post("/zjcs/")
  327. async def read_zjcs(item : Zjcs):
  328. raw = await db.getZjcs(client, item.name, item.bh)
  329. raw2 = []
  330. for entry in raw:
  331. if "__children" in entry:
  332. entry["_children"] = entry["__children"]
  333. del entry["__children"]
  334. raw2.append(entry)
  335. return raw2
  336. @app.post("/djcs/")
  337. async def read_djcs(item : Zjcs):
  338. raw = await db.getDjcs(client, item.name, item.bh)
  339. raw2 = []
  340. for entry in raw:
  341. if "__children" in entry:
  342. entry["_children"] = entry["__children"]
  343. del entry["__children"]
  344. raw2.append(entry)
  345. return raw2
  346. @app.post("/files/")
  347. async def read_files():
  348. result = []
  349. for root, dirs, files in os.walk("./data", topdown=False):
  350. for name in files:
  351. print(os.path.join(root, name))
  352. result.append([name, "", ""])
  353. return result
  354. @app.post("/files2/")
  355. async def read_files2():
  356. result = await db.list_files(client)
  357. return result
  358. @app.post("/des/")
  359. async def read_des(r: DingeshuRequest):
  360. result = service.getDes(r.value)
  361. #print(result)
  362. return result
  363. @app.post("/pbs/")
  364. async def read_pbs(r: DingeshuRequest):
  365. result = service.getPbs(r.value)
  366. #print(result)
  367. result.insert(0, {"id": "0", "label": "全部"})
  368. return result
  369. @app.post("/pbxl/")
  370. async def read_pbxl(r: Info):
  371. result = service.getPbxl(r.name)
  372. #print(result)
  373. return result
  374. @app.post("/cankao/")
  375. async def cankao():
  376. result = service.getCankao()
  377. #print(result)
  378. return result
  379. @app.post("/qufei/")
  380. async def read_qufei(r: Info):
  381. return await db.getQufei(client, r.name)
  382. @app.post("/dexilie/")
  383. async def read_dexilie(r: DingeXilieRequest):
  384. result = service.getDeXilie(r.value, r.id)
  385. #print(result)
  386. return result
  387. @app.post("/singledexilie/")
  388. async def read_singledexilie(r: SingleDingeXilieRequest):
  389. if r.debh.startswith("D") :
  390. return json.dumps({
  391. "reverse": "None",
  392. "rgde": None,
  393. "jxde": None,
  394. "clde": None,
  395. "actual_zhuanye": r.zhuanye,
  396. "bz_selected": {"BZBH": {}},
  397. "bz_selected2": {"BZBH": {}}
  398. }, ensure_ascii=False)
  399. result1, result2, rgde, jxde, clde, bz_selected, bz_selected2, actual_zhuanye= service.getSingleDeXilie(r.zhuanye, r.debh)
  400. print("get result ***************************************")
  401. if result1:
  402. result3 = json.loads(result1)
  403. else:
  404. result3 = {}
  405. result3["reverse"] = str(result2)
  406. result3["rgde"] = rgde
  407. result3["jxde"] = jxde
  408. result3["clde"] = clde
  409. result3["actual_zhuanye"] = actual_zhuanye
  410. if bz_selected != None:
  411. result3["bz_selected"] = json.loads(bz_selected)
  412. else:
  413. result3["bz_selected"] = {"BZBH": {}}
  414. if bz_selected2 != None:
  415. result3["bz_selected2"] = json.loads(bz_selected2)
  416. else:
  417. result3["bz_selected2"] = {"BZBH": {}}
  418. if "[" in r.debh:
  419. position3 = r.debh.find("*")
  420. coef = 1
  421. if position3 == -1:
  422. pass
  423. else:
  424. coef = r.debh[1 + position3:]
  425. print(coef)
  426. tail = 0
  427. for i in range(0, len(coef) + 1):
  428. if i == len(coef):
  429. tail = i
  430. break
  431. if coef[i] > '9' or coef[i] < '0':
  432. tail = i
  433. break
  434. if tail == 0:
  435. print("1113111")
  436. result3["reverse"] = 'None'
  437. return json.dumps(result3, ensure_ascii=False)
  438. coef = int(coef[0:tail])
  439. position1 = r.debh.find("[")
  440. position2 = r.debh.find("]")
  441. if position2 == -1:
  442. print("1121111")
  443. result3["reverse"] = 'None'
  444. return json.dumps(result3, ensure_ascii=False)
  445. if r.debh[position1-1] == "+":
  446. pass
  447. elif r.debh[position1-1] == "-":
  448. coef = -coef
  449. else:
  450. print("111111")
  451. result3["reverse"] = 'None'
  452. return json.dumps(result3, ensure_ascii=False)
  453. debh = r.debh[position1+1: position2]
  454. if result3['GLDE']:
  455. hit = False
  456. for key, value in result3['GLDE'].items():
  457. if value == debh:
  458. hit = True
  459. if not hit:
  460. result3["reverse"] = 'None'
  461. return json.dumps(result3, ensure_ascii=False)
  462. else:
  463. result3["reverse"] = 'None'
  464. return json.dumps(result3, ensure_ascii=False)
  465. result1_, result2_, rgde_, jxde_, clde_, bz_selected_, bz_selected2_, actual_zhuanye_ = service.getSingleDeXilie(r.zhuanye, debh)
  466. if result1_:
  467. util.mergerg(rgde, rgde_, coef)
  468. util.mergejx(jxde, jxde_, coef)
  469. util.mergecl(clde, clde_, coef)
  470. result3["rgde"] = rgde
  471. result3["jxde"] = jxde
  472. result3["clde"] = clde
  473. item_ = None
  474. for item in result3['DW'].keys():
  475. item_ = item
  476. result3['DEBH'] = {item_: util.cleanBM(r.debh)}
  477. else:
  478. result3["reverse"] = 'None'
  479. return json.dumps(result3, ensure_ascii=False)
  480. @app.post("/save/")
  481. async def save(r: Info):
  482. data = json.loads(r.name)
  483. print(data)
  484. return await db.save(client, data)
  485. @app.post("/savedjcs/")
  486. async def savedjcs(r: Info):
  487. data = json.loads(r.name)
  488. print(data)
  489. return await db.savedjcs(client, data)
  490. @app.post("/applyFL/")
  491. async def applyFL(r: InfoWithID):
  492. data = json.loads(r.name)
  493. print(data)
  494. return await db.applyFL(client, r.id, data)
  495. @app.post("/tiaojia/")
  496. async def tiaojia(r: Tiaojia):
  497. ##return []
  498. glf = r.glf
  499. lr = r.lr
  500. if r.glf == '':
  501. glf = '0'
  502. if r.lr == "":
  503. lr = '0'
  504. return await db.tiaojia(client, r.biao_id, r.bh, r.bm, r.mingcheng, r.danwei, r.jiage, glf, lr, r.bz)
  505. @app.post("/searchde/")
  506. async def searchde(r: SearchDe):
  507. ##return []
  508. return await db.searchde(client, r.zhuanye, r.text)
  509. @app.post("/updatezjcs/")
  510. async def updatezjcs(r: UpdateZjcs):
  511. ##return []
  512. return await db.updatezjcs(client, r.biao_id, r.bh, r.mc, r.fl)
  513. async def resolve(websocket, data):
  514. await db.resolve(manager, websocket, data, client)##manager.send_personal_message(f"You wrote: {data}", websocket)
  515. @app.websocket("/ws")
  516. async def websocket_endpoint(websocket: WebSocket):
  517. await manager.connect(websocket)
  518. try:
  519. while True:
  520. data = await websocket.receive_text()
  521. await resolve(websocket, data)
  522. ##await manager.broadcast(f"Client #{client_id} says: {data}")
  523. except WebSocketDisconnect:
  524. manager.disconnect(websocket)
  525. ##await manager.broadcast(f"Client left the chat")