import pymongo
import pandas
import json
from pymongo import MongoClient
client = MongoClient()
db = client["dinge"]
collection = db["de-collection"]
from subdir import service
##print(collection.find_one({"DEBH": "3-94"}))
def handle_cl(cl):
    result = {}
    result["CLBH"] = cl["CLBH"]
    result["CLMC"] = cl["CLMC"]
    result["JLDW"] = cl["JLDW"]
    result["YSJG"] = cl["YSJG"]
    result["SL"] = cl["SL"]
    result["HJ"] = cl["HJ"]
    return result
def handle_rg(rg):
    result = {}
    result["CLBH"] = rg["CLBH"]
    result["CLMC"] = rg["CLMC"]
    result["JLDW"] = rg["JLDW"]
    result["YSJG"] = rg["YSJG"]
    result["gr"] = rg["gr"]
    result["gf"] = rg["gf"]
    return result
def handle_jx(jx):
    result = {}
    result["jxbh"] = jx["jxbh"]
    result["jxmc"] = jx["jxmc"]
    result["DW"] = jx["DW"]
    result["tbdj"] = jx["tbdj"]
    result["sl"] = jx["sl"]
    result["hj"] = jx["hj"]
    return result

for post in collection.find({"zhuanye": "修缮工程安装"}):
   
    
    myquery = { "zhuanye": "修缮工程土建安装",
                "DEBH": post["DEBH"]
                }
    A1, A2, rg, jx, cl, bz_ = service.getSingleDeXilie_xsaz(post["DEBH"])
    if bz_:
        bz = json.loads(bz_)
        mongo_bz = []
        for key in bz["ID"].keys():
            mongo_bz.append({
        "BZBH" : bz["BZBH"][key],
        "SM": bz["SM"][key],
        "THMC" : bz["THMC"][key]

            })
    else:
        mongo_bz = []
    
    mongo_rg = [handle_rg(entry) for entry in rg ]
    
    mongo_jx = [handle_jx(entry) for entry in jx ]
    mongo_cl = [handle_cl(entry) for entry in cl ]
    newvalues = { "$set": { "mongo_bz": mongo_bz,
                            "mongo_rg": mongo_rg,
                            "mongo_cl": mongo_cl,
                            "mongo_jx": mongo_jx
                            } }

    collection.update_one(myquery, newvalues)
    




