# Author renyupeng # coding=utf-8 # @Time : 2024/9/26 16:08 # @Site : # @File : CanalEmail.py # @Software: PyCharm # @contact: renyupeng@c-top.com.cn # @Tel 1501435553 import datetime import urllib from urllib import parse import pandas as pd import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from sqlalchemy import create_engine class CanalEmail: def __init__(self): self.OuterStr = "mysql+pymysql://%s:%s@%s:%d/%s" % ( "data", parse.quote_plus("hcst@2021"), "139.186.27.96", 3390, "application") self.OuterConn = create_engine(self.OuterStr, pool_size=10, max_overflow=5, pool_timeout=10, pool_recycle=3600) self.stat_date = (datetime.date.today() + datetime.timedelta(0)).strftime("%Y%m%d") def handler(self, keys, values): sql = """ select t5.reg_name as `团名称`, concat(t1.item_id, '-') as '商品id', ifnull(t2.user_name, '-') as '招商', ifnull(t2.create_time, '-') as '商品绑定时间', ifnull(t2.item_title, '-') as '商品名称', (case when t2.activity_item_status = 1 then '待审核' when t2.activity_item_status = 2 then '审核通过' when t2.activity_item_status = 3 then '审核拒绝' when t2.activity_item_status = 4 then '商品失效' when t2.activity_item_status = 5 then '活动失效' else '' end ) as '商品状态', t1.* from (select regimental_id, item_id, ifnull(count(oid), 0) as '出单数', ifnull(sum(case when order_status != 80 then 1 else 0 end), 0) as '有效出单数', ifnull(round(sum(case when order_status != 80 then 1 else 0 end) / count(oid) * 100, 2), 0) as '有效率', ifnull(round(sum(case when order_status != 80 then pay_amount else 0 end) / 100, 2), 0) as 'Gvm', ifnull(round(sum(case when order_status != 80 then regimental_promotion_amount else 0 end) / 100, 2), 0) as '预估服务费', ifnull(sum(total_regimental_settle_amount),0) as '结算服务费' from {database}.kuaishou_supply_chain where stat_date >= date_format(date_sub(NOW(), INTERVAL 1 MONTH), '%%Y%%m01') and stat_date <= date_format(date_sub(now(), interval 1 day), '%%Y%%m%%d') and item_id in (select item_id from {database}.kuaishou_item_list where create_time >= date_format(date_sub(NOW(), INTERVAL 1 MONTH), '%%Y-%%m-01') and create_time < date_format(now(), '%%Y-%%m-01')) group by item_id) t1 left join {database}.kuaishou_item_list t2 on t1.item_id = t2.item_id left join (select id,reg_name from {database}.kuaishou_supply_chain_cookie union all select id,reg_name from miaogousi.kuaishou_supply_chain_cookie union all select id,reg_name from yufu.kuaishou_supply_chain_cookie union all select id,reg_name from rocket.kuaishou_supply_chain_cookie union all select id,reg_name from zhishui.kuaishou_supply_chain_cookie union all select id,reg_name from yuxiu.kuaishou_supply_chain_cookie group by id, reg_name)t5 on t1.regimental_id=t5.id """.format(database=keys) print(sql) path = """/data/excel/激励结算-{database}-{stat_date}.xls""".format(database=values, stat_date=self.stat_date) print(path) pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl') my_sender = 'renyupeng@c-top.com.cn' # 发件人邮箱账号 my_pass = 'fcyJKkHUyPo6Zztw' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码 recipients = ['niuxiaoyu@c-top.com.cn', 'lishiwei@c-top.com.cn'] # 收件人邮箱账号 # content = "Please check the attachment. " msg = MIMEMultipart() # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便 msg['From'] = my_sender # [收件人邮箱昵称、收件人邮箱账号], 昵称随便 msg['To'] = ",".join(recipients) # 邮件的主题,也就是邮件的标题 msg['Subject'] = "激励结算-{}".format(values) att1 = MIMEText( open(path, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' filename = '{key}-{stat_date}.xlsx'.format(key=keys, stat_date=self.stat_date) encoded_filename = urllib.parse.quote(filename) att1["Content-Disposition"] = 'attachment; filename="{encoded_filename}"'.format( encoded_filename=encoded_filename) msg.attach(att1) server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465) server.login(my_sender, my_pass) try: server.sendmail(my_sender, recipients, msg.as_string()) print("发送成功") except Exception as e: print("发送失败", e) if __name__ == '__main__': database_mapping = {'yufu': '渔夫', 'miaogousi': '食品', 'ruixuan': '服装', 'rocket': '年轻', 'yuxiu': '玉秀','zhishui':'止水'} for key, value in database_mapping.items(): CanalEmail().handler(key, value)