main.py 20 KB

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