#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 22 21:15:06 2021

@author: rico
"""
#import oss2
from db import MYSQL,MSSQL
from delCalPrice_UpdatePriceDiff import create_price_difference_single
from stockInfo import *
import datetime

batch = datetime.datetime.now().strftime("%Y-%m-%d")

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

mssql_new = MSSQL('123.56.115.207','ZI_NEW')
cursor_zi_new = mssql_new._cur
    
#价格渠道字典
cursor_zi_new.execute(f"select * from zdindex_channel_rel")
price_source = pd.DataFrame(cursor_zi_new.fetchall(), columns=[tuple[0] for tuple in cursor_zi_new.description])

price_source_c2code = dict(zip(price_source['channel_alias_cn'].tolist(),price_source['channel_alias_code'].tolist()))
price_source_c2e = dict(zip(price_source['channel_alias_cn'].tolist(),price_source['channel_alias_en'].tolist()))
price_source_e2code = dict(zip(price_source['channel_alias_en'].tolist(),price_source['channel_alias_code'].tolist()))
price_source_e2c = dict(zip(price_source['channel_alias_en'].tolist(),price_source['channel_alias_cn'].tolist()))

cursor.execute("select * from DW_PRODUCT_ALL_RES where FINISH_P_CHECK_STATUS = 'PREPARING' and FINISH_P_NAME <>'无' and FINISH_P_NAME <>'null'")
data = cursor.fetchall()
df = pd.DataFrame(data, columns=[tuple[0] for tuple in cursor.description])

##判断有几个第三方链接

##如果没有则使用source_p_price价格建库

##如果有，则调取爬虫，爬取图片，爬取价格


##组织数据，建库
doc_id_list =  df['DOC_ID'].tolist()

brand_list = df['FINISH_P_BRAND_NAME'].tolist()

brandId_list = df['FINISH_P_BRAND_CODE'].apply(lambda x:str(x)).tolist()

category_list = df['FINISH_P_LASTCATEGORY_NAME'].tolist()

categoryId_list = df['FINISH_P_LASTCATEGORY_CODE'].apply(lambda x:str(x)).tolist()

name_list = df['FINISH_P_NAME'].tolist()

#params_list = df['FINISH_P_PARAMS'].apply(lambda x:eval(x.replace("null","{}").replace("无","{}"))).tolist()
params_list = [eval(params) if "{" in params else eval("{}") for params in df['FINISH_P_PARAMS'].tolist()]



res = stock_in_product(brand_list,brandId_list,category_list,categoryId_list,params_list,name_list)
print(res)
sku_list = res['sku_list']
had_sku_list = res['had_sku_list']

print("产品入库完成")   
#更新product_all表
#sku_list = ['20210301180403541', '20210301180403602', '20210301180404727', '20210301180404789', '20210301180404789', '20210301180404883', '20210301180404727', '20210301192552947', '20210301192552990', '20210301192553031', '20210301192553077', '20210301192553130', '20210301192553173', '20210301192553215', '20210301192553254', '20210301192553294', '20210301192553333', '20210301192553373', '20210301192553414', '20210301192553454', '20210301192553551', '20210301192553596', '20210301192553638', '20210301192553680', '20210301192553722', '20210301192553769', '20210301192553813', '20210301192553858', '20210301192553899', '20210301192553942', '20210301192553985', '20210301192554025', '20210301192554078', '20210301192554135', '20210301192554179', '20210301192554219', '20210301192554261', '20210301192554302', '20210301192554302', '20210301192554370', '20210301192554410', '20210301192554456', '20210301192554510', '20210301192554553', '20210301192554593', '20210301192554632', '20210301192554671', '20210301192554710', '20210301192554749', '0508001200028', '20210301192554823', '20210301192554874', '20210301192554925', '20210301192554976', '20210301192555022', '20210301192554976', '20210301192555098', '20210301192555140', '20210301192555497', '20210301192555551', '20210301192555598', '20210301192555655', '20210301192555702', '20210301192555754', '20210301192555800', '20210301192555847', '20210301192555893', '20210301192555936', '20210301192555978', '20210301192556016', '20210301192556055', '20210301192556094', '20210301192556135', '20210301192556176', '20210301192556218', '20210301192556259', '20210301192556299', '20210301192556349', '20210301192556391', '20210301192556433', '20210301192556473', '20200629114621131', '20200629114911242', '20210301192556585', '20200629114512656', '20210301192556659', '20210301192556700', '20210301192556753', '20210301192556800', '20210301192556849', '20210301192556891', '20210301192556933', '20210301192556975', '20210301192557014', '20210301192557053', '20210301192557414', '20210301192557472', '20210301192557518', '20210301192557560', '20210301192557602', '20210301192557642', '20210301192557684', '20210301192557751', '20210301192557795', '20210301192557835', '20210301192557874', '20210301192557913', '20210301192557953', '20210301192558005', '20210301192558049', '20210301192558096', '20210301192558148', '20210301192558192', '20210301192558237', '20210301192558279', '20210301192558326', '20210301192558373', '20210301192558414', '20210301192558457', '20210301192558507', '20210301192558547', '20210301192558587', '20210301192558627', '20210301192558668', '20210301192559004', '20210301192559057', '20210301192559101', '20210301192559142', '20210301192559184', '20210301192559227', '20210301192559268', '20210301192559311', '20210301192559353', '20210301192559394', '20210301192559436', '20210301192559476', '0102005163068', '20210301192559556', '0102005163097', '20210301192559635', '20210301192559677', '20210301192559724', '20210301192559774', '20210301192559834', '20210301192559873', '20210301192559912', '20210301192559950', '20210301192600000', '20210301192600042', '20210301192600080', '20210301192600136', '20210301192600177', '20210301192600217', '20210301192600262', '20210301192600301', '20210301192600342', '20210301192600381', '20210301192600420', '0101000935187', '20201208151125603', '0101000935179', '0101000935017', '0101000934682', '0101000934708', '0101000934679', '0101000934678', '0101000934708', '20210301192600790', '20210301192600830', '20210301192600888', '20210301192600930', '20200629114655213', '20210301192601012', '20210301192601053', '20210301192601093', '20210301192601132', '20210301192601171', '20210301192601213', '20210301192601258', '20210301192601303', '20210301192601343', '20210301192601384', '20210301192601424', '20210301192601470', '20210301192601517', '20210301192601560', '20210301192601600', '20210301192601641', '20210301192601681', '20210301192601721', '20210301192601767', '20210301192601809', '20210301192601850', '20210301192601888', '20210301192601939', '20210301192601978', '20210301192602018', '20210301192602063', '20210301192602105', '20210301192602148', '20210301192602190', '20210301192602234', '20210301192602278', '20210301192602330', '20210301192602386', '20210301192602430', '20210301192602471', '20210301192602515', '20210301192602557', '20210301192602599', '20210301192602654', '20210301192602702', '20210301192602702', '20210301192602781', '20210301192602824', '20210301192602874', '20210301192602917', '20210301192602957', '20210301192602999', '20210301192603041', '20210301192603081', '20210301192603137', '20210301192603180', '20210301192603221', '20210301192603270', '20210301192603313', '20210301192603355', '20210301192603395']
i=1
for _id,sku,had_sku in zip(doc_id_list,sku_list,had_sku_list):
    print(i)
    if sku == 'None':
        cursor.execute(f"update DW_PRODUCT_ALL_RES set FINISH_P_SKU = '{had_sku}',FINISH_P_REMARK = '无',FINISH_P_CHECK_STATUS = 'SYS_COMPLETE_PRICE' where DOC_ID = '{_id}'")
    else:
        cursor.execute(f"update DW_PRODUCT_ALL_RES set FINISH_P_SKU = '{sku}',FINISH_P_REMARK = '无',FINISH_P_CHECK_STATUS = 'ADD2DB' where DOC_ID = '{_id}'")
    i+=1


df['FINISH_P_SKU'] = sku_list
df = df[df['FINISH_P_SKU'] != 'None']
print("产品编码同步完成")

#组织价格数据(新建)
code_list = []
source_name_list = []
price_list = []
url_list = []
channelId_list = []

for index,row in df.iterrows():
    
    sku = row['FINISH_P_SKU']
    try:
        count = int(row['THIRD_P_COUNT'])
    except:
        count = 0
    if count == 0:
        code_list.append(sku)
        source_name_list.append(row['SOURCE_P_NAME'])
        url_list.append(str(row['SOURCE_P_URL']).replace("'","''"))
        channel_id = price_source_e2code[row['SOURCE_CHANNEL_SNAME']]
        channelId_list.append(channel_id)
        price_list.append(row['SOURCE_P_PRICE'])
        
    else:
        
        for i in range(count):
            i = i+1
    
            exec("source_name = row['THIRD_P_URL_NAME_%s']"% i)
            exec("url =  row['THIRD_P_URL_%s']"% i)
            exec("channel_name = row['THIRD_P_URL_SOURCE_SNAME_%s']"% i)
            channel_id = price_source_e2code[channel_name]
            exec("price = row['THIRD_P_URL_CRAWL_PRICE_%s']"% i)
            
            code_list.append(sku)
            source_name_list.append(source_name)
            url_list.append(url)
            channelId_list.append(channel_id)
            price_list.append(price)
    
#价格关系入库
res = stock_in_price(code_list,source_name_list,price_list,url_list,channelId_list)
print(res)
print("产品价格录入完成")


cursor.execute("select * from DW_PRODUCT_ALL_RES where FINISH_P_CHECK_STATUS = 'SYS_COMPLETE_PRICE' and FINISH_P_NAME <>'无' and FINISH_P_NAME <>'null'")
data = cursor.fetchall()
complet_price_df_1 = pd.DataFrame(data, columns=[tuple[0] for tuple in cursor.description])

#组织价格数据(补充)
code_list = []
source_name_list = []
price_list = []
url_list = []
channelId_list = []

for index,row in complet_price_df_1.iterrows():
    
    sku = row['FINISH_P_SKU']
    try:
        count = int(row['THIRD_P_COUNT'])
    except:
        count = 0
    if count == 0:
        code_list.append(sku)
        source_name_list.append(row['SOURCE_P_NAME'])
        url_list.append(str(row['SOURCE_P_URL']).replace("'","''"))
        channel_id = price_source_e2code[row['SOURCE_CHANNEL_SNAME']]
        channelId_list.append(channel_id)
        price_list.append(row['SOURCE_P_PRICE'])
        
    else:
        
        for i in range(count):
            i = i+1
    
            exec("source_name = row['THIRD_P_URL_NAME_%s']"% i)
            exec("url =  row['THIRD_P_URL_%s']"% i)
            exec("channel_name = row['THIRD_P_URL_SOURCE_SNAME_%s']"% i)
            channel_id = price_source_e2code[channel_name]
            exec("price = row['THIRD_P_URL_CRAWL_PRICE_%s']"% i)
            
            code_list.append(sku)
            source_name_list.append(source_name)
            url_list.append(url)
            channelId_list.append(channel_id)
            price_list.append(price)
    
#价格关系入库
res = stock_in_price(code_list,source_name_list,price_list,url_list,channelId_list)
print(res)
print("产品价格录入完成")
i=1
for doc_id in complet_price_df_1['DOC_ID'].tolist():
    print(i)
    cursor.execute(f"update DW_PRODUCT_ALL_RES set FINISH_P_REMARK = '无',FINISH_P_CHECK_STATUS = 'PRICE_COMPLETED' where DOC_ID = '{doc_id}'")
    i+=1


cursor.execute("select * from DW_PRODUCT_ALL_RES where FINISH_P_CHECK_STATUS = 'MATCHED' and FINISH_P_NAME <>'无' and FINISH_P_NAME <>'null'")
data = cursor.fetchall()
complet_price_df_2 = pd.DataFrame(data, columns=[tuple[0] for tuple in cursor.description])

#组织价格数据(补充)
code_list = []
source_name_list = []
price_list = []
url_list = []
channelId_list = []

for index,row in complet_price_df_2.iterrows():
    
    sku = row['FINISH_P_SKU']
    try:
        count = int(row['THIRD_P_COUNT'])
    except:
        count = 0
    if count == 0:
        code_list.append(sku)
        source_name_list.append(row['SOURCE_P_NAME'])
        url_list.append(str(row['SOURCE_P_URL']).replace("'","''"))
        channel_id = price_source_e2code[row['SOURCE_CHANNEL_SNAME']]
        channelId_list.append(channel_id)
        price_list.append(row['SOURCE_P_PRICE'])
        
    else:
        
        for i in range(count):
            i = i+1
    
            exec("source_name = row['THIRD_P_URL_NAME_%s']"% i)
            exec("url =  row['THIRD_P_URL_%s']"% i)
            exec("channel_name = row['THIRD_P_URL_SOURCE_SNAME_%s']"% i)
            channel_id = price_source_e2code[channel_name]
            exec("price = row['THIRD_P_URL_CRAWL_PRICE_%s']"% i)
            
            code_list.append(sku)
            source_name_list.append(source_name)
            url_list.append(url)
            channelId_list.append(channel_id)
            price_list.append(price)
    
#价格关系入库
res = stock_in_price(code_list,source_name_list,price_list,url_list,channelId_list)
print(res)
print("产品价格录入完成")
i=1
for doc_id in complet_price_df_2['DOC_ID'].tolist():
    print(i)
    cursor.execute(f"update DW_PRODUCT_ALL_RES set FINISH_P_REMARK = '无',FINISH_P_CHECK_STATUS = 'MATCHED_PRICE_COMPLETED' where DOC_ID = '{doc_id}'")
    i+=1


#如果有整机类，录入配件差异
category_list = ['笔记本','台式机','一体电脑']
for category in category_list:
    cat_df = df[df['FINISH_P_LASTCATEGORY_NAME'] == category]
    if cat_df.empty:
        continue
    else:
        cat_sku_list = cat_df['FINISH_P_SKU'].tolist()
        create_price_difference_single(category,cat_sku_list)




'''

mssql_index = MSSQL('123.56.115.207','ZI_DataBase')
cursor_zdindex = mssql_index._cur

cursor_zdindex.execute(f"select ProductCode,MaximumPrice from ZI_Price_Quote")
price_df = pd.DataFrame(cursor_zdindex.fetchall(), columns=[tuple[0] for tuple in cursor_zdindex.description])

for _id,sku in zip(doc_id_list,sku_list):
    
    try:
        price = price_df[price_df['ProductCode'] == sku]['MaximumPrice'].tolist()[0]
        price = str(float(price))
        print(price)
    except:
        price = "0.0"

    cursor.execute(f"update DW_PRODUCT_ALL_RES set FINISH_P_PRICE = '{price}' where DOC_ID = {_id}")
'''

#组织SKU对应关系数据(暂未完成)
sku_list = []
cust_sku_list = []
channel_alias_list = []


for index,row in df.iterrows():
    
    sku = row['FINISH_P_SKU']
    cust_sku = row['SOURCE_P_SKU']
    channel_alias = row['SOURCE_CHANNEL_SNAME']
    
    sku_list.append(sku)
    cust_sku_list.append(cust_sku)
    channel_alias_list.append(channel_alias)
    try:
        count = int(row['THIRD_P_COUNT'])
        for i in range(count):
            i = i+1
    
            exec("cust_sku = row['THIRD_P_URL_SKU_%s']"% i)
            exec("channel_alias_cn =  row['THIRD_P_URL_SOURCE_SNAME_%s']"% i)
            channel_alias = price_source_c2e[channel_alias_cn]
    
            sku_list.append(sku)
            cust_sku_list.append(cust_sku)
            channel_alias_list.append(channel_alias)
    except:
        sku_list.append(sku)
        cust_sku_list.append(cust_sku)
        channel_alias_list.append(channel_alias)

res = stock_in_sku_relationship(cust_sku_list,sku_list,channel_alias_list)
print(res)
print("SKU对应关系录入完成")


'''
import requests

pic_list = []

pic_power = 1
all_count = len(df)
split_count = 100
for_count = int(all_count/split_count)+1
start_line = 0
end_line = split_count

for i in range(for_count):
    i = i+1

    if i == for_count:

        df_split = df.iloc[start_line:all_count]
        if df_split.empty:
            break
        time = str(datetime.datetime.now())
        print(f"第{i}批次开始处理，起始行：{start_line},终止行：{all_count},开始时间：{time}")

    else:
        time = str(datetime.datetime.now())
        print(f"第{i}批次开始处理，起始行：{start_line},终止行：{end_line},开始时间：{time}")
        df_split = df.iloc[start_line:end_line]
        start_line = end_line
        end_line = end_line + split_count

    #请求地址
    request_url = "http://59.110.219.171:8092/return_data"
    #组织请求数据
    data_list = str({'data':[[str(url),str(price_source_e2c[source]),pic_power] for url,source in zip(df_split['SOURCE_P_URL'].tolist(),df_split['SOURCE_CHANNEL_SNAME'].tolist())]}).replace("'","\"")
    payload={'dataList': f'{data_list}'}
    response = requests.request("POST", request_url, data=payload)
    res = eval(response.text)
    
    #处理未爬取到的数据a
    for j in range(len(res)):
        if res[j]:
            pass
        else:
            res[j] = {'img_list':[],'class_list':{},'url':''}
            
    pic_list += [str(element['img_list']) for element in res]

    time = str(datetime.datetime.now())
    print(f"第{i}批次处理完成，起始行：{start_line},终止行：{all_count},结束时间：{time}")
df['SOURCE_P_PIC'] = pic_list
'''

'''
##图片上传服务器
if not os.path.exists(f"/mythjob/pic/{batch}"):
    os.mkdir(f"/mythjob/pic/{batch}")



upload_local_path = f"/mythjob/pic/{batch}/"
'''

'''
uploda_target_path = f"G:\\pic\\n\\{batch}\\"
transport = paramiko.Transport(('123.56.115.207',22))
transport.connect(username='root',password='123456')
sftp = paramiko.SFTPClient.from_transport(transport)
'''
'''
#oss = OSS('LTAI5tHR33mAN4vqoUPsW9H4','iBoR3O23lFAtwwq5kxdlE6QGKLCrAS','https://oss.zgcindex.com/','zgcpic')
auth = oss2.Auth('LTAI5tHR33mAN4vqoUPsW9H4','iBoR3O23lFAtwwq5kxdlE6QGKLCrAS')
bucket = oss2.Bucket(auth,'http://oss-cn-beijing.aliyuncs.com','zgcpic')


#bucket.put_object_from_file(f"pic/n/{batch}/test.jpg", '/Users/rico/Library/Mobile Documents/com~apple~CloudDocs/Watch Pic/381609324125_.pic.jpg')



saved_path_list = []

for index,row in df.iterrows():

    sku = str(row['FINISH_P_SKU'])
    pic_list = eval(row['SOURCE_P_PIC'])
    
    count = int(row['THIRD_P_COUNT'])
    for i in range(count):
        i = i+1
        exec("pic_list + eval(row['THIRD_P_URL_PIC_%s'])"% i)
        
    sort_num = 0
	for pic_url in pic_list:
      
      sort_num+=1
      r = requests.get(pic_url)
      with open(f"/mythjob/pic/{batch}/{sku}-{sort_num}.jpg",'wb') as f:
          f.write(r.content)
          f.close()

img_path_list = os.listdir(upload_local_path)
for img_path in img_path_list:
    
    bucket.put_object_from_file(f"pic/n/{batch}/{img_path}", "/mythjob/pic/{batch}/{img_path}")

    saved_path_list.append(f"oss.zgcindex.com/pic/n/{batch}/{img_path}")

print("图片数据上传至服务器")



#记录图片地址
mssql_new = MSSQL('123.56.115.207','ZI_NEW')
cursor_zi_new = mssql_new._cur

cursor_zi_new.execute(f"select id,sku from p_sku")
data = cursor_zi_new.fetchall()
skuid_df = pd.DataFrame(data, columns=[tuple[0] for tuple in cursor_zi_new.description])

for path in saved_path_list:

    sku = saved_path.split("/")[-1].split("-")[0]

    skuid = skuid_df[skuid_df['sku'] == sku]['id'].tolist()[0]

    cursor_zi_new.execute(f"insert into p_skupic (skuid,path) values ({skuid},'{path}')")
mssql_new.Close()
print("图片数据位置信息存储至数据库")
'''
    