jianye_report.py 22 KB

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