#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec  3 11:17:16 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_category_params_list(category_name):

    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,identy from vw_property where name = '{category_name}' and subtitle != '产品型号'")
    db_params = pd.DataFrame(cursor_zi_new.fetchall(), columns=[tuple[0] for tuple in cursor_zi_new.description])
    db_params['require_param'] = db_params['identy'].apply(lambda x: x[0])
    db_params['standard_param'] = db_params['identy'].apply(lambda x: x[2])
    db_params = db_params[(db_params['standard_param'] != '0') & (db_params['require_param'] != '0')]
    db_params['subtitle'] = db_params['subtitle'].apply(lambda x: x.strip())
    params_list = db_params['subtitle'].tolist()
    
    return params_list
    



def import_params_data(es,category_name,path):

    #更新补参数据
    df = pd.read_excel(path,converters = {'CPU型号':str})
    try:
        df['驳回原因'] = df['驳回原因'].apply(lambda x:str(x))
    except:
        df['驳回原因'] = ['nan' for i in range(len(df))]
    
    params_list = get_category_params_list(category_name)
    
    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
        
        search_field =  "DOC_ID"
        search_field_value = str(row['DOC_ID'])
      
        remark = row['驳回原因']
        if remark != 'nan':

            now_time = str(datetime.datetime.now()).replace(' ','T').split('.')[0]+"+08:00"
            es.UpdateParamsReturnData(search_field,search_field_value,remark,now_time)

            continue
        
        #update_field = "DEAL_INPUT_PARAMS_DICT" #DEAL_STD_PARAMS_DICT   DEAL_INPUT_PARAMS_DICT FLOW_NODE_STATUS
        #update_field_value = "SYS_GET_CATEGORY_SUCCESS_REDUCTIVE"
        update_dict = {}
        for params in params_list:
            update_dict.update({params:str(row[params]).replace('\xa0',' ')})
        params_info = str(update_dict).replace("'",'\"')
    
        es.UpdateParamsDealData(category_name,search_field,search_field_value,params_info)


#导入数据
host = "http://123.56.114.138:9200/"
index_name = "model_params_test"
es = ES_Client(host,index_name)

category_name = '笔记本'
checked_path = "/Users/rico/Downloads/激光打印机参数补充回导.xlsx"
import_params_data(es,category_name,checked_path)
