main.py 18 KB

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