394 lines
13 KiB
Java
394 lines
13 KiB
Java
package com.tenwa.httpclient;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.amarsoft.are.ARE;
|
||
import com.amarsoft.are.log.Log;
|
||
import org.apache.commons.collections.CollectionUtils;
|
||
import org.dom4j.Element;
|
||
import org.dom4j.tree.DefaultAttribute;
|
||
import java.sql.Statement;
|
||
import java.util.*;
|
||
|
||
/**
|
||
* 鹏元个人发欺诈数据存储sql处理生成
|
||
*/
|
||
public class PyPafUtils {
|
||
|
||
private static Log logger= ARE.getLog();
|
||
|
||
private static String[] personAntiFraudDescInfo = {
|
||
"id",
|
||
"applyid",
|
||
"batNo",
|
||
"reportID",
|
||
"subReportType",
|
||
"subReportTypeCost",
|
||
"treatResult",
|
||
"treatErrorCode",
|
||
"errorMessage",
|
||
"antiFraudScore",
|
||
"antiFraudScoreRiskLevel",
|
||
"policeCheckRiskLevel",
|
||
"policeCheckDesc",
|
||
"mobileCheckRiskLevel",
|
||
"mobileCheckDesc",
|
||
"mobileStatusRiskLevel",
|
||
"mobileStatusDesc",
|
||
"riskRosterRiskLevel",
|
||
"riskRosterDesc",
|
||
"judicialRiskLevel",
|
||
"judicialRiskDesc"
|
||
};
|
||
|
||
private static String[] personAntiFraudScoreInfo = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"subreporttype",
|
||
"subreporttypecost",
|
||
"treatresult",
|
||
"treaterrorcode",
|
||
"errormessage",
|
||
"riskscore",
|
||
"risklevel",
|
||
"hittypes"
|
||
};
|
||
|
||
private static String[] personBaseInfo = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"subreporttype",
|
||
"subreporttypecost",
|
||
"treatresult",
|
||
"treaterrorcode",
|
||
"errormessage",
|
||
"name",
|
||
"docno",
|
||
"birthday",
|
||
"genderid",
|
||
"age",
|
||
"originaladdress",
|
||
"verifyresult"
|
||
};
|
||
|
||
private static String[] policeCheckInfo = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"subreporttype",
|
||
"subreporttypecost",
|
||
"treatresult",
|
||
"treaterrorcode",
|
||
"errormessage"
|
||
};
|
||
|
||
private static String[] policeCheckInfoItem = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"name",
|
||
"documentno",
|
||
"result"
|
||
};
|
||
|
||
private static String[] mobileCheckInfo = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"subreporttype",
|
||
"subreporttypecost",
|
||
"treatresult",
|
||
"treaterrorcode",
|
||
"errormessage"
|
||
};
|
||
|
||
private static String[] mobileCheckInfoItem = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"checkresult",
|
||
"operator",
|
||
"areainfo"
|
||
};
|
||
|
||
private static String[] mobileStatusInfo = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"subreporttype",
|
||
"subreporttypecost",
|
||
"treatresult",
|
||
"treaterrorcode",
|
||
"errormessage"
|
||
};
|
||
|
||
private static String[] mobileStatusInfoItem = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"phonestatus",
|
||
"timelength",
|
||
"operator",
|
||
"areainfo"
|
||
};
|
||
|
||
private static String[] personRiskRosterInfo = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"subreporttype",
|
||
"subreporttypecost",
|
||
"treatresult",
|
||
"treaterrorcode",
|
||
"errormessage",
|
||
"riskTypes"
|
||
};
|
||
|
||
private static String[] personJudicialRiskInfo = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"subreporttype",
|
||
"subreporttypecost",
|
||
"treatresult",
|
||
"treaterrorcode",
|
||
"errormessage"
|
||
};
|
||
|
||
private static String[] personJudicialRiskInfoItem = {
|
||
"id",
|
||
"applyid",
|
||
"batno",
|
||
"reportid",
|
||
"timeperiod",
|
||
"civilcasecount",
|
||
"criminalcasecount",
|
||
"judicialcount",
|
||
"endcasecount",
|
||
"breakfaithcount",
|
||
"judicialtotalcount",
|
||
"taxpunishcount",
|
||
"marketpunishcount",
|
||
"customspunishcount",
|
||
"environmentpunishcount",
|
||
"otherpunishcount",
|
||
"punishtotalcount"
|
||
};
|
||
|
||
/**
|
||
* 数据库表名和列字段对应关系
|
||
*/
|
||
public enum PersonalAntiFraudDataEnum{
|
||
pafdi("personAntiFraudDescInfo", personAntiFraudDescInfo, "1", "个人反欺诈风险综述信息"),
|
||
pafsi("personAntiFraudScoreInfo", personAntiFraudScoreInfo, "1", "欺诈风险指数信息"),
|
||
pbi("personBaseInfo", personBaseInfo, "1", "证件号码校验信息"),
|
||
pci("policeCheckInfo", policeCheckInfo, "1", "身份认证"),
|
||
pcii("policeCheckInfoItem", policeCheckInfoItem, "2", "身份认证item"),
|
||
mci("mobileCheckInfo", mobileCheckInfo, "1", "手机号码核查"),
|
||
mcii("mobileCheckInfoItem", mobileCheckInfoItem, "2", "手机号码核查item"),
|
||
msi("mobileStatusInfo", mobileStatusInfo, "1", "手机号码状态和在网时长"),
|
||
msii("mobileStatusInfoItem", mobileStatusInfoItem, "2", "手机号码状态和在网时长item"),
|
||
prri("personRiskRosterInfo", personRiskRosterInfo, "1", "风险名单信息"),
|
||
pjri("personJudicialRiskInfo", personJudicialRiskInfo, "1", "司法风险汇总信息"),
|
||
pjrii("personJudicialRiskInfoItem", personJudicialRiskInfoItem, "2", "司法风险汇总信息item");
|
||
|
||
private String code; //类型
|
||
private String[] value; // 对应关系
|
||
private String type; //类型
|
||
private String desc; //描述
|
||
|
||
private PersonalAntiFraudDataEnum(String code, String[] value, String type, String desc){
|
||
this.code=code;
|
||
this.value=value;
|
||
this.type=type;
|
||
this.desc=desc;
|
||
}
|
||
|
||
public String[] getValue() {
|
||
return this.value;
|
||
}
|
||
|
||
public String getType() {
|
||
return type;
|
||
}
|
||
|
||
public String getDesc() {
|
||
return desc;
|
||
}
|
||
|
||
public String getCode() {
|
||
return this.code;
|
||
}
|
||
|
||
/**
|
||
* 获取主表
|
||
* @return
|
||
*/
|
||
public static List<PersonalAntiFraudDataEnum> getDataList(){
|
||
List<PersonalAntiFraudDataEnum> list = new ArrayList<>();
|
||
for(PersonalAntiFraudDataEnum data : PersonalAntiFraudDataEnum.values()){
|
||
if("1".equals(data.getType())){
|
||
list.add(data);
|
||
}
|
||
}
|
||
return list;
|
||
}
|
||
|
||
/**
|
||
* 根据code 数据库表名的列字段
|
||
* @param code
|
||
* @return
|
||
*/
|
||
public static String[] getValueData(String code){
|
||
for(PersonalAntiFraudDataEnum data : PersonalAntiFraudDataEnum.values()){
|
||
if(data.getCode().toLowerCase().equals(code.toLowerCase())){
|
||
return data.getValue();
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存个人反欺诈数据
|
||
* @param
|
||
* @param
|
||
* @return
|
||
*/
|
||
public static void savePafData(String applyId, String batNo, String reportID,
|
||
Statement stat, Element cisReport){
|
||
try{
|
||
List<PersonalAntiFraudDataEnum> personalAntiFraudDataEnumList = PersonalAntiFraudDataEnum.getDataList();
|
||
if(CollectionUtils.isNotEmpty(personalAntiFraudDataEnumList)) {
|
||
for (PersonalAntiFraudDataEnum personalAntiFraudDataEnum : personalAntiFraudDataEnumList) {
|
||
insertPafData(personalAntiFraudDataEnum.getCode(), applyId, batNo, reportID, stat, cisReport);
|
||
}
|
||
}
|
||
}catch (Exception e){
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 根据表名和列名初始化sql
|
||
* @param tableName
|
||
* @param
|
||
* @return
|
||
*/
|
||
public static void insertPafData(String tableName, String applyId, String batNo, String reportID,
|
||
Statement stat, Element cisReport){
|
||
try{
|
||
//遍历集合-组装参数
|
||
Element element = cisReport.element(tableName);
|
||
JSONObject obj = initObj(applyId, batNo, reportID);
|
||
List<DefaultAttribute> praAttributes = element.attributes();
|
||
if(CollectionUtils.isNotEmpty(praAttributes)) {
|
||
for (DefaultAttribute praAttribute : praAttributes) {
|
||
obj.put(praAttribute.getName(), praAttribute.getStringValue().trim());//收集主参
|
||
}
|
||
}
|
||
List<Element> listElements = element.elements();
|
||
//item单独保存,暂不处理item中数据
|
||
List<Element> listSonElements = new ArrayList<>();
|
||
if(CollectionUtils.isNotEmpty(listElements)){
|
||
for(Element fieldElement : listElements){
|
||
if("item".equals(fieldElement.getName())) {
|
||
listSonElements.add(fieldElement);//收集item
|
||
}else {
|
||
obj.put(fieldElement.getName(), fieldElement.getStringValue().trim());//收集主参
|
||
}
|
||
}
|
||
}
|
||
//保存主参数据到表
|
||
excuteSql(tableName, obj, stat);
|
||
//处理item数据
|
||
if(CollectionUtils.isNotEmpty(listSonElements)){
|
||
//item数据需更改表名
|
||
tableName = tableName + "item";
|
||
//初始化obj参数
|
||
obj = initObj(applyId, batNo, reportID);
|
||
for(Element fieldSonElement : listSonElements){
|
||
List<Element> listFieldGraElements = fieldSonElement.elements();
|
||
if(CollectionUtils.isNotEmpty(listFieldGraElements)){
|
||
for(Element fieldGraElement : listFieldGraElements){
|
||
obj.put(fieldGraElement.getName(), fieldGraElement.getStringValue().trim());
|
||
}
|
||
//保存item数据到表
|
||
excuteSql(tableName, obj, stat);
|
||
//初始化obj参数
|
||
obj = initObj(applyId, batNo, reportID);
|
||
}
|
||
}
|
||
}
|
||
}catch (Exception e){
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* obj初始化
|
||
* @param applyId
|
||
* @param batNo
|
||
* @param reportID
|
||
*/
|
||
private static JSONObject initObj(String applyId, String batNo, String reportID){
|
||
JSONObject obj = new JSONObject();
|
||
String id = UUID.randomUUID().toString().replace("-", "");
|
||
obj.put("id", id);
|
||
obj.put("applyId", applyId);
|
||
obj.put("batNo", batNo);
|
||
obj.put("reportID", reportID);
|
||
return obj;
|
||
}
|
||
|
||
|
||
/**
|
||
* 整理sql并保存
|
||
* @param tableName 表名
|
||
* @param obj 参数
|
||
* @param stat jdbc通道
|
||
*/
|
||
private static void excuteSql(String tableName, JSONObject obj, Statement stat){
|
||
try{
|
||
logger.info("insertPafData#excuteSql 添加一条记录:" + tableName.toLowerCase());
|
||
String keys = "";
|
||
String values = "";
|
||
String sql = "INSERT INTO bigdata_py_paf_" + tableName.toLowerCase() + "({keys}) VALUES({values})";
|
||
String[] keysSqlArr = PersonalAntiFraudDataEnum.getValueData(tableName);
|
||
if(null == keysSqlArr){
|
||
logger.warn("insertPafData#excuteSql 当前数据库表的列字段对应关系未配置:" + tableName.toLowerCase());
|
||
return;
|
||
}
|
||
String[] valueArr = new String[keysSqlArr.length];
|
||
//填充数组的每个项为单引号
|
||
Arrays.fill(valueArr, "''");
|
||
Set<String> keySet = obj.keySet();
|
||
for(int i = 0; i < keysSqlArr.length; i++){
|
||
for(String key : keySet){
|
||
if(keysSqlArr[i].toLowerCase().equals(key.toLowerCase())){
|
||
valueArr[i] = "'" + obj.getString(key) + "'";
|
||
}
|
||
}
|
||
}
|
||
keys = Arrays.toString(keysSqlArr).replace("[", "").replace("]", "");
|
||
values = Arrays.toString(valueArr).replace("[", "").replace("]", "");
|
||
sql = sql.replaceAll("\\{keys\\}", keys).replaceAll("\\{values\\}", values);
|
||
logger.info("insertPafData#excuteSql 添加一条记录:[封装sql]:" + sql);
|
||
stat.execute(sql);
|
||
}catch (Exception e){
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|