Commit 49d31771 by 范富荣

添加工具类

1 parent 7edbaca9
package cn.com.glsx.dj.supervisor.util;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.rop.response.RopResponse;
import com.rop.RopRequestContext;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class CommonUtil {
private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
//无权限时商户号ID
public static final int NO_PERMISSION_MERCHANT_ID = -1;
public static final List<Integer> NO_PERMISSION_LIST = new ArrayList<>(Arrays.asList(NO_PERMISSION_MERCHANT_ID));//没有权限时的列表数据
//求交集,如果其中一个为空集合 则直接返回另一个
public static List<Integer> getRetainMerchantIdList(final List<Integer> merchantId1List,final List<Integer> merchantId2List) {
if (CollectionUtils.isEmpty(merchantId1List) ) {
return merchantId2List;
}
if (CollectionUtils.isEmpty(merchantId2List) ) {
return merchantId1List;
}
List<Integer> list1 = new ArrayList<>(merchantId1List);
List<Integer> list2 = new ArrayList<>(merchantId2List);
list1.retainAll(list2);
if (CollectionUtils.isEmpty(list1)) {
list1.add(NO_PERMISSION_MERCHANT_ID);
}
return list1;
}
public static <T> RpcPagination<T> convertRpcPagination(RpcPagination<?> origPagination) {
RpcPagination<T> resp = new RpcPagination<>();
resp.setPageSize(origPagination.getPageSize());
resp.setPageNum(origPagination.getPageNum());
resp.setTotal(origPagination.getTotal());
resp.setPages(origPagination.getPages());
resp.setStartRow(origPagination.getStartRow());
resp.setEndRow(origPagination.getEndRow());
resp.setList(new ArrayList<>());
return resp;
}
public static <E,T> RopResponse<RpcPagination<T>> convertPaginationRopResponse(RpcResponse<RpcPagination<E>> rpcRes,ConvertHandler<E,T> handler) {
if(rpcRes.isSuccess()){
RpcPagination<E> result = rpcRes.getResult();
if(result == null) {
return RopResponse.success(null);
}
RpcPagination<T> resp = convertRpcPagination(result);
if (CollectionUtils.isNotEmpty(result.getList())) {
List<T> list = result.getList().stream().map(handler::convert).collect(Collectors.toList());
resp.setList(list);
}
return RopResponse.success(resp);
}
return RopResponse.error(rpcRes);
}
public static List<Integer> convert2Integer(String[] strArr) {
if(strArr == null || strArr.length == 0) {
return null;
}
List<Integer> list = new ArrayList<>();
for(String s:strArr) {
try {
list.add(Integer.valueOf(s));
}catch(Exception e) {
}
}
return list.isEmpty() ? null:list;
}
public static List<Byte> convertByte(String[] strArr) {
if (strArr == null || strArr.length == 0) {
return null;
}
List<Byte> list = new ArrayList<>();
for (String s : strArr) {
try {
list.add(Byte.valueOf(s));
} catch (Exception e) {
}
}
return list.isEmpty() ? null : list;
}
public static <T> RpcPagination<T> getRpcPagination(Class<T> conditionClass){
RpcPagination<T> rpcPagination = new RpcPagination<>();
RopRequestContext content = ThreadLocalCache.getRopRequestContext();
rpcPagination.setPageNum(1);
rpcPagination.setPageSize(10);
if(StringUtils.isNotBlank(content.getParamValue("currentPage"))){
rpcPagination.setPageNum(Integer.valueOf(content.getParamValue("currentPage")));
}else if(StringUtils.isNotBlank(content.getParamValue("pageNum"))){
rpcPagination.setPageNum(Integer.valueOf(content.getParamValue("pageNum")));
}
if(StringUtils.isNotBlank(content.getParamValue("pageSize"))){
rpcPagination.setPageSize(Integer.valueOf(content.getParamValue("pageSize")));
}
rpcPagination.setCondition(convertRequest(conditionClass));
return rpcPagination;
}
public static <T, K> RpcPagination<T> getPageResponse(RpcPagination<K> pagination) {
RpcPagination<T> resp = new RpcPagination<>();
resp.setPageSize(pagination.getPageSize());
resp.setPageNum(pagination.getPageNum());
resp.setTotal(pagination.getTotal());
resp.setPages(pagination.getPages());
resp.setStartRow(pagination.getStartRow());
resp.setEndRow(pagination.getEndRow());
resp.setList(new ArrayList<>());
return resp;
}
public static <T> T convertRequest(Class<T> clazz){
try {
RopRequestContext content = ThreadLocalCache.getRopRequestContext();
T bean = clazz.newInstance();
BeanUtils.copyProperties(content.getRopRequest(), bean);
return bean;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public static<T> RopResponse<T> convertRopResponse(RpcResponse<T> rpcRes){
return RopResponse.result(rpcRes);
}
public static<T,V> RopResponse<V> convertRopResponse(RpcResponse<T> rpcRes,ConvertHandler<T,V> handler){
if(rpcRes.isSuccess()) {
return RopResponse.success(handler.convert(rpcRes.getResult()));
}
return RopResponse.error(rpcRes);
}
public static <T,V> RopResponse<List<V>> convertListRopResponse(RpcResponse<List<T>> rpcRes,ConvertHandler<T,V> handler){
if(rpcRes.isSuccess()){
if(CollectionUtils.isEmpty(rpcRes.getResult())) {
return RopResponse.success(null);
}else{
return RopResponse.success(rpcRes.getResult().stream().map(handler::convert).collect(Collectors.toList()));
}
}
return RopResponse.error(rpcRes);
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!