package com.kyexpress.erp.tms.bill.provider.utils; import com.kyexpress.erp.tms.bill.provider.constant.ResponseCode; import com.kyexpress.framework.exception.ApplicationException; import com.kyexpress.framework.model.GenericBO; import com.kyexpress.framework.model.Pagination; import com.kyexpress.framework.model.ResponseData; import com.kyexpress.framework.service.GenericService; import org.apache.poi.ss.formula.functions.T; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Stream; public class ServiceUtils { /** * 调用方法 */ public static <T> ResponseData call(T param, Consumer<T> callProcess) { return call(param, callProcess, p -> {}); } /** * 调用方法 */ public static <T> ResponseData call(T param, Consumer<T> callProcess, Consumer<T> valid) { valid.accept(param); callProcess.accept(param); return CommonUtils.buildResponseData(); } /** * 调用方法 */ public static <T, R> ResponseData call(T param, Consumer<R> callProcess, Function<T, R> valid) { callProcess.accept(valid.apply(param)); return CommonUtils.buildResponseData(); } /** * 调用方法 */ public static <T, R> ResponseData<R> callWithReturn(T param, Function<T, R> callProcess) { return callWithReturn(param, callProcess, p -> {}); } /** * 调用方法 */ public static <T, R> ResponseData<R> callWithReturn(T param, Function<T, R> callProcess, Consumer<T> valid) { valid.accept(param); return CommonUtils.buildResponseData(callProcess.apply(param)); } /** * 调用方法 */ public static <T, R, RS> ResponseData<RS> callWithReturn(T param, Function<R, RS> callProcess, Function<T, R> valid) { return CommonUtils.buildResponseData(callProcess.apply(valid.apply(param))); } /** * 查询 */ public static <T> ResponseData<Pagination<T>> search(GenericBO<T> bo, GenericService<T, Long> service) { return search(bo, service, BO -> {}); } /** * 查询 */ public static <T> ResponseData<Pagination<T>> search(GenericBO<T> bo, GenericService<T, Long> service, Consumer<GenericBO<T>> queryBuilder) { return search(bo, service::search, queryBuilder); } /** * 查询 */ private static <T> ResponseData<Pagination<T>> search(GenericBO<T> bo, Consumer<Pagination<T>> searchProcess, Consumer<GenericBO<T>> queryBuilder) { queryBuilder.accept(bo); ResponseData<Pagination<T>> resp = new ResponseData<>(); Pagination<T> pagination = Pagination.getInstance4BO(bo); searchProcess.accept(pagination); resp.setData(pagination).ok(); return resp; } /** * 批量操作 */ public static <T, R> ResponseData<R> batch(GenericBO<T> bo, Function<Long[], R> deleteProcess, ResponseCode listIsNullError) { return CommonUtils.buildResponseData(batchByIdsWithReturn(bo, deleteProcess, listIsNullError)); } /** * 删除 */ public static ResponseData delete(GenericBO<T> bo, GenericService<T, Long> service) { return delete(bo, service, Function.identity()); } /** * 删除 */ public static ResponseData delete(GenericBO<T> bo, GenericService<T, Long> service, Function<Long[], Long[]> valid) { deleteByIds(bo, service::disable, valid); return CommonUtils.buildResponseData(); } /** * 删除 */ private static <T> void deleteByIds(GenericBO<T> bo, Consumer<Long[]> deleteProcess, Function<Long[], Long[]> valid) { if (bo.getIds() == null || bo.getIds().length == 0) { throw new ApplicationException(ResponseCode.DELETE_LIST_IS_NULL); } Long[] ids = Stream.of(bo.getIds()).map(Long::parseLong).toArray(Long[]::new); ids = valid.apply(ids); deleteProcess.accept(ids); } /** * 批量操作 */ private static <T, R> R batchByIdsWithReturn(GenericBO<T> bo, Function<Long[], R> deleteProcess, ResponseCode listIsNullError) { return batchByIdsWithReturn(bo, deleteProcess, listIsNullError, Function.identity()); } /** * 批量操作 */ private static <T, R> R batchByIdsWithReturn(GenericBO<T> bo, Function<Long[], R> deleteProcess, ResponseCode listIsNullError, Function<Long[], Long[]> valid) { if (bo.getIds() == null || bo.getIds().length == 0) { throw new ApplicationException(listIsNullError); } Long[] ids = Stream.of(bo.getIds()).map(Long::parseLong).toArray(Long[]::new); ids = valid.apply(ids); return deleteProcess.apply(valid.apply(ids)); } }
2021-10-27-ServiceUtils-kye
最新推荐文章于 2024-09-10 10:40:14 发布