main.py 19 KB

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