#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec  3 11:29:33 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


host = "http://123.56.114.138:9200/"
index_name = "model_params_test"
es = ES_Client(host,index_name)


#按批次号检查结果数据
res = es.SearchByCondition("DATA_BATCH",["202111231900550001"])
#按项目检查结果数据
#res = es.SearchAimAllByCondition("DATA_PROJECT_NAME","徽采商城数据入库")


##按批次下或者项目下的指定类检查结果数据
#res = res[res['FINISH_P_LASTCATEGORY_NAME'] == '台式机']

'''
#状态总揽
FLOW_NODE_STATUS

INIT：初始化状态

STOP_WORD_FAILD：停用词失败
SKU_EXIST：SKU匹配成功（终止态）
URL_EXIST：URL匹配成功（终止态）
FIRST_CHECK_SUCCESS：初步审核成功


SYS_GET_BRAND_SUCCESS：品牌提取成功
SYS_GET_BRAND_FAILD：品牌提取失败

SYS_GET_CATEGORY_SUCCESS_NONPOINT：类别提取成功-非重点类
SYS_GET_CATEGORY_SUCCESS_NONREDUCTIVE：类别提取成功-非还原类
SYS_GET_CATEGORY_SUCCESS_REDUCTIVE：类别提取成功-还原类
SYS_GET_CATEGORY_FAILD：类别提取失败

SYS_GET_MODEL_SUCCESS_NONPOINT：型号提取成功-非重点类
SYS_GET_MODEL_SUCCESSS_NONREDUCTIVE：型号提取成功-非还原类
SYS_GET_MODEL_FAILD_NONPOINT：型号提取失败-非重点类
SYS_GET_MODEL_FAILD_NONREDUCTIVE：型号提取失败-非还原类

SYS_GET_PARAMS_SUCCESS_NONREDUCTIVE：匹配标准参数提取成功-非还原类
SYS_GET_PARAMS_SUCCESS_REDUCTIVE：匹配标准参数提取成功-还原类
SYS_GET_PARAMS_FAILD_NONREDUCTIVE：匹配标准参数提取失败-非还原类
SYS_GET_PARAMS_FAILD_REDUCTIVE：匹配标准参数提取失败-还原类

SYS_FINISH_MATCH：产品名称或匹配项匹配成功（终止态）

SYS_GET_PARAMS_SUCCESS_END：必填项参数提取成功（终止态）
SYS_GET_PARAMS_FAILD_END：必填项参数提取失败
'''

#分析原则：确保下方统计的数据除了驳回的以外其它数据都处于终止态。否则去页面查看处理，或者反馈北京

#数据总揽
print(res[['DOC_ID','FLOW_NODE_STATUS','FINISH_P_LASTCATEGORY_NAME']].groupby(['FLOW_NODE_STATUS','FINISH_P_LASTCATEGORY_NAME']).count())
#各节点驳回的
print(res[['DOC_ID','FLOW_NODE_STATUS','FINISH_P_REMARK']].groupby(['FLOW_NODE_STATUS','FINISH_P_REMARK']).count())



#校验是否有未命名的商品
category_name = ''

if category_name:
    check_flag = True
    if category_name in ['笔记本','台式机','一体电脑','复印纸','工作站']:
        FLOW_NODE_STATUS = "SYS_GET_PARAMS_SUCCESS_REDUCTIVE"
    else:
        FLOW_NODE_STATUS = "SYS_GET_PARAMS_SUCCESS_NONREDUCTIVE"
    
    search_field_list = ['FINISH_P_LASTCATEGORY_NAME','FLOW_NODE_STATUS','FINISH_P_NAME','FLOW_FINISH_STATUS']
    search_field_value_list = [category_name,'SYS_FINISH_MATCH','','Y']
    update_field = "FLOW_FINISH_STATUS"
    update_field_value = "N"
    update_res_1 = es.UpdateByMultipleFourCondition(search_field_list,search_field_value_list,update_field,update_field_value)
    if update_res_1['updated'] != 0:
        check_flag = False
    
    search_field_list = ['FINISH_P_LASTCATEGORY_NAME','FLOW_NODE_STATUS','FINISH_P_NAME','FLOW_FINISH_STATUS']
    search_field_value_list = [category_name,'SYS_FINISH_MATCH','','N']
    update_field = "FLOW_NODE_STATUS"
    update_field_value = FLOW_NODE_STATUS
    update_res_2 = es.UpdateByMultipleFourCondition(search_field_list,search_field_value_list,update_field,update_field_value)
    if update_res_2['updated'] != 0:
        check_flag = False
    
    search_field_list = ['FINISH_P_LASTCATEGORY_NAME','FLOW_NODE_STATUS','FINISH_P_NAME','FLOW_FINISH_STATUS']
    search_field_value_list = [category_name,'SYS_GET_PARAMS_SUCCESS_END','','Y']
    update_field = "FLOW_FINISH_STATUS"
    update_field_value = "N"
    update_res_3 = es.UpdateByMultipleFourCondition(search_field_list,search_field_value_list,update_field,update_field_value)
    if update_res_3['updated'] != 0:
        check_flag = False
        
    search_field_list = ['FINISH_P_LASTCATEGORY_NAME','FLOW_NODE_STATUS','FINISH_P_NAME','FLOW_FINISH_STATUS']
    search_field_value_list = [category_name,'SYS_GET_PARAMS_SUCCESS_END','','N']
    update_field = "FLOW_NODE_STATUS"
    update_field_value = FLOW_NODE_STATUS
    update_res_4 = es.UpdateByMultipleFourCondition(search_field_list,search_field_value_list,update_field,update_field_value)
    if update_res_4['updated'] != 0:
            check_flag = False


if check_flag:
    date = str(datetime.datetime.now()).split(".")[0]
    res.to_excel(f"（未确认）{date}数据.xlsx")

    
