tongce.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. def __init__(self):
  146. self.shangju_db = MysqlDB('shangju')
  147. self.marketing_db = MysqlDB('bi_report')
  148. self.linshi_db = MysqlDB('linshi', db_type=1)
  149. self.options_info = ExcelUtil('工作表6', 'tongce.xlsx').read_options_info()
  150. def get_question_info_from_db(self):
  151. result = self.shangju_db.select(self.sql_2, [67])
  152. insert_data = []
  153. for rt in result:
  154. rt = list(rt)
  155. option_configuration = self.options_info.get('67' + str(rt[6]))
  156. if option_configuration and len(option_configuration) == 3:
  157. rt.insert(0, 67)
  158. rt.extend(option_configuration)
  159. insert_data.append(rt)
  160. return insert_data
  161. def get_option_match_info(self):
  162. result = self.shangju_db.select(self.sql_4)
  163. return result
  164. def get_testcase_ids_by_house_name(self, house_name):
  165. testcase_ids = self.shangju_db.select(self.sql_5, [house_name])
  166. return testcase_ids
  167. def scores(self):
  168. testcase_ids = self.get_testcase_ids_by_house_name('同策 领地')
  169. db_data = self.marketing_db.select(self.sql_1, [testcase_ids])
  170. answer = []
  171. for data in db_data:
  172. answer.append([data[0], data[2], data[4]])
  173. answer.sort(key=lambda obj: obj[0])
  174. sub_option_score = []
  175. for sub_question_id, others in groupby(answer, key=lambda obj: obj[0]):
  176. others_data = []
  177. for ot in others:
  178. others_data.append([x for x in ot])
  179. sub_question_count = sum([x[2] for x in others_data])
  180. for td in others_data:
  181. sub_option_id = td[1]
  182. sub_option_count = td[2]
  183. rate = int(sub_option_count) / sub_question_count
  184. sub_option_score.append([sub_question_id, sub_option_id, rate])
  185. return sub_option_score
  186. def tongce(self):
  187. """
  188. tongce测试数据清洗
  189. :return:
  190. """
  191. match_data = self.get_question_info_from_db()
  192. self.linshi_db.add_some(self.sql_3, match_data)
  193. scores = self.scores()
  194. match_data_info = self.get_option_match_info()
  195. dispaly_data = []
  196. for score in scores:
  197. sub_question_id = score[0]
  198. sub_option_id = score[1]
  199. value = score[2]
  200. for mi in match_data_info:
  201. if str(mi[1]) == str(sub_question_id) and str(mi[2]) == str(sub_option_id):
  202. dispaly_data.append([mi[0], value])
  203. if len(dispaly_data) > 0:
  204. self.linshi_db.add_some(self.sql_6, [dispaly_data])
  205. return {'插入数据条数': len(dispaly_data)}
  206. if __name__ == '__main__':
  207. pass