#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 22 22:38:22 2021

@author: rico
"""

from db import MYSQL,MSSQL
import pandas as pd

path = "/Users/rico/WorkSpace/1_Project/Company/中电中采/TEMP Workspace/ES处理相关/ES线下补参处理/（已确认202111261157470001）全渠道一体电脑2021-12-03 12_23_05数据.xlsx"
df = pd.read_excel(path,converters = {'FINISH_P_SKU':str})
try:
    export_df = df[['DOC_ID','DATA_BATCH','DATA_END_TIME','FLOW_NODE_STATUS','SOURCE_P_SKU','SOURCE_CHANNEL_NAME','SOURCE_CHANNEL_SNAME','SOURCE_P_NAME',
                    'SOURCE_P_LASTCATEGORY_NAME','SOURCE_P_BRAND_NAME','SOURCE_P_PRICE','SOURCE_P_URL','FINISH_P_REMARK','FINISH_P_BRAND_NAME','FINISH_P_BRAND_CODE','FINISH_P_LASTCATEGORY_NAME',
                    'FINISH_P_LASTCATEGORY_CODE','FINISH_P_MODEL','FINISH_P_SKU','FINISH_P_PARAMS','FINISH_P_NAME','FINISH_P_STATUS',
                    'FINISH_P_CHECK_STATUS']]
except:

    export_df = df[['DOC_ID','DATA_BATCH','DATA_END_TIME','FLOW_NODE_STATUS','SOURCE_P_SKU','SOURCE_CHANNEL_NAME','SOURCE_CHANNEL_SNAME','SOURCE_P_NAME',
                    'SOURCE_P_LASTCATEGORY_NAME','SOURCE_P_BRAND_NAME','SOURCE_P_PRICE','SOURCE_P_URL','FINISH_P_REMARK','FINISH_P_BRAND_NAME','FINISH_P_BRAND_CODE','FINISH_P_LASTCATEGORY_NAME',
                    'FINISH_P_LASTCATEGORY_CODE','FINISH_P_PARAMS','FINISH_P_SKU','FINISH_P_NAME','FINISH_P_STATUS',
                    'FINISH_P_CHECK_STATUS']]


export_df = export_df.fillna('无')
mysql = MYSQL('39.105.1.55',2883,'ZD_PUBLIC_pro')
cursor = mysql._cur


#同步数据至DW层
tablename = "DW_PRODUCT_ALL_RES"
cols = ','.join(export_df.columns)
val = (tuple(i) for i in export_df.values)
sqlstr = "INSERT INTO {} ({}) VALUES ({})".format(tablename,cols,','.join(['%s']*len(export_df.columns)))

try:
    cursor.executemany(sqlstr, val)
    print('>>> 插入数据成功，表 {} 共插入 {} 行数据'.format(tablename,len(export_df)))
except Exception as e:
    print('>>> 插入数据失败', e)
    
    
    

'''

df= pd.read_excel("/Users/rico/WorkSpace/1_Project/Company/中电中采/TEMP Workspace/ES处理相关/ES线下补参处理/安徽三大类结果1119(1).xlsx")

for index,row in df.iterrows():
    
    doc_id = row['DOC_ID']
    
    name = row['SOURCE_P_LASTCATEGORY_NAME']
    brand = row['SOURCE_P_BRAND_NAME']
    
    cursor.execute(f"update DW_PRODUCT_ALL_RES set SOURCE_P_LASTCATEGORY_NAME = '{name}', SOURCE_P_BRAND_NAME = '{brand}' where DOC_ID = '{doc_id}'")
    
    
temp_df= pd.read_excel("/Users/rico/WorkSpace/1_Project/Company/中电中采/TEMP Workspace/ES处理相关/ES线下补参处理/（已确认，蓝色为修改了品牌）笔记本结果202111241726340001.xlsx",converters = {'FINISH_P_SKU':str})

for index,row in temp_df.iterrows():
    
    doc_id = row['DOC_ID']
    
    FINISH_P_CHECK_STATUS = row['FINISH_P_CHECK_STATUS']
    if FINISH_P_CHECK_STATUS == 'MATCHED':
        print(index)
        sku = row['FINISH_P_SKU']
        
        cursor.execute(f"update DW_PRODUCT_ALL_RES set FINISH_P_SKU = '{sku}' where DOC_ID = '{doc_id}'")
        


'''