#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: dell
@file: 产品品牌更新.py
@time: 2021/04/08
@desc:
"""

import pymssql
import pandas as pd
#import uuid
import time
import os


def duplicate_product_update(filepath,category,sheet_name=0):
    timestr = time.strftime('%Y-%m-%d %H %M %S', time.localtime())
    folderpath = "./数据库更新日志"
    folder = os.path.exists(folderpath)
    if not folder:
        os.makedirs(folderpath)
    logname = f"{folderpath}/品牌更新{timestr}.txt"
    with open (f"{logname}", 'w', encoding='utf-8') as f:

        df=pd.read_excel(filepath,sheet_name=sheet_name,converters = {'正确品牌':str})
        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()
        conn_zi_test=pymssql.connect(host='123.56.115.207', user='zgcprice3311', password='zgcprice20200628',
                                          database='ZI_NEW_TEST', autocommit=True)
        cursor_zi_test=conn_zi_test.cursor()
        conn_zdindex=pymssql.connect(host='123.56.115.207', user='zgcprice3311', password='zgcprice20200628',
                                          database='zdindex', autocommit=True)
        cursor_zdindex=conn_zdindex.cursor()

        # cursor_zi_new.execute(f"select * from vw_property where name = '{category}' and identy != '0000'")
        # db_params = pd.DataFrame(cursor_zi_new.fetchall(), columns=[tuple[0] for tuple in cursor_zi_new.description])
        # db_params['subtitle'] = db_params['subtitle'].apply(lambda x: x.strip())

        # 找到正确的类别id
        cursor_zi_new.execute(f"select id from p_category where name='{category}'")
        category_id = cursor_zi_new.fetchall()
        if len(category_id)>1:
            print(f"品牌名:'{category}' 在p_category中不是唯一的，请检查",file=f)
            return
        if not category_id:
            print('输入的category不在p_category中',file=f)
            return
        category_id = category_id[0][0]

        # 找到该类别下的品牌名,放入existed_brand_list
        cursor_zi_new.execute(f"select distinct brandid from p_spu where categoryid={category_id}")
        brand_id_fetch=cursor_zi_new.fetchall()
        brand_id_list=[]
        for brand_id_tuple in brand_id_fetch:
            brand_id_list.append(brand_id_tuple[0])
        # existed_brand_list = []
        # for brand_id in brand_id_list:
        #     cursor_zi_new.execute(f"select name from p_brand where id={brand_id}")
        #     existed_brand_name = cursor_zi_new.fetchone()
        #     if existed_brand_name:
        #         existed_brand_list.append(existed_brand_name[0])
        brand_id_tuple = tuple(brand_id_list)
        cursor_zi_new.execute(f"select name from p_brand where id in {brand_id_tuple}")
        existed_brand_list = pd.DataFrame((cursor_zi_new.fetchall()),columns = ['name'])['name'].tolist()

        for index,row in df.iterrows():
            if type(row['正确品牌'])!=float:
                correct_brand = row['正确品牌']
                print(f"index: {index},正确品牌为：{row['正确品牌']}",file=f)
                brand_tobe_modified=[]
                if row['品牌A'] != row['正确品牌']:
                    brand_tobe_modified.append(row['品牌A'])
                if row['品牌B'] != row['正确品牌']:
                    brand_tobe_modified.append(row['品牌B'])
                print(f"brand to be modified:{brand_tobe_modified}",file=f)



                # 找到正确的品牌id
                if correct_brand not in existed_brand_list:
                    cursor_zi_new.execute(f"insert into p_brand (name) values ('{correct_brand}')")  # 除了名称，啥都没加
                    print(f"'{correct_brand}' 被添加到了p_brand",file=f)
                cursor_zi_new.execute(f"select id from p_brand where name='{correct_brand}'") #and id not in (select distinct pid from p_brand)
                correct_brand_id = cursor_zi_new.fetchall()
                if len(correct_brand_id)>1:
                    print(f"品牌名:'{correct_brand}' 在p_brand中不唯一，请检查",file=f)
                    return
                correct_brand_id=correct_brand_id[0][0]

                # 一个品牌一个品牌地改
                for wrong_brand in brand_tobe_modified:
                    print(f"正在修改'{wrong_brand}':",file=f)
                    cursor_zi_new.execute(f"select id from p_brand where name='{wrong_brand}'")
                    wrong_brand_id=cursor_zi_new.fetchone()
                    if not wrong_brand_id:
                        print(f"更改前的品牌：'{wrong_brand}' 不在p_brand中，请检查",file=f)
                        return
                    wrong_brand_id=wrong_brand_id[0]

                    cursor_zi_new.execute(f"select distinct spuname, id from p_spu where brandid={wrong_brand_id}")# 我们把所有类别的该品牌id的spuname都挑出来了，spuname一起改
                    wrong_list=cursor_zi_new.fetchall()#找出所有的错误品牌的spuname,挨个改
                    if not wrong_list:
                        print(f"!!!!! 品牌id:'{wrong_brand_id}' 不在p_spu里，该品牌下spu或以被修改",file=f)
                        continue

                    # wrong_list里是所有要改的spuname, 它们的brandid还没改
                    print(f"    需要更改的spuid:'{wrong_list}'",file=f)
                    for wrong_tuple in wrong_list:
                        print(f"    正在更改的spuid:'{wrong_tuple[1]}'",file=f)
                        wrong_spuname=wrong_tuple[0]
                        correct_spuname=wrong_spuname.replace(wrong_brand, correct_brand)  # 生成新的spuname
                        cursor_zi_new.execute(f"select id from p_spu where spuname='{correct_spuname}'")
                        spu_id_tuple=cursor_zi_new.fetchall()
                        if len(spu_id_tuple)>1:
                            print(f"spuname:'{correct_spuname}'在p_spu表中不是唯一的，请检查",file=f)
                            return
                        if spu_id_tuple:  # 如果存在该spuname,提取它的id
                            spu_id=spu_id_tuple[0][0]
                            change_name=0
                        else:  # 如果不存在，直接更改该spuname，提取id
                            # spu = str(uuid.uuid1()).replace('-', '')
                            # cursor_zi_test.execute(f"select categoryid from p_spu where spuname='{wrong_spuname}'")
                            # spu_category_id = cursor_zi_test.fetchone()[0]  # 这个是一定有的因为wrong_spuname就是从p_spu中提取的
                            # cursor_zi_test.execute(f"insert into p_spu values ('{correct_spuname}','{spu}',{spu_category_id},{wrong_brand_id})") #注意，我们新创建的spu的brandid是错误的
                            spu_id = wrong_tuple[1]  # 注意，此时我们还没更改spuname只是提取了它的spuid
                            change_name=1
                        # 到这一步，我们已经取出了正确的spuid
                        wrong_spuid=wrong_tuple[1]
                        cursor_zi_new.execute(f"select skuname, sku from p_sku where spuid={wrong_spuid}")
                        wrong_sku_list=cursor_zi_new.fetchall()  # 提取所有spuid是错误spuid的skuname, sku

                        # 接下来就是sku的命名过程
                        print(f"    接下来是skuname更改：",file=f)
                        for wrong_sku_tuple in wrong_sku_list:
                            wrong_skuname=wrong_sku_tuple[0]
                            wrong_skucode=wrong_sku_tuple[1]
                            correct_skuname=wrong_skuname.replace(wrong_brand, correct_brand)
                            cursor_zi_new.execute(f"select skuname, sku from p_sku where skuname='{correct_skuname}'")
                            skuname=cursor_zi_new.fetchone()  #我们只取了一个，因为我们觉得一个skuname应该只存在一个
                            if skuname:
                                #如果有返回结果，就代表有重复的sku
                                sku_name=skuname[0] # 正确的产品名称
                                sku_code=skuname[1] # 正确的产品编码
                                sku_name_repeated= sku_name + f"（sku：{wrong_skucode}；重复sku：{sku_code}）"
                                cursor_zi_new.execute(
                                    f"update p_sku set state=6, skuname='{sku_name_repeated}' where sku='{wrong_skucode}'")
                                print(f"        if successful:p_sku, '{wrong_skucode}'的 skuname updated to '{sku_name_repeated}', state updated to 6",file=f)
                                cursor_zi_new.execute(
                                    f"update Productcode_Sku set sku='{sku_code}' where sku='{wrong_skucode}' ")
                                print(f"        if successful:Productcode_Sku, sku updated from '{wrong_skucode}' into '{sku_code}'",file=f)
                                cursor_zdindex.execute("select top 1 max(periods) from zd_electricity_price")
                                zd_e_p = cursor_zdindex.fetchone()[0]
                                cursor_zdindex.execute(
                                    f"update zd_electricity_price set goods_id='{sku_code}' where goods_id = '{wrong_skucode}' and periods = {zd_e_p} ")
                                print(f"        if successful:zd_electricity_price, goods_id updated from '{wrong_skucode}' into '{sku_code}'",file=f)
                                cursor_zdindex.execute("select top 1 max(periods) from zd_purchase_price")
                                zd_p_p = cursor_zdindex.fetchone()[0]
                                cursor_zdindex.execute(
                                    f"update zd_purchase_price set goods_id='{sku_code}' where goods_id = '{wrong_skucode}' and periods = {zd_p_p} ")
                                print(f"        if successful:zd_purchase_price, goods_id updated from '{wrong_skucode}' into '{sku_code}'",file=f)
                                cursor_zdindex.execute("select top 1 max(periods) from zd_entry_goods_price")
                                zd_e_g_p = cursor_zdindex.fetchone()[0]
                                cursor_zdindex.execute(
                                    f"update zd_entry_goods_price set goods_id='{sku_code}' where goods_id = '{wrong_skucode}' and periods = {zd_e_g_p} ")
                                print(f"        if successful:zd_entry_goods_price, goods_id updated from '{wrong_skucode}' into '{sku_code}'",file=f)
                                cursor_zdindex.execute("select top 1 max(periods) from zd_week_price")
                                zd_w_p = cursor_zdindex.fetchone()[0]
                                cursor_zdindex.execute(
                                    f"update zd_week_price set goods_id='{sku_code}' where goods_id = '{wrong_skucode}' and periods = {zd_w_p} ")
                                print(f"        if successful:zd_week_price, goods_id updated from '{wrong_skucode}' into '{sku_code}'",file=f)
                            else:
                                #如果新的skuname没有重复，直接修改该sku的skuname(spuid已经被修改了）
                                cursor_zi_new.execute(f"update p_sku set skuname='{correct_skuname}' where skuname='{wrong_skuname}' and spuid={wrong_spuid}")
                                print(f"        p_sku, skuname updated from '{wrong_skuname}' into '{correct_skuname}' where spuid='{wrong_spuid}'",file=f)

                        # 改完skuname后，更改spuid
                        cursor_zi_new.execute(
                            f"update p_sku set spuid={spu_id} where spuid={wrong_spuid}")  # 我们只更改了p_sku表里的spuid,其他表有没有不知道
                        print(f"    if successful:更改了p_sku中的spuid,从'{wrong_spuid}'改到了'{spu_id}'",file=f)

                        if change_name==1:
                            # 如果，需要改名字，我们把spu表的spuname改了
                            cursor_zi_new.execute(
                                f"update p_spu set spuname='{correct_spuname}' where spuname='{wrong_spuname}'")
                            print(
                                f"    if successful: 更改了 p_spu 中的 spuname to '{correct_spuname}' where spuname='{wrong_spuname}'",file=f)

                    # 改完所有的spuid后我们改brandid
                    cursor_zi_new.execute(
                        f"update p_spu set brandid={correct_brand_id} where brandid={wrong_brand_id}")  # 我们把所有类别的该品牌id都改了
                    print(f"if successful:更改了p_spu中的brandid, 从'{wrong_brand_id}'改到了'{correct_brand_id}'",file=f)

                print('',file=f)



if __name__ == '__main__':
    filepath="E:\\ZDZC\\扫描仪产品品牌分析.xlsx"
    category='扫描仪'
    duplicate_product_update(filepath,category)
