#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 24 15:07:58 2021

@author: rico
"""
import pandas as pd
import pymssql
from public import zgc_api,Index
from SimilarCharactor.string_similarity import similarity_cn,similarity_en


def check_data(category_name,path):

    df = pd.read_excel(path,converters = {'产品编码':str})
    #df['备注'] = df['备注'].apply(lambda x:str(x))
    try:
        check_col = df['Unnamed: 0']
        df = df.rename(columns = {'Unnamed: 0':'id'})
    except:
        df['id'] = df.index.tolist()
    
    '''
    if '无参数，需补充' in df.values:
        print("仍有参数未补充，请补充完整后再继续处理。")
        return False
    '''
        
    #创建新产品库链接
    try:
        conn_zi_new = pymssql.connect(host='172.17.9.129', user='zgcprice3311',password='zgcprice20200628',database= 'ZI_NEW',autocommit=True)
        cursor_zi_new = conn_zi_new.cursor()

    except:
        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，参数从属）
    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['skuorspu'] == 'spu')]
    db_params['subtitle'] = db_params['subtitle'].apply(lambda x: x.strip())
    

    #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()))
    params_list = df.columns.tolist()
    recover_params_list = [params.replace("*","") for params in df.columns.tolist()]
    rename_dict = dict(zip(params_list,recover_params_list))
    
    df = df.rename(columns = rename_dict)

    
    ##数据准备
    id_list = list()
    params_list = list()
    for index,row in df.iterrows():
        id_ = row['产品编码']
        id_list.append(id_)
    
        temp_key_list = list()
        temp_value_list = list()
        for db_param in db_params['subtitle'].tolist():
            if db_param in ['产品型号', 'CPU属性','产品系列（SPU）']:
                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_ = str(row['产品编码'])
        #remark = row['备注']
        #if remark != 'nan':
        #    continue
        try:
            eg_brand_name = row['FINISH_P_BRAND_NAME']
            eg_product_name = row['SOURCE_P_NAME']
        except:
            eg_brand_name = row['产品品牌']
            eg_product_name = row['产品名称']
            
        for db_param in db_params['subtitle'].tolist():
            if db_param in ['产品型号', 'CPU属性','产品系列（SPU）']:#CPU属性为衍生属性，需要特殊处理
                continue
            pri_value = str(row[db_param])
            value, value_flag = tuple(res_value_dict[id_][db_param].items())[0]
            
            if value_flag == False:
                
                cursor_zi_new.execute(f"select * from ShuJuZiDian_Cfg where categoryname = '{category_name}' and subtitle = '{db_param}' and primitive = '{pri_value}'")
                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)
                    
    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")
    else:
        
        for db_param in db_params['subtitle'].tolist():
            if db_param == '产品型号':
                continue
            param_var = '_' + ''.join(db_param.split()).replace('（','').replace('）','').replace('/','').replace('(','').replace(')','').replace('+','').replace('-','').replace('*','').replace('.','')
            exec('%s_list=[]'%param_var)
        harddisk = []
        for index,row in df.iterrows():
            id_ = row['产品编码']
            for db_param in db_params['subtitle'].tolist():
                param_var = '_' + ''.join(db_param.split()).replace('（','').replace('）','').replace('/','').replace('(','').replace(')','').replace('+','').replace('-','').replace('*','').replace('.','')
        
                if db_param == '产品型号':
                    continue
                elif db_param == '产品系列（SPU）':
                    value,value_flag = tuple(res_value_dict[id_]['产品系列'].items())[0]
                    if '彩色' in value:
                        value = '彩色复印纸'
                    else:
                        value = value
                elif db_param == 'CPU属性':
                    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]
                        
                elif db_param == '机械硬盘容量':
                    value,value_flag = tuple(res_value_dict[id_][db_param].items())[0]
                    
                    jx_harddisk,value_flag_1 = tuple(res_value_dict[id_]['机械硬盘容量'].items())[0]
                    gt_harddisk,value_flag_2 = tuple(res_value_dict[id_]['固态硬盘容量'].items())[0]
                    
                    if jx_harddisk == '0' and gt_harddisk != '0':
                        harddisk_value = gt_harddisk
                    elif jx_harddisk != '0' and gt_harddisk == '0':
                        harddisk_value = jx_harddisk
                    elif jx_harddisk != '0' and gt_harddisk != '0':
                        harddisk_value = jx_harddisk+"+"+gt_harddisk
                    harddisk.append(harddisk_value)
                    
                    
                else:
                    value,value_flag = tuple(res_value_dict[id_][db_param].items())[0]
                    
                exec("%s_list.append('%s')"%(param_var,value))
                
        for db_param in db_params['subtitle'].tolist():
            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))
        if harddisk:
            df['硬盘容量'] = harddisk
        
        path = path.split('.xlsx')[0] + '(校验通过).xlsx'
        df.to_excel(path,index=False)
        print(f"{category_name}数据通过校验，可更新库内参数")




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}'")
        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)))
            '''
            std_value_list = cat_data_dict[(cat_data_dict['subtitle'] == subtitle) & (cat_data_dict['stdvalue'] == value)]['stdvalue'].tolist()
            
            for std_value in std_value_list:
        
                if std_value and std_value != 'None':
        
                    res_value_list.append(std_value)
                    res_flag = True
                    res_flag_list.append(res_flag)
                    break
            if res_flag == False:
                
                std_value_pri_list = cat_data_dict[(cat_data_dict['subtitle'] == subtitle) & (cat_data_dict['primitive'] == value)]['stdvalue'].tolist()
                for std_pri_value in std_value_pri_list:
                    if std_pri_value and std_pri_value != 'None':
                        res_value_list.append(std_pri_value)
                        res_flag = True
                        res_flag_list.append(res_flag)
                        break
                
                if res_flag == False:
                    
                    res_value_list.append(value)
                    res_flag_list.append(res_flag)
            ''' 
            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






category_name = '台式机'
path = '/Users/rico/WorkSpace/1_Project/Company/中电中采/4_中电商品库功能类/更新库内参数并重新命名/台式机参数确认.xlsx'
   
check_data(category_name,path)














