import json def renhang_rules(json_data): '''解析json数据,计算规则命中''' ###null值转换函数 def toFloat(value): if str(value)=="" or str(value)=="--" or str(value)=="nan": return 0 else: return float(value) def null2Zero(value): if value==None: return 0 else: return toFloat(value) #如果人行未查得,直接返回固定错误码和结果 if json_data['result'] == '' or json_data['result'] == None: output_data = json.dumps({'base_rules_hit_list':['PBOC1001'], 'base_rules_hit_cnt':1, 'ex_rules_hit_list':[], 'ex_rules_hit_cnt':0, '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}) return output_data else: ###取数,将json字符串解码为python可处理对象 info = json.loads(json_data['result']) ###输出10个原始字段 creditcard_usedquota_avg_rec6m = info['信贷交易授信及负债信息概要']['贷记卡最近6个月平均使用额度'] all_account_usage = info['使用率']['全账户使用率(已用额度/授信总额)'] all_account_od_times_rec12m = info['24期还款状态']['全账户近12月逾期期数大于或等于“1”的次数'] creditcard_total_creditquota = info['信贷交易授信及负债信息概要']['贷记卡授信总额'] unrevo_loan_total_creditquota = info['信贷交易授信及负债信息概要']['非循环贷账户授信总额'] revo_loan_total_creditquota = info['信贷交易授信及负债信息概要']['循环贷账户授信总额'] creditcard_usedquota = info['信贷交易授信及负债信息概要']['贷记卡已用额度'] unrevo_loan_total_balance = info['信贷交易授信及负债信息概要']['非循环贷账户余额'] revo_loan_total_balance = info['信贷交易授信及负债信息概要']['循环贷账户余额'] creditcard_repayment_ratio = info['贷记卡账户']['贷记卡还款比例'] ###统计规则命中 ###基础规则命中集 base_rules_hit_list = [] #PBOC1000 贷记卡授信总额、非循环贷账户授信总额、循环贷账户授信总额都为0 rule = 'PBOC1000' if null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额']) == 0 and null2Zero(info['信贷交易授信及负债信息概要']['非循环贷账户授信总额']) == 0 and null2Zero(info['信贷交易授信及负债信息概要']['循环贷账户授信总额']) == 0: base_rules_hit_list.append(rule) #PBOC0001 呆账信息汇总-账户数>0 rule = 'PBOC0001' if null2Zero(info['呆账信息汇总']['账户数']) > 0: base_rules_hit_list.append(rule) #PBOC0002 贷记卡账户出现锁定码G rule = 'PBOC0002' if null2Zero(info['24期还款状态']['贷记卡账户近24个月是否出现\"G\"']) > 0: base_rules_hit_list.append(rule) #PBOC0003 贷记卡账户当前逾期1期及以上且当前总逾期金额>300元 rule = 'PBOC0003' if null2Zero(info['贷记卡账户']['当前贷记卡最大逾期期数']) >=1 and null2Zero(info['贷记卡账户']['当前贷记卡最大逾期金额']) > 300: base_rules_hit_list.append(rule) #PBOC0004 个贷账户状态为逾期 rule = 'PBOC0004' if null2Zero(info['贷款信息']['当前贷款逾期账户数']) > 0: base_rules_hit_list.append(rule) #PBOC0005 贷款账户出现锁定码G或Z rule = 'PBOC0005' if null2Zero(info['24期还款状态']['贷款账户近24个月是否出现\"G\"']) > 0 or null2Zero(info['24期还款状态']['贷款账户近24个月是否出现\"Z\"']) > 0: base_rules_hit_list.append(rule) #PBOC0006 单张贷记卡近12个月(含)最高逾期期数>=2期 rule = 'PBOC0006' if null2Zero(info['贷记卡账户']['近12月贷记卡最大逾期期数']) >= 2: base_rules_hit_list.append(rule) #PBOC0007 单张贷记卡近24个月(含)最大逾期期数>=3期 rule = 'PBOC0007' if null2Zero(info['贷记卡账户']['近24月贷记卡最大逾期期数']) >= 3: base_rules_hit_list.append(rule) #PBOC0008 贷记卡近6个月累计逾期次数≥3次 rule = 'PBOC0008' if null2Zero(info['24期还款状态']['贷记卡账户近6月逾期期数大于或等于“1”的次数']) >= 3: base_rules_hit_list.append(rule) #PBOC0009 贷记卡近12个月累计逾期次数≥6次 rule = 'PBOC0009' if null2Zero(info['24期还款状态']['贷记卡账户近12月逾期期数大于或等于“1”的次数']) >= 6: base_rules_hit_list.append(rule) #PBOC0010 贷记卡近24个月累计逾期次数≥9次 rule = 'PBOC0010' if null2Zero(info['24期还款状态']['贷记卡账户近24月逾期期数大于或等于“1”的次数']) >= 9: base_rules_hit_list.append(rule) #PBOC0011 贷款近12个月(含)最高逾期期数>=2期 rule = 'PBOC0011' if null2Zero(info['贷款信息']['近12月贷款的最大逾期期数']) >= 2: base_rules_hit_list.append(rule) #PBOC0012 贷款近24个月(含)最高逾期期数>=3期 rule = 'PBOC0012' if null2Zero(info['贷款信息']['近24月贷款的最大逾期期数']) >= 3: base_rules_hit_list.append(rule) #PBOC0013 所有个贷近12个月(含)累计逾期次数>=3次 rule = 'PBOC0013' if null2Zero(info['24期还款状态']['贷款账户近12月逾期期数大于或等于“1”的次数']) >= 3: base_rules_hit_list.append(rule) #PBOC0014 所有个贷近24个月(含)逾期次数累计>=5次 rule = 'PBOC0014' if null2Zero(info['24期还款状态']['贷款账户近24月逾期期数大于或等于“1”的次数']) >= 5: base_rules_hit_list.append(rule) #PBOC0015 所有个贷和贷记卡过去12个月累计逾期次数>8次 rule = 'PBOC0015' if null2Zero(info['24期还款状态']['贷款账户近12月逾期期数大于或等于“1”的次数']) + null2Zero(info['24期还款状态']['贷记卡账户近12月逾期期数大于或等于“1”的次数']) > 8: base_rules_hit_list.append(rule) #PBOC0016 用户所有账户发生不良特殊交易的次数统计>0 rule = 'PBOC0016' if null2Zero(info['特殊交易']['用户所有帐户发生不良特殊交易的次数统计']) > 0: base_rules_hit_list.append(rule) #PBOC0017 被追偿账户总数>0 rule = 'PBOC0017' if null2Zero(info['被追偿信息']['被追偿账户总数']) > 0: base_rules_hit_list.append(rule) #PBOC0018 资产处置业务账户数>0 rule = 'PBOC0018' if null2Zero(info['信贷交易违约信息概要']['资产处置业务账户数']) > 0: base_rules_hit_list.append(rule) #PBOC0019 垫款业务账户数>0 rule = 'PBOC0019' if null2Zero(info['信贷交易违约信息概要']['垫款业务账户数']) > 0: base_rules_hit_list.append(rule) #PBOC0020 债权转移时属于催收状态的账户数>0 rule = 'PBOC0020' if null2Zero(info['信贷交易违约信息概要']['债权转移时属于催收状态的账户数']) > 0: base_rules_hit_list.append(rule) #PBOC0021 强制执行信息-记录数>0 rule = 'PBOC0021' if null2Zero(info['公共信息概要']['强制执行信息-记录数']) > 0: base_rules_hit_list.append(rule) #PBOC0022 过去6个月贷款及贷记卡总逾期次数>=2& 过去6个月贷款和贷记卡审批查询次数>=6次 rule = 'PBOC0022' if null2Zero(info['24期还款状态']['贷款账户近6月逾期期数大于或等于“1”的次数']) + null2Zero(info['24期还款状态']['贷记卡账户近6月逾期期数大于或等于“1”的次数']) >= 2 and null2Zero(info['信贷审批查询记录明细']['近6月查询次数信用卡审批']) + null2Zero(info['信贷审批查询记录明细']['近6月查询次数贷款审批']) >= 6: base_rules_hit_list.append(rule) #PBOC0023 当前贷记卡透支比例超过85% rule = 'PBOC0023' if null2Zero(info['贷记卡账户']['贷记卡账户当前总额度使用率']) > 0.85: base_rules_hit_list.append(rule) #PBOC0024 近6个月(含)信用卡审批总查询次数>=5次且无新开信用卡账户 rule = 'PBOC0024' if null2Zero(info['信贷审批查询记录明细']['近6月查询次数信用卡审批']) >= 5 and null2Zero(info['开户数']['近6个月贷记卡账户开户数']) == 0: base_rules_hit_list.append(rule) #PBOC0025 近6个月(含)“贷款审批”+“信用卡审批”总查询次数>=11次 rule = 'PBOC0025' if null2Zero(info['信贷审批查询记录明细']['近6月查询次数贷款审批']) + null2Zero(info['信贷审批查询记录明细']['近6月查询次数信用卡审批']) >= 11: base_rules_hit_list.append(rule) #PBOC0026 近3个月(含),新增贷款笔数>=3笔 rule = 'PBOC0026' if null2Zero(info['开户数']['近3个月贷款账户开户数']) >= 3: base_rules_hit_list.append(rule) #PBOC0027 过去12个月贷款累计逾期次数>=2次且近6个月贷记卡透支比例超过80%且贷记卡总授信额度>=150000 rule = 'PBOC0027' if null2Zero(info['24期还款状态']['贷款账户近12月逾期期数大于或等于“1”的次数'])>=2 and null2Zero(info['贷记卡账户']['贷记卡账户近6月平均额度总的使用率']) > 0.8 and info['信贷交易授信及负债信息概要']['贷记卡授信总额'] >= 150000: base_rules_hit_list.append(rule) #PBOC0028 贷记卡总额度5万(含)以内,是否近6个月(含)贷记卡透支比例超过100% rule = 'PBOC0028' if null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额']) <= 50000 and null2Zero(info['贷记卡账户']['贷记卡账户近6月平均额度总的使用率']) >= 1: base_rules_hit_list.append(rule) #PBOC0029 贷记卡最低还款比例<5%(为null不拒) rule = 'PBOC0029' if info['贷记卡账户']['贷记卡最低还款比例'] is not None: if null2Zero(info['贷记卡账户']['贷记卡最低还款比例']) < 0.05: base_rules_hit_list.append(rule) #PBOC0030 贷款过去12个月逾期次数大于等于2次且贷记卡总额度(5万,15万],是否近6个月(含)贷记卡透支比例超过95% rule = 'PBOC0030' if null2Zero(info['24期还款状态']['贷款账户近12月逾期期数大于或等于“1”的次数']) >= 2 and null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额']) > 50000 and null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额']) <= 150000 and null2Zero(info['贷记卡账户']['贷记卡账户近6月平均额度总的使用率']) >= 0.95: base_rules_hit_list.append(rule) ###专属规则命中集 ex_rules_hit_list = [] #PBOC0031 近6个月所有贷款逾期总次数>=2 rule = 'PBOC0031' if null2Zero(info['24期还款状态']['贷款账户近6月逾期期数大于或等于“1”的次数']) >= 2: ex_rules_hit_list.append(rule) #PBOC0032 过去12个月中信用卡审批查询次数>=10 rule = 'PBOC0032' if null2Zero(info['信贷审批查询记录明细']['近12月查询次数信用卡审批']) >= 10: ex_rules_hit_list.append(rule) #PBOC0033 过去6个月中信用卡审批查询次数>4且信用卡最近三个月查询次数>2 rule = 'PBOC0033' if null2Zero(info['信贷审批查询记录明细']['近6月查询次数信用卡审批']) > 4 and null2Zero(info['信贷审批查询记录明细']['近3月查询次数信用卡审批']) >2: ex_rules_hit_list.append(rule) ###基础额度计算 #先判断基础规则命中,有命中直接拒绝 if len(base_rules_hit_list) > 0: base_quota = 0 else: #判断贷记卡最近6个月平均使用额度是否为空,为空默认5000 if info['信贷交易授信及负债信息概要']['贷记卡最近6个月平均使用额度'] is None: var1 = 5000 else: var1 = null2Zero(info['信贷交易授信及负债信息概要']['贷记卡最近6个月平均使用额度']) #计算基础额度系数 con1 = info['使用率']['全账户使用率(已用额度/授信总额)'] con2 = info['24期还款状态']['全账户近12月逾期期数大于或等于“1”的次数'] if con1 is None or con2 is None: coef = 1 else: con1,con2 = null2Zero(con1),null2Zero(con2) if con1 <= 0.7 and con2 <= 2: coef = 1.1 elif con1 > 0.7 and con2 <=2: coef = 0.8 elif con2 >= 3 and con2 <= 5: coef = 0.8 elif con2 > 5: coef = 0.5 #计算基础额度,百位取整 base_quota = int(var1 * coef / 100) * 100 #上下限约束 if base_quota > 20000: base_quota = 20000 elif base_quota < 2000: base_quota = 2000 ###专属额度计算 #先判断基础规则和专属规则命中,有任一命中则专属额度为0 if len(base_rules_hit_list) > 0 or len(ex_rules_hit_list) > 0: ex_quota = 0 else: #判断各类额度是否为空,为空替换为0: var1 = null2Zero(info['信贷交易授信及负债信息概要']['贷记卡授信总额']) #var2 = null2Zero(info['信贷交易授信及负债信息概要']['非循环贷账户授信总额'] ) var3 = null2Zero(info['信贷交易授信及负债信息概要']['循环贷账户授信总额']) var4 = null2Zero(info['信贷交易授信及负债信息概要']['贷记卡已用额度']) #var5 = null2Zero(info['信贷交易授信及负债信息概要']['非循环贷账户余额']) var6 = null2Zero(info['信贷交易授信及负债信息概要']['循环贷账户余额']) #计算专属额度系数 con = info['24期还款状态']['全账户近12月逾期期数大于或等于“1”的次数'] if con is None: coef = 0.4 else: con = null2Zero(con) if con <= 2: coef = 0.6 elif con >= 3 and con <= 5: coef = 0.4 elif con > 5: coef = 0.2 #计算专属额度,百位取整 ex_quota = int((var1 + var3 - var4 - var6)/100) * 100 #上下限约束 if ex_quota > 30000: ex_quota = 30000 elif ex_quota < 2000 and ex_quota > 0: ex_quota = 2000 elif ex_quota <= 0: ex_quota = 0 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), '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}) return output_data ''' if __name__ == '__main__': json_data = json.load(open('人行字段test.json',encoding='utf-8')) result = renhang_rules(json_data) print(result) '''