ソースを参照

support pretty xml

Xiaopeng Zhang 6 ヶ月 前
コミット
cfe5c47531
3 ファイル変更232 行追加9 行削除
  1. 7 2
      main.py
  2. 188 7
      subdir/db.py
  3. 37 0
      subdir/service.py

+ 7 - 2
main.py

@@ -13,7 +13,7 @@ from pydantic import BaseModel
 from subdir import service
 from subdir import db
 from subdir import util
-
+import xml.dom.minidom as MD
 import numpy as np
 from fastapi.staticfiles import StaticFiles
 from pymongo import AsyncMongoClient
@@ -674,7 +674,7 @@ async def upload(file: UploadFile):
         
 @app.get("/download/{item_id}")
 async def download_file(item_id):
-    file_path = "export/contacts.xml"
+    file_path = "export/contacts.xml.b"
     # Create the root element
     root = ET.Element("JingJiBiao")
 
@@ -687,6 +687,11 @@ async def download_file(item_id):
     # Create the tree and write to a file
     tree = ET.ElementTree(root)
     tree.write("export/contacts.xml", xml_declaration=True, encoding="UTF-8")
+    dom = MD.parse("export/contacts.xml")
+    with open("export/contacts.xml.b", 'w') as f:
+        f.write(dom.toprettyxml(indent="  "))
+    
+
     return FileResponse(file_path, media_type='application/octet-stream', filename="contacts.xml")
         
 

+ 188 - 7
subdir/db.py

@@ -365,6 +365,7 @@ async def resolve(data, client):
         rcjhz = service.getRcjhz(root, entry["bh"])
         zjcs = service.getZjcs(root, entry["bh"])
         qdbt = service.getQdxm(root, entry["bh"])
+        qdbt_addition = service.getQdxm_addition(root, entry["bh"])
         zygczgj = service.getZygczgj(root, entry["bh"])
         Dwgc = {}
         Dwgc["bjhz"] = bjhz
@@ -378,6 +379,7 @@ async def resolve(data, client):
         Dwgc["rcjhz"] = rcjhz
         Dwgc["zjcs"] = zjcs
         Dwgc["qdbt"] = qdbt
+        Dwgc['qdbt_addition'] = qdbt_addition
         Dwgc["DogNum"] = entry["num"]
         Dwgc["Dwgcbh"] = entry["bh"]
         Dwgc["biao_id"] = biao_id
@@ -559,6 +561,158 @@ async def build(client, root, id):
                 GfsjMx.set('Je', str(gfsj_[5]))
                 GfsjMx.set('Fyxlb', str(gfsj_[6]))
             Qdxm = ET.SubElement(Dwgcxx, 'Qdxm')
+            if 'Default' in qdbt:
+                collection = db["qdxm"]
+                before_sort = []
+                async for post in collection.find({"biao_id": id, "Dwgcbh": entry['Dwgcbh']}):
+                    before_sort.append(post)
+                after_sort = sorted(before_sort, key=lambda x: int(x['序号']))
+                for qdmx in after_sort:
+                    Qdmx = ET.SubElement(Qdxm, 'Qdmx')
+                    Qdmx.set('Xh', str(qdmx['序号']))
+                    Qdmx.set('Qdbm', str(qdmx['清单编码']))
+                    Qdmx.set('Mc', str(qdmx['名称']))
+                    Qdmx.set('Xmtz', str(qdmx['项目特征']))
+                    Qdmx.set('Jsgz', str(qdmx['计算规则']))
+                    Qdmx.set('Dw', str(qdmx['单位']))
+                    Qdmx.set('Sl', str(qdmx['数量']))
+                    Qdmx.set('Zhdj', str(qdmx['综合单价']))
+                    Qdmx.set('Zhhj', str(qdmx['合价']))
+                    Qdmx.set('Rgf', str(qdmx['人工费']))
+                    Qdmx.set('Zcf', str(qdmx['主材费']))
+                    Qdmx.set('Sbf', str(qdmx['设备费']))
+                    Qdmx.set('Fcf', str(qdmx['辅材费']))
+                    Qdmx.set('Clf', str(qdmx['材料费']))
+                    Qdmx.set('Jxf', str(qdmx['机械费']))
+                    Qdmx.set('Glf', str(qdmx['管理费']))
+                    Qdmx.set('Lr', str(qdmx['利润']))
+                    Qdmx.set('Zgj', str(qdmx['暂估价']))
+                    Qdmx.set('Zgr', str(qdmx['综合人工工日']))
+                    Qdmx.set('Bz', str(qdmx['备注']))
+                    Qdxdezj = ET.SubElement(Qdmx, 'Qdxdezj')
+                    if '__children' in qdmx:
+                        for child in qdmx['__children']:
+                            QdxdezjMx = ET.SubElement(Qdxdezj, 'QdxdezjMx')
+                            QdxdezjMx.set('Debm', str(child['清单编码']))
+                            QdxdezjMx.set('Mc', str(child['名称']))
+                            QdxdezjMx.set('Dw', str(child['单位']))
+                            QdxdezjMx.set('DwQdSl', str(child['数量']))
+                            QdxdezjMx.set('Dj', str(child['综合单价']))
+                            QdxdezjMx.set('Hj', str(child['合价']))
+                            QdxdezjMx.set('Rgf', str(child['人工费']))
+                            QdxdezjMx.set('Zcf', str(child['主材费']))
+                            QdxdezjMx.set('Sbf', str(child['设备费']))
+                            QdxdezjMx.set('Fcf', str(child['辅材费']))
+                            QdxdezjMx.set('Clf', str(child['材料费']))
+                            QdxdezjMx.set('Jxf', str(child['机械费']))
+                            QdxdezjMx.set('Glf', str(child['管理费']))
+                            QdxdezjMx.set('Lr', str(child['利润']))
+                            Qdxdercjhl = ET.SubElement(QdxdezjMx, 'Qdxdercjhl')
+                            for i in range(len(child['dercj'])):
+                                if i == 0:
+                                    pass
+                                else:
+                                    QdxdercjhlMx = ET.SubElement(Qdxdercjhl, 'QdxdercjhlMx')
+                                    QdxdercjhlMx.set('RcjId', util.indexrcj(rcjhz, child['dercj'][i]))
+                                    QdxdercjhlMx.set('Rcjhl', str(child['dercj'][i][10]))
+                                    QdxdercjhlMx.set('Rcjhj', str(child['dercj'][i][11]))
+                    Qdxrcjhl = ET.SubElement(Qdmx, 'Qdxrcjhl')
+                    for i in range(len(qdmx['rcj'])):
+                        if i == 0:
+                            pass
+                        else:
+                            QdxrcjhlMx = ET.SubElement(Qdxrcjhl, 'QdxrcjhlMx')
+                            QdxrcjhlMx.set('RcjId', util.indexrcj(rcjhz, qdmx['rcj'][i]))
+                                           ##str(djcs['rcj'][i][0]))
+                            QdxrcjhlMx.set('Rcjhl', str(qdmx['rcj'][i][10]))
+                            QdxrcjhlMx.set('Rcjhj', str(qdmx['rcj'][i][11]))
+                            QdxrcjhlMx.set('Zgjbz', str(qdmx['rcj'][i][12]))
+                            QdxrcjhlMx.set('Zyclbz', str(qdmx['rcj'][i][13]))
+                            QdxrcjhlMx.set('Zcbz', str(qdmx['rcj'][i][14]))
+                            QdxrcjhlMx.set('Sbbz', str(qdmx['rcj'][i][15]))
+            else:
+                qdbt_addition = Dwgc['qdbt_addition']
+                for bt in qdbt_addition:
+                    QdBt = ET.SubElement(Qdxm, 'QdBt')
+                    QdBt.set('Xh', bt['Xh'])
+                    QdBt.set('Mc', bt['Mc'])
+                    QdBt.set('Je', bt['Je'])
+                    QdBt.set('Zgj', bt['Zgj'])
+                    QdBt.set('Bz', bt['Bz'])
+                    collection = db["qdxm"]
+                    before_sort = []
+                    async for post in collection.find({"biao_id": id, "Dwgcbh": entry['Dwgcbh'], 'bt': bt['Mc']}):
+                        before_sort.append(post)
+                    after_sort = sorted(before_sort, key=lambda x: int(x['序号']))
+                    for qdmx in after_sort:
+                        Qdmx = ET.SubElement(QdBt, 'Qdmx')
+                        Qdmx.set('Xh', str(qdmx['序号']))
+                        Qdmx.set('Qdbm', str(qdmx['清单编码']))
+                        Qdmx.set('Mc', str(qdmx['名称']))
+                        Qdmx.set('Xmtz', str(qdmx['项目特征']))
+                        Qdmx.set('Jsgz', str(qdmx['计算规则']))
+                        Qdmx.set('Dw', str(qdmx['单位']))
+                        Qdmx.set('Sl', str(qdmx['数量']))
+                        Qdmx.set('Zhdj', str(qdmx['综合单价']))
+                        Qdmx.set('Zhhj', str(qdmx['合价']))
+                        Qdmx.set('Rgf', str(qdmx['人工费']))
+                        Qdmx.set('Zcf', str(qdmx['主材费']))
+                        Qdmx.set('Sbf', str(qdmx['设备费']))
+                        Qdmx.set('Fcf', str(qdmx['辅材费']))
+                        Qdmx.set('Clf', str(qdmx['材料费']))
+                        Qdmx.set('Jxf', str(qdmx['机械费']))
+                        Qdmx.set('Glf', str(qdmx['管理费']))
+                        Qdmx.set('Lr', str(qdmx['利润']))
+                        Qdmx.set('Zgj', str(qdmx['暂估价']))
+                        Qdmx.set('Zgr', str(qdmx['综合人工工日']))
+                        Qdmx.set('Bz', str(qdmx['备注']))
+                        Qdxdezj = ET.SubElement(Qdmx, 'Qdxdezj')
+                        if '__children' in qdmx:
+                            for child in qdmx['__children']:
+                                QdxdezjMx = ET.SubElement(Qdxdezj, 'QdxdezjMx')
+                                QdxdezjMx.set('Debm', str(child['清单编码']))
+                                QdxdezjMx.set('Mc', str(child['名称']))
+                                QdxdezjMx.set('Dw', str(child['单位']))
+                                QdxdezjMx.set('DwQdSl', str(child['数量']))
+                                QdxdezjMx.set('Dj', str(child['综合单价']))
+                                QdxdezjMx.set('Hj', str(child['合价']))
+                                QdxdezjMx.set('Rgf', str(child['人工费']))
+                                QdxdezjMx.set('Zcf', str(child['主材费']))
+                                QdxdezjMx.set('Sbf', str(child['设备费']))
+                                QdxdezjMx.set('Fcf', str(child['辅材费']))
+                                QdxdezjMx.set('Clf', str(child['材料费']))
+                                QdxdezjMx.set('Jxf', str(child['机械费']))
+                                QdxdezjMx.set('Glf', str(child['管理费']))
+                                QdxdezjMx.set('Lr', str(child['利润']))
+                                Qdxdercjhl = ET.SubElement(QdxdezjMx, 'Qdxdercjhl')
+                                for i in range(len(child['dercj'])):
+                                    if i == 0:
+                                        pass
+                                    else:
+                                        QdxdercjhlMx = ET.SubElement(Qdxdercjhl, 'QdxdercjhlMx')
+                                        QdxdercjhlMx.set('RcjId', util.indexrcj(rcjhz, child['dercj'][i]))
+                                        QdxdercjhlMx.set('Rcjhl', str(child['dercj'][i][10]))
+                                        QdxdercjhlMx.set('Rcjhj', str(child['dercj'][i][11]))
+                        Qdxrcjhl = ET.SubElement(Qdmx, 'Qdxrcjhl')
+                        for i in range(len(qdmx['rcj'])):
+                            if i == 0:
+                                pass
+                            else:
+                                QdxrcjhlMx = ET.SubElement(Qdxrcjhl, 'QdxrcjhlMx')
+                                QdxrcjhlMx.set('RcjId', util.indexrcj(rcjhz, qdmx['rcj'][i]))
+                                           ##str(djcs['rcj'][i][0]))
+                                QdxrcjhlMx.set('Rcjhl', str(qdmx['rcj'][i][10]))
+                                QdxrcjhlMx.set('Rcjhj', str(qdmx['rcj'][i][11]))
+                                QdxrcjhlMx.set('Zgjbz', str(qdmx['rcj'][i][12]))
+                                QdxrcjhlMx.set('Zyclbz', str(qdmx['rcj'][i][13]))
+                                QdxrcjhlMx.set('Zcbz', str(qdmx['rcj'][i][14]))
+                                QdxrcjhlMx.set('Sbbz', str(qdmx['rcj'][i][15]))
+
+
+
+
+
+
 
             ########################################Csxm##########################
             Csxm = ET.SubElement(Dwgcxx, 'Csxm')
@@ -1578,9 +1732,26 @@ async def tbxx(client, biao_id):
 
 async def zongjiaDwgc(client, biao_id, bh):
     db = client["baojia"]
+    djcs_sum = 0
+    collection = db["Djcs"]
+    async for post in collection.find({'biao_id': biao_id, "Dwgcbh": bh}, {'__children': 0}):
+        djcs_sum = djcs_sum + float(post['合价'])
+    
+    collection = db['Dwgc']
+    document = await collection.find_one({'biao_id': biao_id, "Dwgcbh": bh}, {'rcjhz': 0})
+    qdbt = document['qdbt']
+    qdbt_addition = document['qdbt_addition']
+    bts = {}
+    distinguish = False
+    if 'Default' in qdbt:
+        pass
+    else:
+        distinguish = True
+        for item in qdbt_addition:
+            bts[item['Mc']] = 0
+    
     collection = db["qdxm"]
     qd_sum = 0
-    djcs_sum = 0
     rgf_sum = 0
     clf_sum = 0
     jxf_sum = 0
@@ -1594,12 +1765,17 @@ async def zongjiaDwgc(client, biao_id, bh):
         jxf_sum = jxf_sum + float(post['数量']) * float(post['机械费'])
         glf_sum = glf_sum + float(post['数量']) * float(post['管理费'])
         lr_sum = lr_sum + float(post['数量']) * float(post['利润'])
-    collection = db["Djcs"]
-    async for post in collection.find({'biao_id': biao_id, "Dwgcbh": bh}, {'__children': 0}):
-        djcs_sum = djcs_sum + float(post['合价'])
+        if distinguish:
+            bts[post['bt']] = bts[post['bt']] + float(post['合价'])
+
+
+
+
     sum = qd_sum + djcs_sum
-    collection = db['Dwgc']
-    document = await collection.find_one({'biao_id': biao_id, "Dwgcbh": bh}, {'rcjhz': 0})
+
+
+
+
     fbrgycl = document['fbrgycl']
     for entry in fbrgycl:
         fbrgycl_sum = fbrgycl_sum + float(entry['合价'])
@@ -1717,9 +1893,14 @@ async def zongjiaDwgc(client, biao_id, bh):
 
 
     
+    collection = db['Dwgc']
+    if distinguish:
+        for entry in qdbt_addition:
+            entry['Je'] = str(bts[entry['Mc']])
+
 
     
-    await collection.update_one({'biao_id': biao_id, "Dwgcbh": bh}, {'$set': {'zjcs': result, 'gfsj' : gfsj, 'bjhz': bjhz}})
+    await collection.update_one({'biao_id': biao_id, "Dwgcbh": bh}, {'$set': {'zjcs': result, 'gfsj' : gfsj, 'bjhz': bjhz, 'qdbt_addition' : qdbt_addition}})
     
 
 

+ 37 - 0
subdir/service.py

@@ -471,6 +471,43 @@ def getQdmx(root, bh, bt):
 
 
     return result
+
+
+
+
+def getQdxm_addition(root, id):
+    result = []
+    #result.append(["ming cheng", "jin e", "zan gu jia", "anquan wenming fei", "guifei"])
+    item = None
+    for child in root:
+    
+        if child.tag == "Dxgcxx":
+            for child1 in child:
+                Dwgcbh = child1.attrib["Dwgcbh"]
+                if Dwgcbh == id:
+                    item = child1
+                    break
+            ##result.append([child.attrib["Dxgcmc"], child.attrib["Je"], child.attrib["Zgj"], child.attrib["Aqwmf"], child.attrib["Gf"]])
+    Fywj = None
+    for child in item:
+        if child.tag == 'Qdxm':
+            Fywj= child
+            break
+    
+    for child in Fywj:
+        ##child is mx
+        if child.tag == "QdBt":
+            count = 0
+            for grandchild in child:
+                count = count + 1
+            if count > 0:
+                result.append({'Bz': child.attrib["Bz"], 'Je': child.attrib['Je'], 'Mc': child.attrib['Mc'], 'Xh': child.attrib['Xh'], 'Zgj': child.attrib['Zgj']})
+    
+    return result
+
+
+
+
 def getQdxm(root, id):
     result = []
     #result.append(["ming cheng", "jin e", "zan gu jia", "anquan wenming fei", "guifei"])