You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
14 KiB

2 years ago
package com.fr.plugin.utils;
import com.fanruan.api.data.ConnectionKit;
import com.fanruan.api.log.LogKit;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory;
import com.fr.log.FineLoggerProvider;
import java.sql.Date;
import java.sql.*;
import java.util.*;
public class DBUtils {
String db_name = "";
private static FineLoggerProvider logger = FineLoggerFactory.getLogger();
public DBUtils() {
this.db_name = "finebi";
}
public com.fr.data.impl.Connection getDbConnect() {
return ConnectionKit.getConnection(db_name);
}
public List<Map<String, Object>> select(String sql, Object... params) {
logger.info("query data by sql:" + sql + Arrays.toString(params));
try {
com.fr.data.impl.Connection dbConnect = getDbConnect();
Connection con = dbConnect.createConnection();
PreparedStatement preparedStatement = con.prepareStatement(sql);
setParams(preparedStatement, params);
ResultSet rs = preparedStatement.executeQuery(sql);
// 获得记录的详细信息,然后获得总列数
ResultSetMetaData resMetaData = rs.getMetaData();
int colNum = resMetaData.getColumnCount();
// 用对象保存数据
String name = "";
String value = "";
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
while (rs.next()) {
Map<String, Object> cells = new HashMap<String, Object>();
for (int i = 0; i < colNum; i++) {
name = resMetaData.getColumnLabel(i);
if (cells.get(name) != null) {
name = resMetaData.getColumnLabel(i);
}
if (rs.getObject(i) != null && resMetaData.getColumnTypeName(i).equals("DATETIME") || resMetaData.getColumnTypeName(i).equals("TIMESTAMP")) {
value = rs.getObject(i).toString();
cells.put(name, value.substring(0, value.length() - 2));
} else {
cells.put(name, rs.getString(i));
}
}
list.add(cells);
}
// 释放数据库资源
rs.close();
preparedStatement.close();
con.close();
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public int exec(String sql, String date) throws Exception {
logger.info("query data by sql:{} 时间:{}", sql, date);
Connection con = null;
CallableStatement call = null;
try {
com.fr.data.impl.Connection dbConnect = getDbConnect();
con = dbConnect.createConnection();
call = con.prepareCall(sql);
call.registerOutParameter(1, Types.INTEGER);
call.execute();
Integer ret = call.getInt(1);
return ret;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (call != null) {
call.close();
}
if (con != null) {
con.close();
}
}
}
public Map<String, Object> findOneRow(String sql, Object... params) {
List<Map<String, Object>> select = select(sql, params);
if (select != null) {
if (!select.isEmpty()) {
return select.get(0);
}
}
return null;
}
public boolean checkExist(String sql, Object... params) throws Exception {
Connection connection = getDbConnect().createConnection();
PreparedStatement pstmt = connection.prepareStatement(sql);
setParams(pstmt, params);
try {
ResultSet resultSet = pstmt.executeQuery();
if (resultSet.next()) {
return resultSet.getInt(1) > 0;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.close();
}
return false;
}
private void setParams(PreparedStatement pstmt, Object... params) throws SQLException {
if (params.length > 0) {
int length = params.length;
for (int i = 1; i <= length; i++) {
pstmt.setObject(i, params[i - 1]);
}
}
}
public int exSqlUpdate(String sql, Object... params) throws Exception {
logger.info("update data by sql:" + sql + " params " + Arrays.toString(params));
PreparedStatement pstmt = null;
Connection connection = null;
try {
com.fr.data.impl.Connection dbConnect = getDbConnect();
connection = dbConnect.createConnection();
pstmt = connection.prepareStatement(sql);
setParams(pstmt, params);
int i = pstmt.executeUpdate();
return i;
} catch (Exception e) {
FineLoggerFactory.getLogger().error("执行更新SQL报错: sql:{}", e, sql);
} finally {
if (pstmt != null) {
pstmt.close();
}
if (connection != null) {
connection.close();
}
}
return 0;
}
/**
* 取查询结果集字段
*
* @param sql
* @param params
* @return
* @throws Exception
*/
public List<Map<String, String>> exQuery(String sql, Object... params) throws Exception {
logger.info("query data by sql:" + sql + " params " + Arrays.toString(params));
com.fr.data.impl.Connection dbConnect = getDbConnect();
Connection connection = dbConnect.createConnection();
PreparedStatement pstmt = connection.prepareStatement(sql);
setParams(pstmt, params);
ResultSet resultSet = pstmt.executeQuery();
ResultSetMetaData resMetaData = resultSet.getMetaData();
int columnCount = resMetaData.getColumnCount();
List<Map<String, String>> arrs = new ArrayList<Map<String, String>>();
while (resultSet.next()) {
String name;
String value;
Map<String, String> one = new HashMap<String, String>();
for (int i = 1; i <= columnCount; i++) {
name = resMetaData.getColumnLabel(i);
if (one.get(name) != null) {
name = resMetaData.getColumnLabel(i);
}
if (resultSet.getObject(i) != null && resMetaData.getColumnTypeName(i).equals("DATETIME") || resMetaData.getColumnTypeName(i).equals("TIMESTAMP")) {
value = resultSet.getObject(i).toString();
one.put(name, value.substring(0, value.length() - 2));
} else {
one.put(name, resultSet.getString(i));
}
}
arrs.add(one);
}
pstmt.close();
connection.close();
return arrs;
}
private static final String sqlJSON = "insert into TPV_OMP_MATERIAL_DATA ( " +
"TPV_MODEL, " +
"SAFETY_MODEL,\n" +
"MATERIAL_DESC,\n" +
"GROSS_WEIGHT,\n" +
"NET_WEIGHT,\n" +
"VOLUME,\n" +
"PRODUCT_GROUP,\n" +
"MATERIAL_GROUP,\n" +
"BASE_METER_UNIT,\n" +
"CABINET_QTY,\n" +
"CUSTOMER_MODEL,\n" +
"BG,\n" +
"BRAND,\n" +
"SALES_REGION,\n" +
"OEM_MODE,\n" +
"PRODUCT_SERIES,\n" +
"PRODUCT_SERIES_CODE,\n" +
"GET_MODE,\n" +
"SHIPMENT_CATEGORY,\n" +
"WITHOUT_PANEL,\n" +
"SIZE,\n" +
"APPEARANCE_RATIO,\n" +
"BACKLIGHT_TYPE,\n" +
"PANEL_SUPPLY,\n" +
"PANEL_MAKER,\n" +
"FUNCTION_CATEGORY,\n" +
"SIGNAL_CLASS,\n" +
"CUSTOMER_NAME,\n" +
"RESOLUTION,\n" +
"SUB_BG,\n" +
"SMART_TV,\n" +
"DOBLY,\n" +
"CUSTOMERS_CLASS_CODE,\n" +
"COMMODITY_CODE,\n" +
"DECLARATION_ELEMENT,\n" +
"LCM_MODEL,\n" +
"LCM_MODEL_DESC,\n" +
"USER_NAME,\n" +
"CREATE_TIME) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
public void saveJSONArr(JSONArray jsonArray, String userName) throws SQLException {
com.fr.data.impl.Connection dbConnect = getDbConnect();
Connection con = null;
try {
con = dbConnect.createConnection();
con.setAutoCommit(false);
int size = jsonArray.size();
for (int i = 0; i < size; i++) {
JSONObject o = jsonArray.getJSONObject(i);
deleteByJSON(o,con);
saveJSON(o, userName, con);
}
con.commit();
} catch (Exception e) {
LogKit.error("SQL 异常 :",e);
try {
if (con != null) {
con.rollback();
}
}catch (Exception e12){
}
} finally {
if (con != null) {
try {
con.close();
}catch (Exception e){
}
}
}
}
String deleteJSONSQL = "delete from TPV_OMP_MATERIAL_DATA where TPV_MODEL=?";
private Integer deleteByJSON(JSONObject o, Connection con) throws SQLException {
logger.info("update data by sql:" + deleteJSONSQL + " params " + o);
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(deleteJSONSQL);
pstmt.setString(1, o.getString("MATNR"));
int i = pstmt.executeUpdate();
return i;
} catch (Exception e) {
FineLoggerFactory.getLogger().error("执行更新SQL报错: sql:{}", e, sqlJSON);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
}catch (Exception e){
}
}
return 0;
}
public int saveJSON(JSONObject o, String userName, Connection connection) throws SQLException {
logger.info("update data by sql:" + sqlJSON + " params " + o);
PreparedStatement pstmt = null;
try {
pstmt = connection.prepareStatement(sqlJSON);
pstmt.setString(1, o.getString("MATNR"));
pstmt.setString(2, o.getString("BISMT"));
pstmt.setString(3, o.getString("MAKTX"));
pstmt.setString(4, o.getString("BRGEW"));
pstmt.setString(5, o.getString("BTGEW"));
pstmt.setString(6, o.getString("VOLUM"));
pstmt.setString(7, o.getString("SPART"));
pstmt.setString(8, o.getString("MATKL"));
pstmt.setString(9, o.getString("MEINS"));
pstmt.setString(10, o.getString("NORMT"));
pstmt.setString(11, o.getString("FERTH"));
pstmt.setString(12, o.getString("Z_BRDTYPE"));
pstmt.setString(13, o.getString("Z_BRDID"));
pstmt.setString(14, o.getString("Z_REGION"));
pstmt.setString(15, o.getString("Z_CMNFTYPE"));
pstmt.setString(16, o.getString("Z_PRDLINE"));
pstmt.setString(17, o.getString("Z_STYLEID"));
pstmt.setString(18, o.getString("Z_SRCTYPE"));
pstmt.setString(19, o.getString("Z_ASSM"));
pstmt.setString(20, o.getString("Z_WPNL"));
pstmt.setString(21, o.getString("Z_SIZE"));
pstmt.setString(22, o.getString("Z_RATIO"));
pstmt.setString(23, o.getString("Z_PNLLIGHT"));
pstmt.setString(24, o.getString("Z_PNLSUPTYPE"));
pstmt.setString(25, o.getString("Z_PNLBRDID"));
pstmt.setString(26, o.getString("Z_FUNTYPE"));
pstmt.setString(27, o.getString("Z_SGNTYPE"));
pstmt.setString(28, o.getString("Z_CUSTNAME"));
pstmt.setString(29, o.getString("Z_PARSE"));
pstmt.setString(30, o.getString("Z_BG"));
pstmt.setString(31, o.getString("Z_SMARTTV"));
pstmt.setString(32, o.getString("Z_DOBLY"));
pstmt.setString(33, o.getString("GEBSQ"));
pstmt.setString(34, o.getString("HSCODE"));
pstmt.setString(35, o.getString("CMODEL"));
pstmt.setString(36, o.getString("MATNR_LCM"));
pstmt.setString(37, o.getString("MAKTX_LCM"));
pstmt.setString(38, userName);
pstmt.setDate(39, new Date(System.currentTimeMillis()));
int i = pstmt.executeUpdate();
return i;
} catch (Exception e) {
FineLoggerFactory.getLogger().error("执行更新SQL报错: sql:{}", e, sqlJSON);
} finally {
if (pstmt != null) {
pstmt.close();
}
}
return 0;
}
private static final String sqlexJSON = "INSERT INTO TPV_OMP_MATERIAL_DATA_STATUS (FACTORY,TPV_MODEL,STATUS,USER_NAME,CREATE_TIME) VALUES (?,?,?,?,?)";
public int saveReJSON(JSONObject o, String userName) throws SQLException {
logger.info("update data by sql:" + sqlexJSON + " params " + o);
PreparedStatement pstmt = null;
Connection connection = null;
try {
com.fr.data.impl.Connection dbConnect = getDbConnect();
connection = dbConnect.createConnection();
pstmt = connection.prepareStatement(sqlexJSON);
pstmt.setString(1, o.getString("WERKS"));
pstmt.setString(2, o.getString("MATNR"));
pstmt.setString(3, o.getString("RETURN"));
pstmt.setString(4, userName);
pstmt.setDate(5, new Date(System.currentTimeMillis()));
int i = pstmt.executeUpdate();
return i;
} catch (Exception e) {
FineLoggerFactory.getLogger().error("执行更新SQL报错: sql:{}", e, sqlJSON);
} finally {
if (pstmt != null) {
pstmt.close();
}
if (connection != null) {
connection.close();
}
}
return 0;
}
}