package com.product.data.sync.service; import com.alibaba.fastjson.JSONObject; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldMetaEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.exception.BaseException; import com.product.core.permission.PermissionService; import com.product.core.service.support.AbstractBaseService; import com.product.core.service.support.QueryFilterService; import com.product.core.transfer.Transactional; import com.product.data.sync.config.SystemCode; import com.product.data.sync.service.ide.IFunctionSynchrService; import com.product.data.sync.util.DataManipulationUtils; import com.product.util.BaseDaoServiceImpl; import com.product.util.BaseUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; /** * Copyright LX * * @Title: FunctionSynchrService * @Project: product-server * @date: 2021-08-30 14:19 * @author: ZhouJie * @Description: 开发库功能同步 */ @Service public class FunctionSynchrService extends AbstractBaseService implements IFunctionSynchrService { @Autowired public BaseDao baseDao; @Autowired PermissionService permissionService; @Autowired QueryFilterService queryFilterService; /** * 获取JDBC连接 * * @param fs * @return * @throws BaseException */ public Connection getConnection(FieldSetEntity fs) throws BaseException, SQLException, ClassNotFoundException { String databaseType = fs.getString("type");//数据库类型 String ipAddress = fs.getString("IP");//ip地址 String databaseName = fs.getString("database_name");//数据库名称 String portNumber = fs.getString("port");//端口号 String userName = fs.getString("username");//用户名 String userPassword = fs.getString("password");//密码 String instantiation = fs.getString("");//实例名 String url; String diver; if ("mysql".equals(databaseType)) { diver = "com.mysql.cj.jdbc.Driver"; url = "jdbc:mysql://" + ipAddress + ":" + portNumber + "/" + databaseName + "?useSSL=false&serverTimezone=UTC"; } else if ("oracle".equals(databaseType)) { diver = "oracle.jdbc.driver.OracleDriver"; url = "jdbc:oracle:thin:@" + ipAddress + ":" + portNumber + ":orcl"; } else if ("sqlserver".equals(databaseType)) { diver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; url = "jdbc:sqlserver://" + ipAddress + ":" + portNumber + ";DataBaseName=" + databaseName; } else if ("informix".equals(databaseType)) { diver = "com.informix.jdbc.IfxDriver"; url = "jdbc:informix-sqli://" + ipAddress + ":" + portNumber + "/" + databaseName + ":informixserver=" + instantiation; } else { throw new BaseException(SystemCode.SYSTEM_UNKNOWN_DATABASE_TYPE.getValue(), SystemCode.SYSTEM_UNKNOWN_DATABASE_TYPE.getText()); } //获取jdbc连接 return DataManipulationUtils.getConnection(diver, url, userName, userPassword); } /** * 开发库的模块功能树 * * @return * @throws BaseException */ public DataTableEntity getSyncTree(FieldSetEntity fse) throws BaseException, SQLException, ClassNotFoundException { StringBuilder str = new StringBuilder(); // str.append(" SELECT uuid,product_name `name`,tricode,null tricode_parent,1 type,concat(tricode,'_',1) only_key FROM product_sys_products "); // str.append(" union all select a.uuid,`name`,b.tricode,b.tricode_parent,type,only_key from ( "); // str.append(" select uuid,function_name `name`,2 type,concat(tricode,'_',2) only_key FROM product_sys_functions WHERE data_type=2 "); // str.append(" union all select uuid,function_name `name`,3 type,concat(tricode,'_',3) only_key FROM product_sys_functions where data_type=1) a join "); // str.append(""product_sys_module_function_code b on (( a.type=2 and b.data_type=1 ) or (a.type=3 and b.data_type=2)) and a.uuid=b.data_uuid "); // str.append(" ORDER BY type ,tricode_parent asc,tricode,length(tricode) asc "); str.append(" SELECT uuid,product_name `name`,tricode,null tricode_parent,1 type,concat(tricode,'_',1) only_key FROM product_sys_products "); str.append(" union all select a.uuid,`name`,tricode,tricode_parent,type,only_key from ( "); str.append(" select uuid,tricode,tricode_parent,function_name `name`,2 type,concat(tricode,'_',2) only_key FROM product_sys_functions WHERE data_type=2 "); str.append(" union all select uuid,tricode,tricode_parent,function_name `name`,3 type,concat(tricode,'_',3) only_key FROM product_sys_functions where data_type=1) a "); str.append(" ORDER BY type ,tricode_parent asc,tricode,length(tricode) asc "); Connection conn = getConnection(fse); DataTableEntity dt = null; try { dt = BaseDaoServiceImpl.getDataTable(conn, str.toString(), new Object[]{}); } catch (Exception e) { DataManipulationUtils.close(null, null, conn); throw e; } return dt; } /** * 当前库的模块功能树 * * @return * @throws BaseException */ public DataTableEntity getNowTree(FieldSetEntity fse) throws BaseException, SQLException, ClassNotFoundException { StringBuilder str = new StringBuilder(); // str.append(" SELECT uuid,product_name `name`,tricode,null tricode_parent,1 type,concat(tricode,'_',1) only_key FROM product_sys_products "); // str.append(" union all select a.uuid,`name`,b.tricode,b.tricode_parent,type,only_key from ( "); // str.append(" select uuid,function_name `name`,2 type,concat(tricode,'_',2) only_key FROM product_sys_functions WHERE data_type=2 "); // str.append(" union all select uuid,function_name `name`,3 type,concat(tricode,'_',3) only_key FROM product_sys_functions where data_type=1 ) a join "); // str.append(""product_sys_module_function_code b on (( a.type=2 and b.data_type=1 ) or (a.type=3 and b.data_type=2)) and a.uuid=b.data_uuid "); // str.append(" ORDER BY type ,tricode_parent asc,tricode,length(tricode) asc "); str.append(" SELECT uuid,product_name `name`,tricode,null tricode_parent,1 type,concat(tricode,'_',1) only_key FROM product_sys_products "); str.append(" union all select a.uuid,`name`,tricode,tricode_parent,type,only_key from ( "); str.append(" select uuid,tricode,tricode_parent,function_name `name`,2 type,concat(tricode,'_',2) only_key FROM product_sys_functions WHERE data_type=2 "); str.append(" union all select uuid,tricode,tricode_parent,function_name `name`,3 type,concat(tricode,'_',3) only_key FROM product_sys_functions where data_type=1) a "); str.append(" ORDER BY type ,tricode_parent asc,tricode,length(tricode) asc "); DataTableEntity dt = baseDao.listTable(str.toString(), new Object[]{}); return dt; } public boolean verificationCode() { String b = baseDao.getFieldSetEntityByFilter("product_sys_datamodel_field", "table_uuid='product_sys_functions' and field_name='tricode'", new String[]{}, false).getString("field_type"); String c = baseDao.getFieldSetEntityByFilter("product_sys_datamodel_field", "table_uuid='product_sys_products' and field_name='tricode'", new String[]{}, false).getString("field_type"); if ( b.equals("string") && c.equals("string")) { return true; } else { return false; } } /** * 功能同步 * * @return * @throws BaseException */ @Transactional public FieldSetEntity syncFuntion(FieldSetEntity fse) throws BaseException, SQLException, ClassNotFoundException { DataTableEntity dt = fse.getSubDataTable("function_info"); if (dt != null) { JSONObject products_uuid = new JSONObject(); JSONObject modules_uuid = new JSONObject(); JSONObject functions_uuid = new JSONObject(); for (int i = 0; i < dt.getRows(); i++) { FieldSetEntity fsub = dt.getFieldSetEntity(i); if ("1".equals(fsub.getString("type"))) { products_uuid.put(fsub.getString("tricode"), fsub.getString("upADD")); } else if ("2".equals(fsub.getString("type"))) { modules_uuid.put(fsub.getString("tricode"), fsub.getString("upADD")); } else if ("3".equals(fsub.getString("type"))) { functions_uuid.put(fsub.getString("tricode"), fsub.getString("upADD")); } } ArrayList products_uuid_list = new ArrayList<>(); ArrayList modules_uuid_list = new ArrayList<>(); ArrayList functions_uuid_list = new ArrayList<>(); for (String str : products_uuid.keySet()) { products_uuid_list.add(str); } for (String str : modules_uuid.keySet()) { modules_uuid_list.add(str); } for (String str : functions_uuid.keySet()) { functions_uuid_list.add(str); } Connection conn = getConnection(fse); DataTableEntity productsDt = null; if (products_uuid_list.size() > 0) { productsDt = BaseDaoServiceImpl.getDataTable(conn, "product_sys_products", BaseUtil.buildQuestionMarkFilter("tricode", products_uuid_list.size(), true), products_uuid_list.toArray()); } DataTableEntity modulesDt = null; if (modules_uuid_list.size() > 0) { modulesDt = BaseDaoServiceImpl.getDataTable(conn, "product_sys_functions","data_type=2 and" +BaseUtil.buildQuestionMarkFilter("tricode", modules_uuid_list.size(), true), modules_uuid_list.toArray()); // FieldMetaEntity f = new FieldMetaEntity(); // f.setTableName(new Object[] { "product_sys_modules" }); // modulesDt.setMeta(f); } DataTableEntity functionsDt = null; if (functions_uuid_list.size() > 0) { functionsDt = BaseDaoServiceImpl.getDataTable(conn, "product_sys_functions","data_type=1 and" + BaseUtil.buildQuestionMarkFilter("tricode", functions_uuid_list.size(), true), functions_uuid_list.toArray()); } saveDataDT(productsDt, products_uuid); saveDataDT(modulesDt, modules_uuid); saveDataDT(functionsDt, functions_uuid); } return fse; } public void saveDataDT(DataTableEntity dt, JSONObject js) { if (dt != null) { for (int i = 0; i < dt.getRows(); i++) { FieldSetEntity fs = dt.getFieldSetEntity(i); if ("1".equals(js.getString(fs.getString("tricode")))) { if ("行政办公".equals(fs.getString("module_name"))) { System.out.println(111); } fs.setValue("uuid", null); if ("product_sys_products".equals(fs.getTableName())) { fs.setValue("product_id", null); baseDao.add(fs); continue; } else if ("product_sys_functions".equals(fs.getTableName())) { fs.setValue("function_id", null); // String version_uuid = productsMap.getString(fs.getString("version_uuid")); // if (null != version_uuid) { // fs.setValue("product_uuid", version_uuid); // } baseDao.add(fs); // modulesMap.put(fs.getString("tricode"), fs.getString("uuid")); continue; } // else if ("product_sys_functions".equals(fs.getTableName())&&"1".equals(fs.getString("data_type"))) { // fs.setValue("function_id", null); // String tricode = fs.getString("tricode"); // String modules_uuid = modulesMap.getString(tricode.substring(0, tricode.length() - 4)); // if (null != modules_uuid) { // fs.setValue("module_uuid", modules_uuid); // } // baseDao.add(fs); // continue; } } // baseDao.update(fs); // } } } public static void main(String[] args) { String code = "001-017-000"; String substring = code.substring(0, code.length() - 4); System.out.println(substring); } }