#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 15 18:05:40 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
from public import zgc_api,Index
import re



#导入数据
def import_data(es,type,path):
    

    if type == 'brand':
        deal_data_col = '人工确认品牌'
    elif type == 'category':
        deal_data_col = '人工确认类别'
    elif type == 'model_nopoint' or type == 'model_nonreductice':
        deal_data_col = '人工确认型号'
    else:
        print("不支持的类型")
        return False
    
    #更新品牌处理后数据
    df = pd.read_excel(path)
    try:
        df['驳回原因'] = df['驳回原因'].apply(lambda x:str(x))
    except:
        df['驳回原因'] = ['nan' for i in range(len(df))]
        print("无驳回原因数据")
    
    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 =  "ID"
        search_field_value = str(row['ID'])

        remark = row['驳回原因']
        if remark != 'nan':
            #处理驳回数据
            now_time = str(datetime.datetime.now()).replace(' ','T').split('.')[0]+"+08:00"
            es.UpdateReturnData(type,search_field_value,remark,now_time)

            continue
        #导入正常数据
        deal_data = row[deal_data_col]
        es.UpdateDealData(type,search_field_value,deal_data)

            
#校验数据
def check_data(es,type,path):
    
    #校验品牌数据
    if type == 'brand':
        
        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()
        
        
        #更新品牌处理后数据
        df = pd.read_excel(path)
        try:
            df['驳回原因'] = df['驳回原因'].apply(lambda x:str(x))
        except:
            df['驳回原因'] = ['nan' for i in range(len(df))]
            print("无驳回原因数据")
        
        process_index = 0
        index_ = Index()
        
        res_flag = True
        
        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
            
            DOC_ID =  str(row['ID'])
            remark = row['驳回原因']
            if remark != 'nan':
                continue
            
            brand = str(row['人工确认品牌'])

            cursor_zi_new.execute(f"select id from p_brand where name = '{brand}'")
            check_result = cursor_zi_new.fetchone()
            
            if not check_result:
                res_flag = False
                print(f"ID:'{DOC_ID}','{brand}'不是库内品牌，校验不通过")
                
        if res_flag:
            print(f"{path},该数据校验通过，可执行导入")
            
        else:
            print(f"{path},该数据校验通不过，请修改")
            
        cursor_zi_new.close()
        conn_zi_new.close()
        return res_flag,path
    
    #校验类别数据
    elif type == 'category':
        
        
        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()
        
        
        #更新品牌处理后数据
        df = pd.read_excel(path)
        try:
            df['驳回原因'] = df['驳回原因'].apply(lambda x:str(x))
        except:
            df['驳回原因'] = ['nan' for i in range(len(df))]
            print("无驳回原因数据")
        
        process_index = 0
        index_ = Index()
        
        res_flag = True
        
        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
            
            DOC_ID =  str(row['ID'])
            remark = row['驳回原因']
            if remark != 'nan':
                continue
            
            category = str(row['人工确认类别'])

            cursor_zi_new.execute(f"select id from p_category where name = '{category}' and id not in (select DISTINCT pid from p_category)")
            check_result = cursor_zi_new.fetchone()
            
            if not check_result:
                res_flag = False
                print(f"ID:'{DOC_ID}','{category}'不是库内类别，或不是末级类别，校验不通过")
                
        if res_flag:
            print(f"{path},该数据校验通过，可执行导入")
        else:
            print(f"{path},该数据校验通不过，请修改")
            
        cursor_zi_new.close()
        conn_zi_new.close()
        return res_flag,path
    
    #校验型号数据
    elif type == 'model_nopoint' or type == 'model_nonreductice':
        
        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()
        
        #更新品牌处理后数据
        df = pd.read_excel(path)
        try:
            df['驳回原因'] = df['驳回原因'].apply(lambda x:str(x))
        except:
            df['驳回原因'] = ['nan' for i in range(len(df))]
            print("无驳回原因数据")
        
        process_index = 0
        index_ = Index()
        
        res_flag = True
        res_list = []
        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
            
            DOC_ID =  str(row['ID'])
            category = str(row['结果产品末级类名称'])
            brand = str(row['结果产品品牌名称'])

            remark = row['驳回原因']
            if remark != 'nan':
                res_list.append('驳回数据')
                continue

            model = str(row['人工确认型号'])
            model_alias = "".join(re.findall(r"[\u4e00-\u9fa5A-Za-z0-9+.]+", model)).upper()

            cursor_zi_new.execute(f"select model from sku_model where model_alias = '{model_alias}' and categoryname = '{category}' and brandname = '{brand}'")
            check_result = cursor_zi_new.fetchone()
            
            if not check_result:
                
                res_flag = False
                print(f"ID:'{DOC_ID}','{model}'不是型号表内型号，校验不通过")
                res_list.append('型号表外型号，请添加')
            else:
                res_list.append(check_result[0])
                
        if res_flag:
            df['人工确认型号'] = res_list
            pass_path = path.split(".")[0] + "(校验通过)." + path.split(".")[1]
            df.to_excel(pass_path)
            
            print(f"{pass_path},该数据校验通过，可执行导入")
            cursor_zi_new.close()
            conn_zi_new.close()
            return res_flag,pass_path
        else:
            print(f"{path},该数据校验通不过，请修改")
            cursor_zi_new.close()
            conn_zi_new.close()
            return res_flag,path
            
        
    else:
        print("不支持的类型")
        return False,path
    
    
'''
品牌、类别、型号补充完成后导入脚本
根据数据情况，修改type和path参数即可
'''


#创建ES客户端
host = "http://123.56.114.138:9200/"
index_name = "model_params_test"
es = ES_Client(host,index_name)

#请选择要校验及更新的数据类型
#1、品牌提取与标化：brand
#2、类别提取与标化：category
#3、非重点类型号提取与标化：model_nopoint
#4、非还原类型号提取与标化：model_nonreductice
#eg. type = 'brand'
type = 'model_nonreductice'

#补充完成的文件路径（若有驳回原因，请添加列名为：“驳回原因”列，填写驳回原因）
path = "/Users/rico/Downloads/扫描仪型号标化_第一批(1).xlsx"
#校验文件内容
check_status,pass_path = check_data(es,type,path)
#导入数据（校验通过后才可导入！！！）
if check_status:
    import_data(es,type,pass_path)