report_push.py 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. x.brand_id,
  252. x.house_id,
  253. x.house_name,
  254. ifnull(x.pv,0)
  255. x.uv,
  256. x.new_cust,
  257. x.shouquan_cust
  258. from (
  259. SELECT
  260. c.pv,
  261. c.uv,
  262. a.brand_id,
  263. a.house_id,
  264. a.house_name,
  265. a.brand_name,
  266. ifnull(b.house_layout_num, 0) house_layout_num,
  267. ifnull(d.launch_time, '--') launch_time,
  268. c.new_cust,
  269. c.shouquan_cust,
  270. c.revisit_cust
  271. FROM
  272. (
  273. SELECT
  274. brand_id,
  275. ifnull(house_id, '0') house_id,
  276. sum(pv) pv,
  277. count(DISTINCT brand_user_id) uv,
  278. count(
  279. DISTINCT CASE
  280. WHEN is_new_user = 1 THEN
  281. brand_user_id
  282. END
  283. ) new_cust,
  284. count(
  285. DISTINCT CASE
  286. WHEN is_shouquan_user = 1 THEN
  287. brand_user_id
  288. END
  289. ) shouquan_cust,
  290. count(
  291. DISTINCT CASE
  292. WHEN is_new_user = 0 THEN
  293. brand_user_id
  294. END
  295. ) revisit_cust
  296. FROM
  297. a_behavior_brand_mini_day
  298. WHERE
  299. report_d >= %s
  300. AND report_d <= %s
  301. GROUP BY
  302. brand_id,
  303. ifnull(house_id, '0')
  304. ) c
  305. LEFT JOIN (
  306. SELECT
  307. house_id,
  308. count(1) house_layout_num
  309. FROM
  310. d_content_layout
  311. WHERE
  312. `status` = '1'
  313. AND house_id <> 1
  314. GROUP BY
  315. house_id
  316. UNION ALL
  317. SELECT
  318. bb.brand_id house_id,
  319. count(1) house_layout_num
  320. FROM
  321. d_content_layout aa
  322. JOIN d_house bb ON aa.house_id = bb.house_id
  323. WHERE
  324. aa.`status` = '1'
  325. AND bb. STATUS = '1'
  326. AND aa.house_id <> 1
  327. GROUP BY
  328. bb.brand_id
  329. ) b ON c.house_id = b.house_id
  330. JOIN d_house a ON a.house_id = c.house_id
  331. AND a.brand_id = c.brand_id
  332. LEFT JOIN d_house_attr d ON c.house_id = d.house_id
  333. AND c.brand_id = d.brand_id ) x
  334. """
  335. # 默认值/001_大麦/场景_用户来源渠道/用户来源渠道—明细
  336. sql_3_1 = """
  337. SELECT
  338. house_id,
  339. house_name,
  340. label_wx,
  341. COUNT(a.id) as counts
  342. FROM
  343. d_user_attr a
  344. LEFT JOIN d_scene b ON a.scene = b. CODE
  345. WHERE
  346. a.source IN (1, 2, 3, 4, 10)
  347. AND a.report_d >= %s
  348. AND a.report_d <= %s
  349. GROUP BY
  350. house_id,
  351. house_name,
  352. label_wx
  353. """
  354. # 默认值/006_大麦(集团)/场景(集团)_用户来源渠道_v1.1/用户来源渠道—明细
  355. sql_3_2 = """
  356. select
  357. brand_id,
  358. x.brand_name,
  359. house_id,
  360. house_name,
  361. label_wx,
  362. COUNT(1)
  363. from
  364. (SELECT
  365. a.scene,
  366. a.brand_id,
  367. b.*, a.share_brand_customer_id,
  368. '2' adviser_agent,
  369. a.house_id house_id,
  370. c.house_name house_name,
  371. c.brand_name
  372. FROM
  373. (
  374. SELECT
  375. scene,
  376. brand_id,
  377. share_brand_customer_id,
  378. house_id
  379. FROM
  380. d_brand_app_customer
  381. WHERE
  382. created >= %s
  383. AND created < DATE_ADD(
  384. %s, INTERVAL 1 DAY
  385. )
  386. UNION ALL
  387. SELECT
  388. scene,
  389. brand_id,
  390. share_brand_customer_id,
  391. brand_id house_id
  392. FROM
  393. d_brand_app_customer
  394. WHERE
  395. created >= %s
  396. AND created < DATE_ADD(
  397. %s, INTERVAL 1 DAY
  398. )
  399. ) a
  400. LEFT JOIN d_scene b ON a.scene = b. CODE
  401. JOIN d_house c ON a.house_id = c.house_id
  402. AND a.brand_id = c.brand_id) x
  403. group by x.brand_id, x.brand_name, x.house_id, x.house_name, x.label_wx
  404. """
  405. # 根据任务id获取推送客户信息
  406. sql_4 = """
  407. select a.task_key, a.customer_id, b.customer_type, b.name, b.mail, GROUP_CONCAT(c.house_or_brand_id) as ids
  408. from report_task_info a left join report_push_customer_info b on b.id = a.customer_id
  409. left join report_customer_authority_info c on b.id = c.customer_id
  410. where a.task_key = %s and a.status = b.status = c.status = 1
  411. group by a.task_key, a.customer_id, b.customer_type, b.name, b.mail
  412. """
  413. # 根据集团id获取项目id
  414. sql_5 = """
  415. select house_id, house_name from d_house where brand_id = %s
  416. """
  417. sql_5_1 = """
  418. select house_id from d_house where brand_id in %s
  419. """
  420. sql_6 = """insert into report_push_log(name, mail, report_name, push_time, send_status, status) values(%s, %s,
  421. %s, now(), %s, 1) """
  422. # 根据项目id获取集团id和名称
  423. sql_7 = """
  424. select a.brand_id, a.brand_name from d_house a where a.house_id = %s
  425. """
  426. sql_8 = """
  427. select DISTINCT a.brand_id from d_house a where a.house_id in %s
  428. """
  429. def __init__(self, db_name):
  430. self.db = MysqlDB(db_name)
  431. mails = ['1285211525@qq.com', 'lijm@elab-plus.com', 'xuanxc@elab-plus.com']
  432. def report_push(self, task_key):
  433. message = {}
  434. try:
  435. report_data = self.report_data_query(task_key)
  436. message[1] = '数据查询成功:{}'.format(len(report_data))
  437. time_rang = self.get_time_range(task_key)
  438. xu = XlwtUtil()
  439. send_info = xu.create_excel(report_data, time_rang, task_key)
  440. message[2] = '报表文件生成成功'
  441. email_util = EmailUtil()
  442. title, content = self.get_title_content(task_key, time_rang)
  443. logs = []
  444. for value in send_info:
  445. log_data = []
  446. try:
  447. if value[2]:
  448. email_util.send_mail(title, content, self.mails, value[2])
  449. log_data = [value[0], value[1], value[2], 1]
  450. except Exception as e:
  451. log_data = [value[0], value[1], value[2], -1]
  452. print(str(e))
  453. logs.append(log_data)
  454. message[3] = '邮件发送成功'
  455. message['data'] = send_info
  456. self.db.add_some(self.sql_6, logs)
  457. self.db.close()
  458. email_util.quit_mail()
  459. FileUtil.remove_files(7, xu.save_path)
  460. except Exception as e:
  461. print(str(e))
  462. message['error'] = str(e)
  463. pass
  464. finally:
  465. return message
  466. def get_title_content(self, task_key, time_range):
  467. if task_key == 1:
  468. return '移动案场订阅日报[{}]'.format(time_range[0]), '本期间内【{}】至【{}】的数据报告已经准备完成。请点击附件查阅。'.format(time_range[0], time_range[1])
  469. elif task_key in (2, 3):
  470. return '移动案场订阅周报[{}]至[{}]'.format(time_range[0], time_range[1]), '本期间内【{}】至【{}】的数据报告已经准备完成。请点击附件查阅.'.format(time_range[0], time_range[1])
  471. def report_data_query(self, task_key):
  472. """
  473. 定时任务推送数据准备
  474. :param task_key:
  475. :return:
  476. """
  477. result = {}
  478. try:
  479. # 根据任务key获取需要推送的客户以及可以的权限
  480. customers = self.db.select(self.sql_4, [task_key])
  481. # a.task_key, a.customer_id, b.customer_type, b.`name`, b.mail, GROUP_CONCAT(c.house_or_brand_id)
  482. time_range = self.get_time_range(task_key)
  483. all_time_rang = self.get_time_range(4)
  484. # 有限时间范围内的数据
  485. xcx_top_data = self.xcx_top(time_range)
  486. brand_top_data = self.brand_top(time_range)
  487. customer_channel_details_data = self.customer_channel_details(time_range)
  488. brand_customer_channel_details = self.brand_customer_channel_details(time_range)
  489. # 所有历史数据
  490. xcx_top_data_all = self.xcx_top(all_time_rang)
  491. brand_top_data_all = self.brand_top(all_time_rang)
  492. for customer in customers:
  493. customer_data = {}
  494. name = customer[3]
  495. mail = customer[4]
  496. customer_type = customer[2]
  497. house_ids = []
  498. brand_id_list = []
  499. if customer_type == 1:
  500. # 项目
  501. ids = customer[5]
  502. if str(ids).find(',') != -1:
  503. house_ids = [x for x in str(ids).split(',')]
  504. else:
  505. house_ids = [ids]
  506. brand_id_list = self.get_brand_ids_by_house_ids(house_ids)
  507. pass
  508. elif customer_type == 2:
  509. # 集团
  510. brand_ids = customer[5]
  511. if str(brand_ids).find(',') != -1:
  512. brands = [x for x in str(brand_ids).split(',')]
  513. else:
  514. brands = [brand_ids]
  515. for id in brands:
  516. house_ids.extend([x[0] for x in self.get_house_ids_by_brand_id(id)])
  517. brand_id_list = brands
  518. result_data_1 = []
  519. result_data_2 = []
  520. result_data_3 = []
  521. result_data_4 = []
  522. result_data_5 = []
  523. result_data_7 = []
  524. result_data_8 = []
  525. all_house_ids = self.get_house_ids_by_brand_ids(brand_id_list)
  526. xcx_top_data_part = self.filter_by_house_ids(xcx_top_data, all_house_ids)
  527. brand_top_data_part = self.filter_by_brand_ids(brand_top_data, brand_id_list)
  528. xcx_top_data_all_part = self.filter_by_house_ids(xcx_top_data_all, all_house_ids)
  529. brand_top_data_all_part = self.filter_by_brand_ids(brand_top_data_all, brand_id_list)
  530. # 1 数据总览 12个统计指标
  531. data_overview = self.data_overview(time_range, house_ids, xcx_top_data_part, brand_top_data_part)
  532. result_data_1.extend(data_overview)
  533. # 4:单个项目小程序数据排行榜
  534. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  535. for index, x in enumerate(xcx_top_data_part):
  536. if x[0] in house_ids:
  537. result_data_4.append([index, x[1], x[2], x[3], x[4], x[5]])
  538. # 5: 集团项目数据排行榜
  539. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  540. if customer_type == 2:
  541. for index, x2 in enumerate(brand_top_data_part):
  542. if x2[1] in house_ids or x2[0] in brand_id_list:
  543. result_data_5.append([index, x2[2], x2[3], x2[4], x2[5], x2[6]])
  544. pass
  545. pass
  546. elif customer_type == 1:
  547. for index, x1 in enumerate(brand_top_data_part):
  548. if x1[1] in house_ids:
  549. result_data_5.append([index, x1[2], x1[3], x1[4], x1[5], x1[6]])
  550. else:
  551. pass
  552. # 2: 项目数据排行榜
  553. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  554. house_with_brand_data = self.house_with_brand(xcx_top_data_part, brand_top_data_part)
  555. for x in house_with_brand_data:
  556. if x[2] in house_ids:
  557. x.pop(1)
  558. x.pop(1)
  559. result_data_2.append(x)
  560. # 3: 项目历史累计总数
  561. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  562. if customer_type == 2:
  563. all_data_history = self.house_with_brand(xcx_top_data_all_part, brand_top_data_all_part, brand_id_list)
  564. for index, x in enumerate(all_data_history):
  565. if x[2] in house_ids or x[1] in brand_id_list:
  566. x.pop(1)
  567. x.pop(1)
  568. result_data_3.append(x)
  569. else:
  570. all_data_history = self.house_with_brand(xcx_top_data_all_part, brand_top_data_all_part)
  571. for index, x in enumerate(all_data_history):
  572. if x[2] in house_ids:
  573. x.pop(1)
  574. x.pop(1)
  575. result_data_3.append(x)
  576. # 7: 单个项目小程序获客来源场景分析
  577. # 项目 合计 长按识别二维码 会话 公众号菜单 公众号文章 小程序历史列表 扫一扫二维码 搜索 相册选取二维码 其他小程序 其他
  578. for x in customer_channel_details_data:
  579. if x[0] in house_ids:
  580. result_data_7.append(x)
  581. # 8: 集团项目获客来源场景分析
  582. if customer_type == 2:
  583. for x in brand_customer_channel_details:
  584. if x[2] in house_ids or x[0] in brand_id_list:
  585. result_data_8.append(x)
  586. elif customer_type == 1:
  587. for x in brand_customer_channel_details:
  588. if x[2] in house_ids:
  589. result_data_8.append(x)
  590. # 6: 项目获客来源场景分析
  591. result_data_6 = self.house_with_brand_for_share(result_data_7, result_data_8)
  592. customer_data[1] = result_data_1
  593. result_data_2.sort(key=lambda obj: obj[0])
  594. customer_data[2] = result_data_2
  595. result_data_3.sort(key=lambda obj: obj[0])
  596. customer_data[3] = result_data_3
  597. result_data_4.sort(key=lambda obj: obj[0])
  598. customer_data[4] = result_data_4
  599. result_data_5.sort(key=lambda obj: obj[0])
  600. customer_data[5] = result_data_5
  601. self.sort(result_data_6, 1)
  602. customer_data[6] = result_data_6
  603. result_data_7_format = []
  604. for x in result_data_7:
  605. house_name = x[1]
  606. ele = [house_name]
  607. data = x[2:]
  608. total = sum(data)
  609. ele.append(total)
  610. ele.extend(data)
  611. result_data_7_format.append(ele)
  612. self.sort(result_data_7_format, 1)
  613. customer_data[7] = result_data_7_format
  614. result_data_8_format = []
  615. for x in result_data_8:
  616. ele = []
  617. house_name = x[3]
  618. data = x[4:]
  619. total = sum(data)
  620. ele.append(house_name)
  621. ele.append(total)
  622. ele.extend(data)
  623. result_data_8_format.append(ele)
  624. self.sort(result_data_8_format, 1)
  625. customer_data[8] = result_data_8_format
  626. customer_data[0] = mail
  627. result[name] = customer_data
  628. # return result
  629. except Exception as e:
  630. result['error'] = str(e)
  631. pass
  632. finally:
  633. return result
  634. def sort(self, data, idnex):
  635. data.sort(key=lambda obj: obj[idnex])
  636. data.reverse()
  637. def house_with_brand_for_share(self, house_data, brand_data):
  638. house_ids = []
  639. result = []
  640. for x in house_data:
  641. if x[0] not in [a[0] for a in house_ids]:
  642. house_ids.append([x[0], x[1]])
  643. for x in brand_data:
  644. if x[2] not in [a[0] for a in house_ids]:
  645. house_ids.append([x[2], x[3]])
  646. for id in house_ids:
  647. house_id = id[0]
  648. house_name = id[1]
  649. house_result = [house_name]
  650. equal_house = self.equal_by_house_id(house_data, house_id, 0)
  651. equal_brand = self.equal_by_house_id(brand_data, house_id, 2)
  652. house_result_part = []
  653. if equal_house and equal_brand:
  654. equal_house = equal_house[2:]
  655. equal_brand = equal_brand[4:]
  656. for i in range(0, 10):
  657. house_result_part.append(self.add(equal_house[i], equal_brand[i]))
  658. elif equal_house and not equal_brand:
  659. equal_house = equal_house[2:]
  660. for i in range(0, 10):
  661. house_result_part.append(equal_house[i])
  662. elif not equal_house and equal_brand:
  663. equal_brand = equal_brand[4:]
  664. for i in range(0, 10):
  665. house_result_part.append(equal_brand[i])
  666. house_result.append(sum(house_result_part))
  667. house_result.extend(house_result_part)
  668. result.append(house_result)
  669. result.sort(key=lambda obj: obj[1])
  670. result.reverse()
  671. return result
  672. def equal_by_house_id(self, data, house_id, index):
  673. for x in data:
  674. if x[index] == house_id:
  675. return x
  676. def filter_by_brand_ids(self, data, brand_ids):
  677. result = []
  678. for x in data:
  679. if x[0] in brand_ids:
  680. result.append(x)
  681. result.sort(key=lambda obj: obj[3])
  682. result.reverse()
  683. return result
  684. def filter_by_house_ids(self, data, house_ids):
  685. result = []
  686. for x in data:
  687. if x[0] in house_ids:
  688. result.append(x)
  689. result.sort(key=lambda obj: obj[2])
  690. result.reverse()
  691. return result
  692. def data_overview(self, time_range, house_ids, xcx_top_data, brand_top_data):
  693. """
  694. 统计数据总览
  695. :param time_range:
  696. :param house_ids:
  697. :return:
  698. """
  699. result = []
  700. # 1:总浏览量
  701. data_1_1 = self.db.select(self.sql_1_1, [time_range[0], time_range[1], house_ids])
  702. number_1_1 = data_1_1[0][0]
  703. data_1_2 = self.db.select(self.sql_1_2, [time_range[0], time_range[1], house_ids])
  704. number_1_2 = data_1_2[0][0]
  705. number_1 = self.add(number_1_1, number_1_2)
  706. result.append(number_1)
  707. # 2: 总浏览人数
  708. 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])
  709. number_2 = data_2[0][0]
  710. result.append(number_2)
  711. # 3:新增获客
  712. 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])
  713. number_3 = data_3[0][0]
  714. result.append(number_3)
  715. # 4:新增获电
  716. 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]])
  717. number_4 = data_4[0][0]
  718. result.append(number_4)
  719. xcx_top_data_part = [x for x in xcx_top_data if x[0] in house_ids]
  720. brand_top_data_part = [x for x in brand_top_data if x[1] in house_ids]
  721. # 5 6 7 8
  722. number_5 = 0
  723. number_6 = 0
  724. number_7 = 0
  725. number_8 = 0
  726. for x in brand_top_data_part:
  727. number_5 = self.add(number_5, x[3])
  728. number_6 = self.add(number_6, x[4])
  729. number_7 = self.add(number_7, x[5])
  730. number_8 = self.add(number_8, x[6])
  731. result.append(number_5)
  732. result.append(number_6)
  733. result.append(number_7)
  734. result.append(number_8)
  735. # 9 10 11 12
  736. number_9 = 0
  737. number_10 = 0
  738. number_11 = 0
  739. number_12 = 0
  740. for x in xcx_top_data_part:
  741. number_9 = self.add(number_9, x[2])
  742. number_10 = self.add(number_10, x[3])
  743. number_11 = self.add(number_11, x[4])
  744. number_12 = self.add(number_12, x[5])
  745. result.append(number_9)
  746. result.append(number_10)
  747. result.append(number_11)
  748. result.append(number_12)
  749. return result
  750. def house_with_brand(self, xcx_top_data, brand_top_data, brands=None):
  751. """
  752. 项目数据和集团数据的求和
  753. :param brands:
  754. :param xcx_top_data:
  755. :param brand_top_data:
  756. :return:
  757. """
  758. result = []
  759. house_ids = []
  760. for x in xcx_top_data:
  761. if x[0] not in house_ids:
  762. house_ids.append(x[0])
  763. for x in brand_top_data:
  764. if x[1] not in house_ids and x[1] is not None and int(x[1]) > 300:
  765. house_ids.append(x[1])
  766. for house_id in house_ids:
  767. a = []
  768. a_order = 0
  769. for index, x in enumerate(xcx_top_data):
  770. if str(house_id) == str(x[0]):
  771. a.extend(x)
  772. a_order = index + 1
  773. b = []
  774. b_order = 0
  775. for index, y in enumerate(brand_top_data):
  776. if str(house_id) == str(y[1]):
  777. b.extend(y)
  778. b_order = index + 1
  779. order = b_order if b_order > 0 else a_order
  780. if len(a) > 0 and len(b) > 0:
  781. 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])])
  782. elif len(a) > 0 and len(b) == 0:
  783. _a = [order, 1]
  784. for x in a:
  785. _a.append(x)
  786. result.append(_a)
  787. elif len(a) == 0 and len(b) > 0:
  788. _b = [order]
  789. _b.extend(b)
  790. result.append(_b)
  791. else:
  792. pass
  793. if brands:
  794. for index, x in enumerate(brand_top_data):
  795. if x[0] in brands and x[1] is not None and int(x[1]) < 300:
  796. _x = [index]
  797. _x.extend(x)
  798. result.append(_x)
  799. result.sort(key=lambda obj: obj[3])
  800. result.reverse()
  801. return result
  802. def add(self, a=None, b=None):
  803. if a and b:
  804. return a + b
  805. elif a and not b:
  806. return a
  807. elif b and not a:
  808. return b
  809. return 0
  810. def xcx_top(self, time_range):
  811. """
  812. 获取 1.默认值/001_大麦/项目排行榜/小程序排行榜TOP_N
  813. :return:
  814. """
  815. params = []
  816. params.extend(time_range)
  817. params.extend(time_range)
  818. params.extend(time_range)
  819. params.extend(time_range)
  820. xcx_top_data = self.db.select(self.sql_2_1, params)
  821. result = []
  822. for x in xcx_top_data:
  823. result.append([n for n in x])
  824. # xcx_top_data的结果结构
  825. # 0 a.house_id, 项目id
  826. # 1 a.house_name, 项目名称
  827. # 2 SUM(a.pv), 浏览总量
  828. # 3 SUM(a.uv), 浏览人数
  829. # 4 SUM(a.new_cust_num), 新增获客
  830. # 5 SUM(a.wx_num) 授权手机号
  831. result.sort(key=lambda obj: obj[2])
  832. result.reverse()
  833. return result
  834. def brand_top(self, time_range):
  835. """
  836. 2.默认值/006_大麦(集团)/集团项目排行榜v1.3/集团排行榜
  837. :param task_key:
  838. :return:
  839. """
  840. params = [time_range[0], time_range[1]]
  841. brand_top_data = self.db.select(self.sql_2_2, params)
  842. result = []
  843. for x in brand_top_data:
  844. result.append([n for n in x])
  845. # brand_top_data结果的结构
  846. # 0 a.brand_id, 集团id
  847. # 1 a.house_id, 项目id
  848. # 2 a.house_name, 项目名称
  849. # 3 SUM(a.pv), 浏览总量
  850. # 4 SUM(a.uv), 浏览人数
  851. # 5 SUM(a.new_cust), 新增获客
  852. # 6 SUM(a.shouquan_cust) 授权手机号
  853. result.sort(key=lambda obj: obj[3])
  854. result.reverse()
  855. return result
  856. """
  857. 数据分享类别
  858. 1:长按识别二维码 2:会话 3:公众号菜单 4:公众号文章 5:小程序历史列表 6:扫一扫二维码
  859. 7:搜索 8:相册选取二维码 9:其他小程序 10:其他
  860. """
  861. share_way = {
  862. "长按识别二维码": 1,
  863. "会话": 2,
  864. "公众号菜单": 3,
  865. '公众号文章': 4,
  866. '小程序历史列表': 5,
  867. '扫一扫二维码': 6,
  868. '搜索': 7,
  869. '相册选取二维码': 8,
  870. '其他小程序': 9,
  871. '其他': 10
  872. }
  873. def customer_channel_details(self, time_range):
  874. """
  875. 1.默认值/001_大麦/场景_用户来源渠道/用户来源渠道—明细
  876. :param task_key:
  877. :return:
  878. """
  879. params = []
  880. params.extend(time_range)
  881. customer_channel_details_data = self.db.select(self.sql_3_1, params)
  882. result = []
  883. for x in customer_channel_details_data:
  884. ele = []
  885. order = self.share_way.get(x[2])
  886. if order:
  887. ele.append(x[0])
  888. ele.append(x[1])
  889. ele.append(order)
  890. ele.append(x[3])
  891. result.append(ele)
  892. result.sort(key=lambda obj: obj[0])
  893. end_data = []
  894. for key, data in groupby(result, key=lambda obj: obj[0]):
  895. others_data = []
  896. for ot in data:
  897. others_data.append([x for x in ot])
  898. lable_data = []
  899. if len(others_data) > 0:
  900. lable_data.append(others_data[0][0])
  901. lable_data.append(others_data[0][1])
  902. for i in range(1, 11):
  903. number = 0
  904. for od in others_data:
  905. if i == od[2]:
  906. number = od[3]
  907. else:
  908. pass
  909. lable_data.append(number)
  910. pass
  911. end_data.append(lable_data)
  912. # customer_channel_details_data数据结构
  913. # house_id, 项目id
  914. # house_name, 项目名称
  915. # label_wx, 分享类别
  916. # COUNT(a.id) as counts, 数量
  917. return end_data
  918. def brand_customer_channel_details(self, time_range):
  919. """
  920. 2.默认值/006_大麦(集团)/场景(集团)_用户来源渠道_v1.1/用户来源渠道—明细
  921. :param frequency:
  922. :return:
  923. """
  924. params = [time_range[0], time_range[1], time_range[0], time_range[1]]
  925. brand_customer_channel_details_data = self.db.select(self.sql_3_2, params)
  926. # brand_customer_channel_details_data数据结构
  927. # 0 brand_id, 集团id
  928. # 1 x.brand_name, 集团名称
  929. # 2 house_id, 项目id
  930. # 3 house_name, 项目名称
  931. # 4 label_wx, 分享类别
  932. # 5 COUNT(1) 数量
  933. result = []
  934. for x in brand_customer_channel_details_data:
  935. ele = []
  936. order = self.share_way.get(x[4])
  937. if order:
  938. ele.append(x[0])
  939. ele.append(x[1])
  940. ele.append(x[2])
  941. ele.append(x[3])
  942. ele.append(order)
  943. ele.append(x[5])
  944. result.append(ele)
  945. result.sort(key=lambda obj: obj[2])
  946. end_data = []
  947. for key, data in groupby(result, key=lambda obj: obj[2]):
  948. others_data = []
  949. for ot in data:
  950. others_data.append([x for x in ot])
  951. lable_data = []
  952. if len(others_data) > 0:
  953. lable_data.extend(others_data[0][0: 4])
  954. for i in range(1, 11):
  955. number = 0
  956. for od in others_data:
  957. if i == od[4]:
  958. number = od[5]
  959. else:
  960. pass
  961. lable_data.append(number)
  962. pass
  963. end_data.append(lable_data)
  964. return end_data
  965. def push_log_recording(self, push_message):
  966. """
  967. 报表推送日志记录
  968. :param push_message:
  969. :return:
  970. """
  971. self.db.add_some(self.sql_6, push_message)
  972. def get_house_ids_by_brand_id(self, brand_id):
  973. return self.db.select(self.sql_5, [brand_id])
  974. def get_brand_info_by_house_id(self, house_id):
  975. """
  976. 根据项目id或者相应的集团信息
  977. :param house_id:
  978. :return:
  979. """
  980. brand_info = self.db.select(self.sql_7, [house_id])
  981. if len(brand_info) == 1:
  982. return brand_info[0][0]
  983. return
  984. def get_brand_ids_by_house_ids(self, house_ids):
  985. brand_ids = self.db.select(self.sql_8, [house_ids])
  986. ids = []
  987. for x in brand_ids:
  988. if x and x[0]:
  989. ids.append(x[0])
  990. return ids
  991. def get_house_ids_by_brand_ids(self, brand_ids):
  992. result = []
  993. ids = self.db.select(self.sql_5_1, [brand_ids])
  994. for x in ids:
  995. if x[0] not in result:
  996. result.append(x[0])
  997. return result
  998. def get_time_range(self, task_key):
  999. """
  1000. 根据定时任务id获取时间区间
  1001. 时间格式 yyyy-mm-dd
  1002. :param task_key:1: 日报,2:周报, 3:all
  1003. :return:
  1004. """
  1005. now_time = datetime.datetime.now()
  1006. pre_time = None
  1007. if task_key in (2, 3):
  1008. # 上周,上周一到上周天
  1009. pre_time = now_time + datetime.timedelta(days=-7)
  1010. now_time = now_time + datetime.timedelta(days=-1)
  1011. pass
  1012. elif task_key in (1, 1):
  1013. # 昨天
  1014. pre_time = now_time + datetime.timedelta(days=-1)
  1015. now_time = now_time + datetime.timedelta(days=-1)
  1016. pass
  1017. elif task_key in (4, 4):
  1018. # 不限时间
  1019. pre_time = now_time + datetime.timedelta(days=-2999)
  1020. return [pre_time.strftime('%Y-%m-%d'), now_time.strftime('%Y-%m-%d')]
  1021. if __name__ == '__main__':
  1022. rp = ReportPush('linshi')
  1023. print(rp.get_time_range(2))