Commit 7ad7afbb authored by 杨林's avatar 杨林

增加类目判断

parent 11c25c4d
......@@ -11,6 +11,7 @@ class ESTools:
_host = [{"host": '172.18.45.18', "port": 9200}, {"host": '172.18.45.17', "port": 9200},
{"host": '172.18.45.16', "port": 9200}, {"host": '172.18.69.190', "port": 9200},
{"host": '172.18.69.192', "port": 9200}]
# _host = 'localhost:9200'
_TB_RATE = 0.7
_JD_RATE = 0.55
......@@ -18,9 +19,10 @@ class ESTools:
def __init__(self):
self.es = Elasticsearch(self._host)
def search_tools(self, price=0, title="", size=20):
def search_tools(self, price=0, title="", category="", size=20):
"""
搜索es方法
:param category: 商品类目
:param price: 券后价
:param title: 商品名称
:param size: 商品数量
......@@ -58,64 +60,70 @@ class ESTools:
else:
return None
def data_format(self, price=0, title="", size=20):
def data_format(self, price=0, title="", category="", size=20):
"""
格式化数据方法
:param category: 商品类目
:param price: 券后价
:param title: 商品标题
:param size: 商品数量
:return: 格式化后的数据
"""
res, sco = self.search_tools(price=price, title=title, size=size)
res, sco = self.search_tools(price=price, title=title, category=category, size=size)
if res:
temp = []
# 匹配分数平均数
score_avg = sum(sco) / len(sco)
# 匹配分数中位数
score_median = sco[int(len(sco) / 2)]
for i in res:
# 对每条数据进行处理
# 分数大于中位数
if i["_score"] > score_median:
# 分数大于平均数
if i["_score"] > score_avg:
# 源数据
source = i['_source']
# 平台
platform = source["platform"]
# 券后佣金
coupon_commission = source["coupon_commission"]
# 给用户的佣金比例
rate = self._JD_RATE if platform == 2 else self._TB_RATE
# 券金额
coupon_amount = source["coupon_amount"]
# 优惠文案
discount_text = f'¥{coupon_amount}券' if coupon_amount > 0 else ''
# 用户佣金
user_commission = "{:.2f}".format(coupon_commission * rate)
# 券后价
end_price = source["coupon_price"]
# 最终优惠金额
final_price = round(price - end_price, 2)
result = {
"item_id": source["item_id"],
"item_pic": source["item_pic_url"].split(',')[0],
"item_title": source["item_name"],
"discount_type": "1",
"discount_amount": str(source["coupon_amount"]),
"platform": str(platform),
"shop_title": source["shop_name"],
"user_commission": user_commission,
"price": str(source["price"]),
"end_price": str(end_price),
"volume_text": f'已抢{source["item_volume"]}件',
"discount_text": discount_text,
"discount_tips": f'比查券商品便宜{final_price}元',
"user_commission_text": f'返现{user_commission}'
}
temp.append(result)
# 按价格排序
results = sorted(temp, key=lambda x: x["end_price"])
return results
score_max = max(sco)
if score_max < 50:
return None
else:
for i in res:
# 对每条数据进行处理
# 分数大于中位数
if i["_score"] > score_median:
# 分数大于平均数
if i["_score"] > score_avg:
# 源数据
source = i['_source']
# 平台
platform = source["platform"]
# 券后佣金
coupon_commission = source["coupon_commission"]
# 给用户的佣金比例
rate = self._JD_RATE if platform == 2 else self._TB_RATE
# 券金额
coupon_amount = source["coupon_amount"]
# 优惠文案
discount_text = f'¥{coupon_amount}券' if coupon_amount > 0 else ''
# 用户佣金
user_commission = "{:.2f}".format(coupon_commission * rate)
# 券后价
end_price = source["coupon_price"]
# 最终优惠金额
final_price = round(price - end_price, 2)
result = {
"item_id": source["item_id"],
"item_pic": source["item_pic_url"].split(',')[0],
"item_title": source["item_name"],
"discount_type": "1",
"discount_amount": str(source["coupon_amount"]),
"platform": str(platform),
"shop_title": source["shop_name"],
"user_commission": user_commission,
"price": str(source["price"]),
"end_price": str(end_price),
"volume_text": f'已抢{source["item_volume"]}件',
"discount_text": discount_text,
"discount_tips": f'比查券商品便宜{final_price}元',
"user_commission_text": f'返现{user_commission}'
}
temp.append(result)
# 按价格排序
results = sorted(temp, key=lambda x: x["end_price"])
return results
else:
return None
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