report_push.py 40 KB

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