report_push.py 28 KB

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