# OrderByFieldEnum.java

    package cloud.wconnect.frontoffice.cons.badguest;

    import cloud.wconnect.frontoffice.entity.badguest.BadGuest;
    import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
    import lombok.AllArgsConstructor;
    import lombok.Getter;
    import org.apache.commons.lang3.StringUtils;

    import java.util.Arrays;
    import java.util.Optional;
    import java.util.stream.Stream;

    /**
     * @author lichen
     * @description 排序字段
     * @date 2023/3/23 10:03
     */
    @Getter
    @AllArgsConstructor
    public enum OrderByFieldEnum {

        ID("id", BadGuest::getId),
        LASTNAME("last_name", BadGuest::getLastName),
        CHECKINDATE("check_in_date", BadGuest::getCheckInDate);

        private final String field;

        private final SFunction<BadGuest, Object> sFunction;

        /**
         * @param field
         * @return boolean
         * @description field 是否存在
         * @author lichen
         * @date 2023/3/23 10:07
         */
        public static boolean isExist(String field) {
            OrderByFieldEnum[] values = OrderByFieldEnum.values();
            return Stream.of(values).filter(v -> v.getField().equals(field)).findAny().isPresent();
        }

        /**
         * 获取SFunction
         *
         * @param field 字段名称
         * @return com.baomidou.mybatisplus.core.toolkit.support.SFunction<cloud.wconnect.frontoffice.entity.badguest.BadGuest, java.lang.Object>
         * @author lichen
         * @date 2024/4/22
         */
        public static SFunction<BadGuest, Object> getSFunction(String field) {
            if (StringUtils.isEmpty(field)) return OrderByFieldEnum.ID.sFunction;
            Optional<OrderByFieldEnum> optional = Arrays.stream(OrderByFieldEnum.values()).filter(item -> item.getField().equals(field)).findAny();
            return optional.isPresent() ? optional.get().getSFunction() : OrderByFieldEnum.ID.sFunction;
        }

    }