update_goods_hashV1.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # -*- codeing = utf-8 -*-
  2. # @Time : 2021/5/15 12:55
  3. # @Author : Clown
  4. # @File : update_goods_hash.py
  5. # @Software : PyCharm
  6. import pymysql
  7. import time
  8. from datetime import timedelta, datetime
  9. import sys
  10. import os
  11. def linkTomySql(host,passwd,db_name):
  12. '''连接至数据库返回【db】,v2新增local_infile=1 打开文件导入权限'''
  13. try:
  14. #本地连接为:localhost 服务器连接为:
  15. db = pymysql.connect(
  16. host=host, user="root",
  17. passwd=passwd,
  18. db=db_name,
  19. charset='utf8mb4',
  20. local_infile=1)
  21. print('\nconnect to mysql server 成功')
  22. print('---------------------------------------')
  23. except:
  24. print("\ncould not connect to mysql server")
  25. db = "连接失败"
  26. return db
  27. def update_scriptrunninglogs(log_date,log_time,running_Time,operation_results,table_name,db):
  28. '''将脚本执行情况发送至【脚本执行日志】表
  29. \n 配套内容
  30. \n from datetime import datetime
  31. \n import sys
  32. \n import traceback
  33. \n time_begin = time.time()
  34. \n log_date = datetime.now().strftime("%Y-%m-%d") # 日志记录日期
  35. \n log_time = datetime.now().strftime('%H:%M:%S') # 日志记录时间
  36. \n df_name = '数据库表名'
  37. \n try:
  38. \n operation_results = '执行成功'
  39. \n except Exception as e:
  40. \n operation_results = traceback.format_exc()
  41. \n running_Time = time.time() - time_begin # 脚本运行时长
  42. '''
  43. try:
  44. sql = "INSERT INTO scriptrunninglogs VALUES (%s,%s,%s,%s,%s,%s,%s)"#date, time, running_Time, script_name, operation_results, table_name, id
  45. script_name = os.path.basename(sys.argv[0])
  46. value = (log_date,log_time,running_Time,script_name,operation_results,table_name,0)
  47. cursor = db.cursor()
  48. cursor.execute(sql, value) # 执行sql语句
  49. db.commit()
  50. cursor.close()
  51. except:
  52. result = "数据库连接失败"
  53. print(result)
  54. def update_goods_hash(db,formgoods_table_name,brand_name,date_min_h,data_max_h):
  55. '''此函数用于更新goods_hash表,更新内容为:
  56. 1.新增goods_hash中不存在的【商品】信息
  57. \n 2.刷新【商品】【关联品牌】字段中品牌名
  58. \n 3.字段注解
  59. \n [db]:数据库实例
  60. \n [formgoods_table_name]:订单商品明细表名称
  61. \n [brand_name]:品牌名拼音首字母大写
  62. \n [date_min_h]:时间筛选维度起始时间
  63. \n [data_max_h]:时间筛选维度结束时间
  64. '''
  65. time_count = time.time()
  66. cursor_hash = db.cursor()
  67. # 筛选出订单商品明细表中,相应日期内的【商品】数据/对【商品】相关信息进行去重取唯一/判断【商品】数据在goods_hash表中是否存在/输出数据
  68. sql_hash_new = "SELECT DISTINCT "\
  69. "t1.`平台商品名称`,"\
  70. "t1.`标准商品名称`" \
  71. " FROM "\
  72. "( SELECT *" \
  73. f" FROM {formgoods_table_name}" \
  74. f" WHERE {formgoods_table_name}.`日期` >= "+str(date_min_h)+\
  75. f" AND {formgoods_table_name}.`日期` <= "+str(data_max_h)+" ) t1 "\
  76. " WHERE "\
  77. "t1.`标准商品名称` NOT IN ( SELECT goods_hash.`标准商品名称` FROM goods_hash )"
  78. # 导入数据至goods_hash表中
  79. sql_in_hash = '''INSERT INTO goods_hash VALUES(%s,%s,%s,'')'''
  80. cursor_hash.execute(sql_hash_new)
  81. results_h = cursor_hash.fetchall()
  82. good_in_count = 0
  83. for result_h in results_h:
  84. value_h = (result_h[0], result_h[1], brand_name)
  85. cursor_hash.execute(sql_in_hash, value_h)
  86. db.commit()
  87. good_in_count += 1
  88. # 更新原有的商品关联品牌信息
  89. sql_hash_old = "SELECT DISTINCT " \
  90. "t1.`平台商品名称`," \
  91. "t1.`标准商品名称`" \
  92. " FROM " \
  93. "( SELECT *" \
  94. f" FROM {formgoods_table_name}" \
  95. f" WHERE {formgoods_table_name}.`日期` >= " + str(date_min_h) + \
  96. f" AND {formgoods_table_name}.`日期` <= " + str(data_max_h) + " ) t1 " \
  97. " WHERE " \
  98. "t1.`标准商品名称` IN ( SELECT goods_hash.`标准商品名称` FROM goods_hash )"
  99. cursor_hash.execute(sql_hash_old)
  100. results_h = cursor_hash.fetchall()
  101. good_up_count = 0
  102. for result_h in results_h:
  103. sql = f'''SELECT goods_hash.`关联品牌` FROM goods_hash WHERE goods_hash.`标准商品名称` = '{result_h[1]}';'''
  104. cursor_hash.execute(sql)
  105. result = str(cursor_hash.fetchall()[0][0])
  106. if result == 'None':
  107. brand_cent = brand_name + ','
  108. sql_up_hash = f"UPDATE goods_hash SET goods_hash.`关联品牌` = '{brand_cent}' WHERE goods_hash.`标准商品名称` = '{result_h[1]}';"
  109. cursor_hash.execute(sql_up_hash)
  110. db.commit()
  111. good_up_count += 1
  112. elif brand_name not in result:
  113. brand_cent = brand_name + ','
  114. sql_up_hash = f"UPDATE goods_hash SET goods_hash.`关联品牌` = CONCAT(goods_hash.`关联品牌`,'{brand_cent}') WHERE goods_hash.`标准商品名称` = '{result_h[1]}';"
  115. cursor_hash.execute(sql_up_hash)
  116. db.commit()
  117. good_up_count += 1
  118. cursor_hash.close()
  119. print(f"{brand_name},goods_hash导入用时",time.time()-time_count,"秒****","已录入",good_in_count,"个新增品名,","已更新",good_up_count,"个原有品名")
  120. def update_goods_hash_activeDate(db,date):
  121. cursor = db.cursor()
  122. sql = f"UPDATE goods_hash SET goods_hash.`最近一次活跃时间` = '{date}' WHERE goods_hash.`关联品牌` != 'other' AND (goods_hash.`标准商品名称` IN ( SELECT DISTINCT( order_formsgoods.`标准商品名称` ) FROM order_formsgoods WHERE order_formsgoods.`日期` = {int(date)}) OR goods_hash.`标准商品名称` IN ( SELECT DISTINCT( order_formsgoods_zzx.`标准商品名称` ) FROM order_formsgoods_zzx WHERE order_formsgoods_zzx.`日期` = {int(date)}) OR goods_hash.`标准商品名称` IN (SELECT DISTINCT( order_formsgoods_lls.`标准商品名称` ) FROM order_formsgoods_lls WHERE order_formsgoods_lls.`日期` = {int(date)}))"
  123. try:
  124. cursor.execute(sql)
  125. db.commit()
  126. print(f'已完成【{date}】goods时间更新')
  127. except:
  128. db.rollback()
  129. print(f'【{date}】更新存在错误')
  130. if __name__ == '__main__':
  131. #日志参数
  132. time_begin = time.time()
  133. log_date = datetime.now().strftime("%Y-%m-%d") # 日志记录日期
  134. log_time = datetime.now().strftime('%H:%M:%S') # 日志记录时间
  135. #通用参数
  136. host = 'localhost'
  137. passwd = '111???clown'
  138. db_name = 'hexingxing'
  139. table_name = 'goods_hash'
  140. #时间参数,用于限制数据扫描范围,默认为昨日
  141. yesterday = (datetime.now() + timedelta(days=-1)).strftime("%Y%m%d")
  142. date_min_h = yesterday
  143. data_max_h = yesterday
  144. #订单商品信息对照表,表名及对应品牌名
  145. formgoods_table_names = [{'formgoods_table_name':'order_formsgoods','brand_name':'JXB'},
  146. {'formgoods_table_name':'order_formsgoods_zzx','brand_name':'ZXX'},
  147. {'formgoods_table_name':'order_formsgoods_lls','brand_name':'LLS'}]
  148. operation_results = '执行成功'
  149. db = linkTomySql(host,passwd,db_name)
  150. for formgoods_table in formgoods_table_names:
  151. formgoods_table_name = formgoods_table['formgoods_table_name']
  152. brand_name = formgoods_table['brand_name']
  153. try:
  154. update_goods_hash(db,formgoods_table_name,brand_name,date_min_h,data_max_h)
  155. except Exception as e:
  156. operation_results = e
  157. update_goods_hash_activeDate (db, yesterday)
  158. #将此脚本执行结果返回至log中
  159. running_Time = time.time() - time_begin # 脚本运行时长
  160. db = linkTomySql(host, passwd, db_name)
  161. update_scriptrunninglogs(log_date,log_time,running_Time,operation_results,table_name,db)