| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 | 
							- # Author renyupeng
 
- # coding=utf-8
 
- # @Time    : 2024/9/26 16:07
 
- # @Site    : 
 
- # @File    : AttractInvestmentEmail.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 AttractInvestmentEmail:
 
-     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 *
 
- from (select t1.user_id                                                                             as 'userId',
 
-              t1.user_name                                                                           as '名称',
 
-              ifnull(count(distinct t2.item_id),'-')                                                             as '商品量',
 
-              ifnull(count(t2.oid) ,'-')                                                                         as '订单数',
 
-              ifnull(sum(case when t2.order_status != 80 then 1 else 0 end) ,'-')                                as '有效订单数',
 
-              ifnull(round(sum(case when t2.order_status != 80 then 1 else 0 end) / count(t2.oid), 4),'-')       as '有效率',
 
-              ifnull(sum(case when t2.send_status = 1 then 1 else 0 end),'-')                                    as '发货数',
 
-              ifnull(round(sum(case when t2.send_status = 1 and t2.order_status != 80 then 1 else 0 end) / sum(case
 
-                                                                                                            when
 
-                                                                                                                t2.order_status != 80
 
-                                                                                                                then 1
 
-                                                                                                            else 0 end),
 
-                    4),'-')                                                                               as '发货率',
 
-              ifnull(sum(case when t2.send_status = 1 and t2.order_status != 80 then 1 else 0 end),'-')          as '有效发货数',
 
-              ifnull(sum(case when t2.send_status = 0 and t2.order_status != 80 then 1 else 0 end),'-')          as '未发货数',
 
-              ifnull(round(sum(case when t2.send_status = 1 and t2.order_status != 80 then 1 else 0 end) / sum(case
 
-                                                                                                            when send_status = 1
 
-                                                                                                                then 1
 
-                                                                                                            else 0 end),
 
-                    4) ,'-')                                                                              as '有效发货率',
 
-              ifnull(sum(case when t2.order_status = 80 then 1 else 0 end),'-')                                  as '失效订单数',
 
-              ifnull(sum(case when t2.order_status != 80 then t2.pay_amount/100 else 0 end) ,'-')                    as 'GMV',
 
-              ifnull(sum(case
 
-                      when t2.order_status != 80 then t2.regimental_promotion_amount/100
 
-                      else 0 end),'-')                                                                    as '预估服务费',
 
-              ifnull(sum(case when t2.order_status != 80 then t2.total_regimental_settle_amount/100 else 0 end),'-') as
 
-                                                                                                        '结算服务费'
 
-       from {database}.kuaishou_item_bind_user t1
 
-                left join {database}.kuaishou_supply_chain t2
 
-                          on t1.item_id = t2.item_id
 
-       where 1 = 1
 
-         and t2.stat_date >= DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%%Y%%m01')
 
-         and t2.stat_date <= DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH), '%%Y%%m%%d')
 
-       group by t1.user_id) t
 
-          left join (select item_create_id, count(1) as 'sendSampleCount'
 
-                     from {database}.kuaishou_item_collect_samples
 
-                     where courier_number_time >=  str_to_date(concat(date_format( DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%%Y-%%m-01'), '%%Y-%%m-%%d'), '00:00:01'), '%%Y-%%m-%%d %%H:%%i:%%s')
 
-                       and courier_number_time <= str_to_date(concat(date_format(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH), '%%Y-%%m-%%d'), '%%Y-%%m-%%d'), '23:59:59'), '%%Y-%%m-%%d %%H:%%i:%%s')
 
-                     group by item_create_id) t1 on t.userId = t1.item_create_id
 
- where 1 = 1
 
- order by t.订单数 desc
 
-             """.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': '玉秀'}
 
-     for key, value in database_mapping.items():
 
-         AttractInvestmentEmail().handler(key, value)
 
 
  |