report_push.py 38 KB

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