import service
import json
import util
def read_singledexilie2(zhuanye: int, debh: str):
    if debh.startswith("D") :
        return json.dumps({
            "reverse": "None", 
            "rgde": None,
            "jxde": None,
            "clde": None,
            "actual_zhuanye": zhuanye,
            "bz_selected": {"BZBH": {}},
            "bz_selected2": {"BZBH": {}}
        }, ensure_ascii=False)
    if "-F" in debh and zhuanye == 30:
        return service.read_singledexilie_azfy(debh)
    if "-F" in debh and zhuanye == 60:
        return service.read_singledexilie_xsazfy(debh)
    if debh == '19-19':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-20':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-21':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-22':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-23':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-24':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-25':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-26':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-27':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-28':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-29':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-30':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-31':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-32':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-33':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-34':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-35':
        return service.read_singledexilie_tjazfy(debh)
    if debh == '19-36':
        return service.read_singledexilie_tjazfy(debh)
    r_debh = debh.split("!")[0]
    if "附注" in r_debh:
        position = r_debh.find("附注")
        r_debh = r_debh[:position]
    ##print(r_debh)
    result1, result2, rgde, jxde, clde, bz_selected, bz_selected2, actual_zhuanye= service.getSingleDeXilie(zhuanye, r_debh)
    ##print("get result ***************************************")
    if result1:
        result3 = json.loads(result1)
    else:
        result3 = {}
    result3["reverse"] = str(result2)
    result3["rgde"] = rgde
    result3["jxde"] = jxde
    result3["clde"] = clde
    result3["actual_zhuanye"] = actual_zhuanye
    if bz_selected != None:
        result3["bz_selected"] = json.loads(bz_selected)
    else:
        result3["bz_selected"] = {"BZBH": {}}
    if bz_selected2 != None:
        result3["bz_selected2"] = json.loads(bz_selected2)
    else:
        result3["bz_selected2"] = {"BZBH": {}}
    ####14-227+[14-228]*-2
    ####3-409*1.2
    ##print(r_debh)
    r_debh = r_debh.replace("(", "")
    r_debh = r_debh.replace("（", "")
    r_debh = r_debh.replace(")", "")
    r_debh = r_debh.replace("）", "")
    if "[" in r_debh:
        position3 = r_debh.find("*")
        coef = 1
        if position3 == -1:
            pass
        else:
            coef = r_debh[1 + position3:]
            ##print(coef)
            tail = 0
            for i in range(0, len(coef) + 1):
                if i == len(coef):
                    tail = i
                    break
                if (coef[i] > '9' or coef[i] < '0') and coef[i] != '-' and coef[i] != '.':
                    tail = i
                    break
            if tail == 0:
                ##print("1113111")
                result3["reverse"] = 'None'
                return json.dumps(service.chaifen(result3, actual_zhuanye), ensure_ascii=False)
            coef = float(coef[0:tail])

        position1 = r_debh.find("[")
        position2 = r_debh.find("]")
        if position2 == -1:
            ##print("1121111")
            result3["reverse"] = 'None'
            return json.dumps(service.chaifen(result3, actual_zhuanye), ensure_ascii=False)
        if r_debh[position1-1] == "+":
            pass
        elif r_debh[position1-1] == "-":
            coef = -coef
        else:
            ##print("111111")
            result3["reverse"] = 'None'
            return json.dumps(service.chaifen(result3, actual_zhuanye), ensure_ascii=False)
        debh = r_debh[position1+1: position2]
        if result3['GLDE']: ##csv文件里的字段，表示这个定额有没有配套的增减定额
            hit = False
            for key, value in result3['GLDE'].items():
                if debh in value:
                    hit = True
            if not hit:
                result3["reverse"] = 'None'
                return json.dumps(service.chaifen(result3, actual_zhuanye), ensure_ascii=False)
        else:
            result3["reverse"] = 'None'
            return json.dumps(service.chaifen(result3, actual_zhuanye), ensure_ascii=False)
        print("解析算式，系数为" + str(coef))
        result1_, result2_, rgde_, jxde_, clde_, bz_selected_, bz_selected2_, actual_zhuanye_ = service.getSingleDeXilie(zhuanye, debh)
        if result1_:
            rgde = util.mergerg(rgde, rgde_, coef)
            jxde = util.mergejx(jxde, jxde_, coef)
            clde = util.mergecl(clde, clde_, coef)
            result3["rgde"] = rgde
            result3["jxde"] = jxde
            result3["clde"] = clde
            item_ = None
            for item in result3['DW'].keys():
                item_ = item
            result3['DEBH'] = {item_: util.cleanBM(r_debh)}
        else:
            result3["reverse"] = 'None'
    elif "*" in r_debh:##1-234*1.2 
        position3 = r_debh.find("*")
        coef = 1
        
        coef = r_debh[1 + position3:]
        ##print(coef)
        tail = 0
        for i in range(0, len(coef) + 1):
            if i == len(coef):
                tail = i
                break
            if (coef[i] > '9' or coef[i] < '0') and coef[i] != '-' and coef[i] != '.':
                tail = i
                break
        if tail == 0:
            ##print("1113111")
            result3["reverse"] = 'None'
            return json.dumps(service.chaifen(result3, actual_zhuanye), ensure_ascii=False)
        coef = float(coef[0:tail])
        util.multirg(result3["rgde"], coef)
        util.multijx(result3["jxde"], coef)
        util.multicl(result3["clde"], coef)
        item_ = None
        for item in result3['DW'].keys():
            item_ = item
        result3['DEBH'] = {item_: (r_debh)}

    ##print(result3)
    return json.dumps(service.chaifen(result3, actual_zhuanye), ensure_ascii=False)

