report_push.py 40 KB

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