#%%
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
import pandas as pd
import numpy as np
import os
import re

def Str2value(oristr):#将oristr中的数字提取成float返回
    # try:
        pattern=re.compile("\\d+")
        result=pattern.findall(oristr)
        return np.float64(result[0])
    # except:
    #     print(oristr,result,sep=' ')

def Colum2value(df,col):#将oricolum列中所有string转为包含的数字
    oricolum=df[col]
    droplist=[]
    try:
        pattern=re.compile("\\d+")
        for i in range(0,oricolum.values.size):
            if(oricolum.values[i] is np.nan):
                #空数据丢弃
                droplist.append(i)
                continue

            result=pattern.findall(oricolum.values[i])
            if (result):
                oricolum.values[i]=np.float64(result[0])
            else:#无数字数据丢弃
                droplist.append(i)
        df.drop(index=droplist,inplace=True)
        return oricolum
    except:
        print(i,sep=' ')


def JobHist(datapath,renderName="JobHist"):#文件夹路径datapath，统计文件夹下所有文件，分类存储salary平均值，在当前路径生成rendername.html文件
    #生成Salary-职位类型的直方图对比
    TypePrice={}
    TypeCount={}
    oridata=os.walk(datapath)
    #对文件夹下所有数据进行遍历
    for path,dir_list,file_list in oridata:#遍历路径下所有文件,搜集条目数  
        for file_name in file_list:
            oneDataPath=os.path.join(path, file_name)#单个文件路径
            Position,Type=file_name.split(' ')#从 “北京 C++.csv” 中 分割出 "北京" 和 "C++.csv"
            Type,extName=Type.split('.')#从 "C++.csv" 中 分割出 "C++" "csv"
            oneData=pd.read_csv(oneDataPath)#单个文件dataframe
            Colum2value(oneData,"salary")
            TypePrice[Type]=TypePrice.setdefault(Type,float(0))+oneData["salary"].sum()#求sum
            TypeCount[Type]=TypeCount.setdefault(Type,int(0))+oneData.shape[0]#统计条目数
    
    TypeAvg={}
    for i in TypePrice:#计算平均值
        TypeAvg[i]=TypeAvg.setdefault(Type,float(0))+TypePrice[i]/TypeCount[i]

    c = (
        Bar()
        .add_xaxis(list(TypeAvg.keys()))
        .add_yaxis("Salary",list(TypeAvg.values()))
        .set_global_opts(title_opts=opts.TitleOpts(title="Salary-职位类型"))
    )

    c.render('../html/jobs/' + renderName + ".html")
    


#%%示例程序
# JobHist("C:\\MyWorkSpace\\shu-innovation-project\\data\\jobs\\baiduZhaopin","JobHistTest")
