package com.xydl.cac.controller;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public abstract class BasicController {

    protected Integer initPageNum(Integer pageNum) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageNum < 1) {
            pageNum = 1;
        }
        return pageNum;
    }

    protected Integer initPageSize(Integer pageSize) {
        if (pageSize == null) {
            pageSize = 10;
        }
        if (pageSize < 1) {
            pageSize = 1;
        }
        return pageSize;
    }

    /**
     * 获取用户邮箱
     *
     * @param request 请求
     * @return String
     */
    protected String getUserEmail(HttpServletRequest request) {
        return request.getHeader("email");
    }

    /**
     * 获取用户登录方式
     *
     * @param request 请求
     * @return String
     */
    protected String getLoginMethod(HttpServletRequest request) {
        return request.getHeader("login_method");
    }

    /**
     * 获取用户登录IP
     *
     * @param request 请求
     * @return String
     */
    protected String getClientIp(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        return request.getHeader("client_ip");
    }
}
