pboc_hc.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import json
  2. def renhang_rules(json_data):
  3. '''解析json数据,计算规则命中'''
  4. ###null值转换函数
  5. def toFloat(value):
  6. if str(value)=="" or str(value)=="--" or str(value)=="nan":
  7. return 0
  8. else:
  9. return float(value)
  10. def null2Zero(value):
  11. if value==None:
  12. return 0
  13. else:
  14. return toFloat(value)
  15. #如果人行未查得,直接返回固定错误码和结果
  16. if json_data['result'] == '' or json_data['result'] == None:
  17. output_data = json.dumps({'base_rules_hit_list':['PBOC1001'], 'base_rules_hit_cnt':1, 'ex_rules_hit_list':[], 'ex_rules_hit_cnt':0,
  18. 'base_quota':0, 'ex_quota':0,'creditcard_usedquota_avg_rec6m':None,'all_account_usage':None,'all_account_od_times_rec12m':None,'creditcard_total_creditquota':None,'unrevo_loan_total_creditquota':None,'revo_loan_total_creditquota':None,'creditcard_usedquota':None,'unrevo_loan_total_balance':None,'revo_loan_total_balance':None,'creditcard_repayment_ratio':None})
  19. return output_data
  20. else:
  21. ###取数,将json字符串解码为python可处理对象
  22. info = json.loads(json_data['result'])
  23. ###输出10个原始字段
  24. creditcard_usedquota_avg_rec6m = info['信贷交易授信及负债信息概要']['贷记卡最近6个月平均使用额度']
  25. all_account_usage = info['使用率']['全账户使用率(已用额度/授信总额)']
  26. all_account_od_times_rec12m = info['24期还款状态']['全账户近12月逾期期数大于或等于“1”的次数']
  27. creditcard_total_creditquota = info['信贷交易授信及负债信息概要']['贷记卡授信总额']
  28. unrevo_loan_total_creditquota = info['信贷交易授信及负债信息概要']['非循环贷账户授信总额']
  29. revo_loan_total_creditquota = info['信贷交易授信及负债信息概要']['循环贷账户授信总额']
  30. creditcard_usedquota = info['信贷交易授信及负债信息概要']['贷记卡已用额度']
  31. unrevo_loan_total_balance = info['信贷交易授信及负债信息概要']['非循环贷账户余额']
  32. revo_loan_total_balance = info['信贷交易授信及负债信息概要']['循环贷账户余额']
  33. creditcard_repayment_ratio = info['贷记卡账户']['贷记卡还款比例']
  34. ###统计规则命中
  35. ###基础规则命中集
  36. base_rules_hit_list = []
  37. #PBOC1000 贷记卡授信总额、非循环贷账户授信总额、循环贷账户授信总额都为0
  38. rule = 'PBOC1000'
  39. if null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额']) == 0 and null2Zero(info['信贷交易授信及负债信息概要']['非循环贷账户授信总额']) == 0 and null2Zero(info['信贷交易授信及负债信息概要']['循环贷账户授信总额']) == 0:
  40. base_rules_hit_list.append(rule)
  41. #PBOC0001 呆账信息汇总-账户数>0
  42. rule = 'PBOC0001'
  43. if null2Zero(info['呆账信息汇总']['账户数']) > 0:
  44. base_rules_hit_list.append(rule)
  45. #PBOC0002 贷记卡账户出现锁定码G
  46. rule = 'PBOC0002'
  47. if null2Zero(info['24期还款状态']['贷记卡账户近24个月是否出现\"G\"']) > 0:
  48. base_rules_hit_list.append(rule)
  49. #PBOC0003 贷记卡账户当前逾期1期及以上且当前总逾期金额>300元
  50. rule = 'PBOC0003'
  51. if null2Zero(info['贷记卡账户']['当前贷记卡最大逾期期数']) >=1 and null2Zero(info['贷记卡账户']['当前贷记卡最大逾期金额']) > 300:
  52. base_rules_hit_list.append(rule)
  53. #PBOC0004 个贷账户状态为逾期
  54. rule = 'PBOC0004'
  55. if null2Zero(info['贷款信息']['当前贷款逾期账户数']) > 0:
  56. base_rules_hit_list.append(rule)
  57. #PBOC0005 贷款账户出现锁定码G或Z
  58. rule = 'PBOC0005'
  59. if null2Zero(info['24期还款状态']['贷款账户近24个月是否出现\"G\"']) > 0 or null2Zero(info['24期还款状态']['贷款账户近24个月是否出现\"Z\"']) > 0:
  60. base_rules_hit_list.append(rule)
  61. #PBOC0006 单张贷记卡近12个月(含)最高逾期期数>=2期
  62. rule = 'PBOC0006'
  63. if null2Zero(info['贷记卡账户']['近12月贷记卡最大逾期期数']) >= 2:
  64. base_rules_hit_list.append(rule)
  65. #PBOC0007 单张贷记卡近24个月(含)最大逾期期数>=3期
  66. rule = 'PBOC0007'
  67. if null2Zero(info['贷记卡账户']['近24月贷记卡最大逾期期数']) >= 3:
  68. base_rules_hit_list.append(rule)
  69. #PBOC0008 贷记卡近6个月累计逾期次数≥3次
  70. rule = 'PBOC0008'
  71. if null2Zero(info['24期还款状态']['贷记卡账户近6月逾期期数大于或等于“1”的次数']) >= 3:
  72. base_rules_hit_list.append(rule)
  73. #PBOC0009 贷记卡近12个月累计逾期次数≥6次
  74. rule = 'PBOC0009'
  75. if null2Zero(info['24期还款状态']['贷记卡账户近12月逾期期数大于或等于“1”的次数']) >= 6:
  76. base_rules_hit_list.append(rule)
  77. #PBOC0010 贷记卡近24个月累计逾期次数≥9次
  78. rule = 'PBOC0010'
  79. if null2Zero(info['24期还款状态']['贷记卡账户近24月逾期期数大于或等于“1”的次数']) >= 9:
  80. base_rules_hit_list.append(rule)
  81. #PBOC0011 贷款近12个月(含)最高逾期期数>=2期
  82. rule = 'PBOC0011'
  83. if null2Zero(info['贷款信息']['近12月贷款的最大逾期期数']) >= 2:
  84. base_rules_hit_list.append(rule)
  85. #PBOC0012 贷款近24个月(含)最高逾期期数>=3期
  86. rule = 'PBOC0012'
  87. if null2Zero(info['贷款信息']['近24月贷款的最大逾期期数']) >= 3:
  88. base_rules_hit_list.append(rule)
  89. #PBOC0013 所有个贷近12个月(含)累计逾期次数>=3次
  90. rule = 'PBOC0013'
  91. if null2Zero(info['24期还款状态']['贷款账户近12月逾期期数大于或等于“1”的次数']) >= 3:
  92. base_rules_hit_list.append(rule)
  93. #PBOC0014 所有个贷近24个月(含)逾期次数累计>=5次
  94. rule = 'PBOC0014'
  95. if null2Zero(info['24期还款状态']['贷款账户近24月逾期期数大于或等于“1”的次数']) >= 5:
  96. base_rules_hit_list.append(rule)
  97. #PBOC0015 所有个贷和贷记卡过去12个月累计逾期次数>8次
  98. rule = 'PBOC0015'
  99. if null2Zero(info['24期还款状态']['贷款账户近12月逾期期数大于或等于“1”的次数']) + null2Zero(info['24期还款状态']['贷记卡账户近12月逾期期数大于或等于“1”的次数']) > 8:
  100. base_rules_hit_list.append(rule)
  101. #PBOC0016 用户所有账户发生不良特殊交易的次数统计>0
  102. rule = 'PBOC0016'
  103. if null2Zero(info['特殊交易']['用户所有帐户发生不良特殊交易的次数统计']) > 0:
  104. base_rules_hit_list.append(rule)
  105. #PBOC0017 被追偿账户总数>0
  106. rule = 'PBOC0017'
  107. if null2Zero(info['被追偿信息']['被追偿账户总数']) > 0:
  108. base_rules_hit_list.append(rule)
  109. #PBOC0018 资产处置业务账户数>0
  110. rule = 'PBOC0018'
  111. if null2Zero(info['信贷交易违约信息概要']['资产处置业务账户数']) > 0:
  112. base_rules_hit_list.append(rule)
  113. #PBOC0019 垫款业务账户数>0
  114. rule = 'PBOC0019'
  115. if null2Zero(info['信贷交易违约信息概要']['垫款业务账户数']) > 0:
  116. base_rules_hit_list.append(rule)
  117. #PBOC0020 债权转移时属于催收状态的账户数>0
  118. rule = 'PBOC0020'
  119. if null2Zero(info['信贷交易违约信息概要']['债权转移时属于催收状态的账户数']) > 0:
  120. base_rules_hit_list.append(rule)
  121. #PBOC0021 强制执行信息-记录数>0
  122. rule = 'PBOC0021'
  123. if null2Zero(info['公共信息概要']['强制执行信息-记录数']) > 0:
  124. base_rules_hit_list.append(rule)
  125. #PBOC0022 过去6个月贷款及贷记卡总逾期次数>=2& 过去6个月贷款和贷记卡审批查询次数>=6次
  126. rule = 'PBOC0022'
  127. if null2Zero(info['24期还款状态']['贷款账户近6月逾期期数大于或等于“1”的次数']) + null2Zero(info['24期还款状态']['贷记卡账户近6月逾期期数大于或等于“1”的次数']) >= 2 and null2Zero(info['信贷审批查询记录明细']['近6月查询次数信用卡审批']) + null2Zero(info['信贷审批查询记录明细']['近6月查询次数贷款审批']) >= 6:
  128. base_rules_hit_list.append(rule)
  129. #PBOC0023 当前贷记卡透支比例超过85%
  130. rule = 'PBOC0023'
  131. if null2Zero(info['贷记卡账户']['贷记卡账户当前总额度使用率']) > 0.85:
  132. base_rules_hit_list.append(rule)
  133. #PBOC0024 近6个月(含)信用卡审批总查询次数>=5次且无新开信用卡账户
  134. rule = 'PBOC0024'
  135. if null2Zero(info['信贷审批查询记录明细']['近6月查询次数信用卡审批']) >= 5 and null2Zero(info['开户数']['近6个月贷记卡账户开户数']) == 0:
  136. base_rules_hit_list.append(rule)
  137. #PBOC0025 近6个月(含)“贷款审批”+“信用卡审批”总查询次数>=11次
  138. rule = 'PBOC0025'
  139. if null2Zero(info['信贷审批查询记录明细']['近6月查询次数贷款审批']) + null2Zero(info['信贷审批查询记录明细']['近6月查询次数信用卡审批']) >= 11:
  140. base_rules_hit_list.append(rule)
  141. #PBOC0026 近3个月(含),新增贷款笔数>=3笔
  142. rule = 'PBOC0026'
  143. if null2Zero(info['开户数']['近3个月贷款账户开户数']) >= 3:
  144. base_rules_hit_list.append(rule)
  145. #PBOC0027 过去12个月贷款累计逾期次数>=2次且近6个月贷记卡透支比例超过80%且贷记卡总授信额度>=150000
  146. rule = 'PBOC0027'
  147. if null2Zero(info['24期还款状态']['贷款账户近12月逾期期数大于或等于“1”的次数'])>=2 and null2Zero(info['贷记卡账户']['贷记卡账户近6月平均额度总的使用率']) > 0.8 and info['信贷交易授信及负债信息概要']['贷记卡授信总额'] >= 150000:
  148. base_rules_hit_list.append(rule)
  149. #PBOC0028 贷记卡总额度5万(含)以内,是否近6个月(含)贷记卡透支比例超过100%
  150. rule = 'PBOC0028'
  151. if null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额']) <= 50000 and null2Zero(info['贷记卡账户']['贷记卡账户近6月平均额度总的使用率']) >= 1:
  152. base_rules_hit_list.append(rule)
  153. #PBOC0029 贷记卡最低还款比例<5%(为null不拒)
  154. rule = 'PBOC0029'
  155. if info['贷记卡账户']['贷记卡最低还款比例'] is not None:
  156. if null2Zero(info['贷记卡账户']['贷记卡最低还款比例']) < 0.05:
  157. base_rules_hit_list.append(rule)
  158. #PBOC0030 贷款过去12个月逾期次数大于等于2次且贷记卡总额度(5万,15万],是否近6个月(含)贷记卡透支比例超过95%
  159. rule = 'PBOC0030'
  160. if null2Zero(info['24期还款状态']['贷款账户近12月逾期期数大于或等于“1”的次数']) >= 2 and null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额']) > 50000 and null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额']) <= 150000 and null2Zero(info['贷记卡账户']['贷记卡账户近6月平均额度总的使用率']) >= 0.95:
  161. base_rules_hit_list.append(rule)
  162. ###专属规则命中集
  163. ex_rules_hit_list = []
  164. #PBOC0031 近6个月所有贷款逾期总次数>=2
  165. rule = 'PBOC0031'
  166. if null2Zero(info['24期还款状态']['贷款账户近6月逾期期数大于或等于“1”的次数']) >= 2:
  167. ex_rules_hit_list.append(rule)
  168. #PBOC0032 过去12个月中信用卡审批查询次数>=10
  169. rule = 'PBOC0032'
  170. if null2Zero(info['信贷审批查询记录明细']['近12月查询次数信用卡审批']) >= 10:
  171. ex_rules_hit_list.append(rule)
  172. #PBOC0033 过去6个月中信用卡审批查询次数>4且信用卡最近三个月查询次数>2
  173. rule = 'PBOC0033'
  174. if null2Zero(info['信贷审批查询记录明细']['近6月查询次数信用卡审批']) > 4 and null2Zero(info['信贷审批查询记录明细']['近3月查询次数信用卡审批']) >2:
  175. ex_rules_hit_list.append(rule)
  176. ###基础额度计算
  177. #先判断基础规则命中,有命中直接拒绝
  178. if len(base_rules_hit_list) > 0:
  179. base_quota = 0
  180. else:
  181. #判断贷记卡最近6个月平均使用额度是否为空,为空默认5000
  182. if info['信贷交易授信及负债信息概要']['贷记卡最近6个月平均使用额度'] is None:
  183. var1 = 5000
  184. else:
  185. var1 = null2Zero(info['信贷交易授信及负债信息概要']['贷记卡最近6个月平均使用额度'])
  186. #计算基础额度系数
  187. con1 = info['使用率']['全账户使用率(已用额度/授信总额)']
  188. con2 = info['24期还款状态']['全账户近12月逾期期数大于或等于“1”的次数']
  189. if con1 is None or con2 is None:
  190. coef = 1
  191. else:
  192. con1,con2 = null2Zero(con1),null2Zero(con2)
  193. if con1 <= 0.7 and con2 <= 2:
  194. coef = 1.1
  195. elif con1 > 0.7 and con2 <=2:
  196. coef = 0.8
  197. elif con2 >= 3 and con2 <= 5:
  198. coef = 0.8
  199. elif con2 > 5:
  200. coef = 0.5
  201. #计算基础额度,百位取整
  202. base_quota = int(var1 * coef / 100) * 100
  203. #上下限约束
  204. if base_quota > 20000:
  205. base_quota = 20000
  206. elif base_quota < 2000:
  207. base_quota = 2000
  208. ###专属额度计算
  209. #先判断基础规则和专属规则命中,有任一命中则专属额度为0
  210. if len(base_rules_hit_list) > 0 or len(ex_rules_hit_list) > 0:
  211. ex_quota = 0
  212. else:
  213. #判断各类额度是否为空,为空替换为0:
  214. var1 = null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额'])
  215. #var2 = null2Zero(info['信贷交易授信及负债信息概要']['非循环贷账户授信总额'] )
  216. var3 = null2Zero(info['信贷交易授信及负债信息概要']['循环贷账户授信总额'])
  217. var4 = null2Zero(info['信贷交易授信及负债信息概要']['贷记卡已用额度'])
  218. #var5 = null2Zero(info['信贷交易授信及负债信息概要']['非循环贷账户余额'])
  219. var6 = null2Zero(info['信贷交易授信及负债信息概要']['循环贷账户余额'])
  220. #计算专属额度系数
  221. con = info['24期还款状态']['全账户近12月逾期期数大于或等于“1”的次数']
  222. if con is None:
  223. coef = 0.4
  224. else:
  225. con = null2Zero(con)
  226. if con <= 2:
  227. coef = 0.6
  228. elif con >= 3 and con <= 5:
  229. coef = 0.4
  230. elif con > 5:
  231. coef = 0.2
  232. #计算专属额度,百位取整
  233. ex_quota = int((var1 + var3 - var4 - var6)/100) * 100
  234. #上下限约束
  235. if ex_quota > 30000:
  236. ex_quota = 30000
  237. elif ex_quota < 2000 and ex_quota > 0:
  238. ex_quota = 2000
  239. elif ex_quota <= 0:
  240. ex_quota = 0
  241. output_data = json.dumps({'base_rules_hit_list':base_rules_hit_list, 'base_rules_hit_cnt':len(base_rules_hit_list), 'ex_rules_hit_list':ex_rules_hit_list, 'ex_rules_hit_cnt':len(ex_rules_hit_list),
  242. 'base_quota':base_quota, 'ex_quota':ex_quota,'creditcard_usedquota_avg_rec6m':creditcard_usedquota_avg_rec6m,'all_account_usage':all_account_usage,'all_account_od_times_rec12m':all_account_od_times_rec12m,'creditcard_total_creditquota':creditcard_total_creditquota,'unrevo_loan_total_creditquota':unrevo_loan_total_creditquota,'revo_loan_total_creditquota':revo_loan_total_creditquota,'creditcard_usedquota':creditcard_usedquota,'unrevo_loan_total_balance':unrevo_loan_total_balance,'revo_loan_total_balance':revo_loan_total_balance,'creditcard_repayment_ratio':creditcard_repayment_ratio})
  243. return output_data
  244. '''
  245. if __name__ == '__main__':
  246. json_data = json.load(open('人行字段test.json',encoding='utf-8'))
  247. result = renhang_rules(json_data)
  248. print(result)
  249. '''