#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec  3 10:51:23 2021

@author: rico
"""
import os
import sys
sys.path.append(os.path.dirname(os.getcwd()))
from ESCore.ES import ES_Client
import datetime
import pandas as pd
import pymssql
import pymysql
from public import zgc_api,Index
from SimilarCharactor.string_similarity import similarity_cn,similarity_en


def get_zgc_param_values(zgc_categoryname,id_list,param_value_dict_list):
    #获取zgc参数值
    try:
        conn_zi_new = pymssql.connect(host='123.56.115.207', user='zgcprice3311',password='zgcprice20200628',database= 'ZI_NEW',autocommit=True)
        cursor_zi_new = conn_zi_new.cursor()
        
        
        cursor_zi_new.execute(f"select * from ShuJuZiDian_Cfg where categoryname = '{zgc_categoryname}' and stdvalue is not Null")
        data = cursor_zi_new.fetchall()
        cat_data_dict = pd.DataFrame(data, columns=[tuple[0] for tuple in cursor_zi_new.description])

        cat_data_dict['stdvalue'] = cat_data_dict['stdvalue'].apply(lambda x:str(x).strip()).tolist()
        cat_data_dict['primitive'] = cat_data_dict['primitive'].apply(lambda x:str(x).strip().upper()).tolist()
        cat_data_dict['simplevalue'] = cat_data_dict['simplevalue'].apply(lambda x:str(x).strip().upper()).tolist()
        
        final_res_list = list()
        for id_,param_value_dict in zip(id_list,param_value_dict_list):
            
            res = list()
            for subtitle,value_ori in zip(param_value_dict.keys(), param_value_dict.values()):
                
                res_value_list = list()
                res_flag_list = list()
    
                value = str(value_ori).strip().upper()
                
                value_df = cat_data_dict[cat_data_dict['subtitle'] == subtitle][['stdvalue','primitive','simplevalue']]
                flag = False
                
                for value_std in value_df['stdvalue'].tolist():
                    if value_std and value_std != 'None':
                        value_std = str(value_std).upper()
                        if value == value_std:
                            flag = True
                            res_value = value_std
                            break
                if flag == False:
                    for value_std,value_pri in zip(value_df['stdvalue'].tolist(),value_df['primitive'].tolist()):
                        if value_std and value_std != 'None':
                            value_std = str(value_std).upper()
                            value_pri = str(value_pri).upper()
                            if value == value_pri:
                                flag = True
                                res_value = value_std
                                break
                    if flag == False:
                        for value_std,value_sim in zip(value_df['stdvalue'].tolist(),value_df['simplevalue'].tolist()):
                            if value_std and value_std != 'None':
                                value_std = str(value_std).upper()
                                value_sim = str(value_sim).upper()
                                if value == value_sim:
                                    flag = True
                                    res_value = value_std
                                    break
                        if flag == False:
                            #res = '请添加数据字典'
                            res_value = value_ori
                
                if type(res_value) != str:
                    res_value = ""
                
                res_value_list.append(res_value)
                res_flag_list.append(flag)
                res.append(dict(zip(res_value_list, res_flag_list)))

            res_dict = dict(zip(param_value_dict.keys(), res))
            final_res_list.append(res_dict)
        
        return_data = dict(zip(id_list,final_res_list))
            
        conn_zi_new.close()
        
        code = 1
        msg = "success"
        res = {'code':code,'msg':msg,'paramsValue_dict':return_data}
        return res

    
    except Exception as e:
        conn_zi_new.close()
        code = 0
        msg = str(e)
        res = {'code':code,'msg':msg,'paramsValue_dict':{}}
        return res

def check_data(category_name,path,assign_params_list):

    df = pd.read_excel(path,converters = {'产品编码':str})
    try:
        df['驳回原因'] = df['驳回原因'].apply(lambda x:str(x))
    except:
        pass

    
    if '无参数，需补充' in df.values:
        print("仍有参数未补充，请补充完整后再继续处理。")
        return False
        
    #创建新产品库链接
    conn_zi_new = pymssql.connect(host='123.56.115.207', user='zgcprice3311',password='zgcprice20200628',database= 'ZI_NEW',autocommit=True)
    cursor_zi_new = conn_zi_new.cursor()
    
    #加载数据字典
    cursor_zi_new.execute(f"select subtitle,stdvalue,primitive,simplevalue from ShuJuZiDian_Cfg where categoryname = '{category_name}' and stdvalue is not Null")
    data_dict = pd.DataFrame(cursor_zi_new.fetchall(), columns=[tuple[0] for tuple in cursor_zi_new.description])
    #进行非标转标
    
    ##加载库中参数项数据（参数id，参数从属）
    if assign_params_list:
        pass
    else:
        
        cursor_zi_new.execute(f"select * from vw_property where name = '{category_name}'")
        db_params = pd.DataFrame(cursor_zi_new.fetchall(), columns=[tuple[0] for tuple in cursor_zi_new.description])
        db_params['standard_param'] = db_params['identy'].apply(lambda x: x[2])
        db_params = db_params[db_params['standard_param'] != '0']
        db_params['subtitle'] = db_params['subtitle'].apply(lambda x: x.strip())
        
        assign_params_list = db_params['subtitle'].tolist()
    
    #subtitle_id_list = dict(zip(db_params['subtitle'].tolist(),db_params['subtitleid'].tolist()))
    #subtitle_type_list = dict(zip(db_params['subtitle'].tolist(),db_params['skuorspu'].tolist()))
    col_list = df.columns.tolist()
    recover_params_list = [params.replace("*","") for params in df.columns.tolist()]
    rename_dict = dict(zip(col_list,recover_params_list))
    
    df = df.rename(columns = rename_dict)

    ##数据准备
    id_list = list()
    params_list = list()
    for index,row in df.iterrows():
        try:
            remark = row['驳回原因']
            if remark != 'nan':
                continue
        except:
            pass
        id_ = row['DOC_ID']
        id_list.append(id_)
    
        temp_key_list = list()
        temp_value_list = list()
        for db_param in assign_params_list:
            if db_param in ['产品型号', 'CPU属性']:
                continue
            temp_key_list.append(db_param)
            temp_value_list.append(row[db_param])
        params_list.append(dict(zip(temp_key_list, temp_value_list)))
    print(f"{category_name}数据准备完成")
    
    
    ##获取标准参数值

    res = get_zgc_param_values(category_name,id_list,params_list)
    res_value_dict = res['paramsValue_dict']
    
    #非标准值添加数据字典
    flag = False
    dict_id_list = list()
    
    process_index = 0
    index_ = Index()
    
    for index,row in df.iterrows():
        
        try:
            print(index_(process_index,len(df)-1), end='%')
        except:
            print(index_(process_index,1), end='%')
        process_index+=1
        
        id_ = row['DOC_ID']
        try:
            remark = row['驳回原因']
            if remark != 'nan':
                continue
        except:
            pass
        eg_brand_name = row['FINISH_P_BRAND_NAME']
        eg_product_name = row['SOURCE_P_NAME'].replace("'","")
        for db_param in assign_params_list:
            if db_param in ['产品型号', 'CPU属性']:#CPU属性为衍生属性，需要特殊处理
                continue
            
            pri_value = str(row[db_param])
            value, value_flag = tuple(res_value_dict[id_][db_param].items())[0]
            if value_flag == False:
                #print(f"select * from ShuJuZiDian_Cfg where categoryname = '{category_name}' and subtitle = '{db_param}' and primitive = '{pri_value}' and stdvalue is not Null")
                cursor_zi_new.execute(f"select * from ShuJuZiDian_Cfg where categoryname = '{category_name}' and subtitle = '{db_param}' and primitive = '{pri_value}' and stdvalue is Null")
                check_df = pd.DataFrame(cursor_zi_new.fetchall(),
                                        columns=[tuple[0] for tuple in cursor_zi_new.description])
    
                if check_df.empty:
                    
                    #推荐数据字典值
                    init_fraction = 0
                    recommend_pri = ''
                    for dict_pri in data_dict[data_dict['subtitle'] == db_param]['primitive'].unique().tolist():
                        dict_pri = str(dict_pri)
                        try:
                            fraction_part1 = similarity_cn(pri_value,dict_pri)
                        except:
                            #print(f"无法比较  '{pri_value}'与 '{dict_pri}'")
                            #fraction = 0
                            fraction_part1 = 0
                            
                        fraction_part2 = similarity_en(pri_value,dict_pri)
                        fraction = fraction_part1 * 0.5 + fraction_part2 * 0.5
                        
                        if fraction > init_fraction:
                            init_fraction = fraction
                            recommend_pri = dict_pri

                    try:
                        recommend_stdvalue = data_dict[data_dict['primitive'] == recommend_pri]['stdvalue'].tolist()[0]
                        recommend_simplevalue = data_dict[data_dict['primitive'] == recommend_pri]['simplevalue'].tolist()[0]
                    except:
                        recommend_stdvalue = '无'
                        recommend_simplevalue = '无'
                        
                    flag = True

                    cursor_zi_new.execute(f"insert into ShuJuZiDian_Cfg (categoryname,subtitle,primitive,eg_brand_name,eg_product_name,recommend_primitive,recommend_stdvalue,recommend_simplevalue) \
                                          values ('{category_name}','{db_param}','{pri_value}','{eg_brand_name}','{eg_product_name}','{recommend_pri}','{recommend_stdvalue}','{recommend_simplevalue}')")
                    
                    cursor_zi_new.execute(f"select id from ShuJuZiDian_Cfg where categoryname = '{category_name}' and subtitle = '{db_param}' and primitive = '{pri_value}'")
                    dict_id = cursor_zi_new.fetchone()[0]
                    dict_id_list.append(dict_id)
                else:
                    stdvalue = check_df['stdvalue'].tolist()[0]
                    res_value_dict[id_].update({f"{db_param}":{f"{stdvalue}":True}})
                    
    if flag:
        
        if len(dict_id_list) == 1:
            _id = dict_id_list[0]
            cursor_zi_new.execute(f"select * from ShuJuZiDian_Cfg where stdvalue is Null and id = {_id}")
        else:
            cursor_zi_new.execute("select * from ShuJuZiDian_Cfg where stdvalue is Null and id in (%s)" % ','.join(['%s'] * len(dict_id_list)),tuple(dict_id_list))
        fill_shujuzidian_df = pd.DataFrame(cursor_zi_new.fetchall(), columns=[tuple[0] for tuple in cursor_zi_new.description])
        fill_shujuzidian_df.to_excel(f"{category_name}数据字典补充.xlsx")
        print(f"请补充数据字典，并导回。数据字典文件路径：{category_name}数据字典补充.xlsx")
    else:
        
        for db_param in assign_params_list:
            if db_param == '产品型号':
                continue
            param_var = '_' + ''.join(db_param.split()).replace('（','').replace('）','').replace('/','').replace('(','').replace(')','').replace('+','').replace('-','').replace('*','').replace('.','')
            exec('%s_list=[]'%param_var)
        
        for index,row in df.iterrows():
            try:
                remark = row['驳回原因']
            except:
                remark = 'nan'
            
            id_ = int(row['DOC_ID'])
            for db_param in assign_params_list:
                param_var = '_' + ''.join(db_param.split()).replace('（','').replace('）','').replace('/','').replace('(','').replace(')','').replace('+','').replace('-','').replace('*','').replace('.','')
                if db_param == '产品型号':
                    continue
                elif db_param == 'CPU属性':
                    if remark == 'nan':
                        
                        value,value_flag = tuple(res_value_dict[id_]['CPU型号'].items())[0]
                        if '飞腾' in value or '龙芯' in value or '兆芯' in value:
                            pass
                        else:
                            value = value.split('-')[0]
                    else:
                        value = "驳回数据"
                else:
                    if remark == 'nan':
                        value,value_flag = tuple(res_value_dict[id_][db_param].items())[0]
                    else:
                        value = "驳回数据"
                    
                exec("%s_list.append('%s')"%(param_var,value.strip()))
                
        for db_param in assign_params_list:
            if db_param == '产品型号':
                continue
            param_var = '_' + ''.join(db_param.split()).replace('（','').replace('）','').replace('/','').replace('(','').replace(')','').replace('+','').replace('-','').replace('*','').replace('.','')
    
            exec("df['%s']=%s_list"%(db_param,param_var))
        
        pass_path = path.split('.xlsx')[0] + '(校验通过).xlsx'
        df.to_excel(pass_path,index=False)
        print(f"{category_name}数据通过校验，请使用'{pass_path}'更新库内参数")
        
#校验补充后的数据（若有驳回原因，请添加列名为：“驳回原因”列，填写驳回原因）
#如果需要指定校验的参数项，请在assign_params_list中填写参数项。eg:['CPU型号','内存容量']
#如果不需要指定，则使用系统配置的参数项进行校验assign_params_list 为空即可。eg:[]
category_name = '笔记本'
complicated_path = "/Users/rico/Downloads/徽采台式机参数补充2021-12-01-已补充.xlsx"
assign_params_list = []
check_data(category_name,complicated_path,assign_params_list)



