Merge branch 'test' of http://git.estsh.com/i3-IMPP/i3plus-core into test
						commit
						261d494d24
					
				| @ -0,0 +1,85 @@ | ||||
| package cn.estsh.i3plus.core.api.iservice.base; | ||||
| 
 | ||||
| import cn.estsh.i3plus.pojo.base.bean.ListPager; | ||||
| import cn.estsh.i3plus.pojo.base.common.Pager; | ||||
| import cn.estsh.i3plus.pojo.platform.bean.CoreDataSource; | ||||
| import io.swagger.annotations.ApiOperation; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * @Description : | ||||
|  * @Reference : | ||||
|  * @Author : Castle | ||||
|  * @CreateDate : 2022/1/7 10:38 | ||||
|  * @Modify: | ||||
|  **/ | ||||
| public interface ICoreDataSourceService { | ||||
| 
 | ||||
|         /** | ||||
|          * 修改数据源对象 | ||||
|          * | ||||
|          * @param dataObject | ||||
|          * @return | ||||
|          */ | ||||
|         @ApiOperation(value = "修改数据对象接口") | ||||
|         CoreDataSource updateDataObject(CoreDataSource dataObject); | ||||
| 
 | ||||
|         /** | ||||
|          * 保存数据源 | ||||
|          * | ||||
|          * @param source | ||||
|          * @return | ||||
|          */ | ||||
|         @ApiOperation(value = "保存数据对象属性接口") | ||||
|         CoreDataSource saveDataSource(CoreDataSource source); | ||||
| 
 | ||||
|         /** | ||||
|          * 获取数据对象详细信息 | ||||
|          * | ||||
|          * @param id 数据对象ID | ||||
|          * @return 数据对象 | ||||
|          */ | ||||
|         @ApiOperation(value = "获取数据对象信息") | ||||
|         CoreDataSource getCoreDatasourceObject(Long id); | ||||
| 
 | ||||
| 
 | ||||
|         /** | ||||
|          * 检查数据源唯一性 | ||||
|          * | ||||
|          * @param source | ||||
|          */ | ||||
|         @ApiOperation(value = "数据源唯一检查") | ||||
|         void checkBfDataSourceOnly(CoreDataSource source); | ||||
| 
 | ||||
| 
 | ||||
|         /** | ||||
|          * 查询所有数据对象接口 | ||||
|          * | ||||
|          * @param pager | ||||
|          * @return | ||||
|          */ | ||||
|         @ApiOperation(value = "查询所有数据对象接口") | ||||
|         ListPager<CoreDataSource> queryDataObjectByPager(CoreDataSource coreDataSource, Pager pager); | ||||
| 
 | ||||
|         /** | ||||
|          * 获取所有数据源对象 | ||||
|          * | ||||
|          * @return | ||||
|          */ | ||||
|         @ApiOperation(value = "获取所有数据源对象") | ||||
|         List<CoreDataSource> listAll(); | ||||
| 
 | ||||
|         /** | ||||
|          * 删除数据源 | ||||
|          * @param id | ||||
|          */ | ||||
|         void delete(Long id); | ||||
| 
 | ||||
|         /** | ||||
|          * 获取所有可用datasource的code和id | ||||
|          * @return | ||||
|          */ | ||||
|         List<CoreDataSource> list(); | ||||
| 
 | ||||
|     } | ||||
| @ -0,0 +1,176 @@ | ||||
| package cn.estsh.i3plus.core.apiservice.controller.base; | ||||
| 
 | ||||
| import cn.estsh.i3plus.core.api.iservice.base.ICoreDataSourceService; | ||||
| import cn.estsh.i3plus.core.apiservice.configuration.CoreJdbcTemplateConfig; | ||||
| import cn.estsh.i3plus.platform.common.convert.ConvertBean; | ||||
| import cn.estsh.i3plus.platform.common.util.PlatformConstWords; | ||||
| import cn.estsh.i3plus.pojo.base.bean.ListPager; | ||||
| import cn.estsh.i3plus.pojo.base.common.Pager; | ||||
| import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil; | ||||
| import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil; | ||||
| import cn.estsh.i3plus.pojo.base.enumutil.WmsEnumUtil; | ||||
| import cn.estsh.i3plus.pojo.platform.bean.CoreDataSource; | ||||
| import cn.estsh.impp.framework.boot.auth.AuthUtil; | ||||
| import cn.estsh.impp.framework.boot.exception.ImppBusiException; | ||||
| import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder; | ||||
| import cn.estsh.impp.framework.boot.util.LocaleUtils; | ||||
| import cn.estsh.impp.framework.boot.util.ResultBean; | ||||
| import cn.estsh.impp.framework.boot.util.ValidatorBean; | ||||
| import io.swagger.annotations.Api; | ||||
| import io.swagger.annotations.ApiOperation; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * @Description : | ||||
|  * @Reference : | ||||
|  * @Author : Castle | ||||
|  * @CreateDate : 2022/1/7 10:37 | ||||
|  * @Modify: | ||||
|  **/ | ||||
| 
 | ||||
| @Api(value = "冷热数据分离数据源管理", tags = "冷热数据分离数据源管理") | ||||
| @RestController | ||||
| @RequestMapping(PlatformConstWords.BASE_URL + "/datasource") | ||||
| public class DataSourceController { | ||||
| 
 | ||||
| 
 | ||||
|     @Autowired | ||||
|     private ICoreDataSourceService datasourceService; | ||||
| 
 | ||||
|     @Autowired | ||||
|     private CoreJdbcTemplateConfig jdbcTemplateConfig; | ||||
| 
 | ||||
|     /** | ||||
|      * 添加数据源 | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     @PostMapping("/add") | ||||
|     @ApiOperation(value = "添加数据源", notes = "添加数据源") | ||||
|     public ResultBean addDataSource(@RequestBody CoreDataSource coreDatasource) { | ||||
|         try { | ||||
|             ValidatorBean.beginValid(coreDatasource) | ||||
|                     .numberCheck("sourcePort", coreDatasource.getSourcePort()) | ||||
|                     .notNull("sourceCode", coreDatasource.getSourceCode()) | ||||
|                     .notNull("sourceName", coreDatasource.getSourceName()) | ||||
|                     .notNull("sourceHost", coreDatasource.getSourceHost()) | ||||
|                     .notNull("sourceUserName", coreDatasource.getSourceUserName()) | ||||
|                     .notNull("sourcePassword", coreDatasource.getSourcePassword()) | ||||
|                     .notNull("sourceDataBaseName", coreDatasource.getSourceDataBaseName()); | ||||
|             ConvertBean.modelInitialize(coreDatasource, AuthUtil.getSessionUser()); | ||||
|             datasourceService.checkBfDataSourceOnly(coreDatasource); | ||||
|             coreDatasource = datasourceService.saveDataSource(coreDatasource); | ||||
|             return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(coreDatasource); | ||||
|         } catch (ImppBusiException exception) { | ||||
|             return ResultBean.fail(exception).build(); | ||||
|         } catch (Exception e) { | ||||
|             return ImppExceptionBuilder.newInstance().buildExceptionResult(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 删除数据源 | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     @DeleteMapping("/delete/{id}") | ||||
|     @ApiOperation(value = "删除数据源", notes = "删除数据源") | ||||
|     public ResultBean deleteDataSource(@PathVariable("id") Long id) { | ||||
|         try { | ||||
|             ValidatorBean.checkNotNull(id, "数据对象id不能为空"); | ||||
|             datasourceService.delete(id); | ||||
|             return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()); | ||||
|         } catch (ImppBusiException exception) { | ||||
|             return ResultBean.fail(exception).build(); | ||||
|         } catch (Exception e) { | ||||
|             return ImppExceptionBuilder.newInstance().buildExceptionResult(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 更新数据源 | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     @PutMapping("/update") | ||||
|     @ApiOperation(value = "修改数据源", notes = "修改数据源") | ||||
|     public ResultBean updateDataSource(@RequestBody CoreDataSource coreDataSource) { | ||||
|         try { | ||||
| 
 | ||||
|             ValidatorBean.beginValid(coreDataSource) | ||||
|                     .notNull("id", coreDataSource.getId()) | ||||
|                     .notNull("sourceName", coreDataSource.getSourceName()); | ||||
| 
 | ||||
|             ConvertBean.modelUpdate(coreDataSource, AuthUtil.getSessionUser()); | ||||
|             datasourceService.updateDataObject(coreDataSource); | ||||
|             return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()); | ||||
|         } catch (ImppBusiException busExcep) { | ||||
|             return ResultBean.fail(busExcep); | ||||
|         } catch (Exception e) { | ||||
|             return ImppExceptionBuilder.newInstance().buildExceptionResult(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 查询数据源 | ||||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     @PostMapping("/query") | ||||
|     @ApiOperation(value = "查询数据源", notes = "查询数据源") | ||||
|     public ResultBean list(CoreDataSource cusDatasource, Pager pager) { | ||||
|         try { | ||||
|             ListPager<CoreDataSource> listPager = datasourceService.queryDataObjectByPager(cusDatasource, pager); | ||||
|             return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setListPager(listPager); | ||||
|         } catch (ImppBusiException busExcep) { | ||||
|             return ResultBean.fail(busExcep); | ||||
|         } catch (Exception e) { | ||||
|             return ImppExceptionBuilder.newInstance().buildExceptionResult(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @PutMapping("/connection/{id}") | ||||
|     @ApiOperation(value = "数据源链接", notes = "数据源连接测试") | ||||
|     public ResultBean connectionDataSource(@PathVariable("id") Long id) { | ||||
|         try { | ||||
|             ValidatorBean.checkNotNull(id, "数据对象id不能为空"); | ||||
|             CoreDataSource datasource = datasourceService.getCoreDatasourceObject(id); | ||||
|             ValidatorBean.checkNotNull(datasource, "不存在的数据源信息"); | ||||
|             boolean isConn = jdbcTemplateConfig.checkConnection(datasource); | ||||
|             if (isConn) { | ||||
|                 if (WmsEnumUtil.DATA_SOURCE_STATUS.CONN_SUCCESS.getValue() != datasource.getSourceStatus()) { | ||||
|                     datasource.setSourceStatus(WmsEnumUtil.DATA_SOURCE_STATUS.CONN_SUCCESS.getValue()); | ||||
|                     datasourceService.updateDataObject(datasource); | ||||
|                 } | ||||
|                 return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setMsg("连接成功"); | ||||
|             } | ||||
|             return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.FAIL.getCode()).setMsg("连接失败"); | ||||
|         } catch (ImppBusiException busiException) { | ||||
|             return ResultBean.fail(busiException); | ||||
|         } catch (Exception e) { | ||||
|             return ImppExceptionBuilder.newInstance().buildExceptionResult(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @GetMapping("/list") | ||||
|     @ApiOperation(value = "获取所有数据源的code和id", notes = "获取所有数据源的code和id") | ||||
|     public ResultBean listAll() { | ||||
|         try { | ||||
|             List<CoreDataSource> list = datasourceService.list(); | ||||
|             return ResultBean.success().setResultList(list); | ||||
|         } catch (Exception e) { | ||||
|             return ImppExceptionBuilder.newInstance().buildExceptionResult(e); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     @GetMapping("/data-source-type") | ||||
|     @ApiOperation(value = "数据源类型", notes = "数据源类型") | ||||
|     public ResultBean getDataSourceType() { | ||||
|         return new ResultBean(true, "操作成功", | ||||
|                 LocaleUtils.getEnumLocaleResValuesToList(CommonEnumUtil.DATA_SOURCE_TYPE.values())); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| package cn.estsh.i3plus.core.apiservice.dao; | ||||
| 
 | ||||
| import cn.estsh.i3plus.pojo.platform.bean.CoreDataSource; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * @Description : | ||||
|  * @Reference : | ||||
|  * @Author : Castle | ||||
|  * @CreateDate : 2022/1/7 13:36 | ||||
|  * @Modify: | ||||
|  **/ | ||||
| public interface ICoreDataSourceDao { | ||||
| 
 | ||||
|     /** | ||||
|      * 用于下拉框选择 | ||||
|      * @return | ||||
|      */ | ||||
|     List<CoreDataSource> listAllIdAndSourceName(); | ||||
| } | ||||
| @ -0,0 +1,36 @@ | ||||
| package cn.estsh.i3plus.core.apiservice.daoimpl; | ||||
| 
 | ||||
| import cn.estsh.i3plus.core.apiservice.dao.ICoreDataSourceDao; | ||||
| import cn.estsh.i3plus.core.apiservice.util.PlatformEnumUtil; | ||||
| import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil; | ||||
| import cn.estsh.i3plus.pojo.base.enumutil.WmsEnumUtil; | ||||
| import cn.estsh.i3plus.pojo.platform.bean.CoreDataSource; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.stereotype.Component; | ||||
| 
 | ||||
| import javax.persistence.EntityManager; | ||||
| import javax.persistence.Query; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * @Description : | ||||
|  * @Reference : | ||||
|  * @Author : Castle | ||||
|  * @CreateDate : 2022/1/7 13:38 | ||||
|  * @Modify: | ||||
|  **/ | ||||
| @Component | ||||
| public class CoreDataSourceDao implements ICoreDataSourceDao { | ||||
|     @Autowired | ||||
|     private EntityManager entityManager; | ||||
| 
 | ||||
|     @Override | ||||
|     public List<CoreDataSource> listAllIdAndSourceName() { | ||||
|         String hql = "select new CusDatasource(id,sourceName) from CoreDatasource where isDeleted = :is_deleted and isValid = :is_valid and sourceStatus = :sourceStatus"; | ||||
|         Query query = entityManager.createQuery(hql); | ||||
|         query.setParameter("is_deleted", CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue()); | ||||
|         query.setParameter("is_valid", CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue()); | ||||
|         query.setParameter("sourceStatus", PlatformEnumUtil.DATA_SOURCE_STATUS.CONN_SUCCESS.getValue()); | ||||
|         return query.getResultList(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,128 @@ | ||||
| package cn.estsh.i3plus.core.apiservice.serviceimpl.base; | ||||
| 
 | ||||
| import cn.estsh.i3plus.core.api.iservice.base.ICoreDataSourceService; | ||||
| import cn.estsh.i3plus.core.apiservice.configuration.CoreJdbcTemplateConfig; | ||||
| import cn.estsh.i3plus.core.apiservice.util.PlatformEnumUtil; | ||||
| import cn.estsh.i3plus.platform.common.convert.ConvertBean; | ||||
| import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum; | ||||
| import cn.estsh.i3plus.pojo.base.bean.DdlPackBean; | ||||
| import cn.estsh.i3plus.pojo.base.bean.ListPager; | ||||
| import cn.estsh.i3plus.pojo.base.common.Pager; | ||||
| import cn.estsh.i3plus.pojo.base.common.PagerHelper; | ||||
| import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil; | ||||
| import cn.estsh.i3plus.pojo.base.enumutil.WmsEnumUtil; | ||||
| import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack; | ||||
| import cn.estsh.i3plus.pojo.platform.bean.CoreDataSource; | ||||
| import cn.estsh.i3plus.pojo.platform.repository.CoreDataSourceRepository; | ||||
| import cn.estsh.i3plus.pojo.wms.bean.datasource.CusDatasource; | ||||
| import cn.estsh.impp.framework.boot.auth.AuthUtil; | ||||
| import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.stereotype.Service; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * @Description : | ||||
|  * @Reference : | ||||
|  * @Author : Castle | ||||
|  * @CreateDate : 2022/1/7 11:12 | ||||
|  * @Modify: | ||||
|  **/ | ||||
| @Service | ||||
| public class CoreDataSourceServiceImpl implements ICoreDataSourceService{ | ||||
| 
 | ||||
| 
 | ||||
|     @Autowired | ||||
|     private CoreDataSourceRepository coreDataSourceRepository; | ||||
|     @Autowired | ||||
|     private CoreJdbcTemplateConfig jdbcConfig; | ||||
| 
 | ||||
|     @Override | ||||
|     public CoreDataSource updateDataObject(CoreDataSource dataObject) { | ||||
|         coreDataSourceRepository.update(dataObject); | ||||
|         return dataObject; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public CoreDataSource saveDataSource(CoreDataSource source) { | ||||
| 
 | ||||
|         ConvertBean.saveOrUpdate(source, AuthUtil.getSessionUser().getUserName()); | ||||
|         coreDataSourceRepository.insert(source); | ||||
|         if (jdbcConfig.checkConnection(source)) { | ||||
|             source.setSourceStatus(PlatformEnumUtil.DATA_SOURCE_STATUS.CONN_SUCCESS.getValue()); | ||||
|         } else { | ||||
|             source.setSourceStatus(PlatformEnumUtil.DATA_SOURCE_STATUS.CONN_FAILURE.getValue()); | ||||
|         } | ||||
|         coreDataSourceRepository.update(source); | ||||
|         return source; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public CoreDataSource getCoreDatasourceObject(Long id) { | ||||
|         return coreDataSourceRepository.getById(id); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     @Override | ||||
|     public void checkBfDataSourceOnly(CoreDataSource source) { | ||||
|         DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(); | ||||
|         DdlPreparedPack.getStringEqualPack(source.getSourceCode(), "sourceCode", ddlPackBean); | ||||
|         int count = coreDataSourceRepository.findByHqlWhereCount(ddlPackBean); | ||||
|         if (count > 0) { | ||||
|             throw ImppExceptionBuilder.newInstance() | ||||
|                     .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode()) | ||||
|                     .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode()) | ||||
|                     .setErrorDetail("【唯一校验】数据源编码重复") | ||||
|                     .setErrorSolution("请重新输入数据源编码") | ||||
|                     .build(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public ListPager<CoreDataSource> queryDataObjectByPager(CoreDataSource coreDataSource, Pager pager) { | ||||
|         if (coreDataSource == null) { | ||||
|             pager = PagerHelper.getPager(pager, coreDataSourceRepository.listCount()); | ||||
|             return new ListPager(coreDataSourceRepository.listPager(pager), pager); | ||||
|         } else { | ||||
|             DdlPackBean hqlPack = DdlPackBean.getDdlPackBean(); | ||||
|             if (coreDataSource.getSourceCode() != null) { | ||||
|                 DdlPreparedPack.getStringLeftLikerPack(coreDataSource.getSourceCode(), "sourceCode", hqlPack); | ||||
|             } | ||||
|             if (coreDataSource.getSourceType() != null) { | ||||
|                 DdlPreparedPack.getNumEqualPack(coreDataSource.getSourceType(), "sourceType", hqlPack); | ||||
|             } | ||||
|             if (coreDataSource.getSourceStatus() != null) { | ||||
|                 DdlPreparedPack.getNumEqualPack(coreDataSource.getSourceStatus(), "sourceStatus", hqlPack); | ||||
|             } | ||||
|             if (coreDataSource.getSourceDataBaseName() != null) { | ||||
|                 DdlPreparedPack.getStringLeftLikerPack(coreDataSource.getSourceDataBaseName(), "sourceDataBaseName", hqlPack); | ||||
|             } | ||||
|             if (coreDataSource.getSourceHost() != null) { | ||||
|                 DdlPreparedPack.getStringLeftLikerPack(coreDataSource.getSourceHost(), "sourceHost", hqlPack); | ||||
|             } | ||||
|             if (coreDataSource.getSourceName() != null) { | ||||
|                 DdlPreparedPack.getStringLeftLikerPack(coreDataSource.getSourceName(), "sourceName", hqlPack); | ||||
|             } | ||||
|             pager = PagerHelper.getPager(pager, coreDataSourceRepository.findByHqlWhereCount(hqlPack)); | ||||
|             return new ListPager(coreDataSourceRepository.findByHqlWherePage(hqlPack, pager), pager); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public List<CoreDataSource> listAll() { | ||||
|         return coreDataSourceRepository.list(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void delete(Long id) { | ||||
|         CoreDataSource coreDatasource = coreDataSourceRepository.getById(id); | ||||
|         coreDataSourceRepository.deleteWeaklyById(id, AuthUtil.getSessionUser().getUserName()); | ||||
|         jdbcConfig.removeJdbcTemplateBean(coreDatasource); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public List<CoreDataSource> list() { | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
| @ -1,103 +0,0 @@ | ||||
| package cn.estsh.i3plus.core.apiservice.serviceimpl.base; | ||||
| 
 | ||||
| import cn.estsh.i3plus.core.apiservice.util.strategy.IDataSeparatorStrategy; | ||||
| import cn.estsh.i3plus.platform.plugin.datasource.DynamicDataSourceProxy; | ||||
| import cn.estsh.i3plus.pojo.platform.bean.DataSeparatorMessage; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.stereotype.Component; | ||||
| 
 | ||||
| import javax.persistence.Column; | ||||
| import java.lang.reflect.Field; | ||||
| import java.sql.SQLException; | ||||
| import java.util.ArrayList; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| /** | ||||
|  * @Description : | ||||
|  * @Reference : | ||||
|  * @Author : Castle | ||||
|  * @CreateDate : 2021/12/3 14:02 | ||||
|  * @Modify: | ||||
|  **/ | ||||
| @Component | ||||
| @Slf4j | ||||
| public class DataSeparatorStrategyMysql implements IDataSeparatorStrategy { | ||||
| 
 | ||||
|     /** | ||||
|      * 根据目标数据数据的url+数据库名作为key 缓存动态数据源 | ||||
|      */ | ||||
|     public static final Map<String, DynamicDataSourceProxy> DYNAMIC_DATA_SOURCE_PROXY_MAP = new HashMap<>(); | ||||
| 
 | ||||
| 
 | ||||
|     @Override | ||||
|     public void execute(Object bean, DataSeparatorMessage msg) { | ||||
|         //获取动态数据源
 | ||||
|         DynamicDataSourceProxy dynamicDataSource = getDynamicDataSource(msg); | ||||
| 
 | ||||
|         //封装Map<columnName,value>
 | ||||
|         HashMap<String, Object> objMap = getObjMap(bean); | ||||
| 
 | ||||
|         //执行sql
 | ||||
|         String insertSQL = dynamicDataSource.packInsertSQL(msg.getDestTableName(), objMap); | ||||
|         try { | ||||
|             dynamicDataSource.execute(insertSQL); | ||||
|         } catch (SQLException e) { | ||||
|             log.error("执行sql:{}失败,失败原因:{}", insertSQL, e.getMessage()); | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取动态数据源 | ||||
|      * | ||||
|      * @param msg | ||||
|      * @return | ||||
|      */ | ||||
|     private DynamicDataSourceProxy getDynamicDataSource(DataSeparatorMessage msg) { | ||||
|         DynamicDataSourceProxy dataSourceProxy; | ||||
|         String destUrl = msg.getDestUrl(); | ||||
|         String dataBaseName = msg.getDatabaseName(); | ||||
|         String mapKey = destUrl + ":" + dataBaseName; | ||||
|         if (!DYNAMIC_DATA_SOURCE_PROXY_MAP.containsKey(mapKey)) { | ||||
|             String url = "jdbc:mysql://" + destUrl + "/" + dataBaseName + "?autoReconnect=true&useSSL=false&characterEncoding=utf-8"; | ||||
|             dataSourceProxy = DynamicDataSourceProxy.initDataSourceFactory("com.mysql.jdbc.Driver",url,msg.getUserName(),msg.getPassword()); | ||||
|             DYNAMIC_DATA_SOURCE_PROXY_MAP.put(mapKey, dataSourceProxy); | ||||
|         } | ||||
|         dataSourceProxy = DYNAMIC_DATA_SOURCE_PROXY_MAP.get(mapKey); | ||||
|         return dataSourceProxy; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取封装的k-v值 | ||||
|      * | ||||
|      * @param bean | ||||
|      * @return | ||||
|      */ | ||||
|     private HashMap<String, Object> getObjMap(Object bean) { | ||||
|         Field[] declaredFields = bean.getClass().getDeclaredFields(); | ||||
|         Field[] parentFields = bean.getClass().getSuperclass().getDeclaredFields(); | ||||
|         HashMap<String, Object> objMap = new HashMap<>(declaredFields.length); | ||||
|         List<Field[]> list = new ArrayList<>(); | ||||
|         list.add(declaredFields); | ||||
|         list.add(parentFields); | ||||
|         list.forEach(item -> { | ||||
|             for (Field field : item) { | ||||
|                 field.setAccessible(true); | ||||
|                 if (null != field.getAnnotation(Column.class)) { | ||||
|                     Column annotation = field.getAnnotation(Column.class); | ||||
|                     String columnName = annotation.name(); | ||||
|                     try { | ||||
|                         Object value = field.get(bean); | ||||
|                         objMap.put(columnName, value); | ||||
|                     } catch (IllegalAccessException e) { | ||||
|                         log.error("冷热分离Mysql封装数据错误:{}!", columnName); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         }); | ||||
|         return objMap; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,91 @@ | ||||
| package cn.estsh.i3plus.core.apiservice.util; | ||||
| 
 | ||||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||||
| 
 | ||||
| /** | ||||
|  * @Description : | ||||
|  * @Reference : | ||||
|  * @Author : Castle | ||||
|  * @CreateDate : 2022/1/7 13:14 | ||||
|  * @Modify: | ||||
|  **/ | ||||
| public class PlatformEnumUtil { | ||||
|     @JsonFormat(shape = JsonFormat.Shape.OBJECT) | ||||
|     public enum DATA_SOURCE_STATUS { | ||||
|         CONN_SUCCESS(10, "连接成功", "连接成功"), | ||||
|         CONN_FAILURE(20, "连接失败", "连接失败"); | ||||
| 
 | ||||
|         private int value; | ||||
|         private String code; | ||||
|         private String description; | ||||
| 
 | ||||
|         private DATA_SOURCE_STATUS(int value, String code, String description) { | ||||
|             this.value = value; | ||||
|             this.code = code; | ||||
|             this.description = description; | ||||
|         } | ||||
| 
 | ||||
|         public int getValue() { | ||||
|             return value; | ||||
|         } | ||||
| 
 | ||||
|         public String getCode() { | ||||
|             return code; | ||||
|         } | ||||
| 
 | ||||
|         public String getDescription() { | ||||
|             return description; | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         public static String valueOfCode(int val) { | ||||
|             String tmp = null; | ||||
|             for (int i = 0; i < values().length; i++) { | ||||
|                 if (values()[i].value == val) { | ||||
|                     tmp = values()[i].code; | ||||
|                 } | ||||
|             } | ||||
|             return tmp; | ||||
|         } | ||||
| 
 | ||||
|         public static int codeOfValue(String code) { | ||||
|             int tmp = 1; | ||||
|             for (int i = 0; i < values().length; i++) { | ||||
|                 if (values()[i].code.equals(code)) { | ||||
|                     tmp = values()[i].value; | ||||
|                 } | ||||
|             } | ||||
|             return tmp; | ||||
|         } | ||||
| 
 | ||||
|         public static String valueOfDescription(int val) { | ||||
|             String tmp = null; | ||||
|             for (int i = 0; i < values().length; i++) { | ||||
|                 if (values()[i].value == val) { | ||||
|                     tmp = values()[i].description; | ||||
|                 } | ||||
|             } | ||||
|             return tmp; | ||||
|         } | ||||
| 
 | ||||
|         public static DATA_SOURCE_STATUS valueOf(int val) { | ||||
|             String tmp = null; | ||||
|             for (int i = 0; i < values().length; i++) { | ||||
|                 if (values()[i].value == val) { | ||||
|                     return values()[i]; | ||||
|                 } | ||||
|             } | ||||
|             return null; | ||||
|         } | ||||
| 
 | ||||
|         public static String codeOfDescription(String code) { | ||||
|             String tmp = null; | ||||
|             for (int i = 0; i < values().length; i++) { | ||||
|                 if (values()[i].code.equals(code)) { | ||||
|                     tmp = values()[i].description; | ||||
|                 } | ||||
|             } | ||||
|             return tmp; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,70 @@ | ||||
| package cn.estsh.i3plus.core.apiservice.util; | ||||
| 
 | ||||
| import cn.estsh.i3plus.platform.plugin.sqltool.IBaseSqlService; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.stereotype.Component; | ||||
| 
 | ||||
| import javax.persistence.Column; | ||||
| import java.lang.reflect.Field; | ||||
| import java.util.ArrayList; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * @Description : | ||||
|  * @Reference : | ||||
|  * @Author : Castle | ||||
|  * @CreateDate : 2022/1/7 14:19 | ||||
|  * @Modify: | ||||
|  **/ | ||||
| @Component | ||||
| @Slf4j | ||||
| public class SeparatorDataUtil { | ||||
|     @Autowired | ||||
|     private IBaseSqlService baseSqlService; | ||||
| 
 | ||||
|     /** | ||||
|      * 包装 插入sql | ||||
|      * | ||||
|      * @param bean | ||||
|      * @param destTableName | ||||
|      * @return | ||||
|      */ | ||||
|     public String packSql(Object bean, String destTableName) { | ||||
|         HashMap<String, Object> objMap = getObjMap(bean); | ||||
|         String insertSQL = baseSqlService.packInsertSQL(destTableName, objMap); | ||||
|         return insertSQL; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取封装的k-v值 | ||||
|      * | ||||
|      * @param bean | ||||
|      * @return | ||||
|      */ | ||||
|     private HashMap<String, Object> getObjMap(Object bean) { | ||||
|         Field[] declaredFields = bean.getClass().getDeclaredFields(); | ||||
|         Field[] parentFields = bean.getClass().getSuperclass().getDeclaredFields(); | ||||
|         HashMap<String, Object> objMap = new HashMap<>(declaredFields.length); | ||||
|         List<Field[]> list = new ArrayList<>(); | ||||
|         list.add(declaredFields); | ||||
|         list.add(parentFields); | ||||
|         list.forEach(item -> { | ||||
|             for (Field field : item) { | ||||
|                 field.setAccessible(true); | ||||
|                 if (null != field.getAnnotation(Column.class)) { | ||||
|                     Column annotation = field.getAnnotation(Column.class); | ||||
|                     String columnName = annotation.name(); | ||||
|                     try { | ||||
|                         Object value = field.get(bean); | ||||
|                         objMap.put(columnName, value); | ||||
|                     } catch (IllegalAccessException e) { | ||||
|                         log.error("冷热分离Mysql封装数据错误:{}!", columnName); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         }); | ||||
|         return objMap; | ||||
|     } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in New Issue