BeanCopyUtil

import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;



/**
 * @author
 * @date
 */
public class BeanCopyUtil extends BeanUtils {
   private static final int ZERO = 0;
    /**
     * 集合数据的拷贝
     *
     * @param sources: 数据源类
     * @param target:  目标类::new(eg: UserVO::new)
     * @return
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
        return copyListProperties(sources, target, null);
    }


    /**
     * 带回调函数的集合数据的拷贝(可自定义字段拷贝规则)
     *
     * @param sources:  数据源类
     * @param target:   目标类::new(eg: UserVO::new)
     * @param callBack: 回调函数
     * @return
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack)  {
        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T t = target.get();
            copyProperties(source, t);
            list.add(t);
            if (callBack != null) {
                // 回调
                callBack.callBack(source, t);
            }
        }
        return list;
    }

    public static void saveBatch(List list, IService iService) {
        if (CollectionUtils.isNotEmpty(list)) {
            int pageSize = 1000;
            int pageNum = 1;
            int total = list.size();
            int pages = total / pageSize;
            if (total % pageSize != ZERO) {
                pages++;
            }
            while (pages >= pageNum) {
                //默认1000,为什么用1000?相信应该比较合理,故我们也用1000
                iService.saveBatch(list.subList(BeanCopyUtil.getStartIndex(pageNum, pageSize), BeanCopyUtil.getLastIndex(pageNum, pageSize, total)));
                pageNum++;
            }
        }
    }

    public static void saveOrUpdateBatch(List list, IService iService) {
        if (CollectionUtils.isNotEmpty(list)) {
            int pageSize = 1000;
            int pageNum = 1;
            int total = list.size();
            
            int pages = total / pageSize;
            if (total % pageSize != ZERO) {
                pages++;
            }
            while (pages >= pageNum) {
                //默认1000,为什么用1000?相信应该比较合理,故我们也用1000
                iService.saveOrUpdateBatch(list.subList(BeanCopyUtil.getStartIndex(pageNum, pageSize), BeanCopyUtil.getLastIndex(pageNum, pageSize, total)));
                pageNum++;
            }
        }
    }

    /**
     * 计算集合起始位置与结束位置
     */
    public static int getStartIndex(int pageNum, int pageSize) {
        return (pageNum - 1) * pageSize;
    }

    public static int getLastIndex(int pageNum, int pageSize, int total) {
        int num = pageNum * pageSize;
        return Math.min(num, total);
    }
}

 


@FunctionalInterface
public interface BeanCopyUtilCallBack<S, T> {
    /**
     * 定义默认回调方法
     *
     * @param t
     * @param s
     */
    void callBack(S t, T s) ;
}

 

posted @ 2022-04-13 09:56  甜菜波波  阅读(291)  评论(0编辑  收藏  举报