import json import operator import subprocess from dbController import DbController dbController = DbController(); import requests import log import shutil from datetime import datetime logger = log.logger from userController import UserController userController =UserController(); class CustomModel(): def caculate_string(self,a, b, c): if a == None or c == None: return True d = {'<': operator.lt, '<=': operator.le, '=': operator.eq, '>': operator.gt, '>=': operator.ge} return d[b](str(a), str(c)) def caculate_logic(self,a, b, c): d = {'<': operator.lt, '<=': operator.le, '=': operator.eq, '>': operator.gt, '>=': operator.ge} return d[b](float(a), float(c)) def dataFilter(self, businessInfo, filter_str): field_json=json.loads(businessInfo['result']) filter_json=json.loads(filter_str); # with open('field.json', 'r', encoding='utf-8')as fp: filter_dict = {} for k in filter_json: # print(k) field_dict = {} if k in field_json.keys(): for k2 in filter_json[k]: if k2 in field_json[k].keys(): field_dict[k2] = field_json[k][k2] else: print('The field2 does not exist!') filter_dict[k] = field_dict else: print('The field does not exist!') with open('result.json', 'w', encoding='utf8')as fp: json.dump(filter_dict, fp, ensure_ascii=False) # print('filter finished!') return filter_dict def callCustom(self,field_json,model_path): # with open('field.json', 'r', encoding='utf-8')as fp: # field_json = json.load(fp) with open(model_path, 'r', encoding='utf-8')as fp: condition_json = json.load(fp) condition_result = [] for k in condition_json.keys(): select_data = condition_json[k]['select_data'] condition_result_k2 = [] logic_k2 = [] for k2 in select_data: condition_result_k2.append( self.caculate_string(field_json[k2['first_class']][k2['second_class']], k2['logic'], k2['value'])) logic_k2.append(k2['mark']) # print(condition_result_k2) temp_condition = condition_result_k2[0] for i in range(1, len(condition_result_k2)): if logic_k2[i] == 'AND': temp_condition = temp_condition and condition_result_k2[i] elif logic_k2[i] == 'OR': temp_condition = temp_condition or condition_result_k2[i] if condition_json[k]['result'] == '拒绝': temp_condition = not temp_condition condition_result.append(temp_condition) result = condition_result[0] # field_json = json.load(fp) # # with open('filter.json', 'r', encoding='utf-8')as fp: # filter_json = json.load(fp) filter_dict = {} # print(len(condition_result)) for i in range(1, len(condition_result)): result = result and condition_result[i] # print("结果是"+str(result)) return result def query_by_http(self, url, businessInfo): credit_json = businessInfo["result"] request_data = { 'queryReq': credit_json } # logger.info(json.dumps(request_data)) headers = {"Content-Type": "application/json"} resp = requests.post(url, data=json.dumps(request_data), headers=headers) return resp.text def query_by_python(self, python_path, jsonFile): execute = "python {} '{}'".format(python_path, jsonFile) # print(execute) output = subprocess.Popen(execute, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = output.stdout.readlines() return res def query_by_java_jar(self,jar_path, jsonFile): execute = "java -jar {} '{}'".format(jar_path, jsonFile) # print(execute) output = subprocess.Popen(execute, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = output.stdout.readlines() return res def doCustomModel(self,productNum,businessInfo,xml_path,file_name,startTime): # 获取用户信息 product_num = productNum file_name = file_name userController.getConn(); user_list = userController.getUser(); userController.getModelByUserAndNum(user_list,product_num); for user in user_list: if user["user_name"]=='test': model_list=user["model_list"]; # if user.get('field_data') !=None: if model_list != []: for model in model_list: model_name = model["model_name"] else: dbController.getConn() parse_info = str(businessInfo["result"]) parse_info = parse_info.replace("'", "\\'") filter_info = "没有设置自定义模型而无法解析" sql = "INSERT INTO filter_result(report_name,product_num,filter_info,parse_info,start_time) VALUES ('" + file_name + "', '" + productNum + "', '" + filter_info + "', '" + parse_info + "', '" + startTime + "')" # print(sql) dbController.execSql(sql) # if i==1: # break dbController.commit() userController.getFieldByUserAndModel(user_list,model_name); base_path="D:/jin_rong/model_data/" base_url="http://127.0.0.1:" # print(user_list) for user in user_list: model_list=user["model_list"]; # if user.get('field_data') !=None: if 1: for model in model_list: # print(model) if(model.get('product_num')!=None): # print("11111");模型对应产品号非空才走下面流程 print(model) if(model['product_num'].find(productNum)>=0 ): # ` 自定义模型 print("模型类型是:"+str(model["model_type"])) if model["model_type"]==0: # read_data=json.loads(businessInfo['result']) # print(businessInfo["result"]) # print("11111");#模型对应产品号非空才走下面流程 if businessInfo["result"]=="": result = {"approveResult": "0", "rule": "白户"} uploadAudit(result, businessInfo["businessNum"], productNum) return 0 # read_data = businessInfo parse_info = str(businessInfo["result"]) parse_info = parse_info.replace("'","\\'") print("user['field_data']是") print(user) read_data = self.dataFilter(businessInfo, user['field_data']); #yuan在此更新数据库,插入字段组合的过滤结果read_data,500字段的解析结果businessInfo #字段组合过滤结果的表需更新,500字段解析结果表需新建 endTime = str(datetime.now()) # 结束时间 # dbController.getConn() # filter_info = str(read_data) # filter_info = filter_info.replace("'","\\'") # sql = "INSERT INTO filter_result(report_name,filter_info,parse_info) VALUES ('" + file_name + "', '"+ filter_info+"', '"+ parse_info +"')" # dbController.execSql(sql) # dbController.commit() inner_path = xml_path.replace("test_data", "test_data/execed_filter") # print(inner_path) txtPath = inner_path.replace(".xml", ".txt") # print("txt路径"+txtPath) with open(txtPath, 'w', encoding='utf-8') as fp: fp.write(str(read_data)) # dbController.getConn() # report_name = file_name # # sql = "INSERT INTO parse_result(report_name,fmodel_name,result_info,created_by) " \ # "VALUES ('" + report_name +"', '1', '"+ str(read_data)+"','1', 'test')" # print(sql) # dbController.execSql(sql) # dbController.commit() result=self.callCustom(read_data,base_path+user['user_name']+"_"+model['model_name']+'.json'); if result: res = {"approveResult": "1"} # print("征信通过") # dbController.getConn() # # print(len(sqlMap)) # sql = "INSERT INTO credit_approval(approval_result,credit_name) VALUES ('征信通过', '" + file_name + "')" # # print(sql) # dbController.execSql(sql) # # if i==1: # # break # dbController.commit() # yuan新增入库 dbController.getConn() # filter_info = str(read_data) # filter_info = filter_info.replace("'", "\\'") filter_info = "征信通过" sql = "INSERT INTO filter_result(report_name,product_num,filter_info,parse_info,start_time,end_time) VALUES ('" + file_name + "', '" + productNum + "', '" + filter_info + "', '" + parse_info + "', '" + startTime + "', '" + endTime + "')" # print(sql) dbController.execSql(sql) dbController.commit() print("this is not a sadf") uploadAudit(res, businessInfo["businessNum"], productNum); # print("this is a sadf") return 0 else: res = {"approveResult": "0"} # print("征信未通过") # yuan新增入库 dbController.getConn() # filter_info = str(read_data) # filter_info = filter_info.replace("'", "\\'") filter_info = "征信失败" sql = "INSERT INTO filter_result(report_name,product_num,filter_info,parse_info,start_time,end_time) VALUES ('" + file_name + "', '" + productNum + "', '" + filter_info + "', '" + parse_info + "', '" + startTime + "', '" + endTime + "')" # print(sql) dbController.execSql(sql) dbController.commit() # 默认上传审批结果 # print(res) # uploadAudit(res, businessInfo["businessNum"], productNum); # return 0 elif model["model_type"]==1: read_data = self.dataFilter(businessInfo, user['field_data']); jsonPath = xml_path.replace(".xml", "_json.txt") with open(jsonPath, 'w', encoding='utf-8') as fp: json.dump(read_data, fp, ensure_ascii=False) result=self.query_by_java_jar(base_path+"/"+user['user_name']+"/"+model['other_data'],jsonPath); with open(jsonPath, 'w', encoding='utf-8') as fp: fp.write(json.dumps(result)) uploadJsonFile(businessInfo, jsonPath) return 1 elif model["model_type"] == 2: read_data = self.dataFilter(businessInfo, user['field_data']); jsonPath = xml_path.replace(".xml", "_json.txt") with open(jsonPath, 'w', encoding='utf-8') as fp: json.dump(read_data, fp, ensure_ascii=False) result=self.query_by_python(base_path+"/"+user['user_name']+"/"+model['other_data'],jsonPath); with open(jsonPath, 'w', encoding='utf-8') as fp: fp.write(json.dumps(result)) uploadJsonFile(businessInfo, jsonPath) return 2 elif model["model_type"] == 3: result=self.query_by_http(base_url+model['other_data'],businessInfo); jsonPath = xml_path.replace(".xml", "_json.txt") with open(jsonPath, 'w', encoding='utf-8') as fp: fp.write(json.dumps(result)) uploadJsonFile(businessInfo, jsonPath) return 3 elif model["model_type"]==4: uploadJsonFile(businessInfo,xml_path.replace(".xml", ".txt")) return -1; return None from parseCreditXmlModel import uploadJsonFile, uploadAudit