#!/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})
    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
        
    #创建新产品库链接
    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}'")
    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['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']
        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属性']:
                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}数据准备完成")
    
    
    ##获取标准参数值
    data = {
        "params_info": {
            "category": category_name,
            "id_list": id_list,
            "params_dict_list": params_list
        }
    }
    res = zgc_api("GetZGCParamValuesInfo", data)
    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['id'])
        eg_brand_name = row['产品品牌']
        eg_product_name = row['产品名称']
        for db_param in db_params['subtitle'].tolist():
            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:
                
                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)
        
        for index,row in df.iterrows():
            id_ = str(row['id'])
            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 == '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]
                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))
        
        path = path.split('.xlsx')[0] + '(校验通过).xlsx'
        df.to_excel(path,index=False)
        print(f"{category_name}数据通过校验，可更新库内参数")


category_name = '一体电脑'
path = '/Users/rico/Work Space/1_Project/Company/中电中采/数据处理项目_重点类信息提取/一体电脑/一体电脑参数确认3.xlsx'

check_data(category_name,path)














