import xml.etree.ElementTree as ET
from typing import Union
from fastapi import FastAPI
import os
import uuid
import re
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import pandas as pd
from numpy import float64



def indexrcj(rcjhz, rcjarray):
    bm = rcjarray[1]
    name = rcjarray[2]
    danwei = rcjarray[4]
    target = bm + name + danwei
    for i in range(len(rcjhz)):
        entry = rcjhz[i]
        attempt = entry[1] + entry[2] + entry[4]
        if attempt == target:
            return str(i)
    print("###########################warning######################")
    print(rcjarray)
    print("###########################rcjhz not include ###################")
    ##raise Exception("not found rcjid")
    return ""

def huizongrcj(data):
    
    huizong = {}
    result = [
    [ "ID", "编码", "名称", "规格型号", "单位", "数量", "单价", "合价", "产地", 
    "供应厂商", "人材机类别", "主要材料标志", "主材标志", "设备标志", "甲供标志", "暂估价标志"]]
    for entry in data:
        bm = entry[1]
        name = entry[2]
        danwei = entry[4]
        key = bm + name + danwei
        if key in huizong:
            huizong[key].append(entry)
        else:
            huizong[key] = [entry]
    for key,value in huizong.items():
        result_hl = 0
        for i in range(0, len(value)):
            result_hl = result_hl + float(value[i][10])
        result_dj = float(value[0][5])
        result_hj = result_hl * result_dj
        result.append([value[0][0], value[0][1], value[0][2], value[0][3], value[0][4], result_hl,
                       value[0][5], result_hj, value[0][6], value[0][7], value[0][8], value[0][13],
                       value[0][14], value[0][15], value[0][9], value[0][12]])
   
    return result



def mergerg(rg1, rg2, coef):
    
    for entry in rg2:
        CLBH = entry['CLBH']
        for toMerge in rg1:
            if toMerge['CLBH'] == CLBH:
                toMerge['gr'] = float(toMerge['gr']) + coef * float(entry['gr'])
                toMerge['gf'] = float(toMerge['gf']) + coef * float(entry['gf'])


def mergejx(rg1, rg2, coef):
    
    for entry in rg2:
        jxbh = entry['jxbh']
        for toMerge in rg1:
            if toMerge['jxbh'] == jxbh:
                toMerge['sl'] = float(toMerge['sl']) + coef * float(entry['sl'])
                toMerge['hj'] = float(toMerge['hj']) + coef * float(entry['hj'])


def mergecl(rg1, rg2, coef):
    
    for entry in rg2:
        CLBH = entry['CLBH']
        for toMerge in rg1:
            if toMerge['CLBH'] == CLBH:
                toMerge['SL'] = float(toMerge['SL']) + coef * float(entry['SL'])
                toMerge['HJ'] = float(toMerge['HJ']) + coef * float(entry['HJ'])



def cleanBM(raw):
    if raw.find("]") == -1:
        return raw
    if raw.endswith(']'):
        return raw
    pos = raw.find("*")
    tail = 0
    for i in range(pos + 1, len(raw) + 1):
        if i == len(raw):
            tail = i
            break
        
        if raw[i] > '9' or raw[i] < '0':
            tail = i
            break
    return raw[0:tail]
    

    

    


