report_push.py 36 KB

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