report_push.py 42 KB

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