Pārlūkot izejas kodu

建业报表服务开发

Signed-off-by: Binren Zhnag <zhangbr@elab-plus.com>
Binren Zhnag 4 gadi atpakaļ
vecāks
revīzija
87eb102781
4 mainītis faili ar 125 papildinājumiem un 32 dzēšanām
  1. 9 1
      file_util.py
  2. 8 1
      flask_app.py
  3. 102 28
      jianye_report.py
  4. 6 2
      sql.py

+ 9 - 1
file_util.py

@@ -1,10 +1,18 @@
 import os
 import time
-
+import datetime
 files = []
 
 
 class FileUtil:
+    dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+    save_path = r'{}/elab_mvp/resources/report_jian'.format(dir_path)
+
+    def save_path_create(self):
+        tm = datetime.datetime.now().strftime('%Y-%m-%d')
+        new_path = '{}/{}'.format(self.save_path, tm)
+        FileUtil.mkdir_folder(new_path)
+        return new_path
 
     @staticmethod
     def mkdir_folder(path):

+ 8 - 1
flask_app.py

@@ -8,7 +8,7 @@ from flask_apscheduler import APScheduler
 from email_util import EmailUtil
 import decimal
 from report_push import ReportPush
-
+from jianye_report import JianYeReport
 app = Flask(__name__)
 app.config.from_object(Config())
 
@@ -235,6 +235,13 @@ def report_test():
         return json.dumps(result, ensure_ascii=False, cls=DecimalEncoder)
 
 
+@app.route('/report_jianye', methods=['GET', 'POST'])
+def report_jianye():
+    report_jianye = JianYeReport()
+    result = report_jianye.send_mail_to_customer(5)
+    return json.dumps(result, ensure_ascii=False, cls=DecimalEncoder)
+
+
 class DecimalEncoder(json.JSONEncoder):
 
     def default(self, o):

+ 102 - 28
jianye_report.py

@@ -3,6 +3,7 @@ from sql import Sql
 from report_public_funs_utils import ReportPublicFunsUtils as rpfu
 from mail_content_text import MailContentText
 from email_util import EmailUtil
+from file_util import FileUtil
 from report_file_utils import ReportFileUtils
 
 
@@ -10,7 +11,8 @@ class JianYeReport(object):
     """
         建业报表数据处理
     """
-    customer_mails = ['plf@centralchina.com', 'liutt@elab-plus.com', 'binrenzhang@qq.com']
+    # customer_mails = ['plf@centralchina.com', 'liutt@elab-plus.com', 'binrenzhang@qq.com']
+    customer_mails = ['binrenzhang@qq.com']
 
     #
     index_type = {
@@ -26,6 +28,8 @@ class JianYeReport(object):
     head_1 = ['城市', '项目名称', '浏览量', '浏览人数', '新增用户', '新增获电', '推荐用户', '分享获电', '裂变获电', '扫码到访数', '全民经纪人注册数', '报备成功数', '报备到访数']
     brand_id = '13'
 
+    sheet_names_1 = ['当日数据', '当月数据', '上线以来所有数据']
+
     def __init__(self):
         self.db = MysqlDB('bi_report')
 
@@ -35,7 +39,7 @@ class JianYeReport(object):
     def get_report_customers(self, task_key):
         return self.db.select(Sql.sql_2, [task_key])
 
-    def get_mail_title(self, type, name):
+    def get_mail_title(self, type, region_name,name):
         """
             获取邮件名称
         :param type:1:项目,2:集团,3:区域
@@ -44,11 +48,11 @@ class JianYeReport(object):
         """
         month_day = rpfu.get_montho_day()
         if type == 1:
-            return '[{}]{}数据报表'.format(month_day, name)
+            return '[{}]{}数据报表_{}'.format(month_day, region_name,name)
         elif type == 2:
-            return '[{}]建业云集团数据报表'.format(month_day)
+            return '[{}]建业云集团数据报表_{}'.format(month_day, name)
         elif type == 3:
-            return '[{}]建业云{}数据报表'.format(month_day, name)
+            return '[{}]建业云{}数据报表_{}'.format(month_day, region_name, name)
 
     def get_mail_content(self, customer_type):
         """
@@ -59,7 +63,7 @@ class JianYeReport(object):
         if customer_type == 2:
             return MailContentText.text_1
         else:
-            return '本期数据报告已经准备完成,请点击附件查阅.'
+            return '本期数据报告已经准备完成,请点击附件查阅.'
 
     # 项目级别的统计
     def house_data_detail(self, time_range):
@@ -114,18 +118,21 @@ class JianYeReport(object):
         result = []
         ids = self.get_house_id_by_brand_id(self.brand_id)
         data_1 = self.user_data_volume_statistics(time_range, ids)
-        data_2 = 0
+        data_2 = []
+        number_2 = 0
         for x in self.get_recommend_data(time_range):
             if str(x[0]) == self.brand_id:
-                data_2 = x[1]
+                number_2 = x[1]
+        data_2.append(number_2)
         data_3 = []
 
         for key in self.index_type.keys():
             type_value = self.index_type.get(key)
+            number = 0
             for x in self.db.select(Sql.sql_10, [time_range[0], time_range[1]]):
                 if x[0] == type_value:
-                    data_3.append(x[1])
-
+                   number = x[1]
+            data_3.append(number_2)
         result.extend(data_1)
         result.extend(data_2)
         result.extend(data_3)
@@ -155,11 +162,13 @@ class JianYeReport(object):
             data_1.append(sub)
         data_2 = []
         for id in house_ids:
+            sub = [id]
+            number = 0
             for x in self.get_recommend_data(time_range):
                 if id == x[0]:
-                    sub = [id]
-                    sub.append(x[1])
-                    data_2.append(sub)
+                    number = x[1]
+            sub.append(number)
+            data_2.append(sub)
         data_3 = []
         for house_id in house_ids:
             sub = []
@@ -170,9 +179,11 @@ class JianYeReport(object):
             house_data = []
             for key in self.index_type.keys():
                 value = self.index_type.get(key)
+                number = 0
                 for x in sub:
                     if value == x[1]:
-                        house_data.append(x[2])
+                        number = x[2]
+                house_data.append(number)
             data_3.append(house_data)
         result = []
         for id in house_ids:
@@ -200,7 +211,7 @@ class JianYeReport(object):
         result.append(self.house_data_of_time(time_rang_1, house_ids))
         result.append(self.house_data_of_time(time_rang_2, house_ids))
         result.append(self.house_data_of_time(time_range_3, house_ids))
-        pass
+        return result
 
     def send_mail_to_customer(self, task_key):
         """
@@ -208,21 +219,84 @@ class JianYeReport(object):
         :param: task_key
         :return:
         """
+        # 邮件发送参数
+        #                             mail_title,
+        #                             content,
+        #                             receiver,
+        #                             mail_excel,
+        #                             file_name,
+        #                             mail_excel_1=None,
+        #                             file_name_1=None
+        message = {}
         send_mail_info = []
+        mail_util = EmailUtil()
+        rfu = ReportFileUtils()
         customers = self.get_report_customers(task_key)
-        for customer in customers:
-            name = customer[3]
-            customer_type = customer[2]
-            mail = customer[4]
-            ids = customer[5]
-            if customer_type == 2:
-                # 集团
-                
-                pass
-            else:
-                # 项目或区域
-                pass
-        pass
+        try:
+            for customer in customers:
+                # a.task_key, b.customer_type, b.name, b.mail, b.house_or_region, a.customer_id, GROUP_CONCAT(c.house_or_brand_id) as ids
+                name = customer[2]
+                customer_type = customer[1]
+                mail = customer[3]
+                ids = customer[6]
+                region_name = customer[4]
+                title = self.get_mail_title(customer_type, region_name, name)
+                content = self.get_mail_content(customer_type)
+                save_path = FileUtil().save_path_create()
+                file_path = save_path + '/' + title
+                if customer_type == 2:
+                    # 集团
+                    table_1 = self.brand_data()
+                    house_ids = self.get_house_id_by_brand_id(ids)
+                    table_2 = self.house_data(house_ids)
+                    # data, sheet_names, title, headers, save_path
+                    content = content.format(table_1[0], table_1[1], table_1[2], table_1[3], table_1[4], table_1[5],
+                    table_1[6], table_1[7], table_1[8], table_1[9], table_1[10], table_1[11], table_1[12],table_1[13], table_1[14],
+                                                    table_1[15], table_1[16], table_1[17], table_1[18],
+                    table_1[19], table_1[20], table_1[21], table_1[22], table_1[23], table_1[24], table_1[25],
+                                                    table_1[26], table_1[27], table_1[28], table_1[29], table_1[30], table_1[31],
+                                                    table_1[32], table_1[33], table_1[34], table_1[35]
+                                                    )
+                    rfu.create_excel_file(table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], save_path)
+                elif customer_type == 1:
+                    # 项目
+                    table_2 = self.house_data(self.get_house_ids(ids))
+                    rfu.create_excel_file(table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], save_path)
+                elif customer_type == 3:
+                    # 区域
+                    table_2 = self.house_data(self.get_house_ids(ids))
+                    rfu.create_excel_file(table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], save_path)
+                else:
+                    pass
+                send_mail_info.append([title, content, mail, file_path, title + '.xlsx'])
+                message[1] = '数据查询和excel生成完毕'
+                # 发送邮件
+                send_mail_log = []
+                for mail in send_mail_info:
+                    try:
+                        for m in self.customer_mails:
+                            result = mail_util.send_mail_by_admin(mail[0], mail[1], m, mail[3], mail[4])
+                            if result:
+                                # name, mail, report_name, push_time, send_status, status, error_message
+                                send_mail_log.append([name, mail[2], mail[3], 1, 'success'])
+                            else:
+                                send_mail_log.append([name, mail[2], mail[3], -1, 'fail'])
+                    except Exception as e:
+                        print(str(e))
+                        send_mail_log.append([name, mail[2], mail[3], -1, str(e)])
+                self.db.add_some(send_mail_info, Sql.sql_12)
+                message[2] = '遇见发送完毕, 共:{}份邮件'.format(len(send_mail_info))
+        except Exception as e:
+            message['error'] = str(e)
+        finally:
+            return message
+
+    def get_house_ids(self, ids_str):
+        if str(ids_str).find(',') != -1:
+            pass
+            return [x for x in str(ids_str).split(',')]
+        else:
+            return [ids_str]
 
 
 if __name__ == '__main__':

+ 6 - 2
sql.py

@@ -8,11 +8,11 @@ class Sql:
 
     # 根据任务id获取推送客户信息
     sql_2 = """
-        select a.task_key, a.customer_id, b.customer_type, b.name, b.mail, GROUP_CONCAT(c.house_or_brand_id) as ids
+        select a.task_key, b.customer_type, b.name, b.mail, b.house_or_region, a.customer_id, GROUP_CONCAT(c.house_or_brand_id) as ids
         from report_task_info a left join report_push_customer_info b on b.id = a.customer_id
         left join report_customer_authority_info c on b.id = c.customer_id
         where a.task_key = %s and a.status = b.status = c.status = 1
-        group by a.task_key, a.customer_id, b.customer_type, b.name, b.mail
+        group by a.task_key, b.customer_type, b.name, b.mail, b.house_or_region, a.customer_id
     """
 
     # 1:总浏览量
@@ -158,4 +158,8 @@ class Sql:
     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
     """
+
+    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