Commit 0bf7d354 authored by rico.liu's avatar rico.liu

add

parent 547a4064
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 19 22:26:55 2020
@author: rico
"""
import pymssql
import pandas as pd
import time
import warnings
warnings.filterwarnings("ignore")
class ProgressLine(object):
def __init__(self, number=50, decimal=2):
"""
:param decimal: 你保留的保留小数位
:param number: # 号的 个数
"""
self.decimal = decimal
self.number = number
self.a = 100/number # 在百分比 为几时增加一个 # 号
def __call__(self, now, total):
# 1. 获取当前的百分比数
percentage = self.percentage_number(now, total)
# 2. 根据 现在百分比计算
well_num = int(percentage / self.a)
# print("well_num: ", well_num, percentage)
# 3. 打印字符进度条
progress_bar_num = self.progress_bar(well_num)
# 4. 完成的进度条
result = "\r%s %s" % (progress_bar_num, percentage)
return result
def percentage_number(self, now, total):
"""
计算百分比
:param now: 现在的数
:param total: 总数
:return: 百分
"""
return round(now / total * 100, self.decimal)
def progress_bar(self, num):
"""
显示进度条位置
:param num: 拼接的 “#” 号的
:return: 返回的结果当前的进度条
"""
# 1. "#" 号个数
well_num = "#" * num
# 2. 空格的个数
space_num = " " * (self.number - num)
return '[%s%s]' % (well_num, space_num)
def delete_spu_price():
create_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
conn_zdindex = pymssql.connect(host='123.56.115.207', user='zgcprice3311',password='zgcprice20200628',database= 'zdindex',autocommit=True)
cursor_zdindex = conn_zdindex.cursor()
cursor_zdindex.execute("select top 1 max(periods) from zd_entry_goods_price")
periods = str(cursor_zdindex.fetchone()[0])
cursor_zdindex.execute(f"delete from zd_electricity_price where periods = '{periods}' and mall_id = 'DS-SPU-HY'")
cursor_zdindex.execute(f"delete from zd_entry_goods_price where periods = '{periods}' and shop_id = 'ST-SPU-HY'")
cursor_zdindex.execute(f"delete from zd_purchase_price where periods = '{periods}' and purchase_id = 'ZC-SPU-HY'")
conn_zdindex.close()
print(f"SPU还原价删除完毕 -- {create_time}")
def get_configure_price():
#获取配件价格
try:
conn = pymssql.connect(host='123.56.115.207', user='zgcprice3311',password='zgcprice20200628',database= 'ZI_NEW',autocommit=True)
cursor = conn.cursor()
#cursor.execute("select * from vw_electricity_source_price where ProductCode in "+ code_list +" and periods = (select top 1 periods from vw_entry_source_price order by periods desc)")
cursor.execute("select category_code,category_name,part,detail,price from configure_price")
configure_price = pd.DataFrame([v for v in cursor.fetchall()],columns=[tuple[0] for tuple in cursor.description])
configure_price['price'] = pd.to_numeric(configure_price['price'])
cursor.close()
conn.close()
return configure_price
except:
print('连接失败,重新连接')
return get_configure_price()
def get_product_code(category_list):
#获取指定类别 SPU对应的SKU(new)
try:
conn = pymssql.connect(host='123.56.115.207', user='zgcprice3311', password='zgcprice20200628', database='ZI_NEW',autocommit=True)
cursor = conn.cursor()
#cursor.execute("select a.spuid,b.spuname,a.sku,a.skuname,c.name as categoryname from p_sku a \
# left join p_spu b \
# on a.spuid = b.id \
# left join p_category c \
# on b.categoryid = c.id \
# where a.state in (1,4) and c.name in (%s)" % ','.join(['%s'] * len(category_list)),tuple(category_list))
cursor.execute("select a.spuid,b.spuname,a.sku,a.skuname,c.name as categoryname,b.categoryid,b.brandid from p_sku a \
left join p_spu b \
on a.spuid = b.id \
left join p_category c \
on b.categoryid = c.id \
where a.state in (1,4) and c.name in (%s)" % ','.join(['%s'] * len(category_list)),tuple(category_list))
spu_sku_df = pd.DataFrame(cursor.fetchall(),columns=[tuple[0] for tuple in cursor.description])
cursor.close()
conn.close()
return spu_sku_df
except:
print('连接失败,重新连接')
return get_product_code()
def get_attr_data(category_list):
#获取指定类别参数数据
try:
conn = pymssql.connect(host='123.56.115.207', user='zgcprice3311', password='zgcprice20200628', database='ZI_NEW',autocommit=True)
cursor = conn.cursor()
cursor.execute("select * from vw_sku_params where categoryname in (%s)" % ','.join(['%s'] * len(category_list)),tuple(category_list))
attr_data = pd.DataFrame(cursor.fetchall(),columns=[tuple[0] for tuple in cursor.description])
cursor.close()
conn.close()
return attr_data
except:
print('连接失败,重新连接')
return get_attr_data(category_list)
def calculate_configure_price(cat_params_df,productCode,configure_price_df):
#计算配件价格
#productCode = '20210201180416256'
filter_df = cat_params_df[cat_params_df['sku'] == productCode]
price = 0
for name,value in zip(list(filter_df['name']),list(filter_df['value'])):
if name not in list(configure_price_df['part']):
continue
try:
current_price = configure_price_df[(configure_price_df['part'] == name) & (configure_price_df['detail'] == value)]['price'].tolist()[0]
except:
current_price = 0
print("无该配件价格:",name,value)
price+=current_price
return price
def create_price_difference():
#创建SKU 与 SPU 配件差价表
category_list = ['笔记本','台式机','一体电脑']
category_id_list = ['672','673','12798']
category_dict = dict(zip(category_id_list,category_list))
configure_price_df = get_configure_price()
spu_sku_df = get_product_code(category_list)
params_df = get_attr_data(category_list)
#清空原表
conn = pymssql.connect(host='123.57.45.119', user='zgcprice', password='zgcprice20200708', database='price_calculate',autocommit=True)
cursor = conn.cursor()
cursor.execute("delete from configure_price_difference")
create_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
#sku_list = list()
#spuid_list = list()
#configure_difference_list = list()
for category_id in category_id_list:
category = category_dict[category_id]
cat_params_df = params_df[params_df['categoryname'] == category]
cat_configure_price_df = configure_price_df[configure_price_df['category_name'] == category]
sku_source_list = spu_sku_df[spu_sku_df['categoryname'] == category]['sku'].unique().tolist()
process_index = 0
index = ProgressLine()
for sku in sku_source_list:
print(index(process_index,len(sku_source_list)-1), end='%')
process_index+=1
skuname = str(spu_sku_df[spu_sku_df['sku'] == sku]['skuname'].tolist()[0]).replace("'","''")
categoryid = spu_sku_df[spu_sku_df['sku'] == sku]['categoryid'].tolist()[0]
brandid = spu_sku_df[spu_sku_df['sku'] == sku]['brandid'].tolist()[0]
spuid = spu_sku_df[spu_sku_df['sku'] == sku]['spuid'].tolist()[0]
configure_difference = calculate_configure_price(cat_params_df,sku,cat_configure_price_df)
cursor.execute(f"insert into configure_price_difference (sku,skuname,categoryid,brandid,spuid,price_difference,create_time) values ('{sku}','{skuname}','{categoryid}','{brandid}','{spuid}',{configure_difference},'{create_time}')")
#sku_list.append(sku)
#spuid_list.append(spuid)
#configure_difference_list.append(configure_difference)
print(f"{category}计算完成")
conn.close()
def create_price_difference_single(category,sku_list):
#创建SKU 与 SPU 配件差价表
category_list = [category]
configure_price_df = get_configure_price()
spu_sku_df = get_product_code(category_list)
params_df = get_attr_data(category_list)
#清空原表
conn = pymssql.connect(host='123.57.45.119', user='zgcprice', password='zgcprice20200708', database='price_calculate',autocommit=True)
cursor = conn.cursor()
#cursor.execute("delete from configure_price_difference")
create_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
#sku_list = list()
#spuid_list = list()
#configure_difference_list = list()
cat_params_df = params_df[params_df['categoryname'] == category]
cat_configure_price_df = configure_price_df[configure_price_df['category_name'] == category]
#sku_source_list = spu_sku_df[spu_sku_df['categoryname'] == category]['sku'].unique().tolist()
for sku in sku_list:
skuname = str(spu_sku_df[spu_sku_df['sku'] == sku]['skuname'].tolist()[0]).replace("'","''")
categoryid = spu_sku_df[spu_sku_df['sku'] == sku]['categoryid'].tolist()[0]
brandid = spu_sku_df[spu_sku_df['sku'] == sku]['brandid'].tolist()[0]
spuid = spu_sku_df[spu_sku_df['sku'] == sku]['spuid'].tolist()[0]
configure_difference = calculate_configure_price(cat_params_df,sku,cat_configure_price_df)
cursor.execute(f"select sku from configure_price_difference where sku = '{sku}'")
check_df = pd.DataFrame(cursor.fetchall(),columns=[tuple[0] for tuple in cursor.description])
if check_df.empty:
cursor.execute(f"insert into configure_price_difference (sku,skuname,categoryid,brandid,spuid,price_difference,create_time) values ('{sku}','{skuname}','{categoryid}','{brandid}','{spuid}',{configure_difference},'{create_time}')")
else:
cursor.execute(f"update configure_price_difference set price_difference = {configure_difference} , update_time = '{create_time}' where sku = '{sku}'")
#sku_list.append(sku)
#spuid_list.append(spuid)
#configure_difference_list.append(configure_difference)
print(f"{category}新增配件差价计算完成")
conn.close()
def check_configure_price(category,sku_list):
category_list = [category]
configure_price_df = get_configure_price()
params_df = get_attr_data(category_list)
cat_params_df = params_df[params_df['categoryname'] == category]
cat_configure_price_df = configure_price_df[configure_price_df['category_name'] == category]
#sku_source_list = spu_sku_df[spu_sku_df['categoryname'] == category]['sku'].unique().tolist()
for sku in sku_list:
filter_df = cat_params_df[cat_params_df['sku'] == sku]
for name,value in zip(list(filter_df['name']),list(filter_df['value'])):
if name not in list(cat_configure_price_df['part']):
continue
try:
current_price = cat_configure_price_df[(cat_configure_price_df['part'] == name) & (cat_configure_price_df['detail'] == value)]['price'].tolist()[0]
except:
print("无该配件价格:",category,name,value)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment