瀏覽代碼

增加发送指定客户邮件的接口

Signed-off-by: binren <zhangbr@elab-plus.com>
binren 4 年之前
父節點
當前提交
95cf8673d6
共有 4 個文件被更改,包括 116 次插入3 次删除
  1. 16 0
      flask_app.py
  2. 87 0
      jianye_report.py
  3. 1 1
      report_public_funs_utils.py
  4. 12 2
      sql.py

+ 16 - 0
flask_app.py

@@ -265,6 +265,22 @@ def debug_func():
         return json.dumps(result, ensure_ascii=False, cls=DecimalEncoder)
 
 
+@app.route('/send_mail_to_customer', methods=['GET', 'POT'])
+def send_mail_to_customer():
+    result = {}
+    rj = JianYeReport()
+    try:
+        customer_id = request.args.get('id', default=0, type=int)
+        mail = request.args.get('mail', default=0, type=int)
+        data = rj.send_mail_for_customer_id(customer_id, mail)
+        result['data'] = data
+        pass
+    except Exception as e:
+        result['error'] = str(e)
+    finally:
+        return json.dumps(result, ensure_ascii=False, cls=DecimalEncoder)
+
+
 class DecimalEncoder(json.JSONEncoder):
 
     def default(self, o):

+ 87 - 0
jianye_report.py

@@ -392,6 +392,93 @@ class JianYeReport(object):
                                  )
         return content
 
+    def get_customer_info_by_id(self, customer_id):
+        return self.db.select(Sql.sql_14, [customer_id])
+
+    def send_mail_for_customer_id(self, customer_id, new_mail=None):
+        """
+            发送指客户的邮件
+        :param task_key:
+        :return:
+        """
+        message = {}
+        customer = self.get_customer_info_by_id(customer_id)
+        message['customer'] = str(customer_id)
+        send_data = []
+        if customer:
+            try:
+                save_path = FileUtil().save_path_create()
+                name = customer[1]
+                mail = customer[2]
+                customer_type = customer[3]
+                ids = self.get_house_ids(customer[5])
+                region_name = customer[4]
+                title = self.get_mail_title(customer_type, region_name, name)
+                content = self.get_mail_content(customer_type)
+                file_path = save_path + '/' + title + '.xls'
+                if customer_type == 1:
+                    # 项目
+                    table_2 = self.house_data(self.get_house_ids(ids))
+                    send_data.append(
+                        [table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], file_path,
+                         content, name, mail])
+                elif customer_type == 2:
+                    # 集团
+                    brand_table_one = self.brand_data()
+                    house_ids = self.get_house_id_by_brand_id('13')
+                    brand_table_two = self.house_data(house_ids)
+                    content = self.get_brand_content(content, brand_table_one)
+                    send_data.append(
+                        [brand_table_two, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1],
+                         file_path, content, name, mail])
+                elif customer_type == 3:
+                    # 区域
+                    table_2 = self.house_data(self.get_house_ids(ids))
+                    message['data'] = table_2
+                    send_data.append(
+                        [table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], file_path,
+                         content, name, mail])
+                else:
+                    message['customer_type'] = '客户类型错误'
+            except Exception as e:
+                message['数据查询失败'] = str(e)
+        else:
+            message['error'] = '客户id错误:{}'.format(customer_id)
+
+        message['query_data'] = 'success'
+        # 生成文件
+        rfu = ReportFileUtils()
+        send_mail_log = []
+        send_mail_info = []
+        for data in send_data:
+            try:
+                rfu.create_excel_file(data[0], data[1], data[2], data[3], data[4])
+                send_mail_info.append([data[2], data[5], data[7], data[4], data[2] + '.xls', data[6]])
+            except Exception as e:
+                print(e)
+                send_mail_log.append([data[6], data[7], data[2], -1, '文件创建失败:{}'.format(str(e))])
+        message['excel文件创建失败'] = 'success'
+        # 发送邮件
+        mail_util = EmailUtil()
+        for mail in send_mail_info:
+            try:
+                if new_mail:
+                    result = mail_util.send_mail_by_admin(mail[0], mail[1], new_mail, mail[3], mail[4])
+                else:
+                    result = mail_util.send_mail_by_admin(mail[0], mail[1], mail[7], mail[3], mail[4])
+                if result:
+                    # name, mail, report_name, push_time, send_status, status, error_message
+                    send_mail_log.append([mail[5], mail[2], mail[3], 1, 'success'])
+                else:
+                    send_mail_log.append([mail[5], mail[2], mail[3], -1, 'fail'])
+            except Exception as e:
+                print(str(e))
+                message['邮件发送失败'] = str(e)
+                send_mail_log.append([mail[5], mail[2], mail[3], -1, '邮件发送失败:{}'.format(str(e))])
+        # 写入日志
+        self.db.add_some(Sql.sql_12, send_mail_log)
+        pass
+
     def get_house_ids(self, ids_str):
         if str(ids_str).find(',') == -1:
             return [ids_str]

+ 1 - 1
report_public_funs_utils.py

@@ -6,7 +6,7 @@ class ReportPublicFunsUtils:
     def get_time_range_month(type=None):
         """
             获取当月一号到当前时间的时间区间
-        :param type 1:y m d 2: y m d : s m m
+        :param type 1 :y m d 2: y m d : s m m
         :return:
         """
         now_time = datetime.datetime.now()

+ 12 - 2
sql.py

@@ -152,9 +152,11 @@ class Sql:
     sql_9 = """
         SELECT house_id, visit_new_uv_dtd FROM a_brand_customer_share_dtd where brand_id ='13' and report_d >= %s and report_d <= %s  order by report_d
     """
+
     sql_10 = """
     select type, COUNT(DISTINCT customer_mobile) from f_dm_jianye_allagent_day where report_d >= %s and report_d <= %s group by type
     """
+
     sql_11 = """"
     select a.house_or_region, min(b.house_or_brand_id), sum(a.name) from report_push_customer_info a left join report_customer_authority_info b on a.id = b.customer_id group by house_or_region
     """
@@ -162,7 +164,6 @@ class Sql:
     sql_12 = """
         insert into report_push_log(name, mail, report_name, push_time, send_status, status, error_message) values(%s, %s, %s, now(), %s, 1, %s)
     """
-    pass
 
     sql_13 = """
             SELECT
@@ -254,5 +255,14 @@ class Sql:
     """
 
 
+    sql_14 = """
+        select a.id , a.`name`, a.mail, a.customer_type, a.house_or_region, GROUP_CONCAT(b.house_or_brand_id) 
+        from report_push_customer_info a 
+        left join report_customer_authority_info b on a.id = b.customer_id 
+        where a.id = %s and a.status = 1 and b.status = 1
+        group by a.id , a.`name`, a.mail, a.customer_type, a.house_or_region
+    """
+
+
 if __name__ == '__main__':
-    print(len('222'))
+    print(len('222'))