直接贴具体实现代码

    1. /**
    2. * 抽象数据源工厂
    3. */
    4. public abstract class DataAbstractFactory {
    5. /**
    6. * 查询所有
    7. *
    8. * @param dataSource 数据来源
    9. * @return
    10. */
    11. public abstract List<?> findAll(Integer dataSource);
    12. /**
    13. * 分页查询
    14. *
    15. * @param dataSource 数据来源
    16. * @param query 查询条件
    17. * @param page 分页条件
    18. * @param timeField 时间字段
    19. * @return
    20. */
    21. public abstract PageResultDTO findDataByPage(Integer dataSource, QueryConditionDTO query,
    22. PageConditionDTO page, String timeField);
    23. /**
    24. * 插入数据
    25. *
    26. * @param jsonArray
    27. * @return
    28. */
    29. public abstract Long insertData(Integer dataSource,JSONArray jsonArray);
    30. }
    1. @Service("BookFactory")
    2. public class BookFactory extends DataAbstractFactory {
    3. @Autowired
    4. private BookService mysqlBookService;
    5. @Autowired
    6. private ESBookService esBookService;
    7. @Override
    8. public List<BookVO> findAll(Integer dataSource) {
    9. if (DataSourceEnum.NORMAL.getDataSource().equals(dataSource)) {
    10. List<BookVO> list = mysqlBookService.findAll();
    11. return list;
    12. } else if (DataSourceEnum.TEXT.getDataSource().equals(dataSource)) {
    13. // ES
    14. List<BookVO> list = esBookService.findAll();
    15. return list;
    16. }
    17. throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
    18. BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
    19. }
    20. @Override
    21. public PageResultDTO findDataByPage(Integer dataSource, QueryConditionDTO query, PageConditionDTO page, String timeField) {
    22. if (DataSourceEnum.NORMAL.getDataSource().equals(dataSource)) {
    23. if (null != query.getStartTime() || null != query.getEndTime()) {
    24. return mysqlBookService.queryPage(query, page, timeField);
    25. }
    26. return mysqlBookService.queryPage(query, page);
    27. } else if (DataSourceEnum.TEXT.getDataSource().equals(dataSource)) {
    28. // ES
    29. if (null != query.getStartTime() || null != query.getEndTime()) {
    30. return esBookService.queryPage(query, page, timeField);
    31. }
    32. return esBookService.queryPage(query, page);
    33. }
    34. throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
    35. BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
    36. }
    37. @Override
    38. public Long insertData(Integer dataSource, JSONArray jsonArray) {
    39. if (DataSourceEnum.NORMAL.getDataSource().equals(dataSource)) {
    40. List<Book> books = JSONArray.parseArray(jsonArray.toJSONString(), Book.class);
    41. // 补充额外参数对象
    42. books.forEach(item -> {
    43. item.setValid(0);
    44. item.setCreateTime(new Date());
    45. });
    46. mysqlBookService.saveBatch(books);
    47. return (long) jsonArray.size();
    48. } else if (DataSourceEnum.TEXT.getDataSource().equals(dataSource)) {
    49. List<ESBook> books = JSONArray.parseArray(jsonArray.toJSONString(), ESBook.class);
    50. // TODO 后续转AOP插入相关字段?
    51. books.forEach(item -> {
    52. item.setValid(0);
    53. item.setCreateTime(new Date());
    54. });
    55. esBookService.saveAll(books);
    56. return (long) jsonArray.size();
    57. }
    58. throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
    59. BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
    60. }
    61. }

    实现类

    1. @Service
    2. public class DataServiceImpl implements DataService {
    3. @Autowired
    4. private DataSourceService dataSourceService;
    5. @Override
    6. public SearchResultDTO search(SearchDTO record) {
    7. SearchResultDTO result = new SearchResultDTO();
    8. result.setDataType(record.getDataType());
    9. result.setPage(record.getPage());
    10. Integer dataType = DataTypeEnum.getDataType(record.getDataType());
    11. if (null == dataType) {
    12. throw new BusinessException("查询不到该类型数据源", BusinessExceptionEnum.BUSINESS_NO_EXISTENT_ERROR.getCode());
    13. }
    14. DataSource dataSourceModel = dataSourceService.queryByDataType(dataType);
    15. if (null != dataSourceModel) {
    16. // 获取对应工程
    17. DataAbstractFactory factory = this.getFactory(dataSourceModel.getPath());
    18. PageResultDTO pageResult = factory.findDataByPage(dataSourceModel.getDataSource(), record.getQuery(),
    19. record.getPage(), dataSourceModel.getTimeField());
    20. result.setData(pageResult.getList());
    21. result.setTotal(pageResult.getTotalCount());
    22. return result;
    23. }
    24. throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
    25. BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
    26. }
    27. @Override
    28. public InsertResultDTO insert(InsertDTO record) {
    29. InsertResultDTO result = new InsertResultDTO();
    30. result.setDataType(record.getDataType());
    31. result.setTotal((long) record.getData().size());
    32. Integer dataType = DataTypeEnum.getDataType(record.getDataType());
    33. if (null == dataType) {
    34. throw new BusinessException("查询不到该类型数据源", BusinessExceptionEnum.BUSINESS_NO_EXISTENT_ERROR.getCode());
    35. }
    36. DataSource dataSourceModel = dataSourceService.queryByDataType(dataType);
    37. if (null != dataSourceModel) {
    38. // 获取对应工程
    39. DataAbstractFactory factory = this.getFactory(dataSourceModel.getPath());
    40. factory.insertData(dataSourceModel.getDataSource(), record.getData());
    41. return result;
    42. }
    43. throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
    44. BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
    45. }
    46. @Override
    47. public void delete() {
    48. }
    49. @Override
    50. public void update() {
    51. }
    52. /**
    53. * 获取对应类型的抽象工程(主要代码)
    54. */
    55. private DataAbstractFactory getFactory(String path) {
    56. DataAbstractFactory factory = null;
    57. try {
    58. // com.zsd.modules.data.factory.impl.BookFactory
    59. Class<?> aClass = Class.forName(path);
    60. Class<DataAbstractFactory> c = (Class<DataAbstractFactory>) aClass;
    61. return SpringUtil.getBean(c);
    62. } catch (Exception e) {
    63. throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
    64. BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
    65. }
    66. }
    67. }