tongce.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. match_id,
  137. value,
  138. STATUS,
  139. creator,
  140. created
  141. )
  142. VALUES
  143. (%s, %s, 1, 'binren', now())
  144. '''
  145. sql_7 = '''
  146. SELECT
  147. a.testcase_id,
  148. a.uuid,
  149. GROUP_CONCAT(
  150. DISTINCT b.sub_option_content
  151. )
  152. FROM
  153. f_t_daren_score_2 a
  154. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  155. WHERE
  156. a.testcase_id IN (84, 85, 86, 87)
  157. AND b.father_id IN (47, 48, 234, 254)
  158. and a.sub_question_id = b.sub_question_id and a.testcase_id = b.testcase_id
  159. GROUP BY
  160. a.testcase_id,
  161. a.uuid
  162. '''
  163. sql_8 = '''
  164. SELECT
  165. a.uuid,
  166. a.title,
  167. a.testcase_id,
  168. b.father_id,
  169. b.father_content,
  170. b.sub_option_id,
  171. b.sub_option_content
  172. FROM
  173. f_t_daren_score_2 a
  174. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  175. WHERE
  176. a.testcase_id = b.testcase_id
  177. AND a.sub_question_id = b.sub_question_id
  178. AND a.testcase_id IN (84, 85, 86, 87)
  179. '''
  180. sql_9 = '''
  181. SELECT
  182. x.city
  183. ,x.uuid
  184. ,x.sex
  185. ,x.nld
  186. ,x.zhifuli
  187. ,m.father_content
  188. ,m.father_id
  189. ,m.sub_question_id
  190. ,m.sub_question_content
  191. ,m.sub_option_id
  192. ,m.sub_option_content
  193. ,m.testcase_id
  194. ,m.title
  195. FROM
  196. (
  197. SELECT
  198. e.uuid,
  199. e.sex,
  200. f.nld,
  201. c.zhifuli,
  202. d.city
  203. FROM
  204. (
  205. SELECT
  206. a.testcase_id,
  207. a.uuid,
  208. b.sub_option_content AS sex
  209. FROM
  210. f_t_daren_score_2 a
  211. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  212. WHERE
  213. a.testcase_id IN (84, 85, 86, 87)
  214. AND b.father_id IN (47)
  215. AND a.sub_question_id = b.sub_question_id
  216. AND a.testcase_id = b.testcase_id
  217. GROUP BY
  218. a.testcase_id,
  219. a.uuid
  220. ) e
  221. LEFT JOIN (
  222. SELECT
  223. a.uuid,
  224. b.sub_option_content AS nld
  225. FROM
  226. f_t_daren_score_2 a
  227. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  228. WHERE
  229. a.testcase_id IN (84, 85, 86, 87)
  230. AND b.father_id IN (48)
  231. AND a.sub_question_id = b.sub_question_id
  232. AND a.testcase_id = b.testcase_id
  233. GROUP BY
  234. a.testcase_id,
  235. a.uuid
  236. ) f ON e.uuid = f.uuid
  237. LEFT JOIN (
  238. SELECT
  239. a.uuid,
  240. b.sub_option_content AS zhifuli
  241. FROM
  242. f_t_daren_score_2 a
  243. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  244. WHERE
  245. a.testcase_id IN (84, 85, 86, 87)
  246. AND b.father_id IN (234)
  247. AND a.sub_question_id = b.sub_question_id
  248. AND a.testcase_id = b.testcase_id
  249. GROUP BY
  250. a.testcase_id,
  251. a.uuid
  252. ) c ON f.uuid = c.uuid
  253. LEFT JOIN (
  254. SELECT
  255. a.uuid,
  256. b.sub_option_content AS city
  257. FROM
  258. f_t_daren_score_2 a
  259. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  260. WHERE
  261. a.testcase_id IN (84, 85, 86, 87)
  262. AND b.father_id IN (254)
  263. AND a.sub_question_id = b.sub_question_id
  264. AND a.testcase_id = b.testcase_id
  265. GROUP BY
  266. a.testcase_id,
  267. a.uuid
  268. ) d ON c.uuid = d.uuid
  269. ) x
  270. LEFT JOIN (
  271. SELECT
  272. a.uuid,
  273. a.title,
  274. a.testcase_id,
  275. b.father_id,
  276. b.father_content,
  277. b.sub_question_id,
  278. b.sub_question_content,
  279. b.sub_option_id,
  280. b.sub_option_content
  281. FROM
  282. f_t_daren_score_2 a
  283. LEFT JOIN d_shangju_tiku_02 b ON a.score = b.sub_option_id
  284. WHERE
  285. a.testcase_id = b.testcase_id
  286. AND a.sub_question_id = b.sub_question_id
  287. AND a.testcase_id IN (84, 85, 86, 87)
  288. ) m ON x.uuid = m.uuid
  289. '''
  290. sql_10 = '''
  291. INSERT INTO f_t_daren_score_2 (
  292. testcase_id,
  293. title,
  294. uuid, score, created, sub_question_id
  295. )
  296. VALUE
  297. (84, '有钱人的生活就是很枯燥的……', %s, %s, %s, %s)
  298. '''
  299. def __init__(self):
  300. # self.shangju_db = MysqlDB('shangju')
  301. # self.marketing_db = MysqlDB('bi_report')
  302. self.linshi_db = MysqlDB('linshi', db_type=1)
  303. # self.options_info = ExcelUtil('工作表6', 'tongce.xlsx').read_options_info()
  304. def get_question_info_from_db(self):
  305. result = self.shangju_db.select(self.sql_2, [67])
  306. insert_data = []
  307. for rt in result:
  308. rt = list(rt)
  309. option_configuration = self.options_info.get('67' + str(rt[6]))
  310. if option_configuration and len(option_configuration) == 3:
  311. rt.insert(0, 67)
  312. rt.extend(option_configuration)
  313. insert_data.append(rt)
  314. return insert_data
  315. def get_option_match_info(self):
  316. result = self.linshi_db.select(self.sql_4)
  317. return result
  318. def get_testcase_ids_by_house_name(self, house_name):
  319. testcase_ids = self.shangju_db.select(self.sql_5, [house_name])
  320. return testcase_ids
  321. def scores(self):
  322. testcase_ids = self.get_testcase_ids_by_house_name('同策 领地')
  323. db_data = self.marketing_db.select(self.sql_1, [testcase_ids])
  324. answer = []
  325. for data in db_data:
  326. answer.append([data[0], data[2], data[4]])
  327. answer.sort(key=lambda obj: obj[0])
  328. sub_option_score = []
  329. for sub_question_id, others in groupby(answer, key=lambda obj: obj[0]):
  330. others_data = []
  331. for ot in others:
  332. others_data.append([x for x in ot])
  333. sub_question_count = sum([x[2] for x in others_data])
  334. for td in others_data:
  335. sub_option_id = td[1]
  336. sub_option_count = td[2]
  337. rate = int(sub_option_count) / sub_question_count
  338. sub_option_score.append([sub_question_id, sub_option_id, rate])
  339. return sub_option_score
  340. def tongce(self):
  341. """
  342. tongce测试数据清洗
  343. :return:
  344. """
  345. match_data = self.get_question_info_from_db()
  346. self.linshi_db.truncate('mvp_page_display_match')
  347. self.linshi_db.add_some(self.sql_3, match_data)
  348. scores = self.scores()
  349. match_data_info = self.get_option_match_info()
  350. dispaly_data = []
  351. for score in scores:
  352. sub_question_id = score[0]
  353. sub_option_id = score[1]
  354. value = score[2]
  355. for mi in match_data_info:
  356. if str(mi[1]) == str(sub_question_id) and str(mi[2]) == str(sub_option_id):
  357. dispaly_data.append([mi[0], value])
  358. if len(dispaly_data) > 0:
  359. self.linshi_db.truncate('mvp_page_display_data')
  360. self.linshi_db.add_some(self.sql_6, dispaly_data)
  361. return {'插入数据条数': len(dispaly_data), 'scores': dispaly_data}
  362. def wenjuanxin_84(self):
  363. excel = ExcelUtil('Sheet1', 'wenjuanxing.xlsx')
  364. insert_data = excel.wenjuanxin_84()
  365. self.linshi_db.add_some(self.sql_10, insert_data)
  366. print()
  367. if __name__ == '__main__':
  368. tongce = TongCe()
  369. tongce.wenjuanxin_84()