package jnpf.util.mapper;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class MybatisUtil {

    /**
     * 根据字段查询
     * @param mapper 持久层DAO
     * @param c 字段
     * @param value 值
     * @param <T>
     * @return
     */
    public static <T> T findByFiled(BaseMapper<T> mapper, SFunction<T, ?> c, Object value, Boolean isDelete) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(c, value);
        if (isDelete) {
            queryWrapper.eq("F_DeleteMark", 0);
        }
        return mapper.selectOne(queryWrapper);
    }

    /**
     * 根据字段查询列表
     * @param mapper 持久层DAO
     * @param c 字段
     * @param value 值
     * @param <T>
     * @return
     */
    public static <T> List<T> findListByFiled(BaseMapper<T> mapper, SFunction<T, ?> c, Object value, Boolean hasDelete) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(c, value);
        if (hasDelete) {
            queryWrapper.eq("F_DeleteMark", 0);
        }
        return mapper.selectList(queryWrapper);
    }

    /**
     * 根据字段查询
     * @param mapper 持久层DAO
     * @param c 字段
     * @param value 值
     * @param <T>
     * @return
     */
    public static <T, V> Integer findCountByField(BaseMapper<T> mapper, SFunction<T, ?> c, V... value) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<T> lambdaQueryWrapper = queryWrapper.lambda();
        if (ArrayUtil.isNotEmpty(value)) {
            for (V v : value) {
                lambdaQueryWrapper.eq(c, value);
            }
        }
        return Integer.parseInt(String.valueOf(mapper.selectCount(queryWrapper)));
    }

    /**
     * 随机查询
     *
     * @param mapper 持久层DAO
     * @param limit  随机条数
     * @return java.util.List<T>
     * @since 2021/8/10 15:30
     */
    public static <T> List<T> getAny(BaseMapper<T> mapper, T condition, Integer limit) {
        LambdaQueryWrapper<T> wrapper = Wrappers.lambdaQuery(condition);
        Integer total = Integer.parseInt(String.valueOf(mapper.selectCount(wrapper)));
        if (limit == null || limit <= 0 || total == 0) {
            return Collections.emptyList();
        }
        List<T> list = Optional.of(limit).filter(l -> l > total).map(l -> mapper.selectList(wrapper)).orElseGet(() -> mapper.selectList(wrapper.last("LIMIT " + new SecureRandom().nextInt(total - (limit - 1)) + "," + limit)));
        Collections.shuffle(list);
        return list;
    }

    /**
     * 随机查询
     *
     * @param mapper 持久层DAO
     * @param limit  随机条数
     * @return java.util.List<T>
     * @since 2021/8/10 15:30
     */
    public static <T> List<T> getAny(BaseMapper<T> mapper, T condition, Integer limit, QueryWrapper<T> wrapper) {
//        LambdaQueryWrapper<T> wrapper = Wrappers.lambdaQuery(condition);
        Integer total = Integer.parseInt(String.valueOf(mapper.selectCount(wrapper)));
        if (limit == null || limit <= 0 || total == 0) {
            return Collections.emptyList();
        }
        List<T> list = Optional.of(limit).filter(l -> l > total).map(l -> mapper.selectList(wrapper)).orElseGet(() -> mapper.selectList(wrapper.last("LIMIT " + new SecureRandom().nextInt(total - (limit - 1)) + "," + limit)));
        Collections.shuffle(list);
        return list;
    }
}
