# ShiftEnum.java

    package cloud.wconnect.frontoffice.cons.endofshift;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import com.google.common.base.Enums;
    import lombok.AllArgsConstructor;
    import lombok.Getter;

    import java.util.Arrays;
    import java.util.Optional;

    /**
     * shift 排班类型枚举类
     *
     * @author zc
     * @date 2022/5/12
     */
    @AllArgsConstructor
    @Getter
    public enum ShiftEnum {

        /**
         * 早班
         */
        AM(1, "AM", 7, 15, "end-of-shift.am"),
        /**
         * 中班
         */
        PM(2, "PM", 15, 23, "end-of-shift.pm"),
        /**
         * 夜班
         */
        Night(3, "Night", 23, 7, "end-of-shift.night");

        /**
         * 排班code
         */
        private final int code;
        /**
         * 排班名称
         */
        private final String info;
        /**
         * 排班开始时间（几点）
         */
        private final int startHour;

        /**
         * 排班结束时间
         */
        private final int endHour;

        /**
         * 国际化code
         */
        private String i18nCode;

        public static String getInfoByCode(int code){
            ShiftEnum[] shiftEnums = ShiftEnum.values();
            for (ShiftEnum shiftEnum : shiftEnums) {
                if (shiftEnum.getCode() == code) {
                    return shiftEnum.getInfo();
                }
            }
            throw new BadRequestException(BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getI18nCode(),
                    BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "shift code not found");
        }

        /**
         * 查询排班码值
         *
         * @param info 排班info
         * @return int
         * @author lichen
         * @date 2023/12/8
         */
        public static int getCodeByInfo(String info) {
            ShiftEnum shiftEnum = Enums.getIfPresent(ShiftEnum.class, info).orNull();
            if (shiftEnum == null) {
                throw new BadRequestException(BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "the shift value is illegal");
            }
            return shiftEnum.getCode();
        }

        /**
         * 根据排班标识获取i18nCode
         *
         * @param code 排班标识
         * @return java.lang.String
         * @author lichen
         * @date 2024/1/17
         */
        public static String getI18nCodeByCode(int code) {
            Optional<ShiftEnum> optional = Arrays.stream(ShiftEnum.values()).filter(item -> item.getCode() == code).findFirst();
            return optional.isPresent() ? optional.get().getI18nCode() : "";
        }

        /**
         * 根据小时获取当前班次
         *
         * @param hour 小时
         * @return int
         * @author lichen
         * @date 2024/3/16
         */
        public static int getByHour(int hour){
            if(hour >= 7 && hour < 15) return AM.getCode();
            if(hour >= 15 && hour < 23) return PM.getCode();
            return Night.getCode();
        }

        /**
         * 获取上一个班次的code
         *
         * @param shiftCode 班次code
         * @return int
         * @author lichen
         * @date 2024/3/16
         */
        public static int getLastShift(int shiftCode){
            if(AM.getCode() == shiftCode) return Night.getCode();

            if(PM.getCode() == shiftCode) return AM.getCode();

            return PM.getCode();
        }

    }