com.ly.mybatis.mapperservice.service.BaseService Maven / Gradle / Ivy
The newest version!
package com.ly.mybatis.mapperservice.service;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.mybatis.mapperservice.model.ImportBatchResult;
import com.ly.mybatis.mapperservice.model.Result;
import com.ly.mybatis.mapperservice.util.MPSExcelUtil;
import com.ly.mybatis.mapperservice.util.MPSUtil;
import org.springframework.core.GenericTypeResolver;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@SuppressWarnings({"unused", "unchecked"})
public interface BaseService {
default Class getEntityClass() {
return (Class) Objects.requireNonNull(
GenericTypeResolver.resolveTypeArguments(getClass(), BaseService.class)
)[0];
}
default RuntimeException exception(String msg) {
throw new RuntimeException(msg);
}
@Transactional
default Entity save(Form form) {
Result result = MPSUtil.insert(form);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
@Transactional
default boolean delete(Collection ids) {
return MPSUtil.delete(getEntityClass(), ids);
}
@Transactional
default Entity update(String id, Form form) {
Result result = MPSUtil.update(id, form);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
default IPage page(Page page, ConditionForm form) {
Result> result = MPSUtil.page(page, form);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
default IPage page(Class voClass, Page page, ConditionForm form) {
Result> result = MPSUtil.pageVo(voClass, page, form);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
default List list(ConditionForm form) {
Result> result = MPSUtil.list(form);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
default List list(Class voClass, ConditionForm form) {
Result> result = MPSUtil.listVo(voClass, form);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
default Bo detail(String id) {
Result result = MPSUtil.detail(getEntityClass(), id);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
default Vo detail(Class voClass, String id) {
Result result = MPSUtil.detailVo(getEntityClass(), voClass, id);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
default Bo detail(ConditionForm form) {
Result result = MPSUtil.detail(form);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
default Vo detail(Class voClass, ConditionForm form) {
Result result = MPSUtil.detailVo(voClass, form);
if (!result.isSuccess()) {
throw exception(result.getMessage());
}
return result.get();
}
default void export(
HttpServletResponse response,
String name,
Supplier> selectorSupplier,
Class excelClass,
Function convertor,
ConditionForm form
) {
MPSExcelUtil.exportExcel(
name,
name,
excelClass,
Optional.ofNullable(selectorSupplier).map(Supplier::get).orElse(null),
Optional.ofNullable(list(form))
.map(l -> l.parallelStream()
.map(convertor)
.filter(Objects::nonNull)
.collect(Collectors.toList()))
.orElse(null),
response
);
}
default void export(
Class voClass,
HttpServletResponse response,
String name,
Supplier> selectorSupplier,
Class excelClass,
Function convertor,
ConditionForm form
) {
MPSExcelUtil.exportExcel(
name,
name,
excelClass,
Optional.ofNullable(selectorSupplier).map(Supplier::get).orElse(null),
Optional.ofNullable(list(voClass, form))
.map(l -> l.parallelStream()
.map(convertor)
.filter(Objects::nonNull)
.collect(Collectors.toList()))
.orElse(null),
response
);
}
default void exportTemplate(
HttpServletResponse response,
String name,
Supplier> selectorSupplier,
Class excelClass
) {
MPSExcelUtil.exportExcel(
name,
name,
excelClass,
Optional.ofNullable(selectorSupplier).map(Supplier::get).orElse(null),
null,
response
);
}
@Transactional
default void importExcel(InputStream inputStream, Class excelClass, Function convertor) {
ImportParams importParams = new ImportParams();
MPSExcelUtil.importExcel(importParams, inputStream, excelClass, es -> {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy