package com.tenwa.httpclient.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.amarsoft.are.ARE; import com.amarsoft.are.util.StringFunction; import com.amarsoft.awe.Configure; import com.amarsoft.awe.control.model.Page; import com.amarsoft.awe.util.ASResultSet; import com.amarsoft.awe.util.SqlObject; import com.amarsoft.awe.util.Transaction; import com.tenwa.comm.exception.BusinessException; import com.tenwa.httpclient.pboc.FileUtils; import com.tenwa.httpclient.pboc.HttpUtils; import com.tenwa.httpclient.pboc.PbocXmlUtils; import com.tenwa.httpclient.resources.BigDataPropertiesUtil; import com.tenwa.reckon.util.UUIDUtil; import org.dom4j.Element; import org.dom4j.tree.DefaultAttribute; import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; public class PbocDataController { /** * 查询是否存在当前的用户的征信记录 * @param CurPage * @param type * @param Sqlca * @return * @throws Exception */ public String findDataApplyId(Page CurPage, String type, Transaction Sqlca) throws Exception { String count = BigDataPropertiesUtil.get("count"); String sql = "select max(id) applyid, count(1) cou from bigdata_" + type + " where fullname='"+CurPage.getParameter("fullName") +"' and fullcertId='"+CurPage.getParameter("fullcertId")+"' and fullphone='"+CurPage.getParameter("fullphone")+"' and {keyword} group by {gkeyword}"; StringBuffer keyword = new StringBuffer(""); StringBuffer gkeyword = new StringBuffer(""); String FlowNo = ""; for(int i = 1; i <= 5; i ++) { String key = BigDataPropertiesUtil.get("keyword" + i); String keyValue = CurPage.getParameter(key); FlowNo = CurPage.getParameter("FlowNo"); if(key == null || keyValue == null) { continue; } if(keyword.length() != 0) { keyword.append(" and "); gkeyword.append(","); } keyword.append("keyword").append(i).append("='").append(keyValue).append("'"); gkeyword.append("keyword").append(i); } // TODO: 2023/1/31 ExceedTimLlimitApply 这是啥玩意 if("ExceedTimLlimitApply".equals(FlowNo)&&keyword.length() != 0 ){ keyword.append(" and keyword3").append("='").append(FlowNo).append("'"); }else{ keyword.append(" and keyword3 is null"); } sql = sql.replaceAll("\\{keyword\\}", keyword.length() == 0 ? "" : keyword.toString()).replaceAll("\\{gkeyword\\}", gkeyword.length() == 0 ? "" : gkeyword.toString()); ARE.getLog().info("======================sql========================================================"); ARE.getLog().info("======================"+sql+"========================================================"); ARE.getLog().info("======================sql========================================================"); ASResultSet rs = Sqlca.getASResultSet(new SqlObject(sql)); String result = ""; if(rs.next()) { result = "true@" + rs.getString("applyid") + (rs.getString("cou").equals(count) ? "@done" : "@" + rs.getString("cou")); } else { result = "false"; } ARE.getLog().info("======================result========================================================"); ARE.getLog().info("======================"+result+"========================================================"); ARE.getLog().info("======================result========================================================"); return result; } public String findAndSavePbocData(Map params, String table, Transaction Sqlca, String actionType, String applyId, Page CurPage) throws Exception { return this.getAndSavePengyuanData(params, table, Sqlca, actionType, applyId, CurPage, null); } public String getAndSavePengyuanData(Map params, String table, Transaction Sqlca, String actionType, String applyId, Page CurPage, Map id) throws Exception { String type = "html"; ARE.getLog().info("------------------鹏元("+table+")拉取时间----------------------"); ARE.getLog().info(""); ARE.getLog().info(""); ARE.getLog().info("------------------"+ StringFunction.getTodayNow()+"----------------------"); ARE.getLog().info(""); ARE.getLog().info(""); ARE.getLog().info("------------------鹏元("+table+")拉取时间----------------------"); // if("GET".equals(actionType)) { // if(id != null) { // id.put("applyId", applyId); // } // htmlFile = Sqlca.getString(new SqlObject("select html_data from bigdata_" + table + " where id = '" + applyId + "'")); // return htmlFile == null ? "" : htmlFile; // } Connection conn = Sqlca.getConnection(Sqlca); // TODO: 2023/2/1 本地测试暂用 @SuppressWarnings("deprecation") Configure configure = Configure.getInstance(); String fileSavePath = configure.getParameter("FileSavePath") + "/" + "pengyuan" + "/"; // String fileSavePath = "E:\\data"; String FlowNo = CurPage.getParameter("FlowNo"); String fullcardNo = CurPage.getParameter("fullcardNo"); if(fullcardNo==null||"".equals(fullcardNo)){fullcardNo="default";} // TODO: 2023/2/1 生成pboc个人单笔查询请求参数 String personQueryParamStr = PbocXmlUtils.createPersonQueryParamStr(params, CurPage.getAttribute("SubjectId").toString()); type = "xml"; PreparedStatement ps = null; try { ps = conn.prepareStatement("update bigdata_" + table + "_log set " + type + "_url = ?, " + type + "_param = ? where apply_id = ?"); ps.setString(1, BigDataPropertiesUtil.get("pengyuanUrl")); ps.setString(2, personQueryParamStr + "," + BigDataPropertiesUtil.get("user") + "," + BigDataPropertiesUtil.get("password") + "," + type); ps.setString(3, applyId); ps.execute(); } catch(Exception e) { e.printStackTrace(); ARE.getLog().error("数据存储 bigdata_" + table + "_log 失败: ", e); throw new BusinessException("数据存储失败"); } finally { if(ps != null) ps.close(); } ARE.getLog().info("==================" + table + "@" + type + "获取数据===================="); String resXMLReport = this.findQueryReport(personQueryParamStr, type, CurPage.getAttribute("SubjectId").toString()); ARE.getLog().info("==================" + table + "@" + type + "获取数据===================="); JSONObject resJson = PbocXmlUtils.toJson(resXMLReport); JSONObject serviceDataJson = resJson.getJSONObject("service"); JSONObject msgHeadData = serviceDataJson.getJSONObject("msgHead"); JSONObject msgBodyData = serviceDataJson.getJSONObject("msgBody"); String reportId = msgHeadData.getString("resJnlId"); String htmlRptDataStr = msgBodyData.getString("htmlRpt"); String xmlFilePath = FileUtils.createXMLFile(fileSavePath, reportId, resXMLReport); String htmlFilePath = FileUtils.createHtmlFile(fileSavePath,reportId, htmlRptDataStr); ARE.getLog().info(htmlFilePath); ARE.getLog().info(xmlFilePath); StringBuffer keyword = new StringBuffer(); StringBuffer keywordValue = new StringBuffer(); for(int i = 1; i <= 5; i ++) { String key = BigDataPropertiesUtil.get("keyword" + i); String keyValue = CurPage.getParameter(key); if(key == null || keyValue == null) { continue; } if(keyword.length() != 0) { keyword.append(","); keywordValue.append(","); } keyword.append("keyword").append(i); keywordValue.append("'").append(keyValue).append("'"); } if("ExceedTimLlimitApply".equals(FlowNo)&&keyword.length() != 0){ keyword.append(",keyword3"); keywordValue.append(",'"+FlowNo+"'"); } SqlObject sql = new SqlObject(("insert into bigdata_" + table + " (id," + keyword.toString() + ",xml_data,html_data," + "inputuserid,inputorgid,inputtime,fullName,fullcertId,fullphone,fullcardNo) values('" + applyId + "'," + keywordValue.toString() + ",'" + xmlFilePath + "','" + htmlFilePath + "','" + CurPage.getUserId() + "','" + CurPage.getUser().getOrgID() + "','" + StringFunction.getTodayNow() +"','" +CurPage.getParameter("fullName")+"','"+CurPage.getParameter("fullcertId")+"','"+CurPage.getParameter("fullphone")+"','"+fullcardNo+"')").replaceAll(":", "△")); sql.setDebugSql(sql.getDebugSql().replaceAll("△", ":")); sql.setOriginalSql(sql.getOriginalSql().replaceAll("△", ":")); sql.setRunSql(sql.getRunSql().replaceAll("△", ":")); ARE.getLog().error("****************************鹏元大数据************************"); String insertsql =sql.getOriginalSql(); ARE.getLog().error(insertsql); ARE.getLog().error("****************************鹏元大数据************************"); Sqlca.executeSQL(sql); return htmlFilePath; } public String findQueryReport(String params, String type, String SubjectId) throws Exception { ARE.getLog().info("========================鹏元请求参数=================="); ARE.getLog().info(params); ARE.getLog().info("========================鹏元请求参数=================="); String result = ""; try{ String url = "http://9.35.68.228:9090/pbcrs/api/msgDispatchController/postProcess"; result = HttpUtils.sendPost(url, params); } catch(Exception e) { e.printStackTrace(); throw new BusinessException("获取大数据失败"); } return result; } @SuppressWarnings("unchecked") public static void savePengyuanResultData(JSONObject dataType, Element parentElement, String elementName, Statement stat, Map otherFields, Map dateFields, List stringFields) throws Exception { Element element = parentElement.element(elementName); if(element == null ){ return ; } JSONObject obj = new JSONObject(); List praAttributes = element.attributes(); for(DefaultAttribute praAttribute : praAttributes) { obj.put(praAttribute.getName(), praAttribute.getStringValue().trim()); } if(!obj.isEmpty()) { stat.execute(getInsertSql(obj, "bigdata_py_result", null, otherFields, true, dateFields, stringFields)); otherFields.put("RESULT_ID", otherFields.remove("ID")); } savePengyuanResultSubTable(dataType, element, stat, otherFields, dateFields, stringFields); otherFields.remove("RESULT_ID"); } @SuppressWarnings("unchecked") public static void savePengyuanResultSubTable(JSONObject dataType, Element parentElement, Statement stat, Map otherFields, Map dateFields, List stringFields) throws SQLException { if(dataType != null) { JSONObject obj = null; if("table".equals(dataType.get("type"))) { List fieldList = (List)dataType.get("field"); obj = new JSONObject(); for(String field : fieldList) { Element fieldElement = parentElement.element(field); if(fieldElement != null) { obj.put(fieldElement.getName(), fieldElement.getStringValue().trim()); } } if(!obj.isEmpty()) { stat.execute(getInsertSql(obj, dataType.getString("tableName"), null, otherFields, true, dateFields, stringFields)); otherFields.remove("ID"); } } else if("arrayTable".equals(dataType.get("type"))) { List fieldList = (List)dataType.get("field"); List items = parentElement.elements("item"); if(items != null) { for(Element el : items) { obj = new JSONObject(); for(String field : fieldList) { Element fieldElement = el.element(field); if(fieldElement != null) { obj.put(fieldElement.getName(), fieldElement.getStringValue().trim()); } } if(!obj.isEmpty()) { stat.execute(getInsertSql(obj, dataType.getString("tableName"), null, otherFields, true, dateFields, stringFields)); otherFields.remove("ID"); } } } } else { JSONArray subTable = dataType.getJSONArray("subTable"); if(subTable != null) { for(int i = 0; i < subTable.size(); i ++) { JSONObject subObject = subTable.getJSONObject(i); Element subElement = parentElement.element(subObject.getString("name")); if(subElement != null) { savePengyuanResultSubTable(subObject, subElement, stat, otherFields, dateFields, stringFields); } } } } } } public static void saveDetail(JSONObject obj, String dataName, String tableName, String tableListName, List excludeFields, Map detailOtherFields, Map arrayOtherFields, Statement stat) throws SQLException { excludeFields.clear(); excludeFields.add(dataName.toUpperCase()); stat.execute(getInsertSql(obj, tableName, excludeFields, detailOtherFields)); arrayOtherFields.put("PID", detailOtherFields.remove("ID")); JSONArray array = obj.getJSONArray(dataName); if(array != null) { for(int k = 0; k < array.size(); k ++) { stat.execute(getInsertSql(array.getJSONObject(k), tableListName, excludeFields, arrayOtherFields)); arrayOtherFields.remove("ID"); } } } public static void saveDetailData(JSONObject obj, String dataName, String tableName, String tableListName, List excludeFields, Map detailOtherFields, Map arrayOtherFields, Statement stat) throws SQLException { excludeFields.clear(); excludeFields.add(dataName.toUpperCase()); excludeFields.add("DATA"); stat.execute(getInsertSql(obj, tableName, excludeFields, detailOtherFields)); String pid = detailOtherFields.remove("ID"); JSONArray array = obj.getJSONArray(dataName); List dataExcludeField = new ArrayList(); if(array != null) { for(int k = 0; k < array.size(); k ++) { arrayOtherFields.put("PID", pid); JSONObject data = array.getJSONObject(k); stat.execute(getInsertSql(data, tableListName, excludeFields, arrayOtherFields)); arrayOtherFields.put("PID", arrayOtherFields.remove("ID")); JSONArray dataArray = data.getJSONArray("data"); if(dataArray == null) { continue; } for(int i = 0; i < dataArray.size(); i ++) { JSONObject daObj = new JSONObject(); daObj.put("data", dataArray.get(i)); stat.execute(getInsertSql(daObj, "bigdata_td_data", dataExcludeField, arrayOtherFields)); arrayOtherFields.remove("ID"); } } } } public static String getInsertSql(JSONObject obj, String tableName, List excludeFields, Map otherFields) { return getInsertSql(obj, tableName, excludeFields, otherFields, true, null, null); } public static String getInsertSql(JSONObject obj, String tableName, List excludeFields, Map otherFields, boolean isCheckNumber, Map dateFields, List stringFields) { String sql = "INSERT INTO " + tableName.toUpperCase() + "({keys}) VALUES({values})"; String keys = ""; String values = ""; Set> fields = obj.entrySet(); if(fields.size() == 0) { return ""; } boolean flag = false; String id = ""; for(Map.Entry field : fields) { String key = field.getKey(); if("ID".equals(key.toUpperCase())) { id = (String)field.getValue(); flag = true; } Object value = field.getValue(); if(excludeFields != null && excludeFields.contains(key.toUpperCase())) { continue; } if(keys.length() > 0) { keys += ","; values += ","; } keys += changeKey(key, true);; if(value != null) { String valueString = value + ""; if(isCheckNumber && isNumber(valueString)) { if(stringFields != null && stringFields.contains(key)) { values += changeStringToDate(key, valueString, dateFields); } else { values += valueString; } } else { values += changeStringToDate(key, valueString, dateFields); } } else { values += "''"; } } if(!otherFields.isEmpty()) { for(Map.Entry otherField : otherFields.entrySet()) { if(keys.length() > 0) { keys += ","; values += ","; } keys += otherField.getKey().toUpperCase(); values += "'" + otherField.getValue() + "'"; } } if(!flag) { id = UUIDUtil.getUUID(); keys += ",ID"; values += ",'" + id + "'"; } otherFields.put("ID", id); sql = sql.replaceAll("\\{keys\\}", keys).replaceAll("\\{values\\}", values); ARE.getLog().info("[执行]" + sql); return sql; } public static boolean isNumber(String value) { try { new BigDecimal(value); } catch(Exception e) { return false; } return true; } public static String changeStringToDate(String key, String value, Map dateFields) { if(dateFields != null) { for(Map.Entry dateField : dateFields.entrySet()) { if(dateField.getKey().equals(key)) { // return "DATE_FORMAT('" + value + "', '" + dateField.getValue() + "')"; return "'" + value + "'"; } } return "'" + value + "'"; } return "'" + value + "'"; } public static String changeKey(String key) { return PbocDataController.changeKey(key, false); } public static String changeKey(String key, boolean toUpperCase) { String[] keys = key.split(""); boolean flag = false; if(flag) { if(toUpperCase) { return key.toUpperCase(); } else { return key; } } key = ""; int lockField = -1; ot : for(int k = 0; k < keys.length; k ++) { if("".equals(keys[k]) || keys[k] == null) { continue; } if(lockField == k) { key += keys[k].toLowerCase(); lockField = -1; continue; } if(keys[k].matches("[0-9]") && lockField == -1) { int i = k + 1;; for(; i < keys.length; i ++) { if("p".equals(keys[i])) { break; } if(Character.isUpperCase(keys[i].charAt(0))) { lockField = i; key += "_" + keys[k]; continue ot; } } } if(k == keys.length -2 && "I".equals(keys[k]) && ("I".equals(keys[k + 1]) || "D".equals(keys[k + 1]))) { key += keys[k].toLowerCase() + keys[k + 1].toLowerCase(); break; } else if(Character.isUpperCase(keys[k].charAt(0))) { key += "_" + keys[k].toLowerCase(); } else { key += keys[k]; } } return toUpperCase ? key.toUpperCase() : key; } }