Commit 1766d98b by larosa

add script

parents
rabbit_mq_host = "192.168.1.237"
rabbit_mq_queue_consumer = "sku_manage_file_upload"
rabbit_mq_queue_producer = "europa_erp_sku_queue"
rabbit_mq_port = 5672
rabbit_mq_user = "huntadmin"
rabbit_mq_password = "jy2y2900"
rabbit_mq_exchange = "europa_erp_parse_file_exchange"
rabbit_mq_routing_key = "europa_erp_sku_routing"
tmp_xls_file_dir = "/data/golang/src/europa-erp-go/scripts/data_server/tmp"
rabbit_mq_host = "119.23.79.136"
rabbit_mq_queue_consumer = "sku_manage_file_upload"
rabbit_mq_queue_producer = "europa_erp_sku_queue"
rabbit_mq_port = 5672
rabbit_mq_user = "ans2024"
rabbit_mq_password = "ans2024123"
rabbit_mq_exchange = "europa_erp_parse_file_exchange"
rabbit_mq_routing_key = "europa_erp_sku_routing"
tmp_xls_file_dir = "/data/golang/src/europa-erp-go/scripts/data_server/tmp"
\ No newline at end of file
import hashlib
import json
import os
import pandas as pd
import pika
import requests
import config
title_arr = [
"原厂型号",
"品牌",
"库存",
"起订量",
"阶梯数量1", "价格1",
"阶梯数量2", "价格2",
"阶梯数量3", "价格3",
"阶梯数量4", "价格4",
"阶梯数量5", "价格5",
"批次",
"增量",
"封装"
]
class Producer:
def __init__(self):
credentials = pika.PlainCredentials(username=config.rabbit_mq_user, password=config.rabbit_mq_password)
self.conn = pika.BlockingConnection(pika.ConnectionParameters(
host=config.rabbit_mq_host,
port=config.rabbit_mq_port,
credentials=credentials
))
channel = self.conn.channel()
channel.queue_declare(queue=config.rabbit_mq_queue_producer, durable=True)
self.channel = channel
def push(self, message):
self.channel.basic_publish(
exchange=config.rabbit_mq_exchange,
routing_key=config.rabbit_mq_routing_key,
body=str.encode(message))
def close(self):
try:
self.channel.close()
self.conn.close()
except Exception as e:
print(e)
def resolve(file_name: str, supplier_id: int):
producer = Producer()
df = pd.read_excel(file_name, sheet_name=0)
for i in range(len(df.index.values)):
goods_name = str(df.loc[i, "原厂型号"])
brand_name = str(df.loc[i, "品牌"])
stock = str(df.loc[i, "库存"])
moq = str(df.loc[i, "起订量"])
price_is_us = False
supp_id = int(supplier_id)
supplier_name = ""
multiple = str(df.loc[i, "增量"])
batch_sn = {
str(df.loc[i, "批次"]): int(df.loc[i, "库存"]),
}
eccn = ""
if goods_name.strip() == "" or brand_name.strip() == "":
continue
if str(df.loc[i, "批次"]).strip() == "":
batch_sn = {}
table = {
"goods_name": goods_name,
"brand_name": brand_name,
"stock": stock,
"moq": moq,
"price_is_us": price_is_us,
"supplier_name": supplier_name,
"multiple": multiple,
"eccn": eccn,
"supplier_id": supp_id,
"batch_sn": batch_sn,
"ladder_price": [],
"goods_sn": "",
}
normal_text = (goods_name + brand_name + str(supplier_id) + str(df.loc[i, "封装"]) + str(df.loc[i, "批次"])).lower()
md = hashlib.md5(normal_text.encode())
md_str = md.hexdigest()
table["goods_sn"] = md_str
for index in range(1, 6):
ladder_data = df.loc[i, "阶梯数量" + str(index)]
price_data = df.loc[i, "价格" + str(index)]
if str(ladder_data).strip() != "" and str(ladder_data).strip() != "nan" and str(ladder_data).strip() != "NaN" and \
str(price_data).strip() != "" and str(price_data).strip() != "nan" and str(price_data).strip() != "NaN":
item_map = dict()
item_map["purchases"] = int(ladder_data)
item_map["price_us"] = float(0)
item_map["price_cn"] = float(price_data)
table["ladder_price"].append(item_map)
ans = json.dumps(table, ensure_ascii=False)
producer.push(ans)
os.remove(file_name)
producer.close()
def process(url: str, supplier_id: int):
# http://erp.liexindev.net/sku/20240823/p6jMKaPtjMpNnZMK5vEX3KEQf7Gdvgvw3TCA7d6b.xls
try:
response = requests.get(url)
if response.status_code == 200:
tmp_file = url.split(sep="/")[-1]
absolute_path = config.tmp_xls_file_dir + tmp_file
with open(absolute_path, "wb") as f:
f.write(response.content)
resolve(absolute_path, supplier_id)
except Exception as e:
print(e)
import pika
import config
import handle
def callback(ch, method, properties, body):
try:
data = eval(body.decode())
if "file_address" in data and "supplier_id" in data:
url = str(data["file_address"])
url = url.replace("\\", "")
supplier_id = data["supplier_id"]
handle.process(url, supplier_id)
except Exception as e:
print(e)
ch.basic_ack(delivery_tag=method.delivery_tag)
def listen_from_mq():
credentials = pika.PlainCredentials(config.rabbit_mq_user, config.rabbit_mq_password)
conn = pika.BlockingConnection(pika.ConnectionParameters(
host=config.rabbit_mq_host,
port=config.rabbit_mq_port,
credentials=credentials))
channel = conn.channel()
channel.queue_declare(queue=config.rabbit_mq_queue_consumer, durable=True)
channel.basic_consume(config.rabbit_mq_queue_consumer, callback)
channel.start_consuming()
if __name__ == '__main__':
print("start listen from mq...")
listen_from_mq()
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 sign in to comment