report_push.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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', 'liutt@elab-plus.com', 'plf@centralchina.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. # for mail in self.mails:
  455. result = email_util.send_mail_by_admin(title, content, value[1], value[2], value[3])
  456. if result:
  457. log_data = [value[0], value[1], value[2], 1, 'success!!!']
  458. else:
  459. result = email_util.send_mail_by_admin(title, content, value[1], value[2], value[3])
  460. if not result:
  461. log_data = [value[0], value[1], value[2], -1, '第二次发送失败']
  462. except Exception as e:
  463. log_data = [value[0], value[1], value[2], -1, str(e)]
  464. print(str(e))
  465. logs.append(log_data)
  466. message[3] = '邮件发送成功'
  467. message['data'] = send_info
  468. self.db.add_some(self.sql_6, logs)
  469. self.db.close()
  470. FileUtil.remove_files(7, xu.save_path)
  471. except Exception as e:
  472. print(str(e))
  473. message['error'] = str(e)
  474. pass
  475. finally:
  476. return message
  477. def report_push_test(self, task_key):
  478. message = {}
  479. try:
  480. report_data = self.report_data_query(task_key)
  481. message[1] = '数据查询成功:{}'.format(len(report_data))
  482. time_rang = self.get_time_range(task_key)
  483. xu = XlwtUtil()
  484. send_info = xu.create_excel(report_data, time_rang, task_key)
  485. message[2] = '报表文件生成成功'
  486. email_util = EmailUtil()
  487. title, content = self.get_title_content(task_key, time_rang)
  488. logs = []
  489. for value in send_info:
  490. log_data = []
  491. try:
  492. if value[2]:
  493. for mail in self.mails:
  494. result = email_util.send_mail_by_admin(title, content, mail, value[2], value[3])
  495. if result:
  496. log_data = [value[0], value[1], value[2], 1, 'success!!!']
  497. else:
  498. result = email_util.send_mail_by_admin(title, content, mail, value[2], value[3])
  499. if not result:
  500. log_data = [value[0], value[1], value[2], -1, '第二次发送失败']
  501. except Exception as e:
  502. log_data = [value[0], value[1], value[2], -1, str(e)]
  503. print(str(e))
  504. logs.append(log_data)
  505. message[3] = '邮件发送成功'
  506. message['data'] = send_info
  507. self.db.add_some(self.sql_6, logs)
  508. self.db.close()
  509. FileUtil.remove_files(7, xu.save_path)
  510. except Exception as e:
  511. print(str(e))
  512. message['error'] = str(e)
  513. pass
  514. finally:
  515. return message
  516. def get_title_content(self, task_key, time_range):
  517. if task_key in (1, 4):
  518. return '移动案场订阅日报[{}]'.format(time_range[0]), '本期间内【{}】至【{}】的数据报告已经准备完成。请点击附件查阅。'.format(time_range[0], time_range[1])
  519. elif task_key in (2, 3):
  520. return '移动案场订阅周报[{}]至[{}]'.format(time_range[0], time_range[1]), '本期间内【{}】至【{}】的数据报告已经准备完成。请点击附件查阅.'.format(time_range[0], time_range[1])
  521. def report_data_query(self, task_key):
  522. """
  523. 定时任务推送数据准备
  524. :param task_key:
  525. :return:
  526. """
  527. result = {}
  528. try:
  529. # 根据任务key获取需要推送的客户以及可以的权限
  530. customers = self.db.select(self.sql_4, [task_key])
  531. # a.task_key, a.customer_id, b.customer_type, b.`name`, b.mail, GROUP_CONCAT(c.house_or_brand_id)
  532. time_range = self.get_time_range(task_key)
  533. all_time_rang = self.get_time_range(9999)
  534. # 有限时间范围内的数据
  535. xcx_top_data = self.xcx_top(time_range)
  536. brand_top_data = self.brand_top(time_range)
  537. customer_channel_details_data = self.customer_channel_details(time_range)
  538. brand_customer_channel_details = self.brand_customer_channel_details(time_range)
  539. # 所有历史数据
  540. xcx_top_data_all = self.xcx_top(all_time_rang)
  541. brand_top_data_all = self.brand_top(all_time_rang)
  542. for customer in customers:
  543. customer_data = {}
  544. name = customer[3]
  545. mail = customer[4]
  546. customer_type = customer[2]
  547. house_ids = []
  548. brand_id_list = []
  549. if customer_type == 1:
  550. # 项目
  551. ids = customer[5]
  552. if str(ids).find(',') != -1:
  553. house_ids = [x for x in str(ids).split(',')]
  554. else:
  555. house_ids = [ids]
  556. brand_id_list = self.get_brand_ids_by_house_ids(house_ids)
  557. pass
  558. elif customer_type == 2:
  559. # 集团
  560. brand_ids = customer[5]
  561. if str(brand_ids).find(',') != -1:
  562. brands = [x for x in str(brand_ids).split(',')]
  563. else:
  564. brands = [brand_ids]
  565. for id in brands:
  566. house_ids.extend([x[0] for x in self.get_house_ids_by_brand_id(id)])
  567. brand_id_list = brands
  568. result_data_1 = []
  569. result_data_2 = []
  570. result_data_3 = []
  571. result_data_4 = []
  572. result_data_5 = []
  573. result_data_7 = []
  574. result_data_8 = []
  575. all_house_ids = self.get_house_ids_by_brand_ids(brand_id_list)
  576. xcx_top_data_part = self.filter_by_house_ids(xcx_top_data, all_house_ids)
  577. brand_top_data_part = self.filter_by_brand_ids(brand_top_data, brand_id_list)
  578. xcx_top_data_all_part = self.filter_by_house_ids(xcx_top_data_all, all_house_ids)
  579. brand_top_data_all_part = self.filter_by_brand_ids(brand_top_data_all, brand_id_list)
  580. # 1 数据总览 12个统计指标
  581. data_overview = self.data_overview(time_range, house_ids, xcx_top_data_part, brand_top_data_part)
  582. result_data_1.extend(data_overview)
  583. # 4:单个项目小程序数据排行榜
  584. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  585. for index, x in enumerate(xcx_top_data_part):
  586. if x[0] in house_ids:
  587. result_data_4.append([index + 1, x[1], x[2], x[3], x[4], x[5]])
  588. # 5: 集团项目数据排行榜
  589. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  590. if customer_type == 2:
  591. for index, x2 in enumerate(brand_top_data_part):
  592. if x2[1] in house_ids or x2[0] in brand_id_list:
  593. result_data_5.append([index + 1, x2[2], x2[3], x2[4], x2[5], x2[6]])
  594. pass
  595. pass
  596. elif customer_type == 1:
  597. for index, x1 in enumerate(brand_top_data_part):
  598. if x1[1] in house_ids:
  599. result_data_5.append([index + 1, x1[2], x1[3], x1[4], x1[5], x1[6]])
  600. else:
  601. pass
  602. # 2: 项目数据排行榜
  603. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  604. house_with_brand_data = self.house_with_brand(xcx_top_data_part, brand_top_data_part)
  605. for x in house_with_brand_data:
  606. if x[2] in house_ids:
  607. x.pop(1)
  608. x.pop(1)
  609. result_data_2.append(x)
  610. # 3: 项目历史累计总数
  611. # 排名 项目名称 总浏览量 总浏览人数 新增获客 新增获电
  612. if customer_type == 2:
  613. all_data_history = self.house_with_brand(xcx_top_data_all_part, brand_top_data_all_part, brand_id_list)
  614. for x in all_data_history:
  615. if x[2] in house_ids:
  616. x.pop(1)
  617. x.pop(1)
  618. result_data_3.append(x)
  619. else:
  620. all_data_history = self.house_with_brand(xcx_top_data_all_part, brand_top_data_all_part)
  621. for x in all_data_history:
  622. if x[2] in house_ids:
  623. x.pop(1)
  624. x.pop(1)
  625. result_data_3.append(x)
  626. # 7: 单个项目小程序获客来源场景分析
  627. # 项目 合计 长按识别二维码 会话 公众号菜单 公众号文章 小程序历史列表 扫一扫二维码 搜索 相册选取二维码 其他小程序 其他
  628. for x in customer_channel_details_data:
  629. if x[0] in house_ids:
  630. result_data_7.append(x)
  631. # 8: 集团项目获客来源场景分析
  632. if customer_type == 2:
  633. for x in brand_customer_channel_details:
  634. if x[2] in house_ids or x[0] in brand_id_list:
  635. result_data_8.append(x)
  636. elif customer_type == 1:
  637. for x in brand_customer_channel_details:
  638. if x[2] in house_ids:
  639. result_data_8.append(x)
  640. # 6: 项目获客来源场景分析
  641. result_data_6 = self.house_with_brand_for_share(result_data_7, result_data_8)
  642. customer_data[1] = result_data_1
  643. result_data_2.sort(key=lambda obj: obj[0])
  644. customer_data[2] = result_data_2
  645. result_data_3.sort(key=lambda obj: obj[0])
  646. customer_data[3] = result_data_3
  647. result_data_4.sort(key=lambda obj: obj[0])
  648. customer_data[4] = result_data_4
  649. result_data_5.sort(key=lambda obj: obj[0])
  650. customer_data[5] = result_data_5
  651. self.sort(result_data_6, 1)
  652. customer_data[6] = result_data_6
  653. result_data_7_format = []
  654. for x in result_data_7:
  655. house_name = x[1]
  656. ele = [house_name]
  657. data = x[2:]
  658. total = sum(data)
  659. ele.append(total)
  660. ele.extend(data)
  661. result_data_7_format.append(ele)
  662. self.sort(result_data_7_format, 1)
  663. customer_data[7] = result_data_7_format
  664. result_data_8_format = []
  665. for x in result_data_8:
  666. ele = []
  667. house_name = x[3]
  668. data = x[4:]
  669. total = sum(data)
  670. ele.append(house_name)
  671. ele.append(total)
  672. ele.extend(data)
  673. result_data_8_format.append(ele)
  674. self.sort(result_data_8_format, 1)
  675. customer_data[8] = result_data_8_format
  676. customer_data[0] = mail
  677. result[name] = customer_data
  678. # return result
  679. except Exception as e:
  680. result['error'] = str(e)
  681. pass
  682. finally:
  683. return result
  684. def sort(self, data, idnex):
  685. data.sort(key=lambda obj: obj[idnex])
  686. data.reverse()
  687. def house_with_brand_for_share(self, house_data, brand_data):
  688. house_ids = []
  689. result = []
  690. for x in house_data:
  691. if x[0] not in [a[0] for a in house_ids]:
  692. house_ids.append([x[0], x[1]])
  693. for x in brand_data:
  694. if x[2] not in [a[0] for a in house_ids]:
  695. house_ids.append([x[2], x[3]])
  696. for id in house_ids:
  697. house_id = id[0]
  698. house_name = id[1]
  699. house_result = [house_name]
  700. equal_house = self.equal_by_house_id(house_data, house_id, 0)
  701. equal_brand = self.equal_by_house_id(brand_data, house_id, 2)
  702. house_result_part = []
  703. if equal_house and equal_brand:
  704. equal_house = equal_house[2:]
  705. equal_brand = equal_brand[4:]
  706. for i in range(0, 10):
  707. house_result_part.append(self.add(equal_house[i], equal_brand[i]))
  708. elif equal_house and not equal_brand:
  709. equal_house = equal_house[2:]
  710. for i in range(0, 10):
  711. house_result_part.append(equal_house[i])
  712. elif not equal_house and equal_brand:
  713. equal_brand = equal_brand[4:]
  714. for i in range(0, 10):
  715. house_result_part.append(equal_brand[i])
  716. house_result.append(sum(house_result_part))
  717. house_result.extend(house_result_part)
  718. result.append(house_result)
  719. result.sort(key=lambda obj: obj[1])
  720. result.reverse()
  721. return result
  722. def equal_by_house_id(self, data, house_id, index):
  723. for x in data:
  724. if x[index] == house_id:
  725. return x
  726. def filter_by_brand_ids(self, data, brand_ids):
  727. result = []
  728. for x in data:
  729. if x[0] in brand_ids:
  730. result.append(x)
  731. result.sort(key=lambda obj: obj[3])
  732. result.reverse()
  733. return result
  734. def filter_by_house_ids(self, data, house_ids):
  735. result = []
  736. for x in data:
  737. if x[0] in house_ids:
  738. result.append(x)
  739. result.sort(key=lambda obj: obj[2])
  740. result.reverse()
  741. return result
  742. def data_overview(self, time_range, house_ids, xcx_top_data, brand_top_data):
  743. """
  744. 统计数据总览
  745. :param time_range:
  746. :param house_ids:
  747. :return:
  748. """
  749. result = []
  750. # 1:总浏览量
  751. data_1_1 = self.db.select(self.sql_1_1, [time_range[0], time_range[1], house_ids])
  752. number_1_1 = data_1_1[0][0]
  753. data_1_2 = self.db.select(self.sql_1_2, [time_range[0], time_range[1], house_ids])
  754. number_1_2 = data_1_2[0][0]
  755. number_1 = self.add(number_1_1, number_1_2)
  756. result.append(number_1)
  757. # 2: 总浏览人数
  758. 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])
  759. number_2 = data_2[0][0]
  760. result.append(number_2)
  761. # 3:新增获客
  762. time_1 = time_range[0] + ' 00:00:00'
  763. time_2 = time_range[1] + ' 23:59:59'
  764. data_3 = self.db.select(self.sql_1_4, [time_1, time_2, house_ids, time_1, time_2, house_ids])
  765. number_3 = data_3[0][0]
  766. result.append(number_3)
  767. # 4:新增获电
  768. data_4 = self.db.select(self.sql_1_5, [time_1, time_2, house_ids, house_ids, time_1, time_2])
  769. number_4 = data_4[0][0]
  770. result.append(number_4)
  771. xcx_top_data_part = [x for x in xcx_top_data if x[0] in house_ids]
  772. brand_top_data_part = [x for x in brand_top_data if x[1] in house_ids]
  773. # 5 6 7 8
  774. number_5 = 0
  775. number_6 = 0
  776. number_7 = 0
  777. number_8 = 0
  778. for x in brand_top_data_part:
  779. number_5 = self.add(number_5, x[3])
  780. number_6 = self.add(number_6, x[4])
  781. number_7 = self.add(number_7, x[5])
  782. number_8 = self.add(number_8, x[6])
  783. result.append(number_5)
  784. result.append(number_6)
  785. result.append(number_7)
  786. result.append(number_8)
  787. # 9 10 11 12
  788. number_9 = 0
  789. number_10 = 0
  790. number_11 = 0
  791. number_12 = 0
  792. for x in xcx_top_data_part:
  793. number_9 = self.add(number_9, x[2])
  794. number_10 = self.add(number_10, x[3])
  795. number_11 = self.add(number_11, x[4])
  796. number_12 = self.add(number_12, x[5])
  797. result.append(number_9)
  798. result.append(number_10)
  799. result.append(number_11)
  800. result.append(number_12)
  801. return result
  802. def house_with_brand(self, xcx_top_data, brand_top_data, brands=None):
  803. """
  804. 项目数据和集团数据的求和
  805. :param brands:
  806. :param xcx_top_data:
  807. :param brand_top_data:
  808. :return:
  809. """
  810. result = []
  811. house_ids = []
  812. for x in xcx_top_data:
  813. if x[0] not in house_ids:
  814. house_ids.append(x[0])
  815. for x in brand_top_data:
  816. if x[1] not in house_ids and x[1] is not None and int(x[1]) > 300:
  817. house_ids.append(x[1])
  818. for house_id in house_ids:
  819. a = []
  820. a_order = 0
  821. for index, x in enumerate(xcx_top_data):
  822. if str(house_id) == str(x[0]):
  823. a.extend(x)
  824. a_order = index + 1
  825. b = []
  826. b_order = 0
  827. for index, y in enumerate(brand_top_data):
  828. if str(house_id) == str(y[1]):
  829. b.extend(y)
  830. b_order = index + 1
  831. order = b_order if b_order > 0 else a_order
  832. if len(a) > 0 and len(b) > 0:
  833. 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])])
  834. elif len(a) > 0 and len(b) == 0:
  835. _a = [order, 1]
  836. for x in a:
  837. _a.append(x)
  838. result.append(_a)
  839. elif len(a) == 0 and len(b) > 0:
  840. _b = [order]
  841. _b.extend(b)
  842. result.append(_b)
  843. else:
  844. pass
  845. if brands:
  846. for index, x in enumerate(brand_top_data):
  847. if x[0] in brands and x[1] is not None and int(x[1]) == 0:
  848. _x = [index + 1]
  849. _x.extend(x)
  850. result.append(_x)
  851. result.sort(key=lambda obj: obj[3])
  852. result.reverse()
  853. return result
  854. def add(self, a=None, b=None):
  855. if a and b:
  856. return a + b
  857. elif a and not b:
  858. return a
  859. elif b and not a:
  860. return b
  861. return 0
  862. def xcx_top(self, time_range):
  863. """
  864. 获取 1.默认值/001_大麦/项目排行榜/小程序排行榜TOP_N
  865. :return:
  866. """
  867. params = []
  868. params.extend(time_range)
  869. params.extend(time_range)
  870. params.extend(time_range)
  871. params.extend(time_range)
  872. xcx_top_data = self.db.select(self.sql_2_1, params)
  873. result = []
  874. for x in xcx_top_data:
  875. result.append([n for n in x])
  876. # xcx_top_data的结果结构
  877. # 0 a.house_id, 项目id
  878. # 1 a.house_name, 项目名称
  879. # 2 SUM(a.pv), 浏览总量
  880. # 3 SUM(a.uv), 浏览人数
  881. # 4 SUM(a.new_cust_num), 新增获客
  882. # 5 SUM(a.wx_num) 授权手机号
  883. result.sort(key=lambda obj: obj[2])
  884. result.reverse()
  885. return result
  886. def brand_top(self, time_range):
  887. """
  888. 2.默认值/006_大麦(集团)/集团项目排行榜v1.3/集团排行榜
  889. :param task_key:
  890. :return:
  891. """
  892. params = [time_range[0], time_range[1]]
  893. brand_top_data = self.db.select(self.sql_2_2, params)
  894. result = []
  895. for x in brand_top_data:
  896. result.append([n for n in x])
  897. # brand_top_data结果的结构
  898. # 0 a.brand_id, 集团id
  899. # 1 a.house_id, 项目id
  900. # 2 a.house_name, 项目名称
  901. # 3 SUM(a.pv), 浏览总量
  902. # 4 SUM(a.uv), 浏览人数
  903. # 5 SUM(a.new_cust), 新增获客
  904. # 6 SUM(a.shouquan_cust) 授权手机号
  905. result.sort(key=lambda obj: obj[3])
  906. result.reverse()
  907. return result
  908. """
  909. 数据分享类别
  910. 1:长按识别二维码 2:会话 3:公众号菜单 4:公众号文章 5:小程序历史列表 6:扫一扫二维码
  911. 7:搜索 8:相册选取二维码 9:其他小程序 10:其他
  912. """
  913. share_way = {
  914. "长按识别二维码": 1,
  915. "会话": 2,
  916. "公众号菜单": 3,
  917. '公众号文章': 4,
  918. '小程序历史列表': 5,
  919. '扫一扫二维码': 6,
  920. '搜索': 7,
  921. '相册选取二维码': 8,
  922. '其他小程序': 9,
  923. '其他': 10
  924. }
  925. def customer_channel_details(self, time_range):
  926. """
  927. 1.默认值/001_大麦/场景_用户来源渠道/用户来源渠道—明细
  928. :param task_key:
  929. :return:
  930. """
  931. params = []
  932. params.extend(time_range)
  933. customer_channel_details_data = self.db.select(self.sql_3_1, params)
  934. result = []
  935. for x in customer_channel_details_data:
  936. ele = []
  937. order = self.share_way.get(x[2])
  938. if order:
  939. ele.append(x[0])
  940. ele.append(x[1])
  941. ele.append(order)
  942. ele.append(x[3])
  943. result.append(ele)
  944. result.sort(key=lambda obj: obj[0])
  945. end_data = []
  946. for key, data in groupby(result, key=lambda obj: obj[0]):
  947. others_data = []
  948. for ot in data:
  949. others_data.append([x for x in ot])
  950. lable_data = []
  951. if len(others_data) > 0:
  952. lable_data.append(others_data[0][0])
  953. lable_data.append(others_data[0][1])
  954. for i in range(1, 11):
  955. number = 0
  956. for od in others_data:
  957. if i == od[2]:
  958. number = od[3]
  959. else:
  960. pass
  961. lable_data.append(number)
  962. pass
  963. end_data.append(lable_data)
  964. # customer_channel_details_data数据结构
  965. # house_id, 项目id
  966. # house_name, 项目名称
  967. # label_wx, 分享类别
  968. # COUNT(a.id) as counts, 数量
  969. return end_data
  970. def brand_customer_channel_details(self, time_range):
  971. """
  972. 2.默认值/006_大麦(集团)/场景(集团)_用户来源渠道_v1.1/用户来源渠道—明细
  973. :param frequency:
  974. :return:
  975. """
  976. params = [time_range[0], time_range[1], time_range[0], time_range[1]]
  977. brand_customer_channel_details_data = self.db.select(self.sql_3_2, params)
  978. # brand_customer_channel_details_data数据结构
  979. # 0 brand_id, 集团id
  980. # 1 x.brand_name, 集团名称
  981. # 2 house_id, 项目id
  982. # 3 house_name, 项目名称
  983. # 4 label_wx, 分享类别
  984. # 5 COUNT(1) 数量
  985. result = []
  986. for x in brand_customer_channel_details_data:
  987. ele = []
  988. order = self.share_way.get(x[4])
  989. if order:
  990. ele.append(x[0])
  991. ele.append(x[1])
  992. ele.append(x[2])
  993. ele.append(x[3])
  994. ele.append(order)
  995. ele.append(x[5])
  996. result.append(ele)
  997. result.sort(key=lambda obj: obj[2])
  998. end_data = []
  999. for key, data in groupby(result, key=lambda obj: obj[2]):
  1000. others_data = []
  1001. for ot in data:
  1002. others_data.append([x for x in ot])
  1003. lable_data = []
  1004. if len(others_data) > 0:
  1005. lable_data.extend(others_data[0][0: 4])
  1006. for i in range(1, 11):
  1007. number = 0
  1008. for od in others_data:
  1009. if i == od[4]:
  1010. number = od[5]
  1011. else:
  1012. pass
  1013. lable_data.append(number)
  1014. pass
  1015. end_data.append(lable_data)
  1016. return end_data
  1017. def push_log_recording(self, push_message):
  1018. """
  1019. 报表推送日志记录
  1020. :param push_message:
  1021. :return:
  1022. """
  1023. self.db.add_some(self.sql_6, push_message)
  1024. def get_house_ids_by_brand_id(self, brand_id):
  1025. return self.db.select(self.sql_5, [brand_id])
  1026. def get_brand_info_by_house_id(self, house_id):
  1027. """
  1028. 根据项目id或者相应的集团信息
  1029. :param house_id:
  1030. :return:
  1031. """
  1032. brand_info = self.db.select(self.sql_7, [house_id])
  1033. if len(brand_info) == 1:
  1034. return brand_info[0][0]
  1035. return
  1036. def get_brand_ids_by_house_ids(self, house_ids):
  1037. brand_ids = self.db.select(self.sql_8, [house_ids])
  1038. ids = []
  1039. for x in brand_ids:
  1040. if x and x[0]:
  1041. ids.append(x[0])
  1042. return ids
  1043. def get_house_ids_by_brand_ids(self, brand_ids):
  1044. result = []
  1045. ids = self.db.select(self.sql_5_1, [brand_ids])
  1046. for x in ids:
  1047. if x[0] not in result:
  1048. result.append(x[0])
  1049. return result
  1050. def get_time_range(self, task_key):
  1051. """
  1052. 根据定时任务id获取时间区间
  1053. 时间格式 yyyy-mm-dd
  1054. :param task_key:1: 日报,2:周报, 3:all
  1055. :return:
  1056. """
  1057. now_time = datetime.datetime.now()
  1058. pre_time = None
  1059. if task_key in (2, 3):
  1060. # 上周,上周一到上周天
  1061. pre_time = now_time + datetime.timedelta(days=-7)
  1062. now_time = now_time + datetime.timedelta(days=-1)
  1063. pass
  1064. elif task_key in (1, 4):
  1065. # 昨天
  1066. pre_time = now_time + datetime.timedelta(days=-1)
  1067. now_time = now_time + datetime.timedelta(days=-1)
  1068. pass
  1069. elif task_key in (9999, 9999):
  1070. # 不限时间
  1071. pre_time = now_time + datetime.timedelta(days=-2999)
  1072. return [pre_time.strftime('%Y-%m-%d'), now_time.strftime('%Y-%m-%d')]
  1073. if __name__ == '__main__':
  1074. rp = ReportPush('linshi')
  1075. times = rp.get_time_range(1)