import xml.etree.ElementTree as ET
from typing import Union
import os
import uuid
import re
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_order(data):
    
    huizong = {}
    result = [
    ["ID", "人材机编码", "名称", "规格型号", "单位", "单价", "产地", 
    "供应厂商", "人材机类别", "甲供标志", "含量", "合价", "暂估价标志", "主要材料标志", "主材标志", "设备标志" ]]
    left = []
    for entry in data:
        bm = entry[1]
        name = entry[2]
        danwei = entry[4]
        if '000FE' in bm and danwei == '%': ##azfy
            left.append(entry)
            continue
        if '00EXP001' in bm and danwei == '%': ##回程费
            left.append(entry)
            continue
        if '99EXP007' in bm and danwei == '%': ##回程费
            left.append(entry)
            continue
        if '99EXP022' in bm and danwei == '%': ##回程费
            left.append(entry)
            continue
        if '31130104' in bm and danwei == '%': ##回程费
            left.append(entry)
            continue
        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], 
                       value[0][5],value[0][6], value[0][7], value[0][8], value[0][9], result_hl,
                           result_hj, value[0][12],value[0][13],
                       value[0][14], value[0][15],  ])
    for entry in left:
        result.append([entry[0], entry[1], entry[2], entry[3], entry[4], entry[5],
                       entry[6], entry[7], entry[8], entry[9], entry[10], entry[11],
                       entry[12], entry[13], entry[14], entry[15]])
    return result

def huizongrcj(data):
    
    huizong = {}
    result = [
    [ "ID", "编码", "名称", "规格型号", "单位", "数量", "单价", "合价", "产地", 
    "供应厂商", "人材机类别", "主要材料标志", "主材标志", "设备标志", "甲供标志", "暂估价标志"]]
    left = []
    for entry in data:
        bm = entry[1]
        name = entry[2]
        danwei = entry[4]
        if '000FE' in bm and danwei == '%': ##azfy
            left.append(entry)
            continue
        if '00EXP001' in bm and danwei == '%': ##azfy
            left.append(entry)
            continue
        if '99EXP007' in bm and danwei == '%': ##azfy
            left.append(entry)
            continue
        if '99EXP022' in bm and danwei == '%': ##azfy
            left.append(entry)
            continue
        if '31130104' in bm and danwei == '%': ##azfy
            left.append(entry)
            continue
        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]])
    for entry in left:
        result.append([entry[0], entry[1], entry[2], entry[3], entry[4], entry[10],
                       entry[5], entry[11], entry[6], entry[7], entry[8], entry[13],
                       entry[14], entry[15], entry[9], entry[12]])
    return result



def mergerg(rg1, rg2, coef):
    isolated=[]
    for entry in rg2:
        CLBH = entry['CLBH']
        hit = False
        for toMerge in rg1:
            print(toMerge)
            if toMerge['CLBH'] == CLBH:
                hit = True
                toMerge['gr'] = float(toMerge['gr']) + coef * float(entry['gr'])
                toMerge['gf'] = float(toMerge['gf']) + coef * float(entry['gf'])
        if not hit:
            isolated.append({'CLBH':entry['CLBH'],'CLMC':entry['CLMC'],'JLDW':entry['JLDW'],'YSJG':float(entry['YSJG']),'gr':float(entry['gr']),'gf':float(entry['gf'])})
    rg1 = rg1 + isolated
    return rg1
def multirg(rg1, coef):
    
    
    for toMerge in rg1:
        toMerge['gr'] = float(toMerge['gr']) * coef 
        toMerge['gf'] = float(toMerge['gf']) * coef 


def mergejx(rg1, rg2, coef):
    isolated=[]
    for entry in rg2:
        jxbh = entry['jxbh']
        hit=False
        for toMerge in rg1:
            print(toMerge)
            if toMerge['jxbh'] == jxbh:
                hit=True
                toMerge['sl'] = float(toMerge['sl']) + coef * float(entry['sl'])
                toMerge['hj'] = float(toMerge['hj']) + coef * float(entry['hj'])
        if not hit:
            isolated.append({'jxbh':entry['jxbh'],'jxmc':entry['jxmc'],'DW':entry['DW'],'tbdj':float(entry['tbdj']),'sl':float(entry['sl']),'hj':float(entry['hj'])})
    rg1 = rg1 + isolated
    return rg1
def multijx(rg1, coef):
    
    
        
    for toMerge in rg1:
        toMerge['sl'] = float(toMerge['sl']) * coef 
        toMerge['hj'] = float(toMerge['hj']) * coef 


def mergecl(rg1, rg2, coef):
    isolated=[]
    for entry in rg2:
        CLBH = entry['CLBH']
        hit=False
        for toMerge in rg1:
            print(toMerge)
            if toMerge['CLBH'] == CLBH:
                hit=True
                toMerge['SL'] = float(toMerge['SL']) + coef * float(entry['SL'])
                toMerge['HJ'] = float(toMerge['HJ']) + coef * float(entry['HJ'])
        if not hit:
            isolated.append({'CLBH':entry['CLBH'],'CLMC':entry['CLMC'],'JLDW':entry['JLDW'],'YSJG':float(entry['YSJG']),'SL':float(entry['SL']),'HJ':float(entry['HJ'])})
    rg1 = rg1 + isolated
    return rg1
def multicl(rg1, coef):
    
    
    for toMerge in rg1:
        toMerge['SL'] = float(toMerge['SL']) * coef 
        toMerge['HJ'] = float(toMerge['HJ']) * coef 



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') and raw[i] != '-' and raw[i] != '.':
            tail = i
            break
    return raw[0:tail]
    

    

    


