jianye_report.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. from mysql_db import MysqlDB
  2. from sql import Sql
  3. from report_public_funs_utils import ReportPublicFunsUtils as rpfu
  4. from mail_content_text import MailContentText
  5. from email_util import EmailUtil
  6. from file_util import FileUtil
  7. from report_file_utils import ReportFileUtils
  8. class JianYeReport(object):
  9. """
  10. 建业报表数据处理
  11. """
  12. # customer_mails = ['liutt@elab-plus.com', 'binrenzhang@qq.com', 'plf@centralchina.com']
  13. customer_mails = ['liutt@elab-plus.com', 'binrenzhang@qq.com']
  14. #
  15. index_type = [
  16. 'fenxianghuodian',
  17. 'liebianhuodian',
  18. 'saomadaofang',
  19. 'quanminjingjiren',
  20. 'laoyezhu',
  21. 'baobeichenggong',
  22. 'baobeidaofang'
  23. ]
  24. head_1 = ['城市', '项目名称', '浏览量', '浏览人数', '新增用户', '新增获电', '推荐用户', '分享获电', '裂变获电',
  25. '扫码到访数', '全民经纪人注册数', '老业主注册数', '报备成功数', '报备到访数']
  26. brand_id = '13'
  27. sheet_names_1 = ['当日数据', '当月数据', '上线以来所有数据']
  28. def __init__(self):
  29. #self.db = MysqlDB('bi_report')
  30. pass
  31. def get_city_house_id(self):
  32. return self.db.select(Sql.sql_1)
  33. def get_report_customers(self, task_key):
  34. return self.db.select(Sql.sql_2, [task_key])
  35. def get_mail_title(self, type, region_name, name):
  36. """
  37. 获取邮件名称
  38. :param type:1:项目,2:集团,3:区域
  39. :param name: 项目 区域名称
  40. :return:
  41. """
  42. month_day = rpfu.get_montho_day()
  43. if type == 1:
  44. return '[{}]{}数据报表_{}'.format(month_day, region_name, name)
  45. elif type == 2:
  46. return '[{}]建业云集团数据报表_{}'.format(month_day, name)
  47. elif type == 3:
  48. return '[{}]建业云{}数据报表_{}'.format(month_day, region_name, name)
  49. def get_mail_content(self, customer_type):
  50. """
  51. 根据客户类型获取邮件正文
  52. :param customer_type:
  53. :return:
  54. """
  55. if customer_type == 2:
  56. return MailContentText.text_1
  57. else:
  58. return '本期数据报告已经准备完成,请点击附件查阅.'
  59. # 项目级别的统计
  60. def house_data_detail(self, time_range):
  61. result = self.db.select(Sql.sql_8, [time_range[0], time_range[1]])
  62. return result
  63. def user_data_volume_statistics(self, time_range, house_ids):
  64. """
  65. 用户浏览量,人数,新增获客,新增获点数据统计
  66. :param time_range:
  67. :param house_ids:
  68. :return:
  69. """
  70. # 1:总浏览量
  71. result = []
  72. data_1_1 = self.db.select(Sql.sql_3, [time_range[0], time_range[1], house_ids])
  73. number_1_1 = data_1_1[0][0]
  74. data_1_2 = self.db.select(Sql.sql_4, [time_range[0], time_range[1], house_ids])
  75. number_1_2 = data_1_2[0][0]
  76. number_1 = rpfu.add(number_1_1, number_1_2)
  77. result.append(number_1)
  78. # 2: 总浏览人数
  79. data_2 = self.db.select(Sql.sql_5,
  80. [time_range[0], time_range[1], house_ids, time_range[0], time_range[1], house_ids])
  81. number_2 = data_2[0][0]
  82. result.append(number_2)
  83. # 3:新增获客
  84. time_1 = time_range[0] + ' 00:00:00'
  85. time_2 = time_range[1] + ' 23:59:59'
  86. data_3 = self.db.select(Sql.sql_6, [time_1, time_2, house_ids, time_1, time_2, house_ids])
  87. number_3 = data_3[0][0]
  88. result.append(number_3)
  89. # 4:新增获电
  90. data_4 = self.db.select(Sql.sql_7, [time_1, time_2, house_ids, house_ids, time_1, time_2])
  91. number_4 = data_4[0][0]
  92. result.append(number_4)
  93. return result
  94. def region_house_id(self):
  95. return self.db.select(Sql.sql_11)
  96. def get_recommend_data(self, time_range):
  97. return self.db.select(Sql.sql_9, [time_range[0], time_range[1]])
  98. def get_house_id_by_brand_id(self, brand_id):
  99. result = []
  100. for x in self.get_city_house_id():
  101. if str(x[0]) == str(brand_id) and x[1] is not None and len(x[1]) > 4:
  102. result.append(x[1])
  103. return result
  104. def brand_data_of_time(self, time_range):
  105. result = []
  106. ids = self.get_house_id_by_brand_id(self.brand_id)
  107. data_1 = self.user_data_volume_statistics(time_range, ids)
  108. data_2 = []
  109. number_2 = 0
  110. for x in self.get_recommend_data(time_range):
  111. if str(x[0]) == str(self.brand_id):
  112. number_2 = x[1]
  113. data_2.append(number_2)
  114. data_3 = []
  115. for key in self.index_type:
  116. number = 0
  117. for x in self.db.select(Sql.sql_10, [time_range[0], time_range[1]]):
  118. if str(x[0]) == str(key):
  119. number = x[1]
  120. data_3.append(number)
  121. result.extend(data_1)
  122. result.extend(data_2)
  123. result.extend(data_3)
  124. return result
  125. def brand_data(self):
  126. """
  127. 集团数据总览, 表一。
  128. :return:
  129. """
  130. result = []
  131. time_rang_1 = rpfu.get_prd_day()
  132. time_rang_2 = rpfu.get_time_range_month()
  133. time_range_3 = rpfu.get_all_time_data_range()
  134. result.extend(self.brand_data_of_time(time_rang_1))
  135. result.extend(self.brand_data_of_time(time_rang_2))
  136. result.extend(self.brand_data_of_time(time_range_3))
  137. return result
  138. def house_data_of_time(self, time_range, house_ids):
  139. # brand_id, house_id, house_name, city
  140. city_info = self.get_city_house_id()
  141. data_1 = []
  142. for id in house_ids:
  143. sub = [id]
  144. sub.extend(self.user_data_volume_statistics(time_range, [id]))
  145. data_1.append(sub)
  146. data_2 = []
  147. for id in house_ids:
  148. sub = [id]
  149. number = 0
  150. for x in self.get_recommend_data(time_range):
  151. if str(id) == str(x[0]):
  152. number = x[1]
  153. sub.append(number)
  154. data_2.append(sub)
  155. data_3 = []
  156. for house_id in house_ids:
  157. sub = []
  158. for x in self.house_data_detail(time_range):
  159. # house_id, type, COUNT(DISTINCT customer_mobile)
  160. if str(house_id) == str(x[0]):
  161. sub.append(x)
  162. house_data = [house_id]
  163. for key in self.index_type:
  164. number = 0
  165. for x in sub:
  166. if str(key) == str(x[1]):
  167. number = x[2]
  168. house_data.append(number)
  169. data_3.append(house_data)
  170. result = []
  171. for id in house_ids:
  172. sub = []
  173. for x in city_info:
  174. if str(id) == str(x[1]):
  175. sub.extend([x[3], x[2]])
  176. for x in data_1:
  177. if str(id) == str(x[0]):
  178. sub.extend(x[1:])
  179. for x in data_2:
  180. if str(id) == str(x[0]):
  181. sub.extend(x[1:])
  182. for x in data_3:
  183. if str(id) == str(x[0]):
  184. sub.extend(x[1:])
  185. result.append(sub)
  186. result.sort(key=lambda obj: obj[2], reverse=True)
  187. return result
  188. def house_data(self, house_ids):
  189. time_range_1 = rpfu.get_prd_day()
  190. time_range_2 = rpfu.get_time_range_month()
  191. time_range_3 = rpfu.get_all_time_data_range()
  192. result = [self.house_data_of_time(time_range_1, house_ids), self.house_data_of_time(time_range_2, house_ids),
  193. self.house_data_of_time(time_range_3, house_ids)]
  194. return result
  195. def send_mail_to_customer(self, task_key):
  196. """
  197. 统计数据推送给客户, 表二
  198. :param: task_key
  199. :return:
  200. """
  201. # 邮件发送参数
  202. # mail_title,
  203. # content,
  204. # receiver,
  205. # mail_excel,
  206. # file_name,
  207. # mail_excel_1=None,
  208. # file_name_1=None
  209. message = {}
  210. send_mail_info = []
  211. mail_util = EmailUtil()
  212. rfu = ReportFileUtils()
  213. customers = self.get_report_customers(task_key)
  214. message[0] = '客户信息获取成功'
  215. send_mail_log = []
  216. brand_table_one = None
  217. brand_table_two = None
  218. try:
  219. save_path = FileUtil().save_path_create()
  220. send_data = []
  221. # 查询数据
  222. if task_key == 12:
  223. # 集团信息计算一次发送给多个人
  224. brand_table_one = self.brand_data()
  225. house_ids = self.get_house_id_by_brand_id('13')
  226. brand_table_two = self.house_data(house_ids)
  227. message['集团数据查询'] = 'success'
  228. # 总浏览量:xx ,总浏览人数:xx
  229. #
  230. # 新增获客: xx, 新增获电:xx
  231. #
  232. # 推荐用户数:xx,分享获电:xx,裂变获电:xx
  233. #
  234. # 全民经纪人注册数:xx
  235. #
  236. # 报备成功数:xx,报备到访数:xx
  237. for customer in customers:
  238. # 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
  239. name = customer[2]
  240. customer_type = customer[1]
  241. mail = customer[3]
  242. ids = customer[6]
  243. region_name = customer[4]
  244. title = self.get_mail_title(customer_type, region_name, name)
  245. content = self.get_mail_content(customer_type)
  246. file_path = save_path + '/' + title + '.xls'
  247. try:
  248. if customer_type == 2:
  249. # 集团
  250. content = self.get_brand_content(content, brand_table_one)
  251. 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])
  252. # rfu.create_excel_file(table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], file_path)
  253. elif customer_type == 1:
  254. # 项目
  255. table_2 = self.house_data(self.get_house_ids(ids))
  256. send_data.append(
  257. [table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], file_path,
  258. content, name, mail])
  259. # rfu.create_excel_file(table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], file_path)
  260. elif customer_type == 3:
  261. # 区域
  262. table_2 = self.house_data(self.get_house_ids(ids))
  263. send_data.append(
  264. [table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], file_path,
  265. content, name, mail])
  266. # rfu.create_excel_file(table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], file_path)
  267. else:
  268. pass
  269. except Exception as e:
  270. print(str(e))
  271. # # name, mail, report_name, push_time, send_status, status, error_message
  272. send_mail_log.append([name, mail, title, -1, '数据查询失败:{}'.format(str(e))])
  273. message['query_data'] = 'success'
  274. # 生成文件
  275. for data in send_data:
  276. try:
  277. rfu.create_excel_file(data[0], data[1], data[2], data[3], data[4])
  278. # [title, content, mail, file_path, title + '.xlsx', name]
  279. send_mail_info.append([data[2], data[5], data[7], data[4], data[2] + '.xls', data[6]])
  280. except Exception as e:
  281. print(e)
  282. send_mail_log.append([data[6], data[7], data[2], -1, '文件创建失败:{}'.format(str(e))])
  283. message['file'] = 'success'
  284. # 发送邮件
  285. for mail in send_mail_info:
  286. try:
  287. for m in self.customer_mails:
  288. result = mail_util.send_mail_by_admin(mail[0], mail[1], m, mail[3], mail[4])
  289. if result:
  290. # name, mail, report_name, push_time, send_status, status, error_message
  291. send_mail_log.append([mail[5], mail[2], mail[3], 1, 'success'])
  292. else:
  293. send_mail_log.append([mail[5], mail[2], mail[3], -1, 'fail'])
  294. except Exception as e:
  295. print(str(e))
  296. message['error1'] = str(e)
  297. send_mail_log.append([mail[5], mail[2], mail[3], -1, str(e)])
  298. message['mail'] = 'success'
  299. # 写入日志
  300. self.db.add_some(Sql.sql_12, send_mail_log)
  301. message['log'] = 'success'
  302. except Exception as e:
  303. message['error'] = str(e)
  304. finally:
  305. return message
  306. def get_brand_content(self, content, brand_table_one):
  307. content = content.format(rpfu.get_montho_day(),
  308. brand_table_one[0], brand_table_one[1], brand_table_one[2], brand_table_one[3],
  309. brand_table_one[4], brand_table_one[5],
  310. brand_table_one[6], brand_table_one[8], brand_table_one[10], brand_table_one[11], rpfu.get_month(),
  311. brand_table_one[12], brand_table_one[13], brand_table_one[14], brand_table_one[15],
  312. brand_table_one[16],
  313. brand_table_one[17],
  314. brand_table_one[18], brand_table_one[20], brand_table_one[22], brand_table_one[23],
  315. brand_table_one[24], brand_table_one[25], brand_table_one[26], brand_table_one[27],
  316. brand_table_one[28],
  317. brand_table_one[29],
  318. brand_table_one[30], brand_table_one[32], brand_table_one[34], brand_table_one[35]
  319. )
  320. return content
  321. def get_house_ids(self, ids_str):
  322. if str(ids_str).find(',') == -1:
  323. return [ids_str]
  324. else:
  325. return [x for x in str(ids_str).split(',')]
  326. if __name__ == '__main__':
  327. pass