Python改写PHP刷类目映射脚本逻辑

[复制链接]
发表于 2022-8-29 08:16:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1、文件Auto_update_data,需要处理的映射基础数据
  1. #定义需要导入的映射数据
  2. #以字典进行定义 [公司类目id : 平台类目id]
  3. #d = {key1 : value1, key2 : value2, key3 : value3 }
  4. ap_category_relation_data = {
  5.         5807 : 10002200,
  6.         6375 : 10100737,
  7.         6772 : 10100733,
  8.         5816 : 1540,
  9.         5832 : 1540,
  10. }
复制代码
2、文件Auto_update_db,自己封装的数据库操作
  1. #数据库操作
  2. import pymysql
  3. #数据库类
  4. class LazadaDb:
  5.     def __init__(self, host, user, password, database, port):
  6.         #打开数据库连接
  7.         db = pymysql.connect(
  8.             host = host,
  9.             user = user,
  10.             password = password,
  11.             database = database,
  12.             port = port
  13.         )
  14.         #使用 cursor()方法创建一个游标对象 cursor
  15.         self.cursor = db.cursor()
  16.    
  17.     #定义一个查询的方法 查一列
  18.     def get(self, sql):
  19.         try:
  20.             cursor = self.cursor
  21.             #执行SQL语句
  22.             cursor.execute(sql)
  23.             #使用 fetchone() 方法获取单条数据
  24.             data = cursor.fetchone()
  25.             #返回数据
  26.             return data
  27.         except:
  28.             return '获取数据出错101'
复制代码
3、文件Auto_update_way,递归,获取类目树方法
  1. #书写公共方法
  2. import Auto_update_db
  3. #无限极往上获取平台类目树信息
  4. def platformCategoryVerify(platform_category_id):
  5.     tree = []
  6.     #获取平台数据库类
  7.     LazadaDb = Auto_update_db.LazadaDb('数据库ip', 'test', '密码', 'nt_auto_publish', 3311)
  8.     #拼接查询sql
  9.     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)
  10.     #获取类目信息
  11.     apCategories_info = LazadaDb.get(sql)
  12.     #判断类目是否存在
  13.     if apCategories_info:
  14.         #存在,通过父类id继续获取上级
  15.         tree = platformCategoryVerify(apCategories_info[2])
  16.         #将获取到类目,添加到 定义的列表 tree 中
  17.         tree.append(apCategories_info)
  18.     return tree
  19. #无限极往上获取公司类目树信息
  20. def companyCategoryVerify(company_category_id):
  21.     tree = []
  22.     #获取公司数据库
  23.     SysDb = Auto_update_db.LazadaDb('数据库ip', 'test', '密码', 'nt_product', 3307)
  24.     #拼接查询sql
  25.     sql = "SELECT id, category_name, parent_id, level FROM nt_categories WHERE status = 1 AND ID = " + str(company_category_id)
  26.     #获取类目信息
  27.     sysCategories_info = SysDb.get(sql)
  28.     #p判断类目是否存在
  29.     if sysCategories_info:
  30.         #存在,通过父类id继续获取上级
  31.         tree = companyCategoryVerify(sysCategories_info[2])
  32.         #将获取到类目,添加到 定义的列表 tree 中
  33.         tree.append(sysCategories_info)
  34.     return tree
复制代码
4、文件Auto_update_attr,最后组装数据
  1. #刷自动化类目
  2. import Auto_update_data
  3. import Auto_update_way
  4. #获取需要处理的类目映射数据
  5. list_data = Auto_update_data.ap_category_relation_data
  6. #定义一个最终数据的列表
  7. ap_category_relation_data = []
  8. for i in list_data:
  9.     print('处理=', list_data[i])
  10.    
  11.     #获取平台类目树信息
  12.     platform_tree_info = Auto_update_way.platformCategoryVerify(list_data[i])
  13.     platform_tree = '->' . join([str(platform_tree_info[i][0]) for i in range(0, len(platform_tree_info))])
  14.     platform_tree_name = '->' . join([str(platform_tree_info[i][1]) for i in range(0, len(platform_tree_info))])
  15.    
  16.     #获取公司类目树信息
  17.     company_tree_info = Auto_update_way.companyCategoryVerify(i)
  18.     company_tree = '->' . join([str(company_tree_info[i][0]) for i in range(0, len(company_tree_info))])
  19.     company_tree_name = '->' . join([str(company_tree_info[i][1]) for i in range(0, len(company_tree_info))])
  20.     value_dic = {
  21.         'platform' : 1,
  22.         'site_code' : 'MY',
  23.         
  24.         'platform_category_id' : list_data[i], #平台类目信息
  25.         'platform_category_name' : platform_tree_info[1][1],
  26.         'platform_tree' : platform_tree,
  27.         'platform_tree_name' : platform_tree_name,
  28.         'company_category_id' : i, #公司类目信息
  29.         'company_category_name' : company_tree_info[1][1],
  30.         'company_tree' : company_tree,
  31.         'company_tree_name' : company_tree_name,
  32.     }
  33.     ap_category_relation_data.append(value_dic)
  34. #得到最后映射好的数据
  35. for i in ap_category_relation_data:
  36.     print(i)
  37.     print('======')
复制代码
5、最后运行文件,打印输出

 
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表