jianye_report.py 22 KB

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