package cn.teammodel.utils;

import cn.teammodel.common.ErrorCode;
import cn.teammodel.config.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

import java.util.List;

/**
 * @author winter
 * @create 2023-11-29 15:41
 */
@Slf4j
public class RepositoryUtil {
    /**
    * 获取唯一一个结果,如果list为空或者list长度大于1,抛出参数错误异常
    */
    public static <T> T findOne(List<T> list, String errorMsg) {
        if (ObjectUtils.isEmpty(list) || list.size() > 1) {
            log.error("查询结果为空或数量 > 1");
            throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), errorMsg);
        }
        return list.get(0);
    }
}
