tongce.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. def __init__(self):
  181. self.shangju_db = MysqlDB('shangju')
  182. self.marketing_db = MysqlDB('bi_report')
  183. self.linshi_db = MysqlDB('linshi', db_type=1)
  184. self.options_info = ExcelUtil('工作表6', 'tongce.xlsx').read_options_info()
  185. def get_question_info_from_db(self):
  186. result = self.shangju_db.select(self.sql_2, [67])
  187. insert_data = []
  188. for rt in result:
  189. rt = list(rt)
  190. option_configuration = self.options_info.get('67' + str(rt[6]))
  191. if option_configuration and len(option_configuration) == 3:
  192. rt.insert(0, 67)
  193. rt.extend(option_configuration)
  194. insert_data.append(rt)
  195. return insert_data
  196. def get_option_match_info(self):
  197. result = self.linshi_db.select(self.sql_4)
  198. return result
  199. def get_testcase_ids_by_house_name(self, house_name):
  200. testcase_ids = self.shangju_db.select(self.sql_5, [house_name])
  201. return testcase_ids
  202. def scores(self):
  203. testcase_ids = self.get_testcase_ids_by_house_name('同策 领地')
  204. db_data = self.marketing_db.select(self.sql_1, [testcase_ids])
  205. answer = []
  206. for data in db_data:
  207. answer.append([data[0], data[2], data[4]])
  208. answer.sort(key=lambda obj: obj[0])
  209. sub_option_score = []
  210. for sub_question_id, others in groupby(answer, key=lambda obj: obj[0]):
  211. others_data = []
  212. for ot in others:
  213. others_data.append([x for x in ot])
  214. sub_question_count = sum([x[2] for x in others_data])
  215. for td in others_data:
  216. sub_option_id = td[1]
  217. sub_option_count = td[2]
  218. rate = int(sub_option_count) / sub_question_count
  219. sub_option_score.append([sub_question_id, sub_option_id, rate])
  220. return sub_option_score
  221. def tongce(self):
  222. """
  223. tongce测试数据清洗
  224. :return:
  225. """
  226. match_data = self.get_question_info_from_db()
  227. self.linshi_db.truncate('mvp_page_display_match')
  228. self.linshi_db.add_some(self.sql_3, match_data)
  229. scores = self.scores()
  230. match_data_info = self.get_option_match_info()
  231. dispaly_data = []
  232. for score in scores:
  233. sub_question_id = score[0]
  234. sub_option_id = score[1]
  235. value = score[2]
  236. for mi in match_data_info:
  237. if str(mi[1]) == str(sub_question_id) and str(mi[2]) == str(sub_option_id):
  238. dispaly_data.append([mi[0], value])
  239. if len(dispaly_data) > 0:
  240. self.linshi_db.truncate('mvp_page_display_data')
  241. self.linshi_db.add_some(self.sql_6, dispaly_data)
  242. return {'插入数据条数': len(dispaly_data), 'scores': dispaly_data}
  243. # 同策答题人身份信息整理
  244. # 性别父题id: 47
  245. # 年龄父题id:48
  246. # 支付力父题id:234
  247. # 城市父题id: 254
  248. def tongce_answer_info(self):
  249. people_info = self.marketing_db.select(self.sql_7)
  250. people_dict = {}
  251. for pi in people_info:
  252. people_dict[pi[1] + str(pi[0])] = pi
  253. answers = self.marketing_db.select(self.sql_8)
  254. result = []
  255. for aw in answers:
  256. aw = list(aw)
  257. people = people_dict.get(aw[0] + str(aw[2]))
  258. if people:
  259. for pl in str(people[2]).split(','):
  260. aw.append(pl)
  261. result.append(aw)
  262. return result
  263. if __name__ == '__main__':
  264. pass