package com.hdkj.lt.core.mvc;

import com.hdkj.hussar.ApiResponse;
import com.hdkj.hussar.IResultCode;
import com.hdkj.hussar.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description: web控制器的基类
 * @Author: wenhao
 * @Create: 2021/09/08 13:34
 * @Version: 1.0
 */
public class BaseController {

    protected final Logger LOGGER = LoggerFactory.getLogger(getClass());

    private HttpServletRequest request;

    private HttpServletResponse response;

    private HttpSession session;

    @ModelAttribute
    public void setReqAndResp(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }

    public <T> ApiResponse<T> data(T data) {
        return ApiResponse.success(data);
    }

    public <T> ApiResponse<T> data(IResultCode resultCode, T data) {
        return ApiResponse.success(resultCode.getCode(), data, resultCode.getMessage());
    }

    public <T> ApiResponse<T> data(T data, String msg) {
        return ApiResponse.success(data, msg);
    }

    public <T> ApiResponse<T> data(int code, T data, String msg) {
        return ApiResponse.success(code, data, msg);
    }

    public <T> ApiResponse<T> success() {
        return ApiResponse.success();
    }

    public <T> ApiResponse<T> success(String msg) {
        return ApiResponse.success(msg);
    }

    public <T> ApiResponse<T> success(IResultCode resultCode) {
        return ApiResponse.success(resultCode);
    }

    public <T> ApiResponse<T> success(IResultCode resultCode, String msg) {
        return ApiResponse.success(resultCode, msg);
    }

    public <T> ApiResponse<T> fail() {
        return ApiResponse.fail(ResultCode.FAILURE);
    }

    public <T> ApiResponse<T> fail(String msg) {
        return ApiResponse.fail(msg);
    }

    public <T> ApiResponse<T> fail(int code, String msg) {
        return ApiResponse.fail(code, msg);
    }

    public <T> ApiResponse<T> fail(IResultCode resultCode) {
        return ApiResponse.fail(resultCode);
    }

    public <T> ApiResponse<T> fail(IResultCode resultCode, String msg) {
        return ApiResponse.fail(resultCode, msg);
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

}
