直接贴具体实现代码
/**
* 抽象数据源工厂
*/
public abstract class DataAbstractFactory {
/**
* 查询所有
*
* @param dataSource 数据来源
* @return
*/
public abstract List<?> findAll(Integer dataSource);
/**
* 分页查询
*
* @param dataSource 数据来源
* @param query 查询条件
* @param page 分页条件
* @param timeField 时间字段
* @return
*/
public abstract PageResultDTO findDataByPage(Integer dataSource, QueryConditionDTO query,
PageConditionDTO page, String timeField);
/**
* 插入数据
*
* @param jsonArray
* @return
*/
public abstract Long insertData(Integer dataSource,JSONArray jsonArray);
}
@Service("BookFactory")
public class BookFactory extends DataAbstractFactory {
@Autowired
private BookService mysqlBookService;
@Autowired
private ESBookService esBookService;
@Override
public List<BookVO> findAll(Integer dataSource) {
if (DataSourceEnum.NORMAL.getDataSource().equals(dataSource)) {
List<BookVO> list = mysqlBookService.findAll();
return list;
} else if (DataSourceEnum.TEXT.getDataSource().equals(dataSource)) {
// ES
List<BookVO> list = esBookService.findAll();
return list;
}
throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
}
@Override
public PageResultDTO findDataByPage(Integer dataSource, QueryConditionDTO query, PageConditionDTO page, String timeField) {
if (DataSourceEnum.NORMAL.getDataSource().equals(dataSource)) {
if (null != query.getStartTime() || null != query.getEndTime()) {
return mysqlBookService.queryPage(query, page, timeField);
}
return mysqlBookService.queryPage(query, page);
} else if (DataSourceEnum.TEXT.getDataSource().equals(dataSource)) {
// ES
if (null != query.getStartTime() || null != query.getEndTime()) {
return esBookService.queryPage(query, page, timeField);
}
return esBookService.queryPage(query, page);
}
throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
}
@Override
public Long insertData(Integer dataSource, JSONArray jsonArray) {
if (DataSourceEnum.NORMAL.getDataSource().equals(dataSource)) {
List<Book> books = JSONArray.parseArray(jsonArray.toJSONString(), Book.class);
// 补充额外参数对象
books.forEach(item -> {
item.setValid(0);
item.setCreateTime(new Date());
});
mysqlBookService.saveBatch(books);
return (long) jsonArray.size();
} else if (DataSourceEnum.TEXT.getDataSource().equals(dataSource)) {
List<ESBook> books = JSONArray.parseArray(jsonArray.toJSONString(), ESBook.class);
// TODO 后续转AOP插入相关字段?
books.forEach(item -> {
item.setValid(0);
item.setCreateTime(new Date());
});
esBookService.saveAll(books);
return (long) jsonArray.size();
}
throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
}
}
实现类
@Service
public class DataServiceImpl implements DataService {
@Autowired
private DataSourceService dataSourceService;
@Override
public SearchResultDTO search(SearchDTO record) {
SearchResultDTO result = new SearchResultDTO();
result.setDataType(record.getDataType());
result.setPage(record.getPage());
Integer dataType = DataTypeEnum.getDataType(record.getDataType());
if (null == dataType) {
throw new BusinessException("查询不到该类型数据源", BusinessExceptionEnum.BUSINESS_NO_EXISTENT_ERROR.getCode());
}
DataSource dataSourceModel = dataSourceService.queryByDataType(dataType);
if (null != dataSourceModel) {
// 获取对应工程
DataAbstractFactory factory = this.getFactory(dataSourceModel.getPath());
PageResultDTO pageResult = factory.findDataByPage(dataSourceModel.getDataSource(), record.getQuery(),
record.getPage(), dataSourceModel.getTimeField());
result.setData(pageResult.getList());
result.setTotal(pageResult.getTotalCount());
return result;
}
throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
}
@Override
public InsertResultDTO insert(InsertDTO record) {
InsertResultDTO result = new InsertResultDTO();
result.setDataType(record.getDataType());
result.setTotal((long) record.getData().size());
Integer dataType = DataTypeEnum.getDataType(record.getDataType());
if (null == dataType) {
throw new BusinessException("查询不到该类型数据源", BusinessExceptionEnum.BUSINESS_NO_EXISTENT_ERROR.getCode());
}
DataSource dataSourceModel = dataSourceService.queryByDataType(dataType);
if (null != dataSourceModel) {
// 获取对应工程
DataAbstractFactory factory = this.getFactory(dataSourceModel.getPath());
factory.insertData(dataSourceModel.getDataSource(), record.getData());
return result;
}
throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
}
@Override
public void delete() {
}
@Override
public void update() {
}
/**
* 获取对应类型的抽象工程(主要代码)
*/
private DataAbstractFactory getFactory(String path) {
DataAbstractFactory factory = null;
try {
// com.zsd.modules.data.factory.impl.BookFactory
Class<?> aClass = Class.forName(path);
Class<DataAbstractFactory> c = (Class<DataAbstractFactory>) aClass;
return SpringUtil.getBean(c);
} catch (Exception e) {
throw new BusinessException(BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getMsg(),
BusinessExceptionEnum.SERVER_NO_FUNCTION_ERROR.getCode());
}
}
}