init_price_option_factor.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # import pymysql
  2. # DB = dict(
  3. # host="127.0.0.1",
  4. # user="root",
  5. # password="root",
  6. # database="recycle",
  7. # charset="utf8mb4"
  8. # )
  9. # GROUP_MAP = {
  10. # "账号情况": "account",
  11. # "机身外观": "body",
  12. # "屏幕外观": "screen_appearance",
  13. # "屏幕显示": "screen_display",
  14. # "触摸": "touch",
  15. # "拍摄": "camera",
  16. # "WiFi/蓝牙": "wifi",
  17. # "通话": "call",
  18. # "面容/指纹": "biometrics",
  19. # "其他功能问题(可多选或不选)": "other_function",
  20. # "整机维修(可多选或不选)": "repair",
  21. # "转轴情况": "hinge",
  22. # }
  23. # SPECIAL_OPTION_IDS = {
  24. # 100560, # 已开启丢失模式
  25. # }
  26. # # `repair_level` tinyint DEFAULT '0' COMMENT '维修分级:0非维修 1次要 2重要 3核心',
  27. # repair_level_map = {
  28. # "core": 3,
  29. # "important": 2,
  30. # "minor": 1
  31. # }
  32. # REPAIR_LEVEL_MAP = {
  33. # # core
  34. # 100384: repair_level_map["core"],
  35. # # important
  36. # 100346: repair_level_map["important"],
  37. # 100530: repair_level_map["important"],
  38. # 100382: repair_level_map["important"],
  39. # 100383: repair_level_map["important"],
  40. # # minor
  41. # 100381: repair_level_map["minor"],
  42. # 100410: repair_level_map["minor"],
  43. # 100529: repair_level_map["minor"],
  44. # }
  45. # def calc_severity(factor: float) -> int:
  46. # if factor >= 0.40:
  47. # return 3
  48. # if factor >= 0.20:
  49. # return 2
  50. # if factor > 0:
  51. # return 1
  52. # return 0
  53. # def main():
  54. # conn = pymysql.connect(**DB)
  55. # cur = conn.cursor(pymysql.cursors.DictCursor)
  56. # # 取出所有 option + factor + 维度名称
  57. # cur.execute("""
  58. # SELECT
  59. # f.id,
  60. # f.option_id,
  61. # f.factor,
  62. # o.option_key_name
  63. # FROM price_option_factor f
  64. # LEFT JOIN release_option o
  65. # ON f.option_id = o.option_id
  66. # """)
  67. # rows = cur.fetchall()
  68. # print("rows:", len(rows))
  69. # for r in rows:
  70. # option_id = int(r["option_id"])
  71. # factor = float(r["factor"] or 0)
  72. # option_key_name = r["option_key_name"]
  73. # group_code = GROUP_MAP.get(option_key_name, "other")
  74. # severity_level = calc_severity(factor)
  75. # # 次瑕疵折扣系数(统一先给 0.4)
  76. # sub_weight = 0.4
  77. # is_special = 1 if option_id in SPECIAL_OPTION_IDS else 0
  78. # repair_level = REPAIR_LEVEL_MAP.get(option_id)
  79. # cur.execute("""
  80. # UPDATE price_option_factor
  81. # SET
  82. # group_code = %s,
  83. # severity_level = %s,
  84. # sub_weight = %s,
  85. # is_special = %s,
  86. # repair_level = %s
  87. # WHERE id = %s
  88. # """, (
  89. # group_code,
  90. # severity_level,
  91. # sub_weight,
  92. # is_special,
  93. # repair_level,
  94. # r["id"]
  95. # ))
  96. # conn.commit()
  97. # cur.close()
  98. # conn.close()
  99. # print("done.")
  100. import pymysql
  101. conn = pymysql.connect(
  102. host="127.0.0.1",
  103. user="root",
  104. password="root",
  105. database="recycle",
  106. charset="utf8mb4",
  107. cursorclass=pymysql.cursors.DictCursor
  108. )
  109. GROUP_MAP = {
  110. "boot": ["开机情况"],
  111. "body": ["机身外观"],
  112. # 屏幕综合组 = 外观 + 显示 + 触摸
  113. "screen": ["屏幕外观", "屏幕显示", "触摸"],
  114. "account": ["账号情况"],
  115. "function": ["WiFi/蓝牙", "面容/指纹", "拍摄", "通话"],
  116. "hinge": ["转轴情况"],
  117. "repair": ["整机维修(可多选或不选)"],
  118. "other_special": ["其他功能问题(可多选或不选)"]
  119. }
  120. repair_level_map = {
  121. "core": 3,
  122. "important": 2,
  123. "minor": 1
  124. }
  125. def get_group_code(row):
  126. for g, keys in GROUP_MAP.items():
  127. if row["option_key_name"] in keys:
  128. return g
  129. return None
  130. def calc_severity(factor):
  131. if factor is None:
  132. return 0
  133. f = float(factor)
  134. if f <= 0:
  135. return 0
  136. elif f < 0.06:
  137. return 1
  138. elif f < 0.12:
  139. return 2
  140. elif f < 0.25:
  141. return 3
  142. elif f < 0.4:
  143. return 4
  144. else:
  145. return 5
  146. def calc_repair_level(option_name):
  147. if "无维修" in option_name:
  148. return 0
  149. if "主板" in option_name:
  150. return "core"
  151. if "屏幕维修" in option_name:
  152. return "important"
  153. if "更换原厂屏" in option_name:
  154. return "important"
  155. if "摄像头" in option_name:
  156. return "important"
  157. if "电池" in option_name:
  158. return "minor"
  159. if "后壳" in option_name:
  160. return "minor"
  161. if "配件" in option_name:
  162. return "minor"
  163. return None
  164. def is_special(row):
  165. return row["option_key_name"] == "其他功能问题(可多选或不选)"
  166. def main():
  167. with conn.cursor() as cur:
  168. cur.execute("""
  169. SELECT
  170. r.option_id,
  171. r.option_name,
  172. r.option_key_name,
  173. p.factor
  174. FROM release_option r
  175. JOIN price_option_factor p
  176. ON r.option_id = p.option_id
  177. """)
  178. rows = cur.fetchall()
  179. for r in rows:
  180. group_code = get_group_code(r)
  181. severity = calc_severity(r["factor"])
  182. sub_weight = 0.5 if group_code in ("screen", "body", "function", "hinge") else None
  183. special = 1 if is_special(r) else 0
  184. repair_level = None
  185. if group_code == "repair":
  186. repair_level = calc_repair_level(r["option_name"])
  187. if repair_level:
  188. repair_level = repair_level_map[repair_level]
  189. cur.execute("""
  190. UPDATE price_option_factor
  191. SET
  192. group_code = %s,
  193. severity_level = %s,
  194. sub_weight = %s,
  195. is_special = %s,
  196. repair_level = %s
  197. WHERE option_id = %s
  198. """, (
  199. group_code,
  200. severity,
  201. sub_weight,
  202. special,
  203. repair_level,
  204. r["option_id"]
  205. ))
  206. conn.commit()
  207. print("done.")
  208. if __name__ == "__main__":
  209. main()