report_push.py 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. from mysql_db import MysqlDB
  2. import datetime
  3. from itertools import groupby
  4. from xlwt_util import XlwtUtil
  5. from file_util import FileUtil
  6. from email_util import EmailUtil
  7. class ReportPush(object):
  8. """
  9. 报表推送功能实现类
  10. """
  11. pass
  12. # 1:总浏览量
  13. #
  14. # 集团部分+项目部分!!!
  15. # ----项目PV--权限项目范围内,求和
  16. # 参数:时间区间 和 项目列表
  17. sql_1_1 = """
  18. SELECT SUM(pv) AS pv1 FROM a_idfa_behavior_sum
  19. WHERE report_d >= %s and report_d <= %s AND house_id IN %s
  20. """
  21. # ----集团PV--权限项目范围内,求和
  22. # 参数:数据区间和项目列表
  23. sql_1_2 = """
  24. SELECT SUM(pv) AS pav2 FROM a_behavior_brand_mini_day
  25. WHERE report_d >= %s and report_d <= %s AND house_id IN %s
  26. """
  27. # 2:总浏览人数
  28. #
  29. # 参数,数据区间, 项目列表
  30. sql_1_3 = """
  31. SELECT
  32. count(
  33. DISTINCT IFNULL(mobile, user_id)
  34. ) as people
  35. FROM
  36. (
  37. SELECT
  38. A.user_id,
  39. B.mobile
  40. FROM
  41. a_idfa_behavior_sum A
  42. LEFT JOIN d_user B ON A.user_id = B.user_id
  43. WHERE
  44. A.report_d >= %s and A.report_d <= %s
  45. AND A.house_id IN %s
  46. UNION
  47. SELECT
  48. A.brand_user_id AS user_id,
  49. B.mobile
  50. FROM
  51. a_behavior_brand_mini_day A
  52. LEFT JOIN a_brand_app_customer B ON A.brand_user_id = B.brand_customer_id
  53. WHERE
  54. A.report_d >= %s and A.report_d <= %s
  55. AND A.house_id IN %s
  56. ) t1
  57. """
  58. # 3:新增获客
  59. sql_1_4 = """
  60. SELECT
  61. count(
  62. DISTINCT IFNULL(mobile, user_id)
  63. )
  64. FROM
  65. (
  66. SELECT
  67. user_id,
  68. mobile,
  69. created
  70. FROM
  71. d_user
  72. WHERE
  73. created >= %s
  74. AND created <= %s
  75. AND house_id IN %s
  76. UNION
  77. SELECT
  78. brand_customer_id AS user_id,
  79. mobile,
  80. rlat_created
  81. FROM
  82. a_brand_app_customer_house_rlat
  83. WHERE
  84. rlat_created >= %s
  85. AND rlat_created <= %s
  86. AND rlat_house_id IN %s
  87. ) t1
  88. """
  89. # 4:新增获电
  90. sql_1_5 = """
  91. SELECT
  92. COUNT(DISTINCT mobile)
  93. FROM
  94. (
  95. SELECT
  96. user_id,
  97. mobile,
  98. wx_phone_time AS created
  99. FROM
  100. d_user
  101. WHERE
  102. wx_phone_time >= %s
  103. AND wx_phone_time <= %s
  104. AND house_id IN %s
  105. UNION
  106. SELECT
  107. brand_customer_id,
  108. mobile,
  109. houdian_time AS created
  110. FROM
  111. (
  112. SELECT
  113. *,
  114. CASE
  115. WHEN rlat_created > shouquan_time THEN
  116. rlat_created
  117. ELSE
  118. shouquan_time
  119. END AS houdian_time
  120. FROM
  121. a_brand_app_customer_house_rlat
  122. WHERE
  123. mobile IS NOT NULL
  124. AND rlat_house_id IN %s
  125. AND cust_house_flag = '1'
  126. ) t1
  127. WHERE
  128. houdian_time >= %s
  129. AND houdian_time < %s
  130. ) t1
  131. """
  132. # 5:集团小程序总浏览量(针对香港置地要命名为【CNC小程序总浏览量】)
  133. # 集团部分
  134. # 6:集团小程序总浏览人数(针对香港置地要命名为【CNC小程序总浏览人数】)
  135. #
  136. # 集团小程序整体UV(见SQL3)
  137. sql_1_6 = """
  138. SELECT
  139. COUNT(DISTINCT brand_user_id) AS UV4
  140. FROM
  141. a_behavior_brand_mini_day
  142. WHERE
  143. report_d > %s
  144. AND report_d < %s
  145. AND house_id IN %s
  146. """
  147. # 7: 集团小程序新增获客(针对香港置地要命名为【CNC小程序新增获客】)
  148. #
  149. # 权限项目范围内,集团维度的获客
  150. # 8: 集团小程序新增获电(针对香港置地要命名为【CNC小程序新增获电】)
  151. #
  152. # 权限项目范围内,集团维度的获电
  153. # 9.单项目小程序总浏览量
  154. #
  155. # 项目部分
  156. #
  157. # 10.单项目小程序总浏览人数
  158. #
  159. # 权限项目未授权部分求和+去重的授权部分(见SQL2)
  160. #
  161. # 11.单项目小程序新增获客
  162. #
  163. # 权限项目未授权部分求和+去重的授权部分(见SQL7)
  164. #
  165. # 12.单项目小程序新增获电
  166. #
  167. # 权限项目所有授权手机号去重(见SQL8)
  168. # 1.默认值/001_大麦/项目排行榜/小程序排行榜TOP_N
  169. sql_2_1 = """
  170. SELECT
  171. a.house_id,
  172. a.house_name,
  173. ifnull(SUM(a.pv), 0),
  174. SUM(a.uv),
  175. SUM(a.new_cust_num),
  176. SUM(a.wx_num)
  177. from
  178. (SELECT
  179. a.*,
  180. b.house_name,
  181. c.interested_num,
  182. d.wx_num,
  183. e.new_cust_num
  184. FROM
  185. (
  186. SELECT
  187. house_id,
  188. count(
  189. DISTINCT ifnull(user_id, idfa)
  190. ) uv,
  191. sum(session_times) session_times,
  192. sum(sum_session_time) sum_session_time,
  193. sum(pv) pv,
  194. sum(page_num) page_num
  195. FROM
  196. a_idfa_behavior_sum
  197. WHERE
  198. report_d >= %s
  199. AND report_d <= %s
  200. GROUP BY
  201. house_id
  202. ) a
  203. JOIN d_house b ON a.house_id = b.house_id
  204. LEFT JOIN (
  205. SELECT
  206. house_id,
  207. count(*) interested_num
  208. FROM
  209. f_interested_custlist
  210. WHERE
  211. report_d >= %s
  212. AND report_d <= %s
  213. GROUP BY
  214. house_id
  215. ) c ON a.house_id = c.house_id
  216. LEFT JOIN (
  217. SELECT
  218. house_id,
  219. count(*) wx_num
  220. FROM
  221. f_customer_dynamic
  222. WHERE
  223. dynamic = 1
  224. AND report_d >= %s
  225. AND report_d <= %s
  226. GROUP BY
  227. house_id
  228. ) d ON a.house_id = d.house_id
  229. LEFT JOIN (
  230. SELECT
  231. house_id,
  232. count(*) new_cust_num
  233. FROM
  234. d_user
  235. WHERE
  236. created >= %s
  237. AND created <= %s
  238. AND source IN (1, 2, 4, 10)
  239. GROUP BY
  240. house_id
  241. ) e ON a.house_id = e.house_id
  242. ) a
  243. GROUP BY
  244. a.house_id,
  245. a.house_name
  246. order by a.pv desc
  247. """
  248. # 2.默认值/006_大麦(集团)/集团项目排行榜v1.3/集团排行榜
  249. sql_2_2 = """
  250. SELECT
  251. a.house_id,
  252. a.house_name,
  253. ifnull(SUM(a.pv), 0),
  254. SUM(a.uv),
  255. SUM(a.new_cust_num),
  256. SUM(a.wx_num)
  257. from
  258. (
  259. SELECT
  260. a.*, b.house_name,
  261. c.interested_num,
  262. d.wx_num,
  263. e.new_cust_num
  264. FROM
  265. (
  266. SELECT
  267. house_id,
  268. count(
  269. DISTINCT ifnull(user_id, idfa)
  270. ) uv,
  271. sum(session_times) session_times,
  272. sum(sum_session_time) sum_session_time,
  273. sum(pv) pv,
  274. sum(page_num) page_num
  275. FROM
  276. a_idfa_behavior_sum
  277. WHERE
  278. report_d >= %s
  279. AND report_d <= %s
  280. GROUP BY
  281. house_id
  282. ) a
  283. JOIN d_house b ON a.house_id = b.house_id
  284. LEFT JOIN (
  285. SELECT
  286. house_id,
  287. count(DISTINCT customer_id) interested_num
  288. FROM
  289. f_interested_custlist
  290. WHERE
  291. report_d >=%s
  292. AND report_d <=%s
  293. GROUP BY
  294. house_id
  295. ) c ON a.house_id = c.house_id
  296. LEFT JOIN (
  297. SELECT
  298. house_id,
  299. count(DISTINCT mobile) wx_num
  300. FROM
  301. f_customer_dynamic
  302. WHERE
  303. dynamic IN (1, 2, 4)
  304. AND report_d >=%s
  305. AND report_d <=%s
  306. GROUP BY
  307. house_id
  308. ) d ON a.house_id = d.house_id
  309. LEFT JOIN (
  310. SELECT
  311. house_id,
  312. count(DISTINCT user_id) new_cust_num
  313. FROM
  314. d_user
  315. WHERE
  316. source IN (1, 2, 3, 4, 10)
  317. AND created >=%s
  318. AND created < DATE_ADD(
  319. %s, INTERVAL 1 DAY
  320. )
  321. GROUP BY
  322. house_id
  323. ) e ON a.house_id = e.house_id)
  324. a
  325. GROUP BY
  326. a.house_id,
  327. a.house_name
  328. order by a.pv desc
  329. """
  330. # 默认值/001_大麦/场景_用户来源渠道/用户来源渠道—明细
  331. sql_3_1 = """
  332. SELECT
  333. house_id,
  334. house_name,
  335. label_wx,
  336. COUNT(a.id) as counts
  337. FROM
  338. d_user_attr a
  339. LEFT JOIN d_scene b ON a.scene = b. CODE
  340. WHERE
  341. a.source IN (1, 2, 3, 4, 10)
  342. AND a.report_d >= %s
  343. AND a.report_d <= %s
  344. GROUP BY
  345. house_id,
  346. house_name,
  347. label_wx
  348. """
  349. # 默认值/006_大麦(集团)/场景(集团)_用户来源渠道_v1.1/用户来源渠道—明细
  350. sql_3_2 = """
  351. select
  352. brand_id,
  353. x.brand_name,
  354. house_id,
  355. house_name,
  356. label_wx,
  357. COUNT(1)
  358. from
  359. (SELECT
  360. a.scene,
  361. a.brand_id,
  362. b.*, a.share_brand_customer_id,
  363. '2' adviser_agent,
  364. a.house_id house_id,
  365. c.house_name house_name,
  366. c.brand_name
  367. FROM
  368. (
  369. SELECT
  370. scene,
  371. brand_id,
  372. share_brand_customer_id,
  373. house_id
  374. FROM
  375. d_brand_app_customer
  376. WHERE
  377. created >= %s
  378. AND created < DATE_ADD(
  379. %s, INTERVAL 1 DAY
  380. )
  381. UNION ALL
  382. SELECT
  383. scene,
  384. brand_id,
  385. share_brand_customer_id,
  386. brand_id house_id
  387. FROM
  388. d_brand_app_customer
  389. WHERE
  390. created >= %s
  391. AND created < DATE_ADD(
  392. %s, INTERVAL 1 DAY
  393. )
  394. ) a
  395. LEFT JOIN d_scene b ON a.scene = b. CODE
  396. JOIN d_house c ON a.house_id = c.house_id
  397. AND a.brand_id = c.brand_id) x
  398. group by x.brand_id, x.brand_name, x.house_id, x.house_name, x.label_wx
  399. """
  400. # 根据任务id获取推送客户信息
  401. sql_4 = """
  402. select a.task_key, a.customer_id, b.customer_type, b.name, b.mail, GROUP_CONCAT(c.house_or_brand_id) as ids
  403. from report_task_info a left join report_push_customer_info b on b.id = a.customer_id
  404. left join report_customer_authority_info c on b.id = c.customer_id
  405. where a.task_key = %s and a.status = b.status = c.status = 1
  406. group by a.task_key, a.customer_id, b.customer_type, b.name, b.mail
  407. """
  408. # 根据集团id获取项目id
  409. sql_5 = """
  410. select house_id, house_name from d_house where brand_id = %s
  411. """
  412. sql_5_1 = """
  413. select house_id from d_house where brand_id in %s
  414. """
  415. sql_6 = """insert into report_push_log(name, mail, report_name, push_time, send_status, status) values(%s, %s,
  416. %s, now(), %s, 1) """
  417. # 根据项目id获取集团id和名称
  418. sql_7 = """
  419. select a.brand_id, a.brand_name from d_house a where a.house_id = %s
  420. """
  421. sql_8 = """
  422. select DISTINCT a.brand_id from d_house a where a.house_id in %s
  423. """
  424. def __init__(self, db_name):
  425. self.db = MysqlDB(db_name)
  426. mails = ['1285211525@qq.com', 'lijm@elab-plus.com', 'xuanxc@elab-plus.com']
  427. def report_push(self, task_key):
  428. message = {}
  429. try:
  430. report_data = self.report_data_query(task_key)
  431. message[1] = '数据查询成功:{}'.format(len(report_data))
  432. time_rang = self.get_time_range(task_key)
  433. xu = XlwtUtil()
  434. send_info = xu.create_excel(report_data, time_rang, task_key)
  435. message[2] = '报表文件生成成功'
  436. email_util = EmailUtil()
  437. title, content = self.get_title_content(task_key, time_rang)
  438. logs = []
  439. for value in send_info:
  440. log_data = []
  441. try:
  442. if value[2]:
  443. email_util.send_mail(title, content, self.mails, value[2])
  444. log_data = [value[0], value[1], value[2], 1]
  445. except Exception as e:
  446. log_data = [value[0], value[1], value[2], -1]
  447. print(str(e))
  448. logs.append(log_data)
  449. message[3] = '邮件发送成功'
  450. message['data'] = send_info
  451. self.db.add_some(self.sql_6, logs)
  452. self.db.close()
  453. email_util.quit_mail()
  454. FileUtil.remove_files(7, xu.save_path)
  455. except Exception as e:
  456. print(str(e))
  457. message['error'] = str(e)
  458. pass
  459. finally:
  460. return message
  461. def get_title_content(self, task_key, time_range):
  462. if task_key == 1:
  463. return '移动案场订阅日报[{}]'.format(time_range[0]), '本期间内【{}】至【{}】的数据报告已经准备完成。请点击附件查阅。'.format(time_range[0], time_range[1])
  464. elif task_key in (2, 3):
  465. return '移动案场订阅周报[{}]至[{}]'.format(time_range[0], time_range[1]), '本期间内【{}】至【{}】的数据报告已经准备完成。请点击附件查阅.'.format(time_range[0], time_range[1])
  466. def report_data_query(self, task_key):
  467. """
  468. 定时任务推送数据准备
  469. :param task_key:
  470. :return:
  471. """
  472. result = {}
  473. try:
  474. # 根据任务key获取需要推送的客户以及可以的权限
  475. customers = self.db.select(self.sql_4, [task_key])
  476. # a.task_key, a.customer_id, b.customer_type, b.`name`, b.mail, GROUP_CONCAT(c.house_or_brand_id)
  477. time_range = self.get_time_range(task_key)
  478. all_time_rang = self.get_time_range(4)
  479. # 有限时间范围内的数据
  480. xcx_top_data = self.xcx_top(time_range)
  481. brand_top_data = self.brand_top(time_range)
  482. customer_channel_details_data = self.customer_channel_details(time_range)
  483. brand_customer_channel_details = self.brand_customer_channel_details(time_range)
  484. # 所有历史数据
  485. xcx_top_data_all = self.xcx_top(all_time_rang)
  486. brand_top_data_all = self.brand_top(all_time_rang)
  487. for customer in customers:
  488. customer_data = {}
  489. name = customer[3]
  490. mail = customer[4]
  491. customer_type = customer[2]
  492. house_ids = []
  493. brand_id_list = []
  494. if customer_type == 1:
  495. # 项目
  496. ids = customer[5]
  497. if str(ids).find(',') != -1:
  498. house_ids = [x for x in str(ids).split(',')]
  499. else:
  500. house_ids = [ids]
  501. brand_id_list = self.get_brand_ids_by_house_ids(house_ids)
  502. pass
  503. elif customer_type == 2:
  504. # 集团
  505. brand_ids = customer[5]
  506. if str(brand_ids).find(',') != -1:
  507. brands = [x for x in str(brand_ids).split(',')]
  508. else:
  509. brands = [brand_ids]
  510. for id in brands:
  511. house_ids.extend([x[0] for x in self.get_house_ids_by_brand_id(id)])
  512. brand_id_list = brands
  513. result_data_1 = []
  514. result_data_2 = []
  515. result_data_3 = []
  516. result_data_4 = []
  517. result_data_5 = []
  518. result_data_7 = []
  519. result_data_8 = []
  520. all_house_ids = self.get_house_ids_by_brand_ids(brand_id_list)
  521. xcx_top_data_part = self.filter_by_house_ids(xcx_top_data, all_house_ids)
  522. brand_top_data_part = self.filter_by_brand_ids(brand_top_data, brand_id_list)
  523. xcx_top_data_all_part = self.filter_by_house_ids(xcx_top_data_all, all_house_ids)
  524. brand_top_data_all_part = self.filter_by_brand_ids(brand_top_data_all, brand_id_list)
  525. # 1 数据总览 12个统计指标
  526. data_overview = self.data_overview(time_range, house_ids, xcx_top_data_part, brand_top_data_part)
  527. result_data_1.extend(data_overview)
  528. # 4:单个项目小程序数据排行榜
  529. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  530. for index, x in enumerate(xcx_top_data_part):
  531. if x[0] in house_ids:
  532. result_data_4.append([index, x[1], x[2], x[3], x[4], x[5]])
  533. # 5: 集团项目数据排行榜
  534. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  535. if customer_type == 2:
  536. for index, x2 in enumerate(brand_top_data_part):
  537. if x2[1] in house_ids or x2[0] in brand_id_list:
  538. result_data_5.append([index, x2[2], x2[3], x2[4], x2[5], x2[6]])
  539. pass
  540. pass
  541. elif customer_type == 1:
  542. for index, x1 in enumerate(brand_top_data_part):
  543. if x1[1] in house_ids:
  544. result_data_5.append([index, x1[2], x1[3], x1[4], x1[5], x1[6]])
  545. else:
  546. pass
  547. # 2: 项目数据排行榜
  548. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  549. house_with_brand_data = self.house_with_brand(xcx_top_data_part, brand_top_data_part)
  550. for x in house_with_brand_data:
  551. if x[2] in house_ids:
  552. x.pop(1)
  553. x.pop(1)
  554. result_data_2.append(x)
  555. # 3: 项目历史累计总数
  556. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  557. if customer_type == 2:
  558. all_data_history = self.house_with_brand(xcx_top_data_all_part, brand_top_data_all_part, brand_id_list)
  559. for index, x in enumerate(all_data_history):
  560. if x[2] in house_ids or x[1] in brand_id_list:
  561. x.pop(1)
  562. x.pop(1)
  563. result_data_3.append(x)
  564. else:
  565. all_data_history = self.house_with_brand(xcx_top_data_all_part, brand_top_data_all_part)
  566. for index, x in enumerate(all_data_history):
  567. if x[2] in house_ids:
  568. x.pop(1)
  569. x.pop(1)
  570. result_data_3.append(x)
  571. # 7: 单个项目小程序获客来源场景分析
  572. # 项目 合计 长按识别二维码 会话 公众号菜单 公众号文章 小程序历史列表 扫一扫二维码 搜索 相册选取二维码 其他小程序 其他
  573. for x in customer_channel_details_data:
  574. if x[0] in house_ids:
  575. result_data_7.append(x)
  576. # 8: 集团项目获客来源场景分析
  577. if customer_type == 2:
  578. for x in brand_customer_channel_details:
  579. if x[2] in house_ids or x[0] in brand_id_list:
  580. result_data_8.append(x)
  581. elif customer_type == 1:
  582. for x in brand_customer_channel_details:
  583. if x[2] in house_ids:
  584. result_data_8.append(x)
  585. # 6: 项目获客来源场景分析
  586. result_data_6 = self.house_with_brand_for_share(result_data_7, result_data_8)
  587. customer_data[1] = result_data_1
  588. result_data_2.sort(key=lambda obj: obj[0])
  589. customer_data[2] = result_data_2
  590. result_data_3.sort(key=lambda obj: obj[0])
  591. customer_data[3] = result_data_3
  592. result_data_4.sort(key=lambda obj: obj[0])
  593. customer_data[4] = result_data_4
  594. result_data_5.sort(key=lambda obj: obj[0])
  595. customer_data[5] = result_data_5
  596. self.sort(result_data_6, 1)
  597. customer_data[6] = result_data_6
  598. result_data_7_format = []
  599. for x in result_data_7:
  600. house_name = x[1]
  601. ele = [house_name]
  602. data = x[2:]
  603. total = sum(data)
  604. ele.append(total)
  605. ele.extend(data)
  606. result_data_7_format.append(ele)
  607. self.sort(result_data_7_format, 1)
  608. customer_data[7] = result_data_7_format
  609. result_data_8_format = []
  610. for x in result_data_8:
  611. ele = []
  612. house_name = x[3]
  613. data = x[4:]
  614. total = sum(data)
  615. ele.append(house_name)
  616. ele.append(total)
  617. ele.extend(data)
  618. result_data_8_format.append(ele)
  619. self.sort(result_data_8_format, 1)
  620. customer_data[8] = result_data_8_format
  621. customer_data[0] = mail
  622. result[name] = customer_data
  623. # return result
  624. except:
  625. pass
  626. finally:
  627. return result
  628. def sort(self, data, idnex):
  629. data.sort(key=lambda obj: obj[idnex])
  630. data.reverse()
  631. def house_with_brand_for_share(self, house_data, brand_data):
  632. house_ids = []
  633. result = []
  634. for x in house_data:
  635. if x[0] not in [a[0] for a in house_ids]:
  636. house_ids.append([x[0], x[1]])
  637. for x in brand_data:
  638. if x[2] not in [a[0] for a in house_ids]:
  639. house_ids.append([x[2], x[3]])
  640. for id in house_ids:
  641. house_id = id[0]
  642. house_name = id[1]
  643. house_result = [house_name]
  644. equal_house = self.equal_by_house_id(house_data, house_id, 0)
  645. equal_brand = self.equal_by_house_id(brand_data, house_id, 2)
  646. house_result_part = []
  647. if equal_house and equal_brand:
  648. equal_house = equal_house[2:]
  649. equal_brand = equal_brand[4:]
  650. for i in range(0, 10):
  651. house_result_part.append(self.add(equal_house[i], equal_brand[i]))
  652. elif equal_house and not equal_brand:
  653. equal_house = equal_house[2:]
  654. for i in range(0, 10):
  655. house_result_part.append(equal_house[i])
  656. elif not equal_house and equal_brand:
  657. equal_brand = equal_brand[4:]
  658. for i in range(0, 10):
  659. house_result_part.append(equal_brand[i])
  660. house_result.append(sum(house_result_part))
  661. house_result.extend(house_result_part)
  662. result.append(house_result)
  663. result.sort(key=lambda obj: obj[1])
  664. result.reverse()
  665. return result
  666. def equal_by_house_id(self, data, house_id, index):
  667. for x in data:
  668. if x[index] == house_id:
  669. return x
  670. def filter_by_brand_ids(self, data, brand_ids):
  671. result = []
  672. for x in data:
  673. if x[0] in brand_ids:
  674. result.append(x)
  675. result.sort(key=lambda obj: obj[3])
  676. result.reverse()
  677. return result
  678. def filter_by_house_ids(self, data, house_ids):
  679. result = []
  680. for x in data:
  681. if x[0] in house_ids:
  682. result.append(x)
  683. result.sort(key=lambda obj: obj[2])
  684. result.reverse()
  685. return result
  686. def data_overview(self, time_range, house_ids, xcx_top_data, brand_top_data):
  687. """
  688. 统计数据总览
  689. :param time_range:
  690. :param house_ids:
  691. :return:
  692. """
  693. result = []
  694. # 1:总浏览量
  695. data_1_1 = self.db.select(self.sql_1_1, [time_range[0], time_range[1], house_ids])
  696. number_1_1 = data_1_1[0][0]
  697. data_1_2 = self.db.select(self.sql_1_2, [time_range[0], time_range[1], house_ids])
  698. number_1_2 = data_1_2[0][0]
  699. number_1 = self.add(number_1_1, number_1_2)
  700. result.append(number_1)
  701. # 2: 总浏览人数
  702. data_2 = self.db.select(self.sql_1_3, [time_range[0], time_range[1], house_ids, time_range[0], time_range[1], house_ids])
  703. number_2 = data_2[0][0]
  704. result.append(number_2)
  705. # 3:新增获客
  706. data_3 = self.db.select(self.sql_1_4, [time_range[0], time_range[1], house_ids, time_range[0], time_range[1], house_ids])
  707. number_3 = data_3[0][0]
  708. result.append(number_3)
  709. # 4:新增获电
  710. data_4 = self.db.select(self.sql_1_5, [time_range[0], time_range[1], house_ids, house_ids, time_range[0], time_range[1]])
  711. number_4 = data_4[0][0]
  712. result.append(number_4)
  713. xcx_top_data_part = [x for x in xcx_top_data if x[0] in house_ids]
  714. brand_top_data_part = [x for x in brand_top_data if x[1] in house_ids]
  715. # 5 6 7 8
  716. number_5 = 0
  717. number_6 = 0
  718. number_7 = 0
  719. number_8 = 0
  720. for x in brand_top_data_part:
  721. number_5 = self.add(number_5, x[3])
  722. number_6 = self.add(number_6, x[4])
  723. number_7 = self.add(number_7, x[5])
  724. number_8 = self.add(number_8, x[6])
  725. result.append(number_5)
  726. result.append(number_6)
  727. result.append(number_7)
  728. result.append(number_8)
  729. # 9 10 11 12
  730. number_9 = 0
  731. number_10 = 0
  732. number_11 = 0
  733. number_12 = 0
  734. for x in xcx_top_data_part:
  735. number_9 = self.add(number_9, x[2])
  736. number_10 = self.add(number_10, x[3])
  737. number_11 = self.add(number_11, x[4])
  738. number_12 = self.add(number_12, x[5])
  739. result.append(number_9)
  740. result.append(number_10)
  741. result.append(number_11)
  742. result.append(number_12)
  743. return result
  744. def house_with_brand(self, xcx_top_data, brand_top_data, brands=None):
  745. """
  746. 项目数据和集团数据的求和
  747. :param brands:
  748. :param xcx_top_data:
  749. :param brand_top_data:
  750. :return:
  751. """
  752. result = []
  753. house_ids = []
  754. for x in xcx_top_data:
  755. if x[0] not in house_ids:
  756. house_ids.append(x[0])
  757. for x in brand_top_data:
  758. if x[1] not in house_ids and x[1] is not None and int(x[1]) > 300:
  759. house_ids.append(x[1])
  760. for house_id in house_ids:
  761. a = []
  762. a_order = 0
  763. for index, x in enumerate(xcx_top_data):
  764. if str(house_id) == str(x[0]):
  765. a.extend(x)
  766. a_order = index + 1
  767. b = []
  768. b_order = 0
  769. for index, y in enumerate(brand_top_data):
  770. if str(house_id) == str(y[1]):
  771. b.extend(y)
  772. b_order = index + 1
  773. order = b_order if b_order > 0 else a_order
  774. if len(a) > 0 and len(b) > 0:
  775. result.append([order, b[0], a[0], a[1], self.add(a[2], b[3]), self.add(a[3], b[4]), self.add(a[4], b[5]), self.add(a[5], b[6])])
  776. elif len(a) > 0 and len(b) == 0:
  777. _a = [order, 1]
  778. for x in a:
  779. _a.append(x)
  780. result.append(_a)
  781. elif len(a) == 0 and len(b) > 0:
  782. _b = [order]
  783. _b.extend(b)
  784. result.append(_b)
  785. else:
  786. pass
  787. if brands:
  788. for index, x in enumerate(brand_top_data):
  789. if x[0] in brands and x[1] is not None and int(x[1]) < 300:
  790. _x = [index]
  791. _x.extend(x)
  792. result.append(_x)
  793. result.sort(key=lambda obj: obj[3])
  794. result.reverse()
  795. return result
  796. def add(self, a=None, b=None):
  797. if a and b:
  798. return a + b
  799. elif a and not b:
  800. return a
  801. elif b and not a:
  802. return b
  803. return 0
  804. def xcx_top(self, time_range):
  805. """
  806. 获取 1.默认值/001_大麦/项目排行榜/小程序排行榜TOP_N
  807. :return:
  808. """
  809. params = []
  810. params.extend(time_range)
  811. params.extend(time_range)
  812. params.extend(time_range)
  813. params.extend(time_range)
  814. xcx_top_data = self.db.select(self.sql_2_1, params)
  815. result = []
  816. for x in xcx_top_data:
  817. result.append([n for n in x])
  818. # xcx_top_data的结果结构
  819. # 0 a.house_id, 项目id
  820. # 1 a.house_name, 项目名称
  821. # 2 SUM(a.pv), 浏览总量
  822. # 3 SUM(a.uv), 浏览人数
  823. # 4 SUM(a.new_cust_num), 新增获客
  824. # 5 SUM(a.wx_num) 授权手机号
  825. result.sort(key=lambda obj: obj[2])
  826. result.reverse()
  827. return result
  828. def brand_top(self, time_range):
  829. """
  830. 2.默认值/006_大麦(集团)/集团项目排行榜v1.3/集团排行榜
  831. :param task_key:
  832. :return:
  833. """
  834. params = [time_range[0], time_range[1]]
  835. brand_top_data = self.db.select(self.sql_2_2, params)
  836. result = []
  837. for x in brand_top_data:
  838. result.append([n for n in x])
  839. # brand_top_data结果的结构
  840. # 0 a.brand_id, 集团id
  841. # 1 a.house_id, 项目id
  842. # 2 a.house_name, 项目名称
  843. # 3 SUM(a.pv), 浏览总量
  844. # 4 SUM(a.uv), 浏览人数
  845. # 5 SUM(a.new_cust), 新增获客
  846. # 6 SUM(a.shouquan_cust) 授权手机号
  847. result.sort(key=lambda obj: obj[3])
  848. result.reverse()
  849. return result
  850. """
  851. 数据分享类别
  852. 1:长按识别二维码 2:会话 3:公众号菜单 4:公众号文章 5:小程序历史列表 6:扫一扫二维码
  853. 7:搜索 8:相册选取二维码 9:其他小程序 10:其他
  854. """
  855. share_way = {
  856. "长按识别二维码": 1,
  857. "会话": 2,
  858. "公众号菜单": 3,
  859. '公众号文章': 4,
  860. '小程序历史列表': 5,
  861. '扫一扫二维码': 6,
  862. '搜索': 7,
  863. '相册选取二维码': 8,
  864. '其他小程序': 9,
  865. '其他': 10
  866. }
  867. def customer_channel_details(self, time_range):
  868. """
  869. 1.默认值/001_大麦/场景_用户来源渠道/用户来源渠道—明细
  870. :param task_key:
  871. :return:
  872. """
  873. params = []
  874. params.extend(time_range)
  875. customer_channel_details_data = self.db.select(self.sql_3_1, params)
  876. result = []
  877. for x in customer_channel_details_data:
  878. ele = []
  879. order = self.share_way.get(x[2])
  880. if order:
  881. ele.append(x[0])
  882. ele.append(x[1])
  883. ele.append(order)
  884. ele.append(x[3])
  885. result.append(ele)
  886. result.sort(key=lambda obj: obj[0])
  887. end_data = []
  888. for key, data in groupby(result, key=lambda obj: obj[0]):
  889. others_data = []
  890. for ot in data:
  891. others_data.append([x for x in ot])
  892. lable_data = []
  893. if len(others_data) > 0:
  894. lable_data.append(others_data[0][0])
  895. lable_data.append(others_data[0][1])
  896. for i in range(1, 11):
  897. number = 0
  898. for od in others_data:
  899. if i == od[2]:
  900. number = od[3]
  901. else:
  902. pass
  903. lable_data.append(number)
  904. pass
  905. end_data.append(lable_data)
  906. # customer_channel_details_data数据结构
  907. # house_id, 项目id
  908. # house_name, 项目名称
  909. # label_wx, 分享类别
  910. # COUNT(a.id) as counts, 数量
  911. return end_data
  912. def brand_customer_channel_details(self, time_range):
  913. """
  914. 2.默认值/006_大麦(集团)/场景(集团)_用户来源渠道_v1.1/用户来源渠道—明细
  915. :param frequency:
  916. :return:
  917. """
  918. params = [time_range[0], time_range[1], time_range[0], time_range[1]]
  919. brand_customer_channel_details_data = self.db.select(self.sql_3_2, params)
  920. # brand_customer_channel_details_data数据结构
  921. # 0 brand_id, 集团id
  922. # 1 x.brand_name, 集团名称
  923. # 2 house_id, 项目id
  924. # 3 house_name, 项目名称
  925. # 4 label_wx, 分享类别
  926. # 5 COUNT(1) 数量
  927. result = []
  928. for x in brand_customer_channel_details_data:
  929. ele = []
  930. order = self.share_way.get(x[4])
  931. if order:
  932. ele.append(x[0])
  933. ele.append(x[1])
  934. ele.append(x[2])
  935. ele.append(x[3])
  936. ele.append(order)
  937. ele.append(x[5])
  938. result.append(ele)
  939. result.sort(key=lambda obj: obj[2])
  940. end_data = []
  941. for key, data in groupby(result, key=lambda obj: obj[2]):
  942. others_data = []
  943. for ot in data:
  944. others_data.append([x for x in ot])
  945. lable_data = []
  946. if len(others_data) > 0:
  947. lable_data.extend(others_data[0][0: 4])
  948. for i in range(1, 11):
  949. number = 0
  950. for od in others_data:
  951. if i == od[4]:
  952. number = od[5]
  953. else:
  954. pass
  955. lable_data.append(number)
  956. pass
  957. end_data.append(lable_data)
  958. return end_data
  959. def push_log_recording(self, push_message):
  960. """
  961. 报表推送日志记录
  962. :param push_message:
  963. :return:
  964. """
  965. self.db.add_some(self.sql_6, push_message)
  966. def get_house_ids_by_brand_id(self, brand_id):
  967. return self.db.select(self.sql_5, [brand_id])
  968. def get_brand_info_by_house_id(self, house_id):
  969. """
  970. 根据项目id或者相应的集团信息
  971. :param house_id:
  972. :return:
  973. """
  974. brand_info = self.db.select(self.sql_7, [house_id])
  975. if len(brand_info) == 1:
  976. return brand_info[0][0]
  977. return
  978. def get_brand_ids_by_house_ids(self, house_ids):
  979. brand_ids = self.db.select(self.sql_8, [house_ids])
  980. ids = []
  981. for x in brand_ids:
  982. if x and x[0]:
  983. ids.append(x[0])
  984. return ids
  985. def get_house_ids_by_brand_ids(self, brand_ids):
  986. result = []
  987. ids = self.db.select(self.sql_5_1, [brand_ids])
  988. for x in ids:
  989. if x[0] not in result:
  990. result.append(x[0])
  991. return result
  992. def get_time_range(self, task_key):
  993. """
  994. 根据定时任务id获取时间区间
  995. 时间格式 yyyy-mm-dd
  996. :param task_key:1: 日报,2:周报, 3:all
  997. :return:
  998. """
  999. now_time = datetime.datetime.now()
  1000. pre_time = None
  1001. if task_key in (2, 3):
  1002. # 上周,上周一到上周天
  1003. pre_time = now_time + datetime.timedelta(days=-7)
  1004. now_time = now_time + datetime.timedelta(days=-1)
  1005. pass
  1006. elif task_key in (1, 1):
  1007. # 昨天
  1008. pre_time = now_time + datetime.timedelta(days=-1)
  1009. now_time = now_time + datetime.timedelta(days=-1)
  1010. pass
  1011. elif task_key in (4, 4):
  1012. # 不限时间
  1013. pre_time = now_time + datetime.timedelta(days=-2999)
  1014. return [pre_time.strftime('%Y-%m-%d'), now_time.strftime('%Y-%m-%d')]
  1015. if __name__ == '__main__':
  1016. rp = ReportPush('linshi')
  1017. print(rp.get_time_range(2))