jianye_report.py 15 KB

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