main.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. @app.post("/outline")
  123. async def read_root(info: Info):
  124. for root, dirs, files in os.walk("./data", topdown=False):
  125. for name in files:
  126. if name == info.name:
  127. print(os.path.join(root, name))
  128. file_data = os.path.join(root, name)
  129. tree = ET.parse(file_data)
  130. root = tree.getroot()
  131. return getOutline(root)
  132. @app.post("/outline2")
  133. async def read_root2(info: Info):
  134. return await db.getOutline(client, info.name)
  135. @app.post("/detail")
  136. async def read_detail(info: Info):
  137. for root, dirs, files in os.walk("./data", topdown=False):
  138. for name in files:
  139. if name == info.name:
  140. print(os.path.join(root, name))
  141. file_data = os.path.join(root, name)
  142. tree = ET.parse(file_data)
  143. root = tree.getroot()
  144. return getDetail(root)
  145. @app.post("/detail2")
  146. async def read_detail2(info: Info):
  147. return await db.getDetail(client, info.name)
  148. @app.post("/baojiahuizong/")
  149. async def read_bjhz(info: InfoWithID):
  150. for root, dirs, files in os.walk("./data", topdown=False):
  151. for name in files:
  152. if name == info.name:
  153. print(os.path.join(root, name))
  154. file_data = os.path.join(root, name)
  155. tree = ET.parse(file_data)
  156. root = tree.getroot()
  157. return service.getBjhz(root, info.id)
  158. @app.post("/baojiahuizong2/")
  159. async def read_bjhz2(info: InfoWithID):
  160. return await db.getBjhz(client, info.name, info.id)
  161. @app.post("/guifeishuijin/")
  162. async def read_gfsj(info: InfoWithID):
  163. for root, dirs, files in os.walk("./data", topdown=False):
  164. for name in files:
  165. if name == info.name:
  166. print(os.path.join(root, name))
  167. file_data = os.path.join(root, name)
  168. tree = ET.parse(file_data)
  169. root = tree.getroot()
  170. return service.getGfsj(root, info.id)
  171. @app.post("/guifeishuijin2/")
  172. async def read_gfsj2(info: InfoWithID):
  173. return await db.getGfsj(client, info.name, info.id)
  174. @app.post("/qitaxiangmu/")
  175. async def read_qtxm(info: InfoWithID):
  176. for root, dirs, files in os.walk("./data", topdown=False):
  177. for name in files:
  178. if name == info.name:
  179. print(os.path.join(root, name))
  180. file_data = os.path.join(root, name)
  181. tree = ET.parse(file_data)
  182. root = tree.getroot()
  183. return service.getQtxm(root, info.id)
  184. @app.post("/qitaxiangmu2/")
  185. async def read_qtxm2(info: InfoWithID):
  186. return await db.getQtxm(client, info.name, info.id)
  187. @app.post("/zanliejine/")
  188. async def read_zlje(info: InfoWithID):
  189. for root, dirs, files in os.walk("./data", topdown=False):
  190. for name in files:
  191. if name == info.name:
  192. print(os.path.join(root, name))
  193. file_data = os.path.join(root, name)
  194. tree = ET.parse(file_data)
  195. root = tree.getroot()
  196. return service.getZlje(root, info.id)
  197. @app.post("/zanliejine2/")
  198. async def read_zlje2(info: InfoWithID):
  199. return await db.getZlje(client, info.name, info.id)
  200. @app.post("/jirigong/")
  201. async def read_jrg(info: InfoWithID):
  202. for root, dirs, files in os.walk("./data", topdown=False):
  203. for name in files:
  204. if name == info.name:
  205. print(os.path.join(root, name))
  206. file_data = os.path.join(root, name)
  207. tree = ET.parse(file_data)
  208. root = tree.getroot()
  209. return service.getJrg(root, info.id)
  210. @app.post("/jirigong2/")
  211. async def read_jrg2(info: InfoWithID):
  212. return await db.getJrg(client, info.name, info.id)
  213. @app.post("/zongchengbaofuwufei/")
  214. async def read_zcbfwf(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.getZcbfwf(root, info.id)
  223. @app.post("/zongchengbaofuwufei2/")
  224. async def read_zcbfwf2(info: InfoWithID):
  225. return await db.getZcbfwf(client, info.name, info.id)
  226. @app.post("/fabaorengongyingcailiao/")
  227. async def read_fbrgycl(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.getFbrgycl(root, info.id)
  236. @app.post("/fabaorengongyingcailiao2/")
  237. async def read_fbrgycl2(info: InfoWithID):
  238. return await db.getFbrgycl(client, info.name, info.id)
  239. @app.post("/rencaijihuizong/")
  240. async def read_rcjhz(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.getRcjhz(root, info.id)
  249. @app.post("/rencaijihuizong2/")
  250. async def read_rcjhz2(info: InfoWithID):
  251. return await db.getRcjhz(client, info.name, info.id)
  252. @app.post("/qingdanxiangmu/")
  253. async def read_qdxm(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.getQdxm(root, info.id)
  262. #return []
  263. @app.post("/qingdanxiangmu2/")
  264. async def read_qdxm2(info: InfoWithID):
  265. return await db.getQdxm(client, info.name, info.id)
  266. class Item(BaseModel):
  267. bh: str
  268. bt: str
  269. name: str
  270. class Rcj(BaseModel):
  271. bh: str
  272. bt: str
  273. bm: str
  274. name: str
  275. class Dercj(BaseModel):
  276. bh: str
  277. bt: str
  278. qdbm: str
  279. debm: str
  280. name: str
  281. class Zjcs(BaseModel):
  282. bh: str
  283. name: str
  284. @app.post("/qingdanmingxi/")
  285. async def read_qdmx(item : Item):
  286. return await db.getQdmx(client, item.name, item.bh, item.bt)
  287. @app.post("/qingdanrcj/")
  288. async def read_rcj(item : Rcj):
  289. if item.bt == "Djcs":
  290. return await db.getDjcsQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  291. return await db.getQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  292. @app.post("/qingdantuijian/")
  293. async def read_tuijian(item : Rcj):
  294. return service.getQingdanTuijian(item.bh, item.bt, item.bm)
  295. @app.post("/dingercj/")
  296. async def read_dercj(item : Dercj):
  297. if item.bt == "Djcs":
  298. return await db.getDjcsDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  299. return await db.getDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  300. @app.post("/zjcs/")
  301. async def read_zjcs(item : Zjcs):
  302. return await db.getZjcs(client, item.name, item.bh)
  303. @app.post("/djcs/")
  304. async def read_djcs(item : Zjcs):
  305. raw = await db.getDjcs(client, item.name, item.bh)
  306. raw2 = []
  307. for entry in raw:
  308. if "__children" in entry:
  309. entry["_children"] = entry["__children"]
  310. del entry["__children"]
  311. raw2.append(entry)
  312. return raw2
  313. @app.post("/files/")
  314. async def read_files():
  315. result = []
  316. for root, dirs, files in os.walk("./data", topdown=False):
  317. for name in files:
  318. print(os.path.join(root, name))
  319. result.append([name, "", ""])
  320. return result
  321. @app.post("/files2/")
  322. async def read_files2():
  323. result = await db.list_files(client)
  324. return result
  325. @app.post("/des/")
  326. async def read_des(r: DingeshuRequest):
  327. result = service.getDes(r.value)
  328. #print(result)
  329. return result
  330. @app.post("/pbs/")
  331. async def read_pbs(r: DingeshuRequest):
  332. result = service.getPbs(r.value)
  333. #print(result)
  334. result.insert(0, {"id": "0", "label": "全部"})
  335. return result
  336. @app.post("/pbxl/")
  337. async def read_pbxl(r: Info):
  338. result = service.getPbxl(r.name)
  339. #print(result)
  340. return result
  341. @app.post("/qufei/")
  342. async def read_qufei(r: Info):
  343. return await db.getQufei(client, r.name)
  344. @app.post("/dexilie/")
  345. async def read_dexilie(r: DingeXilieRequest):
  346. result = service.getDeXilie(r.value, r.id)
  347. #print(result)
  348. return result
  349. @app.post("/singledexilie/")
  350. async def read_singledexilie(r: SingleDingeXilieRequest):
  351. if r.debh.startswith("D") :
  352. return json.dumps({
  353. "reverse": "None",
  354. "rgde": None,
  355. "jxde": None,
  356. "clde": None,
  357. "actual_zhuanye": r.zhuanye,
  358. "bz_selected": {"BZBH": {}},
  359. "bz_selected2": {"BZBH": {}}
  360. }, ensure_ascii=False)
  361. result1, result2, rgde, jxde, clde, bz_selected, bz_selected2, actual_zhuanye= service.getSingleDeXilie(r.zhuanye, r.debh)
  362. print("get result ***************************************")
  363. if result1:
  364. result3 = json.loads(result1)
  365. else:
  366. result3 = {}
  367. result3["reverse"] = str(result2)
  368. result3["rgde"] = rgde
  369. result3["jxde"] = jxde
  370. result3["clde"] = clde
  371. result3["actual_zhuanye"] = actual_zhuanye
  372. if bz_selected != None:
  373. result3["bz_selected"] = json.loads(bz_selected)
  374. else:
  375. result3["bz_selected"] = {"BZBH": {}}
  376. if bz_selected2 != None:
  377. result3["bz_selected2"] = json.loads(bz_selected2)
  378. else:
  379. result3["bz_selected2"] = {"BZBH": {}}
  380. if "[" in r.debh:
  381. position3 = r.debh.find("*")
  382. coef = 1
  383. if position3 == -1:
  384. pass
  385. else:
  386. coef = r.debh[1 + position3:]
  387. print(coef)
  388. tail = 0
  389. for i in range(0, len(coef) + 1):
  390. if i == len(coef):
  391. tail = i
  392. break
  393. if coef[i] > '9' or coef[i] < '0':
  394. tail = i
  395. break
  396. if tail == 0:
  397. print("1113111")
  398. result3["reverse"] = 'None'
  399. return json.dumps(result3, ensure_ascii=False)
  400. coef = int(coef[0:tail])
  401. position1 = r.debh.find("[")
  402. position2 = r.debh.find("]")
  403. if position2 == -1:
  404. print("1121111")
  405. result3["reverse"] = 'None'
  406. return json.dumps(result3, ensure_ascii=False)
  407. if r.debh[position1-1] == "+":
  408. pass
  409. elif r.debh[position1-1] == "-":
  410. coef = -coef
  411. else:
  412. print("111111")
  413. result3["reverse"] = 'None'
  414. return json.dumps(result3, ensure_ascii=False)
  415. debh = r.debh[position1+1: position2]
  416. if result3['GLDE']:
  417. hit = False
  418. for key, value in result3['GLDE'].items():
  419. if value == debh:
  420. hit = True
  421. if not hit:
  422. result3["reverse"] = 'None'
  423. return json.dumps(result3, ensure_ascii=False)
  424. else:
  425. result3["reverse"] = 'None'
  426. return json.dumps(result3, ensure_ascii=False)
  427. result1_, result2_, rgde_, jxde_, clde_, bz_selected_, bz_selected2_, actual_zhuanye_ = service.getSingleDeXilie(r.zhuanye, debh)
  428. if result1_:
  429. util.mergerg(rgde, rgde_, coef)
  430. util.mergejx(jxde, jxde_, coef)
  431. util.mergecl(clde, clde_, coef)
  432. result3["rgde"] = rgde
  433. result3["jxde"] = jxde
  434. result3["clde"] = clde
  435. item_ = None
  436. for item in result3['DW'].keys():
  437. item_ = item
  438. result3['DEBH'] = {item_: util.cleanBM(r.debh)}
  439. else:
  440. result3["reverse"] = 'None'
  441. return json.dumps(result3, ensure_ascii=False)
  442. @app.post("/save/")
  443. async def save(r: Info):
  444. data = json.loads(r.name)
  445. print(data)
  446. return await db.save(client, data)
  447. @app.post("/savedjcs/")
  448. async def savedjcs(r: Info):
  449. data = json.loads(r.name)
  450. print(data)
  451. return await db.savedjcs(client, data)
  452. @app.post("/applyFL/")
  453. async def applyFL(r: InfoWithID):
  454. data = json.loads(r.name)
  455. print(data)
  456. return await db.applyFL(client, r.id, data)
  457. @app.post("/tiaojia/")
  458. async def tiaojia(r: Tiaojia):
  459. ##return []
  460. glf = r.glf
  461. lr = r.lr
  462. if r.glf == '':
  463. glf = '0'
  464. if r.lr == "":
  465. lr = '0'
  466. return await db.tiaojia(client, r.biao_id, r.bh, r.bm, r.mingcheng, r.danwei, r.jiage, glf, lr, r.bz)
  467. @app.post("/searchde/")
  468. async def searchde(r: SearchDe):
  469. ##return []
  470. return await db.searchde(client, r.zhuanye, r.text)
  471. async def resolve(websocket, data):
  472. await db.resolve(manager, websocket, data, client)##manager.send_personal_message(f"You wrote: {data}", websocket)
  473. @app.websocket("/ws")
  474. async def websocket_endpoint(websocket: WebSocket):
  475. await manager.connect(websocket)
  476. try:
  477. while True:
  478. data = await websocket.receive_text()
  479. await resolve(websocket, data)
  480. ##await manager.broadcast(f"Client #{client_id} says: {data}")
  481. except WebSocketDisconnect:
  482. manager.disconnect(websocket)
  483. ##await manager.broadcast(f"Client left the chat")