report_push.py 42 KB

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