马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
1、文件Auto_update_data,需要处理的映射基础数据- #定义需要导入的映射数据
- #以字典进行定义 [公司类目id : 平台类目id]
- #d = {key1 : value1, key2 : value2, key3 : value3 }
- ap_category_relation_data = {
- 5807 : 10002200,
- 6375 : 10100737,
- 6772 : 10100733,
- 5816 : 1540,
- 5832 : 1540,
- }
复制代码 2、文件Auto_update_db,自己封装的数据库操作- #数据库操作
- import pymysql
- #数据库类
- class LazadaDb:
- def __init__(self, host, user, password, database, port):
- #打开数据库连接
- db = pymysql.connect(
- host = host,
- user = user,
- password = password,
- database = database,
- port = port
- )
- #使用 cursor()方法创建一个游标对象 cursor
- self.cursor = db.cursor()
-
- #定义一个查询的方法 查一列
- def get(self, sql):
- try:
- cursor = self.cursor
- #执行SQL语句
- cursor.execute(sql)
- #使用 fetchone() 方法获取单条数据
- data = cursor.fetchone()
- #返回数据
- return data
- except:
- return '获取数据出错101'
复制代码 3、文件Auto_update_way,递归,获取类目树方法- #书写公共方法
- import Auto_update_db
- #无限极往上获取平台类目树信息
- def platformCategoryVerify(platform_category_id):
- tree = []
- #获取平台数据库类
- LazadaDb = Auto_update_db.LazadaDb('数据库ip', 'test', '密码', 'nt_auto_publish', 3311)
- #拼接查询sql
- sql = "SELECT category_id,category_name,parent_id,level FROM ap_categories WHERE platform = 1 AND site_code = 'MY' AND category_id = " + str(platform_category_id)
- #获取类目信息
- apCategories_info = LazadaDb.get(sql)
- #判断类目是否存在
- if apCategories_info:
- #存在,通过父类id继续获取上级
- tree = platformCategoryVerify(apCategories_info[2])
- #将获取到类目,添加到 定义的列表 tree 中
- tree.append(apCategories_info)
- return tree
- #无限极往上获取公司类目树信息
- def companyCategoryVerify(company_category_id):
- tree = []
- #获取公司数据库
- SysDb = Auto_update_db.LazadaDb('数据库ip', 'test', '密码', 'nt_product', 3307)
- #拼接查询sql
- sql = "SELECT id, category_name, parent_id, level FROM nt_categories WHERE status = 1 AND ID = " + str(company_category_id)
- #获取类目信息
- sysCategories_info = SysDb.get(sql)
- #p判断类目是否存在
- if sysCategories_info:
- #存在,通过父类id继续获取上级
- tree = companyCategoryVerify(sysCategories_info[2])
- #将获取到类目,添加到 定义的列表 tree 中
- tree.append(sysCategories_info)
- return tree
复制代码 4、文件Auto_update_attr,最后组装数据- #刷自动化类目
- import Auto_update_data
- import Auto_update_way
- #获取需要处理的类目映射数据
- list_data = Auto_update_data.ap_category_relation_data
- #定义一个最终数据的列表
- ap_category_relation_data = []
- for i in list_data:
- print('处理=', list_data[i])
-
- #获取平台类目树信息
- platform_tree_info = Auto_update_way.platformCategoryVerify(list_data[i])
- platform_tree = '->' . join([str(platform_tree_info[i][0]) for i in range(0, len(platform_tree_info))])
- platform_tree_name = '->' . join([str(platform_tree_info[i][1]) for i in range(0, len(platform_tree_info))])
-
- #获取公司类目树信息
- company_tree_info = Auto_update_way.companyCategoryVerify(i)
- company_tree = '->' . join([str(company_tree_info[i][0]) for i in range(0, len(company_tree_info))])
- company_tree_name = '->' . join([str(company_tree_info[i][1]) for i in range(0, len(company_tree_info))])
- value_dic = {
- 'platform' : 1,
- 'site_code' : 'MY',
-
- 'platform_category_id' : list_data[i], #平台类目信息
- 'platform_category_name' : platform_tree_info[1][1],
- 'platform_tree' : platform_tree,
- 'platform_tree_name' : platform_tree_name,
- 'company_category_id' : i, #公司类目信息
- 'company_category_name' : company_tree_info[1][1],
- 'company_tree' : company_tree,
- 'company_tree_name' : company_tree_name,
- }
- ap_category_relation_data.append(value_dic)
- #得到最后映射好的数据
- for i in ap_category_relation_data:
- print(i)
- print('======')
复制代码 5、最后运行文件,打印输出

|