

# 如果 dalao数目比较多还无法根据earnper为负进行筛选 使用此方法去除 earn_per 为负比例较高的 dalao

from base_class import BaseVariableFunction
from base_class import *
baseclass = BaseVariableFunction(__file__)


baseclass.makedirpath(baseclass.new_dalao_ana_fm_path)

baseclass.open_base_library()
decimal.getcontext().prec = 100
old_print = print


def timestamped_print(*args, **kwargs):
    old_print(datetime.datetime.utcnow().replace(
        microsecond=0), *args, **kwargs)


print = timestamped_print
print('\n'*5)
print(f"{'{:<6}'.format('ENTER')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")


def makescreenurl(urlname, urlparam):
    urlname = urlname + urlparam
    return '=HYPERLINK("{}","{}")'.format(urlname, urlname)


total_dalao_profit_df = pd.read_excel(baseclass.dalao_total_ana_fm_path /
                                      f"total_dalao_profit_df.xlsx", dtype=object)

total_dalao_profit_df["datetime"] = pd.to_datetime(
    total_dalao_profit_df["datetime"])

total_dalao_profit_df["dalaofirsttimestamp"] = pd.to_datetime(
    total_dalao_profit_df["dalaofirsttimestamp"])

total_dalao_profit_df = total_dalao_profit_df.astype({
    'timestamp': int,
    'buy_counts': int,
    'sell_counts': int,
    'swap_eth_amount': float,
    'outtoken_per': float,
    'cost_main': float,
    'earn_main': float,
    'earn_percent': float,
    "fir_cost_main":float,
    'fir_earn_percent':float,
}
)

# df = df[(df["outtoken_per"] <= 1.1)].reset_index(drop=True)
total_dalao_profit_df = total_dalao_profit_df[total_dalao_profit_df["cost_main"] != 0].reset_index(
    drop=True)
total_dalao_profit_df = total_dalao_profit_df[total_dalao_profit_df["fir_cost_main"] != 0].reset_index(
    drop=True)

outtokenper_total_dalao_profit_df = total_dalao_profit_df[["swap_tokenaddress","outtoken_per", "cost_main",	"earn_main", "earn_percent","fir_earn_percent",	"dalaoaddress",
                                               "dalaofirsttimestamp"]].drop_duplicates().reset_index(drop=True)


total_dalao_profit_df = total_dalao_profit_df[["swap_tokenaddress", "cost_main",	"earn_main", "earn_percent","fir_earn_percent",	"dalaoaddress",
                                               "dalaofirsttimestamp"]].drop_duplicates().reset_index(drop=True)


new_repeat_token_df = pd.read_excel(
    baseclass.dalao_total_ana_fm_path/"new_repeat_token_df.xlsx", dtype=object)

# new_repeat_token_df = new_repeat_token_df[new_repeat_token_df["add"].isin(
#     arr_forfilter_token)]
# new_repeat_token_df.to_excel(
#     baseclass.dalao_total_ana_fm_path/"fiterkuisun_repeat_token_df.xlsx", index=False)

totalana_anaing_dalao_df = pd.read_excel(
    baseclass.dalao_total_ana_fm_path/"totalana_anaing_dalao.xlsx", dtype=object)

arr_dalaoaddress_totalana_anaing_dalao = totalana_anaing_dalao_df["add"].tolist(
)
obj_str_dalaoaddress_2_buytokencounts = {}

for str_dalaoaddress in arr_dalaoaddress_totalana_anaing_dalao:
    # [购买次数， eran为负的次数]
    obj_str_dalaoaddress_2_buytokencounts[str_dalaoaddress] = [0,0, 0,0,0,0,0,0]


# 计算dalao的对于这些共同token的盈利比例等情况

for str_dalaoaddress in arr_dalaoaddress_totalana_anaing_dalao:
    cur_dalao_df = outtokenper_total_dalao_profit_df[outtokenper_total_dalao_profit_df["dalaoaddress"]
                                         == str_dalaoaddress].reset_index(drop=True)

    obj_str_dalaoaddress_2_buytokencounts[str_dalaoaddress][0] = len(
        cur_dalao_df)
    obj_str_dalaoaddress_2_buytokencounts[str_dalaoaddress][1] = len(
        cur_dalao_df[cur_dalao_df["fir_earn_percent"] >=1])
    
    obj_str_dalaoaddress_2_buytokencounts[str_dalaoaddress][2] = len(
        cur_dalao_df[(cur_dalao_df["fir_earn_percent"] >=0.5) & ( cur_dalao_df["fir_earn_percent"] <1)  ])
    
    obj_str_dalaoaddress_2_buytokencounts[str_dalaoaddress][3] = len(
        cur_dalao_df[(cur_dalao_df["fir_earn_percent"] >=0) & ( cur_dalao_df["fir_earn_percent"] <0.5) ] 
    )
    obj_str_dalaoaddress_2_buytokencounts[str_dalaoaddress][4] = len(
      cur_dalao_df[ (cur_dalao_df["fir_earn_percent"] >= -0.2 ) & ( cur_dalao_df["fir_earn_percent"] <0) ]           
    )  
       
    obj_str_dalaoaddress_2_buytokencounts[str_dalaoaddress][5] = len(
        cur_dalao_df[(cur_dalao_df["fir_earn_percent"] < -0.2 ) & ( cur_dalao_df["fir_earn_percent"] >-0.5) ])
    obj_str_dalaoaddress_2_buytokencounts[str_dalaoaddress][6] = len(
        cur_dalao_df[cur_dalao_df["fir_earn_percent"] <= -0.5])
    obj_str_dalaoaddress_2_buytokencounts[str_dalaoaddress][7] = len(
        cur_dalao_df[cur_dalao_df["outtoken_per"] >1])
   
    
dalaoaddress_2_buytokencounts_df = pd.DataFrame.from_dict(
    obj_str_dalaoaddress_2_buytokencounts, orient='index')

dalaoaddress_2_buytokencounts_df = dalaoaddress_2_buytokencounts_df.reset_index(
    names=["dalaoadd"], drop=False)
dalaoaddress_2_buytokencounts_df = dalaoaddress_2_buytokencounts_df.rename(columns={
    0: "counts",
    1: "bigbigearncounts",
    2: "bigearncounts",
    3: "norearncounts",
    4: "norkuisuncounts",
    5: "bigkuisuncounts",
    6: "bigbigkuisuncounts",
    7: "gouzhuangcounts",
})
 
dalaoaddress_2_buytokencounts_df.insert(
    loc=1, column="SolscanURL", value="")

dalaoaddress_2_buytokencounts_df["SolscanURL"] = dalaoaddress_2_buytokencounts_df.apply(lambda ser: makescreenurl(
    "https://solscan.io/account/", f'{ser["dalaoadd"]}'), axis=1)
dalaoaddress_2_buytokencounts_df["totalcounts"] = len(
    new_repeat_token_df)

 

dalaoaddress_2_buytokencounts_df["bigbigearn_per"] = dalaoaddress_2_buytokencounts_df["bigbigearncounts"] / \
    dalaoaddress_2_buytokencounts_df["counts"]
dalaoaddress_2_buytokencounts_df["bigearn_per"] = dalaoaddress_2_buytokencounts_df["bigearncounts"] / \
    dalaoaddress_2_buytokencounts_df["counts"]

dalaoaddress_2_buytokencounts_df["norearn_per"] = dalaoaddress_2_buytokencounts_df["norearncounts"] / \
    dalaoaddress_2_buytokencounts_df["counts"]


dalaoaddress_2_buytokencounts_df["norkuisun_per"] = dalaoaddress_2_buytokencounts_df["norkuisuncounts"] / \
    dalaoaddress_2_buytokencounts_df["counts"]
dalaoaddress_2_buytokencounts_df["bigkuisun_per"] = dalaoaddress_2_buytokencounts_df["bigkuisuncounts"] / \
    dalaoaddress_2_buytokencounts_df["counts"]
dalaoaddress_2_buytokencounts_df["bigbigkuisun_per"] = dalaoaddress_2_buytokencounts_df["bigbigkuisuncounts"] / \
    dalaoaddress_2_buytokencounts_df["counts"]

dalaoaddress_2_buytokencounts_df["bigbig_dif"] = dalaoaddress_2_buytokencounts_df["bigbigearncounts"] -   dalaoaddress_2_buytokencounts_df["bigbigkuisuncounts"]
dalaoaddress_2_buytokencounts_df["bigbig_dif_per"] = dalaoaddress_2_buytokencounts_df["bigbig_dif"] /   dalaoaddress_2_buytokencounts_df["counts"]

dalaoaddress_2_buytokencounts_df["bigkuisunsum_counts"] = dalaoaddress_2_buytokencounts_df["bigkuisuncounts"] +  dalaoaddress_2_buytokencounts_df["bigbigkuisuncounts"]
dalaoaddress_2_buytokencounts_df["bigkuisunsum_per"] = dalaoaddress_2_buytokencounts_df["bigkuisunsum_counts"] /   dalaoaddress_2_buytokencounts_df["counts"]


dalaoaddress_2_buytokencounts_df = dalaoaddress_2_buytokencounts_df[[
    "dalaoadd", "SolscanURL",  "totalcounts", "counts",
   
  "bigbigearncounts", "bigearncounts", "norearncounts",
  "norkuisuncounts", "bigkuisuncounts", "bigbigkuisuncounts",

   "bigbigearn_per",  "bigearn_per",    "norearn_per" , 
  "norkuisun_per",  "bigkuisun_per","bigbigkuisun_per",

   
     "bigbig_dif",   "bigbig_dif_per", 
     "bigkuisunsum_counts", "bigkuisunsum_per",
       "gouzhuangcounts"
     ]]

dalaoaddress_2_buytokencounts_df.to_excel(
    baseclass.dalao_total_ana_fm_path/"forfilter_dalaoaddress_2_buytokencounts.xlsx", index=False)

print(f"{'{:<6}'.format('END')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")
