jianye_report.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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', 'lijm@elab-plus.com', 'xuanxc@elab-plus.com']
  14. customer_mails = ['binrenzhang@qq.com']
  15. #
  16. index_type = [
  17. 'fenxianghuodian',
  18. 'liebianhuodian',
  19. 'saomadaofang',
  20. 'quanminjingjiren',
  21. 'laoyezhu',
  22. 'baobeichenggong',
  23. 'baobeidaofang'
  24. ]
  25. head_1 = ['区域', '城市', '项目名称', '浏览量', '浏览人数', '新增用户', '新增获电', '推荐用户', '分享获电', '裂变获电',
  26. '扫码到访数', '全民经纪人注册数', '老业主注册数', '报备成功数', '报备到访数']
  27. brand_id = '13'
  28. head_2 = ['区域名称', '城市', '项目名称', '推荐人姓名', '推荐人手机号', '组织', '分享获电', '经纪人注册数', '报备成功数']
  29. sheet_names_1 = ['当日数据', '当月数据', '上线以来所有数据']
  30. def __init__(self):
  31. self.db = MysqlDB('bi_report')
  32. self.time_range_1 = rpfu.get_prd_day()
  33. self.time_range_2 = rpfu.get_time_range_month()
  34. self.time_range_3 = rpfu.get_all_time_data_range()
  35. self.pv_data_1 = self.pv_count(self.time_range_1)
  36. self.pv_data_2 = self.pv_count(self.time_range_2)
  37. self.pv_data_3 = self.pv_count(self.time_range_3)
  38. self.house_region = self.get_house_region_info()
  39. self.mail_util = EmailUtil()
  40. def get_house_region(self):
  41. return self.db.select(Sql.sql_15)
  42. def get_house_region_name(self, house_id):
  43. for x in self.house_region:
  44. if str(x[1]) in house_id:
  45. return x[3]
  46. return ' '
  47. def get_city_house_id(self):
  48. return self.db.select(Sql.sql_1)
  49. def get_report_customers(self, task_key):
  50. return self.db.select(Sql.sql_2, [task_key])
  51. def get_mail_title(self, type, region_name, name):
  52. """
  53. 获取邮件名称
  54. :param region_name:
  55. :param type:1:项目,2:集团,3:区域
  56. :param name: 项目 区域名称
  57. :return:
  58. """
  59. month_day = rpfu.get_montho_day()
  60. if type == 1:
  61. return ['[{}]{}数据报表_{}_项目明细'.format(month_day, region_name, name), '[{}]{}数据报表_{}_置业顾问完成情况'.format(month_day, region_name, name)]
  62. elif type == 2:
  63. return '[{}]建业云集团数据报表_{}'.format(month_day, name)
  64. elif type == 3:
  65. return '[{}]建业云{}数据报表_{}'.format(month_day, region_name, name)
  66. def get_mail_content(self, customer_type):
  67. """
  68. 根据客户类型获取邮件正文
  69. :param customer_type:
  70. :return:
  71. """
  72. if customer_type == 2:
  73. return MailContentText.text_1
  74. else:
  75. return '本期数据报告已经准备完成,请点击附件查阅.'
  76. # 项目级别的统计
  77. def house_data_detail(self, time_range):
  78. result = self.db.select(Sql.sql_8, [time_range[0], time_range[1]])
  79. return result
  80. def pv_count(self, time_range):
  81. """
  82. 新的用户行为统计
  83. :param time_range:
  84. :return:
  85. """
  86. sql_result = self.db.select(Sql.sql_13, [time_range[0], time_range[1]])
  87. return sql_result
  88. def user_data_volume_statistics(self, time_range, house_ids):
  89. """
  90. 用户浏览量,人数,新增获客,新增获点数据统计
  91. :param time_range:
  92. :param house_ids:
  93. :return:
  94. """
  95. # 1:总浏览量
  96. result = []
  97. data_1_1 = self.db.select(Sql.sql_3, [time_range[0], time_range[1], house_ids])
  98. number_1_1 = data_1_1[0][0]
  99. data_1_2 = self.db.select(Sql.sql_4, [time_range[0], time_range[1], house_ids])
  100. number_1_2 = data_1_2[0][0]
  101. number_1 = rpfu.add(number_1_1, number_1_2)
  102. result.append(number_1)
  103. # 2: 总浏览人数
  104. data_2 = self.db.select(Sql.sql_5,
  105. [time_range[0], time_range[1], house_ids, time_range[0], time_range[1], house_ids])
  106. number_2 = data_2[0][0]
  107. result.append(number_2)
  108. # 3:新增获客
  109. time_1 = time_range[0] + ' 00:00:00'
  110. time_2 = time_range[1] + ' 23:59:59'
  111. data_3 = self.db.select(Sql.sql_6, [time_1, time_2, house_ids, time_1, time_2, house_ids])
  112. number_3 = data_3[0][0]
  113. result.append(number_3)
  114. # 4:新增获电
  115. data_4 = self.db.select(Sql.sql_7, [time_1, time_2, house_ids, house_ids, time_1, time_2])
  116. number_4 = data_4[0][0]
  117. result.append(number_4)
  118. return result
  119. def region_house_id(self):
  120. return self.db.select(Sql.sql_11)
  121. def get_recommend_data(self, time_range):
  122. return self.db.select(Sql.sql_9, [time_range[0], time_range[1]])
  123. def get_house_id_by_brand_id(self, brand_id):
  124. result = []
  125. for x in self.get_city_house_id():
  126. if str(x[0]) == str(brand_id):
  127. result.append(x[1])
  128. return result
  129. def get_pv_data(self, time_type):
  130. pv_data = None
  131. if time_type == 1:
  132. pv_data = self.pv_data_1
  133. elif time_type == 2:
  134. pv_data = self.pv_data_2
  135. elif time_type == 3:
  136. pv_data = self.pv_data_3
  137. return pv_data
  138. def brand_pv_by_time_type(self, time_type):
  139. pv_data = self.get_pv_data(time_type)
  140. if pv_data:
  141. for x in pv_data:
  142. if str(x[0]) == self.brand_id and x[2] is not None and x[2] == '集团':
  143. return x[3:]
  144. return [0, 0, 0, 0]
  145. def brand_data_of_time(self, time_range, time_type):
  146. result = []
  147. data_1 = self.brand_pv_by_time_type(time_type)
  148. data_2 = []
  149. number_2 = 0
  150. for x in self.get_recommend_data(time_range):
  151. if str(x[0]) == str(self.brand_id):
  152. number_2 += x[1]
  153. data_2.append(number_2)
  154. data_3 = []
  155. sql_data_3 = self.db.select(Sql.sql_10, [time_range[0], time_range[1]])
  156. for key in self.index_type:
  157. number = 0
  158. for x in sql_data_3:
  159. if str(x[0]) == str(key):
  160. number = x[1]
  161. data_3.append(number)
  162. result.extend(data_1)
  163. result.extend(data_2)
  164. result.extend(data_3)
  165. return result
  166. def brand_data(self):
  167. """
  168. 集团数据总览, 表一。
  169. :return:
  170. """
  171. result = []
  172. result.extend(self.brand_data_of_time(self.time_range_1, 1))
  173. result.extend(self.brand_data_of_time(self.time_range_2, 2))
  174. result.extend(self.brand_data_of_time(self.time_range_3, 3))
  175. return result
  176. def get_house_pv_data(self, house_id, time_type):
  177. pv_data = self.get_pv_data(time_type)
  178. if pv_data is not None:
  179. for x in pv_data:
  180. if str(x[1]) == str(house_id):
  181. return x[3:]
  182. return [0, 0, 0, 0]
  183. def house_data_of_time(self, time_range, house_ids, time_type):
  184. # brand_id, house_id, house_name, city
  185. city_info = self.get_city_house_id()
  186. data_1 = []
  187. for id in house_ids:
  188. sub = [id]
  189. sub.extend(self.get_house_pv_data(id, time_type))
  190. data_1.append(sub)
  191. data_2 = []
  192. for id in house_ids:
  193. sub = [id]
  194. number = 0
  195. for x in self.get_recommend_data(time_range):
  196. if str(id) == str(x[0]):
  197. number = x[1]
  198. sub.append(number)
  199. data_2.append(sub)
  200. data_3 = []
  201. for house_id in house_ids:
  202. sub = []
  203. for x in self.house_data_detail(time_range):
  204. # house_id, type, COUNT(DISTINCT customer_mobile)
  205. if str(house_id) == str(x[0]):
  206. sub.append(x)
  207. house_data = [house_id]
  208. for key in self.index_type:
  209. number = 0
  210. for x in sub:
  211. if str(key) == str(x[1]):
  212. number = x[2]
  213. house_data.append(number)
  214. data_3.append(house_data)
  215. result = []
  216. for id in house_ids:
  217. sub = []
  218. if id == '0':
  219. sub.append(['品牌层', '品牌层', '品牌层'])
  220. else:
  221. for x in self.house_region:
  222. if str(x[1]) == str(id):
  223. sub.append(x[3])
  224. if len(sub) == 0:
  225. sub.append(' ')
  226. for x in city_info:
  227. if str(id) == str(x[1]):
  228. sub.extend([x[3], x[2]])
  229. if len(sub) == 0:
  230. sub.extend(['0000', '0000'])
  231. for x in data_1:
  232. if str(id) == str(x[0]):
  233. sub.extend(x[1:])
  234. for x in data_2:
  235. if str(id) == str(x[0]):
  236. sub.extend(x[1:])
  237. for x in data_3:
  238. if str(id) == str(x[0]):
  239. sub.extend(x[1:])
  240. result.append(sub)
  241. result.sort(key=lambda obj: obj[6], reverse=True)
  242. return result
  243. def house_data(self, house_ids):
  244. result = [self.house_data_of_time(self.time_range_1, house_ids, 1), self.house_data_of_time(self.time_range_2, house_ids, 2),
  245. self.house_data_of_time(self.time_range_3, house_ids, 3)]
  246. return result
  247. def get_table_3(self, house_ids):
  248. day_data = self.db.select(Sql.sql_16, [self.time_range_1[0], house_ids])
  249. month_data = self.db.select(Sql.sql_17, [self.time_range_2[1], house_ids])
  250. all_data = self.db.select(Sql.sql_18, [self.time_range_3[1], house_ids])
  251. return [day_data, month_data, all_data]
  252. def get_house_region_info(self):
  253. return self.db.select(Sql.sql_15)
  254. def send_mail_to_customer(self, task_key):
  255. """
  256. 统计数据推送给客户, 表二
  257. :param: task_key
  258. :return:
  259. """
  260. message = {}
  261. send_mail_info = []
  262. rfu = ReportFileUtils()
  263. customers = self.get_report_customers(task_key)
  264. message[0] = '客户信息获取成功'
  265. send_mail_log = []
  266. brand_table_one = None
  267. brand_table_two = None
  268. try:
  269. save_path = FileUtil().save_path_create()
  270. send_data = []
  271. if task_key == 12:
  272. brand_table_one = self.brand_data()
  273. message['brand'] = 'success'
  274. house_ids = self.get_house_id_by_brand_id('13')
  275. brand_table_two = self.house_data(house_ids)
  276. message['house'] = 'success'
  277. for customer in customers:
  278. name = customer[2]
  279. customer_type = customer[1]
  280. mail = customer[3]
  281. ids = customer[6]
  282. region_name = customer[4]
  283. title = self.get_mail_title(customer_type, region_name, name)
  284. content = self.get_mail_content(customer_type)
  285. try:
  286. if customer_type == 2:
  287. file_path = save_path + '/' + title + '.xls'
  288. content = self.get_brand_content(content, brand_table_one)
  289. send_data.append(
  290. [brand_table_two, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1],
  291. file_path, content, name, mail])
  292. elif customer_type == 1:
  293. title_1, title_2 = title[0], title[1]
  294. table_2 = self.house_data(self.get_house_ids(ids))
  295. # table_3 = self.get_table_3(self.get_house_ids(ids))
  296. file_path = save_path + '/' + title_1 + '.xls'
  297. file_path_1 = save_path + '/' + title_2 + '.xls'
  298. send_data.append(
  299. [table_2, self.sheet_names_1, title_1, [self.head_1, self.head_1, self.head_1], file_path,
  300. content, name, mail])
  301. # send_data.append(
  302. # [table_2, self.sheet_names_1, title_1, [self.head_1, self.head_1, self.head_1], file_path,
  303. # content, name, mail, title_2, [self.head_2, self.head_2, self.head_2], table_3, file_path_1])
  304. elif customer_type == 3:
  305. file_path = save_path + '/' + title + '.xls'
  306. table_2 = self.house_data(self.get_house_ids(ids))
  307. send_data.append(
  308. [table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], file_path,
  309. content, name, mail])
  310. else:
  311. send_mail_log.append([name, mail, title, -1, '客户类型错误{}'.format(customer_type)])
  312. pass
  313. except Exception as e:
  314. print(str(e))
  315. send_mail_log.append([name, mail, title, -1, '数据查询失败:{}'.format(str(e))])
  316. message['query_data'] = 'success'
  317. for data in send_data:
  318. try:
  319. send_mai_info_sub = []
  320. rfu.create_excel_file(data[0], data[1], data[2], data[3], data[4])
  321. send_mai_info_sub.extend([data[2], data[5], data[7], data[4], data[2] + '.xls', data[6]])
  322. if len(data) == 12:
  323. rfu.create_excel_file(data[10], data[1], data[8], data[9], data[11])
  324. send_mai_info_sub.extend([data[11], data[8] + '.xls'])
  325. send_mail_info.append(send_mai_info_sub)
  326. except Exception as e:
  327. print(e)
  328. send_mail_log.append([data[6], data[7], data[2], -1, '文件创建失败:{}'.format(str(e))])
  329. message['file'] = 'success'
  330. for mail in send_mail_info:
  331. try:
  332. self.send_mail(mail, send_mail_log)
  333. except Exception as e:
  334. print(str(e))
  335. message['error1'] = str(e)
  336. send_mail_log.append([mail[5], mail[2], mail[3], -1, '邮件发送失败:{}'.format(str(e))])
  337. message['mail'] = 'success'
  338. self.db.add_some(Sql.sql_12, send_mail_log)
  339. message['log'] = 'success'
  340. except Exception as e:
  341. message['error'] = str(e)
  342. finally:
  343. return message
  344. def get_brand_content(self, content, brand_table_one):
  345. content = content.format(rpfu.get_montho_day(),
  346. brand_table_one[0], brand_table_one[1], brand_table_one[2], brand_table_one[3],
  347. brand_table_one[4], brand_table_one[5],
  348. brand_table_one[6], brand_table_one[8], brand_table_one[10], brand_table_one[11],
  349. rpfu.get_month(),
  350. brand_table_one[12], brand_table_one[13], brand_table_one[14], brand_table_one[15],
  351. brand_table_one[16],
  352. brand_table_one[17],
  353. brand_table_one[18], brand_table_one[20], brand_table_one[22], brand_table_one[23],
  354. brand_table_one[24], brand_table_one[25], brand_table_one[26], brand_table_one[27],
  355. brand_table_one[28],
  356. brand_table_one[29],
  357. brand_table_one[30], brand_table_one[32], brand_table_one[34], brand_table_one[35]
  358. )
  359. return content
  360. def get_customer_info_by_id(self, customer_id):
  361. return self.db.select(Sql.sql_14, [customer_id])
  362. def send_mail_for_customer_id(self, customer_id, new_mail=None):
  363. """
  364. 获取指定客户的邮件信息
  365. :param customer_id:
  366. :param new_mail:
  367. :return:
  368. """
  369. message = {}
  370. customers = self.get_customer_info_by_id(customer_id)
  371. message['customer'] = str(customer_id)
  372. send_data = []
  373. if customers:
  374. save_path = FileUtil().save_path_create()
  375. for customer in customers:
  376. try:
  377. name = customer[1]
  378. mail = customer[2]
  379. customer_type = customer[3]
  380. ids = self.get_house_ids(customer[5])
  381. region_name = customer[4]
  382. title = self.get_mail_title(customer_type, region_name, name)
  383. content = self.get_mail_content(customer_type)
  384. if customer_type == 1:
  385. # 项目
  386. # 项目
  387. title_1, title_2 = title[0], title[1]
  388. table_2 = self.house_data(ids)
  389. # table_3 = self.get_table_3(ids)
  390. file_path = save_path + '/' + title_1 + '.xls'
  391. file_path_1 = save_path + '/' + title_2 + '.xls'
  392. send_data.append(
  393. [table_2, self.sheet_names_1, title_1, [self.head_1, self.head_1, self.head_1], file_path,
  394. content, name, mail])
  395. # send_data.append(
  396. # [table_2, self.sheet_names_1, title_1, [self.head_1, self.head_1, self.head_1], file_path,
  397. # content, name, mail, title_2, [self.head_2, self.head_2, self.head_2], table_3,
  398. # file_path_1])
  399. elif customer_type == 2:
  400. # 集团
  401. file_path = save_path + '/' + title + '.xls'
  402. brand_table_one = self.brand_data()
  403. house_ids = self.get_house_id_by_brand_id('13')
  404. brand_table_two = self.house_data(house_ids)
  405. content = self.get_brand_content(content, brand_table_one)
  406. send_data.append(
  407. [brand_table_two, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1],
  408. file_path, content, name, mail])
  409. elif customer_type == 3:
  410. # 区域
  411. file_path = save_path + '/' + title + '.xls'
  412. table_2 = self.house_data(ids)
  413. send_data.append(
  414. [table_2, self.sheet_names_1, title, [self.head_1, self.head_1, self.head_1], file_path,
  415. content, name, mail])
  416. else:
  417. message['customer_type'] = '客户类型错误'
  418. except Exception as e:
  419. message['数据查询失败'] = str(e)
  420. else:
  421. message['error'] = '客户id错误:{}'.format(customer_id)
  422. # 生成文件
  423. rfu = ReportFileUtils()
  424. send_mail_log = []
  425. send_mail_info = []
  426. if len(send_data) > 0:
  427. for data in send_data:
  428. try:
  429. send_mai_info_sub = []
  430. rfu.create_excel_file(data[0], data[1], data[2], data[3], data[4])
  431. send_mai_info_sub.extend([data[2], data[5], data[7], data[4], data[2] + '.xls', data[6]])
  432. if len(data) == 12:
  433. rfu.create_excel_file(data[10], data[1], data[8], data[9], data[11])
  434. send_mai_info_sub.extend([data[11], data[8] + '.xls'])
  435. send_mail_info.append(send_mai_info_sub)
  436. except Exception as e:
  437. print(e)
  438. message['excel文件创建失败'] = str(e)
  439. send_mail_log.append([data[6], data[7], data[2], -1, '文件创建失败:{}'.format(str(e))])
  440. else:
  441. message['excel_info'] = '需要生成excel的数据空'
  442. # 发送邮件
  443. if len(send_data) > 0:
  444. for mail in send_mail_info:
  445. try:
  446. if new_mail:
  447. mail[2] = new_mail
  448. self.send_mail(mail, send_mail_log)
  449. else:
  450. for m in self.customer_mails:
  451. self.send_mail(m, send_mail_log)
  452. except Exception as e:
  453. print(str(e))
  454. message['邮件发送失败'] = str(e)
  455. send_mail_log.append([mail[5], mail[2], mail[3], -1, '邮件发送失败:{}'.format(str(e))])
  456. else:
  457. message['mail_info'] = '需要发送邮件的数据为空'
  458. # 写入日志
  459. self.db.add_some(Sql.sql_12, send_mail_log)
  460. return message
  461. def send_mail(self, mail, send_mail_log):
  462. result = None
  463. # for m in self.customer_mails:
  464. if len(mail) == 6:
  465. result = self.mail_util.send_mail_by_admin(mail[0], mail[1], mail[2], mail[3], mail[4])
  466. elif len(mail) == 8:
  467. result = self.mail_util.send_mail_by_admin(mail[0], mail[1], mail[2], mail[3], mail[4], mail[6],
  468. mail[7])
  469. if result:
  470. # name, mail, report_name, push_time, send_status, status, error_message
  471. send_mail_log.append([mail[5], mail[2], mail[3], 1, 'success'])
  472. else:
  473. send_mail_log.append([mail[5], mail[2], mail[3], -1, '调用邮件接口失败:{}'.format(str(result))])
  474. return send_mail_log
  475. def get_house_ids(self, ids_str):
  476. if str(ids_str).find(',') == -1:
  477. return [str(ids_str)]
  478. else:
  479. return [str(x) for x in str(ids_str).split(',')]
  480. if __name__ == '__main__':
  481. pass