package com.zksy.common.utils;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.google.common.base.Function;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @version 1.0
 * @project credit-rating
 * @description 删除整张表
 * @date 2024/5/13 11:59:24
 */
@NoArgsConstructor
@AllArgsConstructor
public class DeleteTableUtils<T> {

    private T entity;
    /**
     * TODO
      * @param fun 一般传 类名::getId()
     * @param mapper 传实体类对应的mapper
     * @return void
     * @author Administrator
     * @date 2024/5/13 12:22:26
     */
    public <T> void del(Function<T, Long> fun, BaseMapper<T> mapper){
        List<T> list = mapper.selectList(null);
        if(list == null){
            throw new RuntimeException("没有数据");
        }
        List<Long> ids = list.stream().map(fun).collect(Collectors.toList());
        if(ids.size() == 0){
            throw new RuntimeException("没有数据");
        }
        mapper.deleteBatchIds(ids);
    }
}
