tongce.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. from excel_util import ExcelUtil
  2. from mysql_db import MysqlDB
  3. from itertools import groupby
  4. class TongCe:
  5. """
  6. 同策测试数据清洗
  7. """
  8. # 统计筒体结果
  9. sql_1 = '''
  10. SELECT
  11. a.sub_question_id,
  12. b.sub_question_content,
  13. a.score,
  14. b.sub_option_content,
  15. count(1)
  16. FROM
  17. f_t_daren_score_2 a
  18. LEFT JOIN d_shangju_tiku_02 b ON a. STATUS = b. STATUS = 1
  19. WHERE
  20. a.testcase_id in %s and
  21. a.testcase_id = b.testcase_id
  22. AND a.sub_question_id = b.sub_question_id
  23. AND (
  24. a.score = b.score
  25. OR a.score = b.sub_option_id
  26. )
  27. GROUP BY
  28. b.sub_question_content,
  29. a.score,
  30. b.sub_option_content
  31. '''
  32. # 选项信息
  33. sql_2 = '''
  34. SELECT
  35. b.id as question_id,
  36. b. NAME as question_title,
  37. a.id as sub_question_id,
  38. a. NAME as sub_question_title,
  39. d.id as option_id,
  40. d.content as option_title,
  41. c.id as sub_option_id,
  42. c.content as sub_option_title
  43. FROM
  44. bq_sub_question a
  45. LEFT JOIN bq_question b ON a.father_id = b.id
  46. LEFT JOIN bq_sub_option c ON a.id = c.sub_question_id
  47. LEFT JOIN bq_option d ON c.father_id = d.id
  48. WHERE
  49. FIND_IN_SET(
  50. a.id,
  51. (
  52. SELECT
  53. GROUP_CONCAT(question_ids)
  54. FROM
  55. bq_testcase
  56. WHERE
  57. house_ids = %s
  58. GROUP BY
  59. house_ids
  60. )
  61. )
  62. AND a. STATUS = b. STATUS = c. STATUS = 1
  63. ORDER BY
  64. a.id
  65. '''
  66. # 表
  67. sql_3 = '''
  68. INSERT INTO mvp_page_display_match (
  69. house_id,
  70. question_id,
  71. question_title,
  72. sub_question_id,
  73. sub_question_title,
  74. option_id,
  75. option_content,
  76. sub_option_id,
  77. sub_option_content,
  78. data_item_tab,
  79. data_item_title,
  80. data_item_name,
  81. STATUS,
  82. creator,
  83. created
  84. )
  85. VALUES
  86. (
  87. %s,
  88. %s,
  89. %s,
  90. %s,
  91. %s,
  92. %s,
  93. %s,
  94. %s,
  95. %s,
  96. %s,
  97. %s,
  98. %s,
  99. 1,
  100. 'binren',
  101. now()
  102. )
  103. '''
  104. sql_4 = '''
  105. SELECT
  106. id,
  107. sub_question_id,
  108. sub_option_id
  109. FROM
  110. mvp_page_display_match
  111. WHERE
  112. STATUS = 1
  113. '''
  114. sql_5 = '''
  115. SELECT
  116. id
  117. FROM
  118. bq_testcase
  119. WHERE
  120. STATUS = 1
  121. AND FIND_IN_SET(
  122. (
  123. SELECT
  124. id
  125. FROM
  126. bq_house
  127. WHERE
  128. STATUS = 1
  129. AND NAME = %s
  130. ),
  131. house_ids
  132. )
  133. '''
  134. sql_6 = '''
  135. insert INTO mvp_page_display_data (
  136. crowd_info_id,
  137. match_id,
  138. page_display_rule_id,
  139. name,
  140. value,
  141. STATUS,
  142. creator,
  143. created
  144. )
  145. VALUES
  146. (%s, %s, %s, %s, %s, 1, 'binren', now())
  147. '''
  148. sql_7 = '''
  149. SELECT
  150. a.testcase_id,
  151. a.uuid,
  152. GROUP_CONCAT(
  153. DISTINCT b.sub_option_content
  154. )
  155. FROM
  156. f_t_daren_score_2 a
  157. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  158. WHERE
  159. a.testcase_id IN (84, 85, 86, 87)
  160. AND b.father_id IN (47, 48, 234, 254)
  161. and a.sub_question_id = b.sub_question_id and a.testcase_id = b.testcase_id
  162. GROUP BY
  163. a.testcase_id,
  164. a.uuid
  165. '''
  166. sql_8 = '''
  167. SELECT
  168. a.uuid,
  169. a.title,
  170. a.testcase_id,
  171. b.father_id,
  172. b.father_content,
  173. b.sub_option_id,
  174. b.sub_option_content
  175. FROM
  176. f_t_daren_score_2 a
  177. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  178. WHERE
  179. a.testcase_id = b.testcase_id
  180. AND a.sub_question_id = b.sub_question_id
  181. AND a.testcase_id IN (84, 85, 86, 87)
  182. '''
  183. sql_9 = '''
  184. SELECT
  185. x.city
  186. ,x.uuid
  187. ,x.sex
  188. ,x.nld
  189. ,x.zhifuli
  190. ,x.juzhujiegou
  191. ,m.father_content
  192. ,m.father_id
  193. ,m.sub_question_id
  194. ,m.sub_question_content
  195. ,m.sub_option_id
  196. ,m.sub_option_content
  197. ,m.testcase_id
  198. ,m.title
  199. FROM
  200. (
  201. SELECT
  202. e.uuid,
  203. e.sex,
  204. f.nld,
  205. c.zhifuli,
  206. d.city,
  207. w.juzhujiegou
  208. FROM
  209. (
  210. SELECT
  211. a.testcase_id,
  212. a.uuid,
  213. b.sub_option_content AS sex
  214. FROM
  215. f_t_daren_score_2 a
  216. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  217. WHERE
  218. a.testcase_id IN (84, 85, 86, 87)
  219. AND b.father_id = 47
  220. AND a.sub_question_id = b.sub_question_id
  221. AND a.testcase_id = b.testcase_id
  222. GROUP BY
  223. a.testcase_id,
  224. a.uuid
  225. ) e
  226. LEFT JOIN (
  227. SELECT
  228. a.uuid,
  229. b.sub_option_content AS nld
  230. FROM
  231. f_t_daren_score_2 a
  232. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  233. WHERE
  234. a.testcase_id IN (84, 85, 86, 87)
  235. AND b.father_id = 48
  236. AND a.sub_question_id = b.sub_question_id
  237. AND a.testcase_id = b.testcase_id
  238. GROUP BY
  239. a.testcase_id,
  240. a.uuid
  241. ) f ON e.uuid = f.uuid
  242. LEFT JOIN (
  243. SELECT
  244. a.uuid,
  245. b.sub_option_content AS zhifuli
  246. FROM
  247. f_t_daren_score_2 a
  248. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  249. WHERE
  250. a.testcase_id IN (84, 85, 86, 87)
  251. AND b.father_id = 234
  252. AND a.sub_question_id = b.sub_question_id
  253. AND a.testcase_id = b.testcase_id
  254. GROUP BY
  255. a.testcase_id,
  256. a.uuid
  257. ) c ON f.uuid = c.uuid
  258. LEFT JOIN (
  259. SELECT
  260. a.uuid,
  261. b.sub_option_content AS city
  262. FROM
  263. f_t_daren_score_2 a
  264. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  265. WHERE
  266. a.testcase_id IN (84, 85, 86, 87)
  267. AND b.father_id = 254
  268. AND a.sub_question_id = b.sub_question_id
  269. AND a.testcase_id = b.testcase_id
  270. GROUP BY
  271. a.testcase_id,
  272. a.uuid
  273. ) d ON c.uuid = d.uuid
  274. left join (
  275. SELECT
  276. a.uuid,
  277. b.sub_option_content AS juzhujiegou
  278. FROM
  279. f_t_daren_score_2 a
  280. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  281. WHERE
  282. a.testcase_id IN (84, 85, 86, 87)
  283. AND b.father_id = 211
  284. AND a.sub_question_id = b.sub_question_id
  285. AND a.testcase_id = b.testcase_id
  286. GROUP BY
  287. a.testcase_id,
  288. a.uuid
  289. ) w on d.uuid = w.uuid
  290. ) x
  291. LEFT JOIN (
  292. SELECT
  293. a.uuid,
  294. a.title,
  295. a.testcase_id,
  296. b.father_id,
  297. b.father_content,
  298. b.sub_question_id,
  299. b.sub_question_content,
  300. b.sub_option_id,
  301. b.sub_option_content
  302. FROM
  303. f_t_daren_score_2 a
  304. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  305. WHERE
  306. a.testcase_id = b.testcase_id
  307. AND a.sub_question_id = b.sub_question_id
  308. AND a.testcase_id IN (84, 85, 86, 87)
  309. ) m ON x.uuid = m.uuid
  310. '''
  311. sql_10 = '''
  312. INSERT INTO f_t_daren_score_2 (
  313. testcase_id,
  314. title,
  315. uuid, score, created, sub_question_id
  316. )
  317. VALUE
  318. (84, '有钱人的生活就是很枯燥的……', %s, %s, %s, %s)
  319. '''
  320. sql_11 = '''
  321. select id, title_type, title_in_page, sub_question_id from mvp_page_display_rule where status = 1
  322. '''
  323. sql_12 = '''
  324. INSERT INTO mvp_page_display_rule (
  325. house_id,
  326. function_id,
  327. title_type,
  328. title_in_page,
  329. sub_question_id,
  330. STATUS,
  331. creator,
  332. created
  333. )
  334. VALUE
  335. (
  336. 67,
  337. 1,
  338. %s,
  339. %s,
  340. %s,
  341. 1,
  342. 'binren',
  343. now()
  344. )
  345. '''
  346. sql_13 = '''
  347. INSERT INTO mvp_crowd_info (
  348. house_id,
  349. pay_ability,
  350. age_area,
  351. city_name,
  352. life_cycle,
  353. STATUS,
  354. creator,
  355. created
  356. )
  357. VALUES
  358. (
  359. 67,
  360. %s,
  361. %s,
  362. %s,
  363. %s,
  364. 1,
  365. 'binren',
  366. now()
  367. )
  368. '''
  369. def __init__(self):
  370. self.shangju_db = MysqlDB('shangju')
  371. self.marketing_db = MysqlDB('bi_report')
  372. self.linshi_db = MysqlDB('linshi', db_type=1)
  373. self.options_info = ExcelUtil('工作表6', 'tongce.xlsx').read_options_info()
  374. def get_question_info_from_db(self):
  375. result = self.shangju_db.select(self.sql_2, [67])
  376. insert_data = []
  377. for rt in result:
  378. rt = list(rt)
  379. option_configuration = self.options_info.get('67' + str(rt[6]))
  380. if option_configuration and len(option_configuration) == 4:
  381. rt.insert(0, 67)
  382. rt.extend(option_configuration[0:3])
  383. insert_data.append(rt)
  384. return insert_data
  385. def get_option_match_info(self):
  386. result = self.linshi_db.select(self.sql_4)
  387. return result
  388. # 支付力:376,年龄:29,城市:377,居住结构:395。
  389. sql_14 = '''
  390. select content from bq_sub_option where sub_question_id = %s
  391. '''
  392. def insert_into_mvp_crowd_info(self):
  393. zhifuli = self.shangju_db.select(self.sql_14, [376])
  394. age = self.shangju_db.select(self.sql_14, [29])
  395. city = self.shangju_db.select(self.sql_14, [377])
  396. juzhujiegou = self.shangju_db.select(self.sql_14, [395])
  397. insert_data = []
  398. for zfl in zhifuli:
  399. for a in age:
  400. for cy in city:
  401. for jzjg in juzhujiegou:
  402. insert_data.append([zfl, a, cy, jzjg])
  403. if len(insert_data) > 0:
  404. self.linshi_db.truncate('mvp_crowd_info')
  405. self.linshi_db.add_some(self.sql_13, insert_data)
  406. sql_15 = '''
  407. select id, pay_ability, age_area, city_name, life_cycle from mvp_crowd_info where status = 1
  408. '''
  409. def get_crowd_info(self):
  410. data = self.linshi_db.select(self.sql_15)
  411. return data
  412. def insert_into_rule(self):
  413. option_info = self.options_info
  414. insert_data = []
  415. sub_question_ids = []
  416. for key in option_info.keys():
  417. data = option_info[key]
  418. if data[3] not in sub_question_ids:
  419. insert_data.append([data[0], data[1], data[3]])
  420. sub_question_ids.append(data[3])
  421. if len(insert_data) > 0:
  422. self.linshi_db.truncate('mvp_page_display_rule')
  423. self.linshi_db.add_some(self.sql_12, insert_data)
  424. def get_rule_data_info(self):
  425. data = self.linshi_db.select(self.sql_11)
  426. return data
  427. sql_16 = '''
  428. insert INTO mvp_page_display_data (
  429. crowd_info_id,
  430. match_id,
  431. value,
  432. STATUS,
  433. creator,
  434. created
  435. )
  436. VALUES
  437. (%s, %s, %s, 1, 'binren', now())
  438. '''
  439. def lingdi_data_scores(self):
  440. # 1: 写入mvp_crowd_info
  441. self.insert_into_mvp_crowd_info()
  442. crowd_info = self.get_crowd_info()
  443. # 2: 写入rule
  444. self.insert_into_rule()
  445. rule = self.get_rule_data_info()
  446. # 3: 读入答题数据
  447. self.answers = self.marketing_db.select(self.sql_9)
  448. # 4: 写入match信息
  449. match_data = self.get_question_info_from_db()
  450. self.linshi_db.truncate('mvp_page_display_match')
  451. self.linshi_db.add_some(self.sql_3, match_data)
  452. self.match_data_info = self.get_option_match_info()
  453. # 筛选写入data的数据
  454. insert_data = []
  455. for ci in crowd_info:
  456. crowd_info_id = ci[0]
  457. zhifuli = ci[1]
  458. age = ci[2]
  459. city = ci[3]
  460. juzhujiegou = ci[4]
  461. data = self.filter_people(city, age, zhifuli, juzhujiegou)
  462. data.sort(key=lambda obj: obj[0])
  463. for key, questions_data in groupby(data, key=lambda obj: obj[0]):
  464. question_data_list = []
  465. for qd in questions_data:
  466. question_data_list.append([x for x in qd])
  467. rule_id = self.get_rule_id(key, rule)
  468. if rule_id is not None:
  469. question_people = len(question_data_list)
  470. if question_people > 0:
  471. question_data_list.sort(key=lambda obj: obj[3])
  472. for option_name, option_data_1 in groupby(question_data_list, key=lambda obj: obj[3]):
  473. option_data_list = []
  474. for od in option_data_1:
  475. option_data_list.append([x for x in od])
  476. if len(option_data_list) >= 0:
  477. match_id = 0
  478. option_id = option_data_list[0][2]
  479. for md in self.match_data_info:
  480. if str(md[1]) == str(key) and str(md[2]) == str(option_id):
  481. match_id = md[0]
  482. insert_data.append([crowd_info_id, match_id, rule_id, option_name, len(option_data_list) / question_people])
  483. if len(insert_data) > 0:
  484. self.linshi_db.truncate('mvp_page_display_data')
  485. self.linshi_db.add_some(self.sql_6, insert_data)
  486. return len(insert_data)
  487. def get_rule_id(self, sub_question_id, rule):
  488. for re in rule:
  489. if str(re[3]) == str(sub_question_id):
  490. return re[0]
  491. def filter_people(self, city, age, zhifuli, juzhujiegou):
  492. result = []
  493. for answer in self.answers:
  494. if answer[0] == city and answer[3] == age and answer[4] == zhifuli and answer[5] == juzhujiegou:
  495. # 子题id, 子题题目,子选项id,子选项题目
  496. result.append([answer[8], answer[9], answer[10], answer[11]])
  497. return result
  498. def get_testcase_ids_by_house_name(self, house_name):
  499. testcase_ids = self.shangju_db.select(self.sql_5, [house_name])
  500. return testcase_ids
  501. def scores(self):
  502. testcase_ids = self.get_testcase_ids_by_house_name('同策 领地')
  503. db_data = self.marketing_db.select(self.sql_1, [testcase_ids])
  504. answer = []
  505. for data in db_data:
  506. answer.append([data[0], data[2], data[4]])
  507. answer.sort(key=lambda obj: obj[0])
  508. sub_option_score = []
  509. for sub_question_id, others in groupby(answer, key=lambda obj: obj[0]):
  510. others_data = []
  511. for ot in others:
  512. others_data.append([x for x in ot])
  513. sub_question_count = sum([x[2] for x in others_data])
  514. for td in others_data:
  515. sub_option_id = td[1]
  516. sub_option_count = td[2]
  517. rate = int(sub_option_count) / sub_question_count
  518. sub_option_score.append([sub_question_id, sub_option_id, rate])
  519. return sub_option_score
  520. def tongce(self):
  521. """
  522. tongce测试数据清洗
  523. :return:
  524. """
  525. match_data = self.get_question_info_from_db()
  526. self.linshi_db.truncate('mvp_page_display_match')
  527. self.linshi_db.add_some(self.sql_3, match_data)
  528. scores = self.scores()
  529. match_data_info = self.get_option_match_info()
  530. dispaly_data = []
  531. for score in scores:
  532. sub_question_id = score[0]
  533. sub_option_id = score[1]
  534. value = score[2]
  535. for mi in match_data_info:
  536. if str(mi[1]) == str(sub_question_id) and str(mi[2]) == str(sub_option_id):
  537. dispaly_data.append([mi[0], value])
  538. if len(dispaly_data) > 0:
  539. self.linshi_db.truncate('mvp_page_display_data')
  540. self.linshi_db.add_some(self.sql_6, dispaly_data)
  541. return {'插入数据条数': len(dispaly_data), 'scores': dispaly_data}
  542. def wenjuanxin_84(self):
  543. excel = ExcelUtil('Sheet1', '84_1500.xlsx')
  544. insert_data = excel.wenjuanxin_84()
  545. self.linshi_db.add_some(self.sql_10, insert_data)
  546. print()
  547. if __name__ == '__main__':
  548. tongce = TongCe()
  549. tongce.insert_into_rule()