IncentiveSettlementEmail.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2024/9/26 16:08
  4. # @Site :
  5. # @File : CanalEmail.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import datetime
  10. import urllib
  11. from urllib import parse
  12. import pandas as pd
  13. import smtplib
  14. from email.mime.text import MIMEText
  15. from email.mime.multipart import MIMEMultipart
  16. from sqlalchemy import create_engine
  17. class CanalEmail:
  18. def __init__(self):
  19. self.OuterStr = "mysql+pymysql://%s:%s@%s:%d/%s" % (
  20. "data", parse.quote_plus("hcst@2021"), "139.186.27.96", 3390, "application")
  21. self.OuterConn = create_engine(self.OuterStr,
  22. pool_size=10,
  23. max_overflow=5,
  24. pool_timeout=10,
  25. pool_recycle=3600)
  26. self.stat_date = (datetime.date.today() + datetime.timedelta(0)).strftime("%Y%m%d")
  27. def handler(self, keys, values):
  28. sql = """
  29. select concat(t1.item_id, '-') as '商品id',
  30. ifnull(t2.user_name, '-') as '招商',
  31. ifnull(t2.create_time, '-') as '商品绑定时间',
  32. ifnull(t2.item_title, '-') as '商品名称',
  33. (case
  34. when t2.activity_item_status = 1
  35. then '待审核'
  36. when t2.activity_item_status = 2
  37. then '审核通过'
  38. when t2.activity_item_status = 3
  39. then '审核拒绝'
  40. when t2.activity_item_status = 4
  41. then '商品失效'
  42. when t2.activity_item_status = 5
  43. then '活动失效'
  44. else ''
  45. end
  46. ) as '商品状态',
  47. t1.*
  48. from (select item_id,
  49. ifnull(count(oid), 0) as '出单数',
  50. ifnull(sum(case when order_status != 80 then 1 else 0 end), 0) as '有效出单数',
  51. ifnull(round(sum(case when order_status != 80 then 1 else 0 end) / count(oid) * 100,
  52. 2), 0) as '有效率',
  53. ifnull(round(sum(case when order_status != 80 then pay_amount else 0 end) / 100, 2), 0) as 'Gvm',
  54. ifnull(round(sum(case
  55. when order_status != 80 then regimental_promotion_amount
  56. else 0 end) / 100,
  57. 2), 0) as '预估服务费',
  58. ifnull(sum(total_regimental_settle_amount),0) as '结算服务费'
  59. from {database}.kuaishou_supply_chain
  60. where stat_date >= date_format(date_sub(NOW(), INTERVAL 1 MONTH), '%%Y%%m01')
  61. and stat_date <= date_format(date_sub(now(), interval 1 day), '%%Y%%m%%d')
  62. and item_id in (select item_id
  63. from {database}.kuaishou_item_list
  64. where create_time >= date_format(date_sub(NOW(), INTERVAL 1 MONTH), '%%Y-%%m-01')
  65. and create_time < date_format(now(), '%%Y-%%m-01'))
  66. group by item_id) t1
  67. left join {database}.kuaishou_item_list t2
  68. on t1.item_id = t2.item_id
  69. """.format(database=keys)
  70. print(sql)
  71. path = """/data/excel/激励结算-{database}-{stat_date}.xls""".format(database=values, stat_date=self.stat_date)
  72. print(path)
  73. pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')
  74. my_sender = 'renyupeng@c-top.com.cn' # 发件人邮箱账号
  75. my_pass = 'fcyJKkHUyPo6Zztw' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  76. recipients = ['niuxiaoyu@c-top.com.cn', 'lishiwei@c-top.com.cn'] # 收件人邮箱账号
  77. # content = "Please check the attachment. "
  78. msg = MIMEMultipart()
  79. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  80. msg['From'] = my_sender
  81. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  82. msg['To'] = ",".join(recipients)
  83. # 邮件的主题,也就是邮件的标题
  84. msg['Subject'] = "激励结算-{}".format(values)
  85. att1 = MIMEText(
  86. open(path, 'rb').read(), 'base64', 'utf-8')
  87. att1["Content-Type"] = 'application/octet-stream'
  88. filename = '{key}-{stat_date}.xlsx'.format(key=keys, stat_date=self.stat_date)
  89. encoded_filename = urllib.parse.quote(filename)
  90. att1["Content-Disposition"] = 'attachment; filename="{encoded_filename}"'.format(
  91. encoded_filename=encoded_filename)
  92. msg.attach(att1)
  93. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  94. server.login(my_sender, my_pass)
  95. try:
  96. server.sendmail(my_sender, recipients, msg.as_string())
  97. print("发送成功")
  98. except Exception as e:
  99. print("发送失败", e)
  100. if __name__ == '__main__':
  101. database_mapping = {'yufu': '渔夫', 'miaogousi': '食品', 'ruixuan': '服装', 'rocket': '年轻', 'yuxiu': '玉秀'}
  102. for key, value in database_mapping.items():
  103. CanalEmail().handler(key, value)