调整数据结构
This commit is contained in:
@@ -186,7 +186,7 @@ class GoodsModel:
|
||||
for item in db_data:
|
||||
if item['spu'] in index_data:
|
||||
index_data[item['spu']].p_lists.append(
|
||||
types.GoodsInfo.GoodsInfoP(
|
||||
types.GoodsInfo.Attributes(
|
||||
attr_items = [item['son_item1'], item['son_item2'], item['son_item3']],
|
||||
price = item['son_price'],
|
||||
old_price = item['son_old_price'],
|
||||
@@ -212,7 +212,7 @@ class GoodsModel:
|
||||
)
|
||||
if item['attr'] == 'M':
|
||||
index_data[item['spu']].p_lists.append(
|
||||
types.GoodsInfo.GoodsInfoP(
|
||||
types.GoodsInfo.Attributes(
|
||||
attr_items = [item['son_item1'], item['son_item2'], item['son_item3']],
|
||||
price = item['son_price'],
|
||||
old_price = item['son_old_price'],
|
||||
|
||||
117
core/types.py
117
core/types.py
@@ -10,47 +10,52 @@ class GoodsInfo(BaseModel):
|
||||
"""
|
||||
商品类(包含处理)
|
||||
|
||||
:param url: 主商品链接 [必填]
|
||||
:param spu: [必填]
|
||||
:param title: 商品标题 [必填]
|
||||
:param attribute_type: 属性类型 S|M [必填]
|
||||
:param images: 图片列表 [必填]
|
||||
:param brand: 品牌
|
||||
:param desc: 简介
|
||||
:param attr: 属性 S|M
|
||||
:param attr_items: 变体[名称]列表 [Color, Size]
|
||||
:param attribute_names: 属性[名称]列表 [Color, Size]
|
||||
:param price: 价格
|
||||
:param old_price: 原价
|
||||
:param images: 图片列表
|
||||
:param url: 主商品链接
|
||||
:param p_urls: 变体链接列表
|
||||
:param p_lists: 变体类列表
|
||||
:param original_price: 原价
|
||||
:param attributes: 变体类列表
|
||||
:param attribute_urls: 变体链接列表
|
||||
:param other_data: 其他参数
|
||||
|
||||
"""
|
||||
class GoodsInfoP(BaseModel):
|
||||
class Attributes(BaseModel):
|
||||
"""
|
||||
变体类
|
||||
|
||||
:param attr_items: 变体[值]列表 [red, 30]
|
||||
:param attribute_values: 变体[值]列表 [red, 30] [必填]
|
||||
:param price: 价格 [必填]
|
||||
:param original_price: 原价 [必填]
|
||||
:param url: 变体链接
|
||||
:param images: 变体图片列表
|
||||
:param other_data: 其他参数
|
||||
|
||||
"""
|
||||
attr_items: list = [] # 变体名称1,2,3
|
||||
price: float = 0.00
|
||||
old_price: float = 0.00
|
||||
images: list[str] = []
|
||||
url: str = '' # 商品链接
|
||||
attribute_values: list # 属性值1,2,3
|
||||
price: float
|
||||
original_price: float
|
||||
images: list[str] = []
|
||||
other_data: dict = {} # 其他参数
|
||||
|
||||
url: str
|
||||
spu: str
|
||||
title: str
|
||||
brand: str = '' # 品牌
|
||||
desc: str = '' # 简介
|
||||
attr: str = 'S' # 属性 S M
|
||||
attr_items: list = [] # 变体名称1,2,3
|
||||
attribute_type: str # 属性类型 S M
|
||||
images: list[str]
|
||||
attribute_names: list = [] # 属性名称1,2,3
|
||||
price: float = 0.00
|
||||
old_price: float = 0.00
|
||||
images: list[str] = []
|
||||
url: str = '' # 商品链接
|
||||
p_urls: list[str] = [] # 变体链接列表(筛选用,可略)
|
||||
p_lists: list[GoodsInfoP] = [] # 变体列表
|
||||
original_price: float = 0.00
|
||||
attributes: list[Attributes] = [] # 变体列表
|
||||
attribute_urls: list[str] = [] # 变体链接列表(筛选用,可略)
|
||||
other_data: dict = {} # 其他参数
|
||||
|
||||
def to_db_data(self):
|
||||
@@ -60,30 +65,30 @@ class GoodsInfo(BaseModel):
|
||||
'spu': self.spu,
|
||||
'title': self.title,
|
||||
'brand': self.brand,
|
||||
'attr': self.attr,
|
||||
'attr': self.attribute_type,
|
||||
'desc': self.desc,
|
||||
'price': self.price,
|
||||
'old_price': self.old_price,
|
||||
'original_price': self.original_price,
|
||||
'images': orjson.dumps(self.images),
|
||||
'url': self.url,
|
||||
'other_data': orjson.dumps(self.other_data)
|
||||
}
|
||||
for index, item_name in enumerate(self.attr_items):
|
||||
for index, item_name in enumerate(self.attribute_names):
|
||||
if item_name:
|
||||
main_info[f"item{index+1}"] = item_name
|
||||
|
||||
son_infos = []
|
||||
for p_info in self.p_lists:
|
||||
for p_info in self.attributes:
|
||||
item = {
|
||||
'spu': self.spu,
|
||||
'price': p_info.price,
|
||||
'old_price': p_info.old_price,
|
||||
'original_price': p_info.original_price,
|
||||
'images': orjson.dumps(p_info.images),
|
||||
'url': p_info.url,
|
||||
'other_data': orjson.dumps(p_info.other_data)
|
||||
}
|
||||
p_info.attr_items += [None, None]
|
||||
for index, item_name in enumerate(p_info.attr_items[:3]):
|
||||
p_info.attribute_values += [None, None]
|
||||
for index, item_name in enumerate(p_info.attribute_values[:3]):
|
||||
if item_name:
|
||||
item[f"item{index+1}"] = item_name
|
||||
son_infos.append(item)
|
||||
@@ -92,18 +97,18 @@ class GoodsInfo(BaseModel):
|
||||
|
||||
def to_shopyy_row_data(self):
|
||||
"""转为shopyy行数据"""
|
||||
prices = [self.price, self.old_price]
|
||||
for p_info in self.p_lists:
|
||||
prices = [self.price, self.original_price]
|
||||
for p_info in self.attributes:
|
||||
prices.append(p_info.price)
|
||||
prices.append(p_info.old_price)
|
||||
old_price = max(prices)
|
||||
prices.append(p_info.original_price)
|
||||
original_price = max(prices)
|
||||
|
||||
rows_data = []
|
||||
rows_data.append([
|
||||
"", # 商品ID(系统生成,留空)
|
||||
"",
|
||||
f"{self.brand} {self.title}" if self.brand != "" else self.title,
|
||||
self.attr,
|
||||
self.attribute_type,
|
||||
"",
|
||||
"",
|
||||
self.desc,
|
||||
@@ -122,9 +127,9 @@ class GoodsInfo(BaseModel):
|
||||
self.brand, # 专辑名称
|
||||
"",
|
||||
"",
|
||||
*(self.attr_items[:3] + [''] * (3 - len(self.attr_items)))[:3], # 款式1/2/3
|
||||
*(self.attribute_names[:3] + [''] * (3 - len(self.attribute_names)))[:3], # 款式1/2/3
|
||||
self.price,
|
||||
old_price,
|
||||
original_price,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
@@ -133,14 +138,14 @@ class GoodsInfo(BaseModel):
|
||||
'', # 变体备注2
|
||||
','.join(self.images) if self.images else '',
|
||||
self.url,
|
||||
','.join(self.p_urls) if self.p_urls else ''
|
||||
','.join(self.attribute_urls) if self.attribute_urls else ''
|
||||
])
|
||||
|
||||
p_index = []
|
||||
for p_info in self.p_lists:
|
||||
if p_info.attr_items in p_index:
|
||||
for p_info in self.attributes:
|
||||
if p_info.attribute_values in p_index:
|
||||
continue
|
||||
p_index.append(p_info.attr_items)
|
||||
p_index.append(p_info.attribute_values)
|
||||
rows_data.append([
|
||||
"", # 商品ID(系统生成,留空)
|
||||
"",
|
||||
@@ -164,9 +169,9 @@ class GoodsInfo(BaseModel):
|
||||
self.brand, # 专辑名称
|
||||
"",
|
||||
"",
|
||||
*(p_info.attr_items[:3] + [''] * (3 - len(p_info.attr_items)))[:3], # 款式1/2/3
|
||||
*(p_info.attribute_values[:3] + [''] * (3 - len(p_info.attribute_values)))[:3], # 款式1/2/3
|
||||
p_info.price,
|
||||
old_price,
|
||||
original_price,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
@@ -181,7 +186,7 @@ class GoodsInfo(BaseModel):
|
||||
|
||||
def to_woo_row_data(self, category_name):
|
||||
"""转为woo行数据"""
|
||||
def get_lists(attr = 'variable',spu = '', price = '', old_price = '', item_name1: str = '', item_value1: str = '', item_name2: str = '', item_value2: str = '', item_name3: str = '', item_value3: str = '', images: list = []):
|
||||
def get_lists(attr = 'variable',spu = '', price = '', original_price = '', item_name1: str = '', item_value1: str = '', item_name2: str = '', item_value2: str = '', item_name3: str = '', item_value3: str = '', images: list = []):
|
||||
return [
|
||||
attr,
|
||||
self.spu if attr == 'variable' or attr == 'simple' else "",
|
||||
@@ -191,7 +196,7 @@ class GoodsInfo(BaseModel):
|
||||
1,
|
||||
100,
|
||||
price,
|
||||
old_price,
|
||||
original_price,
|
||||
category_name if attr == 'variable' or attr == 'simple' else "",
|
||||
','.join(images) if images else '',
|
||||
self.spu if attr == 'variation' else "",
|
||||
@@ -208,12 +213,12 @@ class GoodsInfo(BaseModel):
|
||||
|
||||
desc = BeautifulSoup(self.desc, 'html.parser').get_text()
|
||||
rows_data = []
|
||||
if self.attr == 'S':
|
||||
if self.attribute_type == 'S':
|
||||
rows_data.append(get_lists(
|
||||
attr='simple',
|
||||
spu=self.spu,
|
||||
price=self.price,
|
||||
old_price=self.old_price,
|
||||
original_price=self.original_price,
|
||||
images=self.images
|
||||
))
|
||||
else:
|
||||
@@ -221,10 +226,10 @@ class GoodsInfo(BaseModel):
|
||||
item_values1 = set()
|
||||
item_values2 = set()
|
||||
item_values3 = set()
|
||||
for p_info in self.p_lists:
|
||||
item_value1 = p_info.attr_items[0] if len(p_info.attr_items) >= 1 else ""
|
||||
item_value2 = p_info.attr_items[1] if len(p_info.attr_items) >= 2 else ""
|
||||
item_value3 = p_info.attr_items[2] if len(p_info.attr_items) >= 3 else ""
|
||||
for p_info in self.attributes:
|
||||
item_value1 = p_info.attribute_values[0] if len(p_info.attribute_values) >= 1 else ""
|
||||
item_value2 = p_info.attribute_values[1] if len(p_info.attribute_values) >= 2 else ""
|
||||
item_value3 = p_info.attribute_values[2] if len(p_info.attribute_values) >= 3 else ""
|
||||
|
||||
item_value1 = item_value1.replace(',', ',').replace('|', ' ')
|
||||
item_values1.add(item_value1)
|
||||
@@ -239,25 +244,25 @@ class GoodsInfo(BaseModel):
|
||||
attr='variation',
|
||||
spu='',
|
||||
price=p_info.price,
|
||||
old_price=p_info.old_price,
|
||||
original_price=p_info.original_price,
|
||||
images=p_info.images,
|
||||
item_name1=self.attr_items[0] if len(self.attr_items) >= 1 else "",
|
||||
item_name1=self.attribute_names[0] if len(self.attribute_names) >= 1 else "",
|
||||
item_value1=item_value1,
|
||||
item_name2=self.attr_items[1] if len(self.attr_items) >= 2 else "",
|
||||
item_name2=self.attribute_names[1] if len(self.attribute_names) >= 2 else "",
|
||||
item_value2=item_value2,
|
||||
item_name3=self.attr_items[2] if len(self.attr_items) >= 3 else "",
|
||||
item_name3=self.attribute_names[2] if len(self.attribute_names) >= 3 else "",
|
||||
item_value3=item_value3
|
||||
))
|
||||
rows_data.insert(0, get_lists(
|
||||
spu=self.spu,
|
||||
price='',
|
||||
old_price='',
|
||||
original_price='',
|
||||
images=self.images,
|
||||
item_name1=self.attr_items[0] if len(self.attr_items) >= 1 else "",
|
||||
item_name1=self.attribute_names[0] if len(self.attribute_names) >= 1 else "",
|
||||
item_value1='|'.join(item_values1),
|
||||
item_name2=self.attr_items[1] if len(self.attr_items) >= 2 else "",
|
||||
item_name2=self.attribute_names[1] if len(self.attribute_names) >= 2 else "",
|
||||
item_value2='|'.join(item_values2),
|
||||
item_name3=self.attr_items[2] if len(self.attr_items) >= 3 else "",
|
||||
item_name3=self.attribute_names[2] if len(self.attribute_names) >= 3 else "",
|
||||
item_value3='|'.join(item_values3)
|
||||
))
|
||||
return rows_data
|
||||
|
||||
Reference in New Issue
Block a user